Compare commits
140 Commits
Author | SHA1 | Date | |
---|---|---|---|
6f075132c4 | |||
4dca98e81d | |||
1e022d6c68 | |||
f20d737ecf | |||
d791eca5e8 | |||
63c6dac8fa | |||
c2c1dee427 | |||
4ae90a5cf6 | |||
803d4d2894 | |||
fcc75af1ff | |||
0e3bb07a69 | |||
c90aa07ace | |||
362f3f1bd0 | |||
12f7348fec | |||
7d478c400e | |||
ab75cf8720 | |||
f7ef8a6828 | |||
ece2803d12 | |||
2384fc1b76 | |||
309c282379 | |||
90c616ca41 | |||
57177074d0 | |||
d3b5c802cd | |||
8e64353026 | |||
290746c191 | |||
abefef8d7c | |||
81b042e670 | |||
6e3ee011a9 | |||
9b5ff4b1b5 | |||
556ba6cb30 | |||
7321ac680d | |||
2fd8219849 | |||
ea56e2218f | |||
9a07817914 | |||
9bc83b0d1e | |||
98c638e1ab | |||
575ddd36a0 | |||
52b731ce68 | |||
3f6e81b2aa | |||
adad99f6bf | |||
2771c92e85 | |||
440ea9ff3a | |||
51bb8dfa90 | |||
ce3bfa01b4 | |||
265109fca6 | |||
8bfd4d8866 | |||
785f247027 | |||
3f3f488dc4 | |||
0241eda296 | |||
66722759af | |||
e9fad241ee | |||
34face164f | |||
f7bf366962 | |||
046059d228 | |||
78e8171a6a | |||
c97a535035 | |||
dcf198787a | |||
a1e0ebd658 | |||
8bf0a71266 | |||
e499612ecb | |||
e049899599 | |||
37f4d34e7a | |||
ca2e6895ce | |||
ccc5c33656 | |||
546f7f4fc7 | |||
8536060ce4 | |||
e57c332d82 | |||
df87c6c75e | |||
53a4375545 | |||
88022ea14b | |||
2a681644eb | |||
e2708c5bb3 | |||
8c7a71ddce | |||
5acbf420ae | |||
5d673799cc | |||
bb0271e021 | |||
718feb74ae | |||
82aa80d7d9 | |||
67831cd37f | |||
777817b588 | |||
c7dd378eb3 | |||
9d9f67c91a | |||
82cc8c29e1 | |||
625b1c6871 | |||
e149b5a5f1 | |||
2879fb8cee | |||
f98c2cc5e7 | |||
567845bc64 | |||
52677ec036 | |||
8be8ce109e | |||
6788fbf715 | |||
325e568329 | |||
7f74fbd713 | |||
5aecb43923 | |||
b2219e89f4 | |||
37b0968c18 | |||
25a9bb4ec8 | |||
2796498984 | |||
68e16e80f0 | |||
d1aa752c8e | |||
e7d8731fca | |||
7c3c7ca90e | |||
db474ebd8a | |||
51219303a9 | |||
ccc9303c63 | |||
69b7147ae7 | |||
3667946b2f | |||
aef1e6183c | |||
68526888e7 | |||
54d4851de1 | |||
362d8cb72d | |||
f54dfb5bb5 | |||
e81b248181 | |||
d92f0fb2e2 | |||
b858f9f711 | |||
50cc5fdbe6 | |||
386d848d51 | |||
9db92be274 | |||
4a06bedf3b | |||
9787adf3f0 | |||
280e67b86b | |||
592e941211 | |||
68ec93459b | |||
8a4b3fa47e | |||
ad7c2e97b8 | |||
0ff28152f3 | |||
4718a03d7d | |||
f99f79efb1 | |||
ac653ac082 | |||
a8ee6b733f | |||
f0117a0921 | |||
4af2d3f712 | |||
0d7146d234 | |||
dfeda9b36a | |||
0010aa1ccb | |||
5afae8c158 | |||
6768022374 | |||
27488b00f1 | |||
760243e079 | |||
52dc138f54 |
4
.gitignore
vendored
4
.gitignore
vendored
@ -15,8 +15,6 @@ node_modules/
|
||||
|
||||
# builds
|
||||
dist/
|
||||
dist_web/
|
||||
dist_serve/
|
||||
dist_ts_web/
|
||||
dist_*/
|
||||
|
||||
# custom
|
104
.gitlab-ci.yml
104
.gitlab-ci.yml
@ -3,14 +3,14 @@ image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
|
||||
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
|
||||
@ -18,60 +18,74 @@ stages:
|
||||
mirror:
|
||||
stage: security
|
||||
script:
|
||||
- npmci git mirror
|
||||
- npmci git mirror
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
snyk:
|
||||
auditProductionDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install -g snyk
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command snyk test
|
||||
- npmci command npm install --production --ignore-scripts
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command npm audit --audit-level=high --only=prod --production
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
- docker
|
||||
|
||||
auditDevDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command npm audit --audit-level=high --only=dev
|
||||
tags:
|
||||
- docker
|
||||
allow_failure: true
|
||||
|
||||
# ====================
|
||||
# test stage
|
||||
# ====================
|
||||
|
||||
testLTS:
|
||||
testStable:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install lts
|
||||
- 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
|
||||
- docker
|
||||
|
||||
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
|
||||
- docker
|
||||
|
||||
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
|
||||
@ -79,35 +93,39 @@ release:
|
||||
codequality:
|
||||
stage: metadata
|
||||
allow_failure: true
|
||||
only:
|
||||
- tags
|
||||
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:
|
||||
@ -115,5 +133,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", "wcc"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
{
|
||||
"gitzone": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "gitlab.com",
|
||||
"gitscope": "apiglobal",
|
||||
@ -14,4 +15,4 @@
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
}
|
||||
}
|
||||
}
|
14622
package-lock.json
generated
14622
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
46
package.json
46
package.json
@ -1,24 +1,48 @@
|
||||
{
|
||||
"name": "@apiglobal/typedrequest",
|
||||
"version": "1.0.2",
|
||||
"version": "2.0.8",
|
||||
"private": false,
|
||||
"description": "make typed requests towards apis",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"type": "module",
|
||||
"author": "Lossless GmbH",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"test": "(tstest test/)",
|
||||
"build": "(tsbuild)",
|
||||
"build": "(tsbuild --web --allowimplicitany && tsbundle npm)",
|
||||
"format": "(gitzone format)"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.0.22",
|
||||
"@gitzone/tstest": "^1.0.15",
|
||||
"@pushrocks/tapbundle": "^3.0.7",
|
||||
"@types/node": "^10.11.7",
|
||||
"tslint": "^5.11.0",
|
||||
"tslint-config-prettier": "^1.15.0"
|
||||
"@gitzone/tsbuild": "^2.1.63",
|
||||
"@gitzone/tsbundle": "^2.0.6",
|
||||
"@gitzone/tstest": "^1.0.72",
|
||||
"@pushrocks/smartenv": "^5.0.2",
|
||||
"@pushrocks/smartexpress": "^4.0.5",
|
||||
"@pushrocks/tapbundle": "^5.0.4",
|
||||
"@types/node": "^18.6.2"
|
||||
},
|
||||
"dependencies": {}
|
||||
"dependencies": {
|
||||
"@apiglobal/typedrequest-interfaces": "^1.0.15",
|
||||
"@pushrocks/isounique": "^1.0.5",
|
||||
"@pushrocks/lik": "^6.0.0",
|
||||
"@pushrocks/smartdelay": "^2.0.13",
|
||||
"@pushrocks/smartpromise": "^3.1.7",
|
||||
"@pushrocks/webrequest": "^3.0.9"
|
||||
},
|
||||
"files": [
|
||||
"ts/**/*",
|
||||
"ts_web/**/*",
|
||||
"dist/**/*",
|
||||
"dist_*/**/*",
|
||||
"dist_ts/**/*",
|
||||
"dist_ts_web/**/*",
|
||||
"assets/**/*",
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
],
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
]
|
||||
}
|
||||
|
109
readme.md
Normal file
109
readme.md
Normal file
@ -0,0 +1,109 @@
|
||||
# @apiglobal/typedrequest
|
||||
make typed requests towards apis
|
||||
|
||||
## Availabililty and Links
|
||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@apiglobal/typedrequest)
|
||||
* [gitlab.com (source)](https://gitlab.com/apiglobal/typedrequest)
|
||||
* [github.com (source mirror)](https://github.com/apiglobal/typedrequest)
|
||||
* [docs (typedoc)](https://apiglobal.gitlab.io/typedrequest/)
|
||||
|
||||
## Status for master
|
||||
|
||||
Status Category | Status Badge
|
||||
-- | --
|
||||
GitLab Pipelines | [](https://lossless.cloud)
|
||||
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||
npm | [](https://lossless.cloud)
|
||||
Snyk | [](https://lossless.cloud)
|
||||
TypeScript Support | [](https://lossless.cloud)
|
||||
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
Code Style | [](https://lossless.cloud)
|
||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||
Platform support | [](https://lossless.cloud) [](https://lossless.cloud)
|
||||
|
||||
## Usage
|
||||
|
||||
Use TypeScript for best in class intellisense.
|
||||
|
||||
```typescript
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as smartexpress from '@pushrocks/smartexpress';
|
||||
|
||||
import * as typedrequest from '../ts/index';
|
||||
|
||||
let testServer: smartexpress.Server;
|
||||
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 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). :)
|
||||
|
||||
For further information read the linked docs at the top of this readme.
|
||||
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||
|
||||
[](https://maintainedby.lossless.com)
|
39
test/test.browser.ts
Normal file
39
test/test.browser.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
|
||||
import * as typedrequest from '../ts/index.js';
|
||||
|
||||
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();
|
69
test/test.ts
69
test/test.ts
@ -1,8 +1,67 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as typedrequest from '../ts/index'
|
||||
import * as smartexpress from '@pushrocks/smartexpress';
|
||||
|
||||
tap.test('first test', async () => {
|
||||
console.log(typedrequest.standardExport)
|
||||
})
|
||||
import * as typedrequest from '../ts/index.js';
|
||||
|
||||
tap.start()
|
||||
let testServer: smartexpress.Server;
|
||||
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 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).toEqual('wow');
|
||||
});
|
||||
|
||||
tap.test('should end the server', async () => {
|
||||
await testServer.stop();
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@apiglobal/typedrequest',
|
||||
version: '2.0.8',
|
||||
description: 'make typed requests towards apis'
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
import * as plugins from './typedrequest.plugins';
|
||||
|
||||
export let standardExport = 'Hi there! :) This is an exported string';
|
||||
export * from './typedrequest.classes.typedrequest.js';
|
||||
export * from './typedrequest.classes.typedhandler.js';
|
||||
export * from './typedrequest.classes.typedrouter.js';
|
||||
export * from './typedrequest.classes.typedresponseerror.js';
|
||||
export * from './typedrequest.classes.typedtarget.js';
|
||||
|
54
ts/typedrequest.classes.typedhandler.ts
Normal file
54
ts/typedrequest.classes.typedhandler.ts
Normal file
@ -0,0 +1,54 @@
|
||||
import * as plugins from './typedrequest.plugins.js';
|
||||
import { TypedResponseError } from './typedrequest.classes.typedresponseerror.js';
|
||||
|
||||
export 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'
|
||||
);
|
||||
}
|
||||
let typedResponseError: TypedResponseError;
|
||||
const response = await this.handlerFunction(typedRequestArg.request).catch((e) => {
|
||||
if (e instanceof TypedResponseError) {
|
||||
typedResponseError = e;
|
||||
} else {
|
||||
console.log(e);
|
||||
}
|
||||
});
|
||||
|
||||
if (typedResponseError) {
|
||||
typedRequestArg.error = {
|
||||
text: typedResponseError.errorText,
|
||||
data: typedResponseError.errorData,
|
||||
};
|
||||
}
|
||||
|
||||
if (response) {
|
||||
typedRequestArg.response = response;
|
||||
}
|
||||
|
||||
typedRequestArg?.correlation?.phase ? (typedRequestArg.correlation.phase = 'response') : null;
|
||||
|
||||
return typedRequestArg;
|
||||
}
|
||||
}
|
77
ts/typedrequest.classes.typedrequest.ts
Normal file
77
ts/typedrequest.classes.typedrequest.ts
Normal file
@ -0,0 +1,77 @@
|
||||
import * as plugins from './typedrequest.plugins.js';
|
||||
import { TypedResponseError } from './typedrequest.classes.typedresponseerror.js';
|
||||
import { TypedRouter } from './typedrequest.classes.typedrouter.js';
|
||||
import { TypedTarget } from './typedrequest.classes.typedtarget.js';
|
||||
|
||||
const webrequestInstance = new plugins.webrequest.WebRequest();
|
||||
|
||||
export class TypedRequest<T extends plugins.typedRequestInterfaces.ITypedRequest> {
|
||||
|
||||
|
||||
/**
|
||||
* in case we post against a url endpoint
|
||||
*/
|
||||
public urlEndPoint?: string;
|
||||
|
||||
/**
|
||||
* in case we post against a TypedTarget
|
||||
*/
|
||||
typedTarget: TypedTarget;
|
||||
|
||||
public method: string;
|
||||
|
||||
/**
|
||||
* 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.typedTarget = postTarget;
|
||||
}
|
||||
this.method = methodArg;
|
||||
}
|
||||
|
||||
/**
|
||||
* fires the request
|
||||
*/
|
||||
public async fire(fireArg: T['request'], useCacheArg: boolean = false): Promise<T['response']> {
|
||||
const payload: plugins.typedRequestInterfaces.ITypedRequest = {
|
||||
method: this.method,
|
||||
request: fireArg,
|
||||
response: null,
|
||||
correlation: {
|
||||
id: plugins.isounique.uni(),
|
||||
phase: 'request',
|
||||
},
|
||||
};
|
||||
|
||||
let responseBody: plugins.typedRequestInterfaces.ITypedRequest;
|
||||
if (this.urlEndPoint) {
|
||||
const response = await webrequestInstance.postJson(this.urlEndPoint, payload, useCacheArg);
|
||||
responseBody = response;
|
||||
} else {
|
||||
responseBody = await this.typedTarget.post(payload);
|
||||
}
|
||||
if (responseBody.error) {
|
||||
console.error(
|
||||
`Got an error ${responseBody.error.text} with data ${JSON.stringify(
|
||||
responseBody.error.data
|
||||
)}`
|
||||
);
|
||||
if (!responseBody.retry) {
|
||||
throw new TypedResponseError(responseBody.error.text, 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;
|
||||
}
|
||||
}
|
10
ts/typedrequest.classes.typedresponseerror.ts
Normal file
10
ts/typedrequest.classes.typedresponseerror.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import * as plugins from './typedrequest.plugins.js';
|
||||
|
||||
export class TypedResponseError {
|
||||
public errorText: string;
|
||||
public errorData: any;
|
||||
constructor(errorTextArg: string, errorDataArg?: any) {
|
||||
this.errorText = errorTextArg;
|
||||
this.errorData = errorDataArg;
|
||||
}
|
||||
}
|
116
ts/typedrequest.classes.typedrouter.ts
Normal file
116
ts/typedrequest.classes.typedrouter.ts
Normal file
@ -0,0 +1,116 @@
|
||||
import * as plugins from './typedrequest.plugins.js';
|
||||
|
||||
import { TypedHandler } from './typedrequest.classes.typedhandler.js';
|
||||
import { TypedRequest } from './typedrequest.classes.typedrequest.js';
|
||||
|
||||
/**
|
||||
* 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 routerMap = new plugins.lik.ObjectMap<TypedRouter>();
|
||||
public handlerMap = new plugins.lik.ObjectMap<
|
||||
TypedHandler<any & plugins.typedRequestInterfaces.ITypedRequest>
|
||||
>();
|
||||
|
||||
public fireEventInterestMap = new plugins.lik.InterestMap<
|
||||
string,
|
||||
plugins.typedRequestInterfaces.ITypedRequest
|
||||
>((correlationId: string) => correlationId);
|
||||
|
||||
/**
|
||||
* adds the handler to the routing map
|
||||
* @param typedHandlerArg
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* adds another sub typedRouter
|
||||
* @param typedRequest
|
||||
*/
|
||||
public addTypedRouter (typedRouterArg: TypedRouter) {
|
||||
const routerExists = this.routerMap.findSync(routerArg => routerArg === typedRouterArg)
|
||||
if (!routerExists) {
|
||||
this.routerMap.add(typedRouterArg);
|
||||
typedRouterArg.addTypedRouter(this);
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
checkedRouters: TypedRouter[] = []
|
||||
): TypedHandler<any> {
|
||||
checkedRouters.push(this);
|
||||
|
||||
let typedHandler: TypedHandler<any>;
|
||||
|
||||
typedHandler = this.handlerMap.findSync((handler) => {
|
||||
return handler.method === methodArg;
|
||||
});
|
||||
|
||||
if (!typedHandler) {
|
||||
this.routerMap.getArray().forEach((typedRouterArg) => {
|
||||
if (!typedHandler && !checkedRouters.includes(typedRouterArg)) {
|
||||
typedHandler = typedRouterArg.getTypedHandlerForMethod(methodArg, checkedRouters);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return typedHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* if typedrequest object has correlation.phase === 'request' -> routes a typed request object to a handler
|
||||
* if typedrequest object has correlation.phase === 'response' -> routes a typed request object to request fire event
|
||||
* @param typedRequestArg
|
||||
*/
|
||||
public async routeAndAddResponse<T extends plugins.typedRequestInterfaces.ITypedRequest = plugins.typedRequestInterfaces.ITypedRequest> (typedRequestArg: T): Promise<T> {
|
||||
if (typedRequestArg?.correlation?.phase === 'request') {
|
||||
const typedHandler = this.getTypedHandlerForMethod(typedRequestArg.method);
|
||||
|
||||
if (!typedHandler) {
|
||||
console.log(`Cannot find handler for methodname ${typedRequestArg.method}`);
|
||||
typedRequestArg.error = {
|
||||
text: 'There is no available method for this call on the server side',
|
||||
data: {},
|
||||
};
|
||||
typedRequestArg.correlation.phase = 'response';
|
||||
return typedRequestArg;
|
||||
}
|
||||
|
||||
typedRequestArg = await typedHandler.addResponse(typedRequestArg);
|
||||
return typedRequestArg;
|
||||
} else if (typedRequestArg?.correlation?.phase === 'response') {
|
||||
this.fireEventInterestMap
|
||||
.findInterest(typedRequestArg.correlation.id)
|
||||
?.fullfillInterest(typedRequestArg);
|
||||
return null;
|
||||
} else {
|
||||
console.log('received weirdly shaped request');
|
||||
console.log(typedRequestArg);
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
79
ts/typedrequest.classes.typedtarget.ts
Normal file
79
ts/typedrequest.classes.typedtarget.ts
Normal file
@ -0,0 +1,79 @@
|
||||
import { TypedRouter } from './typedrequest.classes.typedrouter.js';
|
||||
import * as plugins from './typedrequest.plugins.js';
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,4 +1,13 @@
|
||||
const removeme = {};
|
||||
export {
|
||||
removeme
|
||||
}
|
||||
// apiglobal scope
|
||||
import * as typedRequestInterfaces from '@apiglobal/typedrequest-interfaces';
|
||||
|
||||
export { typedRequestInterfaces };
|
||||
|
||||
// pushrocks scope
|
||||
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, smartpromise, webrequest };
|
||||
|
10
tsconfig.json
Normal file
10
tsconfig.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"experimentalDecorators": true,
|
||||
"useDefineForClassFields": false,
|
||||
"target": "ES2022",
|
||||
"module": "ES2022",
|
||||
"moduleResolution": "nodenext",
|
||||
"esModuleInterop": true
|
||||
}
|
||||
}
|
17
tslint.json
17
tslint.json
@ -1,17 +0,0 @@
|
||||
{
|
||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||
"rules": {
|
||||
"semicolon": [true, "always"],
|
||||
"no-console": false,
|
||||
"ordered-imports": false,
|
||||
"object-literal-sort-keys": false,
|
||||
"member-ordering": {
|
||||
"options":{
|
||||
"order": [
|
||||
"static-method"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"defaultSeverity": "warning"
|
||||
}
|
Reference in New Issue
Block a user