@trayio/axios 1.7.0 → 1.9.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.
@@ -1,9 +1,14 @@
1
1
  import * as TE from 'fp-ts/TaskEither';
2
2
  import { HttpClient } from '@trayio/commons/http/HttpClient';
3
3
  import { HttpMethod, HttpRequest, HttpResponse } from '@trayio/commons/http/Http';
4
+ import FormData = require('form-data');
5
+ import { FileStorage } from '@trayio/commons/file/File';
4
6
  export declare class AxiosHttpClient implements HttpClient {
7
+ private readonly fileStorage;
8
+ constructor(fileStorage: FileStorage);
5
9
  execute(method: HttpMethod, url: string, request: HttpRequest): TE.TaskEither<Error, HttpResponse>;
6
10
  private axiosErrorToHttpResponse;
7
11
  private axiosResponseToHttpResponse;
12
+ appendFields: (formData: FormData) => (fields: Record<string, string>) => FormData;
8
13
  }
9
14
  //# sourceMappingURL=AxiosHttpClient.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"AxiosHttpClient.d.ts","sourceRoot":"","sources":["../../src/http/AxiosHttpClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACvC,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAC7D,OAAO,EACN,UAAU,EACV,WAAW,EAEX,YAAY,EACZ,MAAM,2BAA2B,CAAC;AAInC,qBAAa,eAAgB,YAAW,UAAU;IACjD,OAAO,CACN,MAAM,EAAE,UAAU,EAClB,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,WAAW,GAClB,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,YAAY,CAAC;IA2CrC,OAAO,CAAC,wBAAwB;IAchC,OAAO,CAAC,2BAA2B;CASnC"}
1
+ {"version":3,"file":"AxiosHttpClient.d.ts","sourceRoot":"","sources":["../../src/http/AxiosHttpClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAEvC,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAC7D,OAAO,EAEN,UAAU,EACV,WAAW,EAEX,YAAY,EAEZ,MAAM,2BAA2B,CAAC;AAGnC,OAAO,QAAQ,GAAG,QAAQ,WAAW,CAAC,CAAC;AACvC,OAAO,EAAQ,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAE9D,qBAAa,eAAgB,YAAW,UAAU;IACrC,OAAO,CAAC,QAAQ,CAAC,WAAW;gBAAX,WAAW,EAAE,WAAW;IAErD,OAAO,CACN,MAAM,EAAE,UAAU,EAClB,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,WAAW,GAClB,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,YAAY,CAAC;IA+FrC,OAAO,CAAC,wBAAwB;IAchC,OAAO,CAAC,2BAA2B;IAUnC,YAAY,aAAc,QAAQ,cAAc,OAAO,MAAM,EAAE,MAAM,CAAC,cAKpE;CACF"}
@@ -1,19 +1,90 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
27
  };
5
28
  Object.defineProperty(exports, "__esModule", { value: true });
6
29
  exports.AxiosHttpClient = void 0;
30
+ const TE = __importStar(require("fp-ts/TaskEither"));
31
+ const function_1 = require("fp-ts/function");
32
+ const Http_1 = require("@trayio/commons/http/Http");
7
33
  const Task_1 = require("@trayio/commons/task/Task");
8
34
  const axios_1 = __importDefault(require("axios"));
35
+ const FormData = require("form-data");
9
36
  class AxiosHttpClient {
37
+ constructor(fileStorage) {
38
+ this.fileStorage = fileStorage;
39
+ this.appendFields = (formData) => (fields) => {
40
+ Object.entries(fields).forEach(([key, value]) => {
41
+ formData.append(key, value);
42
+ });
43
+ return formData;
44
+ };
45
+ }
10
46
  execute(method, url, request) {
47
+ /*
48
+ Removes default headers so that we control what we send to the server, without this, it sends default content-type and accept headers,
49
+ the caller of this HttpClient interface is responsible of deciding the values of these headers, axios shouldn't try to be smart
50
+ and derive these from the body or even set defaults.
51
+ */
52
+ axios_1.default.defaults.headers.common = {};
53
+ axios_1.default.defaults.headers.get = {};
54
+ axios_1.default.defaults.headers.post = {};
55
+ axios_1.default.defaults.headers.put = {};
56
+ axios_1.default.defaults.headers.patch = {};
57
+ axios_1.default.defaults.headers.delete = {};
11
58
  const finalUrl = Object.entries(request.pathParams).reduce((acc, [key, value]) => acc.replace(`:${key}`, encodeURIComponent(value)), url);
12
59
  const headers = Object.entries(request.headers).reduce((acc, [key, value]) => {
13
60
  const newValue = typeof value === 'string' ? value : value.join(', ');
14
61
  return Object.assign(Object.assign({}, acc), { [key]: newValue });
15
62
  }, {});
16
- const axiosConfig = {
63
+ let axiosConfig;
64
+ if (headers['content-type'] &&
65
+ headers['content-type'].includes(Http_1.HttpContentType.MultipartRequestBody)) {
66
+ const formData = new FormData();
67
+ this.appendFields(formData)(request.body.fields);
68
+ const fileEntries = Object.entries(request.body.files);
69
+ const taskEithers = fileEntries.map(([key, value]) => (0, function_1.pipe)(this.fileStorage.read(value.key), TE.map((file) => (Object.assign(Object.assign({}, file), { formFileName: key })))));
70
+ return (0, function_1.pipe)(TE.sequenceArray(taskEithers), TE.map((files) => files), TE.chain((files) => {
71
+ files.forEach((file) => {
72
+ formData.append(file.formFileName, file.content, file.metadata.key);
73
+ });
74
+ axiosConfig = {
75
+ url: finalUrl,
76
+ method: method.toString(),
77
+ data: formData,
78
+ responseType: 'arraybuffer',
79
+ headers,
80
+ params: request.queryString,
81
+ };
82
+ return (0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => (0, axios_1.default)(axiosConfig)
83
+ .then(this.axiosResponseToHttpResponse)
84
+ .catch(this.axiosErrorToHttpResponse.bind(this)));
85
+ }));
86
+ }
87
+ axiosConfig = {
17
88
  url: finalUrl,
18
89
  method: method.toString(),
19
90
  data: request.body,
@@ -21,17 +92,6 @@ class AxiosHttpClient {
21
92
  headers,
22
93
  params: request.queryString,
23
94
  };
24
- /*
25
- Removes default headers so that we control what we send to the server, without this, it sends default content-type and accept headers,
26
- the caller of this HttpClient interface is responsible of deciding the values of these headers, axios shouldn't try to be smart
27
- and derive these from the body or even set defaults.
28
- */
29
- axios_1.default.defaults.headers.common = {};
30
- axios_1.default.defaults.headers.get = {};
31
- axios_1.default.defaults.headers.post = {};
32
- axios_1.default.defaults.headers.put = {};
33
- axios_1.default.defaults.headers.patch = {};
34
- axios_1.default.defaults.headers.delete = {};
35
95
  return (0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => (0, axios_1.default)(axiosConfig)
36
96
  .then(this.axiosResponseToHttpResponse)
37
97
  .catch(this.axiosErrorToHttpResponse.bind(this)));
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ const NodeFsFileStorage_1 = require("@trayio/commons/file/NodeFsFileStorage");
3
4
  const HttpClient_abstract_test_1 = require("@trayio/commons/http/HttpClient.abstract.test");
4
5
  const AxiosHttpClient_1 = require("./AxiosHttpClient");
5
- (0, HttpClient_abstract_test_1.httpClientTest)(new AxiosHttpClient_1.AxiosHttpClient());
6
+ (0, HttpClient_abstract_test_1.httpClientTest)(new AxiosHttpClient_1.AxiosHttpClient(new NodeFsFileStorage_1.NodeFsFileStorage()));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/axios",
3
- "version": "1.7.0",
3
+ "version": "1.9.0",
4
4
  "description": "Axios extensions and implementations",
5
5
  "exports": {
6
6
  "./*": "./dist/*.js"
@@ -14,8 +14,9 @@
14
14
  "access": "public"
15
15
  },
16
16
  "dependencies": {
17
- "@trayio/commons": "1.7.0",
18
- "axios": "0.27.2"
17
+ "@trayio/commons": "1.9.0",
18
+ "axios": "0.27.2",
19
+ "form-data": "4.0.0"
19
20
  },
20
21
  "typesVersions": {
21
22
  "*": {