@caruuto/caruuto-js 0.2.0 → 0.3.0

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/CHANGELOG.md CHANGED
@@ -4,3 +4,9 @@ All notable changes to this project will be documented in this file.
4
4
 
5
5
  The format is based on [Keep a Changelog](http://keepachangelog.com/)
6
6
  and this project adheres to [Semantic Versioning](http://semver.org/).
7
+
8
+ ## 0.3.0 - 2024-01-09
9
+
10
+ ### Added
11
+
12
+ - If an API key is provided to the constructor, it is added to the request headers as Authorization: Api-Key-v1 <key>
package/lib/helpers.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const querystring = require('query-string')
2
- const parseUrl = require('url-parse')
2
+ const url = require('url')
3
3
 
4
4
  module.exports = function (CaruutoClient) {
5
5
  /**
@@ -144,7 +144,7 @@ module.exports = function (CaruutoClient) {
144
144
  * @api private
145
145
  */
146
146
  CaruutoClient.prototype._createRequestObject = function (options) {
147
- const parsedUri = parseUrl(options.uri)
147
+ const parsedUri = url.parse(options.uri)
148
148
  const requestObject = Object.assign({}, options, {
149
149
  uri: {
150
150
  href: parsedUri.href,
@@ -25,26 +25,36 @@ module.exports = function (CaruutoClient) {
25
25
  */
26
26
  CaruutoClient.prototype._processRequest = async function (requestObject) {
27
27
  const fetch = resolveFetch(this.options.fetch)
28
+
28
29
  const options = {
29
30
  headers: Object.assign({}, requestObject.headers),
30
31
  method: requestObject.method,
31
32
  uri: requestObject.uri.href
32
33
  }
33
34
 
34
- if (requestObject.body) {
35
- options.body = requestObject.body
35
+ // Add authorization header if an API key has been provided.
36
+ if (this.options.caruutoApiKey) {
37
+ options.headers.authorization = `Api-Key-v1 ${this.options.caruutoApiKey}`
36
38
  }
37
39
 
38
- if (
39
- requestObject.headers &&
40
- requestObject.headers['content-type'] &&
41
- requestObject.headers['content-type'] !== 'application/json'
42
- ) {
43
- options.json = false
44
- } else {
45
- options.json = true
40
+ if (requestObject.body) {
41
+ options.body = JSON.stringify(requestObject.body)
42
+
43
+ if (options.headers['content-type'] !== 'application/json') {
44
+ options.headers['content-type'] = 'application/json'
45
+ }
46
46
  }
47
47
 
48
+ // if (
49
+ // requestObject.headers &&
50
+ // requestObject.headers['content-type'] &&
51
+ // requestObject.headers['content-type'] !== 'application/json'
52
+ // ) {
53
+ // options.json = false
54
+ // } else {
55
+ // options.json = true
56
+ // }
57
+
48
58
  debug(`Querying URI: ${decodeURIComponent(options.uri)}`)
49
59
 
50
60
  const response = await fetch(options.uri, options).catch(err => {
@@ -14,6 +14,8 @@ module.exports = function (CaruutoClient) {
14
14
  uri: this._buildURL()
15
15
  }
16
16
 
17
+ this._setHeader('content-type', 'application/json')
18
+
17
19
  // if (this._isValidHook()) {
18
20
  // requestPayload.body = data
19
21
 
@@ -177,6 +179,8 @@ module.exports = function (CaruutoClient) {
177
179
  uri: this._buildURL()
178
180
  }
179
181
 
182
+ this._setHeader('content-type', 'application/json')
183
+
180
184
  // if (this._isValidHook()) {
181
185
  // requestPayload.body = update
182
186
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caruuto/caruuto-js",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "A high-level library for interacting with Caruuto",
5
5
  "exports": "./index.js",
6
6
  "scripts": {
@@ -14,8 +14,7 @@
14
14
  "debug": "^2.6.1",
15
15
  "got": "^11.0.0",
16
16
  "mocha": "^10.2.0",
17
- "query-string": "5.0.1",
18
- "url-parse": "1.4.3"
17
+ "query-string": "5.0.1"
19
18
  },
20
19
  "repository": {
21
20
  "type": "git",