ts-typed-api 0.2.7 → 0.2.9

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.
@@ -1,6 +1,9 @@
1
+ import type { ApiDefinitionSchema } from './definition';
2
+ import type { TypedRequest, TypedResponse } from './router';
1
3
  export { CreateApiDefinition, CreateResponses } from './definition';
2
4
  export { z as ZodSchema } from 'zod';
3
- export { EndpointMiddleware, EndpointMiddlewareCtx, createTypedHandler } from './object-handlers';
5
+ export { EndpointMiddleware, EndpointMiddlewareCtx } from './object-handlers';
6
+ 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;
4
7
  export { RegisterHonoHandlers, CreateTypedHonoHandlerWithContext } from './hono-cloudflare-workers';
5
8
  export type { ApiDefinitionSchema, RouteSchema, UnifiedError, FileUploadConfig, HttpSuccessStatusCode, HttpClientErrorStatusCode, HttpServerErrorStatusCode, AllowedInputStatusCode, AllowedResponseStatusCode } from './definition';
6
9
  export type { HonoFile, HonoFileType, HonoTypedContext } from './hono-cloudflare-workers';
package/dist/hono-only.js CHANGED
@@ -1,15 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CreateTypedHonoHandlerWithContext = exports.RegisterHonoHandlers = exports.createTypedHandler = exports.ZodSchema = exports.CreateResponses = exports.CreateApiDefinition = void 0;
4
- // Hono-only exports - for Cloudflare Workers and other Hono environments
5
- // Excludes Express dependencies like multer, busboy, etc.
3
+ exports.CreateTypedHonoHandlerWithContext = exports.RegisterHonoHandlers = exports.ZodSchema = exports.CreateResponses = exports.CreateApiDefinition = void 0;
4
+ exports.createTypedHandler = createTypedHandler;
6
5
  var definition_1 = require("./definition");
7
6
  Object.defineProperty(exports, "CreateApiDefinition", { enumerable: true, get: function () { return definition_1.CreateApiDefinition; } });
8
7
  Object.defineProperty(exports, "CreateResponses", { enumerable: true, get: function () { return definition_1.CreateResponses; } });
9
8
  var zod_1 = require("zod");
10
9
  Object.defineProperty(exports, "ZodSchema", { enumerable: true, get: function () { return zod_1.z; } });
11
- var object_handlers_1 = require("./object-handlers");
12
- Object.defineProperty(exports, "createTypedHandler", { enumerable: true, get: function () { return object_handlers_1.createTypedHandler; } });
10
+ // Helper function to create typed handlers with proper type inference
11
+ // Defined locally to avoid Express dependencies in Hono-only bundle
12
+ function createTypedHandler(handler) {
13
+ return handler;
14
+ }
13
15
  // Hono adapter for Cloudflare Workers
14
16
  var hono_cloudflare_workers_1 = require("./hono-cloudflare-workers");
15
17
  Object.defineProperty(exports, "RegisterHonoHandlers", { enumerable: true, get: function () { return hono_cloudflare_workers_1.RegisterHonoHandlers; } });
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-typed-api",
3
- "version": "0.2.7",
3
+ "version": "0.2.9",
4
4
  "description": "A lightweight, type-safe RPC library for TypeScript with Zod validation",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/hono-only.ts CHANGED
@@ -1,8 +1,27 @@
1
1
  // Hono-only exports - for Cloudflare Workers and other Hono environments
2
2
  // Excludes Express dependencies like multer, busboy, etc.
3
+ import type { ApiDefinitionSchema } from './definition';
4
+ import type { TypedRequest, TypedResponse } from './router';
5
+
3
6
  export { CreateApiDefinition, CreateResponses } from './definition';
4
7
  export { z as ZodSchema } from 'zod';
5
- export { EndpointMiddleware, EndpointMiddlewareCtx, createTypedHandler } from './object-handlers'
8
+ export { EndpointMiddleware, EndpointMiddlewareCtx } from './object-handlers'
9
+
10
+ // Helper function to create typed handlers with proper type inference
11
+ // Defined locally to avoid Express dependencies in Hono-only bundle
12
+ export function createTypedHandler<
13
+ TDef extends ApiDefinitionSchema,
14
+ TDomain extends keyof TDef['endpoints'],
15
+ TRouteKey extends keyof TDef['endpoints'][TDomain],
16
+ Ctx extends Record<string, any> = Record<string, any>
17
+ >(
18
+ handler: (
19
+ req: TypedRequest<TDef, TDomain, TRouteKey, any, any, any, any, Ctx>,
20
+ res: TypedResponse<TDef, TDomain, TRouteKey>
21
+ ) => Promise<void> | void
22
+ ) {
23
+ return handler;
24
+ }
6
25
 
7
26
  // Hono adapter for Cloudflare Workers
8
27
  export { RegisterHonoHandlers, CreateTypedHonoHandlerWithContext } from './hono-cloudflare-workers';
@@ -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