ts-typed-api 0.2.7 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -24,6 +24,6 @@ export type ObjectHandlers<TDef extends ApiDefinitionSchema, Ctx extends Record<
|
|
|
24
24
|
};
|
|
25
25
|
};
|
|
26
26
|
export declare function RegisterHandlers<TDef extends ApiDefinitionSchema, Ctx extends Record<string, any> = Record<string, any>>(app: express.Express, apiDefinition: TDef, objectHandlers: ObjectHandlers<TDef, Ctx>, middlewares?: AnyMiddleware<TDef>[]): void;
|
|
27
|
-
export declare function createTypedHandler<TDef extends ApiDefinitionSchema, TDomain extends keyof TDef['endpoints'], TRouteKey extends keyof TDef['endpoints'][TDomain]>(handler: (req: TypedRequest<TDef, TDomain, TRouteKey>, res: TypedResponse<TDef, TDomain, TRouteKey>) => Promise<void> | void): (req: TypedRequest<TDef, TDomain, TRouteKey>, res: TypedResponse<TDef, TDomain, TRouteKey>) => Promise<void> | void;
|
|
27
|
+
export declare function createTypedHandler<TDef extends ApiDefinitionSchema, TDomain extends keyof TDef['endpoints'], TRouteKey extends keyof TDef['endpoints'][TDomain], Ctx extends Record<string, any> = Record<string, any>>(handler: (req: TypedRequest<TDef, TDomain, TRouteKey, any, any, any, any, Ctx>, res: TypedResponse<TDef, TDomain, TRouteKey>) => Promise<void> | void): (req: TypedRequest<TDef, TDomain, TRouteKey, any, any, any, any, Ctx>, res: TypedResponse<TDef, TDomain, TRouteKey>) => Promise<void> | void;
|
|
28
28
|
export declare function makeObjectHandlerRegistrar<TDef extends ApiDefinitionSchema>(apiDefinition: TDef): (app: express.Express, objectHandlers: ObjectHandlers<TDef>, middlewares?: EndpointMiddleware<TDef>[]) => void;
|
|
29
29
|
export {};
|
|
@@ -2,7 +2,7 @@ import { Hono } from 'hono';
|
|
|
2
2
|
import http from 'http';
|
|
3
3
|
import { PublicApiDefinition as SimplePublicApiDefinition, PrivateApiDefinition as SimplePrivateApiDefinition } from './simple/definitions';
|
|
4
4
|
import { CreateTypedHonoHandlerWithContext, RegisterHonoHandlers } from '../src';
|
|
5
|
-
import { EndpointMiddlewareCtx } from '../src/object-handlers';
|
|
5
|
+
import { createTypedHandler, EndpointMiddlewareCtx } from '../src/object-handlers';
|
|
6
6
|
|
|
7
7
|
const HONO_PORT = 3004;
|
|
8
8
|
|
|
@@ -16,6 +16,7 @@ async function startHonoServer() {
|
|
|
16
16
|
type Ctx = { foo: string, blah: () => string }
|
|
17
17
|
|
|
18
18
|
const loggingMiddleware: EndpointMiddlewareCtx<Ctx> = (req, res, next, endpointInfo) => {
|
|
19
|
+
req.ctx = { foo: 'foo', blah: () => { return '' } }
|
|
19
20
|
console.log(`[${new Date().toISOString()}] ${req.method} ${req.path} - Endpoint: ${endpointInfo.domain}.${endpointInfo.routeKey}`);
|
|
20
21
|
next();
|
|
21
22
|
};
|
|
@@ -29,13 +30,13 @@ async function startHonoServer() {
|
|
|
29
30
|
}
|
|
30
31
|
},
|
|
31
32
|
status: {
|
|
32
|
-
probe1: async (req, res) => {
|
|
33
|
-
console.log('Handling probe1 request, query:', req.query);
|
|
33
|
+
probe1: createTypedHandler(async (req, res) => {
|
|
34
|
+
console.log('Handling probe1 request, query:', req.query, req.ctx!.foo);
|
|
34
35
|
if (req.query.match) {
|
|
35
36
|
return res.respond(201, { status: true });
|
|
36
37
|
}
|
|
37
38
|
res.respond(200, "pong");
|
|
38
|
-
},
|
|
39
|
+
}),
|
|
39
40
|
probe2: async (req, res) => {
|
|
40
41
|
console.log('Handling probe2 request');
|
|
41
42
|
res.respond(200, "pong");
|
package/package.json
CHANGED
package/src/object-handlers.ts
CHANGED
|
@@ -140,10 +140,11 @@ export function RegisterHandlers<
|
|
|
140
140
|
export function createTypedHandler<
|
|
141
141
|
TDef extends ApiDefinitionSchema,
|
|
142
142
|
TDomain extends keyof TDef['endpoints'],
|
|
143
|
-
TRouteKey extends keyof TDef['endpoints'][TDomain]
|
|
143
|
+
TRouteKey extends keyof TDef['endpoints'][TDomain],
|
|
144
|
+
Ctx extends Record<string, any> = Record<string, any>
|
|
144
145
|
>(
|
|
145
146
|
handler: (
|
|
146
|
-
req: TypedRequest<TDef, TDomain, TRouteKey>,
|
|
147
|
+
req: TypedRequest<TDef, TDomain, TRouteKey, any, any, any, any, Ctx>,
|
|
147
148
|
res: TypedResponse<TDef, TDomain, TRouteKey>
|
|
148
149
|
) => Promise<void> | void
|
|
149
150
|
) {
|
package/tests/setup.ts
CHANGED
|
@@ -299,10 +299,11 @@ beforeAll(async () => {
|
|
|
299
299
|
await startSimpleServer();
|
|
300
300
|
await startAdvancedServer();
|
|
301
301
|
await startFileUploadServer();
|
|
302
|
+
await startMiddlewareExpressServer();
|
|
303
|
+
|
|
302
304
|
await startHonoServer();
|
|
303
305
|
await startAdvancedHonoServer();
|
|
304
306
|
await startFileUploadHonoServer();
|
|
305
|
-
await startMiddlewareExpressServer();
|
|
306
307
|
await startMiddlewareHonoServer();
|
|
307
308
|
});
|
|
308
309
|
|