adorn-api 1.1.11 → 1.1.13
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 +18 -0
- package/dist/adapter/express/types.d.ts +3 -46
- package/dist/adapter/fastify/coercion.d.ts +12 -0
- package/dist/adapter/fastify/coercion.js +289 -0
- package/dist/adapter/fastify/controllers.d.ts +7 -0
- package/dist/adapter/fastify/controllers.js +201 -0
- package/dist/adapter/fastify/index.d.ts +14 -0
- package/dist/adapter/fastify/index.js +67 -0
- package/dist/adapter/fastify/multipart.d.ts +26 -0
- package/dist/adapter/fastify/multipart.js +75 -0
- package/dist/adapter/fastify/openapi.d.ts +10 -0
- package/dist/adapter/fastify/openapi.js +76 -0
- package/dist/adapter/fastify/response-serializer.d.ts +2 -0
- package/dist/adapter/fastify/response-serializer.js +162 -0
- package/dist/adapter/fastify/types.d.ts +100 -0
- package/dist/adapter/fastify/types.js +2 -0
- package/dist/adapter/metal-orm/index.d.ts +1 -1
- package/dist/adapter/metal-orm/types.d.ts +23 -0
- package/dist/adapter/native/coercion.d.ts +12 -0
- package/dist/adapter/native/coercion.js +289 -0
- package/dist/adapter/native/controllers.d.ts +17 -0
- package/dist/adapter/native/controllers.js +215 -0
- package/dist/adapter/native/index.d.ts +14 -0
- package/dist/adapter/native/index.js +127 -0
- package/dist/adapter/native/openapi.d.ts +7 -0
- package/dist/adapter/native/openapi.js +82 -0
- package/dist/adapter/native/response-serializer.d.ts +5 -0
- package/dist/adapter/native/response-serializer.js +160 -0
- package/dist/adapter/native/router.d.ts +25 -0
- package/dist/adapter/native/router.js +68 -0
- package/dist/adapter/native/types.d.ts +77 -0
- package/dist/adapter/native/types.js +2 -0
- package/dist/core/auth.d.ts +11 -12
- package/dist/core/auth.js +2 -2
- package/dist/core/logger.d.ts +3 -4
- package/dist/core/logger.js +2 -2
- package/dist/core/streaming.d.ts +10 -10
- package/dist/core/streaming.js +31 -19
- package/dist/core/types.d.ts +102 -0
- package/dist/index.d.ts +6 -1
- package/dist/index.js +16 -1
- package/examples/fastify/app.ts +16 -0
- package/examples/fastify/index.ts +21 -0
- package/package.json +24 -18
- package/src/adapter/express/controllers.ts +249 -249
- package/src/adapter/express/types.ts +121 -160
- package/src/adapter/fastify/coercion.ts +369 -0
- package/src/adapter/fastify/controllers.ts +255 -0
- package/src/adapter/fastify/index.ts +53 -0
- package/src/adapter/fastify/multipart.ts +94 -0
- package/src/adapter/fastify/openapi.ts +93 -0
- package/src/adapter/fastify/response-serializer.ts +179 -0
- package/src/adapter/fastify/types.ts +119 -0
- package/src/adapter/metal-orm/index.ts +3 -0
- package/src/adapter/metal-orm/types.ts +25 -0
- package/src/adapter/native/coercion.ts +369 -0
- package/src/adapter/native/controllers.ts +271 -0
- package/src/adapter/native/index.ts +116 -0
- package/src/adapter/native/openapi.ts +109 -0
- package/src/adapter/native/response-serializer.ts +177 -0
- package/src/adapter/native/router.ts +90 -0
- package/src/adapter/native/types.ts +96 -0
- package/src/core/auth.ts +314 -315
- package/src/core/health.ts +234 -235
- package/src/core/logger.ts +245 -247
- package/src/core/streaming.ts +342 -330
- package/src/core/types.ts +115 -0
- package/src/index.ts +46 -16
- package/tests/e2e/fastify.e2e.test.ts +174 -0
- package/tests/native.test.ts +191 -0
- package/tests/typecheck/query-params.typecheck.ts +42 -0
- package/tests/unit/openapi-parameters.test.ts +97 -97
- package/tsconfig.json +14 -13
- package/tsconfig.typecheck.json +8 -0
- package/vitest.config.ts +47 -7
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { HttpMethod } from "../../core/types";
|
|
2
|
+
import type { RouteMeta } from "../../core/metadata";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Result of a route match.
|
|
6
|
+
*/
|
|
7
|
+
export interface RouteMatch {
|
|
8
|
+
controller: any;
|
|
9
|
+
route: RouteMeta;
|
|
10
|
+
params: Record<string, string>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A simple router for matching URL paths to routes.
|
|
15
|
+
*/
|
|
16
|
+
export class Router {
|
|
17
|
+
private routes: Array<{
|
|
18
|
+
method: HttpMethod;
|
|
19
|
+
pattern: RegExp;
|
|
20
|
+
paramNames: string[];
|
|
21
|
+
controller: any;
|
|
22
|
+
route: RouteMeta;
|
|
23
|
+
}> = [];
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Registers a route in the router.
|
|
27
|
+
*/
|
|
28
|
+
add(controller: any, route: RouteMeta, basePath: string): void {
|
|
29
|
+
const fullPath = this.joinPaths(basePath, route.path);
|
|
30
|
+
const { pattern, paramNames } = this.compilePath(fullPath);
|
|
31
|
+
this.routes.push({
|
|
32
|
+
method: route.httpMethod,
|
|
33
|
+
pattern,
|
|
34
|
+
paramNames,
|
|
35
|
+
controller,
|
|
36
|
+
route
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Matches an incoming request to a registered route.
|
|
42
|
+
*/
|
|
43
|
+
match(method: string, path: string): RouteMatch | undefined {
|
|
44
|
+
const normalizedMethod = method.toLowerCase() as HttpMethod;
|
|
45
|
+
const url = new URL(path, "http://localhost");
|
|
46
|
+
const pathname = url.pathname;
|
|
47
|
+
|
|
48
|
+
for (const entry of this.routes) {
|
|
49
|
+
if (entry.method !== normalizedMethod) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const match = entry.pattern.exec(pathname);
|
|
54
|
+
if (match) {
|
|
55
|
+
const params: Record<string, string> = {};
|
|
56
|
+
for (let i = 0; i < entry.paramNames.length; i++) {
|
|
57
|
+
params[entry.paramNames[i]] = decodeURIComponent(match[i + 1]);
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
controller: entry.controller,
|
|
61
|
+
route: entry.route,
|
|
62
|
+
params
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private joinPaths(base: string, path: string): string {
|
|
71
|
+
const normalizedBase = base.replace(/\/+$/, "");
|
|
72
|
+
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
|
|
73
|
+
return `${normalizedBase}${normalizedPath}` || "/";
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private compilePath(path: string): { pattern: RegExp; paramNames: string[] } {
|
|
77
|
+
const paramNames: string[] = [];
|
|
78
|
+
const patternStr = path
|
|
79
|
+
.replace(/:([a-zA-Z0-9_]+)/g, (_, name) => {
|
|
80
|
+
paramNames.push(name);
|
|
81
|
+
return "([^/]+)";
|
|
82
|
+
})
|
|
83
|
+
.replace(/\//g, "\\/");
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
pattern: new RegExp(`^${patternStr}$`),
|
|
87
|
+
paramNames
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import type {
|
|
3
|
+
Constructor,
|
|
4
|
+
RequestContext as CoreRequestContext,
|
|
5
|
+
UploadedFileInfo
|
|
6
|
+
} from "../../core/types";
|
|
7
|
+
import type { OpenApiInfo, OpenApiServer } from "../../core/openapi";
|
|
8
|
+
|
|
9
|
+
export { UploadedFileInfo };
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Request context provided to native route handlers.
|
|
13
|
+
*/
|
|
14
|
+
export type RequestContext<
|
|
15
|
+
TBody = any,
|
|
16
|
+
TQuery extends object | undefined = Record<string, any>,
|
|
17
|
+
TParams extends object | undefined = Record<string, any>,
|
|
18
|
+
THeaders extends object | undefined = Record<string, any>,
|
|
19
|
+
TFiles extends Record<string, UploadedFileInfo | UploadedFileInfo[]> | undefined = any
|
|
20
|
+
> = CoreRequestContext<TBody, TQuery, TParams, THeaders, TFiles>;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Input coercion modes.
|
|
24
|
+
*/
|
|
25
|
+
export type InputCoercionMode = "safe" | "strict";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Input coercion setting - can be a mode or disabled.
|
|
29
|
+
*/
|
|
30
|
+
export type InputCoercionSetting = InputCoercionMode | false;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Options for OpenAPI documentation UI.
|
|
34
|
+
*/
|
|
35
|
+
export interface OpenApiDocsOptions {
|
|
36
|
+
/** Path for documentation UI */
|
|
37
|
+
path?: string;
|
|
38
|
+
/** Title for documentation page */
|
|
39
|
+
title?: string;
|
|
40
|
+
/** URL for Swagger UI assets */
|
|
41
|
+
swaggerUiUrl?: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* OpenAPI configuration for native adapter.
|
|
46
|
+
*/
|
|
47
|
+
export interface OpenApiNativeOptions {
|
|
48
|
+
/** OpenAPI document info */
|
|
49
|
+
info: OpenApiInfo;
|
|
50
|
+
/** Array of servers */
|
|
51
|
+
servers?: OpenApiServer[];
|
|
52
|
+
/** Path for OpenAPI JSON endpoint */
|
|
53
|
+
path?: string;
|
|
54
|
+
/** Whether to pretty-print the JSON output (defaults to false for minified output) */
|
|
55
|
+
prettyPrint?: boolean;
|
|
56
|
+
/** Documentation UI configuration */
|
|
57
|
+
docs?: boolean | OpenApiDocsOptions;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Validation configuration options.
|
|
62
|
+
*/
|
|
63
|
+
export interface ValidationOptions {
|
|
64
|
+
/** Whether validation is enabled. Defaults to true. */
|
|
65
|
+
enabled?: boolean;
|
|
66
|
+
/** Validation mode. 'strict' mode fails on any validation error, 'lax' mode may allow some errors. Defaults to 'strict'. */
|
|
67
|
+
mode?: "strict" | "lax";
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Options for creating a native Node.js application adapter.
|
|
72
|
+
*/
|
|
73
|
+
export interface NativeAdapterOptions {
|
|
74
|
+
/** Array of controller classes */
|
|
75
|
+
controllers: Constructor[];
|
|
76
|
+
/** Whether to enable JSON body parsing */
|
|
77
|
+
jsonBody?: boolean;
|
|
78
|
+
/** Max JSON body size in bytes. */
|
|
79
|
+
bodyLimit?: number;
|
|
80
|
+
/** OpenAPI configuration */
|
|
81
|
+
openApi?: OpenApiNativeOptions;
|
|
82
|
+
/** Input coercion setting */
|
|
83
|
+
inputCoercion?: InputCoercionSetting;
|
|
84
|
+
/** Validation configuration. Set to false to disable validation, or provide options. */
|
|
85
|
+
validation?: boolean | ValidationOptions;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Interface for the native application.
|
|
90
|
+
*/
|
|
91
|
+
export interface NativeApp {
|
|
92
|
+
/** The native Node.js request handler */
|
|
93
|
+
handle(req: IncomingMessage, res: ServerResponse): Promise<void>;
|
|
94
|
+
/** Start the server on the specified port */
|
|
95
|
+
listen(port: number, callback?: () => void): any;
|
|
96
|
+
}
|