feat(typedrouter): add middleware support to TypedRouter and export middleware type

This commit is contained in:
2026-03-03 20:22:01 +00:00
parent a1f5916faf
commit ee820dd126
11 changed files with 1187 additions and 453 deletions

View File

@@ -4,7 +4,7 @@ import * as typedserver from '@api.global/typedserver';
import * as typedrequest from '../ts/index.js';
import * as typedrequestInterfaces from '@api.global/typedrequest-interfaces';
let testServer: typedserver.servertools.Server;
let testServer: typedserver.TypedServer;
let testTypedRouter: typedrequest.TypedRouter;
let testTypedHandler: typedrequest.TypedHandler<ITestReqRes>;
@@ -39,7 +39,7 @@ tap.test('should create a typedHandler', async () => {
});
tap.test('should spawn a server to test with', async () => {
testServer = new typedserver.servertools.Server({
testServer = new typedserver.TypedServer({
cors: true,
forceSsl: false,
port: 3000,
@@ -47,12 +47,8 @@ tap.test('should spawn a server to test with', async () => {
});
tap.test('should define a testHandler', async () => {
testTypedRouter = new typedrequest.TypedRouter(); // typed routers can broker typedrequests between handlers
testTypedRouter = testServer.typedrouter;
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 () => {
@@ -61,7 +57,7 @@ tap.test('should start the server', async () => {
tap.test('should fire a request', async () => {
const typedRequest = new typedrequest.TypedRequest<ITestReqRes>(
'http://localhost:3000/testroute',
'http://localhost:3000/typedrequest',
'hi'
);
const response = await typedRequest.fire({
@@ -86,7 +82,7 @@ tap.test('should allow VirtualStreams', async () => {
};
}));
const typedRequest = new typedrequest.TypedRequest<ITestStream>(
'http://localhost:3000/testroute',
'http://localhost:3000/typedrequest',
'handleStream'
);
const response = await typedRequest.fire({
@@ -95,8 +91,14 @@ tap.test('should allow VirtualStreams', async () => {
console.log(response.responseStream);
newRequestingVS.sendData(Buffer.from('hello'));
const data = await generatedRequestingVS.fetchData();
const decodedData = new TextDecoder().decode(data);
const data: any = await generatedRequestingVS.fetchData();
// Data may arrive as Uint8Array or as JSON-serialized Buffer {type: "Buffer", data: [...]}
const resolvedData = data instanceof Uint8Array || Buffer.isBuffer(data)
? data
: data?.type === 'Buffer' && Array.isArray(data.data)
? new Uint8Array(data.data)
: data;
const decodedData = new TextDecoder().decode(resolvedData);
expect(decodedData).toEqual('hello');
await newRequestingVS.close();
await newRespondingVS.close();