raiton 3.1.1 → 5.4.0-beta.2
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/CHANGELOG.md +37 -0
- package/README.md +38 -3
- package/build/bin/index.mjs +822 -524
- package/examples/user-controller.example.ts +53 -0
- package/package.json +8 -10
- package/scripts/update-version.ts +1 -1
- package/source/bin/bootstrapper.ts +1 -1
- package/source/bin/cli-tools.ts +2 -0
- package/source/commands/build.command.ts +2 -1
- package/source/commands/develop.command.ts +2 -1
- package/source/commands/start.command.ts +15 -10
- package/source/core/application.ts +42 -33
- package/source/core/artifacts/artifact.ts +10 -0
- package/source/core/artifacts/index.ts +1 -0
- package/source/core/artifacts/runner.ts +3 -0
- package/source/core/builder.ts +44 -15
- package/source/core/config/define.ts +1 -1
- package/source/core/controller/builder.ts +1 -1
- package/source/core/controller/metadata.ts +1 -1
- package/source/core/hooks.ts +0 -8
- package/source/core/index.ts +15 -14
- package/source/core/injection/injection.ts +9 -6
- package/source/core/middleware/compose.ts +1 -1
- package/source/core/middleware/pipeline.ts +2 -8
- package/source/core/plugins/scope.ts +18 -6
- package/source/core/router/handler.ts +26 -80
- package/source/core/router/handler.utils.ts +135 -0
- package/source/core/router/matcher.ts +13 -9
- package/source/core/router/router.ts +6 -2
- package/source/core/socket/index.ts +1 -0
- package/source/core/socket/metadata.ts +13 -0
- package/source/core/thread.ts +23 -20
- package/source/{sdk → framework}/artifacts.ts +8 -1
- package/source/framework/constants/decorators.constant.ts +17 -0
- package/source/framework/decorators/api-response.decorator.ts +127 -0
- package/source/framework/decorators/api.decorator.ts +185 -0
- package/source/framework/decorators/index.ts +9 -0
- package/source/{sdk/decorators/parametrable.ts → framework/decorators/parametrable.decorator.ts} +1 -1
- package/source/framework/decorators/socket.decorator.ts +41 -0
- package/source/{sdk → framework}/encryption.ts +2 -1
- package/source/{sdk → framework}/enums/encrypted.enum.ts +2 -2
- package/source/{sdk → framework}/enums/http-parameters.enum.ts +1 -0
- package/source/{sdk → framework}/enums/runtime.enum.ts +2 -2
- package/source/{sdk → framework}/env.ts +2 -2
- package/source/{sdk → framework}/exceptions/http-exception.ts +1 -1
- package/source/{sdk → framework}/index.ts +3 -2
- package/source/{sdk → framework}/plugins/body-parser.plugin.ts +28 -11
- package/source/framework/plugins/index.ts +3 -0
- package/source/framework/plugins/openapi.plugin.ts +98 -0
- package/source/framework/plugins/security/cors.ts +78 -0
- package/source/{sdk → framework}/responses/helpers.ts +4 -3
- package/source/{sdk → framework}/responses/http-throwable.ts +2 -2
- package/source/{sdk → framework}/responses/http.ts +3 -3
- package/source/{sdk → framework}/runtime/bun/server.ts +6 -2
- package/source/framework/runtime/deno/server.ts +104 -0
- package/source/{sdk → framework}/runtime/index.ts +18 -13
- package/source/framework/runtime/node/reply.ts +37 -0
- package/source/framework/runtime/node/request.ts +47 -0
- package/source/framework/runtime/node/server.ts +32 -0
- package/source/framework/runtime/web/server.ts +73 -0
- package/source/{sdk → framework}/utilities/artifact.util.ts +3 -0
- package/source/framework/utilities/cookie.util.ts +11 -0
- package/source/{sdk → framework}/utilities/index.ts +3 -1
- package/source/framework/utilities/openapi.utils.ts +497 -0
- package/source/framework/view-model.ts +4 -0
- package/source/types/access-guards.ts +4 -0
- package/source/types/application.ts +1 -1
- package/source/types/artifact.ts +42 -1
- package/source/types/builder.ts +6 -1
- package/source/types/controller.ts +1 -1
- package/source/types/http-responses.ts +8 -0
- package/source/types/index.ts +1 -0
- package/source/types/responses.ts +1 -1
- package/source/types/router.ts +1 -1
- package/source/types/runtime.ts +5 -3
- package/source/types/scheme.ts +153 -0
- package/source/types/socket.ts +15 -0
- package/source/types/thread.ts +4 -2
- package/source/sdk/constants/decorators.constant.ts +0 -8
- package/source/sdk/decorators/index.ts +0 -6
- package/source/sdk/plugins/index.ts +0 -2
- package/source/sdk/plugins/security/cors.ts +0 -44
- package/source/sdk/runtime/deno/server.ts +0 -53
- package/source/sdk/runtime/node/reply.ts +0 -0
- package/source/sdk/runtime/node/request.ts +0 -0
- package/source/sdk/runtime/node/server.ts +0 -60
- package/source/sdk/runtime/web/server.ts +0 -57
- /package/source/{sdk → framework}/constants/index.ts +0 -0
- /package/source/{sdk → framework}/constants/microservices.constant.ts +0 -0
- /package/source/{sdk → framework}/controllers.ts +0 -0
- /package/source/{sdk → framework}/data-transfer-object.ts +0 -0
- /package/source/{sdk → framework}/decorators/access-guard.decorator.ts +0 -0
- /package/source/{sdk → framework}/decorators/controllable.decorator.ts +0 -0
- /package/source/{sdk → framework}/decorators/guard.decorator.ts +0 -0
- /package/source/{sdk → framework}/decorators/injection.decorator.ts +0 -0
- /package/source/{sdk → framework}/decorators/middleware.decorator.ts +0 -0
- /package/source/{sdk → framework}/decorators/routable.decorator.ts +0 -0
- /package/source/{sdk → framework}/enums/event.message.enum.ts +0 -0
- /package/source/{sdk → framework}/enums/http-method.enum.ts +0 -0
- /package/source/{sdk → framework}/enums/http-status.enum.ts +0 -0
- /package/source/{sdk → framework}/enums/index.ts +0 -0
- /package/source/{sdk → framework}/enums/timestamp.enum.ts +0 -0
- /package/source/{sdk → framework}/exceptions/index.ts +0 -0
- /package/source/{sdk → framework}/exceptions/throwable.ts +0 -0
- /package/source/{sdk → framework}/parameter-bag.ts +0 -0
- /package/source/{sdk → framework}/plugins/security/body-limit.ts +0 -0
- /package/source/{sdk → framework}/plugins/security/headers.ts +0 -0
- /package/source/{sdk → framework}/plugins/security/index.ts +0 -0
- /package/source/{sdk → framework}/plugins/security/method-guard.ts +0 -0
- /package/source/{sdk → framework}/plugins/security/rate-limit.ts +0 -0
- /package/source/{sdk → framework}/repositories.ts +0 -0
- /package/source/{sdk → framework}/responses/error.ts +0 -0
- /package/source/{sdk → framework}/responses/index.ts +0 -0
- /package/source/{sdk → framework}/services.ts +0 -0
- /package/source/{sdk → framework}/utilities/alias-path.util.ts +0 -0
- /package/source/{sdk → framework}/utilities/callable.util.ts +0 -0
- /package/source/{sdk → framework}/utilities/json.util.ts +0 -0
- /package/source/{sdk → framework}/utilities/path.util.ts +0 -0
- /package/source/{sdk → framework}/utilities/url.ts +0 -0
- /package/source/{sdk → framework}/utilities/utilities.util.ts +0 -0
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
import type {ParamMetaInterface} from "../../types";
|
|
2
|
+
import {METADATA_KEYS, Parametrable} from "../index";
|
|
3
|
+
import {PluginScope} from "../../core";
|
|
4
|
+
import {RaitonConfig} from "../../core/config";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Generate the OpenAPI specification object
|
|
8
|
+
*/
|
|
9
|
+
export async function generateOpenApiSpec(scope: PluginScope, opts: any): Promise<any> {
|
|
10
|
+
// Load application config from RaitonConfig singleton
|
|
11
|
+
// Ensure config is synced with current working directory (set by Application)
|
|
12
|
+
await RaitonConfig.sync(process.cwd());
|
|
13
|
+
|
|
14
|
+
const config: Map<keyof any, any> = RaitonConfig.current as any;
|
|
15
|
+
|
|
16
|
+
// Build base URL
|
|
17
|
+
const protocole = (config.get('protocole') as string) ?? 'http';
|
|
18
|
+
const hostname = (config.get('hostname') as string) ?? 'localhost';
|
|
19
|
+
const port = config.get('port') as number;
|
|
20
|
+
const pathname = (config.get('pathname') as string) ?? '/';
|
|
21
|
+
const basePath = pathname.endsWith('/') ? pathname : pathname + '/';
|
|
22
|
+
|
|
23
|
+
let baseUrl = `${protocole}://${hostname}`;
|
|
24
|
+
if (port !== undefined && port !== null) {
|
|
25
|
+
baseUrl += `:${port}`;
|
|
26
|
+
}
|
|
27
|
+
baseUrl += basePath;
|
|
28
|
+
|
|
29
|
+
// Initialize OpenAPI document
|
|
30
|
+
const spec: any = {
|
|
31
|
+
openapi: '3.0.0',
|
|
32
|
+
info: {
|
|
33
|
+
title: opts.title,
|
|
34
|
+
version: opts.version,
|
|
35
|
+
description: opts.description,
|
|
36
|
+
...(opts.termsOfService ? {termsOfService: opts.termsOfService} : {}),
|
|
37
|
+
...(opts.contact ? {contact: opts.contact} : {}),
|
|
38
|
+
...(opts.license ? {license: opts.license} : {})
|
|
39
|
+
},
|
|
40
|
+
servers: [
|
|
41
|
+
{
|
|
42
|
+
url: baseUrl
|
|
43
|
+
}
|
|
44
|
+
],
|
|
45
|
+
paths: {}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const routes = scope.router.getRoutes();
|
|
49
|
+
|
|
50
|
+
for (const route of routes) {
|
|
51
|
+
const handler = route.handler as any;
|
|
52
|
+
const meta = handler._raitonMeta as {
|
|
53
|
+
routeMeta: { method: string; path: string; propertyKey: string };
|
|
54
|
+
controllerMeta: any;
|
|
55
|
+
controllerClass: any
|
|
56
|
+
} | undefined;
|
|
57
|
+
if (!meta) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const {routeMeta, controllerMeta, controllerClass} = meta;
|
|
62
|
+
const method = routeMeta.method.toLowerCase() as keyof any;
|
|
63
|
+
|
|
64
|
+
let path = route.path;
|
|
65
|
+
if (route.version) {
|
|
66
|
+
path = `/${route.version}${path}`;
|
|
67
|
+
}
|
|
68
|
+
if (!path.startsWith('/')) {
|
|
69
|
+
path = '/' + path;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (!spec.paths[path]) {
|
|
73
|
+
spec.paths[path] = {};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const operation: any = {
|
|
77
|
+
parameters: [],
|
|
78
|
+
responses: {}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const operationMetadata = Reflect.getMetadata(METADATA_KEYS.API_OPERATION, controllerClass, routeMeta.propertyKey);
|
|
82
|
+
if (operationMetadata) {
|
|
83
|
+
Object.assign(operation, operationMetadata);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const methodTags = Reflect.getMetadata(METADATA_KEYS.API_TAGS, controllerClass, routeMeta.propertyKey);
|
|
87
|
+
if (methodTags) {
|
|
88
|
+
operation.tags = methodTags;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const methodSecurity = Reflect.getMetadata(METADATA_KEYS.API_SECURITY, controllerClass, routeMeta.propertyKey);
|
|
92
|
+
if (methodSecurity) {
|
|
93
|
+
operation.security = methodSecurity;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const classTags = Reflect.getMetadata(METADATA_KEYS.API_TAGS, controllerClass);
|
|
97
|
+
if (classTags) {
|
|
98
|
+
operation.tags = [...(operation.tags || []), ...classTags];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const classSecurity = Reflect.getMetadata(METADATA_KEYS.API_SECURITY, controllerClass);
|
|
102
|
+
if (classSecurity) {
|
|
103
|
+
operation.security = [...(operation.security || []), ...classSecurity];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const params: ParamMetaInterface[] = Reflect.getMetadata(METADATA_KEYS.ROUTE_PARAMETERS, controllerClass)?.[routeMeta.propertyKey] || [];
|
|
107
|
+
|
|
108
|
+
let bodyParam: ParamMetaInterface | undefined;
|
|
109
|
+
const otherParams = params.filter(p => {
|
|
110
|
+
if (p.type === Parametrable.BODY) {
|
|
111
|
+
bodyParam = p;
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
return true;
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
const apiParameters = Reflect.getMetadata(METADATA_KEYS.API_PARAMETERS, controllerClass)?.[routeMeta.propertyKey] || [];
|
|
118
|
+
for (const param of apiParameters) {
|
|
119
|
+
let inLocation: string;
|
|
120
|
+
switch (param.in) {
|
|
121
|
+
case 'path':
|
|
122
|
+
inLocation = 'path';
|
|
123
|
+
break;
|
|
124
|
+
case 'query':
|
|
125
|
+
inLocation = 'query';
|
|
126
|
+
break;
|
|
127
|
+
case 'header':
|
|
128
|
+
inLocation = 'header';
|
|
129
|
+
break;
|
|
130
|
+
case 'cookie':
|
|
131
|
+
inLocation = 'cookie';
|
|
132
|
+
break;
|
|
133
|
+
default:
|
|
134
|
+
continue; // Skip unknown locations
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const paramObj: any = {
|
|
138
|
+
name: param.name,
|
|
139
|
+
in: inLocation,
|
|
140
|
+
description: param.description,
|
|
141
|
+
required: param.required ?? true,
|
|
142
|
+
schema: {
|
|
143
|
+
type: 'string'
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
if (param.type) {
|
|
148
|
+
const typeName = typeof param.type === 'function' ? param.type.name : typeof param.type;
|
|
149
|
+
let schemaType = 'string'; // default
|
|
150
|
+
|
|
151
|
+
if (typeof param.type === 'function' && param.type.name) {
|
|
152
|
+
switch (param.type.name) {
|
|
153
|
+
case 'String':
|
|
154
|
+
schemaType = 'string';
|
|
155
|
+
break;
|
|
156
|
+
case 'Number':
|
|
157
|
+
schemaType = 'number';
|
|
158
|
+
break;
|
|
159
|
+
case 'Boolean':
|
|
160
|
+
schemaType = 'boolean';
|
|
161
|
+
break;
|
|
162
|
+
case 'Object':
|
|
163
|
+
schemaType = 'object';
|
|
164
|
+
break;
|
|
165
|
+
case 'Array':
|
|
166
|
+
schemaType = 'array';
|
|
167
|
+
break;
|
|
168
|
+
default:
|
|
169
|
+
// For custom classes, treat as object
|
|
170
|
+
schemaType = 'object';
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
} else if (typeof param.type === 'string') {
|
|
174
|
+
switch (param.type.toLowerCase()) {
|
|
175
|
+
case 'string':
|
|
176
|
+
schemaType = 'string';
|
|
177
|
+
break;
|
|
178
|
+
case 'number':
|
|
179
|
+
case 'int':
|
|
180
|
+
case 'float':
|
|
181
|
+
case 'double':
|
|
182
|
+
schemaType = 'number';
|
|
183
|
+
break;
|
|
184
|
+
case 'boolean':
|
|
185
|
+
schemaType = 'boolean';
|
|
186
|
+
break;
|
|
187
|
+
case 'object':
|
|
188
|
+
schemaType = 'object';
|
|
189
|
+
break;
|
|
190
|
+
case 'array':
|
|
191
|
+
schemaType = 'array';
|
|
192
|
+
break;
|
|
193
|
+
default:
|
|
194
|
+
schemaType = 'string'; // fallback
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
paramObj.schema = {
|
|
200
|
+
type: schemaType,
|
|
201
|
+
...(param.default !== undefined ? {default: param.default} : {})
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
// Handle array items if needed (simplified)
|
|
205
|
+
if (schemaType === 'array' && param.type && typeof param.type === 'function' && param.type.name) {
|
|
206
|
+
switch (param.type.name) {
|
|
207
|
+
case 'String':
|
|
208
|
+
paramObj.schema.items = {type: 'string'};
|
|
209
|
+
break;
|
|
210
|
+
case 'Number':
|
|
211
|
+
paramObj.schema.items = {type: 'number'};
|
|
212
|
+
break;
|
|
213
|
+
case 'Boolean':
|
|
214
|
+
paramObj.schema.items = {type: 'boolean'};
|
|
215
|
+
break;
|
|
216
|
+
default:
|
|
217
|
+
paramObj.schema.items = {type: 'string'}; // generic
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
operation.parameters.push(paramObj);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// Process standard Parametrable decorators (@Param, @Query, etc.)
|
|
227
|
+
for (const p of otherParams) {
|
|
228
|
+
let inLocation: string | undefined;
|
|
229
|
+
switch (p.type) {
|
|
230
|
+
case Parametrable.PARAM:
|
|
231
|
+
inLocation = 'path';
|
|
232
|
+
break;
|
|
233
|
+
case Parametrable.QUERY:
|
|
234
|
+
inLocation = 'query';
|
|
235
|
+
break;
|
|
236
|
+
case Parametrable.HEADER:
|
|
237
|
+
inLocation = 'header';
|
|
238
|
+
break;
|
|
239
|
+
// Note: COOKIE is not supported as there is no Parametrable.COOKIE in the framework
|
|
240
|
+
default:
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
if (!inLocation) continue;
|
|
244
|
+
|
|
245
|
+
const paramObj: any = {
|
|
246
|
+
name: p.key ?? '',
|
|
247
|
+
in: inLocation,
|
|
248
|
+
required: true, // We don't have info about required; assume true
|
|
249
|
+
schema: {
|
|
250
|
+
type: 'string' // Default to string; we could try to infer from p.metatype
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
// Try to improve schema based on metatype if available
|
|
254
|
+
if (p.metatype) {
|
|
255
|
+
// If metatype is a constructor like String, Number, Boolean, we can map to JSON types
|
|
256
|
+
const typeName = p.metatype.name;
|
|
257
|
+
switch (typeName) {
|
|
258
|
+
case 'String':
|
|
259
|
+
paramObj.schema.type = 'string';
|
|
260
|
+
break;
|
|
261
|
+
case 'Number':
|
|
262
|
+
paramObj.schema.type = 'number';
|
|
263
|
+
break;
|
|
264
|
+
case 'Boolean':
|
|
265
|
+
paramObj.schema.type = 'boolean';
|
|
266
|
+
break;
|
|
267
|
+
case 'Object':
|
|
268
|
+
paramObj.schema.type = 'object';
|
|
269
|
+
break;
|
|
270
|
+
case 'Array':
|
|
271
|
+
paramObj.schema.type = 'array';
|
|
272
|
+
// We don't know item type; leave as generic
|
|
273
|
+
break;
|
|
274
|
+
default:
|
|
275
|
+
// For custom classes (DTOs), we could generate a schema but skip for simplicity
|
|
276
|
+
// Keep as string for now
|
|
277
|
+
break;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
operation.parameters.push(paramObj);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// Check for explicit API request body decorator
|
|
284
|
+
const apiRequestBody = Reflect.getMetadata(METADATA_KEYS.API_REQUEST_BODY, controllerClass, routeMeta.propertyKey);
|
|
285
|
+
if (apiRequestBody) {
|
|
286
|
+
// Use explicit API request body definition
|
|
287
|
+
const bodyRequired = apiRequestBody.required ?? true;
|
|
288
|
+
let bodySchema = null;
|
|
289
|
+
if (apiRequestBody.type) {
|
|
290
|
+
bodySchema = getSchemaFromType(apiRequestBody.type);
|
|
291
|
+
}
|
|
292
|
+
if (bodySchema !== null) {
|
|
293
|
+
operation.requestBody = {
|
|
294
|
+
required: bodyRequired,
|
|
295
|
+
content: {
|
|
296
|
+
'application/json': {
|
|
297
|
+
schema: bodySchema
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
} else if (bodyParam) {
|
|
303
|
+
// Fallback to automatic body parameter detection
|
|
304
|
+
// Determine if the body param has a metatype that is a class (DTO)
|
|
305
|
+
let bodySchema: any = {type: 'object'}; // default
|
|
306
|
+
if (bodyParam.metatype && typeof bodyParam.metatype === 'function' && 'prototype' in bodyParam.metatype) {
|
|
307
|
+
// It's a class; we treat it as object for simplicity
|
|
308
|
+
bodySchema = {type: 'object'};
|
|
309
|
+
} else {
|
|
310
|
+
// If metatype is a primitive type like String, Number, Boolean, etc.
|
|
311
|
+
const typeName = bodyParam.metatype?.name;
|
|
312
|
+
switch (typeName) {
|
|
313
|
+
case 'String':
|
|
314
|
+
bodySchema = {type: 'string'};
|
|
315
|
+
break;
|
|
316
|
+
case 'Number':
|
|
317
|
+
bodySchema = {type: 'number'};
|
|
318
|
+
break;
|
|
319
|
+
case 'Boolean':
|
|
320
|
+
bodySchema = {type: 'boolean'};
|
|
321
|
+
break;
|
|
322
|
+
case 'Object':
|
|
323
|
+
bodySchema = {type: 'object'};
|
|
324
|
+
break;
|
|
325
|
+
case 'Array':
|
|
326
|
+
bodySchema = {type: 'array', items: {type: 'string'}}; // generic
|
|
327
|
+
break;
|
|
328
|
+
default:
|
|
329
|
+
bodySchema = {type: 'object'};
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
operation.requestBody = {
|
|
333
|
+
required: true,
|
|
334
|
+
content: {
|
|
335
|
+
'application/json': {
|
|
336
|
+
schema: bodySchema
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// Check for API response decorators first
|
|
343
|
+
const apiResponses = Reflect.getMetadata(METADATA_KEYS.API_RESPONSES, controllerClass)?.[routeMeta.propertyKey] || [];
|
|
344
|
+
|
|
345
|
+
if (apiResponses.length > 0) {
|
|
346
|
+
// Use decorator-defined responses
|
|
347
|
+
for (const response of apiResponses) {
|
|
348
|
+
const statusCode = String(response.status);
|
|
349
|
+
let responseContent = undefined;
|
|
350
|
+
|
|
351
|
+
if (response.type) {
|
|
352
|
+
const responseSchema = getSchemaFromType(response.type);
|
|
353
|
+
if (response.isArray) {
|
|
354
|
+
responseContent = {
|
|
355
|
+
type: 'array',
|
|
356
|
+
items: responseSchema
|
|
357
|
+
};
|
|
358
|
+
} else {
|
|
359
|
+
responseContent = responseSchema;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
operation.responses[statusCode] = {
|
|
364
|
+
description: response.description || '',
|
|
365
|
+
...(responseContent ? {content: {'application/json': {schema: responseContent}}} : {})
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
} else {
|
|
369
|
+
// Fallback to return type detection
|
|
370
|
+
const returnType = Reflect.getMetadata('design:returntype', controllerClass, routeMeta.propertyKey);
|
|
371
|
+
if (returnType) {
|
|
372
|
+
// Handle Promise<T> -> T
|
|
373
|
+
let resolvedType = returnType;
|
|
374
|
+
if (typeof returnType === 'function' && returnType.name === 'Promise' && !('length' in (returnType.prototype || {}))) {
|
|
375
|
+
// We can't easily get the generic argument; we'll try to infer from the type string
|
|
376
|
+
// For simplicity, if it's a Promise, we'll still treat the resolved type as unknown.
|
|
377
|
+
// In practice, we could attempt to get the type argument, but it's complex.
|
|
378
|
+
// We'll leave it as object for now.
|
|
379
|
+
resolvedType = Object;
|
|
380
|
+
}
|
|
381
|
+
const responseSchema = getSchemaFromType(resolvedType);
|
|
382
|
+
operation.responses['200'] = {
|
|
383
|
+
description: 'Successful response',
|
|
384
|
+
...(responseSchema ? {content: {'application/json': {schema: responseSchema}}} : {})
|
|
385
|
+
};
|
|
386
|
+
} else {
|
|
387
|
+
// Default response if we cannot determine return type
|
|
388
|
+
operation.responses['200'] = {
|
|
389
|
+
description: 'Successful response'
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// Assign operation to path item under the method key
|
|
395
|
+
(spec.paths[path] as any)[method] = operation;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return spec;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Attempt to derive a JSON Schema fragment from a TypeScript type.
|
|
403
|
+
* This is a simplified implementation; for production use, consider using
|
|
404
|
+
* a library like @sinclair/typebox or class-transformer to generate schemas.
|
|
405
|
+
*/
|
|
406
|
+
export function getSchemaFromType(type: any): any {
|
|
407
|
+
if (typeof type !== 'function') {
|
|
408
|
+
// Not a constructor; fallback to basic type detection
|
|
409
|
+
return getPrimitiveTypeSchema(typeof type);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
const typeName = type.name;
|
|
413
|
+
switch (typeName) {
|
|
414
|
+
case 'String':
|
|
415
|
+
return {type: 'string'};
|
|
416
|
+
case 'Number':
|
|
417
|
+
return {type: 'number'};
|
|
418
|
+
case 'Boolean':
|
|
419
|
+
return {type: 'boolean'};
|
|
420
|
+
case 'Object':
|
|
421
|
+
return {type: 'object'};
|
|
422
|
+
case 'Array':
|
|
423
|
+
return {
|
|
424
|
+
type: 'array',
|
|
425
|
+
items: {type: 'string'} // We don't know the item type
|
|
426
|
+
};
|
|
427
|
+
case 'Date':
|
|
428
|
+
return {
|
|
429
|
+
type: 'string',
|
|
430
|
+
format: 'date-time'
|
|
431
|
+
};
|
|
432
|
+
default:
|
|
433
|
+
// For custom classes (DTOs, entities, etc.), we treat as object.
|
|
434
|
+
// In a more advanced implementation, we could inspect the class properties
|
|
435
|
+
// and generate properties from them (especially if decorated with @ApiProperty etc.)
|
|
436
|
+
return {type: 'object'};
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Get schema for primitive types (string, number, boolean, bigint, symbol, undefined, object, function)
|
|
442
|
+
*/
|
|
443
|
+
export function getPrimitiveTypeSchema(primitive: string): any {
|
|
444
|
+
switch (primitive) {
|
|
445
|
+
case 'string':
|
|
446
|
+
return {type: 'string'};
|
|
447
|
+
case 'number':
|
|
448
|
+
return {type: 'number'};
|
|
449
|
+
case 'boolean':
|
|
450
|
+
return {type: 'boolean'};
|
|
451
|
+
case 'bigint':
|
|
452
|
+
return {type: 'string'}; // Represent bigint as string in JSON
|
|
453
|
+
case 'undefined':
|
|
454
|
+
return {type: 'null'};
|
|
455
|
+
case 'object':
|
|
456
|
+
return {type: 'object'};
|
|
457
|
+
case 'function':
|
|
458
|
+
return {type: 'string'}; // functions are not serializable; represent as string placeholder
|
|
459
|
+
default:
|
|
460
|
+
return {type: 'string'};
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Generate HTML for Swagger UI using CDN
|
|
466
|
+
*/
|
|
467
|
+
// export function generateHtml(title: string, endpoint: string): string {
|
|
468
|
+
// // Ensure endpoint starts with / if not already
|
|
469
|
+
// if (!endpoint.startsWith('/')) {
|
|
470
|
+
// endpoint = '/' + endpoint;
|
|
471
|
+
// }
|
|
472
|
+
// return `<!DOCTYPE html>
|
|
473
|
+
// <html lang="en">
|
|
474
|
+
// <head>
|
|
475
|
+
// <meta charset="UTF-8">
|
|
476
|
+
// <title>${title} - SwaggerUI</title>
|
|
477
|
+
// <link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5/swagger-ui.css" />
|
|
478
|
+
// </head>
|
|
479
|
+
// <body>
|
|
480
|
+
// <div id="swagger-ui"></div>
|
|
481
|
+
// <script src="https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js"></script>
|
|
482
|
+
// <script>
|
|
483
|
+
// window.onload = () => {
|
|
484
|
+
// window.ui = SwaggerUIBundle({
|
|
485
|
+
// url: '${endpoint}',
|
|
486
|
+
// domId: '#swagger-ui',
|
|
487
|
+
// presets: [
|
|
488
|
+
// SwaggerUIBundle.presets.forApis,
|
|
489
|
+
// SwaggerUIBundle.SwaggerUIStandalonePreset
|
|
490
|
+
// ],
|
|
491
|
+
// layout: "BaseLayout"
|
|
492
|
+
// });
|
|
493
|
+
// }
|
|
494
|
+
// </script>
|
|
495
|
+
// </body>
|
|
496
|
+
// </html>`;
|
|
497
|
+
// }
|
package/source/types/artifact.ts
CHANGED
|
@@ -1,3 +1,44 @@
|
|
|
1
|
+
export interface ArtifactEntry {
|
|
2
|
+
id: string;
|
|
3
|
+
timestamp: Date;
|
|
4
|
+
size: number;
|
|
5
|
+
}
|
|
6
|
+
|
|
1
7
|
export interface ArtifactsConfigInterface {
|
|
2
8
|
types: string[]
|
|
3
|
-
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// export type ArtifactDecoratorHandler = () => void;
|
|
12
|
+
|
|
13
|
+
// export interface ArtifactDecorator {
|
|
14
|
+
// syntax: RegExp;
|
|
15
|
+
// handler: ArtifactDecoratorHandler;
|
|
16
|
+
// }
|
|
17
|
+
|
|
18
|
+
// export interface ArtifactOptions {
|
|
19
|
+
// readonly artifact: string;
|
|
20
|
+
// readonly provider: string;
|
|
21
|
+
// readonly decorator: ArtifactDecorator;
|
|
22
|
+
// verbose?: boolean;
|
|
23
|
+
// }
|
|
24
|
+
|
|
25
|
+
// export interface ArtifactInterface {
|
|
26
|
+
// readonly options: ArtifactOptions;
|
|
27
|
+
// readonly directory: string;
|
|
28
|
+
// readonly file: string;
|
|
29
|
+
// readonly workdir: string;
|
|
30
|
+
//
|
|
31
|
+
// get files(): string[];
|
|
32
|
+
//
|
|
33
|
+
// get extensions(): string[];
|
|
34
|
+
//
|
|
35
|
+
// scan(): string[];
|
|
36
|
+
//
|
|
37
|
+
// generate(): boolean;
|
|
38
|
+
// }
|
|
39
|
+
|
|
40
|
+
// export interface ArtifactEntry {
|
|
41
|
+
// vendor: string;
|
|
42
|
+
// decorator: string;
|
|
43
|
+
// pattern: string;
|
|
44
|
+
// }
|
package/source/types/builder.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
import {WatchEventType} from "node:fs";
|
|
2
2
|
|
|
3
|
+
export interface StartCommandOptionsInterface {
|
|
4
|
+
develop?: boolean;
|
|
5
|
+
}
|
|
6
|
+
|
|
3
7
|
export interface BuildCommandOptionsInterface {
|
|
4
8
|
develop?: boolean;
|
|
5
9
|
bootstrap?: boolean;
|
|
6
10
|
}
|
|
7
11
|
|
|
8
12
|
export interface BuilderConfigInterface {
|
|
9
|
-
|
|
13
|
+
serve?: boolean;
|
|
14
|
+
hmr?: boolean;
|
|
10
15
|
}
|
|
11
16
|
|
|
12
17
|
export type BuilderBootCallableType = (builder: BuilderInterface) => Promise<void>
|
package/source/types/index.ts
CHANGED
|
@@ -16,6 +16,7 @@ export type * from "./plugin"
|
|
|
16
16
|
export type * from "./runtime"
|
|
17
17
|
export type * from "./router"
|
|
18
18
|
export type * from "./controller"
|
|
19
|
+
export type * from "./socket"
|
|
19
20
|
export type * from "./injection"
|
|
20
21
|
export type * from "./responses"
|
|
21
22
|
export type * from "./server"
|
package/source/types/router.ts
CHANGED
package/source/types/runtime.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {RuntimeType} from "../
|
|
1
|
+
import {RuntimeType} from "../framework/enums/runtime.enum";
|
|
2
2
|
|
|
3
3
|
export interface RuntimeServerInterface {
|
|
4
4
|
listen(port: number, hostname?: string): Promise<void>
|
|
@@ -32,6 +32,8 @@ export interface RuntimeReplyInterface {
|
|
|
32
32
|
text(text: string | Buffer): void
|
|
33
33
|
|
|
34
34
|
json(data: any): void
|
|
35
|
+
|
|
36
|
+
type(contentType: string): void
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
export interface RuntimeAdapterInterface {
|
|
@@ -44,9 +46,9 @@ export interface RuntimeInterface {
|
|
|
44
46
|
|
|
45
47
|
get isNode(): boolean;
|
|
46
48
|
|
|
47
|
-
get isDeno(): boolean;
|
|
49
|
+
// get isDeno(): boolean;
|
|
48
50
|
|
|
49
|
-
get isWeb(): boolean;
|
|
51
|
+
// get isWeb(): boolean;
|
|
50
52
|
|
|
51
53
|
get isBun(): boolean
|
|
52
54
|
|