1.0.7 • Published 5 years ago
authenticated-json-fetch-client v1.0.7
authenticated-json-fetch-client
A TypeScript base class for building clients for fetch-based JSON APIs that are authenticated by a secret given in a param or header.
Installation
yarn add authenticated-json-fetch-client
# OR
npm install --save authenticated-json-fetch-client
Usage
import API from 'authenticated-json-fetch-client;
interface User {
id: number;
username: string;
}
export class MyAPI extends API {
constructor(secret: string, host = 'http://localhost:8000') {
const secretParamName = 'token';
// To include the secret param as an HTTP header, set `secretParamName` to `"header:Some-Header"`
// (or set it to `"header:"` which defaults to `"header:Authorization"`)
super({host, secret, secretParamName});
}
getAllUsers(): Promise<User[]> {
return this.GET<User[]>('/users');
}
}
Copyright 2019 Duncan Smith