A simple set of wrappers for RESTful calls
Перейти к файлу
Brent Erickson 3d402cf2f4 Publish 0.2.7 2019-01-28 13:27:01 -08:00
karma Setup tests. Add basic GenericRestClient tests 2018-04-15 14:01:34 +03:00
src remove cjs assert (#46) 2019-01-18 09:53:28 -07:00
test remove cjs assert (#46) 2019-01-18 09:53:28 -07: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 Fix Node.js version selection for Travis CI 2018-05-21 11:16:47 +02: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 remove useless type casting to any. improve README example. update deps. start to use npm's package-lock feature (#41) 2018-10-04 21:45:09 -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 Publish 0.2.7 2019-01-28 13:27:01 -08:00
package.json Publish 0.2.7 2019-01-28 13:27:01 -08:00
tsconfig.json fix xhr.send incompatible types 2018-07-05 16:19:44 +03:00
tsconfig.test.json Setup tests. Add basic GenericRestClient tests 2018-04-15 14:01:34 +03:00
tslint.json Feature/add default template parameters to web response (#24) 2018-04-24 18:43:31 -07:00

README.md

SimpleRestClients

GitHub license npm version npm downloads Build Status David David

A simple set of wrappers for RESTful calls.

Installation

npm install --save simplerestclients

SimpleRestClients consists of two modules:

SimpleWebRequest

Wraps a single web request. Has lots of overrides for priorization, delays, retry logic, error handling, etc.

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 * as SyncTasks from 'synctasks';
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(): SyncTasks.Promise<User[]> {
        return this.performApiGet<User[]>('users');
    }

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

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