@ps-aux/api-client-axios 0.3.2-rc1 → 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.
@@ -1,374 +0,0 @@
1
- import 'qs';
2
-
3
- const convertToFormData = (payload, platform) => {
4
- const formData = platform.newFormData();
5
- const addProp = (key, val) => {
6
- if (Array.isArray(val) || platform.isFileList(val)) {
7
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
8
- // @ts-ignore - seems that FileList is iterable despite the warning
9
- // TODO change to other iteration method if this is not true
10
- for (const valItem of val) {
11
- addProp(key, valItem);
12
- }
13
- }
14
- else if (typeof val === 'object' &&
15
- val != null &&
16
- !platform.isFile(val)) {
17
- throw new Error(`Object serialization into FormData not supported for object: ${val}`);
18
- }
19
- else {
20
- if (platform.isFile(val)) {
21
- const { file, name } = platform.getFileAndName(val);
22
- formData.append(key, file, name);
23
- }
24
- else {
25
- formData.append(key, val);
26
- }
27
- }
28
- };
29
- Object.entries(payload).forEach(([key, val]) => addProp(key, val));
30
- return {
31
- data: formData,
32
- headers: platform.getHeaders(formData)
33
- };
34
- };
35
-
36
- const { isArray } = Array;
37
-
38
- function fromPairs(listOfPairs){
39
- const toReturn = {};
40
- listOfPairs.forEach(([ prop, value ]) => toReturn[ prop ] = value);
41
-
42
- return toReturn
43
- }
44
-
45
- function partitionObject(predicate, iterable){
46
- const yes = {};
47
- const no = {};
48
- Object.entries(iterable).forEach(([ prop, value ]) => {
49
- if (predicate(value, prop)){
50
- yes[ prop ] = value;
51
- } else {
52
- no[ prop ] = value;
53
- }
54
- });
55
-
56
- return [ yes, no ]
57
- }
58
-
59
- function partitionArray(
60
- predicate, list, indexed = false
61
- ){
62
- const yes = [];
63
- const no = [];
64
- let counter = -1;
65
-
66
- while (counter++ < list.length - 1){
67
- if (
68
- indexed ? predicate(list[ counter ], counter) : predicate(list[ counter ])
69
- ){
70
- yes.push(list[ counter ]);
71
- } else {
72
- no.push(list[ counter ]);
73
- }
74
- }
75
-
76
- return [ yes, no ]
77
- }
78
-
79
- function partition(predicate, iterable){
80
- if (arguments.length === 1){
81
- return listHolder => partition(predicate, listHolder)
82
- }
83
- if (!isArray(iterable)) return partitionObject(predicate, iterable)
84
-
85
- return partitionArray(predicate, iterable)
86
- }
87
-
88
- var flat = flatten$1;
89
- flatten$1.flatten = flatten$1;
90
- flatten$1.unflatten = unflatten;
91
-
92
- function isBuffer (obj) {
93
- return obj &&
94
- obj.constructor &&
95
- (typeof obj.constructor.isBuffer === 'function') &&
96
- obj.constructor.isBuffer(obj)
97
- }
98
-
99
- function keyIdentity (key) {
100
- return key
101
- }
102
-
103
- function flatten$1 (target, opts) {
104
- opts = opts || {};
105
-
106
- const delimiter = opts.delimiter || '.';
107
- const maxDepth = opts.maxDepth;
108
- const transformKey = opts.transformKey || keyIdentity;
109
- const output = {};
110
-
111
- function step (object, prev, currentDepth) {
112
- currentDepth = currentDepth || 1;
113
- Object.keys(object).forEach(function (key) {
114
- const value = object[key];
115
- const isarray = opts.safe && Array.isArray(value);
116
- const type = Object.prototype.toString.call(value);
117
- const isbuffer = isBuffer(value);
118
- const isobject = (
119
- type === '[object Object]' ||
120
- type === '[object Array]'
121
- );
122
-
123
- const newKey = prev
124
- ? prev + delimiter + transformKey(key)
125
- : transformKey(key);
126
-
127
- if (!isarray && !isbuffer && isobject && Object.keys(value).length &&
128
- (!opts.maxDepth || currentDepth < maxDepth)) {
129
- return step(value, newKey, currentDepth + 1)
130
- }
131
-
132
- output[newKey] = value;
133
- });
134
- }
135
-
136
- step(target);
137
-
138
- return output
139
- }
140
-
141
- function unflatten (target, opts) {
142
- opts = opts || {};
143
-
144
- const delimiter = opts.delimiter || '.';
145
- const overwrite = opts.overwrite || false;
146
- const transformKey = opts.transformKey || keyIdentity;
147
- const result = {};
148
-
149
- const isbuffer = isBuffer(target);
150
- if (isbuffer || Object.prototype.toString.call(target) !== '[object Object]') {
151
- return target
152
- }
153
-
154
- // safely ensure that the key is
155
- // an integer.
156
- function getkey (key) {
157
- const parsedKey = Number(key);
158
-
159
- return (
160
- isNaN(parsedKey) ||
161
- key.indexOf('.') !== -1 ||
162
- opts.object
163
- ) ? key
164
- : parsedKey
165
- }
166
-
167
- function addKeys (keyPrefix, recipient, target) {
168
- return Object.keys(target).reduce(function (result, key) {
169
- result[keyPrefix + delimiter + key] = target[key];
170
-
171
- return result
172
- }, recipient)
173
- }
174
-
175
- function isEmpty (val) {
176
- const type = Object.prototype.toString.call(val);
177
- const isArray = type === '[object Array]';
178
- const isObject = type === '[object Object]';
179
-
180
- if (!val) {
181
- return true
182
- } else if (isArray) {
183
- return !val.length
184
- } else if (isObject) {
185
- return !Object.keys(val).length
186
- }
187
- }
188
-
189
- target = Object.keys(target).reduce(function (result, key) {
190
- const type = Object.prototype.toString.call(target[key]);
191
- const isObject = (type === '[object Object]' || type === '[object Array]');
192
- if (!isObject || isEmpty(target[key])) {
193
- result[key] = target[key];
194
- return result
195
- } else {
196
- return addKeys(
197
- key,
198
- result,
199
- flatten$1(target[key], opts)
200
- )
201
- }
202
- }, {});
203
-
204
- Object.keys(target).forEach(function (key) {
205
- const split = key.split(delimiter).map(transformKey);
206
- let key1 = getkey(split.shift());
207
- let key2 = getkey(split[0]);
208
- let recipient = result;
209
-
210
- while (key2 !== undefined) {
211
- if (key1 === '__proto__') {
212
- return
213
- }
214
-
215
- const type = Object.prototype.toString.call(recipient[key1]);
216
- const isobject = (
217
- type === '[object Object]' ||
218
- type === '[object Array]'
219
- );
220
-
221
- // do not write over falsey, non-undefined values if overwrite is false
222
- if (!overwrite && !isobject && typeof recipient[key1] !== 'undefined') {
223
- return
224
- }
225
-
226
- if ((overwrite && !isobject) || (!overwrite && recipient[key1] == null)) {
227
- recipient[key1] = (
228
- typeof key2 === 'number' &&
229
- !opts.object ? [] : {}
230
- );
231
- }
232
-
233
- recipient = recipient[key1];
234
- if (split.length > 0) {
235
- key1 = getkey(split.shift());
236
- key2 = getkey(split[0]);
237
- }
238
- }
239
-
240
- // unflatten again for 'messy objects'
241
- recipient[key1] = unflatten(target[key], opts);
242
- });
243
-
244
- return result
245
- }
246
-
247
- /**
248
- * Brings the 1st level to the 0th level.
249
- * Flats the object by nesting with '.' path separator.
250
- */
251
- const serializeQueryForSpringBoot = (obj) => {
252
- const parts = [];
253
- Object.entries(obj).forEach(([key, val]) => {
254
- if (Array.isArray(val)) {
255
- val.forEach((v) => {
256
- parts.push([key, v]);
257
- });
258
- }
259
- else if (typeof val === 'object') {
260
- objectToParams(val).forEach(([key, val]) => {
261
- parts.push([key, val.toString()]);
262
- });
263
- }
264
- else {
265
- parts.push([key, val]);
266
- }
267
- });
268
- return parts.map((p) => `${p[0]}=${p[1]}`).join('&');
269
- };
270
- const flatten = (obj) => {
271
- const r = flat.flatten(obj);
272
- // { empty: {} } would be { empty: {} } instead of empty array
273
- return Object.entries(r).filter(([k, v]) => typeof v !== 'object');
274
- };
275
- const objectToParams = (obj) => {
276
- const [arrayProps, nonArrayProps] = partition((e) => Array.isArray(e[1]), Object.entries(obj));
277
- const withoutArrayProps = fromPairs(nonArrayProps);
278
- const res = flatten(withoutArrayProps);
279
- arrayProps.forEach(([k, vals]) => {
280
- vals.forEach((v) => res.push([k, v]));
281
- });
282
- return res;
283
- };
284
-
285
- const fileResFilenameRegex = /filename="([^"]+)"/;
286
- const getFileNameFromContentDispositionHeader = (contentDispositionHeader) => {
287
- if (!contentDispositionHeader)
288
- return 'download-file';
289
- const match = contentDispositionHeader.match(fileResFilenameRegex);
290
- const fileName = match ? match[1] : 'downloaded-file';
291
- return fileName;
292
- };
293
-
294
- const ContentType = {
295
- Json: 'application/json',
296
- FormData: 'multipart/form-data'
297
- };
298
-
299
- const noOpUrlConverter = () => ({ query, path }) => {
300
- return {
301
- url: path,
302
- params: query
303
- };
304
- };
305
- const springBootUrlConverter = () => ({ query, path }) => {
306
- return {
307
- url: path + (query ? `?${serializeQueryForSpringBoot(query)}` : ''),
308
- params: undefined
309
- };
310
- };
311
- const createHttpClient$1 = (axios, platform, urlConverter = noOpUrlConverter()) => {
312
- return {
313
- request: (req) => {
314
- const { query, type, body, headers: reqHeaders } = req;
315
- const isBlobResponse = req.format === 'document';
316
- let headers = { ...(reqHeaders || {}) };
317
- let data = body;
318
- if (type === ContentType.FormData) {
319
- const form = convertToFormData(body, platform);
320
- data = form.data;
321
- headers = form.headers;
322
- }
323
- else if (type) {
324
- headers['Content-Type'] = type;
325
- }
326
- return axios
327
- .request({
328
- method: req.method,
329
- // url: req.path + (query ? `?${serializeQueryForNestJs(query)}` : ''),
330
- // params: req.query, // TODO make this customizable as different behaviour might be needed with different APIs
331
- // url: req.path,
332
- ...urlConverter(req),
333
- data,
334
- headers,
335
- responseType: isBlobResponse ? platform.getFileAxiosResponseType() : 'json'
336
- })
337
- .then(r => {
338
- const { data } = r;
339
- if (isBlobResponse) {
340
- const contDist = r.headers['content-disposition'];
341
- const fileName = getFileNameFromContentDispositionHeader(contDist);
342
- const type = r.headers['content-type'];
343
- return new File([data], fileName, {
344
- type
345
- });
346
- }
347
- return data;
348
- });
349
- }
350
- };
351
- };
352
-
353
- class BrowserPlatFormHelper {
354
- isFile = (obj) => obj instanceof File;
355
- getFileAndName = (obj) => {
356
- if (!(obj instanceof File))
357
- throw new Error(`Obj ${obj} is not a file`);
358
- return {
359
- file: obj,
360
- name: obj.name
361
- };
362
- };
363
- getHeaders = (data) => ({});
364
- isFileList = (obj) => obj instanceof FileList;
365
- // @ts-ignore
366
- newFormData = () => new FormData();
367
- getFileAxiosResponseType = () => 'blob';
368
- }
369
-
370
- const createHttpClient = (axios, urlConverter = noOpUrlConverter()) => {
371
- return createHttpClient$1(axios, new BrowserPlatFormHelper(), urlConverter);
372
- };
373
-
374
- export { ContentType, createHttpClient, noOpUrlConverter, springBootUrlConverter };