Compare commits

...

27 Commits

Author SHA1 Message Date
8e64353026 1.0.63 2021-11-07 23:46:33 +01:00
290746c191 fix(core): update 2021-11-07 23:46:32 +01:00
abefef8d7c 1.0.62 2021-11-07 15:45:18 +01:00
81b042e670 fix(core): update 2021-11-07 15:45:17 +01:00
6e3ee011a9 1.0.61 2021-11-07 15:40:02 +01:00
9b5ff4b1b5 fix(core): update 2021-11-07 15:40:01 +01:00
556ba6cb30 1.0.60 2021-11-07 15:26:13 +01:00
7321ac680d fix(core): update 2021-11-07 15:26:12 +01:00
2fd8219849 1.0.59 2021-11-07 14:56:48 +01:00
ea56e2218f fix(types): better types for TypedRouter.routeAndAddResponse 2021-11-07 14:56:48 +01:00
9a07817914 1.0.58 2021-09-27 12:15:47 +02:00
9bc83b0d1e 1.0.57 2021-09-27 12:14:43 +02:00
98c638e1ab fix(core): update 2021-09-27 12:14:43 +02:00
575ddd36a0 1.0.56 2020-12-21 00:01:06 +00:00
52b731ce68 fix(core): update 2020-12-21 00:00:56 +00:00
3f6e81b2aa 1.0.55 2020-12-18 18:14:30 +00:00
adad99f6bf fix(core): update 2020-12-18 18:14:29 +00:00
2771c92e85 1.0.54 2020-10-09 10:25:29 +00:00
440ea9ff3a fix(core): update 2020-10-09 10:25:28 +00:00
51bb8dfa90 1.0.53 2020-10-06 21:33:47 +00:00
ce3bfa01b4 fix(core): update 2020-10-06 21:33:46 +00:00
265109fca6 1.0.52 2020-10-06 17:37:07 +00:00
8bfd4d8866 fix(core): update 2020-10-06 17:37:06 +00:00
785f247027 1.0.51 2020-10-06 17:28:00 +00:00
3f3f488dc4 fix(core): update 2020-10-06 17:27:59 +00:00
0241eda296 1.0.50 2020-10-06 16:37:25 +00:00
66722759af fix(core): update 2020-10-06 16:37:25 +00:00
8 changed files with 20043 additions and 4020 deletions

23841
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.49",
"version": "1.0.63",
"private": false,
"description": "make typed requests towards apis",
"main": "dist_ts/index.js",
@ -13,22 +13,22 @@
"format": "(gitzone format)"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.1.24",
"@gitzone/tsbundle": "^1.0.72",
"@gitzone/tstest": "^1.0.43",
"@pushrocks/smartexpress": "^3.0.73",
"@pushrocks/tapbundle": "^3.2.9",
"@types/node": "^14.0.26",
"tslint": "^6.1.2",
"@gitzone/tsbuild": "^2.1.28",
"@gitzone/tsbundle": "^1.0.88",
"@gitzone/tstest": "^1.0.60",
"@pushrocks/smartexpress": "^3.0.108",
"@pushrocks/tapbundle": "^3.2.14",
"@types/node": "^16.11.6",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0"
},
"dependencies": {
"@apiglobal/typedrequest-interfaces": "^1.0.15",
"@pushrocks/isounique": "^1.0.4",
"@pushrocks/lik": "^4.0.17",
"@pushrocks/smartdelay": "^2.0.10",
"@pushrocks/smartpromise": "^3.0.6",
"@pushrocks/webrequest": "^2.0.10"
"@pushrocks/lik": "^5.0.0",
"@pushrocks/smartdelay": "^2.0.13",
"@pushrocks/smartpromise": "^3.1.6",
"@pushrocks/webrequest": "^2.0.13"
},
"files": [
"ts/**/*",

39
test/test.browser.ts Normal file
View File

@ -0,0 +1,39 @@
import { expect, tap } from '@pushrocks/tapbundle';
import * as typedrequest from '../ts/index';
let testTypedHandler: typedrequest.TypedHandler<ITestReqRes>;
// lets define an interface
interface ITestReqRes {
method: 'hi';
request: {
name: string;
};
response: {
surname: string;
};
}
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',
};
});
});
tap.test('should define a testHandler', async () => {
const testTypedRouter = new typedrequest.TypedRouter(); // typed routers can broker typedrequests between handlers
testTypedRouter.addTypedHandler(testTypedHandler);
});
tap.test('should fire a request', async () => {
const typedRequest = new typedrequest.TypedRequest<ITestReqRes>(
'http://localhost:3000/testroute',
'hi'
);
});
tap.start();

View File

@ -2,3 +2,4 @@ export * from './typedrequest.classes.typedrequest';
export * from './typedrequest.classes.typedhandler';
export * from './typedrequest.classes.typedrouter';
export * from './typedrequest.classes.typedresponseerror';
export * from './typedrequest.classes.typedtarget';

View File

@ -1,7 +1,7 @@
import * as plugins from './typedrequest.plugins';
import { TypedResponseError } from './typedrequest.classes.typedresponseerror';
type THandlerFunction<T extends plugins.typedRequestInterfaces.ITypedRequest> = (
export type THandlerFunction<T extends plugins.typedRequestInterfaces.ITypedRequest> = (
requestArg: T['request']
) => Promise<T['response']>;

View File

@ -1,21 +1,12 @@
import * as plugins from './typedrequest.plugins';
import { TypedResponseError } from './typedrequest.classes.typedresponseerror';
import { TypedRouter } from './typedrequest.classes.typedrouter';
import { TypedTarget } from './typedrequest.classes.typedtarget';
export type IPostMethod = (
typedRequestPostObject: plugins.typedRequestInterfaces.ITypedRequest
) => Promise<plugins.typedRequestInterfaces.ITypedRequest>;
export type IPostMethodWithTypedRouter = (
typedRequestPostObject: plugins.typedRequestInterfaces.ITypedRequest
) => Promise<void> | Promise<plugins.typedRequestInterfaces.ITypedRequest>;
const webrequestInstance = new plugins.webrequest.WebRequest();
export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest> {
/**
* this typedrouter allows us to have easy async request response cycles
*/
public typedRouterRef: TypedRouter;
public webrequest = new plugins.webrequest.WebRequest();
/**
* in case we post against a url endpoint
@ -23,30 +14,24 @@ export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest
public urlEndPoint?: string;
/**
* in case we post with some other method, ec ipc communication
* in case we post against a TypedTarget
*/
public postMethod?: IPostMethod | IPostMethodWithTypedRouter;
typedTarget: TypedTarget;
public method: string;
// STATIC
constructor(postEndPointArg: string | IPostMethod, methodArg: T['method']);
constructor(
postEndPointArg: string | IPostMethodWithTypedRouter,
methodArg: T['method'],
typedrouterRefArg: TypedRouter
);
constructor(
postEndPointArg: string | IPostMethodWithTypedRouter,
methodArg: T['method'],
typedrouterRefArg?: TypedRouter
) {
if (typeof postEndPointArg === 'string') {
this.urlEndPoint = postEndPointArg;
/**
* note the overloading is thought to deal with promises
* @param postEndPointArg
* @param methodArg
*/
constructor(postTarget: string | TypedTarget, methodArg: T['method']) {
if (typeof postTarget === 'string') {
this.urlEndPoint = postTarget;
} else {
this.postMethod = postEndPointArg;
this.typedTarget = postTarget;
}
this.method = methodArg;
this.typedRouterRef = typedrouterRefArg;
}
/**
@ -65,28 +50,10 @@ export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest
let responseBody: plugins.typedRequestInterfaces.ITypedRequest;
if (this.urlEndPoint) {
const response = await this.webrequest.postJson(this.urlEndPoint, payload);
const response = await webrequestInstance.postJson(this.urlEndPoint, payload);
responseBody = response;
} else {
let responseInterest: plugins.lik.Interest<
string,
plugins.typedRequestInterfaces.ITypedRequest
>;
// having a typedrouter allows us to work with async request response cycles.
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;
}
responseBody = await this.typedTarget.post(payload);
}
if (responseBody.error) {
console.error(

View File

@ -14,7 +14,7 @@ export class TypedRouter {
public routerMap = new plugins.lik.ObjectMap<TypedRouter>();
public handlerMap = new plugins.lik.ObjectMap<
TypedHandler<plugins.typedRequestInterfaces.ITypedRequest>
TypedHandler<any & plugins.typedRequestInterfaces.ITypedRequest>
>();
public fireEventInterestMap = new plugins.lik.InterestMap<
@ -70,7 +70,7 @@ export class TypedRouter {
if (this.upstreamTypedRouter && checkUpstreamRouter) {
typedHandler = this.upstreamTypedRouter.getTypedHandlerForMethod(methodArg);
} else {
typedHandler = this.handlerMap.find((handler) => {
typedHandler = this.handlerMap.findSync((handler) => {
return handler.method === methodArg;
});
@ -91,7 +91,7 @@ export class TypedRouter {
* if typedrequest object has correlation.phase === 'response' -> routes a typed request object to request fire event
* @param typedRequestArg
*/
public async routeAndAddResponse(typedRequestArg: plugins.typedRequestInterfaces.ITypedRequest) {
public async routeAndAddResponse<T extends plugins.typedRequestInterfaces.ITypedRequest = any>(typedRequestArg: T): Promise<T> {
if (!typedRequestArg?.correlation?.phase || typedRequestArg.correlation.phase === 'request') {
const typedHandler = this.getTypedHandlerForMethod(typedRequestArg.method);
@ -109,6 +109,10 @@ export class TypedRouter {
this.fireEventInterestMap
.findInterest(typedRequestArg.correlation.id)
?.fullfillInterest(typedRequestArg);
} else {
console.log('received weirdly shaped request');
console.log(typedRequestArg);
return null
}
return typedRequestArg;
}

View File

@ -0,0 +1,79 @@
import { TypedRouter } from './typedrequest.classes.typedrouter';
import * as plugins from './typedrequest.plugins';
export type IPostMethod = (
typedRequestPostObject: plugins.typedRequestInterfaces.ITypedRequest
) => Promise<plugins.typedRequestInterfaces.ITypedRequest>;
/**
* this is an alternative to a post url supplied in `new Typedrequest(new TypedTarget(...), 'someMethodName')`
* enables the use of custom post functions
* used for things like broadcast channels
* e.g. @designestate/dees-comms
* the main difference here is, that the response comes back async and is routed by interest through typedrouter
*/
export type IPostMethodWithTypedRouter = (
typedRequestPostObject: plugins.typedRequestInterfaces.ITypedRequest
) => Promise<void> | Promise<plugins.typedRequestInterfaces.ITypedRequest>;
export interface ITypedTargetConstructorOptions {
url?: string;
postMethod?: IPostMethod;
/**
* a post method that does not return the answer
*/
postMethodWithTypedRouter?: IPostMethodWithTypedRouter;
/**
* this typedrouter allows us to have easy async request response cycles
*/
typedRouterRef?: TypedRouter;
}
/**
* a typed target defines a target for requests
*/
export class TypedTarget {
url: string;
type: 'rest' | 'socket';
options: ITypedTargetConstructorOptions;
constructor(optionsArg: ITypedTargetConstructorOptions) {
if (optionsArg.postMethodWithTypedRouter && !optionsArg.typedRouterRef) {
throw new Error('you have to specify a typedrouter when using postmethod with typedrouter');
}
this.options = optionsArg;
}
/**
* wether calls to this target are bound to the request/response cycle
* if false, always delivers response as result of a call
* if true, delivers response in a separate call
* can only be async when type is 'socket'
*/
public isAsync: boolean;
public async post<T extends plugins.typedRequestInterfaces.ITypedRequest>(payloadArg: T): Promise<T> {
let responseInterest: plugins.lik.Interest<
string,
plugins.typedRequestInterfaces.ITypedRequest
>;
// having a typedrouter allows us to work with async request response cycles.
if (this.options.typedRouterRef) {
responseInterest = await this.options.typedRouterRef.fireEventInterestMap.addInterest(
payloadArg.correlation.id,
payloadArg
);
}
const postMethod = this.options.postMethod || this.options.postMethodWithTypedRouter;
const postMethodReturnValue = await postMethod(payloadArg);
let responseBody: T;
if (responseInterest) {
responseBody = (await responseInterest.interestFullfilled) as T;
} else if (postMethodReturnValue) {
responseBody = postMethodReturnValue as T;
} else {
responseBody = payloadArg;
}
return responseBody;
}
}