A simple set of wrappers for RESTful calls.
npm install --save simplerestclientsWraps 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).
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{GenericRestClient,ApiCallOptions,Headers}from'simplerestclients';interfaceUser{id: string;firstName: string;lastName: string;}classMyRestClientextendsGenericRestClient{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[]>{returnthis.performApiGet<User[]>('users');}getUserById(id: string): Promise<User>{returnthis.performApiGet<User>(`user/${id}`);}setUser(user: User): Promise<void>{returnthis.performApiPut<void>(`user/${user.id}`,user);}}