@trayio/axios 0.0.1-beta
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/LICENSE.txt +22 -0
- package/dist/http/AxiosHttpClient.d.ts +14 -0
- package/dist/http/AxiosHttpClient.d.ts.map +1 -0
- package/dist/http/AxiosHttpClient.js +96 -0
- package/dist/http/AxiosHttpClient.unit.test.d.ts +2 -0
- package/dist/http/AxiosHttpClient.unit.test.d.ts.map +1 -0
- package/dist/http/AxiosHttpClient.unit.test.js +7 -0
- package/package.json +32 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Tray.io, Inc
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import * as TE from 'fp-ts/TaskEither';
|
|
2
|
+
import { HttpClient } from '@trayio/commons/http/HttpClient';
|
|
3
|
+
import { HttpMethod, HttpRequest, HttpResponse } from '@trayio/commons/http/Http';
|
|
4
|
+
import FormData = require('form-data');
|
|
5
|
+
import { FileStorage } from '@trayio/commons/file/File';
|
|
6
|
+
export declare class AxiosHttpClient implements HttpClient {
|
|
7
|
+
private readonly fileStorage;
|
|
8
|
+
constructor(fileStorage?: FileStorage);
|
|
9
|
+
execute(method: HttpMethod, url: string, request: HttpRequest): TE.TaskEither<Error, HttpResponse>;
|
|
10
|
+
private axiosErrorToHttpResponse;
|
|
11
|
+
private axiosResponseToHttpResponse;
|
|
12
|
+
appendFields: (formData: FormData) => (fields: Record<string, string>) => FormData;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=AxiosHttpClient.d.ts.map
|
|
@@ -0,0 +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,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;AAK9D,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;IAkFrC,OAAO,CAAC,wBAAwB;IAchC,OAAO,CAAC,2BAA2B;IAUnC,YAAY,aAAc,QAAQ,cAAc,OAAO,MAAM,EAAE,MAAM,CAAC,cAKpE;CACF"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.AxiosHttpClient = void 0;
|
|
7
|
+
const Http_1 = require("@trayio/commons/http/Http");
|
|
8
|
+
const Task_1 = require("@trayio/commons/task/Task");
|
|
9
|
+
const axios_1 = __importDefault(require("axios"));
|
|
10
|
+
const FormData = require("form-data");
|
|
11
|
+
const NodeFsFileStorage_1 = require("@trayio/commons/file/NodeFsFileStorage");
|
|
12
|
+
const BufferExtensions_1 = require("@trayio/commons/buffer/BufferExtensions");
|
|
13
|
+
class AxiosHttpClient {
|
|
14
|
+
constructor(fileStorage = new NodeFsFileStorage_1.NodeFsFileStorage()) {
|
|
15
|
+
this.fileStorage = fileStorage;
|
|
16
|
+
this.appendFields = (formData) => (fields) => {
|
|
17
|
+
Object.entries(fields).forEach(([key, value]) => {
|
|
18
|
+
formData.append(key, value);
|
|
19
|
+
});
|
|
20
|
+
return formData;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
execute(method, url, request) {
|
|
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
|
+
const finalUrl = Object.entries(request.pathParams).reduce((acc, [key, value]) => acc.replace(`:${key}`, encodeURIComponent(value)), url);
|
|
36
|
+
const headers = Object.entries(request.headers).reduce((acc, [key, value]) => {
|
|
37
|
+
const newValue = typeof value === 'string' ? value : value.join(', ');
|
|
38
|
+
return Object.assign(Object.assign({}, acc), { [key.toLowerCase()]: newValue });
|
|
39
|
+
}, {});
|
|
40
|
+
let axiosConfig;
|
|
41
|
+
if (headers['content-type'] &&
|
|
42
|
+
headers['content-type'].includes(Http_1.HttpContentType.MultipartRequestBody)) {
|
|
43
|
+
const formData = new FormData();
|
|
44
|
+
this.appendFields(formData)(request.body.fields);
|
|
45
|
+
const { files } = request.body;
|
|
46
|
+
Object.keys(files).forEach((key) => {
|
|
47
|
+
var _a;
|
|
48
|
+
const file = files[key];
|
|
49
|
+
formData.append(key, file.content, {
|
|
50
|
+
filename: (_a = file.metadata.name) !== null && _a !== void 0 ? _a : file.key,
|
|
51
|
+
contentType: file.metadata.contentType,
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
axiosConfig = {
|
|
55
|
+
url: finalUrl,
|
|
56
|
+
method: method.toString(),
|
|
57
|
+
data: formData,
|
|
58
|
+
responseType: 'stream',
|
|
59
|
+
headers,
|
|
60
|
+
params: request.queryString,
|
|
61
|
+
};
|
|
62
|
+
return (0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => (0, axios_1.default)(axiosConfig)
|
|
63
|
+
.then(this.axiosResponseToHttpResponse)
|
|
64
|
+
.catch(this.axiosErrorToHttpResponse.bind(this)));
|
|
65
|
+
}
|
|
66
|
+
axiosConfig = {
|
|
67
|
+
url: finalUrl,
|
|
68
|
+
method: method.toString(),
|
|
69
|
+
data: request.body,
|
|
70
|
+
responseType: 'stream',
|
|
71
|
+
headers,
|
|
72
|
+
params: request.queryString,
|
|
73
|
+
};
|
|
74
|
+
return (0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => (0, axios_1.default)(axiosConfig)
|
|
75
|
+
.then(this.axiosResponseToHttpResponse)
|
|
76
|
+
.catch(this.axiosErrorToHttpResponse.bind(this)));
|
|
77
|
+
}
|
|
78
|
+
axiosErrorToHttpResponse(axiosError) {
|
|
79
|
+
if (axiosError.response !== undefined) {
|
|
80
|
+
return this.axiosResponseToHttpResponse(axiosError.response);
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
headers: {},
|
|
84
|
+
statusCode: 500,
|
|
85
|
+
body: BufferExtensions_1.BufferExtensions.arrayBufferToReadable(new ArrayBuffer(0)),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
axiosResponseToHttpResponse(axiosResponse) {
|
|
89
|
+
return {
|
|
90
|
+
headers: axiosResponse.headers,
|
|
91
|
+
statusCode: axiosResponse.status,
|
|
92
|
+
body: axiosResponse.data,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
exports.AxiosHttpClient = AxiosHttpClient;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AxiosHttpClient.unit.test.d.ts","sourceRoot":"","sources":["../../src/http/AxiosHttpClient.unit.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const HttpClient_abstract_test_1 = require("@trayio/commons/http/HttpClient.abstract.test");
|
|
4
|
+
const AxiosHttpClient_1 = require("./AxiosHttpClient");
|
|
5
|
+
describe('AxiosHttpClient Tests', () => {
|
|
6
|
+
(0, HttpClient_abstract_test_1.httpClientTest)(new AxiosHttpClient_1.AxiosHttpClient());
|
|
7
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@trayio/axios",
|
|
3
|
+
"version": "0.0.1-beta",
|
|
4
|
+
"description": "Axios extensions and implementations",
|
|
5
|
+
"exports": {
|
|
6
|
+
"./*": "./dist/*.js"
|
|
7
|
+
},
|
|
8
|
+
"author": "Tray.io",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": ">=18.x"
|
|
12
|
+
},
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"access": "public"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@trayio/commons": "0.0.1-beta",
|
|
18
|
+
"axios": "1.6.0",
|
|
19
|
+
"form-data": "4.0.0"
|
|
20
|
+
},
|
|
21
|
+
"typesVersions": {
|
|
22
|
+
"*": {
|
|
23
|
+
"*": [
|
|
24
|
+
"dist/*"
|
|
25
|
+
]
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"/dist"
|
|
30
|
+
],
|
|
31
|
+
"stableVersion": "2.15.0"
|
|
32
|
+
}
|