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