@kaspernj/api-maker 1.0.168 → 1.0.171
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/package.json +2 -2
- package/src/{api.cjs → api.js} +24 -21
- package/src/commands-pool.cjs +1 -1
- package/src/config.js +8 -0
package/package.json
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
]
|
|
17
17
|
},
|
|
18
18
|
"name": "@kaspernj/api-maker",
|
|
19
|
-
"version": "1.0.
|
|
19
|
+
"version": "1.0.171",
|
|
20
20
|
"description": "",
|
|
21
21
|
"main": "index.js",
|
|
22
22
|
"repository": {
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"@rails/actioncable": ">= 6.1.0",
|
|
39
39
|
"clone-deep": ">= 4.0.1",
|
|
40
40
|
"debounce": ">= 1.2.1",
|
|
41
|
-
"diggerize": ">= 1.0.
|
|
41
|
+
"diggerize": ">= 1.0.5",
|
|
42
42
|
"epic-locks": ">= 1.0.1",
|
|
43
43
|
"form-data-objectizer": ">= 1.0.0",
|
|
44
44
|
"form-serialize": ">= 0.7.2",
|
package/src/{api.cjs → api.js}
RENAMED
|
@@ -1,39 +1,42 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import config from "./config.js"
|
|
2
|
+
import CustomError from "./custom-error.cjs"
|
|
3
|
+
import FormDataObjectizer from "form-data-objectizer"
|
|
4
|
+
import qs from "qs"
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
export default class Api {
|
|
6
7
|
static get (path, pathParams = null) {
|
|
7
|
-
return
|
|
8
|
+
return Api.requestLocal({path, pathParams, method: "GET"})
|
|
8
9
|
}
|
|
9
10
|
|
|
10
11
|
static delete (path, pathParams = null) {
|
|
11
|
-
return
|
|
12
|
+
return Api.requestLocal({path, pathParams, method: "DELETE"})
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
static patch (path, data = {}) {
|
|
15
|
-
return
|
|
16
|
+
return Api.requestLocal({path, data, method: "PATCH"})
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
static post (path, data = {}) {
|
|
19
|
-
return
|
|
20
|
+
return Api.requestLocal({path, data, method: "POST"})
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
static request (
|
|
23
|
-
let
|
|
23
|
+
static request ({data, headers, method, path, pathParams}) {
|
|
24
|
+
let requestPath = ""
|
|
25
|
+
if (config.getHost()) requestPath += config.getHost()
|
|
26
|
+
requestPath += path
|
|
24
27
|
|
|
25
|
-
if (
|
|
26
|
-
const pathParamsString = qs.stringify(
|
|
27
|
-
|
|
28
|
+
if (pathParams) {
|
|
29
|
+
const pathParamsString = qs.stringify(pathParams, {arrayFormat: "brackets"})
|
|
30
|
+
requestPath += `?${pathParamsString}`
|
|
28
31
|
}
|
|
29
32
|
|
|
30
33
|
return new Promise((resolve, reject) => {
|
|
31
34
|
const xhr = new XMLHttpRequest()
|
|
32
|
-
xhr.open(
|
|
35
|
+
xhr.open(method, requestPath, true)
|
|
33
36
|
|
|
34
|
-
if (
|
|
35
|
-
for (const headerName in
|
|
36
|
-
xhr.setRequestHeader(headerName,
|
|
37
|
+
if (headers) {
|
|
38
|
+
for (const headerName in headers) {
|
|
39
|
+
xhr.setRequestHeader(headerName, headers[headerName])
|
|
37
40
|
}
|
|
38
41
|
}
|
|
39
42
|
|
|
@@ -45,17 +48,17 @@ module.exports = class Api {
|
|
|
45
48
|
} else {
|
|
46
49
|
const customError = new CustomError(`Request failed with code: ${xhr.status}`, {response, xhr})
|
|
47
50
|
|
|
48
|
-
if (
|
|
49
|
-
customError.peakflowParameters = FormDataObjectizer.toObject(
|
|
51
|
+
if (data instanceof FormData) {
|
|
52
|
+
customError.peakflowParameters = FormDataObjectizer.toObject(data)
|
|
50
53
|
} else {
|
|
51
|
-
customError.peakflowParameters =
|
|
54
|
+
customError.peakflowParameters = data
|
|
52
55
|
}
|
|
53
56
|
|
|
54
57
|
reject(customError)
|
|
55
58
|
}
|
|
56
59
|
}
|
|
57
60
|
|
|
58
|
-
xhr.send(
|
|
61
|
+
xhr.send(data)
|
|
59
62
|
})
|
|
60
63
|
}
|
|
61
64
|
|
package/src/commands-pool.cjs
CHANGED
package/src/config.js
CHANGED
|
@@ -11,6 +11,10 @@ class ApiMakerConfig {
|
|
|
11
11
|
return this.global.currenciesCollection
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
getHost() {
|
|
15
|
+
return this.global.host
|
|
16
|
+
}
|
|
17
|
+
|
|
14
18
|
getRouteDefinitions() {
|
|
15
19
|
return this.global.routeDefinitions
|
|
16
20
|
}
|
|
@@ -27,6 +31,10 @@ class ApiMakerConfig {
|
|
|
27
31
|
this.global.history = history
|
|
28
32
|
}
|
|
29
33
|
|
|
34
|
+
setHost(host) {
|
|
35
|
+
this.global.host = host
|
|
36
|
+
}
|
|
37
|
+
|
|
30
38
|
setRouteDefinitions(routeDefinitions) {
|
|
31
39
|
this.global.routeDefinitions = routeDefinitions
|
|
32
40
|
}
|