nural 0.3.7 → 0.3.10
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.
- package/dist/index.cjs +124 -114
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +128 -129
- package/dist/index.d.ts +128 -129
- package/dist/index.js +123 -114
- package/dist/index.js.map +1 -1
- package/package.json +10 -18
- package/README.md +0 -446
- package/dist/cli/index.cjs +0 -2239
- package/dist/cli/index.cjs.map +0 -1
- package/dist/cli/index.d.cts +0 -2
- package/dist/cli/index.d.ts +0 -2
- package/dist/cli/index.js +0 -2210
- package/dist/cli/index.js.map +0 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,130 +1,12 @@
|
|
|
1
|
+
import * as node_http from 'node:http';
|
|
2
|
+
import * as fastify from 'fastify';
|
|
3
|
+
import { FastifyRequest, FastifyReply } from 'fastify';
|
|
4
|
+
import { Server } from 'http';
|
|
5
|
+
import { Request, Response } from 'express';
|
|
1
6
|
import { z } from 'zod';
|
|
2
7
|
export { z } from 'zod';
|
|
3
|
-
import { Request, Response } from 'express';
|
|
4
|
-
import { FastifyRequest, FastifyReply } from 'fastify';
|
|
5
8
|
export { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi';
|
|
6
9
|
|
|
7
|
-
/**
|
|
8
|
-
* Middleware Types and Helpers
|
|
9
|
-
*/
|
|
10
|
-
/**
|
|
11
|
-
* Middleware handler function type
|
|
12
|
-
* Returns data to be merged into route context
|
|
13
|
-
*/
|
|
14
|
-
type MiddlewareHandler<Req = unknown, Res = unknown> = (req: Req, res: Res) => Promise<Record<string, unknown> | void> | Record<string, unknown> | void;
|
|
15
|
-
/**
|
|
16
|
-
* Define a type-safe middleware
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```typescript
|
|
20
|
-
* const authMiddleware = defineMiddleware(async (req: Request) => {
|
|
21
|
-
* const token = req.headers.authorization;
|
|
22
|
-
* if (!token) throw new Error('Unauthorized');
|
|
23
|
-
* return { user: { id: '123', role: 'admin' } };
|
|
24
|
-
* });
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
declare function defineMiddleware<T extends Record<string, unknown> | void, Req = unknown, Res = unknown>(fn: (req: Req, res: Res) => Promise<T> | T): (req: Req, res: Res) => Promise<T> | T;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* HTTP Types
|
|
31
|
-
* Centralized HTTP-related type definitions
|
|
32
|
-
*/
|
|
33
|
-
/**
|
|
34
|
-
* Supported HTTP methods
|
|
35
|
-
*/
|
|
36
|
-
type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS" | "HEAD" | "ALL";
|
|
37
|
-
/**
|
|
38
|
-
* Common HTTP status codes
|
|
39
|
-
*/
|
|
40
|
-
type HttpStatusCode = 200 | 201 | 204 | 400 | 401 | 403 | 404 | 500;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Route Types
|
|
44
|
-
* Type definitions for route configuration and handlers
|
|
45
|
-
*/
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Generic Zod type - either a Zod schema or undefined
|
|
49
|
-
*/
|
|
50
|
-
type ZodAny = z.ZodTypeAny | undefined;
|
|
51
|
-
/**
|
|
52
|
-
* Inference helper: extracts type from Zod schema, returns unknown if undefined
|
|
53
|
-
*/
|
|
54
|
-
type InferZ<T extends ZodAny> = T extends z.ZodTypeAny ? z.infer<T> : unknown;
|
|
55
|
-
/**
|
|
56
|
-
* Extract the return type of a single middleware
|
|
57
|
-
*/
|
|
58
|
-
type MiddlewareReturn<M> = M extends MiddlewareHandler<any, any> ? Awaited<ReturnType<M>> : {};
|
|
59
|
-
/**
|
|
60
|
-
* Convert an array of middlewares into an intersection type
|
|
61
|
-
* @example [{user: string}] & [{role: string}] -> {user: string, role: string}
|
|
62
|
-
*/
|
|
63
|
-
type MergeMiddlewareTypes<M extends MiddlewareHandler<any, any>[] | undefined> = M extends Array<any> ? MiddlewareReturn<M[number]> extends infer R ? R extends void ? {} : R : {} : {};
|
|
64
|
-
/**
|
|
65
|
-
* Context passed to route handlers
|
|
66
|
-
*/
|
|
67
|
-
type RouteContext<P extends ZodAny, Q extends ZodAny, B extends ZodAny, M extends MiddlewareHandler<any, any>[] | undefined> = {
|
|
68
|
-
/** Validated path parameters */
|
|
69
|
-
params: InferZ<P>;
|
|
70
|
-
/** Validated query parameters */
|
|
71
|
-
query: InferZ<Q>;
|
|
72
|
-
/** Validated request body */
|
|
73
|
-
body: InferZ<B>;
|
|
74
|
-
/** Raw request object (Express or Fastify) */
|
|
75
|
-
req: Request | FastifyRequest;
|
|
76
|
-
/** Raw response object (Express or Fastify) */
|
|
77
|
-
res: Response | FastifyReply;
|
|
78
|
-
} & MergeMiddlewareTypes<M>;
|
|
79
|
-
/**
|
|
80
|
-
* Route handler function type
|
|
81
|
-
*/
|
|
82
|
-
type RouteHandler<P extends ZodAny, Q extends ZodAny, B extends ZodAny, R extends ZodAny, M extends MiddlewareHandler<any, any>[] | undefined> = (ctx: RouteContext<P, Q, B, M>) => Promise<InferZ<R> | void> | InferZ<R> | void;
|
|
83
|
-
/**
|
|
84
|
-
* Route configuration object
|
|
85
|
-
*/
|
|
86
|
-
interface RouteConfig<P extends ZodAny = undefined, Q extends ZodAny = undefined, B extends ZodAny = undefined, R extends ZodAny = undefined, M extends MiddlewareHandler<any, any>[] | undefined = undefined> {
|
|
87
|
-
/** HTTP method */
|
|
88
|
-
method: HttpMethod;
|
|
89
|
-
/** Route path (supports :param syntax) */
|
|
90
|
-
path: string;
|
|
91
|
-
/** Short summary for documentation */
|
|
92
|
-
summary?: string;
|
|
93
|
-
/** Detailed description for documentation */
|
|
94
|
-
description?: string;
|
|
95
|
-
/** Tags for grouping in documentation */
|
|
96
|
-
tags?: string[];
|
|
97
|
-
/** Middleware to run before handler */
|
|
98
|
-
middleware?: M;
|
|
99
|
-
/** Request validation schemas */
|
|
100
|
-
request?: {
|
|
101
|
-
/** Path parameters schema */
|
|
102
|
-
params?: P;
|
|
103
|
-
/** Query parameters schema */
|
|
104
|
-
query?: Q;
|
|
105
|
-
/** Request body schema */
|
|
106
|
-
body?: B;
|
|
107
|
-
};
|
|
108
|
-
/** Response schemas by status code */
|
|
109
|
-
responses?: Record<number, z.ZodTypeAny>;
|
|
110
|
-
/**
|
|
111
|
-
* OpenAPI Security Requirements
|
|
112
|
-
* @example [{ bearerAuth: [] }]
|
|
113
|
-
*/
|
|
114
|
-
security?: Array<Record<string, string[]>>;
|
|
115
|
-
/**
|
|
116
|
-
* OpenAPI Operation overrides
|
|
117
|
-
* Allows full customization of the operation (e.g., custom headers, externalDocs)
|
|
118
|
-
*/
|
|
119
|
-
openapi?: Record<string, any>;
|
|
120
|
-
/** Route handler function */
|
|
121
|
-
handler: RouteHandler<P, Q, B, R, M>;
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Catch-all type for arrays of routes
|
|
125
|
-
*/
|
|
126
|
-
type AnyRouteConfig = RouteConfig<any, any, any, any, any>;
|
|
127
|
-
|
|
128
10
|
/**
|
|
129
11
|
* Error Types and Handler
|
|
130
12
|
* Types for global error handling
|
|
@@ -427,6 +309,127 @@ interface NuralConfig {
|
|
|
427
309
|
errorHandler?: boolean | ErrorHandler | ErrorHandlerConfig;
|
|
428
310
|
}
|
|
429
311
|
|
|
312
|
+
/**
|
|
313
|
+
* Middleware Types and Helpers
|
|
314
|
+
*/
|
|
315
|
+
/**
|
|
316
|
+
* Middleware handler function type
|
|
317
|
+
* Returns data to be merged into route context
|
|
318
|
+
*/
|
|
319
|
+
type MiddlewareHandler<Req = unknown, Res = unknown> = (req: Req, res: Res) => Promise<Record<string, unknown> | void> | Record<string, unknown> | void;
|
|
320
|
+
/**
|
|
321
|
+
* Define a type-safe middleware
|
|
322
|
+
*
|
|
323
|
+
* @example
|
|
324
|
+
* ```typescript
|
|
325
|
+
* const authMiddleware = defineMiddleware(async (req: Request) => {
|
|
326
|
+
* const token = req.headers.authorization;
|
|
327
|
+
* if (!token) throw new Error('Unauthorized');
|
|
328
|
+
* return { user: { id: '123', role: 'admin' } };
|
|
329
|
+
* });
|
|
330
|
+
* ```
|
|
331
|
+
*/
|
|
332
|
+
declare function defineMiddleware<T extends Record<string, unknown> | void, Req = unknown, Res = unknown>(fn: (req: Req, res: Res) => Promise<T> | T): (req: Req, res: Res) => Promise<T> | T;
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* HTTP Types
|
|
336
|
+
* Centralized HTTP-related type definitions
|
|
337
|
+
*/
|
|
338
|
+
/**
|
|
339
|
+
* Supported HTTP methods
|
|
340
|
+
*/
|
|
341
|
+
type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS" | "HEAD" | "ALL";
|
|
342
|
+
/**
|
|
343
|
+
* Common HTTP status codes
|
|
344
|
+
*/
|
|
345
|
+
type HttpStatusCode = 200 | 201 | 204 | 400 | 401 | 403 | 404 | 500;
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Route Types
|
|
349
|
+
* Type definitions for route configuration and handlers
|
|
350
|
+
*/
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Generic Zod type - either a Zod schema or undefined
|
|
354
|
+
*/
|
|
355
|
+
type ZodAny = z.ZodTypeAny | undefined;
|
|
356
|
+
/**
|
|
357
|
+
* Inference helper: extracts type from Zod schema, returns unknown if undefined
|
|
358
|
+
*/
|
|
359
|
+
type InferZ<T extends ZodAny> = T extends z.ZodTypeAny ? z.infer<T> : unknown;
|
|
360
|
+
/**
|
|
361
|
+
* Extract the return type of a single middleware
|
|
362
|
+
*/
|
|
363
|
+
type MiddlewareReturn<M> = M extends MiddlewareHandler<any, any> ? Awaited<ReturnType<M>> : {};
|
|
364
|
+
/**
|
|
365
|
+
* Convert an array of middlewares into an intersection type
|
|
366
|
+
* @example [{user: string}] & [{role: string}] -> {user: string, role: string}
|
|
367
|
+
*/
|
|
368
|
+
type MergeMiddlewareTypes<M extends MiddlewareHandler<any, any>[] | undefined> = M extends Array<any> ? MiddlewareReturn<M[number]> extends infer R ? R extends void ? {} : R : {} : {};
|
|
369
|
+
/**
|
|
370
|
+
* Context passed to route handlers
|
|
371
|
+
*/
|
|
372
|
+
type RouteContext<P extends ZodAny, Q extends ZodAny, B extends ZodAny, M extends MiddlewareHandler<any, any>[] | undefined> = {
|
|
373
|
+
/** Validated path parameters */
|
|
374
|
+
params: InferZ<P>;
|
|
375
|
+
/** Validated query parameters */
|
|
376
|
+
query: InferZ<Q>;
|
|
377
|
+
/** Validated request body */
|
|
378
|
+
body: InferZ<B>;
|
|
379
|
+
/** Raw request object (Express or Fastify) */
|
|
380
|
+
req: Request | FastifyRequest;
|
|
381
|
+
/** Raw response object (Express or Fastify) */
|
|
382
|
+
res: Response | FastifyReply;
|
|
383
|
+
} & MergeMiddlewareTypes<M>;
|
|
384
|
+
/**
|
|
385
|
+
* Route handler function type
|
|
386
|
+
*/
|
|
387
|
+
type RouteHandler<P extends ZodAny, Q extends ZodAny, B extends ZodAny, R extends ZodAny, M extends MiddlewareHandler<any, any>[] | undefined> = (ctx: RouteContext<P, Q, B, M>) => Promise<InferZ<R> | void> | InferZ<R> | void;
|
|
388
|
+
/**
|
|
389
|
+
* Route configuration object
|
|
390
|
+
*/
|
|
391
|
+
interface RouteConfig<P extends ZodAny = undefined, Q extends ZodAny = undefined, B extends ZodAny = undefined, R extends ZodAny = undefined, M extends MiddlewareHandler<any, any>[] | undefined = undefined> {
|
|
392
|
+
/** HTTP method */
|
|
393
|
+
method: HttpMethod;
|
|
394
|
+
/** Route path (supports :param syntax) */
|
|
395
|
+
path: string;
|
|
396
|
+
/** Short summary for documentation */
|
|
397
|
+
summary?: string;
|
|
398
|
+
/** Detailed description for documentation */
|
|
399
|
+
description?: string;
|
|
400
|
+
/** Tags for grouping in documentation */
|
|
401
|
+
tags?: string[];
|
|
402
|
+
/** Middleware to run before handler */
|
|
403
|
+
middleware?: M;
|
|
404
|
+
/** Request validation schemas */
|
|
405
|
+
request?: {
|
|
406
|
+
/** Path parameters schema */
|
|
407
|
+
params?: P;
|
|
408
|
+
/** Query parameters schema */
|
|
409
|
+
query?: Q;
|
|
410
|
+
/** Request body schema */
|
|
411
|
+
body?: B;
|
|
412
|
+
};
|
|
413
|
+
/** Response schemas by status code */
|
|
414
|
+
responses?: Record<number, z.ZodTypeAny>;
|
|
415
|
+
/**
|
|
416
|
+
* OpenAPI Security Requirements
|
|
417
|
+
* @example [{ bearerAuth: [] }]
|
|
418
|
+
*/
|
|
419
|
+
security?: Array<Record<string, string[]>>;
|
|
420
|
+
/**
|
|
421
|
+
* OpenAPI Operation overrides
|
|
422
|
+
* Allows full customization of the operation (e.g., custom headers, externalDocs)
|
|
423
|
+
*/
|
|
424
|
+
openapi?: Record<string, any>;
|
|
425
|
+
/** Route handler function */
|
|
426
|
+
handler: RouteHandler<P, Q, B, R, M>;
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Catch-all type for arrays of routes
|
|
430
|
+
*/
|
|
431
|
+
type AnyRouteConfig = RouteConfig<any, any, any, any, any>;
|
|
432
|
+
|
|
430
433
|
/**
|
|
431
434
|
* Zero-Dependency Logger
|
|
432
435
|
* Lightweight, colorful, and powerful logging system
|
|
@@ -454,11 +457,6 @@ declare class Logger {
|
|
|
454
457
|
debug(message: string): void;
|
|
455
458
|
}
|
|
456
459
|
|
|
457
|
-
/**
|
|
458
|
-
* Nural Framework
|
|
459
|
-
* Main application class that orchestrates adapters and documentation
|
|
460
|
-
*/
|
|
461
|
-
|
|
462
460
|
/**
|
|
463
461
|
* Nural - The intelligent, schema-first REST framework
|
|
464
462
|
*
|
|
@@ -485,6 +483,7 @@ declare class Nural {
|
|
|
485
483
|
private isExpress;
|
|
486
484
|
logger: Logger;
|
|
487
485
|
constructor(config?: NuralConfig);
|
|
486
|
+
get server(): fastify.RawServerDefault | Server<typeof node_http.IncomingMessage, typeof node_http.ServerResponse>;
|
|
488
487
|
/**
|
|
489
488
|
* Apply CORS and Helmet middleware based on config
|
|
490
489
|
*/
|
|
@@ -496,7 +495,7 @@ declare class Nural {
|
|
|
496
495
|
/**
|
|
497
496
|
* Start the server
|
|
498
497
|
*/
|
|
499
|
-
start(port: number):
|
|
498
|
+
start(port: number): Server;
|
|
500
499
|
/**
|
|
501
500
|
* Setup documentation routes
|
|
502
501
|
*/
|
package/dist/index.d.ts
CHANGED
|
@@ -1,130 +1,12 @@
|
|
|
1
|
+
import * as node_http from 'node:http';
|
|
2
|
+
import * as fastify from 'fastify';
|
|
3
|
+
import { FastifyRequest, FastifyReply } from 'fastify';
|
|
4
|
+
import { Server } from 'http';
|
|
5
|
+
import { Request, Response } from 'express';
|
|
1
6
|
import { z } from 'zod';
|
|
2
7
|
export { z } from 'zod';
|
|
3
|
-
import { Request, Response } from 'express';
|
|
4
|
-
import { FastifyRequest, FastifyReply } from 'fastify';
|
|
5
8
|
export { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi';
|
|
6
9
|
|
|
7
|
-
/**
|
|
8
|
-
* Middleware Types and Helpers
|
|
9
|
-
*/
|
|
10
|
-
/**
|
|
11
|
-
* Middleware handler function type
|
|
12
|
-
* Returns data to be merged into route context
|
|
13
|
-
*/
|
|
14
|
-
type MiddlewareHandler<Req = unknown, Res = unknown> = (req: Req, res: Res) => Promise<Record<string, unknown> | void> | Record<string, unknown> | void;
|
|
15
|
-
/**
|
|
16
|
-
* Define a type-safe middleware
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```typescript
|
|
20
|
-
* const authMiddleware = defineMiddleware(async (req: Request) => {
|
|
21
|
-
* const token = req.headers.authorization;
|
|
22
|
-
* if (!token) throw new Error('Unauthorized');
|
|
23
|
-
* return { user: { id: '123', role: 'admin' } };
|
|
24
|
-
* });
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
declare function defineMiddleware<T extends Record<string, unknown> | void, Req = unknown, Res = unknown>(fn: (req: Req, res: Res) => Promise<T> | T): (req: Req, res: Res) => Promise<T> | T;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* HTTP Types
|
|
31
|
-
* Centralized HTTP-related type definitions
|
|
32
|
-
*/
|
|
33
|
-
/**
|
|
34
|
-
* Supported HTTP methods
|
|
35
|
-
*/
|
|
36
|
-
type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS" | "HEAD" | "ALL";
|
|
37
|
-
/**
|
|
38
|
-
* Common HTTP status codes
|
|
39
|
-
*/
|
|
40
|
-
type HttpStatusCode = 200 | 201 | 204 | 400 | 401 | 403 | 404 | 500;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Route Types
|
|
44
|
-
* Type definitions for route configuration and handlers
|
|
45
|
-
*/
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Generic Zod type - either a Zod schema or undefined
|
|
49
|
-
*/
|
|
50
|
-
type ZodAny = z.ZodTypeAny | undefined;
|
|
51
|
-
/**
|
|
52
|
-
* Inference helper: extracts type from Zod schema, returns unknown if undefined
|
|
53
|
-
*/
|
|
54
|
-
type InferZ<T extends ZodAny> = T extends z.ZodTypeAny ? z.infer<T> : unknown;
|
|
55
|
-
/**
|
|
56
|
-
* Extract the return type of a single middleware
|
|
57
|
-
*/
|
|
58
|
-
type MiddlewareReturn<M> = M extends MiddlewareHandler<any, any> ? Awaited<ReturnType<M>> : {};
|
|
59
|
-
/**
|
|
60
|
-
* Convert an array of middlewares into an intersection type
|
|
61
|
-
* @example [{user: string}] & [{role: string}] -> {user: string, role: string}
|
|
62
|
-
*/
|
|
63
|
-
type MergeMiddlewareTypes<M extends MiddlewareHandler<any, any>[] | undefined> = M extends Array<any> ? MiddlewareReturn<M[number]> extends infer R ? R extends void ? {} : R : {} : {};
|
|
64
|
-
/**
|
|
65
|
-
* Context passed to route handlers
|
|
66
|
-
*/
|
|
67
|
-
type RouteContext<P extends ZodAny, Q extends ZodAny, B extends ZodAny, M extends MiddlewareHandler<any, any>[] | undefined> = {
|
|
68
|
-
/** Validated path parameters */
|
|
69
|
-
params: InferZ<P>;
|
|
70
|
-
/** Validated query parameters */
|
|
71
|
-
query: InferZ<Q>;
|
|
72
|
-
/** Validated request body */
|
|
73
|
-
body: InferZ<B>;
|
|
74
|
-
/** Raw request object (Express or Fastify) */
|
|
75
|
-
req: Request | FastifyRequest;
|
|
76
|
-
/** Raw response object (Express or Fastify) */
|
|
77
|
-
res: Response | FastifyReply;
|
|
78
|
-
} & MergeMiddlewareTypes<M>;
|
|
79
|
-
/**
|
|
80
|
-
* Route handler function type
|
|
81
|
-
*/
|
|
82
|
-
type RouteHandler<P extends ZodAny, Q extends ZodAny, B extends ZodAny, R extends ZodAny, M extends MiddlewareHandler<any, any>[] | undefined> = (ctx: RouteContext<P, Q, B, M>) => Promise<InferZ<R> | void> | InferZ<R> | void;
|
|
83
|
-
/**
|
|
84
|
-
* Route configuration object
|
|
85
|
-
*/
|
|
86
|
-
interface RouteConfig<P extends ZodAny = undefined, Q extends ZodAny = undefined, B extends ZodAny = undefined, R extends ZodAny = undefined, M extends MiddlewareHandler<any, any>[] | undefined = undefined> {
|
|
87
|
-
/** HTTP method */
|
|
88
|
-
method: HttpMethod;
|
|
89
|
-
/** Route path (supports :param syntax) */
|
|
90
|
-
path: string;
|
|
91
|
-
/** Short summary for documentation */
|
|
92
|
-
summary?: string;
|
|
93
|
-
/** Detailed description for documentation */
|
|
94
|
-
description?: string;
|
|
95
|
-
/** Tags for grouping in documentation */
|
|
96
|
-
tags?: string[];
|
|
97
|
-
/** Middleware to run before handler */
|
|
98
|
-
middleware?: M;
|
|
99
|
-
/** Request validation schemas */
|
|
100
|
-
request?: {
|
|
101
|
-
/** Path parameters schema */
|
|
102
|
-
params?: P;
|
|
103
|
-
/** Query parameters schema */
|
|
104
|
-
query?: Q;
|
|
105
|
-
/** Request body schema */
|
|
106
|
-
body?: B;
|
|
107
|
-
};
|
|
108
|
-
/** Response schemas by status code */
|
|
109
|
-
responses?: Record<number, z.ZodTypeAny>;
|
|
110
|
-
/**
|
|
111
|
-
* OpenAPI Security Requirements
|
|
112
|
-
* @example [{ bearerAuth: [] }]
|
|
113
|
-
*/
|
|
114
|
-
security?: Array<Record<string, string[]>>;
|
|
115
|
-
/**
|
|
116
|
-
* OpenAPI Operation overrides
|
|
117
|
-
* Allows full customization of the operation (e.g., custom headers, externalDocs)
|
|
118
|
-
*/
|
|
119
|
-
openapi?: Record<string, any>;
|
|
120
|
-
/** Route handler function */
|
|
121
|
-
handler: RouteHandler<P, Q, B, R, M>;
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Catch-all type for arrays of routes
|
|
125
|
-
*/
|
|
126
|
-
type AnyRouteConfig = RouteConfig<any, any, any, any, any>;
|
|
127
|
-
|
|
128
10
|
/**
|
|
129
11
|
* Error Types and Handler
|
|
130
12
|
* Types for global error handling
|
|
@@ -427,6 +309,127 @@ interface NuralConfig {
|
|
|
427
309
|
errorHandler?: boolean | ErrorHandler | ErrorHandlerConfig;
|
|
428
310
|
}
|
|
429
311
|
|
|
312
|
+
/**
|
|
313
|
+
* Middleware Types and Helpers
|
|
314
|
+
*/
|
|
315
|
+
/**
|
|
316
|
+
* Middleware handler function type
|
|
317
|
+
* Returns data to be merged into route context
|
|
318
|
+
*/
|
|
319
|
+
type MiddlewareHandler<Req = unknown, Res = unknown> = (req: Req, res: Res) => Promise<Record<string, unknown> | void> | Record<string, unknown> | void;
|
|
320
|
+
/**
|
|
321
|
+
* Define a type-safe middleware
|
|
322
|
+
*
|
|
323
|
+
* @example
|
|
324
|
+
* ```typescript
|
|
325
|
+
* const authMiddleware = defineMiddleware(async (req: Request) => {
|
|
326
|
+
* const token = req.headers.authorization;
|
|
327
|
+
* if (!token) throw new Error('Unauthorized');
|
|
328
|
+
* return { user: { id: '123', role: 'admin' } };
|
|
329
|
+
* });
|
|
330
|
+
* ```
|
|
331
|
+
*/
|
|
332
|
+
declare function defineMiddleware<T extends Record<string, unknown> | void, Req = unknown, Res = unknown>(fn: (req: Req, res: Res) => Promise<T> | T): (req: Req, res: Res) => Promise<T> | T;
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* HTTP Types
|
|
336
|
+
* Centralized HTTP-related type definitions
|
|
337
|
+
*/
|
|
338
|
+
/**
|
|
339
|
+
* Supported HTTP methods
|
|
340
|
+
*/
|
|
341
|
+
type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS" | "HEAD" | "ALL";
|
|
342
|
+
/**
|
|
343
|
+
* Common HTTP status codes
|
|
344
|
+
*/
|
|
345
|
+
type HttpStatusCode = 200 | 201 | 204 | 400 | 401 | 403 | 404 | 500;
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Route Types
|
|
349
|
+
* Type definitions for route configuration and handlers
|
|
350
|
+
*/
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Generic Zod type - either a Zod schema or undefined
|
|
354
|
+
*/
|
|
355
|
+
type ZodAny = z.ZodTypeAny | undefined;
|
|
356
|
+
/**
|
|
357
|
+
* Inference helper: extracts type from Zod schema, returns unknown if undefined
|
|
358
|
+
*/
|
|
359
|
+
type InferZ<T extends ZodAny> = T extends z.ZodTypeAny ? z.infer<T> : unknown;
|
|
360
|
+
/**
|
|
361
|
+
* Extract the return type of a single middleware
|
|
362
|
+
*/
|
|
363
|
+
type MiddlewareReturn<M> = M extends MiddlewareHandler<any, any> ? Awaited<ReturnType<M>> : {};
|
|
364
|
+
/**
|
|
365
|
+
* Convert an array of middlewares into an intersection type
|
|
366
|
+
* @example [{user: string}] & [{role: string}] -> {user: string, role: string}
|
|
367
|
+
*/
|
|
368
|
+
type MergeMiddlewareTypes<M extends MiddlewareHandler<any, any>[] | undefined> = M extends Array<any> ? MiddlewareReturn<M[number]> extends infer R ? R extends void ? {} : R : {} : {};
|
|
369
|
+
/**
|
|
370
|
+
* Context passed to route handlers
|
|
371
|
+
*/
|
|
372
|
+
type RouteContext<P extends ZodAny, Q extends ZodAny, B extends ZodAny, M extends MiddlewareHandler<any, any>[] | undefined> = {
|
|
373
|
+
/** Validated path parameters */
|
|
374
|
+
params: InferZ<P>;
|
|
375
|
+
/** Validated query parameters */
|
|
376
|
+
query: InferZ<Q>;
|
|
377
|
+
/** Validated request body */
|
|
378
|
+
body: InferZ<B>;
|
|
379
|
+
/** Raw request object (Express or Fastify) */
|
|
380
|
+
req: Request | FastifyRequest;
|
|
381
|
+
/** Raw response object (Express or Fastify) */
|
|
382
|
+
res: Response | FastifyReply;
|
|
383
|
+
} & MergeMiddlewareTypes<M>;
|
|
384
|
+
/**
|
|
385
|
+
* Route handler function type
|
|
386
|
+
*/
|
|
387
|
+
type RouteHandler<P extends ZodAny, Q extends ZodAny, B extends ZodAny, R extends ZodAny, M extends MiddlewareHandler<any, any>[] | undefined> = (ctx: RouteContext<P, Q, B, M>) => Promise<InferZ<R> | void> | InferZ<R> | void;
|
|
388
|
+
/**
|
|
389
|
+
* Route configuration object
|
|
390
|
+
*/
|
|
391
|
+
interface RouteConfig<P extends ZodAny = undefined, Q extends ZodAny = undefined, B extends ZodAny = undefined, R extends ZodAny = undefined, M extends MiddlewareHandler<any, any>[] | undefined = undefined> {
|
|
392
|
+
/** HTTP method */
|
|
393
|
+
method: HttpMethod;
|
|
394
|
+
/** Route path (supports :param syntax) */
|
|
395
|
+
path: string;
|
|
396
|
+
/** Short summary for documentation */
|
|
397
|
+
summary?: string;
|
|
398
|
+
/** Detailed description for documentation */
|
|
399
|
+
description?: string;
|
|
400
|
+
/** Tags for grouping in documentation */
|
|
401
|
+
tags?: string[];
|
|
402
|
+
/** Middleware to run before handler */
|
|
403
|
+
middleware?: M;
|
|
404
|
+
/** Request validation schemas */
|
|
405
|
+
request?: {
|
|
406
|
+
/** Path parameters schema */
|
|
407
|
+
params?: P;
|
|
408
|
+
/** Query parameters schema */
|
|
409
|
+
query?: Q;
|
|
410
|
+
/** Request body schema */
|
|
411
|
+
body?: B;
|
|
412
|
+
};
|
|
413
|
+
/** Response schemas by status code */
|
|
414
|
+
responses?: Record<number, z.ZodTypeAny>;
|
|
415
|
+
/**
|
|
416
|
+
* OpenAPI Security Requirements
|
|
417
|
+
* @example [{ bearerAuth: [] }]
|
|
418
|
+
*/
|
|
419
|
+
security?: Array<Record<string, string[]>>;
|
|
420
|
+
/**
|
|
421
|
+
* OpenAPI Operation overrides
|
|
422
|
+
* Allows full customization of the operation (e.g., custom headers, externalDocs)
|
|
423
|
+
*/
|
|
424
|
+
openapi?: Record<string, any>;
|
|
425
|
+
/** Route handler function */
|
|
426
|
+
handler: RouteHandler<P, Q, B, R, M>;
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Catch-all type for arrays of routes
|
|
430
|
+
*/
|
|
431
|
+
type AnyRouteConfig = RouteConfig<any, any, any, any, any>;
|
|
432
|
+
|
|
430
433
|
/**
|
|
431
434
|
* Zero-Dependency Logger
|
|
432
435
|
* Lightweight, colorful, and powerful logging system
|
|
@@ -454,11 +457,6 @@ declare class Logger {
|
|
|
454
457
|
debug(message: string): void;
|
|
455
458
|
}
|
|
456
459
|
|
|
457
|
-
/**
|
|
458
|
-
* Nural Framework
|
|
459
|
-
* Main application class that orchestrates adapters and documentation
|
|
460
|
-
*/
|
|
461
|
-
|
|
462
460
|
/**
|
|
463
461
|
* Nural - The intelligent, schema-first REST framework
|
|
464
462
|
*
|
|
@@ -485,6 +483,7 @@ declare class Nural {
|
|
|
485
483
|
private isExpress;
|
|
486
484
|
logger: Logger;
|
|
487
485
|
constructor(config?: NuralConfig);
|
|
486
|
+
get server(): fastify.RawServerDefault | Server<typeof node_http.IncomingMessage, typeof node_http.ServerResponse>;
|
|
488
487
|
/**
|
|
489
488
|
* Apply CORS and Helmet middleware based on config
|
|
490
489
|
*/
|
|
@@ -496,7 +495,7 @@ declare class Nural {
|
|
|
496
495
|
/**
|
|
497
496
|
* Start the server
|
|
498
497
|
*/
|
|
499
|
-
start(port: number):
|
|
498
|
+
start(port: number): Server;
|
|
500
499
|
/**
|
|
501
500
|
* Setup documentation routes
|
|
502
501
|
*/
|