@ministryofjustice/fb-jwt-client-node
Advanced tools
Comparing version 0.0.30 to 0.0.33
@@ -17,37 +17,66 @@ const got = require('got') | ||
if (response.status) { | ||
responseLabels.status = response.status | ||
} | ||
if (response.status) responseLabels.status = response.status | ||
if (response.statusCode) { | ||
responseLabels.status_code = response.statusCode | ||
} | ||
/* | ||
* Are the fields present? | ||
*/ | ||
if ( | ||
Reflect.has(response, 'statusCode') && | ||
Reflect.has(response, 'statusMessage')) { | ||
const statusCode = Reflect.get(response, 'statusCode') | ||
const statusMessage = Reflect.get(response, 'statusMessage') | ||
/* | ||
* Are the values null? | ||
*/ | ||
if (statusCode) responseLabels.status_code = statusCode | ||
if (statusMessage) responseLabels.status_message = statusMessage | ||
} else { | ||
/* | ||
* Alternatively, explicitly test for an Error | ||
*/ | ||
if (response instanceof Error) { | ||
if (Reflect.has(response, 'body')) { | ||
const { | ||
body: { | ||
code, | ||
name: message | ||
} | ||
} = response | ||
if (response.statusMessage) { | ||
responseLabels.status_message = response.statusMessage | ||
} | ||
if (code) responseLabels.error_code = code | ||
if (message) responseLabels.error_message = message | ||
} else { | ||
const code = getErrorStatusCode(Reflect.get(response, 'code') || Reflect.get(response, 'name')) | ||
const message = getErrorStatusMessage(code, Reflect.get(response, 'message')) | ||
if (response.name) { | ||
responseLabels.error_name = response.name | ||
responseLabels.error_code = code | ||
responseLabels.error_message = message | ||
} | ||
} | ||
} | ||
if (response.message) { | ||
responseLabels.error_message = response.message | ||
} | ||
return responseLabels | ||
} | ||
const getErrorStatusCode = (message) => { | ||
let statusCode = 500 | ||
if (message === 'ENOTFOUND') { | ||
// no dns resolution | ||
statusCode = 502 | ||
} else if (message === 'ECONNREFUSED') { | ||
// connection rejected | ||
statusCode = 503 | ||
/* | ||
* Known errors. This can, of course, be extended | ||
*/ | ||
const getErrorStatusCode = (key) => { | ||
switch (key) { | ||
case 'HTTPError': | ||
return 404 | ||
case 'ENOTFOUND': | ||
return 502 | ||
case 'ECONNREFUSED': | ||
return 503 | ||
default: | ||
return 500 | ||
} | ||
return statusCode | ||
} | ||
/* | ||
* This can, of course, be extended, too | ||
*/ | ||
const getErrorStatusMessage = (key, message) => key === 404 ? 'Not Found' : message | ||
/** | ||
@@ -82,2 +111,3 @@ * Creates client using JSON Web Tokens | ||
} | ||
if (!serviceSecret) { | ||
@@ -107,2 +137,3 @@ this.throwRequestError('ENOSERVICESECRET', 'No service secret passed to client') | ||
} | ||
this.requestMetrics = { | ||
@@ -198,9 +229,8 @@ startTimer | ||
* | ||
* @param {string} [ivSeed] | ||
* Initialization Vector | ||
* | ||
* @return {string} | ||
* | ||
**/ | ||
encryptUserIdAndToken (userId, userToken, ivSeed = userId + userToken) { | ||
encryptUserIdAndToken (userId, userToken) { | ||
const ivSeed = userId + userToken | ||
return this.encrypt(this.serviceSecret, {userId, userToken}, ivSeed) | ||
@@ -212,3 +242,3 @@ } | ||
* | ||
* @param {string} encryptedData | ||
* @param {string} data | ||
* Encrypted user ID and token | ||
@@ -219,4 +249,4 @@ * | ||
**/ | ||
decryptUserIdAndToken (encryptedData) { | ||
return this.decrypt(this.serviceSecret, encryptedData) | ||
decryptUserIdAndToken (data) { | ||
return this.decrypt(this.serviceSecret, data) | ||
} | ||
@@ -275,10 +305,10 @@ | ||
if (hasData) { | ||
if (isGET) { | ||
if (isGET) { | ||
if (hasData) { | ||
requestOptions.searchParams = { | ||
payload: Buffer.from(JSON.stringify(data)).toString('Base64') | ||
} | ||
} else { | ||
requestOptions.body = data | ||
} | ||
} else { | ||
requestOptions.json = data | ||
} | ||
@@ -326,3 +356,3 @@ | ||
logger.error(logObject, logMessage) | ||
if (logger) logger.error(logObject, logMessage) | ||
} | ||
@@ -378,5 +408,2 @@ | ||
let requestMetricsEnd | ||
let retryCounter = 1 | ||
function logError (type, error) { | ||
@@ -386,2 +413,5 @@ client.logError(type, error, Object.assign({}, labels, {name: `jwt_${type.toLowerCase()}_request_error`}), logger) | ||
let requestMetricsEnd | ||
let retryCounter = 1 | ||
const gotOptions = got.mergeOptions(got.defaults.options, { | ||
@@ -398,3 +428,3 @@ hooks: { | ||
logError('client', error) | ||
if (logger) logError('client', error) | ||
@@ -414,22 +444,6 @@ if (requestMetricsEnd) requestMetricsEnd(getResponseLabels(error)) | ||
(response) => { | ||
if (response.statusCode >= 400) { | ||
const { | ||
statusCode, | ||
statusMessage, | ||
body, | ||
retryCount | ||
} = response | ||
if (response.statusCode >= 400 && logger) logError('client', response) | ||
const error = { | ||
statusCode, | ||
statusMessage, | ||
body, | ||
retryCount | ||
} | ||
logError('client', error) | ||
} | ||
if (requestMetricsEnd) requestMetricsEnd(getResponseLabels(response)) | ||
response.body = response.body || {} | ||
response.body = response.body || '{}' | ||
return response | ||
@@ -439,3 +453,3 @@ } | ||
} | ||
}, sendOptions, requestOptions) | ||
}, sendOptions, requestOptions, {method}) | ||
@@ -454,7 +468,5 @@ const apiMetricsEnd = this.apiMetrics.startTimer(labels) | ||
if (response.statusCode < 300) { | ||
if (response.body && /^\s*$/.test(response.body)) { | ||
if (requestMetricsEnd) requestMetricsEnd(getResponseLabels(response)) | ||
return {} | ||
} | ||
apiMetricsEnd(getResponseLabels(e)) | ||
if (response.body && /^\s*$/.test(response.body)) return {} | ||
return response.body | ||
@@ -467,3 +479,3 @@ } | ||
this.handleRequestError(e) | ||
client.handleRequestError(e) | ||
} | ||
@@ -470,0 +482,0 @@ } |
{ | ||
"name": "@ministryofjustice/fb-jwt-client-node", | ||
"version": "0.0.30", | ||
"version": "0.0.33", | ||
"description": "Form Builder JSON Web Token Client (Node)", | ||
@@ -20,3 +20,3 @@ "main": "lib/fb-jwt-client.js", | ||
"aes256": "^1.0.4", | ||
"got": "^10.0.1", | ||
"got": "10.0.1", | ||
"jsonwebtoken": "^8.5.1", | ||
@@ -31,5 +31,5 @@ "path-to-regexp": "^6.1.0" | ||
"nock": "^11.6.0", | ||
"proxyquire": "^2.1.3", | ||
"sinon": "^7.5.0", | ||
"sinon-chai": "^3.3.0" | ||
"sinon-chai": "^3.3.0", | ||
"proxyquire": "^2.1.3" | ||
}, | ||
@@ -36,0 +36,0 @@ "_moduleAliases": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
21906
5
632
+ Added@sindresorhus/is@1.2.0(transitive)
+ Added@szmarczak/http-timer@3.1.1(transitive)
+ Added@types/node@22.9.3(transitive)
+ Addedcacheable-lookup@0.2.2(transitive)
+ Addeddefer-to-connect@1.1.3(transitive)
+ Addedgot@10.0.1(transitive)
+ Addedtype-fest@0.8.1(transitive)
+ Addedundici-types@6.19.8(transitive)
- Removed@sindresorhus/is@2.1.1(transitive)
- Removed@szmarczak/http-timer@4.0.6(transitive)
- Removed@types/node@22.10.1(transitive)
- Removedcacheable-lookup@2.0.1(transitive)
- Removeddefer-to-connect@2.0.1(transitive)
- Removedgot@10.7.0(transitive)
- Removedp-event@4.2.0(transitive)
- Removedp-finally@1.0.0(transitive)
- Removedp-timeout@3.2.0(transitive)
- Removedtype-fest@0.10.0(transitive)
- Removedundici-types@6.20.0(transitive)
Updatedgot@10.0.1