attlaz-client 1.17.4 → 1.17.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Http/Data/QueryString.js +5 -1
- package/dist/Http/HttpClient.js +29 -100
- package/package.json +1 -1
|
@@ -8,6 +8,10 @@ export class QueryString {
|
|
|
8
8
|
this.command = command;
|
|
9
9
|
}
|
|
10
10
|
set(parameter, value) {
|
|
11
|
+
const existingParam = this.parameters.find((param) => param.parameter === parameter);
|
|
12
|
+
if (existingParam !== undefined) {
|
|
13
|
+
throw new Error('Param already defined');
|
|
14
|
+
}
|
|
11
15
|
this.parameters.push({ parameter, value });
|
|
12
16
|
}
|
|
13
17
|
addPagination(pagination) {
|
|
@@ -41,7 +45,7 @@ export class QueryString {
|
|
|
41
45
|
if (Array.isArray(value)) {
|
|
42
46
|
value = value.join(',');
|
|
43
47
|
}
|
|
44
|
-
output.push(parameter.parameter + '=' + value);
|
|
48
|
+
output.push(parameter.parameter + '=' + encodeURIComponent(value));
|
|
45
49
|
}
|
|
46
50
|
if (this.command.includes('?')) {
|
|
47
51
|
return this.command + '&' + output.join('&');
|
package/dist/Http/HttpClient.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
import axios from 'axios';
|
|
1
|
+
import axios, { AxiosError } from 'axios';
|
|
3
2
|
import { HttpClientResponse } from './HttpClientResponse.js';
|
|
4
3
|
import { ClientError } from './ClientError.js';
|
|
5
4
|
export class HttpClient {
|
|
@@ -15,7 +14,6 @@ export class HttpClient {
|
|
|
15
14
|
static HTTP_TIMEOUT = 504;
|
|
16
15
|
static async request(request) {
|
|
17
16
|
const response = await HttpClient.axiosRequest(request);
|
|
18
|
-
// const response: HttpClientResponse = await HttpClient.superAgentRequest(request);
|
|
19
17
|
const error = ClientError.byStatus(response.status, response.statusText);
|
|
20
18
|
if (error !== null && error !== undefined) {
|
|
21
19
|
if (response.body !== null && response.body !== undefined && response.body.error !== null && response.body.error !== undefined && response.body.error.message !== null && response.body.error.message !== undefined) {
|
|
@@ -30,107 +28,38 @@ export class HttpClient {
|
|
|
30
28
|
url: request.getFullUrl(),
|
|
31
29
|
method: request.method,
|
|
32
30
|
headers: request.headers,
|
|
33
|
-
data: request.body,
|
|
34
31
|
};
|
|
32
|
+
if (request.body !== null && request.body !== undefined) {
|
|
33
|
+
requestConfig.data = request.body;
|
|
34
|
+
}
|
|
35
35
|
// TODO: should we share this instance between requests?
|
|
36
36
|
const axiosInstance = axios.create();
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
37
|
+
// TODo: should we catch axios error sand make them into ClientErrors?
|
|
38
|
+
try {
|
|
39
|
+
const rawResponse = await axiosInstance.request(requestConfig);
|
|
40
|
+
const httpResponse = new HttpClientResponse(rawResponse.status, rawResponse.statusText);
|
|
41
|
+
Object.keys(rawResponse.headers).forEach((header) => {
|
|
42
|
+
const headerValue = rawResponse.headers[header];
|
|
43
|
+
httpResponse.headers[header] = headerValue === null ? '' : headerValue;
|
|
44
|
+
});
|
|
45
|
+
httpResponse.body = await rawResponse.data;
|
|
46
|
+
return httpResponse;
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
if (e instanceof AxiosError) {
|
|
50
|
+
const status = e.status === undefined ? 500 : e.status;
|
|
51
|
+
const statusCode = e.code === undefined ? 'Error' : e.code;
|
|
52
|
+
const response = new HttpClientResponse(status, statusCode);
|
|
53
|
+
if (e.response !== undefined) {
|
|
54
|
+
response.body = e.response.data;
|
|
55
|
+
}
|
|
56
|
+
return response;
|
|
57
|
+
}
|
|
58
|
+
console.error('Unable to perform request', { error: e });
|
|
59
|
+
const response = new HttpClientResponse(500, '');
|
|
60
|
+
return response;
|
|
61
|
+
}
|
|
61
62
|
}
|
|
62
|
-
// private static async popsicleRequest(request: HttpClientRequest): Promise<HttpClientResponse> {
|
|
63
|
-
// const rawRequest: CommonRequestOptions<any> = {
|
|
64
|
-
// url: request.getFullUrl(),
|
|
65
|
-
// method: request.method,
|
|
66
|
-
// headers: request.headers,
|
|
67
|
-
// omitDefaultHeaders: true,
|
|
68
|
-
// body: request.body,
|
|
69
|
-
// } as CommonRequestOptions<any>;
|
|
70
|
-
// const rawResponse: PopsicleResponse = await fetch(request.getFullUrl(), rawRequest);
|
|
71
|
-
// const httpResponse: HttpClientResponse = new HttpClientResponse(rawResponse.status, rawResponse.statusText);
|
|
72
|
-
// for (const header of rawResponse.headers.keys()) {
|
|
73
|
-
// const headerValue: string | null = rawResponse.headers.get(header);
|
|
74
|
-
// httpResponse.headers[header] = headerValue === null ? '' : headerValue;
|
|
75
|
-
// }
|
|
76
|
-
//
|
|
77
|
-
// httpResponse.body = await rawResponse.text();
|
|
78
|
-
// if (HttpClient.isJson(httpResponse)) {
|
|
79
|
-
// let jsonData: object | null = null;
|
|
80
|
-
// if (httpResponse.body !== '') {
|
|
81
|
-
// try {
|
|
82
|
-
// jsonData = JSON.parse(httpResponse.body);
|
|
83
|
-
// } catch (e) {
|
|
84
|
-
// // console.error(, {statusText: httpResponse.statusText, error: e});
|
|
85
|
-
// throw new Error('Unable to parse response data to JSON');
|
|
86
|
-
// }
|
|
87
|
-
// }
|
|
88
|
-
// httpResponse.body = jsonData;
|
|
89
|
-
// } else {
|
|
90
|
-
// // response.body = rawData;
|
|
91
|
-
// }
|
|
92
|
-
//
|
|
93
|
-
//
|
|
94
|
-
// return httpResponse;
|
|
95
|
-
// }
|
|
96
|
-
// private static async superAgentRequest(request: HttpClientRequest): Promise<HttpClientResponse> {
|
|
97
|
-
//
|
|
98
|
-
//
|
|
99
|
-
// let x: SuperAgent.Request | null = null;
|
|
100
|
-
// if (request.method === HttpClientRequest.GET) {
|
|
101
|
-
// x = SuperAgent.get(request.getFullUrl());
|
|
102
|
-
// } else if (request.method === HttpClientRequest.POST) {
|
|
103
|
-
// x = SuperAgent.post(request.getFullUrl());
|
|
104
|
-
//
|
|
105
|
-
// } else if (request.method === HttpClientRequest.PUT) {
|
|
106
|
-
// x = SuperAgent.put(request.getFullUrl());
|
|
107
|
-
// } else {
|
|
108
|
-
// throw new Error('Unknown request method `' + request.method + '`');
|
|
109
|
-
// }
|
|
110
|
-
//
|
|
111
|
-
//
|
|
112
|
-
// if (x === null) {
|
|
113
|
-
// throw new Error('Hm');
|
|
114
|
-
// }
|
|
115
|
-
//
|
|
116
|
-
//
|
|
117
|
-
// for (const key in request.headers) {
|
|
118
|
-
// if (Object.prototype.hasOwnProperty.call(request.headers, key)) {
|
|
119
|
-
// console.log('Set ' + key + ': ' + request.headers[key]);
|
|
120
|
-
// x.set(key, request.headers[key] as string);
|
|
121
|
-
// }
|
|
122
|
-
// }
|
|
123
|
-
//
|
|
124
|
-
//
|
|
125
|
-
// const rawResponse: SuperAgent.Response = await x.send(request.body);
|
|
126
|
-
//
|
|
127
|
-
//
|
|
128
|
-
// const response: HttpClientResponse = new HttpClientResponse(rawResponse.statusCode, '');
|
|
129
|
-
// response.body = rawResponse.body;
|
|
130
|
-
// // TODO: append/validate headers
|
|
131
|
-
// response.headers = rawResponse.headers;
|
|
132
|
-
// return response;
|
|
133
|
-
// }
|
|
134
63
|
static isJson(response) {
|
|
135
64
|
return response.getContentType() === 'application/json';
|
|
136
65
|
}
|