@ps-aux/api-client-axios 0.7.0-rc.1 → 0.7.0-rc.2
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/README.md +16 -10
- package/dist/browser.cjs +211 -0
- package/dist/browser.d.cts +39 -0
- package/dist/browser.d.mts +39 -0
- package/dist/browser.d.ts +21 -21
- package/dist/browser.mjs +209 -0
- package/dist/node.cjs +211 -0
- package/dist/node.d.cts +39 -0
- package/dist/node.d.mts +39 -0
- package/dist/node.d.ts +21 -21
- package/dist/node.mjs +209 -0
- package/package.json +23 -17
- package/dist/browser.esm.js +0 -110
- package/dist/browser.js +0 -112
- package/dist/node.esm.js +0 -132
- package/dist/node.js +0 -134
package/dist/node.cjs
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fileResFilenameRegex = /filename="([^"]+)"/;
|
|
4
|
+
const getFileNameFromContentDispositionHeader = (contentDispositionHeader) => {
|
|
5
|
+
if (!contentDispositionHeader) return "download-file";
|
|
6
|
+
const match = contentDispositionHeader.match(fileResFilenameRegex);
|
|
7
|
+
const fileName = match ? match[1] : "downloaded-file";
|
|
8
|
+
return fileName;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const convertToFormData = (payload) => {
|
|
12
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
13
|
+
throw new Error("Multipart payload must be an object");
|
|
14
|
+
}
|
|
15
|
+
const formData = new FormData();
|
|
16
|
+
const source = payload;
|
|
17
|
+
const addProp = (key, value) => {
|
|
18
|
+
if (value === void 0) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (Array.isArray(value)) {
|
|
22
|
+
value.forEach((item) => addProp(key, item));
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (isFileList(value)) {
|
|
26
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
27
|
+
addProp(key, value.item(i));
|
|
28
|
+
}
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (isBlob(value)) {
|
|
32
|
+
formData.append(key, value);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (isNodeFileDescriptor(value)) {
|
|
36
|
+
const file = fileDescriptorToBlob(value);
|
|
37
|
+
formData.append(key, file, value.name);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (typeof value === "object" && value != null) {
|
|
41
|
+
throw new Error(
|
|
42
|
+
`Object serialization into FormData not supported: ${safeStringify(value)}`
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
formData.append(key, String(value));
|
|
46
|
+
};
|
|
47
|
+
Object.entries(source).forEach(([key, value]) => addProp(key, value));
|
|
48
|
+
return formData;
|
|
49
|
+
};
|
|
50
|
+
const withoutContentTypeHeader = (headers) => {
|
|
51
|
+
return Object.fromEntries(
|
|
52
|
+
Object.entries(headers).filter(
|
|
53
|
+
([key]) => key.toLowerCase() !== "content-type"
|
|
54
|
+
)
|
|
55
|
+
);
|
|
56
|
+
};
|
|
57
|
+
const isBlob = (value) => {
|
|
58
|
+
return typeof Blob !== "undefined" && value instanceof Blob;
|
|
59
|
+
};
|
|
60
|
+
const isFileList = (value) => {
|
|
61
|
+
return typeof FileList !== "undefined" && value instanceof FileList;
|
|
62
|
+
};
|
|
63
|
+
const isNodeFileDescriptor = (value) => {
|
|
64
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
const typed = value;
|
|
68
|
+
return typeof typed.name === "string" && "file" in typed;
|
|
69
|
+
};
|
|
70
|
+
const fileDescriptorToBlob = (descriptor) => {
|
|
71
|
+
const { file, type } = descriptor;
|
|
72
|
+
if (isBlob(file)) {
|
|
73
|
+
if (type && type !== file.type) {
|
|
74
|
+
return new Blob([file], { type });
|
|
75
|
+
}
|
|
76
|
+
return file;
|
|
77
|
+
}
|
|
78
|
+
if (typeof Buffer !== "undefined" && Buffer.isBuffer(file)) {
|
|
79
|
+
const bytes = new Uint8Array(file.byteLength);
|
|
80
|
+
bytes.set(file);
|
|
81
|
+
return new Blob([bytes.buffer], { type });
|
|
82
|
+
}
|
|
83
|
+
if (file instanceof ArrayBuffer) {
|
|
84
|
+
return new Blob([file], { type });
|
|
85
|
+
}
|
|
86
|
+
if (ArrayBuffer.isView(file)) {
|
|
87
|
+
const bytes = new Uint8Array(file.byteLength);
|
|
88
|
+
bytes.set(new Uint8Array(file.buffer, file.byteOffset, file.byteLength));
|
|
89
|
+
return new Blob([bytes.buffer], { type });
|
|
90
|
+
}
|
|
91
|
+
throw new Error(
|
|
92
|
+
"Object serialization into FormData not supported: Readable stream file descriptors are not supported"
|
|
93
|
+
);
|
|
94
|
+
};
|
|
95
|
+
const safeStringify = (value) => {
|
|
96
|
+
try {
|
|
97
|
+
return JSON.stringify(value);
|
|
98
|
+
} catch {
|
|
99
|
+
return String(value);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
class HttpClientHelper {
|
|
104
|
+
validateRequest(req) {
|
|
105
|
+
this.assertRelativePathWithoutQuery(req.path);
|
|
106
|
+
}
|
|
107
|
+
assertRelativePathWithoutQuery(path) {
|
|
108
|
+
if (!path.startsWith("/")) {
|
|
109
|
+
throw new Error('Request path must start with "/".');
|
|
110
|
+
}
|
|
111
|
+
if (path.includes("?")) {
|
|
112
|
+
throw new Error(
|
|
113
|
+
"Request path must not contain a query string. Use req.query instead."
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
withQueryString(path, query, querySerializer) {
|
|
118
|
+
if (!query) {
|
|
119
|
+
return path;
|
|
120
|
+
}
|
|
121
|
+
const queryString = this.serializeQuery(query, querySerializer);
|
|
122
|
+
if (!queryString) {
|
|
123
|
+
return path;
|
|
124
|
+
}
|
|
125
|
+
return `${path}?${queryString}`;
|
|
126
|
+
}
|
|
127
|
+
serializeQuery(query, querySerializer) {
|
|
128
|
+
if (querySerializer) {
|
|
129
|
+
return querySerializer(query);
|
|
130
|
+
}
|
|
131
|
+
return this.toDefaultQueryString(query);
|
|
132
|
+
}
|
|
133
|
+
resolveUrl(path, baseUrl) {
|
|
134
|
+
if (!baseUrl) {
|
|
135
|
+
return path;
|
|
136
|
+
}
|
|
137
|
+
return new URL(path, baseUrl).toString();
|
|
138
|
+
}
|
|
139
|
+
convertToFormData(payload) {
|
|
140
|
+
return convertToFormData(payload);
|
|
141
|
+
}
|
|
142
|
+
withoutContentTypeHeader(headers) {
|
|
143
|
+
return withoutContentTypeHeader(headers);
|
|
144
|
+
}
|
|
145
|
+
getFileNameFromContentDispositionHeader(contentDispositionHeader) {
|
|
146
|
+
return getFileNameFromContentDispositionHeader(contentDispositionHeader);
|
|
147
|
+
}
|
|
148
|
+
toDefaultQueryString(query) {
|
|
149
|
+
const params = new URLSearchParams();
|
|
150
|
+
const addValue = (key, value) => {
|
|
151
|
+
if (value === void 0) return;
|
|
152
|
+
if (Array.isArray(value)) {
|
|
153
|
+
value.forEach((item) => addValue(key, item));
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
params.append(key, String(value));
|
|
157
|
+
};
|
|
158
|
+
Object.entries(query).forEach(([key, value]) => addValue(key, value));
|
|
159
|
+
return params.toString();
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const createHttpClient$1 = (axios, opts) => {
|
|
164
|
+
const { querySerializer } = opts;
|
|
165
|
+
const helper = new HttpClientHelper();
|
|
166
|
+
return {
|
|
167
|
+
request: (req) => {
|
|
168
|
+
helper.validateRequest(req);
|
|
169
|
+
const { query, requestContentType, body, headers: reqHeaders } = req;
|
|
170
|
+
const paramsSerializer = querySerializer ? (r) => helper.serializeQuery(r, querySerializer) : void 0;
|
|
171
|
+
const isBlobResponse = req.format === "document";
|
|
172
|
+
const headers = { ...reqHeaders || {} };
|
|
173
|
+
let data = body;
|
|
174
|
+
if (requestContentType === "multipart/form-data") {
|
|
175
|
+
data = helper.convertToFormData(body);
|
|
176
|
+
} else if (requestContentType) {
|
|
177
|
+
headers["Content-Type"] = requestContentType;
|
|
178
|
+
}
|
|
179
|
+
return axios.request({
|
|
180
|
+
method: req.method,
|
|
181
|
+
url: req.path,
|
|
182
|
+
params: query,
|
|
183
|
+
paramsSerializer,
|
|
184
|
+
data,
|
|
185
|
+
headers,
|
|
186
|
+
responseType: isBlobResponse ? "arraybuffer" : "json"
|
|
187
|
+
}).then((r) => {
|
|
188
|
+
const { data: data2 } = r;
|
|
189
|
+
if (isBlobResponse) {
|
|
190
|
+
const contDist = r.headers["content-disposition"];
|
|
191
|
+
const fileName = helper.getFileNameFromContentDispositionHeader(
|
|
192
|
+
contDist
|
|
193
|
+
);
|
|
194
|
+
const type = r.headers["content-type"];
|
|
195
|
+
return new File([data2], fileName, {
|
|
196
|
+
type
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
return data2;
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
const createHttpClient = (axios, opts) => {
|
|
206
|
+
return createHttpClient$1(axios, {
|
|
207
|
+
querySerializer: opts.querySerializer
|
|
208
|
+
});
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
exports.createHttpClient = createHttpClient;
|
package/dist/node.d.cts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { AxiosInstance } from 'axios';
|
|
2
|
+
|
|
3
|
+
type KnownRequestContentType = 'application/json' | 'multipart/form-data' | 'application/x-www-form-urlencoded';
|
|
4
|
+
type RequestContentType = KnownRequestContentType | string;
|
|
5
|
+
type QueryValue = string | number | boolean | null | undefined | QueryValue[] | Record<string, any> | unknown;
|
|
6
|
+
type QueryParams = Record<string, QueryValue>;
|
|
7
|
+
type Request = {
|
|
8
|
+
path: string;
|
|
9
|
+
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
10
|
+
format?: 'json' | 'document';
|
|
11
|
+
headers?: Record<string, string>;
|
|
12
|
+
query?: QueryParams;
|
|
13
|
+
body?: any;
|
|
14
|
+
requestContentType?: RequestContentType;
|
|
15
|
+
};
|
|
16
|
+
type QuerySerializer = (params: QueryParams) => string;
|
|
17
|
+
|
|
18
|
+
type HttpClient<RequestParams = never> = {
|
|
19
|
+
request: <Data>(req: Request, params?: RequestParams) => Promise<Data>;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/// <reference lib="esnext.asynciterable" />
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Note: This will add Symbol.observable globally for all TypeScript users,
|
|
26
|
+
* however, we are no longer polyfilling Symbol.observable
|
|
27
|
+
*/
|
|
28
|
+
declare global {
|
|
29
|
+
interface SymbolConstructor {
|
|
30
|
+
readonly observable: symbol;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
declare const createHttpClient: (axios: AxiosInstance, opts: {
|
|
35
|
+
querySerializer: QuerySerializer | null;
|
|
36
|
+
}) => HttpClient;
|
|
37
|
+
|
|
38
|
+
export { createHttpClient };
|
|
39
|
+
export type { HttpClient as PromiseHttpClient, QuerySerializer };
|
package/dist/node.d.mts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { AxiosInstance } from 'axios';
|
|
2
|
+
|
|
3
|
+
type KnownRequestContentType = 'application/json' | 'multipart/form-data' | 'application/x-www-form-urlencoded';
|
|
4
|
+
type RequestContentType = KnownRequestContentType | string;
|
|
5
|
+
type QueryValue = string | number | boolean | null | undefined | QueryValue[] | Record<string, any> | unknown;
|
|
6
|
+
type QueryParams = Record<string, QueryValue>;
|
|
7
|
+
type Request = {
|
|
8
|
+
path: string;
|
|
9
|
+
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
10
|
+
format?: 'json' | 'document';
|
|
11
|
+
headers?: Record<string, string>;
|
|
12
|
+
query?: QueryParams;
|
|
13
|
+
body?: any;
|
|
14
|
+
requestContentType?: RequestContentType;
|
|
15
|
+
};
|
|
16
|
+
type QuerySerializer = (params: QueryParams) => string;
|
|
17
|
+
|
|
18
|
+
type HttpClient<RequestParams = never> = {
|
|
19
|
+
request: <Data>(req: Request, params?: RequestParams) => Promise<Data>;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/// <reference lib="esnext.asynciterable" />
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Note: This will add Symbol.observable globally for all TypeScript users,
|
|
26
|
+
* however, we are no longer polyfilling Symbol.observable
|
|
27
|
+
*/
|
|
28
|
+
declare global {
|
|
29
|
+
interface SymbolConstructor {
|
|
30
|
+
readonly observable: symbol;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
declare const createHttpClient: (axios: AxiosInstance, opts: {
|
|
35
|
+
querySerializer: QuerySerializer | null;
|
|
36
|
+
}) => HttpClient;
|
|
37
|
+
|
|
38
|
+
export { createHttpClient };
|
|
39
|
+
export type { HttpClient as PromiseHttpClient, QuerySerializer };
|
package/dist/node.d.ts
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
import { AxiosInstance } from 'axios';
|
|
2
2
|
|
|
3
|
+
type KnownRequestContentType = 'application/json' | 'multipart/form-data' | 'application/x-www-form-urlencoded';
|
|
4
|
+
type RequestContentType = KnownRequestContentType | string;
|
|
5
|
+
type QueryValue = string | number | boolean | null | undefined | QueryValue[] | Record<string, any> | unknown;
|
|
6
|
+
type QueryParams = Record<string, QueryValue>;
|
|
7
|
+
type Request = {
|
|
8
|
+
path: string;
|
|
9
|
+
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
10
|
+
format?: 'json' | 'document';
|
|
11
|
+
headers?: Record<string, string>;
|
|
12
|
+
query?: QueryParams;
|
|
13
|
+
body?: any;
|
|
14
|
+
requestContentType?: RequestContentType;
|
|
15
|
+
};
|
|
16
|
+
type QuerySerializer = (params: QueryParams) => string;
|
|
17
|
+
|
|
18
|
+
type HttpClient<RequestParams = never> = {
|
|
19
|
+
request: <Data>(req: Request, params?: RequestParams) => Promise<Data>;
|
|
20
|
+
};
|
|
21
|
+
|
|
3
22
|
/// <reference lib="esnext.asynciterable" />
|
|
4
23
|
|
|
5
24
|
/**
|
|
@@ -12,28 +31,9 @@ declare global {
|
|
|
12
31
|
}
|
|
13
32
|
}
|
|
14
33
|
|
|
15
|
-
type RequestContentType = 'application/json' | 'multipart/form-data';
|
|
16
|
-
type Request = {
|
|
17
|
-
path: string;
|
|
18
|
-
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
19
|
-
format?: 'json' | 'document';
|
|
20
|
-
headers?: Record<string, string>;
|
|
21
|
-
query?: Record<string, unknown>;
|
|
22
|
-
body?: any;
|
|
23
|
-
requestContentType?: RequestContentType;
|
|
24
|
-
};
|
|
25
|
-
type QuerySerializer = (req: {
|
|
26
|
-
queryParams: Record<string, unknown>;
|
|
27
|
-
}) => {
|
|
28
|
-
queryString: string;
|
|
29
|
-
};
|
|
30
|
-
type PromiseHttpClient<RequestParams = never> = {
|
|
31
|
-
request: <Data>(req: Request, params?: RequestParams) => Promise<Data>;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
34
|
declare const createHttpClient: (axios: AxiosInstance, opts: {
|
|
35
35
|
querySerializer: QuerySerializer | null;
|
|
36
|
-
}) =>
|
|
36
|
+
}) => HttpClient;
|
|
37
37
|
|
|
38
38
|
export { createHttpClient };
|
|
39
|
-
export type { PromiseHttpClient, QuerySerializer };
|
|
39
|
+
export type { HttpClient as PromiseHttpClient, QuerySerializer };
|
package/dist/node.mjs
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
const fileResFilenameRegex = /filename="([^"]+)"/;
|
|
2
|
+
const getFileNameFromContentDispositionHeader = (contentDispositionHeader) => {
|
|
3
|
+
if (!contentDispositionHeader) return "download-file";
|
|
4
|
+
const match = contentDispositionHeader.match(fileResFilenameRegex);
|
|
5
|
+
const fileName = match ? match[1] : "downloaded-file";
|
|
6
|
+
return fileName;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const convertToFormData = (payload) => {
|
|
10
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
11
|
+
throw new Error("Multipart payload must be an object");
|
|
12
|
+
}
|
|
13
|
+
const formData = new FormData();
|
|
14
|
+
const source = payload;
|
|
15
|
+
const addProp = (key, value) => {
|
|
16
|
+
if (value === void 0) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (Array.isArray(value)) {
|
|
20
|
+
value.forEach((item) => addProp(key, item));
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (isFileList(value)) {
|
|
24
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
25
|
+
addProp(key, value.item(i));
|
|
26
|
+
}
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (isBlob(value)) {
|
|
30
|
+
formData.append(key, value);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (isNodeFileDescriptor(value)) {
|
|
34
|
+
const file = fileDescriptorToBlob(value);
|
|
35
|
+
formData.append(key, file, value.name);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (typeof value === "object" && value != null) {
|
|
39
|
+
throw new Error(
|
|
40
|
+
`Object serialization into FormData not supported: ${safeStringify(value)}`
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
formData.append(key, String(value));
|
|
44
|
+
};
|
|
45
|
+
Object.entries(source).forEach(([key, value]) => addProp(key, value));
|
|
46
|
+
return formData;
|
|
47
|
+
};
|
|
48
|
+
const withoutContentTypeHeader = (headers) => {
|
|
49
|
+
return Object.fromEntries(
|
|
50
|
+
Object.entries(headers).filter(
|
|
51
|
+
([key]) => key.toLowerCase() !== "content-type"
|
|
52
|
+
)
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
const isBlob = (value) => {
|
|
56
|
+
return typeof Blob !== "undefined" && value instanceof Blob;
|
|
57
|
+
};
|
|
58
|
+
const isFileList = (value) => {
|
|
59
|
+
return typeof FileList !== "undefined" && value instanceof FileList;
|
|
60
|
+
};
|
|
61
|
+
const isNodeFileDescriptor = (value) => {
|
|
62
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
const typed = value;
|
|
66
|
+
return typeof typed.name === "string" && "file" in typed;
|
|
67
|
+
};
|
|
68
|
+
const fileDescriptorToBlob = (descriptor) => {
|
|
69
|
+
const { file, type } = descriptor;
|
|
70
|
+
if (isBlob(file)) {
|
|
71
|
+
if (type && type !== file.type) {
|
|
72
|
+
return new Blob([file], { type });
|
|
73
|
+
}
|
|
74
|
+
return file;
|
|
75
|
+
}
|
|
76
|
+
if (typeof Buffer !== "undefined" && Buffer.isBuffer(file)) {
|
|
77
|
+
const bytes = new Uint8Array(file.byteLength);
|
|
78
|
+
bytes.set(file);
|
|
79
|
+
return new Blob([bytes.buffer], { type });
|
|
80
|
+
}
|
|
81
|
+
if (file instanceof ArrayBuffer) {
|
|
82
|
+
return new Blob([file], { type });
|
|
83
|
+
}
|
|
84
|
+
if (ArrayBuffer.isView(file)) {
|
|
85
|
+
const bytes = new Uint8Array(file.byteLength);
|
|
86
|
+
bytes.set(new Uint8Array(file.buffer, file.byteOffset, file.byteLength));
|
|
87
|
+
return new Blob([bytes.buffer], { type });
|
|
88
|
+
}
|
|
89
|
+
throw new Error(
|
|
90
|
+
"Object serialization into FormData not supported: Readable stream file descriptors are not supported"
|
|
91
|
+
);
|
|
92
|
+
};
|
|
93
|
+
const safeStringify = (value) => {
|
|
94
|
+
try {
|
|
95
|
+
return JSON.stringify(value);
|
|
96
|
+
} catch {
|
|
97
|
+
return String(value);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
class HttpClientHelper {
|
|
102
|
+
validateRequest(req) {
|
|
103
|
+
this.assertRelativePathWithoutQuery(req.path);
|
|
104
|
+
}
|
|
105
|
+
assertRelativePathWithoutQuery(path) {
|
|
106
|
+
if (!path.startsWith("/")) {
|
|
107
|
+
throw new Error('Request path must start with "/".');
|
|
108
|
+
}
|
|
109
|
+
if (path.includes("?")) {
|
|
110
|
+
throw new Error(
|
|
111
|
+
"Request path must not contain a query string. Use req.query instead."
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
withQueryString(path, query, querySerializer) {
|
|
116
|
+
if (!query) {
|
|
117
|
+
return path;
|
|
118
|
+
}
|
|
119
|
+
const queryString = this.serializeQuery(query, querySerializer);
|
|
120
|
+
if (!queryString) {
|
|
121
|
+
return path;
|
|
122
|
+
}
|
|
123
|
+
return `${path}?${queryString}`;
|
|
124
|
+
}
|
|
125
|
+
serializeQuery(query, querySerializer) {
|
|
126
|
+
if (querySerializer) {
|
|
127
|
+
return querySerializer(query);
|
|
128
|
+
}
|
|
129
|
+
return this.toDefaultQueryString(query);
|
|
130
|
+
}
|
|
131
|
+
resolveUrl(path, baseUrl) {
|
|
132
|
+
if (!baseUrl) {
|
|
133
|
+
return path;
|
|
134
|
+
}
|
|
135
|
+
return new URL(path, baseUrl).toString();
|
|
136
|
+
}
|
|
137
|
+
convertToFormData(payload) {
|
|
138
|
+
return convertToFormData(payload);
|
|
139
|
+
}
|
|
140
|
+
withoutContentTypeHeader(headers) {
|
|
141
|
+
return withoutContentTypeHeader(headers);
|
|
142
|
+
}
|
|
143
|
+
getFileNameFromContentDispositionHeader(contentDispositionHeader) {
|
|
144
|
+
return getFileNameFromContentDispositionHeader(contentDispositionHeader);
|
|
145
|
+
}
|
|
146
|
+
toDefaultQueryString(query) {
|
|
147
|
+
const params = new URLSearchParams();
|
|
148
|
+
const addValue = (key, value) => {
|
|
149
|
+
if (value === void 0) return;
|
|
150
|
+
if (Array.isArray(value)) {
|
|
151
|
+
value.forEach((item) => addValue(key, item));
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
params.append(key, String(value));
|
|
155
|
+
};
|
|
156
|
+
Object.entries(query).forEach(([key, value]) => addValue(key, value));
|
|
157
|
+
return params.toString();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const createHttpClient$1 = (axios, opts) => {
|
|
162
|
+
const { querySerializer } = opts;
|
|
163
|
+
const helper = new HttpClientHelper();
|
|
164
|
+
return {
|
|
165
|
+
request: (req) => {
|
|
166
|
+
helper.validateRequest(req);
|
|
167
|
+
const { query, requestContentType, body, headers: reqHeaders } = req;
|
|
168
|
+
const paramsSerializer = querySerializer ? (r) => helper.serializeQuery(r, querySerializer) : void 0;
|
|
169
|
+
const isBlobResponse = req.format === "document";
|
|
170
|
+
const headers = { ...reqHeaders || {} };
|
|
171
|
+
let data = body;
|
|
172
|
+
if (requestContentType === "multipart/form-data") {
|
|
173
|
+
data = helper.convertToFormData(body);
|
|
174
|
+
} else if (requestContentType) {
|
|
175
|
+
headers["Content-Type"] = requestContentType;
|
|
176
|
+
}
|
|
177
|
+
return axios.request({
|
|
178
|
+
method: req.method,
|
|
179
|
+
url: req.path,
|
|
180
|
+
params: query,
|
|
181
|
+
paramsSerializer,
|
|
182
|
+
data,
|
|
183
|
+
headers,
|
|
184
|
+
responseType: isBlobResponse ? "arraybuffer" : "json"
|
|
185
|
+
}).then((r) => {
|
|
186
|
+
const { data: data2 } = r;
|
|
187
|
+
if (isBlobResponse) {
|
|
188
|
+
const contDist = r.headers["content-disposition"];
|
|
189
|
+
const fileName = helper.getFileNameFromContentDispositionHeader(
|
|
190
|
+
contDist
|
|
191
|
+
);
|
|
192
|
+
const type = r.headers["content-type"];
|
|
193
|
+
return new File([data2], fileName, {
|
|
194
|
+
type
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
return data2;
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const createHttpClient = (axios, opts) => {
|
|
204
|
+
return createHttpClient$1(axios, {
|
|
205
|
+
querySerializer: opts.querySerializer
|
|
206
|
+
});
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
export { createHttpClient };
|
package/package.json
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ps-aux/api-client-axios",
|
|
3
|
-
"version": "0.7.0-rc.
|
|
4
|
-
"main": "dist/node.
|
|
5
|
-
"
|
|
6
|
-
"browser": "dist/browser.esm.js",
|
|
7
|
-
"types": "dist/node.d.ts",
|
|
3
|
+
"version": "0.7.0-rc.2",
|
|
4
|
+
"main": "dist/node.cjs",
|
|
5
|
+
"type": "module",
|
|
8
6
|
"scripts": {
|
|
9
|
-
"build": "rollup -c",
|
|
7
|
+
"build": "npm run clean && rollup -c",
|
|
10
8
|
"clean": "rm -rf dist",
|
|
11
9
|
"lint": "eslint",
|
|
12
10
|
"prepublishOnly": "echo \"Publishing disabled for this repo. Use CI workflow.\" && exit 1",
|
|
@@ -16,11 +14,8 @@
|
|
|
16
14
|
},
|
|
17
15
|
"author": "psaux",
|
|
18
16
|
"license": "MIT",
|
|
19
|
-
"dependencies": {
|
|
20
|
-
"form-data": "^4.0.0"
|
|
21
|
-
},
|
|
22
17
|
"peerDependencies": {
|
|
23
|
-
"axios": "^1.5
|
|
18
|
+
"axios": "^1.13.5"
|
|
24
19
|
},
|
|
25
20
|
"devDependencies": {
|
|
26
21
|
"axios": "^1.5.0"
|
|
@@ -46,7 +41,7 @@
|
|
|
46
41
|
"url": "https://github.com/ps-aux/api-tests/issues"
|
|
47
42
|
},
|
|
48
43
|
"engines": {
|
|
49
|
-
"node": ">=
|
|
44
|
+
"node": ">=20.0.0"
|
|
50
45
|
},
|
|
51
46
|
"publishConfig": {
|
|
52
47
|
"access": "public"
|
|
@@ -58,13 +53,24 @@
|
|
|
58
53
|
"exports": {
|
|
59
54
|
".": {
|
|
60
55
|
"browser": {
|
|
61
|
-
"
|
|
62
|
-
|
|
63
|
-
|
|
56
|
+
"import": {
|
|
57
|
+
"types": "./dist/browser.d.mts",
|
|
58
|
+
"default": "./dist/browser.mjs"
|
|
59
|
+
},
|
|
60
|
+
"require": {
|
|
61
|
+
"types": "./dist/browser.d.cts",
|
|
62
|
+
"default": "./dist/browser.cjs"
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
"import": {
|
|
66
|
+
"types": "./dist/node.d.mts",
|
|
67
|
+
"default": "./dist/node.mjs"
|
|
68
|
+
},
|
|
69
|
+
"require": {
|
|
70
|
+
"types": "./dist/node.d.cts",
|
|
71
|
+
"default": "./dist/node.cjs"
|
|
64
72
|
},
|
|
65
|
-
"
|
|
66
|
-
"import": "./dist/node.esm.js",
|
|
67
|
-
"require": "./dist/node.js"
|
|
73
|
+
"default": "./dist/node.mjs"
|
|
68
74
|
}
|
|
69
75
|
},
|
|
70
76
|
"sideEffects": false
|