@ps-aux/api-client-axios 0.0.4 → 0.0.6-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.
Files changed (38) hide show
  1. package/dist/api-client-axios/src/AxiosOpenApiHttpClient.d.ts +7 -0
  2. package/dist/api-client-axios/src/browser.d.ts +4 -0
  3. package/dist/api-client-axios/src/commonExports.d.ts +3 -0
  4. package/dist/api-client-axios/src/node.d.ts +4 -0
  5. package/dist/{platform → api-client-axios/src/platform}/browser.d.ts +2 -2
  6. package/dist/api-client-axios/src/platform/foo.d.ts +1 -0
  7. package/dist/{mytyp.d.ts → api-client-axios/src/t.d.ts} +1 -4
  8. package/dist/api-client-common/src/index.d.ts +9 -0
  9. package/dist/api-client-common/src/serializeQueryForSpringBoot.d.ts +5 -0
  10. package/dist/api-client-common/src/serializeQueryWithQs.d.ts +1 -0
  11. package/dist/browser.esm.js +340 -0
  12. package/dist/browser.js +344 -0
  13. package/dist/node.esm.js +352 -0
  14. package/dist/node.js +356 -0
  15. package/package.json +10 -11
  16. package/rollup.config.mjs +16 -9
  17. package/src/AxiosOpenApiHttpClient.ts +28 -11
  18. package/src/browser.ts +12 -0
  19. package/src/commonExports.ts +3 -0
  20. package/src/node.ts +11 -0
  21. package/src/platform/browser.ts +5 -5
  22. package/src/platform/foo.ts +1 -0
  23. package/src/platform/node.ts +3 -3
  24. package/src/{mytyp.ts → t.ts} +1 -6
  25. package/tsconfig.json +2 -1
  26. package/dist/AxiosOpenApiHttpClient.d.ts +0 -3
  27. package/dist/index.d.ts +0 -1
  28. package/dist/index.esm.js +0 -139
  29. package/dist/index.js +0 -139
  30. package/dist/platform/index.d.ts +0 -2
  31. package/dist/serializeQuery.d.ts +0 -1
  32. package/foo.sh +0 -3
  33. package/src/index.ts +0 -1
  34. package/src/platform/index.ts +0 -3
  35. package/src/serializeQuery.ts +0 -47
  36. /package/dist/{convertFormData.d.ts → api-client-axios/src/convertFormData.d.ts} +0 -0
  37. /package/dist/{platform → api-client-axios/src/platform}/node.d.ts +0 -0
  38. /package/dist/{platform → api-client-axios/src/platform}/types.d.ts +0 -0
@@ -0,0 +1,7 @@
1
+ import { AxiosInstance } from 'axios';
2
+ import { HttpClient } from './t';
3
+ import { PlatformHelper } from './platform/types';
4
+ import { UrlConverter } from '@ps-aux/api-client-common/src';
5
+ export declare const noOpUrlConverter: () => UrlConverter;
6
+ export declare const springBootUrlConverter: () => UrlConverter;
7
+ export declare const createHttpClient: (axios: AxiosInstance, platform: PlatformHelper, urlConverter?: UrlConverter) => HttpClient;
@@ -0,0 +1,4 @@
1
+ import { AxiosInstance } from 'axios';
2
+ import { HttpClient } from './t';
3
+ export declare const createHttpClient: (axios: AxiosInstance, urlConverter?: import("@ps-aux/api-client-common/src").UrlConverter) => HttpClient;
4
+ export * from './commonExports';
@@ -0,0 +1,3 @@
1
+ export { UrlConverter } from '@ps-aux/api-client-common';
2
+ export { HttpClient, Request, RequestParams } from './t';
3
+ export { springBootUrlConverter, noOpUrlConverter } from './AxiosOpenApiHttpClient';
@@ -0,0 +1,4 @@
1
+ import { AxiosInstance } from 'axios';
2
+ import { HttpClient } from './t';
3
+ export declare const createHttpClient: (axios: AxiosInstance, urlConverter?: import("@ps-aux/api-client-common/src").UrlConverter) => HttpClient;
4
+ export * from './commonExports';
@@ -1,4 +1,4 @@
1
- import { PlatformHelper } from "./types";
1
+ import { PlatformHelper } from './types';
2
2
  export declare class BrowserPlatFormHelper implements PlatformHelper {
3
3
  isFile: (obj: any) => boolean;
4
4
  getFileAndName: (obj: any) => {
@@ -6,5 +6,5 @@ export declare class BrowserPlatFormHelper implements PlatformHelper {
6
6
  name: string;
7
7
  };
8
8
  isFileList: (obj: any) => boolean;
9
- newFormData: () => any;
9
+ newFormData: () => FormData;
10
10
  }
@@ -0,0 +1 @@
1
+ export { BrowserPlatFormHelper as Platform } from './browser';
@@ -1,7 +1,5 @@
1
1
  export type ContentType = {};
2
- export type RequestParams = {
3
- fileDownload?: boolean;
4
- };
2
+ export type RequestParams = {};
5
3
  export declare const ContentType: {
6
4
  Json: string;
7
5
  FormData: string;
@@ -14,7 +12,6 @@ export type Request = {
14
12
  body?: any;
15
13
  type?: string;
16
14
  secure?: boolean;
17
- fileDownload?: boolean;
18
15
  };
19
16
  export type HttpClient<Any = any> = {
20
17
  request: <Data, A = any>(req: Request) => Promise<Data>;
@@ -0,0 +1,9 @@
1
+ export { serializeQueryForSpringBoot } from './serializeQueryForSpringBoot';
2
+ export { serializeQueryWithQs } from './serializeQueryWithQs';
3
+ export type UrlConverter = (req: {
4
+ path: string;
5
+ query?: Record<string, unknown>;
6
+ }) => {
7
+ url: string;
8
+ params?: any;
9
+ };
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Brings the 1st level to the 0th level.
3
+ * Flats the object by nesting with '.' path separator.
4
+ */
5
+ export declare const serializeQueryForSpringBoot: (obj: object) => string;
@@ -0,0 +1 @@
1
+ export declare const serializeQueryWithQs: (obj: object) => string;
@@ -0,0 +1,340 @@
1
+ import 'qs';
2
+
3
+ const ContentType = {
4
+ Json: 'application/json',
5
+ FormData: 'multipart/form-data',
6
+ };
7
+
8
+ const convertToFormData = (payload, platform) => {
9
+ const formData = platform.newFormData();
10
+ const addProp = (key, val) => {
11
+ if (Array.isArray(val) || platform.isFileList(val)) {
12
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
13
+ // @ts-ignore - seems that FileList is iterable despite the warning
14
+ // TODO change to other iteration method if this is not true
15
+ for (const valItem of val) {
16
+ addProp(key, valItem);
17
+ }
18
+ }
19
+ else if (typeof val === 'object' &&
20
+ val != null &&
21
+ !platform.isFile(val)) {
22
+ throw new Error(`Object serialization into FormData not supported for object: ${val}`);
23
+ }
24
+ else {
25
+ if (platform.isFile(val)) {
26
+ const { file, name } = platform.getFileAndName(val);
27
+ formData.append(key, file, name);
28
+ }
29
+ else {
30
+ formData.append(key, val);
31
+ }
32
+ }
33
+ };
34
+ Object.entries(payload).forEach(([key, val]) => addProp(key, val));
35
+ return formData;
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 noOpUrlConverter = () => ({ query, path }) => {
288
+ return {
289
+ url: path,
290
+ params: query,
291
+ };
292
+ };
293
+ const springBootUrlConverter = () => ({ query, path }) => {
294
+ return {
295
+ url: path + (query ? `?${serializeQueryForSpringBoot(query)}` : ''),
296
+ params: undefined,
297
+ };
298
+ };
299
+ const createHttpClient$1 = (axios, platform, urlConverter = noOpUrlConverter()) => {
300
+ return {
301
+ request: (req) => {
302
+ const { query, type, body } = req;
303
+ const data = type === ContentType.FormData ? convertToFormData(body, platform) : body;
304
+ return axios
305
+ .request({
306
+ method: req.method,
307
+ // url: req.path + (query ? `?${serializeQueryForNestJs(query)}` : ''),
308
+ // params: req.query, // TODO make this customizable as different behaviour might be needed with different APIs
309
+ // url: req.path,
310
+ ...urlConverter(req),
311
+ data,
312
+ headers: {
313
+ ContentType: type,
314
+ },
315
+ })
316
+ .then((r) => r.data);
317
+ },
318
+ };
319
+ };
320
+
321
+ class BrowserPlatFormHelper {
322
+ isFile = (obj) => obj instanceof File;
323
+ getFileAndName = (obj) => {
324
+ if (!(obj instanceof File))
325
+ throw new Error(`Obj ${obj} is not a file`);
326
+ return {
327
+ file: obj,
328
+ name: obj.name,
329
+ };
330
+ };
331
+ isFileList = (obj) => obj instanceof FileList;
332
+ // @ts-ignore
333
+ newFormData = () => new FormData();
334
+ }
335
+
336
+ const createHttpClient = (axios, urlConverter = noOpUrlConverter()) => {
337
+ return createHttpClient$1(axios, new BrowserPlatFormHelper(), urlConverter);
338
+ };
339
+
340
+ export { createHttpClient, noOpUrlConverter, springBootUrlConverter };