Compare commits

...

34 Commits

Author SHA1 Message Date
25a9bb4ec8 1.0.26 2019-09-01 15:39:57 +02:00
2796498984 fix(core): update 2019-09-01 15:39:56 +02:00
68e16e80f0 1.0.25 2019-09-01 15:39:08 +02:00
d1aa752c8e fix(core): update 2019-09-01 15:39:08 +02:00
e7d8731fca 1.0.24 2019-09-01 14:14:29 +02:00
7c3c7ca90e 1.0.23 2019-09-01 13:17:21 +02:00
db474ebd8a fix(core): update 2019-09-01 13:17:21 +02:00
51219303a9 1.0.22 2019-08-31 23:15:05 +02:00
ccc9303c63 fix(core): update 2019-08-31 23:15:05 +02:00
69b7147ae7 1.0.21 2019-08-31 23:14:47 +02:00
3667946b2f fix(core): update 2019-08-31 23:14:46 +02:00
aef1e6183c 1.0.20 2019-08-31 23:05:37 +02:00
68526888e7 1.0.19 2019-08-31 23:04:37 +02:00
54d4851de1 fix(core): update 2019-08-31 23:04:37 +02:00
362d8cb72d 1.0.18 2019-08-31 14:11:57 +02:00
f54dfb5bb5 fix(core): update 2019-08-31 14:11:56 +02:00
e81b248181 1.0.17 2019-08-26 17:49:19 +02:00
d92f0fb2e2 1.0.16 2019-08-26 17:08:10 +02:00
b858f9f711 fix(core): update 2019-08-26 17:08:09 +02:00
50cc5fdbe6 1.0.15 2019-08-25 17:19:13 +02:00
386d848d51 fix(core): update 2019-08-25 17:19:12 +02:00
9db92be274 1.0.14 2019-08-25 10:42:47 +02:00
4a06bedf3b 1.0.13 2019-08-23 18:01:38 +02:00
9787adf3f0 fix(core): update 2019-08-23 18:01:37 +02:00
280e67b86b 1.0.12 2019-08-23 17:07:34 +02:00
592e941211 fix(core): update 2019-08-23 17:07:34 +02:00
68ec93459b 1.0.11 2019-08-23 17:01:26 +02:00
8a4b3fa47e fix(core): update 2019-08-23 17:01:26 +02:00
ad7c2e97b8 1.0.10 2019-08-23 16:37:40 +02:00
0ff28152f3 fix(core): update 2019-08-23 16:37:40 +02:00
4718a03d7d 1.0.9 2019-08-23 14:53:39 +02:00
f99f79efb1 fix(core): update 2019-08-23 14:53:39 +02:00
ac653ac082 1.0.8 2019-08-23 14:50:12 +02:00
a8ee6b733f fix(core): update 2019-08-23 14:50:11 +02:00
9 changed files with 1382 additions and 68 deletions

View File

@ -30,7 +30,7 @@ snyk:
script:
- npmci npm prepare
- npmci node install stable
- npmci command npm cache verify
- npmci command npm cache clean --force
- npmci command npm install -g snyk
- npmci command npm install --ignore-scripts
- npmci command snyk test

1246
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "@apiglobal/typedrequest",
"version": "1.0.7",
"version": "1.0.26",
"private": false,
"description": "make typed requests towards apis",
"main": "dist/index.js",
@ -15,13 +15,16 @@
"devDependencies": {
"@gitzone/tsbuild": "^2.0.22",
"@gitzone/tstest": "^1.0.15",
"@pushrocks/smartexpress": "^3.0.40",
"@pushrocks/tapbundle": "^3.0.13",
"@types/node": "^12.7.2",
"tslint": "^5.19.0",
"tslint-config-prettier": "^1.18.0"
},
"dependencies": {
"@apiglobal/typedrequest-interfaces": "^1.0.4",
"@apiglobal/typedrequest-interfaces": "^1.0.7",
"@pushrocks/lik": "^3.0.11",
"@pushrocks/smartjson": "^3.0.8",
"@pushrocks/smartrequest": "^1.1.23"
},
"files": [

View File

@ -1,8 +1,59 @@
import { expect, tap } from '@pushrocks/tapbundle';
import * as smartexpress from '@pushrocks/smartexpress';
import * as typedrequest from '../ts/index';
tap.test('first test', async () => {
typedrequest.TypedRequest;
let testServer: smartexpress.Server;
let testTypedHandler: typedrequest.TypedHandler<ITestReqRes>;
interface ITestReqRes {
method: 'hi';
request: {
name: string;
};
response: {
surname: string;
};
}
tap.test('should create a typedHandler', async () => {
testTypedHandler = new typedrequest.TypedHandler<ITestReqRes>('hi', async (reqArg) => {
return {
surname: 'wow'
};
});
});
tap.test('should spawn a server to test with', async () => {
testServer = new smartexpress.Server({
cors: true,
forceSsl: false,
port: 3000
});
});
tap.test('should define a testHandler', async () => {
testServer.addRoute('/testroute', new smartexpress.Handler('POST', async (req, res) => {
console.log(req.body);
res.json(await testTypedHandler.addResponse(req.body));
}));
});
tap.test('should start the server', async () => {
await testServer.start();
});
tap.test('should fire a request', async () => {
const typedRequest = new typedrequest.TypedRequest<ITestReqRes>('http://localhost:3000/testroute', 'hi');
const response = await typedRequest.fire({
name: 'really'
});
console.log(response);
expect(response.surname).to.equal('wow');
});
tap.test('should end the server', async () => {
await testServer.stop();
});
tap.start();

View File

@ -1,27 +1,3 @@
import * as plugins from './typedrequest.plugins';
export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest> {
public urlEndPoint: string;
public method: string;
// STATIC
constructor(urlEndPointArg: string, methodArg: T['method']) {
this.urlEndPoint = urlEndPointArg;
this.method = methodArg;
}
/**
* firest the request
*/
public async fire(fireArg: T['request']): Promise<T['response']> {
const response = await plugins.smartrequest.request(this.urlEndPoint, {
method: 'POST',
requestBody: {
method: this.method,
request: fireArg,
response: null
}
});
return response.body.response;
}
}
export * from './typedrequest.classes.typedrequest';
export * from './typedrequest.classes.typedhandler';
export * from './typedrequest.classes.typedrouter';

View File

@ -0,0 +1,29 @@
import * as plugins from './typedrequest.plugins';
type THandlerFunction<T extends plugins.typedRequestInterfaces.ITypedRequest> = (requestArg: T['request']) => Promise<T['response']>;
/**
* typed handler for dealing with typed requests
*/
export class TypedHandler<T extends plugins.typedRequestInterfaces.ITypedRequest> {
public method: string;
private handlerFunction: THandlerFunction<T>;
constructor(methodArg: T['method'], handlerFunctionArg: THandlerFunction<T>) {
this.method = methodArg;
this.handlerFunction = handlerFunctionArg;
}
/**
* adds a response to the typedRequest
* @param typedRequestArg
*/
public async addResponse(typedRequestArg: T) {
if (typedRequestArg.method !== this.method) {
throw new Error('this handler has been given a wrong method to answer to. Please use a TypedRouter to filter requests');
}
const response = await this.handlerFunction(typedRequestArg.request);
typedRequestArg.response = response;
return typedRequestArg;
}
}

View File

@ -0,0 +1,31 @@
import * as plugins from './typedrequest.plugins';
export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest> {
public urlEndPoint: string;
public method: string;
// STATIC
constructor(urlEndPointArg: string, methodArg: T['method']) {
this.urlEndPoint = urlEndPointArg;
this.method = methodArg;
}
/**
* firest the request
*/
public async fire(fireArg: T['request']): Promise<T['response']> {
const response = await plugins.smartrequest.postJson(this.urlEndPoint, {
requestBody: {
method: this.method,
request: fireArg,
response: null
}
});
const responseBody: T = response.body;
if (responseBody.error) {
console.log(responseBody.error.text);
console.log(responseBody.error.data);
}
return response.body.response;
}
}

View File

@ -0,0 +1,46 @@
import * as plugins from './typedrequest.plugins';
import { TypedHandler } from './typedrequest.classes.typedhandler';
/**
* A typed router decides on which typed handler to call based on the method
* specified in the typed request
* This is thought for reusing the same url endpoint for different methods
*/
export class TypedRouter {
public handlerMap = new plugins.lik.Objectmap<
TypedHandler<plugins.typedRequestInterfaces.ITypedRequest>
>();
/**
* adds the handler to the routing map
* @param handlerArg
*/
public addTypedHandler<T extends plugins.typedRequestInterfaces.ITypedRequest>(handlerArg: TypedHandler<T>) {
this.handlerMap.add(handlerArg);
}
public async addResponse(typedRequest: plugins.typedRequestInterfaces.ITypedRequest) {
const typedHandler = this.handlerMap.find(handler => {
return handler.method === typedRequest.method;
});
if (!typedHandler) {
const availableMethods: string[] = [];
await this.handlerMap.forEach(async (handler) => {
availableMethods.push(handler.method);
});
console.log(`Cannot find method for ${typedHandler}`);
console.log(`Available methods are:`);
console.log(availableMethods);
typedRequest.error = {
text: 'There is no available method for this call on the server side',
data: {}
};
return typedRequest;
}
typedRequest = await typedHandler.addResponse(typedRequest);
return typedRequest;
}
}

View File

@ -4,6 +4,8 @@ import * as typedRequestInterfaces from '@apiglobal/typedrequest-interfaces';
export { typedRequestInterfaces };
// pushrocks scope
import * as lik from '@pushrocks/lik';
import * as smartrequest from '@pushrocks/smartrequest';
import * as smartjson from '@pushrocks/smartjson';
export { smartrequest };
export { lik, smartrequest, smartjson };