@lokalise/node-core 10.0.0-RC1 → 10.0.1
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/README.md +16 -60
- package/dist/index.d.ts +12 -7
- package/dist/index.js +19 -14
- package/dist/index.js.map +1 -1
- package/dist/src/errors/InternalError.d.ts +1 -0
- package/dist/src/errors/InternalError.js +3 -1
- package/dist/src/errors/InternalError.js.map +1 -1
- package/dist/src/errors/PublicNonRecoverableError.d.ts +1 -0
- package/dist/src/errors/PublicNonRecoverableError.js +3 -1
- package/dist/src/errors/PublicNonRecoverableError.js.map +1 -1
- package/dist/src/errors/errorTypeGuards.d.ts +2 -2
- package/dist/src/errors/errorTypeGuards.js +5 -4
- package/dist/src/errors/errorTypeGuards.js.map +1 -1
- package/dist/src/errors/globalErrorHandler.d.ts +8 -7
- package/dist/src/errors/globalErrorHandler.js +15 -16
- package/dist/src/errors/globalErrorHandler.js.map +1 -1
- package/dist/src/errors/publicErrors.d.ts +5 -4
- package/dist/src/errors/publicErrors.js +15 -1
- package/dist/src/errors/publicErrors.js.map +1 -1
- package/dist/src/logging/commonLogger.d.ts +17 -0
- package/dist/src/logging/commonLogger.js +3 -0
- package/dist/src/logging/commonLogger.js.map +1 -0
- package/dist/src/logging/loggerConfigResolver.d.ts +3 -3
- package/dist/src/logging/loggerConfigResolver.js +16 -15
- package/dist/src/logging/loggerConfigResolver.js.map +1 -1
- package/dist/src/observability/observabilityTypes.d.ts +22 -0
- package/dist/src/observability/observabilityTypes.js +3 -0
- package/dist/src/observability/observabilityTypes.js.map +1 -0
- package/dist/src/utils/checksumUtils.d.ts +7 -0
- package/dist/src/utils/checksumUtils.js +38 -0
- package/dist/src/utils/checksumUtils.js.map +1 -0
- package/dist/src/utils/encryptionUtility.d.ts +21 -0
- package/dist/src/utils/encryptionUtility.js +58 -0
- package/dist/src/utils/encryptionUtility.js.map +1 -0
- package/dist/src/utils/hashUtils.d.ts +9 -0
- package/dist/src/utils/hashUtils.js +19 -0
- package/dist/src/utils/hashUtils.js.map +1 -0
- package/dist/src/utils/objectUtils.d.ts +17 -0
- package/dist/src/utils/objectUtils.js +58 -1
- package/dist/src/utils/objectUtils.js.map +1 -1
- package/dist/src/utils/streamUtils.d.ts +38 -0
- package/dist/src/utils/streamUtils.js +85 -0
- package/dist/src/utils/streamUtils.js.map +1 -0
- package/package.json +21 -26
- package/dist/index.cjs +0 -898
- package/dist/index.d.cts +0 -304
- package/dist/index.d.mts +0 -304
- package/dist/index.mjs +0 -848
- package/dist/src/config/ConfigScope.spec.d.ts +0 -1
- package/dist/src/config/ConfigScope.spec.js +0 -587
- package/dist/src/config/ConfigScope.spec.js.map +0 -1
- package/dist/src/config/configTransformers.spec.d.ts +0 -1
- package/dist/src/config/configTransformers.spec.js +0 -28
- package/dist/src/config/configTransformers.spec.js.map +0 -1
- package/dist/src/errors/PublicNonRecoverableError.spec.d.ts +0 -1
- package/dist/src/errors/PublicNonRecoverableError.spec.js +0 -13
- package/dist/src/errors/PublicNonRecoverableError.spec.js.map +0 -1
- package/dist/src/errors/ResponseStatusError.d.ts +0 -7
- package/dist/src/errors/ResponseStatusError.js +0 -27
- package/dist/src/errors/ResponseStatusError.js.map +0 -1
- package/dist/src/errors/errorTypeGuards.spec.d.ts +0 -1
- package/dist/src/errors/errorTypeGuards.spec.js +0 -23
- package/dist/src/errors/errorTypeGuards.spec.js.map +0 -1
- package/dist/src/errors/globalErrorHandler.spec.d.ts +0 -1
- package/dist/src/errors/globalErrorHandler.spec.js +0 -30
- package/dist/src/errors/globalErrorHandler.spec.js.map +0 -1
- package/dist/src/http/httpClient.d.ts +0 -50
- package/dist/src/http/httpClient.js +0 -180
- package/dist/src/http/httpClient.js.map +0 -1
- package/dist/src/http/httpClient.spec.d.ts +0 -1
- package/dist/src/http/httpClient.spec.js +0 -777
- package/dist/src/http/httpClient.spec.js.map +0 -1
- package/dist/src/http/mock-data/mockProduct1.json +0 -12
- package/dist/src/http/mock-data/mockProductsLimit3.json +0 -38
- package/dist/src/logging/loggerConfigResolver.spec.d.ts +0 -1
- package/dist/src/logging/loggerConfigResolver.spec.js +0 -34
- package/dist/src/logging/loggerConfigResolver.spec.js.map +0 -1
- package/dist/src/utils/arrayUtils.spec.d.ts +0 -1
- package/dist/src/utils/arrayUtils.spec.js +0 -60
- package/dist/src/utils/arrayUtils.spec.js.map +0 -1
- package/dist/src/utils/objectUtils.spec.d.ts +0 -1
- package/dist/src/utils/objectUtils.spec.js +0 -612
- package/dist/src/utils/objectUtils.spec.js.map +0 -1
- package/dist/src/utils/typeUtils.spec.d.ts +0 -1
- package/dist/src/utils/typeUtils.spec.js +0 -115
- package/dist/src/utils/typeUtils.spec.js.map +0 -1
- package/dist/src/utils/waitUtils.spec.d.ts +0 -1
- package/dist/src/utils/waitUtils.spec.js +0 -50
- package/dist/src/utils/waitUtils.spec.js.map +0 -1
- package/dist/vitest.config.d.ts +0 -2
- package/dist/vitest.config.js +0 -34
- package/dist/vitest.config.js.map +0 -1
|
@@ -1,777 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
const undici_1 = require("undici");
|
|
5
|
-
const undici_retry_1 = require("undici-retry");
|
|
6
|
-
const zod_1 = require("zod");
|
|
7
|
-
const httpClient_1 = require("./httpClient");
|
|
8
|
-
const mockProduct1_json_1 = tslib_1.__importDefault(require("./mock-data/mockProduct1.json"));
|
|
9
|
-
const mockProductsLimit3_json_1 = tslib_1.__importDefault(require("./mock-data/mockProductsLimit3.json"));
|
|
10
|
-
const JSON_HEADERS = {
|
|
11
|
-
'content-type': 'application/json',
|
|
12
|
-
};
|
|
13
|
-
const TEXT_HEADERS = {
|
|
14
|
-
'content-type': 'text/plain',
|
|
15
|
-
};
|
|
16
|
-
const baseUrl = 'https://fakestoreapi.com';
|
|
17
|
-
const reqContext = {
|
|
18
|
-
reqId: 'dummyId',
|
|
19
|
-
};
|
|
20
|
-
describe('httpClient', () => {
|
|
21
|
-
let mockAgent;
|
|
22
|
-
let client;
|
|
23
|
-
beforeEach(() => {
|
|
24
|
-
mockAgent = new undici_1.MockAgent();
|
|
25
|
-
mockAgent.disableNetConnect();
|
|
26
|
-
(0, undici_1.setGlobalDispatcher)(mockAgent);
|
|
27
|
-
client = mockAgent.get(baseUrl);
|
|
28
|
-
});
|
|
29
|
-
describe('buildClient', () => {
|
|
30
|
-
it('creates a client', () => {
|
|
31
|
-
const client = (0, httpClient_1.buildClient)(baseUrl);
|
|
32
|
-
expect(client).toBeInstanceOf(undici_1.Client);
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
describe('GET', () => {
|
|
36
|
-
it('validates response structure with provided schema, throws an error', async () => {
|
|
37
|
-
const schema = zod_1.z.object({
|
|
38
|
-
id: zod_1.z.string(),
|
|
39
|
-
});
|
|
40
|
-
client
|
|
41
|
-
.intercept({
|
|
42
|
-
path: '/products/1',
|
|
43
|
-
method: 'GET',
|
|
44
|
-
})
|
|
45
|
-
.reply(200, mockProduct1_json_1.default, { headers: JSON_HEADERS });
|
|
46
|
-
await expect((0, httpClient_1.sendGet)(client, '/products/1', {
|
|
47
|
-
responseSchema: schema,
|
|
48
|
-
reqContext,
|
|
49
|
-
validateResponse: true,
|
|
50
|
-
})).rejects.toThrow(/Expected string, received number/);
|
|
51
|
-
});
|
|
52
|
-
it('validates response structure with provided schema, passes validation', async () => {
|
|
53
|
-
const schema = zod_1.z.object({
|
|
54
|
-
category: zod_1.z.string(),
|
|
55
|
-
description: zod_1.z.string(),
|
|
56
|
-
id: zod_1.z.number(),
|
|
57
|
-
image: zod_1.z.string(),
|
|
58
|
-
price: zod_1.z.number(),
|
|
59
|
-
rating: zod_1.z.object({
|
|
60
|
-
count: zod_1.z.number(),
|
|
61
|
-
rate: zod_1.z.number(),
|
|
62
|
-
}),
|
|
63
|
-
title: zod_1.z.string(),
|
|
64
|
-
});
|
|
65
|
-
client
|
|
66
|
-
.intercept({
|
|
67
|
-
path: '/products/1',
|
|
68
|
-
method: 'GET',
|
|
69
|
-
})
|
|
70
|
-
.reply(200, mockProduct1_json_1.default, { headers: JSON_HEADERS });
|
|
71
|
-
const result = await (0, httpClient_1.sendGet)(client, '/products/1', {
|
|
72
|
-
responseSchema: schema,
|
|
73
|
-
validateResponse: true,
|
|
74
|
-
});
|
|
75
|
-
expect(result.result.body).toEqual(mockProduct1_json_1.default);
|
|
76
|
-
});
|
|
77
|
-
it('validates response structure with provided schema, skips validation', async () => {
|
|
78
|
-
const schema = zod_1.z.object({
|
|
79
|
-
id: zod_1.z.string(),
|
|
80
|
-
});
|
|
81
|
-
client
|
|
82
|
-
.intercept({
|
|
83
|
-
path: '/products/1',
|
|
84
|
-
method: 'GET',
|
|
85
|
-
})
|
|
86
|
-
.reply(200, mockProduct1_json_1.default, { headers: JSON_HEADERS });
|
|
87
|
-
const result = await (0, httpClient_1.sendGet)(client, '/products/1', {
|
|
88
|
-
responseSchema: schema,
|
|
89
|
-
validateResponse: false,
|
|
90
|
-
});
|
|
91
|
-
expect(result.result.body).toEqual(mockProduct1_json_1.default);
|
|
92
|
-
});
|
|
93
|
-
it('validates response structure with provided schema, no validation specified', async () => {
|
|
94
|
-
client
|
|
95
|
-
.intercept({
|
|
96
|
-
path: '/products/1',
|
|
97
|
-
method: 'GET',
|
|
98
|
-
})
|
|
99
|
-
.reply(200, mockProduct1_json_1.default, { headers: JSON_HEADERS });
|
|
100
|
-
const result = await (0, httpClient_1.sendGet)(client, '/products/1', {
|
|
101
|
-
validateResponse: true,
|
|
102
|
-
});
|
|
103
|
-
expect(result.result.body).toEqual(mockProduct1_json_1.default);
|
|
104
|
-
});
|
|
105
|
-
it('returns original payload when breaking during parsing and throw on error is true', async () => {
|
|
106
|
-
expect.assertions(1);
|
|
107
|
-
client
|
|
108
|
-
.intercept({
|
|
109
|
-
path: '/products/1',
|
|
110
|
-
method: 'GET',
|
|
111
|
-
})
|
|
112
|
-
.reply(200, 'this is not a real json', { headers: JSON_HEADERS });
|
|
113
|
-
try {
|
|
114
|
-
await (0, httpClient_1.sendGet)(client, '/products/1', {
|
|
115
|
-
throwOnError: true,
|
|
116
|
-
safeParseJson: true,
|
|
117
|
-
requestLabel: 'label',
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
catch (err) {
|
|
121
|
-
// This is needed, because built-in error assertions do not assert nested fields
|
|
122
|
-
// eslint-disable-next-line vitest/no-conditional-expect
|
|
123
|
-
expect(err).toMatchObject({
|
|
124
|
-
message: 'Error while parsing HTTP JSON response',
|
|
125
|
-
errorCode: 'INVALID_HTTP_RESPONSE_JSON',
|
|
126
|
-
details: {
|
|
127
|
-
rawBody: 'this is not a real json',
|
|
128
|
-
requestLabel: 'label',
|
|
129
|
-
},
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
it('does not throw if broken during parsing but throwOnError is false', async () => {
|
|
134
|
-
expect.assertions(1);
|
|
135
|
-
client
|
|
136
|
-
.intercept({
|
|
137
|
-
path: '/products/1',
|
|
138
|
-
method: 'GET',
|
|
139
|
-
})
|
|
140
|
-
.reply(200, 'this is not a real json', { headers: JSON_HEADERS });
|
|
141
|
-
const result = await (0, httpClient_1.sendGet)(client, '/products/1', {
|
|
142
|
-
throwOnError: false,
|
|
143
|
-
safeParseJson: true,
|
|
144
|
-
requestLabel: 'label',
|
|
145
|
-
});
|
|
146
|
-
expect(result.error).toMatchObject({
|
|
147
|
-
message: 'Error while parsing HTTP JSON response',
|
|
148
|
-
errorCode: 'INVALID_HTTP_RESPONSE_JSON',
|
|
149
|
-
details: {
|
|
150
|
-
rawBody: 'this is not a real json',
|
|
151
|
-
requestLabel: 'label',
|
|
152
|
-
},
|
|
153
|
-
});
|
|
154
|
-
});
|
|
155
|
-
it('GET without queryParams', async () => {
|
|
156
|
-
client
|
|
157
|
-
.intercept({
|
|
158
|
-
path: '/products/1',
|
|
159
|
-
method: 'GET',
|
|
160
|
-
})
|
|
161
|
-
.reply(200, mockProduct1_json_1.default, { headers: JSON_HEADERS });
|
|
162
|
-
const result = await (0, httpClient_1.sendGet)(client, '/products/1');
|
|
163
|
-
expect(result.result.body).toEqual(mockProduct1_json_1.default);
|
|
164
|
-
});
|
|
165
|
-
it('GET returning text', async () => {
|
|
166
|
-
client
|
|
167
|
-
.intercept({
|
|
168
|
-
path: '/products/1',
|
|
169
|
-
method: 'GET',
|
|
170
|
-
})
|
|
171
|
-
.reply(200, 'just text', {
|
|
172
|
-
headers: TEXT_HEADERS,
|
|
173
|
-
});
|
|
174
|
-
const result = await (0, httpClient_1.sendGet)(client, '/products/1');
|
|
175
|
-
expect(result.result.body).toBe('just text');
|
|
176
|
-
});
|
|
177
|
-
it('GET returning text without content type', async () => {
|
|
178
|
-
client
|
|
179
|
-
.intercept({
|
|
180
|
-
path: '/products/1',
|
|
181
|
-
method: 'GET',
|
|
182
|
-
})
|
|
183
|
-
.reply(200, 'just text', {});
|
|
184
|
-
const result = await (0, httpClient_1.sendGet)(client, '/products/1');
|
|
185
|
-
expect(result.result.body).toBe('just text');
|
|
186
|
-
});
|
|
187
|
-
it('GET with queryParams', async () => {
|
|
188
|
-
const query = {
|
|
189
|
-
limit: 3,
|
|
190
|
-
};
|
|
191
|
-
client
|
|
192
|
-
.intercept({
|
|
193
|
-
path: '/products',
|
|
194
|
-
method: 'GET',
|
|
195
|
-
query,
|
|
196
|
-
})
|
|
197
|
-
.reply(200, mockProductsLimit3_json_1.default, { headers: JSON_HEADERS });
|
|
198
|
-
const result = await (0, httpClient_1.sendGet)(client, '/products', {
|
|
199
|
-
query,
|
|
200
|
-
});
|
|
201
|
-
expect(result.result.body).toEqual(mockProductsLimit3_json_1.default);
|
|
202
|
-
});
|
|
203
|
-
it('Throws an error on internal error', async () => {
|
|
204
|
-
expect.assertions(1);
|
|
205
|
-
const query = {
|
|
206
|
-
limit: 3,
|
|
207
|
-
};
|
|
208
|
-
client
|
|
209
|
-
.intercept({
|
|
210
|
-
path: '/products',
|
|
211
|
-
method: 'GET',
|
|
212
|
-
query,
|
|
213
|
-
})
|
|
214
|
-
.replyWithError(new Error('connection error'));
|
|
215
|
-
await expect((0, httpClient_1.sendGet)(client, '/products', {
|
|
216
|
-
query,
|
|
217
|
-
})).rejects.toMatchObject({
|
|
218
|
-
message: 'connection error',
|
|
219
|
-
});
|
|
220
|
-
});
|
|
221
|
-
it('Throws an error with a label on internal error', async () => {
|
|
222
|
-
expect.assertions(2);
|
|
223
|
-
const query = {
|
|
224
|
-
limit: 3,
|
|
225
|
-
};
|
|
226
|
-
try {
|
|
227
|
-
await (0, httpClient_1.sendGet)((0, httpClient_1.buildClient)('http://127.0.0.1:999'), '/dummy', {
|
|
228
|
-
requestLabel: 'label',
|
|
229
|
-
throwOnError: true,
|
|
230
|
-
query,
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
catch (err) {
|
|
234
|
-
if (!(0, undici_retry_1.isInternalRequestError)(err)) {
|
|
235
|
-
throw new Error('Invalid error type');
|
|
236
|
-
}
|
|
237
|
-
expect(err.message).toBe('connect ECONNREFUSED 127.0.0.1:999');
|
|
238
|
-
expect(err.requestLabel).toBe('label');
|
|
239
|
-
}
|
|
240
|
-
});
|
|
241
|
-
it('Returns error response', async () => {
|
|
242
|
-
expect.assertions(1);
|
|
243
|
-
const query = {
|
|
244
|
-
limit: 3,
|
|
245
|
-
};
|
|
246
|
-
client
|
|
247
|
-
.intercept({
|
|
248
|
-
path: '/products',
|
|
249
|
-
method: 'GET',
|
|
250
|
-
query,
|
|
251
|
-
})
|
|
252
|
-
.reply(400, 'Invalid request');
|
|
253
|
-
await expect((0, httpClient_1.sendGet)(client, '/products', {
|
|
254
|
-
query,
|
|
255
|
-
requestLabel: 'label',
|
|
256
|
-
})).rejects.toMatchObject({
|
|
257
|
-
message: 'Response status code 400',
|
|
258
|
-
response: {
|
|
259
|
-
body: 'Invalid request',
|
|
260
|
-
statusCode: 400,
|
|
261
|
-
},
|
|
262
|
-
details: {
|
|
263
|
-
requestLabel: 'label',
|
|
264
|
-
response: {
|
|
265
|
-
body: 'Invalid request',
|
|
266
|
-
statusCode: 400,
|
|
267
|
-
},
|
|
268
|
-
},
|
|
269
|
-
});
|
|
270
|
-
});
|
|
271
|
-
it('Works with retry', async () => {
|
|
272
|
-
expect.assertions(1);
|
|
273
|
-
const query = {
|
|
274
|
-
limit: 3,
|
|
275
|
-
};
|
|
276
|
-
client
|
|
277
|
-
.intercept({
|
|
278
|
-
path: '/products',
|
|
279
|
-
method: 'GET',
|
|
280
|
-
query,
|
|
281
|
-
})
|
|
282
|
-
.reply(500, 'Invalid request');
|
|
283
|
-
client
|
|
284
|
-
.intercept({
|
|
285
|
-
path: '/products',
|
|
286
|
-
method: 'GET',
|
|
287
|
-
query,
|
|
288
|
-
})
|
|
289
|
-
.reply(200, 'OK');
|
|
290
|
-
const response = await (0, httpClient_1.sendGet)(client, '/products', {
|
|
291
|
-
query,
|
|
292
|
-
retryConfig: {
|
|
293
|
-
statusCodesToRetry: [500],
|
|
294
|
-
retryOnTimeout: false,
|
|
295
|
-
delayBetweenAttemptsInMsecs: 0,
|
|
296
|
-
maxAttempts: 2,
|
|
297
|
-
},
|
|
298
|
-
});
|
|
299
|
-
expect(response.result.body).toBe('OK');
|
|
300
|
-
});
|
|
301
|
-
});
|
|
302
|
-
describe('DELETE', () => {
|
|
303
|
-
it('DELETE without queryParams', async () => {
|
|
304
|
-
client
|
|
305
|
-
.intercept({
|
|
306
|
-
path: '/products/1',
|
|
307
|
-
method: 'DELETE',
|
|
308
|
-
})
|
|
309
|
-
.reply(204, undefined, { headers: TEXT_HEADERS });
|
|
310
|
-
const result = await (0, httpClient_1.sendDelete)(client, '/products/1', {
|
|
311
|
-
reqContext,
|
|
312
|
-
});
|
|
313
|
-
expect(result.result.statusCode).toBe(204);
|
|
314
|
-
expect(result.result.body).toBe('');
|
|
315
|
-
});
|
|
316
|
-
it('DELETE with queryParams', async () => {
|
|
317
|
-
const query = {
|
|
318
|
-
limit: 3,
|
|
319
|
-
};
|
|
320
|
-
client
|
|
321
|
-
.intercept({
|
|
322
|
-
path: '/products',
|
|
323
|
-
method: 'DELETE',
|
|
324
|
-
query,
|
|
325
|
-
})
|
|
326
|
-
.reply(204, undefined, { headers: TEXT_HEADERS });
|
|
327
|
-
const result = await (0, httpClient_1.sendDelete)(client, '/products', {
|
|
328
|
-
query,
|
|
329
|
-
});
|
|
330
|
-
expect(result.result.statusCode).toBe(204);
|
|
331
|
-
expect(result.result.body).toBe('');
|
|
332
|
-
});
|
|
333
|
-
it('Throws an error on internal error', async () => {
|
|
334
|
-
expect.assertions(1);
|
|
335
|
-
const query = {
|
|
336
|
-
limit: 3,
|
|
337
|
-
};
|
|
338
|
-
client
|
|
339
|
-
.intercept({
|
|
340
|
-
path: '/products',
|
|
341
|
-
method: 'DELETE',
|
|
342
|
-
query,
|
|
343
|
-
})
|
|
344
|
-
.replyWithError(new Error('connection error'));
|
|
345
|
-
await expect((0, httpClient_1.sendDelete)(client, '/products', {
|
|
346
|
-
query,
|
|
347
|
-
})).rejects.toMatchObject({
|
|
348
|
-
message: 'connection error',
|
|
349
|
-
});
|
|
350
|
-
});
|
|
351
|
-
});
|
|
352
|
-
describe('POST', () => {
|
|
353
|
-
it('validates response structure with provided schema, throws an error', async () => {
|
|
354
|
-
const schema = zod_1.z.object({
|
|
355
|
-
id: zod_1.z.string(),
|
|
356
|
-
});
|
|
357
|
-
client
|
|
358
|
-
.intercept({
|
|
359
|
-
path: '/products/1',
|
|
360
|
-
method: 'POST',
|
|
361
|
-
})
|
|
362
|
-
.reply(200, mockProduct1_json_1.default, { headers: JSON_HEADERS });
|
|
363
|
-
await expect((0, httpClient_1.sendPost)(client, '/products/1', {}, {
|
|
364
|
-
responseSchema: schema,
|
|
365
|
-
validateResponse: true,
|
|
366
|
-
})).rejects.toThrow(/Expected string, received number/);
|
|
367
|
-
});
|
|
368
|
-
it('validates response structure with provided schema, passes validation', async () => {
|
|
369
|
-
const schema = zod_1.z.object({
|
|
370
|
-
category: zod_1.z.string(),
|
|
371
|
-
description: zod_1.z.string(),
|
|
372
|
-
id: zod_1.z.number(),
|
|
373
|
-
image: zod_1.z.string(),
|
|
374
|
-
price: zod_1.z.number(),
|
|
375
|
-
rating: zod_1.z.object({
|
|
376
|
-
count: zod_1.z.number(),
|
|
377
|
-
rate: zod_1.z.number(),
|
|
378
|
-
}),
|
|
379
|
-
title: zod_1.z.string(),
|
|
380
|
-
});
|
|
381
|
-
client
|
|
382
|
-
.intercept({
|
|
383
|
-
path: '/products/1',
|
|
384
|
-
method: 'POST',
|
|
385
|
-
})
|
|
386
|
-
.reply(200, mockProduct1_json_1.default, { headers: JSON_HEADERS });
|
|
387
|
-
const result = await (0, httpClient_1.sendPost)(client, '/products/1', {}, {
|
|
388
|
-
responseSchema: schema,
|
|
389
|
-
validateResponse: true,
|
|
390
|
-
reqContext,
|
|
391
|
-
});
|
|
392
|
-
expect(result.result.body).toEqual(mockProduct1_json_1.default);
|
|
393
|
-
});
|
|
394
|
-
it('validates response structure with provided schema, skips validation', async () => {
|
|
395
|
-
const schema = zod_1.z.object({
|
|
396
|
-
id: zod_1.z.string(),
|
|
397
|
-
});
|
|
398
|
-
client
|
|
399
|
-
.intercept({
|
|
400
|
-
path: '/products/1',
|
|
401
|
-
method: 'POST',
|
|
402
|
-
})
|
|
403
|
-
.reply(200, mockProduct1_json_1.default, { headers: JSON_HEADERS });
|
|
404
|
-
const result = await (0, httpClient_1.sendPost)(client, '/products/1', {}, {
|
|
405
|
-
responseSchema: schema,
|
|
406
|
-
validateResponse: false,
|
|
407
|
-
});
|
|
408
|
-
expect(result.result.body).toEqual(mockProduct1_json_1.default);
|
|
409
|
-
});
|
|
410
|
-
it('POST without queryParams', async () => {
|
|
411
|
-
client
|
|
412
|
-
.intercept({
|
|
413
|
-
path: '/products',
|
|
414
|
-
method: 'POST',
|
|
415
|
-
})
|
|
416
|
-
.reply(200, { id: 21 }, { headers: JSON_HEADERS });
|
|
417
|
-
const result = await (0, httpClient_1.sendPost)(client, '/products', mockProduct1_json_1.default);
|
|
418
|
-
expect(result.result.body).toEqual({ id: 21 });
|
|
419
|
-
});
|
|
420
|
-
it('POST without body', async () => {
|
|
421
|
-
client
|
|
422
|
-
.intercept({
|
|
423
|
-
path: '/products',
|
|
424
|
-
method: 'POST',
|
|
425
|
-
})
|
|
426
|
-
.reply(200, { id: 21 }, { headers: JSON_HEADERS });
|
|
427
|
-
const result = await (0, httpClient_1.sendPost)(client, '/products', undefined);
|
|
428
|
-
expect(result.result.body).toEqual({ id: 21 });
|
|
429
|
-
});
|
|
430
|
-
it('POST with queryParams', async () => {
|
|
431
|
-
const query = {
|
|
432
|
-
limit: 3,
|
|
433
|
-
};
|
|
434
|
-
client
|
|
435
|
-
.intercept({
|
|
436
|
-
path: '/products',
|
|
437
|
-
method: 'POST',
|
|
438
|
-
query,
|
|
439
|
-
})
|
|
440
|
-
.reply(200, { id: 21 }, { headers: JSON_HEADERS });
|
|
441
|
-
const result = await (0, httpClient_1.sendPost)(client, '/products', mockProduct1_json_1.default, {
|
|
442
|
-
query,
|
|
443
|
-
});
|
|
444
|
-
expect(result.result.body).toEqual({ id: 21 });
|
|
445
|
-
});
|
|
446
|
-
it('POST that returns 400 throws an error', async () => {
|
|
447
|
-
client
|
|
448
|
-
.intercept({
|
|
449
|
-
path: '/products',
|
|
450
|
-
method: 'POST',
|
|
451
|
-
})
|
|
452
|
-
.reply(400, { errorCode: 'err' }, { headers: JSON_HEADERS });
|
|
453
|
-
await expect((0, httpClient_1.sendPost)(client, '/products', mockProduct1_json_1.default)).rejects.toThrow('Response status code 400');
|
|
454
|
-
});
|
|
455
|
-
it('Throws an error on internal error', async () => {
|
|
456
|
-
expect.assertions(1);
|
|
457
|
-
const query = {
|
|
458
|
-
limit: 3,
|
|
459
|
-
};
|
|
460
|
-
client
|
|
461
|
-
.intercept({
|
|
462
|
-
path: '/products',
|
|
463
|
-
method: 'POST',
|
|
464
|
-
query,
|
|
465
|
-
})
|
|
466
|
-
.replyWithError(new Error('connection error'));
|
|
467
|
-
await expect((0, httpClient_1.sendPost)(client, '/products', undefined, {
|
|
468
|
-
query,
|
|
469
|
-
})).rejects.toMatchObject({
|
|
470
|
-
message: 'connection error',
|
|
471
|
-
});
|
|
472
|
-
});
|
|
473
|
-
});
|
|
474
|
-
describe('POST binary', () => {
|
|
475
|
-
it('validates response structure with provided schema, throws an error', async () => {
|
|
476
|
-
const schema = zod_1.z.object({
|
|
477
|
-
id: zod_1.z.string(),
|
|
478
|
-
});
|
|
479
|
-
client
|
|
480
|
-
.intercept({
|
|
481
|
-
path: '/products/1',
|
|
482
|
-
method: 'POST',
|
|
483
|
-
})
|
|
484
|
-
.reply(200, mockProduct1_json_1.default, { headers: JSON_HEADERS });
|
|
485
|
-
await expect((0, httpClient_1.sendPostBinary)(client, '/products/1', Buffer.from(JSON.stringify({})), {
|
|
486
|
-
responseSchema: schema,
|
|
487
|
-
validateResponse: true,
|
|
488
|
-
})).rejects.toThrow(/Expected string, received number/);
|
|
489
|
-
});
|
|
490
|
-
it('validates response structure with provided schema, passes validation', async () => {
|
|
491
|
-
const schema = zod_1.z.object({
|
|
492
|
-
category: zod_1.z.string(),
|
|
493
|
-
description: zod_1.z.string(),
|
|
494
|
-
id: zod_1.z.number(),
|
|
495
|
-
image: zod_1.z.string(),
|
|
496
|
-
price: zod_1.z.number(),
|
|
497
|
-
rating: zod_1.z.object({
|
|
498
|
-
count: zod_1.z.number(),
|
|
499
|
-
rate: zod_1.z.number(),
|
|
500
|
-
}),
|
|
501
|
-
title: zod_1.z.string(),
|
|
502
|
-
});
|
|
503
|
-
client
|
|
504
|
-
.intercept({
|
|
505
|
-
path: '/products/1',
|
|
506
|
-
method: 'POST',
|
|
507
|
-
})
|
|
508
|
-
.reply(200, mockProduct1_json_1.default, { headers: JSON_HEADERS });
|
|
509
|
-
const result = await (0, httpClient_1.sendPostBinary)(client, '/products/1', Buffer.from(JSON.stringify({})), {
|
|
510
|
-
responseSchema: schema,
|
|
511
|
-
validateResponse: true,
|
|
512
|
-
reqContext,
|
|
513
|
-
});
|
|
514
|
-
expect(result.result.body).toEqual(mockProduct1_json_1.default);
|
|
515
|
-
});
|
|
516
|
-
it('validates response structure with provided schema, skips validation', async () => {
|
|
517
|
-
const schema = zod_1.z.object({
|
|
518
|
-
id: zod_1.z.string(),
|
|
519
|
-
});
|
|
520
|
-
client
|
|
521
|
-
.intercept({
|
|
522
|
-
path: '/products/1',
|
|
523
|
-
method: 'POST',
|
|
524
|
-
})
|
|
525
|
-
.reply(200, mockProduct1_json_1.default, { headers: JSON_HEADERS });
|
|
526
|
-
const result = await (0, httpClient_1.sendPostBinary)(client, '/products/1', Buffer.from(JSON.stringify({})), {
|
|
527
|
-
responseSchema: schema,
|
|
528
|
-
validateResponse: false,
|
|
529
|
-
});
|
|
530
|
-
expect(result.result.body).toEqual(mockProduct1_json_1.default);
|
|
531
|
-
});
|
|
532
|
-
it('POST without queryParams', async () => {
|
|
533
|
-
client
|
|
534
|
-
.intercept({
|
|
535
|
-
path: '/products',
|
|
536
|
-
method: 'POST',
|
|
537
|
-
})
|
|
538
|
-
.reply(200, { id: 21 }, { headers: JSON_HEADERS });
|
|
539
|
-
const result = await (0, httpClient_1.sendPostBinary)(client, '/products', Buffer.from(JSON.stringify(mockProduct1_json_1.default)));
|
|
540
|
-
expect(result.result.body).toEqual({ id: 21 });
|
|
541
|
-
});
|
|
542
|
-
it('POST with queryParams', async () => {
|
|
543
|
-
const query = {
|
|
544
|
-
limit: 3,
|
|
545
|
-
};
|
|
546
|
-
client
|
|
547
|
-
.intercept({
|
|
548
|
-
path: '/products',
|
|
549
|
-
method: 'POST',
|
|
550
|
-
query,
|
|
551
|
-
})
|
|
552
|
-
.reply(200, { id: 21 }, { headers: JSON_HEADERS });
|
|
553
|
-
const result = await (0, httpClient_1.sendPostBinary)(client, '/products', Buffer.from(JSON.stringify(mockProduct1_json_1.default)), {
|
|
554
|
-
query,
|
|
555
|
-
});
|
|
556
|
-
expect(result.result.body).toEqual({ id: 21 });
|
|
557
|
-
});
|
|
558
|
-
it('POST that returns 400 throws an error', async () => {
|
|
559
|
-
client
|
|
560
|
-
.intercept({
|
|
561
|
-
path: '/products',
|
|
562
|
-
method: 'POST',
|
|
563
|
-
})
|
|
564
|
-
.reply(400, { errorCode: 'err' }, { headers: JSON_HEADERS });
|
|
565
|
-
await expect((0, httpClient_1.sendPostBinary)(client, '/products', Buffer.from(JSON.stringify(mockProduct1_json_1.default)))).rejects.toThrow('Response status code 400');
|
|
566
|
-
});
|
|
567
|
-
it('Throws an error on internal error', async () => {
|
|
568
|
-
expect.assertions(1);
|
|
569
|
-
const query = {
|
|
570
|
-
limit: 3,
|
|
571
|
-
};
|
|
572
|
-
client
|
|
573
|
-
.intercept({
|
|
574
|
-
path: '/products',
|
|
575
|
-
method: 'POST',
|
|
576
|
-
query,
|
|
577
|
-
})
|
|
578
|
-
.replyWithError(new Error('connection error'));
|
|
579
|
-
await expect((0, httpClient_1.sendPostBinary)(client, '/products', Buffer.from(JSON.stringify({})), {
|
|
580
|
-
query,
|
|
581
|
-
})).rejects.toMatchObject({
|
|
582
|
-
message: 'connection error',
|
|
583
|
-
});
|
|
584
|
-
});
|
|
585
|
-
});
|
|
586
|
-
describe('PUT', () => {
|
|
587
|
-
it('PUT without queryParams', async () => {
|
|
588
|
-
client
|
|
589
|
-
.intercept({
|
|
590
|
-
path: '/products/1',
|
|
591
|
-
method: 'PUT',
|
|
592
|
-
})
|
|
593
|
-
.reply(200, { id: 21 }, { headers: JSON_HEADERS });
|
|
594
|
-
const result = await (0, httpClient_1.sendPut)(client, '/products/1', mockProduct1_json_1.default, {
|
|
595
|
-
reqContext,
|
|
596
|
-
});
|
|
597
|
-
expect(result.result.body).toEqual({ id: 21 });
|
|
598
|
-
});
|
|
599
|
-
it('PUT without body', async () => {
|
|
600
|
-
client
|
|
601
|
-
.intercept({
|
|
602
|
-
path: '/products/1',
|
|
603
|
-
method: 'PUT',
|
|
604
|
-
})
|
|
605
|
-
.reply(200, { id: 21 }, { headers: JSON_HEADERS });
|
|
606
|
-
const result = await (0, httpClient_1.sendPut)(client, '/products/1', undefined);
|
|
607
|
-
expect(result.result.body).toEqual({ id: 21 });
|
|
608
|
-
});
|
|
609
|
-
it('PUT with queryParams', async () => {
|
|
610
|
-
const query = {
|
|
611
|
-
limit: 3,
|
|
612
|
-
};
|
|
613
|
-
client
|
|
614
|
-
.intercept({
|
|
615
|
-
path: '/products/1',
|
|
616
|
-
method: 'PUT',
|
|
617
|
-
query,
|
|
618
|
-
})
|
|
619
|
-
.reply(200, { id: 21 }, { headers: JSON_HEADERS });
|
|
620
|
-
const result = await (0, httpClient_1.sendPut)(client, '/products/1', mockProduct1_json_1.default, {
|
|
621
|
-
query,
|
|
622
|
-
});
|
|
623
|
-
expect(result.result.body).toEqual({ id: 21 });
|
|
624
|
-
});
|
|
625
|
-
it('PUT that returns 400 throws an error', async () => {
|
|
626
|
-
client
|
|
627
|
-
.intercept({
|
|
628
|
-
path: '/products/1',
|
|
629
|
-
method: 'PUT',
|
|
630
|
-
})
|
|
631
|
-
.reply(400, { errorCode: 'err' }, { headers: JSON_HEADERS });
|
|
632
|
-
await expect((0, httpClient_1.sendPut)(client, '/products/1', mockProduct1_json_1.default)).rejects.toThrow('Response status code 400');
|
|
633
|
-
});
|
|
634
|
-
it('Throws an error on internal error', async () => {
|
|
635
|
-
expect.assertions(1);
|
|
636
|
-
const query = {
|
|
637
|
-
limit: 3,
|
|
638
|
-
};
|
|
639
|
-
client
|
|
640
|
-
.intercept({
|
|
641
|
-
path: '/products',
|
|
642
|
-
method: 'PUT',
|
|
643
|
-
query,
|
|
644
|
-
})
|
|
645
|
-
.replyWithError(new Error('connection error'));
|
|
646
|
-
await expect((0, httpClient_1.sendPut)(client, '/products', undefined, {
|
|
647
|
-
query,
|
|
648
|
-
})).rejects.toMatchObject({
|
|
649
|
-
message: 'connection error',
|
|
650
|
-
});
|
|
651
|
-
});
|
|
652
|
-
});
|
|
653
|
-
describe('PUT binary', () => {
|
|
654
|
-
it('PUT without queryParams', async () => {
|
|
655
|
-
client
|
|
656
|
-
.intercept({
|
|
657
|
-
path: '/products/1',
|
|
658
|
-
method: 'PUT',
|
|
659
|
-
})
|
|
660
|
-
.reply(200, { id: 21 }, { headers: JSON_HEADERS });
|
|
661
|
-
const result = await (0, httpClient_1.sendPutBinary)(client, '/products/1', Buffer.from('text'), {
|
|
662
|
-
reqContext,
|
|
663
|
-
});
|
|
664
|
-
expect(result.result.body).toEqual({ id: 21 });
|
|
665
|
-
});
|
|
666
|
-
it('PUT with queryParams', async () => {
|
|
667
|
-
const query = {
|
|
668
|
-
limit: 3,
|
|
669
|
-
};
|
|
670
|
-
client
|
|
671
|
-
.intercept({
|
|
672
|
-
path: '/products/1',
|
|
673
|
-
method: 'PUT',
|
|
674
|
-
query,
|
|
675
|
-
})
|
|
676
|
-
.reply(200, { id: 21 }, { headers: JSON_HEADERS });
|
|
677
|
-
const result = await (0, httpClient_1.sendPutBinary)(client, '/products/1', Buffer.from('text'), {
|
|
678
|
-
query,
|
|
679
|
-
});
|
|
680
|
-
expect(result.result.body).toEqual({ id: 21 });
|
|
681
|
-
});
|
|
682
|
-
it('PUT that returns 400 throws an error', async () => {
|
|
683
|
-
client
|
|
684
|
-
.intercept({
|
|
685
|
-
path: '/products/1',
|
|
686
|
-
method: 'PUT',
|
|
687
|
-
})
|
|
688
|
-
.reply(400, { errorCode: 'err' }, { headers: JSON_HEADERS });
|
|
689
|
-
await expect((0, httpClient_1.sendPutBinary)(client, '/products/1', Buffer.from('text'))).rejects.toThrow('Response status code 400');
|
|
690
|
-
});
|
|
691
|
-
it('Throws an error on internal error', async () => {
|
|
692
|
-
expect.assertions(1);
|
|
693
|
-
const query = {
|
|
694
|
-
limit: 3,
|
|
695
|
-
};
|
|
696
|
-
client
|
|
697
|
-
.intercept({
|
|
698
|
-
path: '/products',
|
|
699
|
-
method: 'PUT',
|
|
700
|
-
query,
|
|
701
|
-
})
|
|
702
|
-
.replyWithError(new Error('connection error'));
|
|
703
|
-
await expect((0, httpClient_1.sendPutBinary)(client, '/products', null, {
|
|
704
|
-
query,
|
|
705
|
-
})).rejects.toMatchObject({
|
|
706
|
-
message: 'connection error',
|
|
707
|
-
});
|
|
708
|
-
});
|
|
709
|
-
});
|
|
710
|
-
describe('PATCH', () => {
|
|
711
|
-
it('PATCH without queryParams', async () => {
|
|
712
|
-
client
|
|
713
|
-
.intercept({
|
|
714
|
-
path: '/products/1',
|
|
715
|
-
method: 'PATCH',
|
|
716
|
-
})
|
|
717
|
-
.reply(200, { id: 21 }, { headers: JSON_HEADERS });
|
|
718
|
-
const result = await (0, httpClient_1.sendPatch)(client, '/products/1', mockProduct1_json_1.default);
|
|
719
|
-
expect(result.result.body).toEqual({ id: 21 });
|
|
720
|
-
});
|
|
721
|
-
it('PATCH without body', async () => {
|
|
722
|
-
client
|
|
723
|
-
.intercept({
|
|
724
|
-
path: '/products/1',
|
|
725
|
-
method: 'PATCH',
|
|
726
|
-
})
|
|
727
|
-
.reply(200, { id: 21 }, { headers: JSON_HEADERS });
|
|
728
|
-
const result = await (0, httpClient_1.sendPatch)(client, '/products/1', undefined);
|
|
729
|
-
expect(result.result.body).toEqual({ id: 21 });
|
|
730
|
-
});
|
|
731
|
-
it('PATCH with queryParams', async () => {
|
|
732
|
-
const query = {
|
|
733
|
-
limit: 3,
|
|
734
|
-
};
|
|
735
|
-
client
|
|
736
|
-
.intercept({
|
|
737
|
-
path: '/products/1',
|
|
738
|
-
method: 'PATCH',
|
|
739
|
-
query,
|
|
740
|
-
})
|
|
741
|
-
.reply(200, { id: 21 }, { headers: JSON_HEADERS });
|
|
742
|
-
const result = await (0, httpClient_1.sendPatch)(client, '/products/1', mockProduct1_json_1.default, {
|
|
743
|
-
query,
|
|
744
|
-
reqContext,
|
|
745
|
-
});
|
|
746
|
-
expect(result.result.body).toEqual({ id: 21 });
|
|
747
|
-
});
|
|
748
|
-
it('PATCH that returns 400 throws an error', async () => {
|
|
749
|
-
client
|
|
750
|
-
.intercept({
|
|
751
|
-
path: '/products/1',
|
|
752
|
-
method: 'PATCH',
|
|
753
|
-
})
|
|
754
|
-
.reply(400, { errorCode: 'err' }, { headers: JSON_HEADERS });
|
|
755
|
-
await expect((0, httpClient_1.sendPatch)(client, '/products/1', mockProduct1_json_1.default)).rejects.toThrow('Response status code 400');
|
|
756
|
-
});
|
|
757
|
-
it('Throws an error on internal error', async () => {
|
|
758
|
-
expect.assertions(1);
|
|
759
|
-
const query = {
|
|
760
|
-
limit: 3,
|
|
761
|
-
};
|
|
762
|
-
client
|
|
763
|
-
.intercept({
|
|
764
|
-
path: '/products',
|
|
765
|
-
method: 'PATCH',
|
|
766
|
-
query,
|
|
767
|
-
})
|
|
768
|
-
.replyWithError(new Error('connection error'));
|
|
769
|
-
await expect((0, httpClient_1.sendPatch)(client, '/products', undefined, {
|
|
770
|
-
query,
|
|
771
|
-
})).rejects.toMatchObject({
|
|
772
|
-
message: 'connection error',
|
|
773
|
-
});
|
|
774
|
-
});
|
|
775
|
-
});
|
|
776
|
-
});
|
|
777
|
-
//# sourceMappingURL=httpClient.spec.js.map
|