ts-openapi-contract-first-boilerplate
Version:
A boilerplate for building contract-first APIs in TypeScript with OpenAPI. Includes OpenAPI definition, compliant server setup, and npm package generation for seamless API development.
27 lines (26 loc) • 821 B
JavaScript
import createClient from "openapi-fetch";
export class ContractFirstAPIClient {
constructor(baseUrl) {
this.authenticate = {
onRequest: ({ request }) => {
if (this.authentication) {
request.headers.set('Authorization', `Bearer ${this.authentication}`);
}
return request;
}
};
this.auth = {
set: (access_token) => {
this.authentication = access_token;
},
get: () => {
return this.authentication;
}
};
this.client = createClient({ baseUrl, headers: { Bearer: 'access_token' } });
this.client.use(this.authenticate);
}
healthCheck() {
return this.client.GET('/health-check');
}
}