rpcapi
Advanced tools
Comparing version 1.4.0 to 1.5.0
{ | ||
"name": "rpcapi", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "Provides a struture for hosting RPC style APIs, supports both http and websocket access out of the box", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -14,3 +14,3 @@ /// <reference types="express" /> | ||
formatResult(error?: string, result?: any): string; | ||
processRequest(endpointName: string, actionName: string, strParams: any): Promise<any>; | ||
processRequest(endpointName: string, actionName: string, strParams: any, accessKey?: string): Promise<any>; | ||
} |
@@ -58,3 +58,15 @@ "use strict"; | ||
var actionName = req.params.action; | ||
_this.processRequest(endpointName, actionName, req.query) | ||
var authToken = null; | ||
if (req.query.accessKey) { | ||
authToken = req.query.accessKey; | ||
} | ||
else if (req.get('Authorization')) { | ||
var authHeader = req.get('Authorization'); | ||
if (authHeader.startsWith('Bearer ')) { | ||
authToken = authHeader.substring('Bearer '.length); | ||
} | ||
} | ||
//All responses are in json format | ||
res.setHeader('Content-Type', 'application/json'); | ||
_this.processRequest(endpointName, actionName, req.query, authToken) | ||
.then(function (result) { | ||
@@ -110,3 +122,4 @@ debug("Request: " + endpointName + "/" + actionName + " %o: %o", req.query, result); | ||
}; | ||
WebAPIAccessMethod.prototype.processRequest = function (endpointName, actionName, strParams) { | ||
WebAPIAccessMethod.prototype.processRequest = function (endpointName, actionName, strParams, accessKey) { | ||
if (accessKey === void 0) { accessKey = null; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -124,3 +137,3 @@ var endpoint, actionParams, typedParams, paramName, actionParamType, epValue; | ||
} | ||
endpoint.accessKey = strParams.accessKey; | ||
endpoint.accessKey = accessKey; | ||
return [4 /*yield*/, endpoint.callConnect()]; | ||
@@ -127,0 +140,0 @@ case 1: |
@@ -149,2 +149,34 @@ "use strict"; | ||
}); }); | ||
ava_1.default('Allows authenticating via Bearer header', function (t) { return __awaiter(_this, void 0, void 0, function () { | ||
var res1, _a, _b, res2, _c, _d; | ||
return __generator(this, function (_e) { | ||
switch (_e.label) { | ||
case 0: return [4 /*yield*/, node_fetch_1.default('http://localhost:8057/api/test/requiresAuth', { | ||
headers: { | ||
"Authorization": "Bearer myAccessKey" | ||
} | ||
})]; | ||
case 1: | ||
res1 = _e.sent(); | ||
t.is(res1.status, 200); | ||
_b = (_a = t).deepEqual; | ||
return [4 /*yield*/, res1.json()]; | ||
case 2: | ||
_b.apply(_a, [_e.sent(), { error: null, result: { hello: 'world' } }]); | ||
return [4 /*yield*/, node_fetch_1.default('http://localhost:8057/api/test/requiresAuth', { | ||
headers: { | ||
"Authorization": "Bearer invalidaccesskey" | ||
} | ||
})]; | ||
case 3: | ||
res2 = _e.sent(); | ||
t.is(res2.status, 401); | ||
_d = (_c = t).deepEqual; | ||
return [4 /*yield*/, res2.json()]; | ||
case 4: | ||
_d.apply(_c, [_e.sent(), { error: 'Invalid access key', result: null }]); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
//# sourceMappingURL=WebAPIAccessMethod.itest.js.map |
@@ -266,3 +266,3 @@ "use strict"; | ||
accessMethod = new index_1.WebAPIAccessMethod(testApi); | ||
return [4 /*yield*/, accessMethod.processRequest('test', 'testFunc', { accessKey: 'testAccessKey' })]; | ||
return [4 /*yield*/, accessMethod.processRequest('test', 'testFunc', {}, 'testAccessKey')]; | ||
case 1: | ||
@@ -269,0 +269,0 @@ _a.sent(); |
@@ -11,7 +11,7 @@ /// <reference types="node" /> | ||
server: http.Server; | ||
customCreateEndpointInstance: (epClass: typeof APIEndpoint) => APIEndpoint; | ||
customCreateEndpointInstance: (epClass: any) => APIEndpoint; | ||
private endpoints; | ||
registerEndpoint(endpointName: string, endpointClass: new () => APIEndpoint): void; | ||
registerEndpoint(endpointName: string, endpointClass: any): void; | ||
getEndpoint(endpointName: string): APIEndpoint; | ||
listen(port: number, options?: IAPIListenConfig): Promise<void>; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
451302
4381