service2service 2.2.0 → 2.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/README.md CHANGED
@@ -34,24 +34,23 @@ agent.generate()
34
34
  // token is verified, error gets thrown if not verified
35
35
  })
36
36
 
37
- // This is an instance of request promise that will automatically add the
37
+ // This is a wrapper around axios that will automatically add the
38
38
  // header to every request. This takes care of the `generate` functionality for
39
39
  // you.
40
40
 
41
- // Note that this is just a wrapper around request promise because we have to
41
+ // Note that this is just a wrapper around axios because we have to
42
42
  // handle the case of asynchronous secret getting. If you wish to use the raw
43
- // request object, implement the `generate()` method into your workflow
43
+ // axios object, implement the `generate()` method into your workflow
44
44
  agent
45
45
  .request({
46
- uri: 'http://www.example.com',
47
- method: 'POST'
48
- body: {
46
+ url: 'http://www.example.com',
47
+ method: 'POST',
48
+ data: {
49
49
  some: 'payload'
50
- },
51
- resolveWithFullResponse: true,
52
- json: true
50
+ }
53
51
  })
54
- .then(({ body, statusCode }) => {
52
+ .then((response) => {
53
+ // response is the response body (axios automatically parses JSON)
55
54
  // Successful request!
56
55
  })
57
56
  ```
@@ -179,15 +178,37 @@ parameter is optional and is only used if you want to override the agent's
179
178
 
180
179
  ## `agent.request(reqOptions[, genOptions [, payload])`
181
180
 
182
- This is wrapper around [`request-promise`][request]. It accepts all the same
183
- options. Note that this is just a wrapper function and does not provide the
184
- sugar methods like `request.post()` or `request.get()`.
181
+ This is a wrapper around [`axios`][axios]. It accepts axios-compatible options.
182
+ Note that this is just a wrapper function and does not provide the
183
+ sugar methods like `axios.post()` or `axios.get()`.
184
+
185
+ - `reqOptions` {Object} - The options you pass into axios. You can see all
186
+ of the available options [`here`][axios]. Common options:
187
+ - `url` {string} - The URL to request
188
+ - `method` {string} - HTTP method (GET, POST, etc.)
189
+ - `data` {Object|string} - Request body
190
+ - `headers` {Object} - Custom headers
191
+ - `params` {Object} - URL query parameters
192
+
193
+ **Backward compatibility:** For compatibility with `request-promise`, the
194
+ following aliases are also supported:
195
+ - `uri` - Alias for `url`
196
+ - `body` - Alias for `data`
197
+ - `qs` - Alias for `params`
185
198
 
186
- - `reqOptions` {Object} - The options you pass into `request`. You can see all
187
- of the available options [`here`][request]
188
199
  - `genOptions` {Object} - The options that get passed into `agent.generate()`
189
200
  - `payload` {Object} - The payload that gets passed into `agent.generate()`
190
201
 
202
+ **Return value:** The method returns a promise that resolves to the response
203
+ body (`response.data`), not the full response object.
204
+
205
+ **Error handling:** HTTP errors (4xx, 5xx) will reject with an error object
206
+ containing:
207
+ - `message` - The response body or error message
208
+ - `statusCode` - The HTTP status code
209
+ - `error` - The response body (for compatibility with request-promise)
210
+ - `response` - The full axios response object (for debugging)
211
+
191
212
  ## middleware
192
213
 
193
214
  The api for express middleware and koa middleware is the same. It accepts all
@@ -216,4 +237,4 @@ koaMiddleware(agent)
216
237
  `agent.verify(token, options)`
217
238
 
218
239
  [jwt.sign]: https://www.npmjs.com/package/jsonwebtoken#user-content-jwtsignpayload-secretorprivatekey-options-callback
219
- [request]: https://www.npmjs.com/package/request-promise
240
+ [axios]: https://www.npmjs.com/package/axios
@@ -0,0 +1,62 @@
1
+ 'use strict'
2
+
3
+ const axios = require('axios')
4
+
5
+ function mapOption(reqOptions, from, to) {
6
+ const hasFrom = typeof reqOptions[from] !== 'undefined'
7
+ const noTo = typeof reqOptions[to] === 'undefined'
8
+ if (hasFrom && noTo) {
9
+ reqOptions[to] = reqOptions[from]
10
+ delete reqOptions[from]
11
+ }
12
+ }
13
+
14
+ function requestOptionsToAxios(reqOptions) {
15
+ if (reqOptions.uri && !reqOptions.url) {
16
+ reqOptions.url = reqOptions.uri
17
+ delete reqOptions.uri
18
+ }
19
+ mapOption(reqOptions, 'qs', 'params')
20
+ mapOption(reqOptions, 'body', 'data')
21
+ delete reqOptions.json
22
+ }
23
+
24
+
25
+ function executeAxiosRequest(reqOptions) {
26
+ const resolveWithFullResponse = reqOptions.resolveWithFullResponse === true
27
+ const simple = reqOptions.simple !== false
28
+ if ('resolveWithFullResponse' in reqOptions) {
29
+ delete reqOptions.resolveWithFullResponse
30
+ }
31
+ if ('simple' in reqOptions) delete reqOptions.simple
32
+
33
+ requestOptionsToAxios(reqOptions)
34
+ if (simple === false) {
35
+ reqOptions.validateStatus = () => true
36
+ }
37
+
38
+ return axios(reqOptions)
39
+ .then((response) => {
40
+ if (resolveWithFullResponse) {
41
+ return {
42
+ statusCode: response.status,
43
+ headers: response.headers,
44
+ body: response.data
45
+ }
46
+ }
47
+ return response.data
48
+ })
49
+ .catch((error) => {
50
+ if (error.response) {
51
+ const err = new Error(error.response.data || error.message)
52
+ err.statusCode = error.response.status
53
+ err.error = error.response.data
54
+ err.response = error.response
55
+ return Promise.reject(err)
56
+ }
57
+ return Promise.reject(error)
58
+ })
59
+ }
60
+
61
+ module.exports = requestOptionsToAxios
62
+ module.exports.executeAxiosRequest = executeAxiosRequest
package/index.js CHANGED
@@ -1,11 +1,11 @@
1
1
  'use strict'
2
2
 
3
3
  const Promise = require('bluebird')
4
- const request = require('request-promise')
5
4
  const jwt = require('jsonwebtoken')
6
5
  const uuid = require('uuid')
7
6
  const tryFallbacks = require('./lib/try-fallbacks')
8
7
  const Housekeeper = require('./lib/housekeeper')
8
+ const { executeAxiosRequest } = require('./helpers/requestOptionsToAxios')
9
9
 
10
10
  Promise.promisifyAll(jwt)
11
11
 
@@ -87,7 +87,7 @@ class ServiceAgent {
87
87
  reqOptions.headers = Object.assign({
88
88
  [this.header]: this.tokenPrefix + token
89
89
  }, reqOptions.headers)
90
- return request(reqOptions)
90
+ return executeAxiosRequest(reqOptions)
91
91
  })
92
92
  }
93
93
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "service2service",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "description": "A node module to make service to service communication secure and easy",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -13,6 +13,7 @@
13
13
  },
14
14
  "files": [
15
15
  "lib",
16
+ "helpers",
16
17
  "express.js",
17
18
  "index.js",
18
19
  "koa.js"
@@ -38,10 +39,9 @@
38
39
  },
39
40
  "homepage": "https://github.com/KualiCo/service2service#readme",
40
41
  "dependencies": {
42
+ "axios": "^0.21.2",
41
43
  "bluebird": "^3.5.0",
42
44
  "jsonwebtoken": "^8.4.0",
43
- "request": "^2.81.0",
44
- "request-promise": "^4.2.0",
45
45
  "uuid": "^3.0.1"
46
46
  },
47
47
  "devDependencies": {