@ps-aux/api-client-axios 0.0.1 → 0.0.3
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/AxiosOpenApiHttpClient.d.ts +1 -1
- package/dist/convertFormData.d.ts +2 -1
- package/dist/index.esm.js +61 -8
- package/dist/index.js +63 -12
- package/dist/platform/browser.d.ts +10 -0
- package/dist/platform/index.d.ts +2 -0
- package/dist/platform/node.d.ts +10 -0
- package/dist/platform/types.d.ts +9 -0
- package/package.json +5 -5
- package/src/AxiosOpenApiHttpClient.ts +11 -6
- package/src/convertFormData.ts +13 -6
- package/src/platform/browser.ts +22 -0
- package/src/platform/index.ts +3 -0
- package/src/platform/node.ts +34 -0
- package/src/platform/types.ts +9 -0
- package/tsconfig.json +6 -0
- package/dist/api-client-axios/src/AxiosOpenApiHttpClient.d.ts +0 -3
- package/dist/api-client-axios/src/convertFormData.d.ts +0 -1
- package/dist/api-client-axios/src/index.d.ts +0 -1
- package/dist/api-client-axios/src/mytyp.d.ts +0 -21
- package/dist/api-client-axios/src/serializeQuery.d.ts +0 -1
- package/dist/is-even/index.d.ts +0 -1
- package/dist/tests/foo.test.d.ts +0 -1
- package/dist/tests/go.d.ts +0 -1
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { PlatformHelper } from "./platform/types";
|
|
2
|
+
export declare const convertToFormData: (payload: Record<string, any>, platform: PlatformHelper) => FormData;
|
package/dist/index.esm.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { partition, fromPairs } from 'rambda';
|
|
2
2
|
import { flatten as flatten$1 } from 'flat';
|
|
3
|
+
import NodeFormData from 'form-data';
|
|
4
|
+
import stream from 'stream';
|
|
3
5
|
|
|
4
6
|
// Use this file also as a symlink for http-client.eta file
|
|
5
7
|
const ContentType = {
|
|
@@ -41,10 +43,10 @@ const objectToParams = (obj) => {
|
|
|
41
43
|
return res;
|
|
42
44
|
};
|
|
43
45
|
|
|
44
|
-
const convertToFormData = (payload) => {
|
|
45
|
-
const formData =
|
|
46
|
+
const convertToFormData = (payload, platform) => {
|
|
47
|
+
const formData = platform.newFormData();
|
|
46
48
|
const addProp = (key, val) => {
|
|
47
|
-
if (Array.isArray(val) || val
|
|
49
|
+
if (Array.isArray(val) || platform.isFileList(val)) {
|
|
48
50
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
49
51
|
// @ts-ignore - seems that FileList is iterable despite the warning
|
|
50
52
|
// TODO change to other iteration method if this is not true
|
|
@@ -54,22 +56,73 @@ const convertToFormData = (payload) => {
|
|
|
54
56
|
}
|
|
55
57
|
else if (typeof val === 'object' &&
|
|
56
58
|
val != null &&
|
|
57
|
-
!(val
|
|
58
|
-
throw new Error(
|
|
59
|
+
!platform.isFile(val)) {
|
|
60
|
+
throw new Error(`Object serialization into FormData not supported for object: ${val}`);
|
|
59
61
|
}
|
|
60
62
|
else {
|
|
61
|
-
|
|
63
|
+
if (platform.isFile(val)) {
|
|
64
|
+
const { file, name } = platform.getFileAndName(val);
|
|
65
|
+
formData.append(key, file, name);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
formData.append(key, val);
|
|
69
|
+
}
|
|
62
70
|
}
|
|
63
71
|
};
|
|
64
72
|
Object.entries(payload).forEach(([key, val]) => addProp(key, val));
|
|
65
73
|
return formData;
|
|
66
74
|
};
|
|
67
75
|
|
|
68
|
-
|
|
76
|
+
class BrowserPlatFormHelper {
|
|
77
|
+
constructor() {
|
|
78
|
+
this.isFile = obj => obj instanceof File;
|
|
79
|
+
this.getFileAndName = (obj) => {
|
|
80
|
+
if (!(obj instanceof File))
|
|
81
|
+
throw new Error(`Obj ${obj} is not a file`);
|
|
82
|
+
return {
|
|
83
|
+
file: obj,
|
|
84
|
+
name: obj.name
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
this.isFileList = obj => obj instanceof FileList;
|
|
88
|
+
// @ts-ignore
|
|
89
|
+
this.newFormData = () => new FormData();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
class NodePlatFormHelper {
|
|
94
|
+
constructor() {
|
|
95
|
+
this.isFile = obj => {
|
|
96
|
+
if (typeof obj !== 'object')
|
|
97
|
+
return false;
|
|
98
|
+
const { file, name } = obj;
|
|
99
|
+
return typeof name === 'string' &&
|
|
100
|
+
(Buffer.isBuffer(file)
|
|
101
|
+
|| file instanceof stream.Readable);
|
|
102
|
+
};
|
|
103
|
+
this.getFileAndName = (obj) => {
|
|
104
|
+
if (!this.isFile(obj))
|
|
105
|
+
throw new Error(`Obj ${obj} is not a file`);
|
|
106
|
+
return {
|
|
107
|
+
file: obj.file,
|
|
108
|
+
name: obj.name,
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
this.isFileList = obj => {
|
|
112
|
+
// No FileList in node?
|
|
113
|
+
return false;
|
|
114
|
+
};
|
|
115
|
+
// @ts-ignore
|
|
116
|
+
this.newFormData = () => new NodeFormData();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const createApiClient = (axios, platformType) => {
|
|
121
|
+
const platform = platformType === 'node' ? new NodePlatFormHelper() : new BrowserPlatFormHelper();
|
|
69
122
|
return {
|
|
70
123
|
request: (req) => {
|
|
71
124
|
const { query, type, body } = req;
|
|
72
|
-
const data = type === ContentType.FormData ? convertToFormData(body) : body;
|
|
125
|
+
const data = type === ContentType.FormData ? convertToFormData(body, platform) : body;
|
|
73
126
|
return axios
|
|
74
127
|
.request({
|
|
75
128
|
method: req.method,
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('rambda'), require('flat')) :
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports', 'rambda', 'flat'], factory) :
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.MyLibrary = {}, global.rambda, global.flat));
|
|
5
|
-
})(this, (function (exports, rambda, flat) { 'use strict';
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('rambda'), require('flat'), require('form-data'), require('stream')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', 'rambda', 'flat', 'form-data', 'stream'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.MyLibrary = {}, global.rambda, global.flat, global.NodeFormData, global.stream));
|
|
5
|
+
})(this, (function (exports, rambda, flat, NodeFormData, stream) { 'use strict';
|
|
6
6
|
|
|
7
7
|
// Use this file also as a symlink for http-client.eta file
|
|
8
8
|
const ContentType = {
|
|
@@ -44,10 +44,10 @@
|
|
|
44
44
|
return res;
|
|
45
45
|
};
|
|
46
46
|
|
|
47
|
-
const convertToFormData = (payload) => {
|
|
48
|
-
const formData =
|
|
47
|
+
const convertToFormData = (payload, platform) => {
|
|
48
|
+
const formData = platform.newFormData();
|
|
49
49
|
const addProp = (key, val) => {
|
|
50
|
-
if (Array.isArray(val) || val
|
|
50
|
+
if (Array.isArray(val) || platform.isFileList(val)) {
|
|
51
51
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
52
52
|
// @ts-ignore - seems that FileList is iterable despite the warning
|
|
53
53
|
// TODO change to other iteration method if this is not true
|
|
@@ -57,22 +57,73 @@
|
|
|
57
57
|
}
|
|
58
58
|
else if (typeof val === 'object' &&
|
|
59
59
|
val != null &&
|
|
60
|
-
!(val
|
|
61
|
-
throw new Error(
|
|
60
|
+
!platform.isFile(val)) {
|
|
61
|
+
throw new Error(`Object serialization into FormData not supported for object: ${val}`);
|
|
62
62
|
}
|
|
63
63
|
else {
|
|
64
|
-
|
|
64
|
+
if (platform.isFile(val)) {
|
|
65
|
+
const { file, name } = platform.getFileAndName(val);
|
|
66
|
+
formData.append(key, file, name);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
formData.append(key, val);
|
|
70
|
+
}
|
|
65
71
|
}
|
|
66
72
|
};
|
|
67
73
|
Object.entries(payload).forEach(([key, val]) => addProp(key, val));
|
|
68
74
|
return formData;
|
|
69
75
|
};
|
|
70
76
|
|
|
71
|
-
|
|
77
|
+
class BrowserPlatFormHelper {
|
|
78
|
+
constructor() {
|
|
79
|
+
this.isFile = obj => obj instanceof File;
|
|
80
|
+
this.getFileAndName = (obj) => {
|
|
81
|
+
if (!(obj instanceof File))
|
|
82
|
+
throw new Error(`Obj ${obj} is not a file`);
|
|
83
|
+
return {
|
|
84
|
+
file: obj,
|
|
85
|
+
name: obj.name
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
this.isFileList = obj => obj instanceof FileList;
|
|
89
|
+
// @ts-ignore
|
|
90
|
+
this.newFormData = () => new FormData();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
class NodePlatFormHelper {
|
|
95
|
+
constructor() {
|
|
96
|
+
this.isFile = obj => {
|
|
97
|
+
if (typeof obj !== 'object')
|
|
98
|
+
return false;
|
|
99
|
+
const { file, name } = obj;
|
|
100
|
+
return typeof name === 'string' &&
|
|
101
|
+
(Buffer.isBuffer(file)
|
|
102
|
+
|| file instanceof stream.Readable);
|
|
103
|
+
};
|
|
104
|
+
this.getFileAndName = (obj) => {
|
|
105
|
+
if (!this.isFile(obj))
|
|
106
|
+
throw new Error(`Obj ${obj} is not a file`);
|
|
107
|
+
return {
|
|
108
|
+
file: obj.file,
|
|
109
|
+
name: obj.name,
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
this.isFileList = obj => {
|
|
113
|
+
// No FileList in node?
|
|
114
|
+
return false;
|
|
115
|
+
};
|
|
116
|
+
// @ts-ignore
|
|
117
|
+
this.newFormData = () => new NodeFormData();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const createApiClient = (axios, platformType) => {
|
|
122
|
+
const platform = platformType === 'node' ? new NodePlatFormHelper() : new BrowserPlatFormHelper();
|
|
72
123
|
return {
|
|
73
124
|
request: (req) => {
|
|
74
125
|
const { query, type, body } = req;
|
|
75
|
-
const data = type === ContentType.FormData ? convertToFormData(body) : body;
|
|
126
|
+
const data = type === ContentType.FormData ? convertToFormData(body, platform) : body;
|
|
76
127
|
return axios
|
|
77
128
|
.request({
|
|
78
129
|
method: req.method,
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { PlatformHelper } from "./types";
|
|
2
|
+
export declare class BrowserPlatFormHelper implements PlatformHelper {
|
|
3
|
+
isFile: (obj: any) => boolean;
|
|
4
|
+
getFileAndName: (obj: any) => {
|
|
5
|
+
file: File;
|
|
6
|
+
name: string;
|
|
7
|
+
};
|
|
8
|
+
isFileList: (obj: any) => boolean;
|
|
9
|
+
newFormData: () => any;
|
|
10
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ps-aux/api-client-axios",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"scripts": {
|
|
@@ -10,12 +10,12 @@
|
|
|
10
10
|
"author": "",
|
|
11
11
|
"license": "ISC",
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
13
|
+
"axios": "^1.5.0",
|
|
14
|
+
"flat": "5.0.2",
|
|
15
|
+
"form-data": "^4.0.0",
|
|
16
|
+
"rambda": "^8.5.0"
|
|
16
17
|
},
|
|
17
18
|
"devDependencies": {
|
|
18
|
-
"@types/axios": "^0.14.0",
|
|
19
19
|
"rollup": "^4.1.4",
|
|
20
20
|
"rollup-plugin-typescript2": "^0.36.0"
|
|
21
21
|
}
|
|
@@ -1,17 +1,22 @@
|
|
|
1
|
-
import {AxiosInstance} from 'axios'
|
|
2
|
-
import {ContentType, HttpClient} from './mytyp'
|
|
3
|
-
import {serializeQuery} from './serializeQuery'
|
|
4
|
-
import {convertToFormData} from './convertFormData'
|
|
1
|
+
import { AxiosInstance } from 'axios'
|
|
2
|
+
import { ContentType, HttpClient } from './mytyp'
|
|
3
|
+
import { serializeQuery } from './serializeQuery'
|
|
4
|
+
import { convertToFormData } from './convertFormData'
|
|
5
|
+
import { BrowserPlatFormHelper, NodePlatFormHelper } from './platform'
|
|
6
|
+
import { PlatformHelper } from './platform/types'
|
|
5
7
|
|
|
6
8
|
export const createApiClient = (
|
|
7
9
|
axios: AxiosInstance,
|
|
10
|
+
platformType: 'browser' | 'node',
|
|
8
11
|
): HttpClient => {
|
|
12
|
+
const platform: PlatformHelper = platformType === 'node' ? new NodePlatFormHelper() : new BrowserPlatFormHelper()
|
|
9
13
|
return {
|
|
10
14
|
request: (req) => {
|
|
11
|
-
const {query, type, body} = req
|
|
15
|
+
const { query, type, body } = req
|
|
16
|
+
|
|
12
17
|
|
|
13
18
|
const data =
|
|
14
|
-
type === ContentType.FormData ? convertToFormData(body) : body
|
|
19
|
+
type === ContentType.FormData ? convertToFormData(body, platform) : body
|
|
15
20
|
|
|
16
21
|
return axios
|
|
17
22
|
.request({
|
package/src/convertFormData.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import {PlatformHelper} from "./platform/types";
|
|
1
2
|
|
|
2
3
|
|
|
3
|
-
export const convertToFormData = (payload: Record<string, any
|
|
4
|
-
const formData =
|
|
4
|
+
export const convertToFormData = (payload: Record<string, any>, platform: PlatformHelper): FormData => {
|
|
5
|
+
const formData = platform.newFormData()
|
|
6
|
+
|
|
5
7
|
const addProp = (key: string, val: any) => {
|
|
6
|
-
if (Array.isArray(val) || val
|
|
8
|
+
if (Array.isArray(val) || platform.isFileList(val)) {
|
|
7
9
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
8
10
|
// @ts-ignore - seems that FileList is iterable despite the warning
|
|
9
11
|
// TODO change to other iteration method if this is not true
|
|
@@ -13,11 +15,16 @@ export const convertToFormData = (payload: Record<string, any>): FormData => {
|
|
|
13
15
|
} else if (
|
|
14
16
|
typeof val === 'object' &&
|
|
15
17
|
val != null &&
|
|
16
|
-
!(val
|
|
18
|
+
!platform.isFile(val)
|
|
17
19
|
) {
|
|
18
|
-
throw new Error(
|
|
20
|
+
throw new Error(`Object serialization into FormData not supported for object: ${val}`)
|
|
19
21
|
} else {
|
|
20
|
-
|
|
22
|
+
if (platform.isFile(val)) {
|
|
23
|
+
const {file, name} = platform.getFileAndName(val)
|
|
24
|
+
formData.append(key, file, name)
|
|
25
|
+
} else {
|
|
26
|
+
formData.append(key, val)
|
|
27
|
+
}
|
|
21
28
|
}
|
|
22
29
|
}
|
|
23
30
|
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import {PlatformHelper} from "./types";
|
|
2
|
+
|
|
3
|
+
export class BrowserPlatFormHelper implements PlatformHelper {
|
|
4
|
+
|
|
5
|
+
isFile = obj =>
|
|
6
|
+
obj instanceof File
|
|
7
|
+
|
|
8
|
+
getFileAndName = (obj) => {
|
|
9
|
+
if (!(obj instanceof File))
|
|
10
|
+
throw new Error(`Obj ${obj} is not a file`)
|
|
11
|
+
return {
|
|
12
|
+
file: obj,
|
|
13
|
+
name: obj.name
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
isFileList = obj =>
|
|
19
|
+
obj instanceof FileList
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
newFormData = () => new FormData()
|
|
22
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { PlatformHelper } from './types'
|
|
2
|
+
import NodeFormData from 'form-data'
|
|
3
|
+
import stream from 'stream'
|
|
4
|
+
|
|
5
|
+
export class NodePlatFormHelper implements PlatformHelper {
|
|
6
|
+
|
|
7
|
+
isFile = obj => {
|
|
8
|
+
if (typeof obj !== 'object')
|
|
9
|
+
return false
|
|
10
|
+
|
|
11
|
+
const { file, name } = obj
|
|
12
|
+
|
|
13
|
+
return typeof name === 'string' &&
|
|
14
|
+
(Buffer.isBuffer(file)
|
|
15
|
+
|| file instanceof stream.Readable)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
getFileAndName = (obj) => {
|
|
19
|
+
if (!this.isFile(obj))
|
|
20
|
+
throw new Error(`Obj ${obj} is not a file`)
|
|
21
|
+
return {
|
|
22
|
+
file: obj.file,
|
|
23
|
+
name: obj.name,
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
isFileList = obj => {
|
|
29
|
+
// No FileList in node?
|
|
30
|
+
return false
|
|
31
|
+
}
|
|
32
|
+
// @ts-ignore
|
|
33
|
+
newFormData = (): FormData => new NodeFormData()
|
|
34
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const convertToFormData: (payload: Record<string, any>) => FormData;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { createApiClient } from './AxiosOpenApiHttpClient';
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export type ContentType = {};
|
|
2
|
-
export type RequestParams = {
|
|
3
|
-
fileDownload?: boolean;
|
|
4
|
-
};
|
|
5
|
-
export declare const ContentType: {
|
|
6
|
-
Json: string;
|
|
7
|
-
FormData: string;
|
|
8
|
-
};
|
|
9
|
-
export type Request = {
|
|
10
|
-
path: string;
|
|
11
|
-
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
12
|
-
format?: 'json';
|
|
13
|
-
query?: any;
|
|
14
|
-
body?: any;
|
|
15
|
-
type?: string;
|
|
16
|
-
secure?: boolean;
|
|
17
|
-
fileDownload?: boolean;
|
|
18
|
-
};
|
|
19
|
-
export type HttpClient<Any = any> = {
|
|
20
|
-
request: <Data, A = any>(req: Request) => Promise<Data>;
|
|
21
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const serializeQuery: (obj: object) => string;
|
package/dist/is-even/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const isEven: (x: number) => boolean;
|
package/dist/tests/foo.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/tests/go.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|