azurajs 3.0.2 → 3.0.4
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 +32 -0
- package/dist/IpResolver-BVgnGnpf.d.mts +5 -0
- package/dist/IpResolver-BVgnGnpf.d.ts +5 -0
- package/dist/SwaggerPlugin-C0UZTjaZ.d.ts +6 -0
- package/dist/SwaggerPlugin-wr9S4SRG.d.mts +6 -0
- package/dist/cookies/index.d.mts +7 -0
- package/dist/cookies/index.d.ts +7 -0
- package/dist/cookies/index.js +38 -0
- package/dist/cookies/index.js.map +1 -0
- package/dist/cookies/index.mjs +35 -0
- package/dist/cookies/index.mjs.map +1 -0
- package/dist/core/index.d.mts +18 -27
- package/dist/core/index.d.ts +18 -27
- package/dist/core/index.js +214 -14
- package/dist/core/index.js.map +1 -1
- package/dist/core/index.mjs +214 -14
- package/dist/core/index.mjs.map +1 -1
- package/dist/cors/index.d.mts +7 -0
- package/dist/cors/index.d.ts +7 -0
- package/dist/cors/index.js +52 -0
- package/dist/cors/index.js.map +1 -0
- package/dist/cors/index.mjs +50 -0
- package/dist/cors/index.mjs.map +1 -0
- package/dist/decorators/index.d.mts +2 -0
- package/dist/decorators/index.d.ts +2 -0
- package/dist/decorators/index.js +25 -0
- package/dist/decorators/index.js.map +1 -1
- package/dist/decorators/index.mjs +24 -1
- package/dist/decorators/index.mjs.map +1 -1
- package/dist/decorators-B6l3CbxC.d.ts +13 -0
- package/dist/decorators-D5nY109r.d.mts +13 -0
- package/dist/http-error/index.d.mts +18 -0
- package/dist/http-error/index.d.ts +18 -0
- package/dist/http-error/index.js +81 -0
- package/dist/http-error/index.js.map +1 -0
- package/dist/http-error/index.mjs +79 -0
- package/dist/http-error/index.mjs.map +1 -0
- package/dist/index-j6QGMhZU.d.mts +30 -0
- package/dist/index-tpPZS_UK.d.ts +30 -0
- package/dist/index.d.mts +16 -5
- package/dist/index.d.ts +16 -5
- package/dist/index.js +1178 -14
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1176 -15
- package/dist/index.mjs.map +1 -1
- package/dist/infra/index.d.mts +6 -0
- package/dist/infra/index.d.ts +6 -0
- package/dist/infra/index.js +162 -0
- package/dist/infra/index.js.map +1 -0
- package/dist/infra/index.mjs +159 -0
- package/dist/infra/index.mjs.map +1 -0
- package/dist/{Logger-iEQNVVSc.d.mts → logger/index.d.mts} +2 -1
- package/dist/{Logger-iEQNVVSc.d.ts → logger/index.d.ts} +2 -1
- package/dist/logger/index.js +123 -0
- package/dist/logger/index.js.map +1 -0
- package/dist/logger/index.mjs +120 -0
- package/dist/logger/index.mjs.map +1 -0
- package/dist/plugins/index.d.mts +8 -6
- package/dist/plugins/index.d.ts +8 -6
- package/dist/plugins/index.js +1101 -0
- package/dist/plugins/index.js.map +1 -1
- package/dist/plugins/index.mjs +1101 -1
- package/dist/plugins/index.mjs.map +1 -1
- package/dist/rate-limit/index.d.mts +7 -0
- package/dist/rate-limit/index.d.ts +7 -0
- package/dist/rate-limit/index.js +81 -0
- package/dist/rate-limit/index.js.map +1 -0
- package/dist/rate-limit/index.mjs +79 -0
- package/dist/rate-limit/index.mjs.map +1 -0
- package/dist/router/index.d.mts +4 -0
- package/dist/router/index.d.ts +4 -0
- package/dist/router/index.js +218 -0
- package/dist/router/index.js.map +1 -0
- package/dist/router/index.mjs +216 -0
- package/dist/router/index.mjs.map +1 -0
- package/dist/routes.type-DZO5VBW2.d.mts +58 -0
- package/dist/routes.type-DzHNkCag.d.ts +58 -0
- package/dist/swagger/index.d.mts +30 -0
- package/dist/swagger/index.d.ts +30 -0
- package/dist/swagger/index.js +1136 -0
- package/dist/swagger/index.js.map +1 -0
- package/dist/swagger/index.mjs +1126 -0
- package/dist/swagger/index.mjs.map +1 -0
- package/dist/swagger/swagger-ui-modern.html +894 -0
- package/dist/swagger.type-Bfn5nGR8.d.mts +42 -0
- package/dist/swagger.type-CfDbFCZC.d.ts +42 -0
- package/dist/types/index.d.mts +5 -58
- package/dist/types/index.d.ts +5 -58
- package/dist/utils/index.d.mts +7 -72
- package/dist/utils/index.d.ts +7 -72
- package/dist/validators/index.d.mts +48 -0
- package/dist/validators/index.d.ts +48 -0
- package/dist/validators/index.js +144 -0
- package/dist/validators/index.js.map +1 -0
- package/dist/validators/index.mjs +141 -0
- package/dist/validators/index.mjs.map +1 -0
- package/package.json +86 -2
- package/src/cookies/index.ts +1 -0
- package/src/core/index.ts +1 -0
- package/src/core/router.ts +26 -15
- package/src/core/server.ts +64 -14
- package/src/cors/index.ts +2 -0
- package/src/decorators/index.ts +2 -0
- package/src/http-error/index.ts +1 -0
- package/src/infra/index.ts +3 -0
- package/src/logger/index.ts +1 -0
- package/src/plugins/SwaggerPlugin.ts +45 -0
- package/src/plugins/index.ts +1 -0
- package/src/rate-limit/index.ts +2 -0
- package/src/router/index.ts +1 -0
- package/src/swagger/constants.ts +8 -0
- package/src/swagger/decorators.ts +35 -0
- package/src/swagger/index.ts +10 -0
- package/src/swagger/openapi-builder.ts +199 -0
- package/src/swagger/swagger-ui-html.ts +24 -0
- package/src/swagger/swagger-ui-modern.html +894 -0
- package/src/swagger/swagger-ui-template.ts +5 -0
- package/src/types/index.ts +7 -0
- package/src/types/swagger.type.ts +36 -0
- package/src/validators/index.ts +4 -0
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import type { HttpMethod } from "../types/common.type.js";
|
|
2
|
+
import type { RouteDocument } from "../core/router.js";
|
|
3
|
+
import type { RouteMeta } from "../types/routes.type.js";
|
|
4
|
+
import type { BuildOpenApiOptions, OpenApiInfo } from "../types/swagger.type.js";
|
|
5
|
+
|
|
6
|
+
function resolveInfo(options: BuildOpenApiOptions): OpenApiInfo {
|
|
7
|
+
return (
|
|
8
|
+
options.info ?? {
|
|
9
|
+
title: "AzuraJS API",
|
|
10
|
+
version: "1.0.0",
|
|
11
|
+
description: "Generated OpenAPI specification",
|
|
12
|
+
}
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Converte path AzuraJS `/users/:id` → OpenAPI `/users/{id}` */
|
|
17
|
+
export function toOpenApiPath(path: string): string {
|
|
18
|
+
if (path === "/") return "/";
|
|
19
|
+
const mapped = path
|
|
20
|
+
.split("/")
|
|
21
|
+
.map((seg) => {
|
|
22
|
+
if (seg === "*") return "{wildcard}";
|
|
23
|
+
return seg.startsWith(":") ? `{${seg.slice(1)}}` : seg;
|
|
24
|
+
})
|
|
25
|
+
.join("/")
|
|
26
|
+
.replace(/\/{2,}/g, "/");
|
|
27
|
+
if (mapped.endsWith("/*")) {
|
|
28
|
+
return mapped.slice(0, -2) + "/{wildcard}";
|
|
29
|
+
}
|
|
30
|
+
return mapped;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function pathParametersFromTemplate(path: string): Array<Record<string, unknown>> {
|
|
34
|
+
const re = /\{([^}]+)\}/g;
|
|
35
|
+
const out: Array<Record<string, unknown>> = [];
|
|
36
|
+
let m: RegExpExecArray | null;
|
|
37
|
+
const seen = new Set<string>();
|
|
38
|
+
while ((m = re.exec(path)) !== null) {
|
|
39
|
+
const name = m[1];
|
|
40
|
+
if (seen.has(name)) continue;
|
|
41
|
+
seen.add(name);
|
|
42
|
+
out.push({
|
|
43
|
+
name,
|
|
44
|
+
in: "path",
|
|
45
|
+
required: true,
|
|
46
|
+
schema: { type: "string" },
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function defaultResponses(meta?: RouteMeta): Record<string, unknown> {
|
|
53
|
+
if (meta?.responses && Object.keys(meta.responses).length > 0) {
|
|
54
|
+
const out: Record<string, unknown> = {};
|
|
55
|
+
for (const [code, r] of Object.entries(meta.responses)) {
|
|
56
|
+
out[String(code)] = {
|
|
57
|
+
description: r.description,
|
|
58
|
+
...(r.schema ? { content: { "application/json": { schema: r.schema } } } : {}),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
"200": {
|
|
65
|
+
description: "Successful response",
|
|
66
|
+
content: {
|
|
67
|
+
"application/json": {
|
|
68
|
+
schema: { type: "object", additionalProperties: true },
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function mergeParameters(
|
|
76
|
+
meta: RouteMeta | undefined,
|
|
77
|
+
pathParams: Array<Record<string, unknown>>,
|
|
78
|
+
): Array<Record<string, unknown>> {
|
|
79
|
+
const fromMeta = meta?.parameters ?? [];
|
|
80
|
+
const names = new Set(pathParams.map((p) => p.name as string));
|
|
81
|
+
const rest = fromMeta.filter((p) => !(p.in === "path" && names.has(p.name)));
|
|
82
|
+
const mapped = fromMeta
|
|
83
|
+
.filter((p) => p.in === "path")
|
|
84
|
+
.map((p) => ({
|
|
85
|
+
name: p.name,
|
|
86
|
+
in: "path",
|
|
87
|
+
required: p.required ?? true,
|
|
88
|
+
description: p.description,
|
|
89
|
+
schema: p.schema ?? { type: p.type ?? "string" },
|
|
90
|
+
}));
|
|
91
|
+
const pathMerged = [...pathParams];
|
|
92
|
+
for (const m of mapped) {
|
|
93
|
+
if (!pathMerged.find((x) => x.name === m.name)) pathMerged.push(m);
|
|
94
|
+
}
|
|
95
|
+
return [...pathMerged, ...rest.map((p) => mapParameter(p))];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function mapParameter(p: import("../types/routes.type.js").ParameterMeta): Record<string, unknown> {
|
|
99
|
+
const base: Record<string, unknown> = {
|
|
100
|
+
name: p.name,
|
|
101
|
+
in: p.in,
|
|
102
|
+
required: p.required ?? (p.in === "path" ? true : false),
|
|
103
|
+
description: p.description,
|
|
104
|
+
};
|
|
105
|
+
if (p.in === "body") {
|
|
106
|
+
return {
|
|
107
|
+
...base,
|
|
108
|
+
content: {
|
|
109
|
+
"application/json": {
|
|
110
|
+
schema: p.schema ?? { type: "object" },
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
...base,
|
|
117
|
+
schema: p.schema ?? { type: p.type ?? "string" },
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function operationIdFor(method: HttpMethod, oPath: string): string {
|
|
122
|
+
const slug = oPath
|
|
123
|
+
.replace(/[/{}\-*]/g, "_")
|
|
124
|
+
.replace(/_+/g, "_")
|
|
125
|
+
.replace(/^_|_$/g, "");
|
|
126
|
+
return `${method.toLowerCase()}_${slug || "root"}`;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function operationForMethod(
|
|
130
|
+
method: HttpMethod,
|
|
131
|
+
oPath: string,
|
|
132
|
+
meta: RouteMeta | undefined,
|
|
133
|
+
): Record<string, unknown> {
|
|
134
|
+
const op: Record<string, unknown> = {
|
|
135
|
+
operationId: operationIdFor(method, oPath),
|
|
136
|
+
summary: meta?.summary ?? `${method} ${oPath}`,
|
|
137
|
+
...(meta?.description ? { description: meta.description } : {}),
|
|
138
|
+
...(meta?.deprecated ? { deprecated: true } : {}),
|
|
139
|
+
...(meta?.tags?.length ? { tags: meta.tags } : {}),
|
|
140
|
+
...(meta?.security?.length ? { security: meta.security } : {}),
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
if (["POST", "PUT", "PATCH"].includes(method) && !meta?.parameters?.some((x) => x.in === "body")) {
|
|
144
|
+
op.requestBody = {
|
|
145
|
+
content: {
|
|
146
|
+
"application/json": {
|
|
147
|
+
schema: { type: "object", additionalProperties: true },
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
required: false,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
op.responses = defaultResponses(meta);
|
|
155
|
+
return op;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Gera documento OpenAPI 3.0 a partir das rotas registadas.
|
|
160
|
+
*/
|
|
161
|
+
export function buildOpenApiDocument(
|
|
162
|
+
routes: RouteDocument[],
|
|
163
|
+
options: BuildOpenApiOptions,
|
|
164
|
+
): Record<string, unknown> {
|
|
165
|
+
const info = resolveInfo(options);
|
|
166
|
+
const prefix = options.pathPrefix ?? "";
|
|
167
|
+
const paths: Record<string, Record<string, unknown>> = {};
|
|
168
|
+
|
|
169
|
+
const seen = new Set<string>();
|
|
170
|
+
for (const r of routes) {
|
|
171
|
+
const full = prefix + r.path;
|
|
172
|
+
const oPath = toOpenApiPath(full);
|
|
173
|
+
const key = `${r.method}:${oPath}`;
|
|
174
|
+
if (seen.has(key)) continue;
|
|
175
|
+
seen.add(key);
|
|
176
|
+
|
|
177
|
+
const pathParams = pathParametersFromTemplate(oPath);
|
|
178
|
+
const op = operationForMethod(r.method, oPath, r.meta);
|
|
179
|
+
const parameters = mergeParameters(r.meta, pathParams);
|
|
180
|
+
if (parameters.length) op.parameters = parameters;
|
|
181
|
+
|
|
182
|
+
const methodLc = r.method.toLowerCase();
|
|
183
|
+
if (!paths[oPath]) paths[oPath] = {};
|
|
184
|
+
paths[oPath][methodLc] = op;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return {
|
|
188
|
+
openapi: "3.0.3",
|
|
189
|
+
info: {
|
|
190
|
+
title: info.title,
|
|
191
|
+
version: info.version,
|
|
192
|
+
...(info.description ? { description: info.description } : {}),
|
|
193
|
+
...(info.contact ? { contact: info.contact } : {}),
|
|
194
|
+
...(info.license ? { license: info.license } : {}),
|
|
195
|
+
},
|
|
196
|
+
...(options.servers?.length ? { servers: options.servers } : {}),
|
|
197
|
+
paths,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML da UI de documentação OpenAPI (paridade visual com azurajs-legacy: sidebar, Inter/JetBrains Mono, tema zinc/indigo).
|
|
3
|
+
*/
|
|
4
|
+
import { SWAGGER_UI_MODERN_HTML } from "./swagger-ui-template.js";
|
|
5
|
+
|
|
6
|
+
export function renderSwaggerUiPage(options: {
|
|
7
|
+
title: string;
|
|
8
|
+
specUrl: string;
|
|
9
|
+
deepLinking?: boolean;
|
|
10
|
+
}): string {
|
|
11
|
+
const { title, specUrl } = options;
|
|
12
|
+
return SWAGGER_UI_MODERN_HTML.replaceAll("__PAGE_TITLE__", escapeHtml(title)).replaceAll(
|
|
13
|
+
'"___AZURA_SPEC_URL_PLACEHOLDER___"',
|
|
14
|
+
JSON.stringify(specUrl),
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function escapeHtml(s: string): string {
|
|
19
|
+
return s
|
|
20
|
+
.replace(/&/g, "&")
|
|
21
|
+
.replace(/</g, "<")
|
|
22
|
+
.replace(/>/g, ">")
|
|
23
|
+
.replace(/"/g, """);
|
|
24
|
+
}
|