blaizejs 0.2.0 → 0.2.3
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/README.md +1 -1
- package/dist/index.cjs +1976 -1560
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +731 -22
- package/dist/index.d.ts +731 -22
- package/dist/index.js +1981 -1560
- package/dist/index.js.map +1 -1
- package/package.json +6 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
1
|
+
import http, { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import http2, { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
|
|
3
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { EventEmitter } from 'node:events';
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
|
-
*
|
|
8
|
+
* Compose multiple middleware functions into a single middleware function
|
|
7
9
|
*/
|
|
8
|
-
declare function
|
|
10
|
+
declare function compose(middlewareStack: Middleware[]): MiddlewareFunction;
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
|
-
*
|
|
13
|
+
* Create a middleware
|
|
12
14
|
*/
|
|
13
|
-
declare function
|
|
15
|
+
declare function create$2(handlerOrOptions: MiddlewareFunction | MiddlewareOptions): Middleware;
|
|
14
16
|
|
|
15
17
|
/**
|
|
16
18
|
* Create a plugin with the given name, version, and setup function
|
|
@@ -51,18 +53,725 @@ declare const createOptionsRoute: CreateOptionsRoute;
|
|
|
51
53
|
*/
|
|
52
54
|
declare function create(options?: ServerOptionsInput): Server;
|
|
53
55
|
|
|
56
|
+
/**
|
|
57
|
+
* Unified request type supporting both HTTP/1.1 and HTTP/2
|
|
58
|
+
*/
|
|
59
|
+
type UnifiedRequest = IncomingMessage | Http2ServerRequest;
|
|
60
|
+
/**
|
|
61
|
+
* Unified response type supporting both HTTP/1.1 and HTTP/2
|
|
62
|
+
*/
|
|
63
|
+
type UnifiedResponse = ServerResponse | Http2ServerResponse;
|
|
64
|
+
/**
|
|
65
|
+
* Request parameters extracted from URL path
|
|
66
|
+
*/
|
|
67
|
+
interface RequestParams {
|
|
68
|
+
[key: string]: string;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Query parameters from URL
|
|
72
|
+
*/
|
|
73
|
+
interface QueryParams {
|
|
74
|
+
[key: string]: string | string[] | undefined;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Options for streaming responses
|
|
78
|
+
*/
|
|
79
|
+
interface StreamOptions {
|
|
80
|
+
contentType?: string;
|
|
81
|
+
status?: number;
|
|
82
|
+
headers?: Record<string, string>;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* State container for storing request-scoped data
|
|
86
|
+
* Allows for proper typing with generics
|
|
87
|
+
*/
|
|
88
|
+
interface State {
|
|
89
|
+
[key: string]: unknown;
|
|
90
|
+
}
|
|
91
|
+
interface ContextResponse<S extends State = State> {
|
|
92
|
+
raw: UnifiedResponse;
|
|
93
|
+
sent: boolean;
|
|
94
|
+
status: (code: number) => ContextResponse<S>;
|
|
95
|
+
header: (name: string, value: string) => ContextResponse<S>;
|
|
96
|
+
headers: (headers: Record<string, string>) => ContextResponse<S>;
|
|
97
|
+
type: (contentType: string) => ContextResponse<S>;
|
|
98
|
+
json: (body: unknown, status?: number) => void;
|
|
99
|
+
text: (body: string, status?: number) => void;
|
|
100
|
+
html: (body: string, status?: number) => void;
|
|
101
|
+
redirect: (url: string, status?: number) => void;
|
|
102
|
+
stream: (readable: NodeJS.ReadableStream, options?: StreamOptions) => void;
|
|
103
|
+
}
|
|
104
|
+
interface ContextRequest<TBody = unknown> {
|
|
105
|
+
raw: UnifiedRequest;
|
|
106
|
+
method: string;
|
|
107
|
+
path: string;
|
|
108
|
+
url: URL | null;
|
|
109
|
+
query: QueryParams;
|
|
110
|
+
params: RequestParams;
|
|
111
|
+
protocol: string;
|
|
112
|
+
isHttp2: boolean;
|
|
113
|
+
body?: TBody;
|
|
114
|
+
header: (name: string) => string | undefined;
|
|
115
|
+
headers: (names?: string[]) => Record<string, string | undefined>;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Context object representing a request/response cycle
|
|
119
|
+
*/
|
|
120
|
+
interface Context<S extends State = State, TBody = unknown, TQuery = QueryParams> {
|
|
121
|
+
/**
|
|
122
|
+
* Request information
|
|
123
|
+
*/
|
|
124
|
+
request: Omit<ContextRequest, 'body' | 'query'> & {
|
|
125
|
+
body: TBody;
|
|
126
|
+
query: TQuery;
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* Response handling
|
|
130
|
+
*/
|
|
131
|
+
response: ContextResponse<S>;
|
|
132
|
+
/**
|
|
133
|
+
* Request-scoped state for storing data during the request lifecycle
|
|
134
|
+
*/
|
|
135
|
+
state: S;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Options for creating a context
|
|
139
|
+
*/
|
|
140
|
+
interface ContextOptions {
|
|
141
|
+
/**
|
|
142
|
+
* Whether to parse the request body
|
|
143
|
+
*/
|
|
144
|
+
parseBody?: boolean;
|
|
145
|
+
/**
|
|
146
|
+
* Additional state to merge into the context
|
|
147
|
+
*/
|
|
148
|
+
initialState?: State;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Function to get the current context from AsyncLocalStorage
|
|
152
|
+
*/
|
|
153
|
+
type GetContextFn = <S extends State = State>() => Context<S> | undefined;
|
|
154
|
+
/**
|
|
155
|
+
* Factory function for creating a new context
|
|
156
|
+
*/
|
|
157
|
+
type CreateContextFn = (req: UnifiedRequest, res: UnifiedResponse, options?: ContextOptions) => Promise<Context>;
|
|
158
|
+
/**
|
|
159
|
+
* Type representing unknown function
|
|
160
|
+
*
|
|
161
|
+
* This is a generic function type that can accept any number of arguments
|
|
162
|
+
* and return any type of value. It is used for type inference in various
|
|
163
|
+
* contexts where the specific function signature is not known or not
|
|
164
|
+
* important.
|
|
165
|
+
*/
|
|
166
|
+
type UnknownFunction = (...args: unknown[]) => unknown;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Function to pass control to the next middleware
|
|
170
|
+
*/
|
|
171
|
+
type NextFunction = () => Promise<void> | void;
|
|
172
|
+
/**
|
|
173
|
+
* Middleware function signature
|
|
174
|
+
*/
|
|
175
|
+
type MiddlewareFunction = (ctx: Context, next: NextFunction) => Promise<void> | void;
|
|
176
|
+
/**
|
|
177
|
+
* Named middleware options
|
|
178
|
+
*/
|
|
179
|
+
interface MiddlewareOptions {
|
|
180
|
+
/** Name of the middleware for debugging and logging */
|
|
181
|
+
name?: string;
|
|
182
|
+
/** The middleware handler function */
|
|
183
|
+
handler: MiddlewareFunction;
|
|
184
|
+
/** Skip function to conditionally bypass middleware */
|
|
185
|
+
skip?: ((ctx: Context) => boolean) | undefined;
|
|
186
|
+
/** Enable debugging for this middleware */
|
|
187
|
+
debug?: boolean;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Middleware type
|
|
191
|
+
*/
|
|
192
|
+
interface Middleware {
|
|
193
|
+
name: string;
|
|
194
|
+
execute: MiddlewareFunction;
|
|
195
|
+
skip?: ((ctx: Context) => boolean) | undefined;
|
|
196
|
+
debug?: boolean | undefined;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Helper type to extract TypeScript type from Zod schema
|
|
201
|
+
*/
|
|
202
|
+
type Infer<T> = T extends z.ZodType<infer R> ? R : unknown;
|
|
203
|
+
/**
|
|
204
|
+
* HTTP methods supported by the router
|
|
205
|
+
*/
|
|
206
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
|
|
207
|
+
/**
|
|
208
|
+
* Schema for route validation with generic type parameters
|
|
209
|
+
*/
|
|
210
|
+
interface RouteSchema<P extends z.ZodType = z.ZodType<any>, Q extends z.ZodType = z.ZodType<any>, B extends z.ZodType = z.ZodType<any>, R extends z.ZodType = z.ZodType<any>> {
|
|
211
|
+
/** Parameter schema for validation */
|
|
212
|
+
params?: P;
|
|
213
|
+
/** Query schema for validation */
|
|
214
|
+
query?: Q;
|
|
215
|
+
/** Body schema for validation */
|
|
216
|
+
body?: B;
|
|
217
|
+
/** Response schema for validation */
|
|
218
|
+
response?: R;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Route handler function with strongly typed params and response
|
|
222
|
+
*/
|
|
223
|
+
type RouteHandler<TParams = Record<string, string>, TQuery = Record<string, string | string[] | undefined>, TBody = unknown, TResponse = unknown> = (ctx: Context<State, TBody, TQuery>, params: TParams) => Promise<TResponse> | TResponse;
|
|
224
|
+
/**
|
|
225
|
+
* Options for a route method with schema-based type inference
|
|
226
|
+
*/
|
|
227
|
+
interface RouteMethodOptions<P extends z.ZodType = z.ZodType<any>, Q extends z.ZodType = z.ZodType<any>, B extends z.ZodType = z.ZodType<any>, R extends z.ZodType = z.ZodType<any>> {
|
|
228
|
+
/** Schema for request/response validation */
|
|
229
|
+
schema?: RouteSchema<P, Q, B, R>;
|
|
230
|
+
/** Handler function for the route */
|
|
231
|
+
handler: RouteHandler<P extends z.ZodType ? Infer<P> : Record<string, string>, Q extends z.ZodType ? Infer<Q> : QueryParams, B extends z.ZodType ? Infer<B> : unknown, R extends z.ZodType ? Infer<R> : unknown>;
|
|
232
|
+
/** Middleware to apply to this route */
|
|
233
|
+
middleware?: Middleware[];
|
|
234
|
+
/** Route-specific options */
|
|
235
|
+
options?: Record<string, unknown>;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Route definition mapping HTTP methods to handlers
|
|
239
|
+
*/
|
|
240
|
+
interface RouteDefinition {
|
|
241
|
+
GET?: RouteMethodOptions<any, any, never, any>;
|
|
242
|
+
POST?: RouteMethodOptions<any, any, any, any>;
|
|
243
|
+
PUT?: RouteMethodOptions<any, any, any, any>;
|
|
244
|
+
DELETE?: RouteMethodOptions<any, any, never, any>;
|
|
245
|
+
PATCH?: RouteMethodOptions<any, any, any, any>;
|
|
246
|
+
HEAD?: RouteMethodOptions<any, any, never, any>;
|
|
247
|
+
OPTIONS?: RouteMethodOptions<any, any, never, any>;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Route object with path
|
|
251
|
+
*/
|
|
252
|
+
interface Route extends RouteDefinition {
|
|
253
|
+
/** Path of the route */
|
|
254
|
+
path: string;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Options for route creation
|
|
258
|
+
*/
|
|
259
|
+
interface RouteOptions {
|
|
260
|
+
/** Base path for the route */
|
|
261
|
+
basePath?: string;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Result type for handling success and error responses
|
|
265
|
+
*/
|
|
266
|
+
type Result<T, E = {
|
|
267
|
+
error: string;
|
|
268
|
+
message: string;
|
|
269
|
+
details?: unknown;
|
|
270
|
+
}> = {
|
|
271
|
+
success: true;
|
|
272
|
+
data: T;
|
|
273
|
+
status?: number;
|
|
274
|
+
} | {
|
|
275
|
+
success: false;
|
|
276
|
+
error: E;
|
|
277
|
+
status?: number;
|
|
278
|
+
};
|
|
279
|
+
/**
|
|
280
|
+
* Router options
|
|
281
|
+
*/
|
|
282
|
+
interface RouterOptions {
|
|
283
|
+
/** Directory containing route files */
|
|
284
|
+
routesDir: string;
|
|
285
|
+
/** Base path for all routes */
|
|
286
|
+
basePath?: string;
|
|
287
|
+
/** Watch for file changes in development */
|
|
288
|
+
watchMode?: boolean;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Router interface
|
|
292
|
+
*/
|
|
293
|
+
interface Router {
|
|
294
|
+
/** Handle an incoming request */
|
|
295
|
+
handleRequest: (ctx: Context) => Promise<void>;
|
|
296
|
+
/** Get all registered routes */
|
|
297
|
+
getRoutes: () => Route[];
|
|
298
|
+
/** Add a route programmatically */
|
|
299
|
+
addRoute: (route: Route) => void;
|
|
300
|
+
/** Add multiple routes programmatically with batch processing */
|
|
301
|
+
addRoutes: (routes: Route[]) => {
|
|
302
|
+
added: Route[];
|
|
303
|
+
removed: string[];
|
|
304
|
+
changed: Route[];
|
|
305
|
+
};
|
|
306
|
+
/** Add a route directory for plugins */
|
|
307
|
+
addRouteDirectory(directory: string, options?: {
|
|
308
|
+
prefix?: string;
|
|
309
|
+
}): Promise<void>;
|
|
310
|
+
/** Get route conflicts */
|
|
311
|
+
getRouteConflicts(): Array<{
|
|
312
|
+
path: string;
|
|
313
|
+
sources: string[];
|
|
314
|
+
}>;
|
|
315
|
+
/** Close watchers and cleanup resources */
|
|
316
|
+
close?: () => Promise<void>;
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Route match result
|
|
320
|
+
*/
|
|
321
|
+
interface RouteMatch {
|
|
322
|
+
/** The matched route handler (null if method not allowed) */
|
|
323
|
+
route: RouteMethodOptions | null;
|
|
324
|
+
/** Extracted route parameters */
|
|
325
|
+
params: Record<string, string>;
|
|
326
|
+
/** Flag indicating if the path exists but method isn't allowed */
|
|
327
|
+
methodNotAllowed?: boolean;
|
|
328
|
+
/** List of allowed methods for this path (when method not allowed) */
|
|
329
|
+
allowedMethods?: HttpMethod[];
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Route matcher interface
|
|
333
|
+
*/
|
|
334
|
+
interface Matcher {
|
|
335
|
+
/** Add a route to the matcher */
|
|
336
|
+
add: (path: string, method: HttpMethod, route: RouteMethodOptions) => void;
|
|
337
|
+
/** Match a URL path to a route */
|
|
338
|
+
match: (path: string, method: HttpMethod) => RouteMatch | null;
|
|
339
|
+
/** Get all registered routes */
|
|
340
|
+
getRoutes: () => {
|
|
341
|
+
path: string;
|
|
342
|
+
method: HttpMethod;
|
|
343
|
+
}[];
|
|
344
|
+
/** Find routes matching a specific path */
|
|
345
|
+
findRoutes: (path: string) => {
|
|
346
|
+
path: string;
|
|
347
|
+
method: HttpMethod;
|
|
348
|
+
params: Record<string, string>;
|
|
349
|
+
}[];
|
|
350
|
+
/** Remove a route from the matcher (optional for compatibility) */
|
|
351
|
+
remove: (path: string) => void;
|
|
352
|
+
/** Clear all routes from the matcher (optional for compatibility) */
|
|
353
|
+
clear: () => void;
|
|
354
|
+
}
|
|
355
|
+
interface ParsedRoute {
|
|
356
|
+
filePath: string;
|
|
357
|
+
routePath: string;
|
|
358
|
+
params: string[];
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Node in the radix tree for efficient route matching
|
|
362
|
+
*/
|
|
363
|
+
interface RouteNode {
|
|
364
|
+
segment: string;
|
|
365
|
+
paramName: string | null;
|
|
366
|
+
isWildcard: boolean;
|
|
367
|
+
children: RouteNode[];
|
|
368
|
+
handlers: Partial<Record<HttpMethod, RouteMethodOptions>>;
|
|
369
|
+
pattern: RegExp | null;
|
|
370
|
+
}
|
|
371
|
+
interface RouteEntry {
|
|
372
|
+
/** The route path pattern */
|
|
373
|
+
path: string;
|
|
374
|
+
/** The HTTP method */
|
|
375
|
+
method: HttpMethod;
|
|
376
|
+
/** The compiled regex pattern */
|
|
377
|
+
pattern: RegExp;
|
|
378
|
+
/** The parameter names in order */
|
|
379
|
+
paramNames: string[];
|
|
380
|
+
/** The route handler options */
|
|
381
|
+
routeOptions: RouteMethodOptions;
|
|
382
|
+
}
|
|
383
|
+
interface ErrorHandlerOptions {
|
|
384
|
+
/** Show detailed errors in response */
|
|
385
|
+
detailed?: boolean;
|
|
386
|
+
/** Log errors to console */
|
|
387
|
+
log?: boolean;
|
|
388
|
+
}
|
|
389
|
+
interface ProcessResponseOptions {
|
|
390
|
+
/** Status code to use if not specified */
|
|
391
|
+
defaultStatus?: number;
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Standard error response structure
|
|
395
|
+
*/
|
|
396
|
+
interface StandardErrorResponse {
|
|
397
|
+
error: string;
|
|
398
|
+
message: string;
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* GET route creator - no body schema needed
|
|
402
|
+
*/
|
|
403
|
+
type CreateGetRoute = <P extends z.ZodType = z.ZodType<any>, Q extends z.ZodType = z.ZodType<any>, R extends z.ZodType = z.ZodType<any>>(config: {
|
|
404
|
+
schema?: {
|
|
405
|
+
params?: P;
|
|
406
|
+
query?: Q;
|
|
407
|
+
response?: R;
|
|
408
|
+
};
|
|
409
|
+
handler: RouteHandler<P extends z.ZodType ? Infer<P> : Record<string, string>, Q extends z.ZodType ? Infer<Q> : QueryParams, never, R extends z.ZodType ? Infer<R> : unknown>;
|
|
410
|
+
middleware?: Middleware[];
|
|
411
|
+
options?: Record<string, unknown>;
|
|
412
|
+
}) => {
|
|
413
|
+
GET: RouteMethodOptions<P, Q, never, R>;
|
|
414
|
+
path: string;
|
|
415
|
+
};
|
|
416
|
+
/**
|
|
417
|
+
* POST route creator - includes body schema
|
|
418
|
+
*/
|
|
419
|
+
type CreatePostRoute = <P extends z.ZodType = z.ZodType<any>, Q extends z.ZodType = z.ZodType<any>, B extends z.ZodType = z.ZodType<any>, R extends z.ZodType = z.ZodType<any>>(config: {
|
|
420
|
+
schema?: {
|
|
421
|
+
params?: P;
|
|
422
|
+
query?: Q;
|
|
423
|
+
body?: B;
|
|
424
|
+
response?: R;
|
|
425
|
+
};
|
|
426
|
+
handler: RouteHandler<P extends z.ZodType ? Infer<P> : Record<string, string>, Q extends z.ZodType ? Infer<Q> : QueryParams, B extends z.ZodType ? Infer<B> : unknown, R extends z.ZodType ? Infer<R> : unknown>;
|
|
427
|
+
middleware?: Middleware[];
|
|
428
|
+
options?: Record<string, unknown>;
|
|
429
|
+
}) => {
|
|
430
|
+
POST: RouteMethodOptions<P, Q, B, R>;
|
|
431
|
+
path: string;
|
|
432
|
+
};
|
|
433
|
+
/**
|
|
434
|
+
* PUT route creator - includes body schema
|
|
435
|
+
*/
|
|
436
|
+
type CreatePutRoute = <P extends z.ZodType = z.ZodType<any>, Q extends z.ZodType = z.ZodType<any>, B extends z.ZodType = z.ZodType<any>, R extends z.ZodType = z.ZodType<any>>(config: {
|
|
437
|
+
schema?: {
|
|
438
|
+
params?: P;
|
|
439
|
+
query?: Q;
|
|
440
|
+
body?: B;
|
|
441
|
+
response?: R;
|
|
442
|
+
};
|
|
443
|
+
handler: RouteHandler<P extends z.ZodType ? Infer<P> : Record<string, string>, Q extends z.ZodType ? Infer<Q> : QueryParams, B extends z.ZodType ? Infer<B> : unknown, R extends z.ZodType ? Infer<R> : unknown>;
|
|
444
|
+
middleware?: Middleware[];
|
|
445
|
+
options?: Record<string, unknown>;
|
|
446
|
+
}) => {
|
|
447
|
+
PUT: RouteMethodOptions<P, Q, B, R>;
|
|
448
|
+
path: string;
|
|
449
|
+
};
|
|
450
|
+
/**
|
|
451
|
+
* DELETE route creator - typically no body
|
|
452
|
+
*/
|
|
453
|
+
type CreateDeleteRoute = <P extends z.ZodType = z.ZodType<any>, Q extends z.ZodType = z.ZodType<any>, R extends z.ZodType = z.ZodType<any>>(config: {
|
|
454
|
+
schema?: {
|
|
455
|
+
params?: P;
|
|
456
|
+
query?: Q;
|
|
457
|
+
response?: R;
|
|
458
|
+
};
|
|
459
|
+
handler: RouteHandler<P extends z.ZodType ? Infer<P> : Record<string, string>, Q extends z.ZodType ? Infer<Q> : QueryParams, never, R extends z.ZodType ? Infer<R> : unknown>;
|
|
460
|
+
middleware?: Middleware[];
|
|
461
|
+
options?: Record<string, unknown>;
|
|
462
|
+
}) => {
|
|
463
|
+
DELETE: RouteMethodOptions<P, Q, never, R>;
|
|
464
|
+
path: string;
|
|
465
|
+
};
|
|
466
|
+
/**
|
|
467
|
+
* PATCH route creator - includes body schema
|
|
468
|
+
*/
|
|
469
|
+
type CreatePatchRoute = <P extends z.ZodType = z.ZodType<any>, Q extends z.ZodType = z.ZodType<any>, B extends z.ZodType = z.ZodType<any>, R extends z.ZodType = z.ZodType<any>>(config: {
|
|
470
|
+
schema?: {
|
|
471
|
+
params?: P;
|
|
472
|
+
query?: Q;
|
|
473
|
+
body?: B;
|
|
474
|
+
response?: R;
|
|
475
|
+
};
|
|
476
|
+
handler: RouteHandler<P extends z.ZodType ? Infer<P> : Record<string, string>, Q extends z.ZodType ? Infer<Q> : QueryParams, B extends z.ZodType ? Infer<B> : unknown, R extends z.ZodType ? Infer<R> : unknown>;
|
|
477
|
+
middleware?: Middleware[];
|
|
478
|
+
options?: Record<string, unknown>;
|
|
479
|
+
}) => {
|
|
480
|
+
PATCH: RouteMethodOptions<P, Q, B, R>;
|
|
481
|
+
path: string;
|
|
482
|
+
};
|
|
483
|
+
/**
|
|
484
|
+
* HEAD route creator - no body schema needed (same as GET)
|
|
485
|
+
*/
|
|
486
|
+
type CreateHeadRoute = <P extends z.ZodType = z.ZodType<any>, Q extends z.ZodType = z.ZodType<any>, R extends z.ZodType = z.ZodType<any>>(config: {
|
|
487
|
+
schema?: {
|
|
488
|
+
params?: P;
|
|
489
|
+
query?: Q;
|
|
490
|
+
response?: R;
|
|
491
|
+
};
|
|
492
|
+
handler: RouteHandler<P extends z.ZodType ? Infer<P> : Record<string, string>, Q extends z.ZodType ? Infer<Q> : QueryParams, never, R extends z.ZodType ? Infer<R> : unknown>;
|
|
493
|
+
middleware?: Middleware[];
|
|
494
|
+
options?: Record<string, unknown>;
|
|
495
|
+
}) => {
|
|
496
|
+
HEAD: RouteMethodOptions<P, Q, never, R>;
|
|
497
|
+
path: string;
|
|
498
|
+
};
|
|
499
|
+
/**
|
|
500
|
+
* OPTIONS route creator - typically no body or response schema
|
|
501
|
+
*/
|
|
502
|
+
type CreateOptionsRoute = <P extends z.ZodType = z.ZodType<any>, Q extends z.ZodType = z.ZodType<any>, R extends z.ZodType = z.ZodType<any>>(config: {
|
|
503
|
+
schema?: {
|
|
504
|
+
params?: P;
|
|
505
|
+
query?: Q;
|
|
506
|
+
response?: R;
|
|
507
|
+
};
|
|
508
|
+
handler: RouteHandler<P extends z.ZodType ? Infer<P> : Record<string, string>, Q extends z.ZodType ? Infer<Q> : QueryParams, never, R extends z.ZodType ? Infer<R> : unknown>;
|
|
509
|
+
middleware?: Middleware[];
|
|
510
|
+
options?: Record<string, unknown>;
|
|
511
|
+
}) => {
|
|
512
|
+
OPTIONS: RouteMethodOptions<P, Q, never, R>;
|
|
513
|
+
path: string;
|
|
514
|
+
};
|
|
515
|
+
interface FileCache {
|
|
516
|
+
routes: Route[];
|
|
517
|
+
timestamp: number;
|
|
518
|
+
hash: string;
|
|
519
|
+
}
|
|
520
|
+
interface ReloadMetrics {
|
|
521
|
+
fileChanges: number;
|
|
522
|
+
totalReloadTime: number;
|
|
523
|
+
averageReloadTime: number;
|
|
524
|
+
slowReloads: Array<{
|
|
525
|
+
file: string;
|
|
526
|
+
time: number;
|
|
527
|
+
}>;
|
|
528
|
+
}
|
|
529
|
+
interface WatchOptions {
|
|
530
|
+
debounceMs?: number;
|
|
531
|
+
/** Directories to ignore */
|
|
532
|
+
ignore?: string[];
|
|
533
|
+
/** Callback for new routes */
|
|
534
|
+
onRouteAdded?: (filePath: string, routes: Route[]) => void;
|
|
535
|
+
/** Callback for changed routes */
|
|
536
|
+
onRouteChanged?: (filePath: string, routes: Route[]) => void;
|
|
537
|
+
/** Callback for removed routes */
|
|
538
|
+
onRouteRemoved?: (filePath: string, routes: Route[]) => void;
|
|
539
|
+
/** Callback for errors */
|
|
540
|
+
onError?: (error: Error) => void;
|
|
541
|
+
}
|
|
542
|
+
interface RouteRegistry {
|
|
543
|
+
routesByPath: Map<string, Route>;
|
|
544
|
+
routesByFile: Map<string, Set<string>>;
|
|
545
|
+
pathToFile: Map<string, string>;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
type ExtractParams<T> = T extends RouteMethodOptions<infer P, any, any, any> ? P extends z.ZodType ? Infer<P> : Record<string, string> : never;
|
|
549
|
+
type ExtractQuery<T> = T extends RouteMethodOptions<any, infer Q, any, any> ? Q extends z.ZodType ? Infer<Q> : Record<string, string | string[] | undefined> : never;
|
|
550
|
+
type ExtractBody<T> = T extends RouteMethodOptions<any, any, infer B, any> ? B extends z.ZodType ? Infer<B> : unknown : never;
|
|
551
|
+
type ExtractResponse<T> = T extends RouteMethodOptions<any, any, any, infer R> ? R extends z.ZodType ? Infer<R> : unknown : never;
|
|
552
|
+
type ExtractMethod<T> = T extends {
|
|
553
|
+
GET: any;
|
|
554
|
+
} ? 'GET' : T extends {
|
|
555
|
+
POST: any;
|
|
556
|
+
} ? 'POST' : T extends {
|
|
557
|
+
PUT: any;
|
|
558
|
+
} ? 'PUT' : T extends {
|
|
559
|
+
DELETE: any;
|
|
560
|
+
} ? 'DELETE' : T extends {
|
|
561
|
+
PATCH: any;
|
|
562
|
+
} ? 'PATCH' : T extends {
|
|
563
|
+
HEAD: any;
|
|
564
|
+
} ? 'HEAD' : T extends {
|
|
565
|
+
OPTIONS: any;
|
|
566
|
+
} ? 'OPTIONS' : never;
|
|
567
|
+
type BuildRoutesRegistry<TRoutes extends Record<string, any>> = {
|
|
568
|
+
[Method in ExtractMethod<TRoutes[keyof TRoutes]> as `$${Lowercase<Method>}`]: {
|
|
569
|
+
[K in keyof TRoutes as ExtractMethod<TRoutes[K]> extends Method ? K : never]: TRoutes[K];
|
|
570
|
+
};
|
|
571
|
+
};
|
|
572
|
+
type GetRouteMethod<TRoute> = TRoute extends {
|
|
573
|
+
path: string;
|
|
574
|
+
} ? Omit<TRoute, 'path'>[keyof Omit<TRoute, 'path'>] : never;
|
|
575
|
+
type CreateClientMethod<TRoute> = GetRouteMethod<TRoute> extends RouteMethodOptions<any, any, any, any> ? (args?: {
|
|
576
|
+
params?: ExtractParams<GetRouteMethod<TRoute>>;
|
|
577
|
+
query?: ExtractQuery<GetRouteMethod<TRoute>>;
|
|
578
|
+
body?: ExtractBody<GetRouteMethod<TRoute>>;
|
|
579
|
+
}) => Promise<ExtractResponse<GetRouteMethod<TRoute>>> : never;
|
|
580
|
+
type CreateClient<TRoutes extends Record<string, Record<string, any>>> = {
|
|
581
|
+
[Method in keyof TRoutes]: {
|
|
582
|
+
[RouteName in keyof TRoutes[Method]]: CreateClientMethod<TRoutes[Method][RouteName]>;
|
|
583
|
+
};
|
|
584
|
+
};
|
|
585
|
+
interface ClientConfig {
|
|
586
|
+
baseUrl: string;
|
|
587
|
+
defaultHeaders?: Record<string, string>;
|
|
588
|
+
timeout?: number;
|
|
589
|
+
}
|
|
590
|
+
interface RequestArgs {
|
|
591
|
+
params?: Record<string, string>;
|
|
592
|
+
query?: Record<string, any>;
|
|
593
|
+
body?: unknown;
|
|
594
|
+
}
|
|
595
|
+
interface RequestOptions {
|
|
596
|
+
method: string;
|
|
597
|
+
url: string;
|
|
598
|
+
headers: Record<string, string>;
|
|
599
|
+
body?: string;
|
|
600
|
+
timeout: number;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* BlaizeJS Plugin Module
|
|
605
|
+
*
|
|
606
|
+
* Provides the plugin system for extending framework functionality.
|
|
607
|
+
*/
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* Plugin options
|
|
611
|
+
*/
|
|
612
|
+
interface PluginOptions<_T = any> {
|
|
613
|
+
/** Plugin configuration */
|
|
614
|
+
[key: string]: any;
|
|
615
|
+
}
|
|
616
|
+
/**
|
|
617
|
+
* Plugin lifecycle hooks
|
|
618
|
+
*/
|
|
619
|
+
interface PluginHooks {
|
|
620
|
+
/** Called when the plugin is registered */
|
|
621
|
+
register: (app: Server) => void | Promise<void>;
|
|
622
|
+
/** Called when the server is initialized */
|
|
623
|
+
initialize?: (app?: Server) => void | Promise<void>;
|
|
624
|
+
/** Called when the server is terminated */
|
|
625
|
+
terminate?: (app?: Server) => void | Promise<void>;
|
|
626
|
+
/** Called when the server starts */
|
|
627
|
+
onServerStart?: (server: any) => void | Promise<void>;
|
|
628
|
+
/** Called when the server stops */
|
|
629
|
+
onServerStop?: (server: any) => void | Promise<void>;
|
|
630
|
+
}
|
|
631
|
+
/**
|
|
632
|
+
* Plugin interface
|
|
633
|
+
*/
|
|
634
|
+
interface Plugin extends PluginHooks {
|
|
635
|
+
/** Plugin name */
|
|
636
|
+
name: string;
|
|
637
|
+
/** Plugin version */
|
|
638
|
+
version: string;
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* Plugin factory function
|
|
642
|
+
*/
|
|
643
|
+
type PluginFactory<T = any> = (options?: T) => Plugin;
|
|
644
|
+
interface PluginLifecycleManager {
|
|
645
|
+
initializePlugins(server: Server): Promise<void>;
|
|
646
|
+
terminatePlugins(server: Server): Promise<void>;
|
|
647
|
+
onServerStart(server: Server, httpServer: any): Promise<void>;
|
|
648
|
+
onServerStop(server: Server, httpServer: any): Promise<void>;
|
|
649
|
+
}
|
|
650
|
+
interface PluginLifecycleOptions {
|
|
651
|
+
/** Continue initialization even if a plugin fails */
|
|
652
|
+
continueOnError?: boolean;
|
|
653
|
+
/** Log plugin lifecycle events */
|
|
654
|
+
debug?: boolean;
|
|
655
|
+
/** Custom error handler for plugin failures */
|
|
656
|
+
onError?: (plugin: Plugin, phase: string, error: Error) => void;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* BlaizeJS Server Module
|
|
661
|
+
*
|
|
662
|
+
* Provides the core HTTP/2 server implementation with HTTP/1.1 fallback.
|
|
663
|
+
*/
|
|
664
|
+
|
|
665
|
+
interface Http2Options {
|
|
666
|
+
enabled?: boolean | undefined;
|
|
667
|
+
keyFile?: string | undefined;
|
|
668
|
+
certFile?: string | undefined;
|
|
669
|
+
}
|
|
670
|
+
interface StartOptions {
|
|
671
|
+
port?: number;
|
|
672
|
+
host?: string;
|
|
673
|
+
}
|
|
674
|
+
interface StopOptions {
|
|
675
|
+
timeout?: number;
|
|
676
|
+
plugins?: Plugin[];
|
|
677
|
+
onStopping?: () => Promise<void> | void;
|
|
678
|
+
onStopped?: () => Promise<void> | void;
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* Server options for configuring the BlaizeJS server
|
|
682
|
+
*/
|
|
683
|
+
interface ServerOptionsInput {
|
|
684
|
+
/** Port to listen on (default: 3000) */
|
|
685
|
+
port?: number;
|
|
686
|
+
/** Host to bind to (default: localhost) */
|
|
687
|
+
host?: string;
|
|
688
|
+
/** Directory containing route files (default: ./routes) */
|
|
689
|
+
routesDir?: string;
|
|
690
|
+
/** HTTP/2 options */
|
|
691
|
+
http2?: {
|
|
692
|
+
/** Enable HTTP/2 (default: true) */
|
|
693
|
+
enabled?: boolean | undefined;
|
|
694
|
+
/** Path to key file for HTTPS/HTTP2 */
|
|
695
|
+
keyFile?: string | undefined;
|
|
696
|
+
/** Path to certificate file for HTTPS/HTTP2 */
|
|
697
|
+
certFile?: string | undefined;
|
|
698
|
+
};
|
|
699
|
+
/** Global middleware to apply to all routes */
|
|
700
|
+
middleware?: Middleware[];
|
|
701
|
+
/** Plugins to register */
|
|
702
|
+
plugins?: Plugin[];
|
|
703
|
+
}
|
|
704
|
+
/**
|
|
705
|
+
* Configuration for a BlaizeJS server
|
|
706
|
+
*/
|
|
707
|
+
interface ServerOptions {
|
|
708
|
+
/** Port to listen on (default: 3000) */
|
|
709
|
+
port: number;
|
|
710
|
+
/** Host to bind to (default: localhost) */
|
|
711
|
+
host: string;
|
|
712
|
+
/** Directory containing route files (default: ./routes) */
|
|
713
|
+
routesDir: string;
|
|
714
|
+
/** HTTP/2 options */
|
|
715
|
+
http2?: {
|
|
716
|
+
/** Enable HTTP/2 (default: true) */
|
|
717
|
+
enabled?: boolean | undefined;
|
|
718
|
+
/** Path to key file for HTTPS/HTTP2 */
|
|
719
|
+
keyFile?: string | undefined;
|
|
720
|
+
/** Path to certificate file for HTTPS/HTTP2 */
|
|
721
|
+
certFile?: string | undefined;
|
|
722
|
+
};
|
|
723
|
+
/** Global middleware to apply to all routes */
|
|
724
|
+
middleware?: Middleware[];
|
|
725
|
+
/** Plugins to register */
|
|
726
|
+
plugins?: Plugin[];
|
|
727
|
+
}
|
|
728
|
+
/**
|
|
729
|
+
* BlaizeJS Server instance
|
|
730
|
+
*/
|
|
731
|
+
interface Server {
|
|
732
|
+
/** The underlying HTTP or HTTP/2 server */
|
|
733
|
+
server: http.Server | http2.Http2Server | undefined;
|
|
734
|
+
/** The port the server is configured to listen on */
|
|
735
|
+
port: number;
|
|
736
|
+
/** The host the server is bound to */
|
|
737
|
+
host: string;
|
|
738
|
+
events: EventEmitter;
|
|
739
|
+
/** Direct access to registered plugins */
|
|
740
|
+
plugins: Plugin[];
|
|
741
|
+
/** Direct access to registered plugins */
|
|
742
|
+
middleware: Middleware[];
|
|
743
|
+
/** Internal property for signal hanlders */
|
|
744
|
+
_signalHandlers?: {
|
|
745
|
+
unregister: () => void;
|
|
746
|
+
};
|
|
747
|
+
/** Start the server and listen for connections */
|
|
748
|
+
listen: (port?: number, host?: string) => Promise<Server>;
|
|
749
|
+
/** Stop the server */
|
|
750
|
+
close: (stopOptions?: StopOptions) => Promise<void>;
|
|
751
|
+
/** Add global middleware */
|
|
752
|
+
use: (middleware: Middleware | Middleware[]) => Server;
|
|
753
|
+
/** Register a plugin */
|
|
754
|
+
register: (plugin: Plugin) => Promise<Server>;
|
|
755
|
+
/** Access to the routing system */
|
|
756
|
+
router: Router;
|
|
757
|
+
/** Context storage system */
|
|
758
|
+
context: AsyncLocalStorage<Context>;
|
|
759
|
+
pluginManager: PluginLifecycleManager;
|
|
760
|
+
}
|
|
761
|
+
type RequestHandler = (req: http.IncomingMessage | http2.Http2ServerRequest, res: http.ServerResponse | http2.Http2ServerResponse) => Promise<void>;
|
|
762
|
+
|
|
54
763
|
declare const VERSION = "0.1.0";
|
|
55
764
|
declare const ServerAPI: {
|
|
56
765
|
createServer: typeof create;
|
|
57
766
|
};
|
|
58
767
|
declare const RouterAPI: {
|
|
59
|
-
createDeleteRoute:
|
|
60
|
-
createGetRoute:
|
|
61
|
-
createHeadRoute:
|
|
62
|
-
createOptionsRoute:
|
|
63
|
-
createPatchRoute:
|
|
64
|
-
createPostRoute:
|
|
65
|
-
createPutRoute:
|
|
768
|
+
createDeleteRoute: CreateDeleteRoute;
|
|
769
|
+
createGetRoute: CreateGetRoute;
|
|
770
|
+
createHeadRoute: CreateHeadRoute;
|
|
771
|
+
createOptionsRoute: CreateOptionsRoute;
|
|
772
|
+
createPatchRoute: CreatePatchRoute;
|
|
773
|
+
createPostRoute: CreatePostRoute;
|
|
774
|
+
createPutRoute: CreatePutRoute;
|
|
66
775
|
};
|
|
67
776
|
declare const MiddlewareAPI: {
|
|
68
777
|
createMiddleware: typeof create$2;
|
|
@@ -79,13 +788,13 @@ declare const Blaize: {
|
|
|
79
788
|
createServer: typeof create;
|
|
80
789
|
};
|
|
81
790
|
Router: {
|
|
82
|
-
createDeleteRoute:
|
|
83
|
-
createGetRoute:
|
|
84
|
-
createHeadRoute:
|
|
85
|
-
createOptionsRoute:
|
|
86
|
-
createPatchRoute:
|
|
87
|
-
createPostRoute:
|
|
88
|
-
createPutRoute:
|
|
791
|
+
createDeleteRoute: CreateDeleteRoute;
|
|
792
|
+
createGetRoute: CreateGetRoute;
|
|
793
|
+
createHeadRoute: CreateHeadRoute;
|
|
794
|
+
createOptionsRoute: CreateOptionsRoute;
|
|
795
|
+
createPatchRoute: CreatePatchRoute;
|
|
796
|
+
createPostRoute: CreatePostRoute;
|
|
797
|
+
createPutRoute: CreatePutRoute;
|
|
89
798
|
};
|
|
90
799
|
Middleware: {
|
|
91
800
|
createMiddleware: typeof create$2;
|
|
@@ -97,4 +806,4 @@ declare const Blaize: {
|
|
|
97
806
|
VERSION: string;
|
|
98
807
|
};
|
|
99
808
|
|
|
100
|
-
export { Blaize, MiddlewareAPI, PluginsAPI, RouterAPI, ServerAPI, VERSION, compose, createDeleteRoute, createGetRoute, createHeadRoute, create$2 as createMiddleware, createOptionsRoute, createPatchRoute, create$1 as createPlugin, createPostRoute, createPutRoute, create as createServer, Blaize as default };
|
|
809
|
+
export { Blaize, type BuildRoutesRegistry, type ClientConfig, type Context, type ContextOptions, type ContextRequest, type ContextResponse, type CreateClient, type CreateContextFn, type CreateDeleteRoute, type CreateGetRoute, type CreateHeadRoute, type CreateOptionsRoute, type CreatePatchRoute, type CreatePostRoute, type CreatePutRoute, type ErrorHandlerOptions, type ExtractBody, type ExtractMethod, type ExtractParams, type ExtractQuery, type ExtractResponse, type FileCache, type GetContextFn, type Http2Options, type HttpMethod, type Infer, type Matcher, type Middleware, MiddlewareAPI, type MiddlewareFunction, type MiddlewareOptions, type NextFunction, type ParsedRoute, type Plugin, type PluginFactory, type PluginHooks, type PluginLifecycleManager, type PluginLifecycleOptions, type PluginOptions, PluginsAPI, type ProcessResponseOptions, type QueryParams, type ReloadMetrics, type RequestArgs, type RequestHandler, type RequestOptions, type RequestParams, type Result, type Route, type RouteDefinition, type RouteEntry, type RouteHandler, type RouteMatch, type RouteMethodOptions, type RouteNode, type RouteOptions, type RouteRegistry, type RouteSchema, type Router, RouterAPI, type RouterOptions, type Server, ServerAPI, type ServerOptions, type ServerOptionsInput, type StandardErrorResponse, type StartOptions, type State, type StopOptions, type StreamOptions, type UnifiedRequest, type UnifiedResponse, type UnknownFunction, VERSION, type WatchOptions, compose, createDeleteRoute, createGetRoute, createHeadRoute, create$2 as createMiddleware, createOptionsRoute, createPatchRoute, create$1 as createPlugin, createPostRoute, createPutRoute, create as createServer, Blaize as default };
|