From 68f1f9ac7629d2e859199ff6601e8c2ecce1c595 Mon Sep 17 00:00:00 2001 From: Greg Hogan Date: Wed, 29 Nov 2023 14:44:58 -0800 Subject: [PATCH] add error response callback (#29) * add unauthorized handler * build * fix unauthorizedHandler param * rename to errorResponseHandler * rename to errorResponseCallback --- CHANGELOG.md | 5 +++++ build.sh | 5 +++++ dist/athena.js | 3 ++- dist/billing.js | 3 ++- dist/instance.js | 26 ++++++++++++++++++++------ package.json | 4 ++-- src/athena.js | 4 ++-- src/billing.js | 4 ++-- src/instance.js | 10 +++++++++- 9 files changed, 49 insertions(+), 15 deletions(-) create mode 100755 build.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index 30f4bc2..ce60d72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +Version 3.1.1 +============= +* add error response callback +* revert package name to @commaai/api + Version 3.1.0 ============= * rename package to @comma/api diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..ec34915 --- /dev/null +++ b/build.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -e + +cd "$(dirname "$0")" +docker run --rm --name comma-api-build -v $(pwd):/app -w /app node:16-alpine sh -c "yarn install --immutable --immutable-cache --check-cache && yarn lint && yarn build && yarn generate" diff --git a/dist/athena.js b/dist/athena.js index d9175cd..114a4a9 100644 --- a/dist/athena.js +++ b/dist/athena.js @@ -4,7 +4,8 @@ import Config from './config'; import ConfigRequest from './instance'; var request = new ConfigRequest(Config.ATHENA_URL_ROOT); export function configure(accessToken) { - request.configure(accessToken); + var errorResponseCallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; + request.configure(accessToken, errorResponseCallback); } export function postJsonRpcPayload(_x, _x2) { return _postJsonRpcPayload.apply(this, arguments); diff --git a/dist/billing.js b/dist/billing.js index 24268c8..7c3f48d 100644 --- a/dist/billing.js +++ b/dist/billing.js @@ -4,7 +4,8 @@ import Config from './config'; import ConfigRequest from './instance'; var request = new ConfigRequest(Config.BILLING_URL_ROOT); export function configure(accessToken) { - request.configure(accessToken); + var errorResponseCallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; + request.configure(accessToken, errorResponseCallback); } export function getSubscription(_x) { return _getSubscription.apply(this, arguments); diff --git a/dist/instance.js b/dist/instance.js index 28793fa..b640a1f 100644 --- a/dist/instance.js +++ b/dist/instance.js @@ -34,13 +34,18 @@ var ConfigRequest = /*#__PURE__*/function () { 'Content-Type': 'application/json' }; this.baseUrl = baseUrl + (!baseUrl.endsWith('/') ? '/' : ''); + this.errorResponseCallback = null; } _createClass(ConfigRequest, [{ key: "configure", value: function configure(accessToken) { + var errorResponseCallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; if (accessToken) { this.defaultHeaders.Authorization = "JWT ".concat(accessToken); } + if (errorResponseCallback) { + this.errorResponseCallback = errorResponseCallback; + } } }, { key: "request", @@ -83,23 +88,32 @@ var ConfigRequest = /*#__PURE__*/function () { case 8: resp = _context.sent; if (resp.ok) { + _context.next = 18; + break; + } + if (!this.errorResponseCallback) { _context.next = 14; break; } - _context.next = 12; + _context.next = 13; + return this.errorResponseCallback(resp); + case 13: + return _context.abrupt("return", null); + case 14: + _context.next = 16; return resp.text(); - case 12: + case 16: error = _context.sent; throw new RequestError(resp, "".concat(resp.status, ": ").concat(error)); - case 14: + case 18: if (respJson) { - _context.next = 16; + _context.next = 20; break; } return _context.abrupt("return", resp); - case 16: + case 20: return _context.abrupt("return", resp.json()); - case 17: + case 21: case "end": return _context.stop(); } diff --git a/package.json b/package.json index 51dfee6..a72dfb3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "@comma/api", - "version": "3.1.0", + "name": "@commaai/api", + "version": "3.1.1", "main": "src/index.js", "browser": "dist/index.js", "types": "index.d.ts", diff --git a/src/athena.js b/src/athena.js index 07ff63d..a5302c6 100644 --- a/src/athena.js +++ b/src/athena.js @@ -4,8 +4,8 @@ import ConfigRequest from './instance'; const request = new ConfigRequest(Config.ATHENA_URL_ROOT); -export function configure(accessToken) { - request.configure(accessToken); +export function configure(accessToken, errorResponseCallback = null) { + request.configure(accessToken, errorResponseCallback); } export async function postJsonRpcPayload(dongleId, payload) { diff --git a/src/billing.js b/src/billing.js index 4e7e67b..8b0f5cd 100644 --- a/src/billing.js +++ b/src/billing.js @@ -4,8 +4,8 @@ import ConfigRequest from './instance'; const request = new ConfigRequest(Config.BILLING_URL_ROOT); -export function configure(accessToken) { - request.configure(accessToken); +export function configure(accessToken, errorResponseCallback = null) { + request.configure(accessToken, errorResponseCallback); } export async function getSubscription(dongle_id) { diff --git a/src/instance.js b/src/instance.js index b9d57c8..b0c3070 100644 --- a/src/instance.js +++ b/src/instance.js @@ -14,12 +14,16 @@ export default class ConfigRequest { 'Content-Type': 'application/json', }; this.baseUrl = baseUrl + (!baseUrl.endsWith('/') ? '/' : ''); + this.errorResponseCallback = null; } - configure(accessToken) { + configure(accessToken, errorResponseCallback = null) { if (accessToken) { this.defaultHeaders.Authorization = `JWT ${accessToken}`; } + if (errorResponseCallback) { + this.errorResponseCallback = errorResponseCallback; + } } async request(method, endpoint, params, dataJson = true, respJson = true) { @@ -42,6 +46,10 @@ export default class ConfigRequest { const resp = await fetch(requestUrl, { method, headers, body }); if (!resp.ok) { + if (this.errorResponseCallback) { + await this.errorResponseCallback(resp); + return null; + } const error = await resp.text(); throw new RequestError(resp, `${resp.status}: ${error}`); }