@trayio/commons 2.1.0 → 2.2.0

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.
@@ -23,7 +23,7 @@ export type HttpRequest = {
23
23
  export type HttpResponse = {
24
24
  statusCode: number;
25
25
  headers: HttpHeaders;
26
- body: ArrayBuffer;
26
+ body: Readable;
27
27
  };
28
28
  export declare enum HttpMethod {
29
29
  Get = "GET",
@@ -1 +1 @@
1
- {"version":3,"file":"Http.d.ts","sourceRoot":"","sources":["../../src/http/Http.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,CAAC,MAAM,kBAAkB,CAAC;AAEtC,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAClC,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAOrE,MAAM,MAAM,eAAe,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC;AAEhD,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,eAAe,GAAG,eAAe,CAAC,CAAC;AAE5E,MAAM,MAAM,0BAA0B,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;AAEnE,MAAM,MAAM,oBAAoB,GAC7B,0BAA0B,GAC1B,0BAA0B,EAAE,CAAC;AAEhC,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,oBAAoB,CAAC,CAAC;AAEnE,MAAM,MAAM,sBAAsB,GAAG,QAAQ,CAAC;AAE9C,MAAM,MAAM,wBAAwB,GAAG;IACtC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC/B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;CACpC,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG,sBAAsB,GAAG,wBAAwB,CAAC;AAEhF,MAAM,MAAM,WAAW,GAAG;IACzB,O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C5C,IAAI,eAAe;CACnB;AAED,oBAAY,UAAU;IACrB,aAAa,kBAAkB;IAC/B,WAAW,iBAAiB;IAC5B,aAAa,6BAA6B;IAC1C,SAAS,eAAe;CACxB;AAED,MAAM,WAAW,oBAAoB;IACpC,GAAG,EAAE,CAAC,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,KAAK,CAAC,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC;IACvE,kBAAkB,EAAE,CAAC,OAAO,EAAE,WAAW,KAAK,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;CAC/D;AAED,eAAO,MAAM,WAAW,EAAE,oBAYzB,CAAC;AAEF,eAAO,MAAM,oCAAoC,EAAE,MAAM,CACxD,mBAAmB,EACnB,eAAe,CAGf,CAAC;AAEF,eAAO,MAAM,eAAe,SACrB,eAAe,qCAEkC,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"HttpClient.abstract.test.d.ts","sourceRoot":"","sources":["../../src/http/HttpClient.abstract.test.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAM1C,wBAAgB,cAAc,CAAC,UAAU,EAAE,UAAU,QAycpD"}
1
+ {"version":3,"file":"HttpClient.abstract.test.d.ts","sourceRoot":"","sources":["../../src/http/HttpClient.abstract.test.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAM1C,wBAAgB,cAAc,CAAC,UAAU,EAAE,UAAU,QAuepD"}
@@ -208,8 +208,9 @@ function httpClientTest(httpClient) {
208
208
  strField: 'customVal',
209
209
  intField: 123,
210
210
  };
211
- const result = (0, function_1.pipe)(response, E.chain((httpResponse) => serialization.deserialize(httpResponse.body)), E.chain((encoded) => testResponseCodec.decode(encoded)));
212
- expect(result).toStrictEqual(E.right(testValue));
211
+ const result = (0, function_1.pipe)(response, TE.fromEither, TE.chain((httpResponse) => BufferExtensions_1.BufferExtensions.readableToArrayBuffer(httpResponse.body)), TE.match((error) => E.left(error), (buffer) => (0, function_1.pipe)(serialization.deserialize(buffer), E.chain((encoded) => testResponseCodec.decode(encoded)))));
212
+ const resultE = yield result();
213
+ expect(resultE).toStrictEqual(E.right(testValue));
213
214
  }));
214
215
  test('should send a POST request', () => __awaiter(this, void 0, void 0, function* () {
215
216
  const testInput = {
@@ -227,8 +228,9 @@ function httpClientTest(httpClient) {
227
228
  strField: 'customVal',
228
229
  intField: 321,
229
230
  };
230
- const result = (0, function_1.pipe)(response, E.chain((httpResponse) => serialization.deserialize(httpResponse.body)), E.chain((encoded) => testResponseCodec.decode(encoded)));
231
- expect(result).toStrictEqual(E.right(testValue));
231
+ const result = (0, function_1.pipe)(response, TE.fromEither, TE.chain((httpResponse) => BufferExtensions_1.BufferExtensions.readableToArrayBuffer(httpResponse.body)), TE.match((error) => E.left(error), (buffer) => (0, function_1.pipe)(serialization.deserialize(buffer), E.chain((encoded) => testResponseCodec.decode(encoded)))));
232
+ const resultE = yield result();
233
+ expect(resultE).toStrictEqual(E.right(testValue));
232
234
  }));
233
235
  test('should send a POST request with multipart body', () => __awaiter(this, void 0, void 0, function* () {
234
236
  yield fileStorage.delete('file1')();
@@ -274,8 +276,9 @@ function httpClientTest(httpClient) {
274
276
  queryString: {},
275
277
  body: testInput,
276
278
  })();
277
- const result = (0, function_1.pipe)(response, E.chain((httpResponse) => serialization.deserialize(httpResponse.body)), E.chain((res) => testMultiPartBodyTypeCodec.decode(res)));
278
- expect(result).toEqual(E.right(expect.objectContaining({
279
+ const result = (0, function_1.pipe)(response, TE.fromEither, TE.chain((httpResponse) => BufferExtensions_1.BufferExtensions.readableToArrayBuffer(httpResponse.body)), TE.match((error) => E.left(error), (buffer) => (0, function_1.pipe)(serialization.deserialize(buffer), E.chain((encoded) => testMultiPartBodyTypeCodec.decode(encoded)))));
280
+ const resultE = yield result();
281
+ expect(resultE).toEqual(E.right(expect.objectContaining({
279
282
  fields: testInput.fields,
280
283
  files: expect.objectContaining({
281
284
  file1: expect.objectContaining({
@@ -297,12 +300,12 @@ function httpClientTest(httpClient) {
297
300
  const testErrorResponse = {
298
301
  message: 'some error message 500',
299
302
  };
300
- const expectedResponse = (0, function_1.pipe)(response, E.bindTo('httpResponse'), E.bind('serialisedErrorResponse', () => E.right(serialization.serialize(testErrorResponseCodec.encode(testErrorResponse)))), E.map(({ httpResponse, serialisedErrorResponse }) => ({
301
- headers: httpResponse.headers,
302
- statusCode: 500,
303
- body: Buffer.from(serialisedErrorResponse),
304
- })));
305
- expect(response).toStrictEqual(expectedResponse);
303
+ if (E.isLeft(response)) {
304
+ throw response.left;
305
+ }
306
+ const responseBodyE = yield BufferExtensions_1.BufferExtensions.readableToArrayBuffer(response.right.body)();
307
+ expect(response.right.statusCode).toStrictEqual(500);
308
+ expect(responseBodyE).toStrictEqual(E.right(serialization.serialize(testErrorResponseCodec.encode(testErrorResponse))));
306
309
  }));
307
310
  test('should parse a not found error response', () => __awaiter(this, void 0, void 0, function* () {
308
311
  const response = yield httpClient.execute(Http_1.HttpMethod.Get, baseUrl, {
@@ -314,12 +317,12 @@ function httpClientTest(httpClient) {
314
317
  const testErrorResponse = {
315
318
  message: 'some error message 404',
316
319
  };
317
- const expectedResponse = (0, function_1.pipe)(response, E.bindTo('httpResponse'), E.bind('serialisedErrorResponse', () => E.right(serialization.serialize(testErrorResponseCodec.encode(testErrorResponse)))), E.map(({ httpResponse, serialisedErrorResponse }) => ({
318
- headers: httpResponse.headers,
319
- statusCode: 404,
320
- body: Buffer.from(serialisedErrorResponse),
321
- })));
322
- expect(response).toStrictEqual(expectedResponse);
320
+ if (E.isLeft(response)) {
321
+ throw response.left;
322
+ }
323
+ const responseBodyE = yield BufferExtensions_1.BufferExtensions.readableToArrayBuffer(response.right.body)();
324
+ expect(response.right.statusCode).toStrictEqual(404);
325
+ expect(responseBodyE).toStrictEqual(E.right(serialization.serialize(testErrorResponseCodec.encode(testErrorResponse))));
323
326
  }));
324
327
  });
325
328
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/commons",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "Extensions to the standard/core libraries and basic features",
5
5
  "exports": {
6
6
  "./*": "./dist/*.js"