@trayio/axios 0.11.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.
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,9 @@
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
+ export declare class AxiosHttpClient implements HttpClient {
5
+ execute(method: HttpMethod, url: string, request: HttpRequest): TE.TaskEither<Error, HttpResponse>;
6
+ private axiosErrorToHttpResponse;
7
+ private axiosResponseToHttpResponse;
8
+ }
9
+ //# 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,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"}
@@ -0,0 +1,57 @@
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 Task_1 = require("@trayio/commons/task/Task");
8
+ const axios_1 = __importDefault(require("axios"));
9
+ class AxiosHttpClient {
10
+ execute(method, url, request) {
11
+ const finalUrl = Object.entries(request.pathParams).reduce((acc, [key, value]) => acc.replace(`:${key}`, encodeURIComponent(value)), url);
12
+ const headers = Object.entries(request.headers).reduce((acc, [key, value]) => {
13
+ const newValue = typeof value === 'string' ? value : value.join(', ');
14
+ return Object.assign(Object.assign({}, acc), { [key]: newValue });
15
+ }, {});
16
+ const axiosConfig = {
17
+ url: finalUrl,
18
+ method: method.toString(),
19
+ data: request.body,
20
+ responseType: 'arraybuffer',
21
+ headers,
22
+ params: request.queryString,
23
+ };
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
+ return (0, Task_1.createTaskEitherFromPromiseWithSimpleError)(() => (0, axios_1.default)(axiosConfig)
36
+ .then(this.axiosResponseToHttpResponse)
37
+ .catch(this.axiosErrorToHttpResponse.bind(this)));
38
+ }
39
+ axiosErrorToHttpResponse(axiosError) {
40
+ if (axiosError.response !== undefined) {
41
+ return this.axiosResponseToHttpResponse(axiosError.response);
42
+ }
43
+ return {
44
+ headers: {},
45
+ statusCode: 500,
46
+ body: new ArrayBuffer(0),
47
+ };
48
+ }
49
+ axiosResponseToHttpResponse(axiosResponse) {
50
+ return {
51
+ headers: axiosResponse.headers,
52
+ statusCode: axiosResponse.status,
53
+ body: axiosResponse.data,
54
+ };
55
+ }
56
+ }
57
+ exports.AxiosHttpClient = AxiosHttpClient;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=AxiosHttpClient.unit.test.d.ts.map
@@ -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,5 @@
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
+ (0, HttpClient_abstract_test_1.httpClientTest)(new AxiosHttpClient_1.AxiosHttpClient());
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@trayio/axios",
3
+ "version": "0.11.0",
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.11.0",
18
+ "axios": "*"
19
+ },
20
+ "typesVersions": {
21
+ "*": {
22
+ "*": [
23
+ "dist/*"
24
+ ]
25
+ }
26
+ },
27
+ "files": [
28
+ "/dist"
29
+ ]
30
+ }