-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuronAPI.ts
More file actions
53 lines (44 loc) · 1.32 KB
/
NeuronAPI.ts
File metadata and controls
53 lines (44 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import secrets from '../secrets.json' with {type: 'json'};
export class NeuronAPI {
private API_BASE = 'https://neuron.serifhealth.com/api/rates/v1?network_template_ids=07c56f6b-82cd-44a4-af42-d570b6ae89c6&limit=1000&codes=99203';
private readonly API_KEY: string = '';
/**
* calls API for given endpoint
*
* In a real scenario, this method would take additional parameters, including,
* for example, the endpoint to append to API_BASE. Alternatively, we may
* build out further layers of abstraction, depending on repeated use cases.
*
* @param method
* @returns {Promise<Response>}
*/
protected async callApi (method = 'GET'): Promise<Response> {
if (!this.API_KEY) {
throw new Error('Missing API key');
}
const headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('X-API-KEY', this.API_KEY);
const response = await fetch(
this.API_BASE,
{
method,
headers,
}
);
if (!response.ok) {
throw new Error(`callApi status: ${response.status}`);
}
return response;
};
constructor () {
this.API_KEY = (secrets?.API_KEY ?? '');
}
async loadData (): Promise<Record<string, unknown>> {
const response = await this.callApi();
if (!response.ok) {
throw new Error(`loadJokes status: ${response.status}`);
}
return response.json();
};
}