@genee/rest
TypeScript icon, indicating that this package has built-in type declarations

1.4.0 • Public • Published

@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'
    });
})();

Readme

Keywords

none

Package Sidebar

Install

npm i @genee/rest

Weekly Downloads

18

Version

1.4.0

License

MIT

Unpacked Size

22.4 kB

Total Files

10

Last publish

Collaborators

  • iamfat