@ps-aux/api-client-axios 0.0.3 → 0.0.4
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/index.js +72 -75
- package/foo.sh +3 -0
- package/package.json +4 -1
- package/src/AxiosOpenApiHttpClient.ts +3 -0
package/dist/index.js
CHANGED
|
@@ -1,93 +1,89 @@
|
|
|
1
|
-
(function
|
|
1
|
+
(function(global, factory) {
|
|
2
2
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('rambda'), require('flat'), require('form-data'), require('stream')) :
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
})(this, (function
|
|
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) {
|
|
6
|
+
'use strict'
|
|
6
7
|
|
|
7
8
|
// Use this file also as a symlink for http-client.eta file
|
|
8
9
|
const ContentType = {
|
|
9
10
|
Json: 'application/json',
|
|
10
11
|
FormData: 'multipart/form-data',
|
|
11
|
-
}
|
|
12
|
+
}
|
|
12
13
|
|
|
13
14
|
const serializeQuery = (obj) => {
|
|
14
|
-
const parts = []
|
|
15
|
+
const parts = []
|
|
15
16
|
Object.entries(obj).forEach(([key, val]) => {
|
|
16
17
|
if (Array.isArray(val)) {
|
|
17
18
|
val.forEach((v) => {
|
|
18
|
-
parts.push([key, v])
|
|
19
|
-
})
|
|
20
|
-
}
|
|
21
|
-
else if (typeof val === 'object') {
|
|
19
|
+
parts.push([key, v])
|
|
20
|
+
})
|
|
21
|
+
} else if (typeof val === 'object') {
|
|
22
22
|
objectToParams(val).forEach(([key, val]) => {
|
|
23
|
-
parts.push([key, val.toString()])
|
|
24
|
-
})
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
parts.push([key, val]);
|
|
23
|
+
parts.push([key, val.toString()])
|
|
24
|
+
})
|
|
25
|
+
} else {
|
|
26
|
+
parts.push([key, val])
|
|
28
27
|
}
|
|
29
|
-
})
|
|
30
|
-
return parts.map((p) => `${p[0]}=${p[1]}`).join('&')
|
|
31
|
-
}
|
|
28
|
+
})
|
|
29
|
+
return parts.map((p) => `${p[0]}=${p[1]}`).join('&')
|
|
30
|
+
}
|
|
32
31
|
const flatten = (obj) => {
|
|
33
|
-
const r = flat.flatten(obj)
|
|
32
|
+
const r = flat.flatten(obj)
|
|
34
33
|
// { empty: {} } would be { empty: {} } instead of empty array
|
|
35
|
-
return Object.entries(r).filter(([k, v]) => typeof v !== 'object')
|
|
36
|
-
}
|
|
34
|
+
return Object.entries(r).filter(([k, v]) => typeof v !== 'object')
|
|
35
|
+
}
|
|
37
36
|
const objectToParams = (obj) => {
|
|
38
|
-
const [arrayProps, nonArrayProps] = rambda.partition((e) => Array.isArray(e[1]), Object.entries(obj))
|
|
39
|
-
const withoutArrayProps = rambda.fromPairs(nonArrayProps)
|
|
40
|
-
const res = flatten(withoutArrayProps)
|
|
37
|
+
const [arrayProps, nonArrayProps] = rambda.partition((e) => Array.isArray(e[1]), Object.entries(obj))
|
|
38
|
+
const withoutArrayProps = rambda.fromPairs(nonArrayProps)
|
|
39
|
+
const res = flatten(withoutArrayProps)
|
|
41
40
|
arrayProps.forEach(([k, vals]) => {
|
|
42
|
-
vals.forEach((v) => res.push([k, v]))
|
|
43
|
-
})
|
|
44
|
-
return res
|
|
45
|
-
}
|
|
41
|
+
vals.forEach((v) => res.push([k, v]))
|
|
42
|
+
})
|
|
43
|
+
return res
|
|
44
|
+
}
|
|
46
45
|
|
|
47
46
|
const convertToFormData = (payload, platform) => {
|
|
48
|
-
const formData = platform.newFormData()
|
|
47
|
+
const formData = platform.newFormData()
|
|
49
48
|
const addProp = (key, val) => {
|
|
50
49
|
if (Array.isArray(val) || platform.isFileList(val)) {
|
|
51
50
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
52
51
|
// @ts-ignore - seems that FileList is iterable despite the warning
|
|
53
52
|
// TODO change to other iteration method if this is not true
|
|
54
53
|
for (const valItem of val) {
|
|
55
|
-
addProp(key, valItem)
|
|
54
|
+
addProp(key, valItem)
|
|
56
55
|
}
|
|
57
|
-
}
|
|
58
|
-
else if (typeof val === 'object' &&
|
|
56
|
+
} else if (typeof val === 'object' &&
|
|
59
57
|
val != null &&
|
|
60
58
|
!platform.isFile(val)) {
|
|
61
|
-
throw new Error(`Object serialization into FormData not supported for object: ${val}`)
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
59
|
+
throw new Error(`Object serialization into FormData not supported for object: ${val}`)
|
|
60
|
+
} else {
|
|
64
61
|
if (platform.isFile(val)) {
|
|
65
|
-
const { file, name } = platform.getFileAndName(val)
|
|
66
|
-
formData.append(key, file, name)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
formData.append(key, val);
|
|
62
|
+
const { file, name } = platform.getFileAndName(val)
|
|
63
|
+
formData.append(key, file, name)
|
|
64
|
+
} else {
|
|
65
|
+
formData.append(key, val)
|
|
70
66
|
}
|
|
71
67
|
}
|
|
72
|
-
}
|
|
73
|
-
Object.entries(payload).forEach(([key, val]) => addProp(key, val))
|
|
74
|
-
return formData
|
|
75
|
-
}
|
|
68
|
+
}
|
|
69
|
+
Object.entries(payload).forEach(([key, val]) => addProp(key, val))
|
|
70
|
+
return formData
|
|
71
|
+
}
|
|
76
72
|
|
|
77
73
|
class BrowserPlatFormHelper {
|
|
78
74
|
constructor() {
|
|
79
|
-
this.isFile = obj => obj instanceof File
|
|
75
|
+
this.isFile = obj => obj instanceof File
|
|
80
76
|
this.getFileAndName = (obj) => {
|
|
81
77
|
if (!(obj instanceof File))
|
|
82
|
-
throw new Error(`Obj ${obj} is not a file`)
|
|
78
|
+
throw new Error(`Obj ${obj} is not a file`)
|
|
83
79
|
return {
|
|
84
80
|
file: obj,
|
|
85
|
-
name: obj.name
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
this.isFileList = obj => obj instanceof FileList
|
|
81
|
+
name: obj.name,
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
this.isFileList = obj => obj instanceof FileList
|
|
89
85
|
// @ts-ignore
|
|
90
|
-
this.newFormData = () => new FormData()
|
|
86
|
+
this.newFormData = () => new FormData()
|
|
91
87
|
}
|
|
92
88
|
}
|
|
93
89
|
|
|
@@ -95,48 +91,49 @@
|
|
|
95
91
|
constructor() {
|
|
96
92
|
this.isFile = obj => {
|
|
97
93
|
if (typeof obj !== 'object')
|
|
98
|
-
return false
|
|
99
|
-
const { file, name } = obj
|
|
94
|
+
return false
|
|
95
|
+
const { file, name } = obj
|
|
100
96
|
return typeof name === 'string' &&
|
|
101
97
|
(Buffer.isBuffer(file)
|
|
102
|
-
|| file instanceof stream.Readable)
|
|
103
|
-
}
|
|
98
|
+
|| file instanceof stream.Readable)
|
|
99
|
+
}
|
|
104
100
|
this.getFileAndName = (obj) => {
|
|
105
101
|
if (!this.isFile(obj))
|
|
106
|
-
throw new Error(`Obj ${obj} is not a file`)
|
|
102
|
+
throw new Error(`Obj ${obj} is not a file`)
|
|
107
103
|
return {
|
|
108
104
|
file: obj.file,
|
|
109
105
|
name: obj.name,
|
|
110
|
-
}
|
|
111
|
-
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
112
108
|
this.isFileList = obj => {
|
|
113
109
|
// No FileList in node?
|
|
114
|
-
return false
|
|
115
|
-
}
|
|
110
|
+
return false
|
|
111
|
+
}
|
|
116
112
|
// @ts-ignore
|
|
117
|
-
this.newFormData = () => new NodeFormData()
|
|
113
|
+
this.newFormData = () => new NodeFormData()
|
|
118
114
|
}
|
|
119
115
|
}
|
|
120
116
|
|
|
121
117
|
const createApiClient = (axios, platformType) => {
|
|
122
|
-
const platform = platformType === 'node' ? new NodePlatFormHelper() : new BrowserPlatFormHelper()
|
|
118
|
+
const platform = platformType === 'node' ? new NodePlatFormHelper() : new BrowserPlatFormHelper()
|
|
123
119
|
return {
|
|
124
120
|
request: (req) => {
|
|
125
|
-
const { query, type, body } = req
|
|
126
|
-
const data = type === ContentType.FormData ? convertToFormData(body, platform) : body
|
|
121
|
+
const { query, type, body } = req
|
|
122
|
+
const data = type === ContentType.FormData ? convertToFormData(body, platform) : body
|
|
123
|
+
|
|
127
124
|
return axios
|
|
128
125
|
.request({
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
.then((r) => r.data)
|
|
126
|
+
method: req.method,
|
|
127
|
+
url: req.path + (query ? `?${serializeQuery(query)}` : ''),
|
|
128
|
+
// url: req.path,
|
|
129
|
+
data,
|
|
130
|
+
params: req.query
|
|
131
|
+
})
|
|
132
|
+
.then((r) => r.data)
|
|
136
133
|
},
|
|
137
|
-
}
|
|
138
|
-
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
139
136
|
|
|
140
|
-
exports.createApiClient = createApiClient
|
|
137
|
+
exports.createApiClient = createApiClient
|
|
141
138
|
|
|
142
|
-
}))
|
|
139
|
+
}))
|
package/foo.sh
ADDED
package/package.json
CHANGED