client-js/src/Reflect.ts
Victor Westerlund 278868984d
fix: ESM exports (#2)
* wip: 2023-09-20T13:43:23+0200 (1695210203)

* wip: 2023-09-20T12:53:34+0200 (1695207214)

* wip: 2023-09-20T12:53:34+0200 (1695207214)

* wip: 2023-11-07T14:37:18+0100 (1699364238)
2023-11-09 10:51:01 +01:00

45 lines
No EOL
1.1 KiB
TypeScript

import { Method } from "./Method.js";
// Export Method as importable directly from the Reflect.js ESM
export { Method };
// Connect to a Reflect API instance over HTTP
export default class Client {
private url: string;
private headers: object = {};
constructor(url: string, key: string|null = null) {
this.url = url;
// Use API key with requests if defined
if (key) {
this.setApiKey(key);
}
}
// Set API key to use for all requests
private setApiKey(key: string): void {
this.headers["Authorization"] = `Bearer ${key}`;
}
// Get fully qualified URL to endpoint
private getEndpoint(endpoint: string): string {
return this.url + endpoint;
}
// Call a Reflect API endpoint with method and optional payload
public async call(endpoint: string, method: Method, payload: any = null): Promise<Response> {
const options: object = {
method: Method[method],
headers: this.headers
}
// JSON stringify and append body to request if provided
if (payload) {
options["body"] = JSON.stringify(payload);
}
// Fetch and return Response object
return await fetch(this.getEndpoint(endpoint), options);
}
}