Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
8bf0a71266 | |||
e499612ecb | |||
e049899599 | |||
37f4d34e7a | |||
ca2e6895ce | |||
ccc5c33656 | |||
546f7f4fc7 | |||
8536060ce4 | |||
e57c332d82 | |||
df87c6c75e | |||
53a4375545 | |||
88022ea14b |
73
package-lock.json
generated
73
package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@apiglobal/typedrequest",
|
||||
"version": "1.0.39",
|
||||
"version": "1.0.45",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
@ -1384,9 +1384,9 @@
|
||||
"integrity": "sha512-P1xLsuA1+8LQpoWCo2nP2vIQXKGUl5wDWU6CD7xTDZc3uw0He5V/qCPHM5zpIZsS7IuZOxDDpWb7aFveB11tXw=="
|
||||
},
|
||||
"@pushrocks/lik": {
|
||||
"version": "4.0.14",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2flik/-/lik-4.0.14.tgz",
|
||||
"integrity": "sha512-S4AMLp+hs1AQ2iCOz5PYReDEfVmfjdf/mf6S5TnAlEhsLU/v3u78xUmMcTPDFotTfuFQr57dGhncHNvljjQ7EQ==",
|
||||
"version": "4.0.17",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2flik/-/lik-4.0.17.tgz",
|
||||
"integrity": "sha512-K5dX3k3i7iVxFMJ+IYwJRljewukJCc2zgj6+88R18/8SajVAq7ITOl3/FTbmEPFCJv5rl/LQ9FtcMynWlwSlzQ==",
|
||||
"requires": {
|
||||
"@pushrocks/smartdelay": "^2.0.10",
|
||||
"@pushrocks/smartmatch": "^1.0.7",
|
||||
@ -1395,49 +1395,6 @@
|
||||
"@pushrocks/smarttime": "^3.0.24",
|
||||
"@types/minimatch": "^3.0.3",
|
||||
"symbol-tree": "^3.2.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pushrocks/smartpromise": {
|
||||
"version": "3.0.6",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.0.6.tgz",
|
||||
"integrity": "sha512-vlQlBGNVIjfClgnsfgQBU6GIKcskYSFzEcKLt18ngPzPEcjKklXcxaqzLXpnoxR+KBh30QPE8255ncYHXuPPOg=="
|
||||
},
|
||||
"@pushrocks/smartrx": {
|
||||
"version": "2.0.18",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartrx/-/smartrx-2.0.18.tgz",
|
||||
"integrity": "sha512-DqUmtS7mONO6bvWEZmSLy4M9hlxNWysMYc7XOZLr50BMaa0HUNYWI1myUFCMNcbUUOxuQNig01XqR1smfIN69g==",
|
||||
"requires": {
|
||||
"@pushrocks/lik": "^4.0.14",
|
||||
"@pushrocks/smartpromise": "^3.0.6",
|
||||
"rxjs": "^6.6.0"
|
||||
}
|
||||
},
|
||||
"@pushrocks/smarttime": {
|
||||
"version": "3.0.24",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmarttime/-/smarttime-3.0.24.tgz",
|
||||
"integrity": "sha512-c/n0Y3CUnVNa3TnuXZ5aRc0bWj4RX7+XSjPes1J4HidU1yBOd9UOt6l08W8aPH5jMjT9Ei2jH0GsheaTGoVEYA==",
|
||||
"requires": {
|
||||
"@pushrocks/lik": "^4.0.13",
|
||||
"@pushrocks/smartdelay": "^2.0.10",
|
||||
"@pushrocks/smartpromise": "^3.0.2",
|
||||
"croner": "^1.1.23",
|
||||
"dayjs": "^1.8.29",
|
||||
"is-nan": "^1.3.0"
|
||||
}
|
||||
},
|
||||
"dayjs": {
|
||||
"version": "1.8.29",
|
||||
"resolved": "https://verdaccio.lossless.one/dayjs/-/dayjs-1.8.29.tgz",
|
||||
"integrity": "sha512-Vm6teig8ZWK7rH/lxzVGxZJCljPdmUr6q/3f4fr5F0VWNGVkZEjZOQJsAN8hUHUqn+NK4XHNEpJZS1MwLyDcLw=="
|
||||
},
|
||||
"rxjs": {
|
||||
"version": "6.6.0",
|
||||
"resolved": "https://verdaccio.lossless.one/rxjs/-/rxjs-6.6.0.tgz",
|
||||
"integrity": "sha512-3HMA8z/Oz61DUHe+SdOiQyzIf4tOx5oQHmMir7IZEu6TMqCLHT4LRcmNaUS0NwOz8VLvmmBduMsoaUvMaIiqzg==",
|
||||
"requires": {
|
||||
"tslib": "^1.9.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"@pushrocks/smartbrowser": {
|
||||
@ -1982,10 +1939,9 @@
|
||||
}
|
||||
},
|
||||
"@pushrocks/smartpromise": {
|
||||
"version": "3.0.2",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.0.2.tgz",
|
||||
"integrity": "sha512-jmrJMUEmBCWChWK8CIcx4Vw3wv/8OgVNmkaxJrbs+WMaoRUfJtpWWJfrAwwHWt9ZXJbarJ+CwfwfYiiZXymndQ==",
|
||||
"dev": true
|
||||
"version": "3.0.6",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.0.6.tgz",
|
||||
"integrity": "sha512-vlQlBGNVIjfClgnsfgQBU6GIKcskYSFzEcKLt18ngPzPEcjKklXcxaqzLXpnoxR+KBh30QPE8255ncYHXuPPOg=="
|
||||
},
|
||||
"@pushrocks/smartpuppeteer": {
|
||||
"version": "1.0.15",
|
||||
@ -2023,7 +1979,6 @@
|
||||
"version": "2.0.18",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartrx/-/smartrx-2.0.18.tgz",
|
||||
"integrity": "sha512-DqUmtS7mONO6bvWEZmSLy4M9hlxNWysMYc7XOZLr50BMaa0HUNYWI1myUFCMNcbUUOxuQNig01XqR1smfIN69g==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@pushrocks/lik": "^4.0.14",
|
||||
"@pushrocks/smartpromise": "^3.0.6",
|
||||
@ -2033,14 +1988,12 @@
|
||||
"@pushrocks/smartpromise": {
|
||||
"version": "3.0.6",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.0.6.tgz",
|
||||
"integrity": "sha512-vlQlBGNVIjfClgnsfgQBU6GIKcskYSFzEcKLt18ngPzPEcjKklXcxaqzLXpnoxR+KBh30QPE8255ncYHXuPPOg==",
|
||||
"dev": true
|
||||
"integrity": "sha512-vlQlBGNVIjfClgnsfgQBU6GIKcskYSFzEcKLt18ngPzPEcjKklXcxaqzLXpnoxR+KBh30QPE8255ncYHXuPPOg=="
|
||||
},
|
||||
"rxjs": {
|
||||
"version": "6.6.0",
|
||||
"resolved": "https://verdaccio.lossless.one/rxjs/-/rxjs-6.6.0.tgz",
|
||||
"integrity": "sha512-3HMA8z/Oz61DUHe+SdOiQyzIf4tOx5oQHmMir7IZEu6TMqCLHT4LRcmNaUS0NwOz8VLvmmBduMsoaUvMaIiqzg==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"tslib": "^1.9.0"
|
||||
}
|
||||
@ -2078,7 +2031,6 @@
|
||||
"version": "3.0.24",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmarttime/-/smarttime-3.0.24.tgz",
|
||||
"integrity": "sha512-c/n0Y3CUnVNa3TnuXZ5aRc0bWj4RX7+XSjPes1J4HidU1yBOd9UOt6l08W8aPH5jMjT9Ei2jH0GsheaTGoVEYA==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@pushrocks/lik": "^4.0.13",
|
||||
"@pushrocks/smartdelay": "^2.0.10",
|
||||
@ -2407,9 +2359,9 @@
|
||||
"integrity": "sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA=="
|
||||
},
|
||||
"@types/node": {
|
||||
"version": "14.0.23",
|
||||
"resolved": "https://verdaccio.lossless.one/@types%2fnode/-/node-14.0.23.tgz",
|
||||
"integrity": "sha512-Z4U8yDAl5TFkmYsZdFPdjeMa57NOvnaf1tljHzhouaPEp7LCj2JKkejpI1ODviIAQuW4CcQmxkQ77rnLsOOoKw=="
|
||||
"version": "14.0.26",
|
||||
"resolved": "https://verdaccio.lossless.one/@types%2fnode/-/node-14.0.26.tgz",
|
||||
"integrity": "sha512-W+fpe5s91FBGE0pEa0lnqGLL4USgpLgs4nokw16SrBBco/gQxuua7KnArSEOd5iaMqbbSHV10vUDkJYJJqpXKA=="
|
||||
},
|
||||
"@types/parcel-bundler": {
|
||||
"version": "1.12.1",
|
||||
@ -4274,8 +4226,7 @@
|
||||
"dayjs": {
|
||||
"version": "1.8.29",
|
||||
"resolved": "https://verdaccio.lossless.one/dayjs/-/dayjs-1.8.29.tgz",
|
||||
"integrity": "sha512-Vm6teig8ZWK7rH/lxzVGxZJCljPdmUr6q/3f4fr5F0VWNGVkZEjZOQJsAN8hUHUqn+NK4XHNEpJZS1MwLyDcLw==",
|
||||
"dev": true
|
||||
"integrity": "sha512-Vm6teig8ZWK7rH/lxzVGxZJCljPdmUr6q/3f4fr5F0VWNGVkZEjZOQJsAN8hUHUqn+NK4XHNEpJZS1MwLyDcLw=="
|
||||
},
|
||||
"deasync": {
|
||||
"version": "0.1.20",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@apiglobal/typedrequest",
|
||||
"version": "1.0.39",
|
||||
"version": "1.0.45",
|
||||
"private": false,
|
||||
"description": "make typed requests towards apis",
|
||||
"main": "dist_ts/index.js",
|
||||
@ -18,15 +18,16 @@
|
||||
"@gitzone/tstest": "^1.0.43",
|
||||
"@pushrocks/smartexpress": "^3.0.73",
|
||||
"@pushrocks/tapbundle": "^3.2.9",
|
||||
"@types/node": "^14.0.23",
|
||||
"@types/node": "^14.0.26",
|
||||
"tslint": "^6.1.2",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@apiglobal/typedrequest-interfaces": "^1.0.15",
|
||||
"@pushrocks/isounique": "^1.0.4",
|
||||
"@pushrocks/lik": "^4.0.14",
|
||||
"@pushrocks/lik": "^4.0.17",
|
||||
"@pushrocks/smartdelay": "^2.0.10",
|
||||
"@pushrocks/smartpromise": "^3.0.6",
|
||||
"@pushrocks/webrequest": "^2.0.10"
|
||||
},
|
||||
"files": [
|
||||
|
70
readme.md
70
readme.md
@ -27,6 +27,76 @@ Platform support | [ => {
|
||||
// lets use the interface in a TypedHandler
|
||||
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 () => {
|
||||
const testTypedRouter = new typedrequest.TypedRouter(); // typed routers can broker typedrequests between handlers
|
||||
testTypedRouter.addTypedHandler(testTypedHandler);
|
||||
testServer.addRoute(
|
||||
'/testroute',
|
||||
new smartexpress.HandlerTypedRouter(testTypedRouter as any) // the "any" is testspecific, since smartexpress ships with its own version of typedrequest.
|
||||
);
|
||||
});
|
||||
|
||||
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('this is the response:');
|
||||
console.log(response);
|
||||
expect(response.surname).to.equal('wow');
|
||||
});
|
||||
|
||||
tap.test('should end the server', async () => {
|
||||
await testServer.stop();
|
||||
});
|
||||
|
||||
tap.start();
|
||||
```
|
||||
|
||||
## Contribution
|
||||
|
||||
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
||||
|
@ -6,6 +6,7 @@ import * as typedrequest from '../ts/index';
|
||||
let testServer: smartexpress.Server;
|
||||
let testTypedHandler: typedrequest.TypedHandler<ITestReqRes>;
|
||||
|
||||
// lets define an interface
|
||||
interface ITestReqRes {
|
||||
method: 'hi';
|
||||
request: {
|
||||
@ -17,6 +18,7 @@ interface ITestReqRes {
|
||||
}
|
||||
|
||||
tap.test('should create a typedHandler', async () => {
|
||||
// lets use the interface in a TypedHandler
|
||||
testTypedHandler = new typedrequest.TypedHandler<ITestReqRes>('hi', async reqArg => {
|
||||
return {
|
||||
surname: 'wow'
|
||||
@ -33,12 +35,11 @@ tap.test('should spawn a server to test with', async () => {
|
||||
});
|
||||
|
||||
tap.test('should define a testHandler', async () => {
|
||||
const testTypedRouter = new typedrequest.TypedRouter(); // typed routers can broker typedrequests between handlers
|
||||
testTypedRouter.addTypedHandler(testTypedHandler);
|
||||
testServer.addRoute(
|
||||
'/testroute',
|
||||
new smartexpress.Handler('POST', async (req, res) => {
|
||||
console.log(req.body);
|
||||
res.json(await testTypedHandler.addResponse(req.body));
|
||||
})
|
||||
new smartexpress.HandlerTypedRouter(testTypedRouter as any) // the "any" is testspecific, since smartexpress ships with its own version of typedrequest.
|
||||
);
|
||||
});
|
||||
|
||||
|
@ -47,7 +47,7 @@ export class TypedHandler<T extends plugins.typedRequestInterfaces.ITypedRequest
|
||||
typedRequestArg.response = response;
|
||||
}
|
||||
|
||||
typedRequestArg.correlation.phase = 'response';
|
||||
typedRequestArg?.correlation?.phase ? typedRequestArg.correlation.phase = 'response' : null;
|
||||
|
||||
return typedRequestArg;
|
||||
}
|
||||
|
@ -2,16 +2,38 @@ import * as plugins from './typedrequest.plugins';
|
||||
import { TypedResponseError } from './typedrequest.classes.typedresponseerror';
|
||||
import { TypedRouter } from './typedrequest.classes.typedrouter';
|
||||
|
||||
export type IPostMethod = (
|
||||
typedRequestPostObject: plugins.typedRequestInterfaces.ITypedRequest
|
||||
) => void | Promise<plugins.typedRequestInterfaces.ITypedRequest>;
|
||||
|
||||
export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest> {
|
||||
public typedRouterRef: TypedRouter;
|
||||
public webrequest = new plugins.webrequest.WebRequest();
|
||||
public urlEndPoint: string;
|
||||
|
||||
/**
|
||||
* in case we post against a url endpoint
|
||||
*/
|
||||
public urlEndPoint?: string;
|
||||
|
||||
/**
|
||||
* in case we post with some other method, ec ipc communication
|
||||
*/
|
||||
public postMethod?: IPostMethod;
|
||||
public method: string;
|
||||
|
||||
// STATIC
|
||||
constructor(urlEndPointArg: string, methodArg: T['method']) {
|
||||
this.urlEndPoint = urlEndPointArg;
|
||||
constructor(
|
||||
postEndPointArg: string | IPostMethod,
|
||||
methodArg: T['method'],
|
||||
typedrouterRefArg?: TypedRouter
|
||||
) {
|
||||
if (typeof postEndPointArg === 'string') {
|
||||
this.urlEndPoint = postEndPointArg;
|
||||
} else {
|
||||
this.postMethod = postEndPointArg;
|
||||
}
|
||||
this.method = methodArg;
|
||||
this.typedRouterRef = typedrouterRefArg;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -24,12 +46,34 @@ export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest
|
||||
response: null,
|
||||
correlation: {
|
||||
id: plugins.isounique.uni(),
|
||||
phase: 'request'
|
||||
}
|
||||
phase: 'request',
|
||||
},
|
||||
};
|
||||
|
||||
const response = await this.webrequest.postJson(this.urlEndPoint, payload);
|
||||
const responseBody: T = response;
|
||||
let responseBody: plugins.typedRequestInterfaces.ITypedRequest;
|
||||
if (this.urlEndPoint) {
|
||||
const response = await this.webrequest.postJson(this.urlEndPoint, payload);
|
||||
responseBody = response;
|
||||
} else {
|
||||
let responseInterest: plugins.lik.Interest<
|
||||
string,
|
||||
plugins.typedRequestInterfaces.ITypedRequest
|
||||
>;
|
||||
if (this.typedRouterRef) {
|
||||
responseInterest = await this.typedRouterRef.fireEventInterestMap.addInterest(
|
||||
payload.correlation.id,
|
||||
payload
|
||||
);
|
||||
}
|
||||
const postMethodReturnValue = await this.postMethod(payload);
|
||||
if (responseInterest) {
|
||||
responseBody = await responseInterest.interestFullfilled;
|
||||
} else if (postMethodReturnValue) {
|
||||
responseBody = postMethodReturnValue;
|
||||
} else {
|
||||
responseBody = payload;
|
||||
}
|
||||
}
|
||||
if (responseBody.error) {
|
||||
console.error(
|
||||
`Got an error ${responseBody.error.text} with data ${JSON.stringify(
|
||||
@ -49,13 +93,4 @@ export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest
|
||||
}
|
||||
return responseBody.response;
|
||||
}
|
||||
|
||||
public addTypedRouterForResponse(typedRouterArg: TypedRouter) {
|
||||
if (!this.typedRouterRef) {
|
||||
this.typedRouterRef = typedRouterArg;
|
||||
typedRouterArg.addTypedRequest(this);
|
||||
}
|
||||
}
|
||||
|
||||
public registerAlternateFireMethod() {}
|
||||
}
|
||||
|
@ -12,21 +12,15 @@ export class TypedRouter {
|
||||
public upstreamTypedRouter: TypedRouter;
|
||||
|
||||
public routerMap = new plugins.lik.ObjectMap<TypedRouter>();
|
||||
public typedRequestMap = new plugins.lik.ObjectMap<TypedRequest<any>>();
|
||||
|
||||
public handlerMap = new plugins.lik.ObjectMap<
|
||||
TypedHandler<plugins.typedRequestInterfaces.ITypedRequest>
|
||||
>();
|
||||
|
||||
public fireEventInterestMap = new plugins.lik.InterestMap((correlationId: string) => correlationId);
|
||||
|
||||
public addTypedRequest(typedRequestArg: TypedRequest<any>) {
|
||||
if(!this.typedRequestMap.checkForObject(typedRequestArg)) {
|
||||
this.typedRequestMap.add(typedRequestArg);
|
||||
typedRequestArg.addTypedRouterForResponse(this);
|
||||
}
|
||||
}
|
||||
|
||||
public fireEventInterestMap = new plugins.lik.InterestMap<
|
||||
string,
|
||||
plugins.typedRequestInterfaces.ITypedRequest
|
||||
>((correlationId: string) => correlationId);
|
||||
|
||||
/**
|
||||
* adds the handler to the routing map
|
||||
@ -98,7 +92,7 @@ export class TypedRouter {
|
||||
* @param typedRequestArg
|
||||
*/
|
||||
public async routeAndAddResponse(typedRequestArg: plugins.typedRequestInterfaces.ITypedRequest) {
|
||||
if (typedRequestArg.correlation.phase === 'request') {
|
||||
if (!typedRequestArg?.correlation?.phase || typedRequestArg.correlation.phase === 'request') {
|
||||
const typedHandler = this.getTypedHandlerForMethod(typedRequestArg.method);
|
||||
|
||||
if (!typedHandler) {
|
||||
@ -112,7 +106,9 @@ export class TypedRouter {
|
||||
|
||||
typedRequestArg = await typedHandler.addResponse(typedRequestArg);
|
||||
} else if (typedRequestArg.correlation.phase === 'response') {
|
||||
|
||||
this.fireEventInterestMap
|
||||
.findInterest(typedRequestArg.correlation.id)
|
||||
?.fullfillInterest(typedRequestArg);
|
||||
}
|
||||
return typedRequestArg;
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ export { typedRequestInterfaces };
|
||||
import * as isounique from '@pushrocks/isounique';
|
||||
import * as lik from '@pushrocks/lik';
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as webrequest from '@pushrocks/webrequest';
|
||||
|
||||
export { isounique, lik, smartdelay, webrequest };
|
||||
export { isounique, lik, smartdelay, smartpromise, webrequest };
|
||||
|
Reference in New Issue
Block a user