@smartlyio/oats-axios-adapter 4.0.2 → 4.0.3
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/dist/index.d.ts +30 -0
- package/dist/index.js +116 -0
- package/dist/index.js.map +1 -0
- package/dist/src/utils.d.ts +6 -0
- package/dist/src/utils.js +29 -0
- package/dist/src/utils.js.map +1 -0
- package/package.json +3 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as runtime from '@smartlyio/oats-runtime';
|
|
2
|
+
import { AxiosInstance } from 'axios';
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated By default axios suffixes query array parameter names with "[]".
|
|
5
|
+
* This is likely to break oats request validation.
|
|
6
|
+
* See https://github.com/axios/axios/issues/2840.
|
|
7
|
+
* Use `create()` instead.
|
|
8
|
+
*/
|
|
9
|
+
export declare const bind: runtime.client.ClientAdapter;
|
|
10
|
+
/**
|
|
11
|
+
* @deprecated By default axios suffixes query array parameter names with "[]".
|
|
12
|
+
* This is likely to break oats request validation.
|
|
13
|
+
* See https://github.com/axios/axios/issues/2840.
|
|
14
|
+
* Use `create()` instead.
|
|
15
|
+
*/
|
|
16
|
+
export declare function withAxios(axiosInstance: AxiosInstance): runtime.client.ClientAdapter;
|
|
17
|
+
export interface AxiosAdapterOptions {
|
|
18
|
+
axiosInstance: AxiosInstance;
|
|
19
|
+
/**
|
|
20
|
+
* Whether to preserve query array param names and do not suffix them with "[]".
|
|
21
|
+
* By default axios will append "[]" suffix which is likely to break oats request validation.
|
|
22
|
+
* See https://github.com/axios/axios/issues/2840.
|
|
23
|
+
* @default true
|
|
24
|
+
*/
|
|
25
|
+
preserveQueryArrayParamNames: boolean;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* @returns oats runtime client adapter.
|
|
29
|
+
*/
|
|
30
|
+
export declare function create({ axiosInstance, preserveQueryArrayParamNames }?: Partial<AxiosAdapterOptions>): runtime.client.ClientAdapter;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.create = exports.withAxios = exports.bind = void 0;
|
|
4
|
+
const runtime = require("@smartlyio/oats-runtime");
|
|
5
|
+
const FormData = require("form-data");
|
|
6
|
+
const assert = require("assert");
|
|
7
|
+
const axios_1 = require("axios");
|
|
8
|
+
const utils_1 = require("./src/utils");
|
|
9
|
+
function toRequestData(data) {
|
|
10
|
+
if (data == null) {
|
|
11
|
+
return data;
|
|
12
|
+
}
|
|
13
|
+
if (data.contentType === 'application/json') {
|
|
14
|
+
return axiosToJson(data.value);
|
|
15
|
+
}
|
|
16
|
+
if (['application/x-www-form-urlencoded', 'multipart/form-data'].indexOf(data.contentType) >= 0) {
|
|
17
|
+
const form = new FormData();
|
|
18
|
+
Object.keys(data.value).forEach(key => {
|
|
19
|
+
const element = data.value[key];
|
|
20
|
+
if (element instanceof runtime.make.FormBinary) {
|
|
21
|
+
form.append(key, element.binary, element.options);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
form.append(key, element);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
return form;
|
|
28
|
+
}
|
|
29
|
+
assert.fail('unknown content type for axios client ' + data.contentType);
|
|
30
|
+
}
|
|
31
|
+
function axiosToJson(data) {
|
|
32
|
+
if (data instanceof runtime.valueClass.ValueClass) {
|
|
33
|
+
return runtime.valueClass.toJSON(data);
|
|
34
|
+
}
|
|
35
|
+
return data;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* @deprecated By default axios suffixes query array parameter names with "[]".
|
|
39
|
+
* This is likely to break oats request validation.
|
|
40
|
+
* See https://github.com/axios/axios/issues/2840.
|
|
41
|
+
* Use `create()` instead.
|
|
42
|
+
*/
|
|
43
|
+
exports.bind = createAxiosAdapter({
|
|
44
|
+
axiosInstance: axios_1.default,
|
|
45
|
+
preserveQueryArrayParamNames: false
|
|
46
|
+
});
|
|
47
|
+
/**
|
|
48
|
+
* @deprecated By default axios suffixes query array parameter names with "[]".
|
|
49
|
+
* This is likely to break oats request validation.
|
|
50
|
+
* See https://github.com/axios/axios/issues/2840.
|
|
51
|
+
* Use `create()` instead.
|
|
52
|
+
*/
|
|
53
|
+
function withAxios(axiosInstance) {
|
|
54
|
+
return createAxiosAdapter({ axiosInstance, preserveQueryArrayParamNames: false });
|
|
55
|
+
}
|
|
56
|
+
exports.withAxios = withAxios;
|
|
57
|
+
/**
|
|
58
|
+
* @returns oats runtime client adapter.
|
|
59
|
+
*/
|
|
60
|
+
function create({ axiosInstance = axios_1.default, preserveQueryArrayParamNames = true } = {}) {
|
|
61
|
+
return createAxiosAdapter({ axiosInstance, preserveQueryArrayParamNames });
|
|
62
|
+
}
|
|
63
|
+
exports.create = create;
|
|
64
|
+
function createAxiosAdapter({ axiosInstance, preserveQueryArrayParamNames }) {
|
|
65
|
+
return async (arg) => {
|
|
66
|
+
var _a;
|
|
67
|
+
if (arg.servers.length !== 1) {
|
|
68
|
+
return assert.fail('cannot decide which server to use from ' + arg.servers.join(', '));
|
|
69
|
+
}
|
|
70
|
+
const server = arg.servers[0];
|
|
71
|
+
const params = axiosToJson(arg.query);
|
|
72
|
+
const data = toRequestData(arg.body);
|
|
73
|
+
const url = server + arg.path;
|
|
74
|
+
const headers = Object.assign(Object.assign({}, arg.headers), (data instanceof FormData ? data.getHeaders() : {}));
|
|
75
|
+
const response = await axiosInstance.request({
|
|
76
|
+
method: arg.method,
|
|
77
|
+
headers,
|
|
78
|
+
url,
|
|
79
|
+
params,
|
|
80
|
+
paramsSerializer: preserveQueryArrayParamNames ? utils_1.urlSearchParamsSerializer : undefined,
|
|
81
|
+
data,
|
|
82
|
+
validateStatus: () => true
|
|
83
|
+
});
|
|
84
|
+
const contentType = getContentType(response);
|
|
85
|
+
return {
|
|
86
|
+
status: response.status,
|
|
87
|
+
value: {
|
|
88
|
+
contentType,
|
|
89
|
+
value: getResponseData(contentType, response)
|
|
90
|
+
},
|
|
91
|
+
headers: (_a = response.headers) !== null && _a !== void 0 ? _a : {}
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function getContentType(response) {
|
|
96
|
+
const type = response.headers['content-type'];
|
|
97
|
+
if (!type) {
|
|
98
|
+
return runtime.noContentContentType;
|
|
99
|
+
}
|
|
100
|
+
if (response.status === 204) {
|
|
101
|
+
return 'text/plain';
|
|
102
|
+
}
|
|
103
|
+
return type.split(';')[0].trim();
|
|
104
|
+
}
|
|
105
|
+
function getResponseData(contentType, response) {
|
|
106
|
+
if (contentType === runtime.noContentContentType) {
|
|
107
|
+
if (!response.data) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (response.status === 204) {
|
|
112
|
+
return '';
|
|
113
|
+
}
|
|
114
|
+
return response.data;
|
|
115
|
+
}
|
|
116
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;AAAA,mDAAmD;AACnD,sCAAsC;AACtC,iCAAiC;AACjC,iCAAkE;AAClE,uCAAwD;AAExD,SAAS,aAAa,CAAC,IAAiD;IACtE,IAAI,IAAI,IAAI,IAAI,EAAE;QAChB,OAAO,IAAI,CAAC;KACb;IACD,IAAI,IAAI,CAAC,WAAW,KAAK,kBAAkB,EAAE;QAC3C,OAAO,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;KAChC;IACD,IAAI,CAAC,mCAAmC,EAAE,qBAAqB,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,EAAE;QAC/F,MAAM,IAAI,GAAG,IAAI,QAAQ,EAAE,CAAC;QAC5B,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,EA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AAC,cAAc,CAAC,CAAC;IAC9C,IAAI,CAAC,IAAI,EAAE;QACT,OAAO,OAAO,CAAC,oBAAoB,CAAC;KACrC;IACD,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,EAAE;QAC3B,OAAO,YAAY,CAAC;KACrB;IACD,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;AACnC,CAAC;AAED,SAAS,eAAe,CAAC,WAAmB,EAAE,QAA4B;IACxE,IAAI,WAAW,KAAK,OAAO,CAAC,oBAAoB,EAAE;QAChD,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE;YAClB,OAAO,IAAI,CAAC;SACb;KACF;IACD,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,EAAE;QAC3B,OAAO,EAAE,CAAC;KACX;IACD,OAAO,QAAQ,CAAC,IAAI,CAAC;AACvB,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.urlSearchParamsSerializer = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Use `URLSearchParams` instead of passing raw data to axios.
|
|
6
|
+
* Otherwise axios will suffix array query parameters with "[]".
|
|
7
|
+
* @see https://github.com/axios/axios/issues/2840
|
|
8
|
+
*/
|
|
9
|
+
function urlSearchParamsSerializer(params) {
|
|
10
|
+
if (typeof params !== 'object') {
|
|
11
|
+
return String(params);
|
|
12
|
+
}
|
|
13
|
+
if (params === null) {
|
|
14
|
+
return '';
|
|
15
|
+
}
|
|
16
|
+
if (params instanceof URLSearchParams) {
|
|
17
|
+
return params.toString();
|
|
18
|
+
}
|
|
19
|
+
const queryParams = new URLSearchParams();
|
|
20
|
+
Object.keys(params).forEach(key => {
|
|
21
|
+
const value = params[key];
|
|
22
|
+
for (const item of [].concat(value)) {
|
|
23
|
+
queryParams.append(key, String(item));
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
return queryParams.toString();
|
|
27
|
+
}
|
|
28
|
+
exports.urlSearchParamsSerializer = urlSearchParamsSerializer;
|
|
29
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";;;AAAA;;;;GAIG;AACH,SAAgB,yBAAyB,CAAC,MAAe;IACvD,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE;QAC9B,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC;KACvB;IACD,IAAI,MAAM,KAAK,IAAI,EAAE;QACnB,OAAO,EAAE,CAAC;KACX;IACD,IAAI,MAAM,YAAY,eAAe,EAAE;QACrC,OAAO,MAAM,CAAC,QAAQ,EAAE,CAAC;KAC1B;IACD,MAAM,WAAW,GAAG,IAAI,eAAe,EAAE,CAAC;IAC1C,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;QAChC,MAAM,KAAK,GAAI,MAAkC,CAAC,GAAG,CAAC,CAAC;QACvD,KAAK,MAAM,IAAI,IAAK,EAAgB,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE;YAClD,WAAW,CAAC,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;SACvC;IACH,CAAC,CAAC,CAAC;IACH,OAAO,WAAW,CAAC,QAAQ,EAAE,CAAC;AAChC,CAAC;AAlBD,8DAkBC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smartlyio/oats-axios-adapter",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.3",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Axios adapter for Oats a Openapi3 based generator for typescript aware servers and clients",
|
|
6
6
|
"private": false,
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
],
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@jest/globals": "27.4.4",
|
|
38
|
-
"@smartlyio/oats-runtime": "^4.0.
|
|
38
|
+
"@smartlyio/oats-runtime": "^4.0.3",
|
|
39
39
|
"@types/form-data": "2.5.0",
|
|
40
40
|
"@types/node": "16.11.14",
|
|
41
41
|
"axios": "0.24.0",
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"ts-jest": "27.1.2",
|
|
44
44
|
"typescript": "4.5.4"
|
|
45
45
|
},
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "5897af3aa6bea0c602978a10816c577a6b051c90"
|
|
47
47
|
}
|