core-services-sdk 1.1.0 → 1.1.1
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/.vscode/settings.json +1 -1
- package/package.json +3 -2
- package/src/http/http.js +68 -11
- package/src/http/responseType.js +6 -0
- package/src/index.js +1 -0
package/.vscode/settings.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "core-services-sdk",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"main": "src/index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -22,7 +22,8 @@
|
|
|
22
22
|
"amqplib": "^0.10.8",
|
|
23
23
|
"http-status": "^2.1.0",
|
|
24
24
|
"node-fetch": "^3.3.2",
|
|
25
|
-
"uuid": "^11.1.0"
|
|
25
|
+
"uuid": "^11.1.0",
|
|
26
|
+
"xml2js": "^0.6.2"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
28
29
|
"@vitest/coverage-v8": "^3.2.4",
|
package/src/http/http.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import fetch from 'node-fetch'
|
|
2
2
|
import httpStatus from 'http-status'
|
|
3
|
+
import { parseStringPromise } from 'xml2js'
|
|
3
4
|
|
|
4
5
|
import { HttpError } from './HttpError.js'
|
|
5
6
|
import { HTTP_METHODS } from './http-method.js'
|
|
7
|
+
import { ResponseType } from './responseType.js'
|
|
6
8
|
|
|
7
9
|
const JSON_HEADER = {
|
|
8
10
|
'Content-Type': 'application/json',
|
|
@@ -28,6 +30,11 @@ const checkStatus = async (res) => {
|
|
|
28
30
|
return res
|
|
29
31
|
}
|
|
30
32
|
|
|
33
|
+
const getTextResponse = async (response) => {
|
|
34
|
+
const text = await response.text()
|
|
35
|
+
return text
|
|
36
|
+
}
|
|
37
|
+
|
|
31
38
|
const tryConvertJsonResponse = (responseText) => {
|
|
32
39
|
try {
|
|
33
40
|
const obj = JSON.parse(responseText)
|
|
@@ -39,12 +46,53 @@ const tryConvertJsonResponse = (responseText) => {
|
|
|
39
46
|
}
|
|
40
47
|
|
|
41
48
|
const tryGetJsonResponse = async (response) => {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
49
|
+
let jsonText
|
|
50
|
+
try {
|
|
51
|
+
jsonText = getTextResponse(response)
|
|
52
|
+
const obj = tryConvertJsonResponse(jsonText)
|
|
53
|
+
return obj
|
|
54
|
+
} catch (error) {
|
|
55
|
+
if (!jsonText) {
|
|
56
|
+
throw error
|
|
57
|
+
}
|
|
58
|
+
return jsonText
|
|
59
|
+
}
|
|
45
60
|
}
|
|
46
61
|
|
|
47
|
-
|
|
62
|
+
const tryGetXmlResponse = async (response) => {
|
|
63
|
+
let xmlText
|
|
64
|
+
try {
|
|
65
|
+
xmlText = await getTextResponse(response)
|
|
66
|
+
const xml = await parseStringPromise(xmlText)
|
|
67
|
+
return xml
|
|
68
|
+
} catch (error) {
|
|
69
|
+
if (!xmlText) {
|
|
70
|
+
throw error
|
|
71
|
+
}
|
|
72
|
+
return xmlText
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const getResponsePayload = async (response, responseType) => {
|
|
77
|
+
switch (responseType) {
|
|
78
|
+
case ResponseType.json:
|
|
79
|
+
return tryGetJsonResponse(response)
|
|
80
|
+
|
|
81
|
+
case ResponseType.xml:
|
|
82
|
+
return tryGetXmlResponse(response)
|
|
83
|
+
|
|
84
|
+
default:
|
|
85
|
+
case ResponseType.text:
|
|
86
|
+
return getTextResponse(response)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export const get = async ({
|
|
91
|
+
url,
|
|
92
|
+
headers = {},
|
|
93
|
+
credentials = 'include',
|
|
94
|
+
expectedType = ResponseType.json,
|
|
95
|
+
}) => {
|
|
48
96
|
const response = await fetch(url, {
|
|
49
97
|
method: HTTP_METHODS.GET,
|
|
50
98
|
headers: {
|
|
@@ -55,11 +103,17 @@ export const get = async ({ url, headers = {}, credentials = 'include' }) => {
|
|
|
55
103
|
})
|
|
56
104
|
|
|
57
105
|
await checkStatus(response)
|
|
58
|
-
const data = await
|
|
106
|
+
const data = await getResponsePayload(response, expectedType)
|
|
59
107
|
return data
|
|
60
108
|
}
|
|
61
109
|
|
|
62
|
-
export const post = async ({
|
|
110
|
+
export const post = async ({
|
|
111
|
+
url,
|
|
112
|
+
body,
|
|
113
|
+
headers,
|
|
114
|
+
credentials = 'include',
|
|
115
|
+
expectedType = ResponseType.json,
|
|
116
|
+
}) => {
|
|
63
117
|
const response = await fetch(url, {
|
|
64
118
|
method: HTTP_METHODS.POST,
|
|
65
119
|
headers: {
|
|
@@ -70,7 +124,7 @@ export const post = async ({ url, body, headers, credentials = 'include' }) => {
|
|
|
70
124
|
...(credentials ? { credentials } : {}),
|
|
71
125
|
})
|
|
72
126
|
await checkStatus(response)
|
|
73
|
-
const data = await
|
|
127
|
+
const data = await getResponsePayload(response, expectedType)
|
|
74
128
|
return data
|
|
75
129
|
}
|
|
76
130
|
|
|
@@ -79,6 +133,7 @@ export const put = async ({
|
|
|
79
133
|
body,
|
|
80
134
|
headers = {},
|
|
81
135
|
credentials = 'include',
|
|
136
|
+
expectedType = ResponseType.json,
|
|
82
137
|
}) => {
|
|
83
138
|
const response = await fetch(url, {
|
|
84
139
|
method: HTTP_METHODS.PUT,
|
|
@@ -91,7 +146,7 @@ export const put = async ({
|
|
|
91
146
|
})
|
|
92
147
|
|
|
93
148
|
await checkStatus(response)
|
|
94
|
-
const data = await
|
|
149
|
+
const data = await getResponsePayload(response, expectedType)
|
|
95
150
|
return data
|
|
96
151
|
}
|
|
97
152
|
|
|
@@ -100,6 +155,7 @@ export const patch = async ({
|
|
|
100
155
|
body,
|
|
101
156
|
headers,
|
|
102
157
|
credentials = 'include',
|
|
158
|
+
expectedType = ResponseType.json,
|
|
103
159
|
}) => {
|
|
104
160
|
const response = await fetch(url, {
|
|
105
161
|
method: HTTP_METHODS.PATCH,
|
|
@@ -112,7 +168,7 @@ export const patch = async ({
|
|
|
112
168
|
})
|
|
113
169
|
|
|
114
170
|
await checkStatus(response)
|
|
115
|
-
const data = await
|
|
171
|
+
const data = await getResponsePayload(response, expectedType)
|
|
116
172
|
return data
|
|
117
173
|
}
|
|
118
174
|
|
|
@@ -121,6 +177,7 @@ export const deleteApi = async ({
|
|
|
121
177
|
body,
|
|
122
178
|
headers,
|
|
123
179
|
credentials = 'include',
|
|
180
|
+
expectedType = ResponseType.json,
|
|
124
181
|
}) => {
|
|
125
182
|
const response = await fetch(url, {
|
|
126
183
|
method: HTTP_METHODS.DELETE,
|
|
@@ -133,14 +190,14 @@ export const deleteApi = async ({
|
|
|
133
190
|
})
|
|
134
191
|
|
|
135
192
|
await checkStatus(response)
|
|
136
|
-
const data = await
|
|
193
|
+
const data = await getResponsePayload(response, expectedType)
|
|
137
194
|
return data
|
|
138
195
|
}
|
|
139
196
|
|
|
140
197
|
export const http = {
|
|
141
198
|
get,
|
|
199
|
+
put,
|
|
142
200
|
post,
|
|
143
201
|
patch,
|
|
144
|
-
put,
|
|
145
202
|
deleteApi,
|
|
146
203
|
}
|
package/src/index.js
CHANGED