@genee/rest
v1.4.3
Published
## Usage
Downloads
198
Readme
@genee/rest
Usage
import REST from '@genee/rest';
type UserData = {
id: number;
name: string;
};
class UserService extends REST {
private token: string;
construct() {
super('https://host/base/url/api');
}
// will be called only once before the very first request
protected async init() {
// you have to use rawRequest
this.token = await this.rawRequest(this.url('v1/token'));
}
// pass inited token to headers in each request
protected get additionalHeaders() {
return {
Authorization: `Bearer ${this.token}`;
}
}
// or pass inited token to queries in each request
protected get additionalQueries() {
return {
token: this.token;
}
}
}
(async () => {
const gateway = new REST('https://host/base/url/api');
const user = await gateway.post<UserData>('v1/user', {
name: 'Doe John'
});
})();