A simple set of wrappers for RESTful calls.
npm install --save simplerestclients
Wraps a single web request. Has lots of overrides for priorization, delays, retry logic, error handling, etc.
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.
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);
}
}