Compare commits

...

130 Commits

Author SHA1 Message Date
83b43f501d 3.0.4 2023-12-08 19:36:25 +01:00
9b626a562d fix(core): update 2023-12-08 19:36:24 +01:00
c216f97bfd 3.0.3 2023-12-05 23:42:44 +01:00
71453877d7 fix(core): update 2023-12-05 23:42:43 +01:00
d1a601d006 3.0.2 2023-10-20 17:39:09 +02:00
0a9236a605 fix(core): update 2023-10-20 17:39:08 +02:00
3a384307ee 3.0.1 2023-08-04 16:55:25 +02:00
c215356b31 fix(core): update 2023-08-04 16:55:24 +02:00
96f37dd470 3.0.0 2023-08-04 16:10:48 +02:00
10fac39d30 BREAKING CHANGE(core): update 2023-08-04 16:10:47 +02:00
912572cba5 2.0.13 2022-10-26 14:23:24 +02:00
b001ebaab8 fix(core): update 2022-10-26 14:23:24 +02:00
aa15da6b92 2.0.12 2022-10-09 18:57:31 +02:00
f144f27daa fix(core): update 2022-10-09 18:57:31 +02:00
a58c9a0541 2.0.11 2022-10-09 18:52:50 +02:00
649db1059c fix(core): update 2022-10-09 18:52:49 +02:00
e0c75716d7 2.0.10 2022-08-19 17:59:47 +02:00
009985c226 fix(core): update 2022-08-19 17:59:47 +02:00
f3f2f8e3bf 2.0.9 2022-08-18 23:24:04 +02:00
cf304ceccd fix(core): update 2022-08-18 23:24:04 +02:00
6f075132c4 2.0.8 2022-07-29 04:48:34 +02:00
4dca98e81d fix(core): update 2022-07-29 04:48:34 +02:00
1e022d6c68 2.0.7 2022-05-29 21:43:37 +02:00
f20d737ecf fix(core): update 2022-05-29 21:43:37 +02:00
d791eca5e8 2.0.6 2022-05-29 21:01:57 +02:00
63c6dac8fa fix(core): update 2022-05-29 21:01:56 +02:00
c2c1dee427 2.0.5 2022-05-29 20:43:30 +02:00
4ae90a5cf6 fix(core): update 2022-05-29 20:43:29 +02:00
803d4d2894 2.0.4 2022-05-29 20:38:47 +02:00
fcc75af1ff fix(core): update 2022-05-29 20:38:46 +02:00
0e3bb07a69 2.0.3 2022-03-29 14:50:40 +02:00
c90aa07ace fix(core): update 2022-03-29 14:50:39 +02:00
362f3f1bd0 2.0.2 2022-03-29 10:50:27 +02:00
12f7348fec fix(core): update 2022-03-29 10:50:26 +02:00
7d478c400e 2.0.1 2022-03-29 09:48:47 +02:00
ab75cf8720 fix(core): update 2022-03-29 09:48:47 +02:00
f7ef8a6828 2.0.0 2022-03-24 21:56:03 +01:00
ece2803d12 1.0.66 2022-03-24 20:02:59 +01:00
2384fc1b76 fix(core): update 2022-03-24 20:02:58 +01:00
309c282379 1.0.65 2021-11-10 13:14:41 +01:00
90c616ca41 fix(core): update 2021-11-10 13:14:40 +01:00
57177074d0 1.0.64 2021-11-10 01:41:55 +01:00
d3b5c802cd fix(core): update 2021-11-10 01:41:55 +01:00
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
e9fad241ee 1.0.49 2020-10-06 15:50:12 +00:00
34face164f fix(core): update 2020-10-06 15:50:12 +00:00
f7bf366962 1.0.48 2020-10-06 15:05:29 +00:00
046059d228 fix(core): update 2020-10-06 15:05:29 +00:00
78e8171a6a 1.0.47 2020-10-06 14:29:50 +00:00
c97a535035 fix(core): update 2020-10-06 14:29:49 +00:00
dcf198787a 1.0.46 2020-09-29 15:51:06 +00:00
a1e0ebd658 fix(core): update 2020-09-29 15:51:05 +00:00
8bf0a71266 1.0.45 2020-09-14 08:28:08 +00:00
e499612ecb fix(core): update 2020-09-14 08:28:07 +00:00
e049899599 1.0.44 2020-09-14 08:22:28 +00:00
37f4d34e7a fix(core): update 2020-09-14 08:22:27 +00:00
ca2e6895ce 1.0.43 2020-07-30 09:27:31 +00:00
ccc5c33656 fix(core): update 2020-07-30 09:27:30 +00:00
546f7f4fc7 1.0.42 2020-07-30 08:46:45 +00:00
8536060ce4 fix(core): update 2020-07-30 08:46:45 +00:00
e57c332d82 1.0.41 2020-07-26 13:48:57 +00:00
df87c6c75e fix(core): update 2020-07-26 13:48:57 +00:00
53a4375545 1.0.40 2020-07-14 16:37:45 +00:00
88022ea14b fix(core): update 2020-07-14 16:37:44 +00:00
2a681644eb 1.0.39 2020-07-14 02:20:15 +00:00
e2708c5bb3 fix(core): update 2020-07-14 02:20:15 +00:00
8c7a71ddce 1.0.38 2020-06-25 23:53:06 +00:00
5acbf420ae fix(core): update 2020-06-25 23:53:05 +00:00
5d673799cc 1.0.37 2020-06-16 22:25:20 +00:00
bb0271e021 fix(core): update 2020-06-16 22:25:20 +00:00
718feb74ae 1.0.36 2020-06-16 22:23:32 +00:00
82aa80d7d9 fix(core): update 2020-06-16 22:23:31 +00:00
67831cd37f 1.0.35 2020-06-16 22:00:39 +00:00
777817b588 fix(core): update 2020-06-16 22:00:38 +00:00
c7dd378eb3 1.0.34 2020-06-16 15:17:47 +00:00
9d9f67c91a fix(core): update 2020-06-16 15:17:46 +00:00
82cc8c29e1 1.0.33 2020-06-16 15:17:24 +00:00
625b1c6871 fix(core): update 2020-06-16 15:17:23 +00:00
e149b5a5f1 1.0.32 2020-06-16 15:03:11 +00:00
2879fb8cee fix(core): update 2020-06-16 15:03:10 +00:00
f98c2cc5e7 1.0.31 2020-06-15 16:52:56 +00:00
567845bc64 fix(core): update 2020-06-15 16:52:56 +00:00
52677ec036 1.0.30 2020-06-15 16:39:49 +00:00
8be8ce109e fix(core): update 2020-06-15 16:39:48 +00:00
6788fbf715 1.0.29 2020-06-15 16:36:54 +00:00
325e568329 fix(core): update 2020-06-15 16:36:53 +00:00
7f74fbd713 1.0.28 2020-02-12 08:29:43 +00:00
5aecb43923 fix(core): update 2020-02-12 08:29:43 +00:00
b2219e89f4 1.0.27 2020-02-11 18:55:08 +00:00
37b0968c18 fix(core): update 2020-02-11 18:55:07 +00:00
25a9bb4ec8 1.0.26 2019-09-01 15:39:57 +02:00
2796498984 fix(core): update 2019-09-01 15:39:56 +02:00
68e16e80f0 1.0.25 2019-09-01 15:39:08 +02:00
d1aa752c8e fix(core): update 2019-09-01 15:39:08 +02:00
e7d8731fca 1.0.24 2019-09-01 14:14:29 +02:00
7c3c7ca90e 1.0.23 2019-09-01 13:17:21 +02:00
db474ebd8a fix(core): update 2019-09-01 13:17:21 +02:00
51219303a9 1.0.22 2019-08-31 23:15:05 +02:00
ccc9303c63 fix(core): update 2019-08-31 23:15:05 +02:00
69b7147ae7 1.0.21 2019-08-31 23:14:47 +02:00
3667946b2f fix(core): update 2019-08-31 23:14:46 +02:00
aef1e6183c 1.0.20 2019-08-31 23:05:37 +02:00
68526888e7 1.0.19 2019-08-31 23:04:37 +02:00
54d4851de1 fix(core): update 2019-08-31 23:04:37 +02:00
21 changed files with 6550 additions and 1869 deletions

4
.gitignore vendored
View File

@ -15,8 +15,6 @@ node_modules/
# builds
dist/
dist_web/
dist_serve/
dist_ts_web/
dist_*/
# custom

View File

@ -1,42 +1,46 @@
# 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
before_script:
- npm install -g @shipzone/npmci
# ====================
# security stage
# ====================
mirror:
stage: security
script:
- npmci git mirror
tags:
- docker
- notpriv
snyk:
auditProductionDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
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
- 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
allow_failure: true
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
@ -45,37 +49,36 @@ 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
- 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
@ -83,35 +86,37 @@ release:
codequality:
stage: metadata
allow_failure: true
only:
- tags
script:
- npmci command npm install -g tslint typescript
- npmci command npm install -g 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 command npm install -g @gitzone/tsdoc
- npmci node install stable
- npmci npm prepare
- npmci npm install
- npmci command tsdoc
- npmci command npm run buildDocs
tags:
- lossless
- docker
- notpriv
only:
@ -119,5 +124,5 @@ pages:
artifacts:
expire_in: 1 week
paths:
- public
- public
allow_failure: true

24
.vscode/launch.json vendored
View File

@ -2,28 +2,10 @@
"version": "0.2.0",
"configurations": [
{
"name": "current file",
"type": "node",
"command": "npm test",
"name": "Run npm test",
"request": "launch",
"args": [
"${relativeFile}"
],
"runtimeArgs": ["-r", "@gitzone/tsrun"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": "test.ts",
"type": "node",
"request": "launch",
"args": [
"test/test.ts"
],
"runtimeArgs": ["-r", "@gitzone/tsrun"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
"type": "node-terminal"
}
]
}

26
.vscode/settings.json vendored Normal file
View 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"]
}
}
}
}
}
}
]
}

View File

@ -1,11 +1,12 @@
{
"gitzone": {
"projectType": "npm",
"module": {
"githost": "gitlab.com",
"gitscope": "apiglobal",
"gitscope": "api.global",
"gitrepo": "typedrequest",
"shortDescription": "make typed requests towards apis",
"npmPackagename": "@apiglobal/typedrequest",
"description": "make typed requests towards apis",
"npmPackagename": "@api.global/typedrequest",
"license": "MIT",
"projectDomain": "api.global"
}

1691
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,40 +1,49 @@
{
"name": "@apiglobal/typedrequest",
"version": "1.0.18",
"name": "@api.global/typedrequest",
"version": "3.0.4",
"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)",
"format": "(gitzone format)"
"build": "(tsbuild --web --allowimplicitany && tsbundle npm)",
"buildDocs": "tsdoc"
},
"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",
"tslint-config-prettier": "^1.18.0"
"@api.global/typedserver": "^3.0.9",
"@git.zone/tsbuild": "^2.1.66",
"@git.zone/tsbundle": "^2.0.8",
"@git.zone/tsrun": "^1.2.44",
"@git.zone/tstest": "^1.0.84",
"@push.rocks/smartenv": "^5.0.12",
"@push.rocks/tapbundle": "^5.0.15",
"@types/node": "^20.10.3"
},
"dependencies": {
"@apiglobal/typedrequest-interfaces": "^1.0.7",
"@pushrocks/lik": "^3.0.11",
"@pushrocks/smartjson": "^3.0.8",
"@pushrocks/smartrequest": "^1.1.23"
"@api.global/typedrequest-interfaces": "^3.0.1",
"@push.rocks/isounique": "^1.0.5",
"@push.rocks/lik": "^6.0.12",
"@push.rocks/smartdelay": "^3.0.5",
"@push.rocks/smartpromise": "^4.0.3",
"@push.rocks/webrequest": "^3.0.34"
},
"files": [
"ts/*",
"ts_web/*",
"dist/*",
"dist_web/*",
"dist_ts_web/*",
"assets/*",
"ts/**/*",
"ts_web/**/*",
"dist/**/*",
"dist_*/**/*",
"dist_ts/**/*",
"dist_ts_web/**/*",
"assets/**/*",
"cli.js",
"npmextra.json",
"readme.md"
],
"browserslist": [
"last 1 chrome versions"
]
}

5924
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -8,16 +8,99 @@ make typed requests towards apis
* [docs (typedoc)](https://apiglobal.gitlab.io/typedrequest/)
## Status for master
[![build status](https://gitlab.com/apiglobal/typedrequest/badges/master/build.svg)](https://gitlab.com/apiglobal/typedrequest/commits/master)
[![coverage report](https://gitlab.com/apiglobal/typedrequest/badges/master/coverage.svg)](https://gitlab.com/apiglobal/typedrequest/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/@apiglobal/typedrequest.svg)](https://www.npmjs.com/package/@apiglobal/typedrequest)
[![Known Vulnerabilities](https://snyk.io/test/npm/@apiglobal/typedrequest/badge.svg)](https://snyk.io/test/npm/@apiglobal/typedrequest)
[![TypeScript](https://img.shields.io/badge/TypeScript->=%203.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-prettier-ff69b4.svg)](https://prettier.io/)
Status Category | Status Badge
-- | --
GitLab Pipelines | [![pipeline status](https://gitlab.com/apiglobal/typedrequest/badges/master/pipeline.svg)](https://lossless.cloud)
GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/apiglobal/typedrequest/badges/master/coverage.svg)](https://lossless.cloud)
npm | [![npm downloads per month](https://badgen.net/npm/dy/@apiglobal/typedrequest)](https://lossless.cloud)
Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/apiglobal/typedrequest)](https://lossless.cloud)
TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud)
node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud)
PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@apiglobal/typedrequest)](https://lossless.cloud)
PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@apiglobal/typedrequest)](https://lossless.cloud)
BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@apiglobal/typedrequest)](https://lossless.cloud)
Platform support | [![Supports Windows 10](https://badgen.net/badge/supports%20Windows%2010/yes/green?icon=windows)](https://lossless.cloud) [![Supports Mac OS X](https://badgen.net/badge/supports%20Mac%20OS%20X/yes/green?icon=apple)](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 | **&copy;** [Lossless GmbH](https://lossless.gmbh)

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

@ -0,0 +1,39 @@
import { expect, tap } from '@push.rocks/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();

View File

@ -1,8 +1,67 @@
import { expect, tap } from '@pushrocks/tapbundle';
import * as typedrequest from '../ts/index';
import { expect, tap } from '@push.rocks/tapbundle';
import * as typedserver from '@api.global/typedserver';
tap.test('first test', async () => {
typedrequest.TypedRequest;
import * as typedrequest from '../ts/index.js';
let testServer: typedserver.servertools.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 typedserver.servertools.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 typedserver.servertools.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
View File

@ -0,0 +1,8 @@
/**
* autocreated commitinfo by @pushrocks/commitinfo
*/
export const commitinfo = {
name: '@api.global/typedrequest',
version: '3.0.4',
description: 'make typed requests towards apis'
}

View File

@ -1,3 +1,5 @@
export * from './typedrequest.classes.typedrequest';
export * from './typedrequest.classes.typedhandler';
export * from './typedrequest.classes.typedrouter';
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';

View File

@ -1,6 +1,9 @@
import * as plugins from './typedrequest.plugins';
import * as plugins from './typedrequest.plugins.js';
import { TypedResponseError } from './typedrequest.classes.typedresponseerror.js';
type THandlerFunction<T extends plugins.typedRequestInterfaces.ITypedRequest> = (requestArg: T['request']) => Promise<T['response']>;
export type THandlerFunction<T extends plugins.typedRequestInterfaces.ITypedRequest> = (
requestArg: T['request']
) => Promise<T['response']>;
/**
* typed handler for dealing with typed requests
@ -20,10 +23,32 @@ 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;
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;
}
}

View File

@ -1,27 +1,75 @@
import * as plugins from './typedrequest.plugins';
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> {
public urlEndPoint: string;
/**
* in case we post against a url endpoint
*/
public urlEndPoint?: string;
/**
* in case we post against a TypedTarget
*/
typedTarget: TypedTarget;
public method: string;
// STATIC
constructor(urlEndPointArg: string, methodArg: T['method']) {
this.urlEndPoint = urlEndPointArg;
/**
* 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;
}
/**
* firest the request
* fires the request
*/
public async fire(fireArg: T['request']): Promise<T['response']> {
const response = await plugins.smartrequest.request(this.urlEndPoint, {
method: 'POST',
requestBody: {
method: this.method,
request: fireArg,
response: null
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 response.body.response;
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;
}
}

View 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;
}
}

View File

@ -1,6 +1,7 @@
import * as plugins from './typedrequest.plugins';
import * as plugins from './typedrequest.plugins.js';
import { TypedHandler } from './typedrequest.classes.typedhandler';
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
@ -8,34 +9,110 @@ import { TypedHandler } from './typedrequest.classes.typedhandler';
* This is thought for reusing the same url endpoint for different methods
*/
export class TypedRouter {
public handlerMap = new plugins.lik.Objectmap<
TypedHandler<plugins.typedRequestInterfaces.ITypedRequest>
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 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) {
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) {
const availableMethods: string[] = [];
this.handlerMap.forEach(async (handler) => {
availableMethods.push(handler.method);
this.routerMap.getArray().forEach((typedRouterArg) => {
if (!typedHandler && !checkedRouters.includes(typedRouterArg)) {
typedHandler = typedRouterArg.getTypedHandlerForMethod(methodArg, checkedRouters);
}
});
console.log(`Cannot find method for ${typedHandler}`);
console.log(`Available methods are:`);
console.log(availableMethods);
}
typedRequest = await typedHandler.addResponse(typedRequest);
return typedRequest;
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, localRequestArg = false): Promise<T> {
if (typedRequestArg?.correlation?.phase === 'request' || localRequestArg) {
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;
}
}
}

View File

@ -0,0 +1,81 @@
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;
}
}

View File

@ -1,11 +1,13 @@
// apiglobal scope
import * as typedRequestInterfaces from '@apiglobal/typedrequest-interfaces';
import * as typedRequestInterfaces from '@api.global/typedrequest-interfaces';
export { typedRequestInterfaces };
// pushrocks scope
import * as lik from '@pushrocks/lik';
import * as smartrequest from '@pushrocks/smartrequest';
import * as smartjson from '@pushrocks/smartjson';
import * as isounique from '@push.rocks/isounique';
import * as lik from '@push.rocks/lik';
import * as smartdelay from '@push.rocks/smartdelay';
import * as smartpromise from '@push.rocks/smartpromise';
import * as webrequest from '@push.rocks/webrequest';
export { lik, smartrequest, smartjson };
export { isounique, lik, smartdelay, smartpromise, webrequest };

10
tsconfig.json Normal file
View File

@ -0,0 +1,10 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"useDefineForClassFields": false,
"target": "ES2022",
"module": "ES2022",
"moduleResolution": "nodenext",
"esModuleInterop": true
}
}

View File

@ -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"
}