A simple set of wrappers for RESTful calls
Перейти к файлу
microsoft-github-policy-service[bot] 0c05b450ee
Microsoft mandatory file (#100)
Co-authored-by: microsoft-github-policy-service[bot] <77245923+microsoft-github-policy-service[bot]@users.noreply.github.com>
2023-06-12 13:10:49 -07:00
karma Setup tests. Add basic GenericRestClient tests 2018-04-15 14:01:34 +03:00
src Removed SyncTasks from the project. Massively alterered tests to handle the new async behavior (#62) 2020-02-20 15:37:26 -07:00
test Removed SyncTasks from the project. Massively alterered tests to handle the new async behavior (#62) 2020-02-20 15:37:26 -07:00
.eslintrc 0.2.12: Publishing recent PR and updating all package versions, which pulled in a bunch of eslint changes that needed tweaking 2019-12-10 00:42:09 +01:00
.gitignore Misc cleanup 2016-11-02 15:57:33 -07:00
.npmignore Initial commit: Extracting out GenericRestClient, SimpleWebRequest, and ExponentialTime into a new SimpleRestClients reusable NPM module. 2016-11-02 15:52:44 -07:00
.travis.yml Migrate to ESLint from TSLint 2019-05-20 14:30:52 +03:00
CHANGELOG.md Make blockRequestUntil work with MaxSimultaneousRequests and handle e… (#35) 2018-08-31 13:35:02 -07:00
LICENSE Add tests 2018-05-14 11:46:15 +03:00
README.md Removed SyncTasks from the project. Massively alterered tests to handle the new async behavior (#62) 2020-02-20 15:37:26 -07:00
SECURITY.md Microsoft mandatory file (#100) 2023-06-12 13:10:49 -07:00
index.js Misc cleanup 2016-11-02 15:57:33 -07:00
karma.conf.js Setup tests. Add basic GenericRestClient tests 2018-04-15 14:01:34 +03:00
package-lock.json Bump ua-parser-js from 0.7.31 to 0.7.33 (#94) 2023-01-27 07:14:24 -08:00
package.json Bump json5, karma-webpack, ts-loader and webpack (#92) 2023-01-01 02:44:37 -07:00
tsconfig.json Removed SyncTasks from the project. Massively alterered tests to handle the new async behavior (#62) 2020-02-20 15:37:26 -07:00
tsconfig.test.json Setup tests. Add basic GenericRestClient tests 2018-04-15 14:01:34 +03:00

README.md

SimpleRestClients

GitHub license npm version Build Status npm downloads npm bundle size (minified) npm bundle size (minified + gzip)

A simple set of wrappers for RESTful calls.

Installation

npm install --save simplerestclients

SimpleRestClients consists of two modules:

SimpleWebRequest

Wraps a single web request. Takes an options structure with overrides for priorization, delays, retry logic, error handling, etc. Has an abort() method to cancel the request early (will result in a rejected promise from the start() method).

GenericRestClient

Wraps SimpleWebRequest for usage across a single RESTful service. In our codebase, we have several specific RESTful service interaction classes that each implement GenericRestClient so that all of the requests get the same error handling, authentication, header-setting, etc.

GenericRestClient Sample Usage

import { GenericRestClient, ApiCallOptions, Headers } from 'simplerestclients';

interface User {
    id: string;
    firstName: string;
    lastName: string;
}

class MyRestClient extends GenericRestClient {
    constructor(private _appId: string) {
        super('https://myhost.com/api/v1/');
    }

    // Override _getHeaders to append a custom header with the app ID.
    protected _getHeaders(options: ApiCallOptions): Headers {
        return { ...super._getHeaders(options), 'X-AppId': this._appId };
    }

    // Define public methods that expose the APIs provided through the REST service.
    getAllUsers(): Promise<User[]> {
        return this.performApiGet<User[]>('users');
    }

    getUserById(id: string): Promise<User> {
        return this.performApiGet<User>(`user/${ id }`);
    }

    setUser(user: User): Promise<void> {
        return this.performApiPut<void>(`user/${ user.id }`, user);
    }
}