@kaspernj/api-maker 1.0.170 → 1.0.173

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 CHANGED
@@ -16,7 +16,7 @@
16
16
  ]
17
17
  },
18
18
  "name": "@kaspernj/api-maker",
19
- "version": "1.0.170",
19
+ "version": "1.0.173",
20
20
  "description": "",
21
21
  "main": "index.js",
22
22
  "repository": {
@@ -1,39 +1,42 @@
1
- const CustomError = require("./custom-error.cjs")
2
- const FormDataObjectizer = require("form-data-objectizer")
3
- const qs = require("qs")
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
- module.exports = class Api {
6
+ export default class Api {
6
7
  static get (path, pathParams = null) {
7
- return this.requestLocal({path, pathParams, method: "GET"})
8
+ return Api.requestLocal({path, pathParams, method: "GET"})
8
9
  }
9
10
 
10
11
  static delete (path, pathParams = null) {
11
- return this.requestLocal({path, pathParams, method: "DELETE"})
12
+ return Api.requestLocal({path, pathParams, method: "DELETE"})
12
13
  }
13
14
 
14
15
  static patch (path, data = {}) {
15
- return this.requestLocal({path, data, method: "PATCH"})
16
+ return Api.requestLocal({path, data, method: "PATCH"})
16
17
  }
17
18
 
18
19
  static post (path, data = {}) {
19
- return this.requestLocal({path, data, method: "POST"})
20
+ return Api.requestLocal({path, data, method: "POST"})
20
21
  }
21
22
 
22
- static request (args) {
23
- let path = args.path
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 (args.pathParams) {
26
- const pathParamsString = qs.stringify(args.pathParams, {arrayFormat: "brackets"})
27
- path += `?${pathParamsString}`
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(args.method, path, true)
35
+ xhr.open(method, requestPath, true)
33
36
 
34
- if (args.headers) {
35
- for (const headerName in args.headers) {
36
- xhr.setRequestHeader(headerName, args.headers[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 (args.data instanceof FormData) {
49
- customError.peakflowParameters = FormDataObjectizer.toObject(args.data)
51
+ if (data instanceof FormData) {
52
+ customError.peakflowParameters = FormDataObjectizer.toObject(data)
50
53
  } else {
51
- customError.peakflowParameters = args.data
54
+ customError.peakflowParameters = data
52
55
  }
53
56
 
54
57
  reject(customError)
55
58
  }
56
59
  }
57
60
 
58
- xhr.send(args.data)
61
+ xhr.send(data)
59
62
  })
60
63
  }
61
64
 
@@ -1,4 +1,4 @@
1
- const Api = require("./api.cjs")
1
+ const Api = require("./api.js").default
2
2
  const CommandSubmitData = require("./command-submit-data.cjs")
3
3
  const CustomError = require("./custom-error.cjs")
4
4
  const DestroyError = require("./destroy-error.cjs")
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
  }