@veloxts/router 0.1.0 → 0.2.0

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,313 +0,0 @@
1
- /**
2
- * tRPC adapter for procedure collections
3
- *
4
- * Converts VeloxTS procedure definitions into tRPC routers, enabling type-safe
5
- * API calls between frontend and backend.
6
- *
7
- * @module trpc/adapter
8
- */
9
- import { initTRPC, TRPCError } from '@trpc/server';
10
- // ============================================================================
11
- // tRPC Initialization
12
- // ============================================================================
13
- // Create a base tRPC instance for type inference
14
- // Note: No custom errorFormatter here to keep the type simple and portable
15
- const baseTRPC = initTRPC.context().create();
16
- /**
17
- * Create a tRPC instance with VeloxTS context
18
- *
19
- * This initializes tRPC with the BaseContext type, allowing procedures
20
- * to access request context and plugin-provided features.
21
- *
22
- * @returns tRPC instance with context
23
- *
24
- * @example
25
- * ```typescript
26
- * const t = createTRPC();
27
- *
28
- * const router = t.router({
29
- * hello: t.procedure.query(() => 'Hello World'),
30
- * });
31
- * ```
32
- */
33
- export function createTRPC() {
34
- // Create with default options - custom error formatting removed for type compatibility
35
- // with tRPC v11.7+. Use veloxErrorToTRPCError() for custom error handling instead.
36
- return initTRPC.context().create();
37
- }
38
- // ============================================================================
39
- // Router Building
40
- // ============================================================================
41
- /**
42
- * Build a tRPC router from a single procedure collection
43
- *
44
- * Converts all procedures in a collection to tRPC procedures,
45
- * preserving type information for client inference.
46
- *
47
- * @param t - tRPC instance
48
- * @param collection - Procedure collection to convert
49
- * @returns tRPC router
50
- *
51
- * @example
52
- * ```typescript
53
- * const t = createTRPC();
54
- * const userRouter = buildTRPCRouter(t, userProcedures);
55
- *
56
- * // Router has typed procedures:
57
- * // userRouter.getUser({ id: '123' })
58
- * ```
59
- */
60
- export function buildTRPCRouter(t, collection) {
61
- // Build procedures object with explicit types
62
- const routerConfig = {};
63
- for (const [name, procedure] of Object.entries(collection.procedures)) {
64
- routerConfig[name] = buildTRPCProcedure(t, procedure);
65
- }
66
- // Use type assertion for dynamic router creation
67
- // This is safe because we're building from validated procedures
68
- return t.router(routerConfig);
69
- }
70
- /**
71
- * Build a single tRPC procedure from a compiled VeloxTS procedure
72
- *
73
- * @internal
74
- */
75
- function buildTRPCProcedure(t, procedure) {
76
- // Start with base procedure builder
77
- const baseProcedure = t.procedure;
78
- // Build the procedure chain based on configuration
79
- if (procedure.inputSchema && procedure.outputSchema) {
80
- // Both input and output schemas
81
- const withInput = baseProcedure.input(procedure.inputSchema);
82
- const withOutput = withInput.output(procedure.outputSchema);
83
- const handler = createHandler(procedure);
84
- return procedure.type === 'query' ? withOutput.query(handler) : withOutput.mutation(handler);
85
- }
86
- if (procedure.inputSchema) {
87
- // Only input schema
88
- const withInput = baseProcedure.input(procedure.inputSchema);
89
- const handler = createHandler(procedure);
90
- return procedure.type === 'query' ? withInput.query(handler) : withInput.mutation(handler);
91
- }
92
- if (procedure.outputSchema) {
93
- // Only output schema
94
- const withOutput = baseProcedure.output(procedure.outputSchema);
95
- const handler = createNoInputHandler(procedure);
96
- return procedure.type === 'query' ? withOutput.query(handler) : withOutput.mutation(handler);
97
- }
98
- // No schemas - use base procedure
99
- const handler = createNoInputHandler(procedure);
100
- return procedure.type === 'query'
101
- ? baseProcedure.query(handler)
102
- : baseProcedure.mutation(handler);
103
- }
104
- /**
105
- * Create a handler function for a procedure with input
106
- *
107
- * @internal
108
- */
109
- function createHandler(procedure) {
110
- return async (opts) => {
111
- const { input, ctx } = opts;
112
- // Execute middleware chain if any
113
- if (procedure.middlewares.length > 0) {
114
- return executeWithMiddleware(procedure, input, ctx);
115
- }
116
- // Direct handler execution
117
- return procedure.handler({ input, ctx });
118
- };
119
- }
120
- /**
121
- * Create a handler function for a procedure without input
122
- *
123
- * @internal
124
- */
125
- function createNoInputHandler(procedure) {
126
- return async (opts) => {
127
- const { ctx } = opts;
128
- const input = undefined;
129
- // Execute middleware chain if any
130
- if (procedure.middlewares.length > 0) {
131
- return executeWithMiddleware(procedure, input, ctx);
132
- }
133
- // Direct handler execution
134
- return procedure.handler({ input, ctx });
135
- };
136
- }
137
- /**
138
- * Execute procedure with middleware chain
139
- *
140
- * @internal
141
- */
142
- async function executeWithMiddleware(procedure, input, ctx) {
143
- // Build middleware chain from end to start
144
- let next = async () => {
145
- const output = await procedure.handler({ input, ctx });
146
- return { output };
147
- };
148
- // Wrap each middleware from last to first
149
- for (let i = procedure.middlewares.length - 1; i >= 0; i--) {
150
- const middleware = procedure.middlewares[i];
151
- const currentNext = next;
152
- next = async () => {
153
- return middleware({
154
- input,
155
- ctx,
156
- next: async (opts) => {
157
- // Allow middleware to extend context
158
- if (opts?.ctx) {
159
- Object.assign(ctx, opts.ctx);
160
- }
161
- return currentNext();
162
- },
163
- });
164
- };
165
- }
166
- const result = await next();
167
- return result.output;
168
- }
169
- // ============================================================================
170
- // App Router Creation
171
- // ============================================================================
172
- /**
173
- * Create a namespaced app router from multiple procedure collections
174
- *
175
- * Each collection becomes a nested router under its namespace.
176
- *
177
- * @param t - tRPC instance
178
- * @param collections - Array of procedure collections
179
- * @returns Merged app router
180
- *
181
- * @example
182
- * ```typescript
183
- * const t = createTRPC();
184
- * const appRouter = createAppRouter(t, [
185
- * userProcedures, // namespace: 'users'
186
- * postProcedures, // namespace: 'posts'
187
- * ]);
188
- *
189
- * // Usage:
190
- * // appRouter.users.getUser({ id: '123' })
191
- * // appRouter.posts.listPosts({ page: 1 })
192
- *
193
- * // Export type for client
194
- * export type AppRouter = typeof appRouter;
195
- * ```
196
- */
197
- export function createAppRouter(t, collections) {
198
- const routerConfig = {};
199
- for (const collection of collections) {
200
- routerConfig[collection.namespace] = buildTRPCRouter(t, collection);
201
- }
202
- return t.router(routerConfig);
203
- }
204
- // ============================================================================
205
- // Context Utilities
206
- // ============================================================================
207
- /**
208
- * Create a tRPC context factory for Fastify
209
- *
210
- * This factory creates the context for each tRPC request,
211
- * pulling from the Fastify request's decorated context.
212
- *
213
- * @template TContext - Context type
214
- * @returns Context factory function
215
- *
216
- * @example
217
- * ```typescript
218
- * import { fastifyTRPCPlugin } from '@trpc/server/adapters/fastify';
219
- *
220
- * await server.register(fastifyTRPCPlugin, {
221
- * prefix: '/trpc',
222
- * trpcOptions: {
223
- * router: appRouter,
224
- * createContext: createTRPCContextFactory(),
225
- * },
226
- * });
227
- * ```
228
- */
229
- export function createTRPCContextFactory() {
230
- return ({ req }) => {
231
- if (!req.context) {
232
- throw new TRPCError({
233
- code: 'INTERNAL_SERVER_ERROR',
234
- message: 'Request context not found. Ensure @veloxts/core is properly initialized ' +
235
- 'and the VeloxApp is started before registering tRPC routes.',
236
- });
237
- }
238
- return req.context;
239
- };
240
- }
241
- // ============================================================================
242
- // Error Utilities
243
- // ============================================================================
244
- /**
245
- * Convert a VeloxTS error to a tRPC error
246
- *
247
- * Maps VeloxTS error codes to appropriate tRPC error codes.
248
- *
249
- * @param error - Error to convert
250
- * @param defaultCode - Default tRPC code if mapping not found
251
- * @returns TRPCError
252
- */
253
- export function veloxErrorToTRPCError(error, defaultCode = 'INTERNAL_SERVER_ERROR') {
254
- // Map HTTP status codes to tRPC codes
255
- const statusToTRPC = {
256
- 400: 'BAD_REQUEST',
257
- 401: 'UNAUTHORIZED',
258
- 403: 'FORBIDDEN',
259
- 404: 'NOT_FOUND',
260
- 408: 'TIMEOUT',
261
- 409: 'CONFLICT',
262
- 422: 'UNPROCESSABLE_CONTENT',
263
- 429: 'TOO_MANY_REQUESTS',
264
- 500: 'INTERNAL_SERVER_ERROR',
265
- };
266
- const trpcCode = error.statusCode ? (statusToTRPC[error.statusCode] ?? defaultCode) : defaultCode;
267
- return new TRPCError({
268
- code: trpcCode,
269
- message: error.message,
270
- cause: error.code,
271
- });
272
- }
273
- /**
274
- * Type guard for tRPC errors with VeloxTS cause
275
- *
276
- * Use this when handling errors that may have been converted from VeloxTS errors
277
- * using veloxErrorToTRPCError().
278
- */
279
- export function isVeloxTRPCError(error) {
280
- return typeof error.cause === 'string';
281
- }
282
- /**
283
- * Register tRPC plugin with Fastify server
284
- *
285
- * This is a convenience wrapper around fastifyTRPCPlugin that handles
286
- * the context factory automatically.
287
- *
288
- * @param server - Fastify server instance
289
- * @param options - tRPC plugin options
290
- *
291
- * @example
292
- * ```typescript
293
- * const app = await createVeloxApp({ port: 3000 });
294
- * const appRouter = createAppRouter(t, [userProcedures]);
295
- *
296
- * await registerTRPCPlugin(app.server, {
297
- * prefix: '/trpc',
298
- * router: appRouter,
299
- * });
300
- * ```
301
- */
302
- export async function registerTRPCPlugin(server, options) {
303
- // Dynamic import to avoid pulling in complex types at compile time
304
- const { fastifyTRPCPlugin } = await import('@trpc/server/adapters/fastify');
305
- await server.register(fastifyTRPCPlugin, {
306
- prefix: options.prefix ?? '/trpc',
307
- trpcOptions: {
308
- router: options.router,
309
- createContext: createTRPCContextFactory(),
310
- },
311
- });
312
- }
313
- //# sourceMappingURL=adapter.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"adapter.js","sourceRoot":"","sources":["../../src/trpc/adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAanD,+EAA+E;AAC/E,sBAAsB;AACtB,+EAA+E;AAE/E,iDAAiD;AACjD,2EAA2E;AAC3E,MAAM,QAAQ,GAAG,QAAQ,CAAC,OAAO,EAAe,CAAC,MAAM,EAAE,CAAC;AAU1D;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,UAAU;IACxB,uFAAuF;IACvF,mFAAmF;IACnF,OAAO,QAAQ,CAAC,OAAO,EAAe,CAAC,MAAM,EAAE,CAAC;AAClD,CAAC;AAED,+EAA+E;AAC/E,kBAAkB;AAClB,+EAA+E;AAE/E;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,eAAe,CAC7B,CAA4B,EAC5B,UAA+B;IAE/B,8CAA8C;IAC9C,MAAM,YAAY,GAA0D,EAAE,CAAC;IAE/E,KAAK,MAAM,CAAC,IAAI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AC7B,CAAC;IAEF,MAAM,QAAQ,GAAG,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,KAAK,CAAC,UAAU,CAAC,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC;IAElG,OAAO,IAAI,SAAS,CAAC;QACnB,IAAI,EAAE,QAAQ;QACd,OAAO,EAAE,KAAK,CAAC,OAAO;QACtB,KAAK,EAAE,KAAK,CAAC,IAAI;KAClB,CAAC,CAAC;AACL,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAgB;IAC/C,OAAO,OAAO,KAAK,CAAC,KAAK,KAAK,QAAQ,CAAC;AACzC,CAAC;AAgBD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,MAAuE,EACvE,OAA0B;IAE1B,mEAAmE;IACnE,MAAM,EAAE,iBAAiB,EAAE,GAAG,MAAM,MAAM,CAAC,+BAA+B,CAAC,CAAC;IAE5E,MAAM,MAAM,CAAC,QAAQ,CAAC,iBAAiB,EAAE;QACvC,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,OAAO;QACjC,WAAW,EAAE;YACX,MAAM,EAAE,OAAO,CAAC,MAAM;YACtB,aAAa,EAAE,wBAAwB,EAAE;SAC1C;KACF,CAAC,CAAC;AACL,CAAC"}
@@ -1,8 +0,0 @@
1
- /**
2
- * tRPC adapter exports
3
- *
4
- * @module trpc
5
- */
6
- export type { AnyRouter, InferAppRouter, TRPCInstance, TRPCPluginOptions } from './adapter.js';
7
- export { buildTRPCRouter, createAppRouter, createTRPC, createTRPCContextFactory, isVeloxTRPCError, registerTRPCPlugin, veloxErrorToTRPCError, } from './adapter.js';
8
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/trpc/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,EAAE,SAAS,EAAE,cAAc,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAC/F,OAAO,EACL,eAAe,EACf,eAAe,EACf,UAAU,EACV,wBAAwB,EACxB,gBAAgB,EAChB,kBAAkB,EAClB,qBAAqB,GACtB,MAAM,cAAc,CAAC"}
@@ -1,7 +0,0 @@
1
- /**
2
- * tRPC adapter exports
3
- *
4
- * @module trpc
5
- */
6
- export { buildTRPCRouter, createAppRouter, createTRPC, createTRPCContextFactory, isVeloxTRPCError, registerTRPCPlugin, veloxErrorToTRPCError, } from './adapter.js';
7
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/trpc/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EACL,eAAe,EACf,eAAe,EACf,UAAU,EACV,wBAAwB,EACxB,gBAAgB,EAChB,kBAAkB,EAClB,qBAAqB,GACtB,MAAM,cAAc,CAAC"}
package/dist/types.d.ts DELETED
@@ -1,240 +0,0 @@
1
- /**
2
- * Core type definitions for @veloxts/router
3
- *
4
- * Provides foundational types for the procedure system, context handling,
5
- * and router configuration.
6
- *
7
- * @module types
8
- */
9
- import type { BaseContext } from '@veloxts/core';
10
- /**
11
- * Procedure operation types
12
- *
13
- * - query: Read-only operations (maps to GET in REST)
14
- * - mutation: Write operations (maps to POST/PUT/DELETE in REST)
15
- */
16
- export type ProcedureType = 'query' | 'mutation';
17
- /**
18
- * HTTP methods supported by the REST adapter
19
- *
20
- * MVP (v0.1.0): GET and POST only
21
- * v1.1+: Full REST (GET, POST, PUT, PATCH, DELETE)
22
- */
23
- export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
24
- /**
25
- * Maps procedure naming conventions to HTTP methods
26
- *
27
- * @example
28
- * - getUser -> GET
29
- * - listUsers -> GET
30
- * - createUser -> POST
31
- * - updateUser -> PUT (v1.1+)
32
- * - deleteUser -> DELETE (v1.1+)
33
- */
34
- export declare const PROCEDURE_METHOD_MAP: Record<string, HttpMethod>;
35
- /**
36
- * Extended context type that can be augmented by middleware
37
- *
38
- * Starts with BaseContext and can be extended through the middleware chain.
39
- *
40
- * @template TExtensions - Additional context properties added by middleware
41
- */
42
- export type ExtendedContext<TExtensions extends object = object> = BaseContext & TExtensions;
43
- /**
44
- * Context factory function type
45
- *
46
- * Creates the initial context for a request. Can be customized to add
47
- * application-specific context properties.
48
- */
49
- export type ContextFactory<TContext extends BaseContext = BaseContext> = (baseContext: BaseContext) => TContext | Promise<TContext>;
50
- /**
51
- * Arguments passed to procedure handlers
52
- *
53
- * @template TInput - The validated input type
54
- * @template TContext - The context type available in the handler
55
- */
56
- export interface ProcedureHandlerArgs<TInput, TContext extends BaseContext = BaseContext> {
57
- /** Validated input data (from request body/params/query) */
58
- readonly input: TInput;
59
- /** Request context with framework and plugin-provided properties */
60
- readonly ctx: TContext;
61
- }
62
- /**
63
- * Procedure handler function signature
64
- *
65
- * @template TInput - The validated input type
66
- * @template TOutput - The handler return type
67
- * @template TContext - The context type
68
- */
69
- export type ProcedureHandler<TInput, TOutput, TContext extends BaseContext = BaseContext> = (args: ProcedureHandlerArgs<TInput, TContext>) => TOutput | Promise<TOutput>;
70
- /**
71
- * Result returned by the next() function in middleware
72
- *
73
- * @template TOutput - The output type from the handler
74
- */
75
- export interface MiddlewareResult<TOutput> {
76
- /** The output from the handler or subsequent middleware */
77
- readonly output: TOutput;
78
- }
79
- /**
80
- * Computes the context extensions added by middleware
81
- *
82
- * This utility type extracts properties that exist in TNewContext but not in TContext.
83
- * It enables type-safe context extension in middleware.
84
- *
85
- * @template TContext - The current context type
86
- * @template TNewContext - The extended context type after middleware
87
- */
88
- export type ContextExtensions<TContext extends BaseContext, TNewContext extends BaseContext> = Omit<TNewContext, keyof TContext>;
89
- /**
90
- * Next function passed to middleware
91
- *
92
- * The next function accepts optional context extensions that will be merged
93
- * into the request context. The extensions are typed based on the difference
94
- * between the input context (TContext) and output context (TNewContext).
95
- *
96
- * @template TContext - The current context type before middleware
97
- * @template TNewContext - The context type after middleware modifications
98
- * @template TOutput - Expected output type
99
- */
100
- export type MiddlewareNext<TContext extends BaseContext = BaseContext, TNewContext extends BaseContext = TContext, TOutput = unknown> = (opts?: {
101
- /**
102
- * Context extensions to merge into the request context.
103
- *
104
- * When TNewContext extends TContext with additional properties,
105
- * this parameter accepts either:
106
- * - The new properties (type-safe extension)
107
- * - A partial of the new context (for flexibility)
108
- */
109
- ctx?: ContextExtensions<TContext, TNewContext> | Partial<TNewContext>;
110
- }) => Promise<MiddlewareResult<TOutput>>;
111
- /**
112
- * Arguments passed to middleware functions
113
- *
114
- * @template TInput - The input type
115
- * @template TContext - The current context type before middleware
116
- * @template TNewContext - The context type after middleware modifications
117
- * @template TOutput - The expected output type
118
- */
119
- export interface MiddlewareArgs<TInput, TContext extends BaseContext = BaseContext, TNewContext extends BaseContext = TContext, TOutput = unknown> {
120
- /** Input data (may not be validated yet depending on middleware position) */
121
- readonly input: TInput;
122
- /** Current request context */
123
- readonly ctx: TContext;
124
- /**
125
- * Function to call the next middleware or handler
126
- *
127
- * The next function accepts context extensions that transform TContext into TNewContext.
128
- * This enables type-safe context extension through the middleware chain.
129
- */
130
- readonly next: MiddlewareNext<TContext, TNewContext, TOutput>;
131
- }
132
- /**
133
- * Middleware function signature
134
- *
135
- * Middleware can:
136
- * - Modify context before passing to handler
137
- * - Modify output after handler execution
138
- * - Short-circuit the chain by not calling next()
139
- * - Throw errors to abort processing
140
- *
141
- * The TNewContext parameter tracks context extensions for type safety.
142
- * When using .use<TNewContext>(), the builder captures this type and ensures
143
- * subsequent middleware and handlers see the extended context.
144
- *
145
- * @template TInput - The input type
146
- * @template TContext - The current context type before middleware
147
- * @template TNewContext - Context type after middleware modifications
148
- * @template TOutput - The output type
149
- */
150
- export type MiddlewareFunction<TInput, TContext extends BaseContext = BaseContext, TNewContext extends BaseContext = TContext, TOutput = unknown> = (args: MiddlewareArgs<TInput, TContext, TNewContext, TOutput>) => Promise<MiddlewareResult<TOutput>>;
151
- /**
152
- * REST route override configuration
153
- *
154
- * Allows manual override of auto-generated REST routes.
155
- */
156
- export interface RestRouteOverride {
157
- /** HTTP method to use */
158
- method?: HttpMethod;
159
- /** Custom path pattern (e.g., '/users/:id/activate') */
160
- path?: string;
161
- }
162
- /**
163
- * Compiled procedure with all metadata and handlers
164
- *
165
- * This is the final output of the procedure builder, ready for registration
166
- * with both tRPC and REST adapters.
167
- *
168
- * @template TInput - The validated input type
169
- * @template TOutput - The handler output type
170
- * @template TContext - The context type
171
- */
172
- export interface CompiledProcedure<TInput = unknown, TOutput = unknown, TContext extends BaseContext = BaseContext> {
173
- /** Whether this is a query or mutation */
174
- readonly type: ProcedureType;
175
- /** The procedure handler function */
176
- readonly handler: ProcedureHandler<TInput, TOutput, TContext>;
177
- /** Input validation schema (if specified) */
178
- readonly inputSchema?: {
179
- parse: (input: unknown) => TInput;
180
- };
181
- /** Output validation schema (if specified) */
182
- readonly outputSchema?: {
183
- parse: (output: unknown) => TOutput;
184
- };
185
- /** Middleware chain to execute before handler */
186
- readonly middlewares: ReadonlyArray<MiddlewareFunction<TInput, TContext, TContext, TOutput>>;
187
- /** REST route override (if specified) */
188
- readonly restOverride?: RestRouteOverride;
189
- /**
190
- * Pre-compiled middleware chain executor
191
- *
192
- * PERFORMANCE: This function is created once during procedure compilation
193
- * and reused for every request, avoiding the overhead of building the
194
- * middleware chain dynamically on each invocation.
195
- *
196
- * @internal
197
- */
198
- readonly _precompiledExecutor?: (input: TInput, ctx: TContext) => Promise<TOutput>;
199
- }
200
- /**
201
- * Record of named procedures
202
- *
203
- * NOTE: Uses `any` for variance compatibility - see ProcedureDefinitions for explanation.
204
- */
205
- export type ProcedureRecord = Record<string, CompiledProcedure<any, any, any>>;
206
- /**
207
- * Procedure collection with namespace
208
- *
209
- * Groups related procedures under a common namespace for routing.
210
- *
211
- * @template TProcedures - The record of named procedures
212
- */
213
- export interface ProcedureCollection<TProcedures extends ProcedureRecord = ProcedureRecord> {
214
- /** Resource namespace (e.g., 'users', 'posts') */
215
- readonly namespace: string;
216
- /** Named procedures in this collection */
217
- readonly procedures: TProcedures;
218
- }
219
- /**
220
- * Extracts the input type from a compiled procedure
221
- */
222
- export type InferProcedureInput<T> = T extends CompiledProcedure<infer I, unknown, BaseContext> ? I : never;
223
- /**
224
- * Extracts the output type from a compiled procedure
225
- */
226
- export type InferProcedureOutput<T> = T extends CompiledProcedure<unknown, infer O, BaseContext> ? O : never;
227
- /**
228
- * Extracts the context type from a compiled procedure
229
- */
230
- export type InferProcedureContext<T> = T extends CompiledProcedure<unknown, unknown, infer C> ? C : never;
231
- /**
232
- * Extracts procedure types from a collection
233
- */
234
- export type InferProcedureTypes<T extends ProcedureCollection> = {
235
- [K in keyof T['procedures']]: {
236
- input: InferProcedureInput<T['procedures'][K]>;
237
- output: InferProcedureOutput<T['procedures'][K]>;
238
- };
239
- };
240
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAMjD;;;;;GAKG;AACH,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,UAAU,CAAC;AAEjD;;;;;GAKG;AACH,MAAM,MAAM,UAAU,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,OAAO,GAAG,QAAQ,CAAC;AAErE;;;;;;;;;GASG;AACH,eAAO,MAAM,oBAAoB,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAWlD,CAAC;AAMX;;;;;;GAMG;AACH,MAAM,MAAM,eAAe,CAAC,WAAW,SAAS,MAAM,GAAG,MAAM,IAAI,WAAW,GAAG,WAAW,CAAC;AAE7F;;;;;GAKG;AACH,MAAM,MAAM,cAAc,CAAC,QAAQ,SAAS,WAAW,GAAG,WAAW,IAAI,CAC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MAAM,MAAM,mBAAmB,CAAC,CAAC,SAAS,mBAAmB,IAAI;KAC9D,CAAC,IAAI,MAAM,CAAC,CAAC,YAAY,CAAC,GAAG;QAC5B,KAAK,EAAE,mBAAmB,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAC/C,MAAM,EAAE,oBAAoB,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;KAClD;CACF,CAAC"}
package/dist/types.js DELETED
@@ -1,31 +0,0 @@
1
- /**
2
- * Core type definitions for @veloxts/router
3
- *
4
- * Provides foundational types for the procedure system, context handling,
5
- * and router configuration.
6
- *
7
- * @module types
8
- */
9
- /**
10
- * Maps procedure naming conventions to HTTP methods
11
- *
12
- * @example
13
- * - getUser -> GET
14
- * - listUsers -> GET
15
- * - createUser -> POST
16
- * - updateUser -> PUT (v1.1+)
17
- * - deleteUser -> DELETE (v1.1+)
18
- */
19
- export const PROCEDURE_METHOD_MAP = {
20
- get: 'GET',
21
- list: 'GET',
22
- find: 'GET',
23
- create: 'POST',
24
- add: 'POST',
25
- update: 'PUT',
26
- edit: 'PUT',
27
- patch: 'PATCH',
28
- delete: 'DELETE',
29
- remove: 'DELETE',
30
- };
31
- //# sourceMappingURL=types.js.map
package/dist/types.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAwBH;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAA+B;IAC9D,GAAG,EAAE,KAAK;IACV,IAAI,EAAE,KAAK;IACX,IAAI,EAAE,KAAK;IACX,MAAM,EAAE,MAAM;IACd,GAAG,EAAE,MAAM;IACX,MAAM,EAAE,KAAK;IACb,IAAI,EAAE,KAAK;IACX,KAAK,EAAE,OAAO;IACd,MAAM,EAAE,QAAQ;IAChB,MAAM,EAAE,QAAQ;CACR,CAAC"}