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