Skip to content

Commit

Permalink
add error response callback (#29)
Browse files Browse the repository at this point in the history
* add unauthorized handler

* build

* fix unauthorizedHandler param

* rename to errorResponseHandler

* rename to errorResponseCallback
  • Loading branch information
gregjhogan authored Nov 29, 2023
1 parent bd70c65 commit 68f1f9a
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 5 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 2 additions & 1 deletion dist/athena.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion dist/billing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 20 additions & 6 deletions dist/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/athena.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/billing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 9 additions & 1 deletion src/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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}`);
}
Expand Down

0 comments on commit 68f1f9a

Please sign in to comment.