-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDopplerRestApiClientImpl.ts
67 lines (61 loc) · 1.91 KB
/
DopplerRestApiClientImpl.ts
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { Result } from "../abstractions/common/result-types";
import { AppConfiguration } from "../abstractions";
import {
DopplerRestApiClient,
Field,
} from "../abstractions/doppler-rest-api-client";
import { AxiosStatic, Method } from "axios";
import { AppSessionStateAccessor } from "../abstractions/app-session";
export class DopplerRestApiClientImpl implements DopplerRestApiClient {
private axios;
private appSessionStateAccessor;
constructor({
axiosStatic,
appSessionStateAccessor,
appConfiguration: { dopplerRestApiBaseUrl },
}: {
axiosStatic: AxiosStatic;
appSessionStateAccessor: AppSessionStateAccessor;
appConfiguration: Partial<AppConfiguration>;
}) {
this.axios = axiosStatic.create({
baseURL: dopplerRestApiBaseUrl,
});
this.appSessionStateAccessor = appSessionStateAccessor;
}
private getConnectionData() {
const connectionData =
this.appSessionStateAccessor.getCurrentSessionState();
if (connectionData.status !== "authenticated") {
throw new Error("Authenticated session required");
}
return {
accountName: connectionData.dopplerAccountName,
jwtToken: connectionData.jwtToken,
};
}
private request<T>(method: Method, url: string, data: unknown = undefined) {
const { accountName, jwtToken } = this.getConnectionData();
return this.axios.request<T>({
method,
url: `/accounts/${accountName}${url}`,
headers: { Authorization: `Bearer ${jwtToken}` },
data,
});
}
private GET<T>(url: string) {
return this.request<T>("GET", url);
}
async getFields(): Promise<Result<Field[]>> {
const response = await this.GET<any>(`/fields`);
return {
success: true,
// TODO: consider to sanitize and validate this response
value: response.data.items.map(({ name, predefined, type }: any) => ({
name,
predefined,
type,
})),
};
}
}