Compare commits

...

2 Commits

Author SHA1 Message Date
7c3c7ca90e 1.0.23 2019-09-01 13:17:21 +02:00
db474ebd8a fix(core): update 2019-09-01 13:17:21 +02:00
4 changed files with 36 additions and 7 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "@apiglobal/typedrequest",
"version": "1.0.22",
"version": "1.0.23",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@ -1,6 +1,6 @@
{
"name": "@apiglobal/typedrequest",
"version": "1.0.22",
"version": "1.0.23",
"private": false,
"description": "make typed requests towards apis",
"main": "dist/index.js",

View File

@ -4,6 +4,25 @@ import * as smartexpress from '@pushrocks/smartexpress';
import * as typedrequest from '../ts/index';
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({
@ -14,13 +33,24 @@ tap.test('should spawn a server to test with', async () => {
});
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'
});
expect(response.surname).to.equal('wow');
});
tap.test('should end the server', async () => {
await testServer.stop();
});

View File

@ -14,13 +14,12 @@ export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest
* firest the request
*/
public async fire(fireArg: T['request']): Promise<T['response']> {
const response = await plugins.smartrequest.request(this.urlEndPoint, {
method: 'POST',
requestBody: JSON.stringify({
const response = await plugins.smartrequest.postJson(this.urlEndPoint, {
requestBody: {
method: this.method,
request: fireArg,
response: null
})
}
});
return response.body.response;
}