-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.ts
114 lines (103 loc) · 3.27 KB
/
api.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import {Session} from "../index";
import * as rm from "typed-rest-client/RestClient";
import {IHeaders} from "typed-rest-client/Interfaces";
/**
* API response schema.
*/
interface IApiResponse {
/**
* The payload.
*/
payload?: string;
/**
* Response headers.
*/
headers?: { [key: string]: string };
/**
* Error message.
*/
error?: string;
}
/**
* An invalid API response.
*/
export class InvalidApiResponseError extends Error {
constructor(message?: string) {
super(message);
}
}
/**
* Executes a request at the given URL with the given input.
*
* Note: not a public API.
* @param session The {@link Session}
* @param url The API URL
* @param input The request body
*/
export async function sendRequest(session: Session, url: string, input: any): Promise<string> {
const headers: IHeaders = {
"Content-Type": "application/json",
"X-Api-Key": session.apiKey
};
if (session.jwtKey != undefined && session.jwtKey.length > 0) {
headers["X-Signature"] = session.generateSignature();
}
// Execute request
const response: rm.IRestResponse<IApiResponse> = await session.client.create(url, input, {
acceptHeader: "application/json",
additionalHeaders: headers
});
// Validate response and return
if (response.statusCode != 200) {
throw new InvalidApiResponseError("Bad HTTP status code " + response.statusCode);
}
if (response.result == null) {
throw new InvalidApiResponseError("Invalid API response");
}
if (response.result.error != undefined) {
throw new InvalidApiResponseError(response.result.error);
}
if (response.result.payload == undefined) {
throw new InvalidApiResponseError("No payload obtained from API");
}
return response.result.payload;
}
/**
* Executes a request at the given URL with the given input and returns both payload and headers.
*
* Note: not a public API.
* @param session The {@link Session}
* @param url The API URL
* @param input The request body
*/
export async function sendRequestWithHeaders(session: Session, url: string, input: any): Promise<{ payload: string, headers: { [key: string]: string } }> {
const headers: IHeaders = {
"Content-Type": "application/json",
"X-Api-Key": session.apiKey
};
if (session.jwtKey != undefined && session.jwtKey.length > 0) {
headers["X-Signature"] = session.generateSignature();
}
// Execute request
const response: rm.IRestResponse<IApiResponse> = await session.client.create(url, input, {
acceptHeader: "application/json",
additionalHeaders: headers
});
// Validate response
if (response.statusCode != 200) {
throw new InvalidApiResponseError("Bad HTTP status code " + response.statusCode);
}
if (response.result == null) {
throw new InvalidApiResponseError("Invalid API response");
}
if (response.result.error != undefined) {
throw new InvalidApiResponseError(response.result.error);
}
if (response.result.payload == undefined) {
throw new InvalidApiResponseError("No payload obtained from API");
}
return {
payload: response.result.payload,
headers: response.result.headers || {}
};
}