-
Notifications
You must be signed in to change notification settings - Fork 0
/
KlipperClient.ts
290 lines (239 loc) · 10.8 KB
/
KlipperClient.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* This file is part of the Klipper package.
*
* (c) François Pluchino <francois.pluchino@klipper.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import {Canceler} from '@klipper/http-client/Canceler';
import {BatchResultListResponse} from '@klipper/http-client/models/responses/BatchResultListResponse';
import {ListResponse} from '@klipper/http-client/models/responses/ListResponse';
import {ServiceNotFoundError} from '@klipper/sdk/errors/ServiceNotFoundError';
import {removeEmptyRequestAuth} from '@klipper/sdk/interceptors';
import {KlipperClientConfig} from '@klipper/sdk/KlipperClientConfig';
import {OauthConfig} from '@klipper/sdk/OauthConfig';
import {BatchRequestConfig} from '@klipper/sdk/requests/BatchRequestConfig';
import {CancelerCancelToken} from '@klipper/sdk/requests/CancelerCancelToken';
import {CommonRequestConfig} from '@klipper/sdk/requests/CommonRequestConfig';
import {DeleteRequestConfig} from '@klipper/sdk/requests/DeleteRequestConfig';
import {ListRequestConfig} from '@klipper/sdk/requests/ListRequestConfig';
import {MetadataRequestConfig} from '@klipper/sdk/requests/MetadataRequestConfig';
import {RequestConfigItem} from '@klipper/sdk/requests/RequestConfigItem';
import {RequestConfigType} from '@klipper/sdk/requests/RequestConfigTypes';
import {SearchRequestConfig} from '@klipper/sdk/requests/SearchRequestConfig';
import {Sort} from '@klipper/sdk/requests/Sort';
import {Service, ServiceConstructor} from '@klipper/sdk/Service';
import {Authorization} from '@klipper/sdk/services/Authorization';
import {Intl} from '@klipper/sdk/services/Intl';
import {Metadata} from '@klipper/sdk/services/Metadata';
import {createApiError} from '@klipper/sdk/utils/error';
import axios, {AxiosInstance, AxiosRequestConfig, AxiosResponse} from 'axios';
const SERVICES: ServiceConstructor[] = [
Authorization,
Intl,
Metadata,
];
/**
* @author François Pluchino <francois.pluchino@klipper.dev>
*/
export class KlipperClient {
private readonly axios: AxiosInstance;
public readonly oauthConfig: OauthConfig;
private readonly services: Record<string, Service> = {};
constructor(config: KlipperClientConfig) {
const axiosConfig = config.axiosConfig || {};
axiosConfig.baseURL = config.baseUrl;
axiosConfig.timeout = config.timeout;
axiosConfig.maxRedirects = axiosConfig.maxRedirects || axiosConfig.maxRedirects;
axiosConfig.headers = Object.assign({}, {
'Accept': 'application/json',
'Content-Type': 'application/json',
}, config.headers || {});
this.axios = axios.create(axiosConfig);
this.oauthConfig = config.oauth;
const services = config.services ? config.services : SERVICES;
for (const service of services) {
this.add(service);
}
this.addRequestInterceptor(removeEmptyRequestAuth);
}
/**
* Add the api service.
*/
public add(service: ServiceConstructor): void {
if (typeof service === 'function' && service.getName()) {
this.services[service.getName()] = new service(this);
}
}
/**
* Get the api service.
*/
public get<T extends Service>(service: ServiceConstructor | string): T {
let name = null;
if (typeof service === 'string') {
name = service;
} else if (service.hasOwnProperty('getName')) {
name = (service as ServiceConstructor).getName();
} else {
throw new ServiceNotFoundError(service);
}
if (!this.services[name]) {
throw new ServiceNotFoundError(name);
}
return this.services[name] as T;
}
public getBaseUrl(): string {
return this.axios.defaults.baseURL as string;
}
/**
* Add a request interceptor.
*/
public addRequestInterceptor(onFulfilled?: (value: AxiosRequestConfig) => AxiosRequestConfig|Promise<AxiosRequestConfig>, onRejected?: (error: any) => any): number {
return this.axios.interceptors.request.use(onFulfilled, onRejected);
}
/**
* Add response interceptor.
*/
public addResponseInterceptor(onFulfilled?: (value: AxiosResponse) => AxiosResponse|Promise<AxiosResponse>, onRejected?: (error: any) => any): number {
return this.axios.interceptors.response.use(onFulfilled, onRejected);
}
/**
* Build and run requests in parallel.
*/
public async requestAll<T = any>(requestConfigs: RequestConfigItem[]): Promise<(T|null)[]> {
const requests = [] as Promise<AxiosResponse>[];
const response = [] as (T|null)[];
for (const requestConfig of requestConfigs) {
if (requestConfig.canceler) {
requestConfig.config.cancelToken = new axios.CancelToken((c: Function) => {
(requestConfig.canceler as Canceler).setExecutor(c);
});
}
KlipperClient.updateRequestConfig(requestConfig.config);
requests.push(this.axios.request<T>(requestConfig.config));
response.push(null);
}
try {
const results = await axios.all(requests);
for (let i = 0; i < results.length; ++i) {
response[i] = results[i] ? results[i].data : null;
}
} catch (e: any) {
if (!axios.isCancel(e)) {
throw await createApiError(e);
}
}
return response;
}
/**
* Build and run the request.
*/
public async request<T = Record<string, any>>(config: RequestConfigType, canceler?: Canceler): Promise<T|null> {
const res = await this.requestRaw<T>(config, canceler);
return res ? res.data : null;
}
/**
* Build and run the raw request.
*/
public async requestRaw<T = any>(config: RequestConfigType, canceler?: Canceler): Promise<AxiosResponse<T>|null> {
if (canceler) {
config.cancelToken = new axios.CancelToken((c: Function) => {
canceler.setExecutor(c);
});
(config.cancelToken as CancelerCancelToken).originalCanceler = canceler;
}
try {
KlipperClient.updateRequestConfig(config);
return await this.axios.request(config);
} catch (e: any) {
if (!axios.isCancel(e)) {
throw await createApiError(e);
}
}
return null;
}
/**
* Build and run the request.
*/
public async requestList<T = Record<string, any>>(config: ListRequestConfig|AxiosRequestConfig, canceler?: Canceler): Promise<ListResponse<T>> {
const res = await this.request<ListResponse<T>>(config, canceler);
return res ? res : {results: [], page: 0, limit: 0, pages: 0, total: 0} as ListResponse<T>;
}
/**
* Build and run the request for batch.
*/
public async requestBatch<T = Record<string, any>>(config: BatchRequestConfig|AxiosRequestConfig, canceler?: Canceler): Promise<BatchResultListResponse<T>> {
const res = await this.request<BatchResultListResponse<T>>(config, canceler);
return res ? res : {has_errors: false, status: 'successfully', records: []} as BatchResultListResponse<T>;
}
private static updateRequestConfig(config: RequestConfigType): void {
if (!config.method) {
config.method = 'GET';
}
if (undefined !== (config as ListRequestConfig).page) {
config.params = config.params || {};
config.params.page = (config as ListRequestConfig).page;
}
if (undefined !== (config as ListRequestConfig).limit) {
config.params = config.params || {};
config.params.limit = (config as ListRequestConfig).limit;
}
if (undefined !== (config as ListRequestConfig).sort) {
const sort: Sort|Sort[] = (config as ListRequestConfig).sort as Sort|Sort[];
config.headers = config.headers || {};
config.headers['X-Sort'] = (typeof sort === 'object' ? [sort as Sort] : sort).toString();
}
if (undefined !== (config as ListRequestConfig).filter) {
config.headers = config.headers || {};
config.headers['X-Filter'] = JSON.stringify((config as ListRequestConfig).filter);
}
if (undefined !== (config as ListRequestConfig).search) {
config.headers = config.headers || {};
config.headers['X-Search'] = (config as ListRequestConfig).search;
}
if (undefined !== (config as ListRequestConfig).searchFields) {
config.headers = config.headers || {};
config.headers['X-Search-Fields'] = ((config as ListRequestConfig).searchFields as string[]).join(',');
}
if (undefined !== (config as ListRequestConfig).viewsDetails) {
config.headers = config.headers || {};
config.headers['X-Views-Details'] = 'true';
}
if (undefined !== (config as CommonRequestConfig).fields) {
config.headers = config.headers || {};
config.headers['X-Fields'] = ((config as CommonRequestConfig).fields as string[]).join(',');
}
if (undefined !== (config as CommonRequestConfig).timezone) {
config.headers = config.headers || {};
config.headers['X-Timezone'] = (config as CommonRequestConfig).timezone;
}
if (undefined !== (config as CommonRequestConfig).acceptVersion) {
config.headers = config.headers || {};
config.headers['X-Accept-Version'] = (config as CommonRequestConfig).acceptVersion;
}
if ((config as DeleteRequestConfig).forceDelete) {
config.headers = config.headers || {};
config.headers['X-Force-Delete'] = 'true';
}
if ((config as BatchRequestConfig).transactional) {
config.headers = config.headers || {};
config.headers['X-Transactional'] = 'true';
}
if (undefined !== (config as SearchRequestConfig).query) {
config.headers = config.headers || {};
config.headers['X-Query'] = (config as SearchRequestConfig).query;
if (undefined !== (config as SearchRequestConfig).queryFields) {
config.headers = config.headers || {};
config.headers['X-Query-Fields'] = (config as SearchRequestConfig).queryFields;
}
if (undefined !== (config as SearchRequestConfig).objects) {
config.headers['X-Objects'] = ((config as SearchRequestConfig).objects as string[]).join(',');
}
}
if ((config as MetadataRequestConfig).metadataDetails) {
config.headers = config.headers || {};
config.headers['X-Metadata-Details'] = 'true';
}
}
}