Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
6788fbf715 | |||
325e568329 | |||
7f74fbd713 | |||
5aecb43923 | |||
b2219e89f4 | |||
37b0968c18 | |||
25a9bb4ec8 | |||
2796498984 | |||
68e16e80f0 | |||
d1aa752c8e | |||
e7d8731fca | |||
7c3c7ca90e | |||
db474ebd8a | |||
51219303a9 | |||
ccc9303c63 | |||
69b7147ae7 | |||
3667946b2f | |||
aef1e6183c | |||
68526888e7 | |||
54d4851de1 |
@ -1,18 +1,16 @@
|
||||
# gitzone ci_default
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
variables:
|
||||
GIT_STRATEGY: clone
|
||||
|
||||
cache:
|
||||
paths:
|
||||
- .npmci_cache/
|
||||
key: "$CI_BUILD_STAGE"
|
||||
- .npmci_cache/
|
||||
key: '$CI_BUILD_STAGE'
|
||||
|
||||
stages:
|
||||
- security
|
||||
- test
|
||||
- release
|
||||
- metadata
|
||||
- security
|
||||
- test
|
||||
- release
|
||||
- metadata
|
||||
|
||||
# ====================
|
||||
# security stage
|
||||
@ -20,23 +18,23 @@ stages:
|
||||
mirror:
|
||||
stage: security
|
||||
script:
|
||||
- npmci git mirror
|
||||
- npmci git mirror
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
snyk:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:snyk
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci command npm cache clean --force
|
||||
- npmci command npm install -g snyk
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command snyk test
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
# ====================
|
||||
# test stage
|
||||
@ -45,37 +43,40 @@ snyk:
|
||||
testStable:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- priv
|
||||
- lossless
|
||||
- docker
|
||||
- priv
|
||||
|
||||
testBuild:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install lts
|
||||
- npmci npm install
|
||||
- npmci command npm run build
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci command npm run build
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
release:
|
||||
stage: release
|
||||
script:
|
||||
- npmci node install lts
|
||||
- npmci npm publish
|
||||
- npmci node install stable
|
||||
- npmci npm publish
|
||||
only:
|
||||
- tags
|
||||
- tags
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
# ====================
|
||||
# metadata stage
|
||||
@ -85,33 +86,35 @@ codequality:
|
||||
allow_failure: true
|
||||
script:
|
||||
- npmci command npm install -g tslint typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||
tags:
|
||||
- docker
|
||||
- priv
|
||||
- lossless
|
||||
- docker
|
||||
- priv
|
||||
|
||||
trigger:
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci trigger
|
||||
- npmci trigger
|
||||
only:
|
||||
- tags
|
||||
- tags
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
pages:
|
||||
image: hosttoday/ht-docker-dbase:npmci
|
||||
services:
|
||||
- docker:18-dind
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci node install lts
|
||||
- npmci command npm install -g @gitzone/tsdoc
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command tsdoc
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
only:
|
||||
@ -119,5 +122,5 @@ pages:
|
||||
artifacts:
|
||||
expire_in: 1 week
|
||||
paths:
|
||||
- public
|
||||
- public
|
||||
allow_failure: true
|
||||
|
26
.vscode/settings.json
vendored
Normal file
26
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
{
|
||||
"json.schemas": [
|
||||
{
|
||||
"fileMatch": ["/npmextra.json"],
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"npmci": {
|
||||
"type": "object",
|
||||
"description": "settings for npmci"
|
||||
},
|
||||
"gitzone": {
|
||||
"type": "object",
|
||||
"description": "settings for gitzone",
|
||||
"properties": {
|
||||
"projectType": {
|
||||
"type": "string",
|
||||
"enum": ["website", "element", "service", "npm"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
{
|
||||
"gitzone": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "gitlab.com",
|
||||
"gitscope": "apiglobal",
|
||||
|
1825
package-lock.json
generated
1825
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
34
package.json
34
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@apiglobal/typedrequest",
|
||||
"version": "1.0.18",
|
||||
"version": "1.0.29",
|
||||
"private": false,
|
||||
"description": "make typed requests towards apis",
|
||||
"main": "dist/index.js",
|
||||
@ -13,26 +13,28 @@
|
||||
"format": "(gitzone format)"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.0.22",
|
||||
"@gitzone/tstest": "^1.0.15",
|
||||
"@pushrocks/tapbundle": "^3.0.13",
|
||||
"@types/node": "^12.7.2",
|
||||
"tslint": "^5.19.0",
|
||||
"@gitzone/tsbuild": "^2.1.17",
|
||||
"@gitzone/tstest": "^1.0.28",
|
||||
"@pushrocks/smartexpress": "^3.0.57",
|
||||
"@pushrocks/tapbundle": "^3.2.0",
|
||||
"@types/node": "^13.7.1",
|
||||
"tslint": "^6.0.0",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@apiglobal/typedrequest-interfaces": "^1.0.7",
|
||||
"@pushrocks/lik": "^3.0.11",
|
||||
"@pushrocks/smartjson": "^3.0.8",
|
||||
"@pushrocks/smartrequest": "^1.1.23"
|
||||
"@apiglobal/typedrequest-interfaces": "^1.0.12",
|
||||
"@pushrocks/lik": "^3.0.17",
|
||||
"@pushrocks/smartdelay": "^2.0.6",
|
||||
"@pushrocks/smartjson": "^3.0.10",
|
||||
"@pushrocks/smartrequest": "^1.1.47"
|
||||
},
|
||||
"files": [
|
||||
"ts/*",
|
||||
"ts_web/*",
|
||||
"dist/*",
|
||||
"dist_web/*",
|
||||
"dist_ts_web/*",
|
||||
"assets/*",
|
||||
"ts/**/*",
|
||||
"ts_web/**/*",
|
||||
"dist/**/*",
|
||||
"dist_web/**/*",
|
||||
"dist_ts_web/**/*",
|
||||
"assets/**/*",
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
|
@ -8,7 +8,7 @@ make typed requests towards apis
|
||||
* [docs (typedoc)](https://apiglobal.gitlab.io/typedrequest/)
|
||||
|
||||
## Status for master
|
||||
[](https://gitlab.com/apiglobal/typedrequest/commits/master)
|
||||
[](https://gitlab.com/apiglobal/typedrequest/commits/master)
|
||||
[](https://gitlab.com/apiglobal/typedrequest/commits/master)
|
||||
[](https://www.npmjs.com/package/@apiglobal/typedrequest)
|
||||
[](https://snyk.io/test/npm/@apiglobal/typedrequest)
|
||||
@ -18,6 +18,11 @@ make typed requests towards apis
|
||||
|
||||
## Usage
|
||||
|
||||
|
||||
## 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). :)
|
||||
|
||||
For further information read the linked docs at the top of this readme.
|
||||
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
|
61
test/test.ts
61
test/test.ts
@ -1,8 +1,65 @@
|
||||
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();
|
||||
|
@ -1,6 +1,8 @@
|
||||
import * as plugins from './typedrequest.plugins';
|
||||
|
||||
type THandlerFunction<T extends plugins.typedRequestInterfaces.ITypedRequest> = (requestArg: T['request']) => Promise<T['response']>;
|
||||
type THandlerFunction<T extends plugins.typedRequestInterfaces.ITypedRequest> = (
|
||||
requestArg: T['request']
|
||||
) => Promise<T['response']>;
|
||||
|
||||
/**
|
||||
* typed handler for dealing with typed requests
|
||||
@ -20,7 +22,9 @@ export class TypedHandler<T extends plugins.typedRequestInterfaces.ITypedRequest
|
||||
*/
|
||||
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');
|
||||
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;
|
||||
|
@ -14,14 +14,25 @@ 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',
|
||||
const response = await plugins.smartrequest.postJson(this.urlEndPoint, {
|
||||
requestBody: {
|
||||
method: this.method,
|
||||
request: fireArg,
|
||||
response: null
|
||||
}
|
||||
});
|
||||
return response.body.response;
|
||||
const responseBody: T = response.body;
|
||||
if (responseBody.error) {
|
||||
console.log(responseBody.error.text);
|
||||
console.log(responseBody.error.data);
|
||||
return null;
|
||||
}
|
||||
if (responseBody.retry) {
|
||||
console.log(`server requested retry for the following reason: ${responseBody.retry.reason}`);
|
||||
await plugins.smartdelay.delayFor(responseBody.retry.waitForMs);
|
||||
// tslint:disable-next-line: no-return-await
|
||||
return await this.fire(fireArg);
|
||||
}
|
||||
return responseBody.response;
|
||||
}
|
||||
}
|
||||
|
@ -8,34 +8,99 @@ import { TypedHandler } from './typedrequest.classes.typedhandler';
|
||||
* This is thought for reusing the same url endpoint for different methods
|
||||
*/
|
||||
export class TypedRouter {
|
||||
public upstreamTypedRouter: TypedRouter;
|
||||
|
||||
public routerMap = new plugins.lik.Objectmap<
|
||||
TypedRouter
|
||||
>();
|
||||
|
||||
public handlerMap = new plugins.lik.Objectmap<
|
||||
TypedHandler<plugins.typedRequestInterfaces.ITypedRequest>
|
||||
>();
|
||||
|
||||
/**
|
||||
* adds the handler to the routing map
|
||||
* @param handlerArg
|
||||
* @param typedHandlerArg
|
||||
*/
|
||||
public addTypedHandler<T extends plugins.typedRequestInterfaces.ITypedRequest>(handlerArg: TypedHandler<T>) {
|
||||
this.handlerMap.add(handlerArg);
|
||||
public addTypedHandler<T extends plugins.typedRequestInterfaces.ITypedRequest>(
|
||||
typedHandlerArg: TypedHandler<T>
|
||||
) {
|
||||
// lets check for deduplication
|
||||
const existingTypedHandler = this.getTypedHandlerForMethod(typedHandlerArg.method);
|
||||
if (existingTypedHandler) {
|
||||
throw new Error(`a TypedHandler for ${typedHandlerArg.method} alredy exists! Can't add another one.`);
|
||||
}
|
||||
|
||||
this.handlerMap.add(typedHandlerArg);
|
||||
}
|
||||
|
||||
public async addResponse(typedRequest: plugins.typedRequestInterfaces.ITypedRequest) {
|
||||
const typedHandler = this.handlerMap.find(handler => {
|
||||
return handler.method === typedRequest.method;
|
||||
});
|
||||
/**
|
||||
* adds another sub typedRouter
|
||||
* @param typedRequest
|
||||
*/
|
||||
public addTypedRouter(typedRouterArg: TypedRouter) {
|
||||
this.routerMap.add(typedRouterArg);
|
||||
}
|
||||
|
||||
public setUpstreamTypedRouter(typedRouterArg: TypedRouter) {
|
||||
this.upstreamTypedRouter = typedRouterArg;
|
||||
}
|
||||
|
||||
public checkForTypedHandler(methodArg: string): boolean {
|
||||
return !! this.getTypedHandlerForMethod(methodArg);
|
||||
}
|
||||
|
||||
/**
|
||||
* gets a typed Router from the router chain, upstream and downstream
|
||||
* @param methodArg
|
||||
* @param checkUpstreamRouter
|
||||
*/
|
||||
public getTypedHandlerForMethod(methodArg: string, checkUpstreamRouter = true): TypedHandler<any> {
|
||||
let typedHandler: TypedHandler<any>;
|
||||
|
||||
if (this.upstreamTypedRouter && checkUpstreamRouter) {
|
||||
typedHandler = this.upstreamTypedRouter.getTypedHandlerForMethod(methodArg);
|
||||
} else {
|
||||
typedHandler = this.handlerMap.find(handler => {
|
||||
return handler.method === methodArg;
|
||||
});
|
||||
|
||||
if (!typedHandler) {
|
||||
this.routerMap.getArray().forEach(typedRouter => {
|
||||
if (!typedHandler) {
|
||||
typedHandler = typedRouter.getTypedHandlerForMethod(methodArg, false);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return typedHandler;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* routes a typed request to a handler
|
||||
* @param typedRequestArg
|
||||
*/
|
||||
public async routeAndAddResponse(typedRequestArg: plugins.typedRequestInterfaces.ITypedRequest) {
|
||||
const typedHandler = this.getTypedHandlerForMethod(typedRequestArg.method);
|
||||
|
||||
if (!typedHandler) {
|
||||
const availableMethods: string[] = [];
|
||||
this.handlerMap.forEach(async (handler) => {
|
||||
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);
|
||||
typedRequestArg.error = {
|
||||
text: 'There is no available method for this call on the server side',
|
||||
data: {}
|
||||
};
|
||||
return typedRequestArg;
|
||||
}
|
||||
|
||||
typedRequest = await typedHandler.addResponse(typedRequest);
|
||||
return typedRequest;
|
||||
typedRequestArg = await typedHandler.addResponse(typedRequestArg);
|
||||
return typedRequestArg;
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,8 @@ export { typedRequestInterfaces };
|
||||
|
||||
// pushrocks scope
|
||||
import * as lik from '@pushrocks/lik';
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartrequest from '@pushrocks/smartrequest';
|
||||
import * as smartjson from '@pushrocks/smartjson';
|
||||
|
||||
export { lik, smartrequest, smartjson };
|
||||
export { lik, smartdelay, smartrequest, smartjson };
|
||||
|
Reference in New Issue
Block a user