2016-11-03 01:52:44 +03:00
|
|
|
# SimpleRestClients
|
|
|
|
|
2019-10-08 11:02:07 +03:00
|
|
|
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/Microsoft/SimpleRestClients/blob/master/LICENSE) [![npm version](https://img.shields.io/npm/v/simplerestclients.svg?style=flat-square)](https://www.npmjs.com/package/simplerestclients) [![Build Status](https://img.shields.io/travis/Microsoft/SimpleRestClients/master.svg?style=flat-square)](https://travis-ci.org/Microsoft/SimpleRestClients) [![npm downloads](https://img.shields.io/npm/dm/simplerestclients.svg?style=flat-square)](https://www.npmjs.com/package/simplerestclients) ![npm bundle size (minified)](https://img.shields.io/bundlephobia/min/simplerestclients.svg?style=flat-square) ![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/simplerestclients.svg?style=flat-square)
|
2018-05-14 11:45:25 +03:00
|
|
|
|
2018-10-05 07:45:09 +03:00
|
|
|
> A simple set of wrappers for RESTful calls.
|
2018-09-17 18:13:10 +03:00
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
|
|
|
```shell
|
|
|
|
npm install --save simplerestclients
|
|
|
|
```
|
2016-11-03 01:52:44 +03:00
|
|
|
|
2018-10-05 07:45:09 +03:00
|
|
|
## SimpleRestClients consists of two modules:
|
|
|
|
|
|
|
|
### `SimpleWebRequest`
|
2016-11-03 01:52:44 +03:00
|
|
|
|
2020-02-21 01:37:26 +03:00
|
|
|
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).
|
2016-11-03 01:52:44 +03:00
|
|
|
|
2018-10-05 07:45:09 +03:00
|
|
|
### `GenericRestClient`
|
2016-11-03 01:52:44 +03:00
|
|
|
|
|
|
|
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.
|
2018-04-08 19:21:29 +03:00
|
|
|
|
|
|
|
## GenericRestClient Sample Usage
|
|
|
|
|
2018-04-18 12:10:27 +03:00
|
|
|
```typescript
|
2018-10-05 07:45:09 +03:00
|
|
|
import { GenericRestClient, ApiCallOptions, Headers } from 'simplerestclients';
|
2018-04-08 19:21:29 +03:00
|
|
|
|
|
|
|
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.
|
2018-10-05 07:45:09 +03:00
|
|
|
protected _getHeaders(options: ApiCallOptions): Headers {
|
|
|
|
return { ...super._getHeaders(options), 'X-AppId': this._appId };
|
2018-04-08 19:21:29 +03:00
|
|
|
}
|
|
|
|
|
2018-10-05 07:45:09 +03:00
|
|
|
// Define public methods that expose the APIs provided through the REST service.
|
2020-02-21 01:37:26 +03:00
|
|
|
getAllUsers(): Promise<User[]> {
|
2018-04-08 19:21:29 +03:00
|
|
|
return this.performApiGet<User[]>('users');
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:37:26 +03:00
|
|
|
getUserById(id: string): Promise<User> {
|
2018-10-05 07:45:09 +03:00
|
|
|
return this.performApiGet<User>(`user/${ id }`);
|
2018-04-08 19:21:29 +03:00
|
|
|
}
|
|
|
|
|
2020-02-21 01:37:26 +03:00
|
|
|
setUser(user: User): Promise<void> {
|
2018-10-05 07:45:09 +03:00
|
|
|
return this.performApiPut<void>(`user/${ user.id }`, user);
|
2018-04-08 19:21:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|