@trayio/axios 1.8.0 → 1.9.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.
@@ -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;AAG9D,qBAAa,eAAgB,YAAW,UAAU;IAEhD,OAAO,CAAC,QAAQ,CAAC,WAAW;gBAAX,WAAW,GAAE,WAAqC;IAGpE,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,91 @@
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");
36
+ const NodeFsFileStorage_1 = require("@trayio/commons/file/NodeFsFileStorage");
9
37
  class AxiosHttpClient {
38
+ constructor(fileStorage = new NodeFsFileStorage_1.NodeFsFileStorage()) {
39
+ this.fileStorage = fileStorage;
40
+ this.appendFields = (formData) => (fields) => {
41
+ Object.entries(fields).forEach(([key, value]) => {
42
+ formData.append(key, value);
43
+ });
44
+ return formData;
45
+ };
46
+ }
10
47
  execute(method, url, request) {
48
+ /*
49
+ Removes default headers so that we control what we send to the server, without this, it sends default content-type and accept headers,
50
+ the caller of this HttpClient interface is responsible of deciding the values of these headers, axios shouldn't try to be smart
51
+ and derive these from the body or even set defaults.
52
+ */
53
+ axios_1.default.defaults.headers.common = {};
54
+ axios_1.default.defaults.headers.get = {};
55
+ axios_1.default.defaults.headers.post = {};
56
+ axios_1.default.defaults.headers.put = {};
57
+ axios_1.default.defaults.headers.patch = {};
58
+ axios_1.default.defaults.headers.delete = {};
11
59
  const finalUrl = Object.entries(request.pathParams).reduce((acc, [key, value]) => acc.replace(`:${key}`, encodeURIComponent(value)), url);
12
60
  const headers = Object.entries(request.headers).reduce((acc, [key, value]) => {
13
61
  const newValue = typeof value === 'string' ? value : value.join(', ');
14
62
  return Object.assign(Object.assign({}, acc), { [key]: newValue });
15
63
  }, {});
16
- const axiosConfig = {
64
+ let axiosConfig;
65
+ if (headers['content-type'] &&
66
+ headers['content-type'].includes(Http_1.HttpContentType.MultipartRequestBody)) {
67
+ const formData = new FormData();
68
+ this.appendFields(formData)(request.body.fields);
69
+ const fileEntries = Object.entries(request.body.files);
70
+ 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 })))));
71
+ return (0, function_1.pipe)(TE.sequenceArray(taskEithers), TE.map((files) => files), TE.chain((files) => {
72
+ files.forEach((file) => {
73
+ formData.append(file.formFileName, file.content, file.metadata.key);
74
+ });
75
+ axiosConfig = {
76
+ url: finalUrl,
77
+ method: method.toString(),
78
+ data: formData,
79
+ responseType: 'arraybuffer',
80
+ headers,
81
+ params: request.queryString,
82
+ };
83
+ return (0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => (0, axios_1.default)(axiosConfig)
84
+ .then(this.axiosResponseToHttpResponse)
85
+ .catch(this.axiosErrorToHttpResponse.bind(this)));
86
+ }));
87
+ }
88
+ axiosConfig = {
17
89
  url: finalUrl,
18
90
  method: method.toString(),
19
91
  data: request.body,
@@ -21,17 +93,6 @@ class AxiosHttpClient {
21
93
  headers,
22
94
  params: request.queryString,
23
95
  };
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
96
  return (0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => (0, axios_1.default)(axiosConfig)
36
97
  .then(this.axiosResponseToHttpResponse)
37
98
  .catch(this.axiosErrorToHttpResponse.bind(this)));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/axios",
3
- "version": "1.8.0",
3
+ "version": "1.9.1",
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.8.0",
18
- "axios": "0.27.2"
17
+ "@trayio/commons": "1.9.1",
18
+ "axios": "0.27.2",
19
+ "form-data": "4.0.0"
19
20
  },
20
21
  "typesVersions": {
21
22
  "*": {