raiton 5.9.7-beta.18 → 6.0.1-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/README.md +13 -9
- package/build/bin/index.mjs +7 -18
- package/docs/README.md +6 -129
- package/docs/contributors/README.md +103 -0
- package/docs/{cli → contributors/cli}/README.md +1 -1
- package/docs/{cli → contributors/cli}/hmr.md +20 -23
- package/docs/{core → contributors/core}/README.md +1 -1
- package/docs/{core → contributors/core}/builder-thread.md +4 -7
- package/docs/{core → contributors/core}/controller.md +1 -1
- package/docs/contributors/framework/README.md +67 -0
- package/docs/{framework → contributors/framework}/base-classes.md +30 -2
- package/docs/{framework → contributors/framework}/decorators/socket.md +31 -3
- package/docs/{types → contributors/types}/README.md +1 -1
- package/docs/user-guide/README.md +62 -0
- package/docs/user-guide/api/README.md +21 -0
- package/docs/user-guide/api/example.md +50 -0
- package/docs/user-guide/api/responses.md +58 -0
- package/docs/user-guide/api/routes.md +56 -0
- package/docs/user-guide/architecture.md +83 -0
- package/docs/user-guide/best-practices.md +36 -0
- package/docs/user-guide/body-parser.md +39 -0
- package/docs/user-guide/cli.md +51 -0
- package/docs/user-guide/config.md +47 -0
- package/docs/user-guide/decorators/README.md +33 -0
- package/docs/user-guide/decorators/api-body.md +47 -0
- package/docs/user-guide/decorators/api-operation.md +47 -0
- package/docs/user-guide/decorators/api-param.md +47 -0
- package/docs/user-guide/decorators/api-property.md +46 -0
- package/docs/user-guide/decorators/api-query.md +47 -0
- package/docs/user-guide/decorators/api-response.md +48 -0
- package/docs/user-guide/decorators/api-tags.md +47 -0
- package/docs/user-guide/decorators/controllable.md +46 -0
- package/docs/user-guide/decorators/guard.md +57 -0
- package/docs/user-guide/decorators/injection.md +55 -0
- package/docs/user-guide/decorators/middleware.md +56 -0
- package/docs/user-guide/decorators/parameters.md +57 -0
- package/docs/user-guide/decorators/route-interaction.md +47 -0
- package/docs/user-guide/decorators/routes.md +62 -0
- package/docs/user-guide/decorators/socket.md +59 -0
- package/docs/user-guide/encryption.md +40 -0
- package/docs/user-guide/env.md +39 -0
- package/docs/user-guide/getting-started.md +72 -0
- package/docs/user-guide/injection.md +80 -0
- package/docs/user-guide/middleware.md +98 -0
- package/docs/user-guide/openapi/README.md +70 -0
- package/docs/user-guide/openapi/plugin.md +72 -0
- package/docs/user-guide/plugins.md +69 -0
- package/docs/user-guide/responses.md +99 -0
- package/docs/user-guide/runtime.md +44 -0
- package/docs/user-guide/security.md +46 -0
- package/docs/user-guide/sockets.md +49 -0
- package/docs/user-guide/tutorial.md +175 -0
- package/docs/user-guide/types.md +118 -0
- package/package.json +1 -1
- package/source/commands/build.command.ts +2 -2
- package/source/core/builder.ts +15 -14
- package/source/core/controller/builder.ts +14 -6
- package/source/core/socket/builder.ts +5 -0
- package/source/core/socket/index.ts +1 -0
- package/source/core/socket/metadata.ts +54 -1
- package/source/core/thread.ts +4 -1
- package/source/framework/artifacts.ts +2 -12
- package/source/framework/constants/decorators.constant.ts +1 -0
- package/source/framework/decorators/api-response.decorator.ts +4 -5
- package/source/framework/decorators/api.decorator.ts +36 -20
- package/source/framework/decorators/socket.decorator.ts +2 -1
- package/source/framework/runtime/bun/server.ts +100 -3
- package/source/framework/runtime/index.ts +4 -3
- package/source/framework/utilities/openapi.utils.ts +154 -109
- package/source/types/raiton.ts +2 -2
- package/source/types/runtime.ts +5 -1
- package/docs/framework/README.md +0 -96
- /package/docs/{cli → contributors/cli}/commands.md +0 -0
- /package/docs/{core → contributors/core}/application.md +0 -0
- /package/docs/{core → contributors/core}/config.md +0 -0
- /package/docs/{core → contributors/core}/injection.md +0 -0
- /package/docs/{core → contributors/core}/middleware.md +0 -0
- /package/docs/{core → contributors/core}/plugin-scope.md +0 -0
- /package/docs/{core → contributors/core}/router.md +0 -0
- /package/docs/{framework → contributors/framework}/decorators/README.md +0 -0
- /package/docs/{framework → contributors/framework}/decorators/controllable.md +0 -0
- /package/docs/{framework → contributors/framework}/decorators/injection.md +0 -0
- /package/docs/{framework → contributors/framework}/decorators/middleware.md +0 -0
- /package/docs/{framework → contributors/framework}/decorators/openapi.md +0 -0
- /package/docs/{framework → contributors/framework}/decorators/parametrable.md +0 -0
- /package/docs/{framework → contributors/framework}/encryption.md +0 -0
- /package/docs/{framework → contributors/framework}/env.md +0 -0
- /package/docs/{framework → contributors/framework}/plugins/README.md +0 -0
- /package/docs/{framework → contributors/framework}/plugins/body-parser.md +0 -0
- /package/docs/{framework → contributors/framework}/plugins/openapi.md +0 -0
- /package/docs/{framework → contributors/framework}/plugins/security.md +0 -0
- /package/docs/{framework → contributors/framework}/responses.md +0 -0
- /package/docs/{framework → contributors/framework}/runtime.md +0 -0
|
@@ -30,8 +30,6 @@ export interface ApiTagOptions {
|
|
|
30
30
|
*/
|
|
31
31
|
export function ApiTags(...tags: (string | ApiTagOptions)[]) {
|
|
32
32
|
return function (target: any, propertyKey?: string | symbol) {
|
|
33
|
-
const tagsMetadata = Reflect.getMetadata(METADATA_KEYS.API_TAGS, target) || [];
|
|
34
|
-
|
|
35
33
|
// Convert string tags to objects
|
|
36
34
|
const processedTags = tags.map(tag =>
|
|
37
35
|
typeof tag === 'string' ? {name: tag} : tag
|
|
@@ -39,11 +37,12 @@ export function ApiTags(...tags: (string | ApiTagOptions)[]) {
|
|
|
39
37
|
|
|
40
38
|
if (propertyKey) {
|
|
41
39
|
// Method-level decorator
|
|
42
|
-
const
|
|
43
|
-
Reflect.defineMetadata(METADATA_KEYS.API_TAGS, [...
|
|
40
|
+
const existingTags = Reflect.getOwnMetadata(METADATA_KEYS.API_TAGS, target, propertyKey) || [];
|
|
41
|
+
Reflect.defineMetadata(METADATA_KEYS.API_TAGS, [...existingTags, ...processedTags], target, propertyKey);
|
|
44
42
|
} else {
|
|
45
43
|
// Class-level decorator
|
|
46
|
-
Reflect.
|
|
44
|
+
const existingTags = Reflect.getOwnMetadata(METADATA_KEYS.API_TAGS, target) || [];
|
|
45
|
+
Reflect.defineMetadata(METADATA_KEYS.API_TAGS, [...existingTags, ...processedTags], target);
|
|
47
46
|
}
|
|
48
47
|
};
|
|
49
48
|
}
|
|
@@ -108,14 +107,13 @@ export interface ApiSecurityOptions {
|
|
|
108
107
|
*/
|
|
109
108
|
export function ApiSecurity(...security: ApiSecurityOptions[]) {
|
|
110
109
|
return function (target: any, propertyKey?: string | symbol) {
|
|
111
|
-
const securities = Reflect.getMetadata(METADATA_KEYS.API_SECURITY, target) || [];
|
|
112
|
-
|
|
113
110
|
if (propertyKey) {
|
|
114
111
|
// Method-level decorator
|
|
115
|
-
const methodSecurities = Reflect.
|
|
112
|
+
const methodSecurities = Reflect.getOwnMetadata(METADATA_KEYS.API_SECURITY, target, propertyKey) || [];
|
|
116
113
|
Reflect.defineMetadata(METADATA_KEYS.API_SECURITY, [...methodSecurities, ...security], target, propertyKey);
|
|
117
114
|
} else {
|
|
118
115
|
// Class-level decorator
|
|
116
|
+
const securities = Reflect.getOwnMetadata(METADATA_KEYS.API_SECURITY, target) || [];
|
|
119
117
|
Reflect.defineMetadata(METADATA_KEYS.API_SECURITY, [...securities, ...security], target);
|
|
120
118
|
}
|
|
121
119
|
};
|
|
@@ -180,25 +178,43 @@ export function ApiParam(name: string, options: {
|
|
|
180
178
|
enum?: any[]
|
|
181
179
|
}) {
|
|
182
180
|
return function (target: any, propertyKey: string | symbol) {
|
|
183
|
-
const params = Reflect.
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
params[propertyKey].push({name, ...options});
|
|
181
|
+
const params = Reflect.getOwnMetadata(METADATA_KEYS.API_PARAMETERS, target, propertyKey) || {};
|
|
182
|
+
const methodParams = params[propertyKey] ? [...params[propertyKey]] : [];
|
|
183
|
+
methodParams.push({name, ...options});
|
|
184
|
+
params[propertyKey] = methodParams;
|
|
188
185
|
|
|
189
186
|
if (options.enum) {
|
|
190
|
-
Reflect.
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
[propertyKey]: options.enum
|
|
194
|
-
},
|
|
195
|
-
target
|
|
196
|
-
);
|
|
187
|
+
const enums = Reflect.getOwnMetadata(METADATA_KEYS.API_ENUMS, target) || {};
|
|
188
|
+
enums[propertyKey] = options.enum;
|
|
189
|
+
Reflect.defineMetadata(METADATA_KEYS.API_ENUMS, enums, target);
|
|
197
190
|
}
|
|
198
191
|
Reflect.defineMetadata(METADATA_KEYS.API_PARAMETERS, params, target, propertyKey);
|
|
199
192
|
};
|
|
200
193
|
}
|
|
201
194
|
|
|
195
|
+
/**
|
|
196
|
+
* Decorator to define a property of a DTO for OpenAPI
|
|
197
|
+
* @param options Property options
|
|
198
|
+
*/
|
|
199
|
+
export function ApiProperty(options: {
|
|
200
|
+
type?: any;
|
|
201
|
+
description?: string;
|
|
202
|
+
example?: any;
|
|
203
|
+
required?: boolean;
|
|
204
|
+
enum?: any[];
|
|
205
|
+
isArray?: boolean;
|
|
206
|
+
} = {}) {
|
|
207
|
+
return function (target: any, propertyKey: string | symbol) {
|
|
208
|
+
const properties = Reflect.getOwnMetadata(METADATA_KEYS.API_PROPERTY, target.constructor) || {};
|
|
209
|
+
properties[propertyKey] = {
|
|
210
|
+
...options,
|
|
211
|
+
// If type is not provided, try to infer it using design:type
|
|
212
|
+
type: options.type || Reflect.getMetadata('design:type', target, propertyKey)
|
|
213
|
+
};
|
|
214
|
+
Reflect.defineMetadata(METADATA_KEYS.API_PROPERTY, properties, target.constructor);
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
202
218
|
/**
|
|
203
219
|
* Decorator to add query parameter metadata (shortcut for ApiParam with in: 'query')
|
|
204
220
|
* @param name Parameter name
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {getSocketMetadata} from "../../core/socket";
|
|
1
|
+
import {getSocketMetadata, registerSocket} from "../../core/socket";
|
|
2
2
|
import {Injectable} from "..";
|
|
3
3
|
import {LifetimeEnum} from "@protorians/core";
|
|
4
4
|
import {SocketEventType} from "../../types";
|
|
@@ -10,6 +10,7 @@ export function Socket(namespace = '/') {
|
|
|
10
10
|
|
|
11
11
|
const meta = getSocketMetadata(target.prototype || target)
|
|
12
12
|
meta.namespace = namespace;
|
|
13
|
+
registerSocket(target)
|
|
13
14
|
}
|
|
14
15
|
}
|
|
15
16
|
|
|
@@ -1,21 +1,118 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
RuntimeAdapterInterface,
|
|
3
|
+
RuntimeServerOptionsInterface,
|
|
4
|
+
} from '../../../types'
|
|
2
5
|
import {Logger} from "@protorians/logger";
|
|
3
6
|
import {getRealIp} from "../../utilities";
|
|
7
|
+
import {findSocketForPath} from "../../../core/socket";
|
|
8
|
+
import {Injection} from "../../../core/injection";
|
|
4
9
|
|
|
5
10
|
export const bunRuntime: RuntimeAdapterInterface = {
|
|
6
|
-
createServer(handler) {
|
|
11
|
+
createServer(handler, options?: RuntimeServerOptionsInterface) {
|
|
7
12
|
if (typeof Bun === 'undefined') throw new Error(
|
|
8
13
|
'bun is not installed, please run `npm install bun`'
|
|
9
14
|
)
|
|
10
15
|
|
|
11
16
|
let server: any
|
|
12
17
|
|
|
18
|
+
const socketHandlers = {
|
|
19
|
+
async open(ws: any) {
|
|
20
|
+
try {
|
|
21
|
+
const entry = ws.data?.socketEntry
|
|
22
|
+
if (!entry) return
|
|
23
|
+
|
|
24
|
+
const instance: any = Injection.resolve(entry.construct)
|
|
25
|
+
if (!instance) return
|
|
26
|
+
|
|
27
|
+
ws.data.instance = instance
|
|
28
|
+
|
|
29
|
+
const connect = entry.metadata.events.find((event: any) => event.type === 'connect')
|
|
30
|
+
if (connect) await instance[connect.propertyKey]?.()
|
|
31
|
+
} catch (e: any) {
|
|
32
|
+
Logger.error('Socket connect failed', e.message ?? e)
|
|
33
|
+
try { ws.close(1011, 'Internal error') } catch { /* noop */ }
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
async message(ws: any, message: any) {
|
|
37
|
+
try {
|
|
38
|
+
const instance = ws.data?.instance
|
|
39
|
+
if (!instance) return
|
|
40
|
+
|
|
41
|
+
let payload: any = message
|
|
42
|
+
if (typeof message === 'string') {
|
|
43
|
+
try { payload = JSON.parse(message) } catch { /* keep raw string */ }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const entry = ws.data.socketEntry
|
|
47
|
+
const eventName = payload && typeof payload === 'object' ? payload.event : undefined
|
|
48
|
+
const event = eventName
|
|
49
|
+
? entry.metadata.events.find((item: any) => item.name === eventName && (item.type === 'event' || item.type === 'message'))
|
|
50
|
+
: entry.metadata.events.find((item: any) => item.type === 'message')
|
|
51
|
+
|
|
52
|
+
if (!event) {
|
|
53
|
+
ws.send(JSON.stringify({
|
|
54
|
+
statusCode: 404,
|
|
55
|
+
message: `Socket event "${eventName ?? 'message'}" not found`,
|
|
56
|
+
data: null
|
|
57
|
+
}))
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const response = await instance[event.propertyKey]?.(payload?.data ?? payload)
|
|
62
|
+
if (response !== undefined) {
|
|
63
|
+
ws.send(JSON.stringify(response))
|
|
64
|
+
}
|
|
65
|
+
} catch (e: any) {
|
|
66
|
+
Logger.error('Socket message failed', e.message ?? e)
|
|
67
|
+
ws.send(JSON.stringify({
|
|
68
|
+
statusCode: 500,
|
|
69
|
+
message: 'Internal server error',
|
|
70
|
+
data: null
|
|
71
|
+
}))
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
async close(ws: any) {
|
|
75
|
+
try {
|
|
76
|
+
const instance = ws.data?.instance
|
|
77
|
+
if (!instance) return
|
|
78
|
+
|
|
79
|
+
const entry = ws.data.socketEntry
|
|
80
|
+
const disconnect = entry.metadata.events.find((event: any) => event.type === 'disconnect')
|
|
81
|
+
if (disconnect) await instance[disconnect.propertyKey]?.()
|
|
82
|
+
} catch (e: any) {
|
|
83
|
+
Logger.error('Socket disconnect failed', e.message ?? e)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
13
88
|
return {
|
|
14
89
|
async listen(port, hostname) {
|
|
15
90
|
server = Bun.serve({
|
|
16
91
|
port,
|
|
17
92
|
hostname,
|
|
93
|
+
websocket: socketHandlers,
|
|
18
94
|
fetch: async (request: Request, server: any) => {
|
|
95
|
+
const upgrade = request.headers.get('upgrade')?.toLowerCase()
|
|
96
|
+
|
|
97
|
+
if (upgrade === 'websocket') {
|
|
98
|
+
const pathname = new URL(request.url).pathname
|
|
99
|
+
const socketEntry = findSocketForPath(pathname, options?.prefix)
|
|
100
|
+
|
|
101
|
+
if (socketEntry) {
|
|
102
|
+
if (server.upgrade(request, {
|
|
103
|
+
data: {
|
|
104
|
+
socketEntry,
|
|
105
|
+
url: request.url,
|
|
106
|
+
headers: request.headers
|
|
107
|
+
}
|
|
108
|
+
})) {
|
|
109
|
+
return undefined
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return new Response('WebSocket upgrade failed', {status: 400})
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
19
116
|
let responseBody: any
|
|
20
117
|
let statusCode = 200
|
|
21
118
|
const headers = new Headers()
|
|
@@ -79,4 +176,4 @@ export const bunRuntime: RuntimeAdapterInterface = {
|
|
|
79
176
|
}
|
|
80
177
|
}
|
|
81
178
|
}
|
|
82
|
-
}
|
|
179
|
+
}
|
|
@@ -2,7 +2,8 @@ import type {
|
|
|
2
2
|
RuntimeAdapterInterface,
|
|
3
3
|
RuntimeHandlerCallable,
|
|
4
4
|
RuntimeInterface,
|
|
5
|
-
RuntimeServerInterface
|
|
5
|
+
RuntimeServerInterface,
|
|
6
|
+
RuntimeServerOptionsInterface
|
|
6
7
|
} from "../../types";
|
|
7
8
|
import {RuntimeType} from "../enums/runtime.enum";
|
|
8
9
|
import {nodeRuntime} from "./node/server";
|
|
@@ -46,7 +47,7 @@ export class Runtime implements RuntimeInterface {
|
|
|
46
47
|
}
|
|
47
48
|
}
|
|
48
49
|
|
|
49
|
-
createServer(handler: RuntimeHandlerCallable): RuntimeServerInterface {
|
|
50
|
-
return this.adapter().createServer(handler)
|
|
50
|
+
createServer(handler: RuntimeHandlerCallable, options?: RuntimeServerOptionsInterface): RuntimeServerInterface {
|
|
51
|
+
return this.adapter().createServer(handler, options)
|
|
51
52
|
}
|
|
52
53
|
}
|
|
@@ -2,6 +2,8 @@ import type {ParamMetaInterface} from "../../types";
|
|
|
2
2
|
import {METADATA_KEYS, Parametrable} from "../index";
|
|
3
3
|
import {PluginScope} from "../../core";
|
|
4
4
|
import {RaitonConfig} from "../../core/config";
|
|
5
|
+
import {getSocketRegistry} from "../../core/socket";
|
|
6
|
+
import {RaitonThread} from "../../core/thread";
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* Generate the OpenAPI specification object
|
|
@@ -78,17 +80,19 @@ export async function generateOpenApiSpec(scope: PluginScope, opts: any): Promis
|
|
|
78
80
|
responses: {}
|
|
79
81
|
};
|
|
80
82
|
|
|
81
|
-
const
|
|
83
|
+
const controllerPrototype = controllerClass?.prototype ?? controllerClass;
|
|
84
|
+
|
|
85
|
+
const operationMetadata = Reflect.getMetadata(METADATA_KEYS.API_OPERATION, controllerPrototype, routeMeta.propertyKey);
|
|
82
86
|
if (operationMetadata) {
|
|
83
87
|
Object.assign(operation, operationMetadata);
|
|
84
88
|
}
|
|
85
89
|
|
|
86
|
-
const methodTags = Reflect.getMetadata(METADATA_KEYS.API_TAGS,
|
|
90
|
+
const methodTags = Reflect.getMetadata(METADATA_KEYS.API_TAGS, controllerPrototype, routeMeta.propertyKey);
|
|
87
91
|
if (methodTags) {
|
|
88
92
|
operation.tags = methodTags;
|
|
89
93
|
}
|
|
90
94
|
|
|
91
|
-
const methodSecurity = Reflect.getMetadata(METADATA_KEYS.API_SECURITY,
|
|
95
|
+
const methodSecurity = Reflect.getMetadata(METADATA_KEYS.API_SECURITY, controllerPrototype, routeMeta.propertyKey);
|
|
92
96
|
if (methodSecurity) {
|
|
93
97
|
operation.security = methodSecurity;
|
|
94
98
|
}
|
|
@@ -103,7 +107,7 @@ export async function generateOpenApiSpec(scope: PluginScope, opts: any): Promis
|
|
|
103
107
|
operation.security = [...(operation.security || []), ...classSecurity];
|
|
104
108
|
}
|
|
105
109
|
|
|
106
|
-
const params: ParamMetaInterface[] = Reflect.getMetadata(METADATA_KEYS.ROUTE_PARAMETERS,
|
|
110
|
+
const params: ParamMetaInterface[] = Reflect.getMetadata(METADATA_KEYS.ROUTE_PARAMETERS, controllerPrototype)?.[routeMeta.propertyKey] || [];
|
|
107
111
|
|
|
108
112
|
let bodyParam: ParamMetaInterface | undefined;
|
|
109
113
|
const otherParams = params.filter(p => {
|
|
@@ -114,7 +118,7 @@ export async function generateOpenApiSpec(scope: PluginScope, opts: any): Promis
|
|
|
114
118
|
return true;
|
|
115
119
|
});
|
|
116
120
|
|
|
117
|
-
const apiParameters = Reflect.getMetadata(METADATA_KEYS.API_PARAMETERS,
|
|
121
|
+
const apiParameters = Reflect.getMetadata(METADATA_KEYS.API_PARAMETERS, controllerPrototype, routeMeta.propertyKey)?.[routeMeta.propertyKey] || [];
|
|
118
122
|
for (const param of apiParameters) {
|
|
119
123
|
let inLocation: string;
|
|
120
124
|
switch (param.in) {
|
|
@@ -145,78 +149,9 @@ export async function generateOpenApiSpec(scope: PluginScope, opts: any): Promis
|
|
|
145
149
|
};
|
|
146
150
|
|
|
147
151
|
if (param.type) {
|
|
148
|
-
|
|
149
|
-
|
|
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
|
-
}
|
|
152
|
+
paramObj.schema = getSchemaFromType(param.type);
|
|
153
|
+
if (param.default !== undefined) {
|
|
154
|
+
paramObj.schema.default = param.default;
|
|
220
155
|
}
|
|
221
156
|
}
|
|
222
157
|
|
|
@@ -281,7 +216,7 @@ export async function generateOpenApiSpec(scope: PluginScope, opts: any): Promis
|
|
|
281
216
|
}
|
|
282
217
|
|
|
283
218
|
// Check for explicit API request body decorator
|
|
284
|
-
const apiRequestBody = Reflect.getMetadata(METADATA_KEYS.API_REQUEST_BODY,
|
|
219
|
+
const apiRequestBody = Reflect.getMetadata(METADATA_KEYS.API_REQUEST_BODY, controllerPrototype, routeMeta.propertyKey);
|
|
285
220
|
if (apiRequestBody) {
|
|
286
221
|
// Use explicit API request body definition
|
|
287
222
|
const bodyRequired = apiRequestBody.required ?? true;
|
|
@@ -303,31 +238,8 @@ export async function generateOpenApiSpec(scope: PluginScope, opts: any): Promis
|
|
|
303
238
|
// Fallback to automatic body parameter detection
|
|
304
239
|
// Determine if the body param has a metatype that is a class (DTO)
|
|
305
240
|
let bodySchema: any = {type: 'object'}; // default
|
|
306
|
-
if (bodyParam.metatype
|
|
307
|
-
|
|
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
|
-
}
|
|
241
|
+
if (bodyParam.metatype) {
|
|
242
|
+
bodySchema = getSchemaFromType(bodyParam.metatype);
|
|
331
243
|
}
|
|
332
244
|
operation.requestBody = {
|
|
333
245
|
required: true,
|
|
@@ -340,7 +252,7 @@ export async function generateOpenApiSpec(scope: PluginScope, opts: any): Promis
|
|
|
340
252
|
}
|
|
341
253
|
|
|
342
254
|
// Check for API response decorators first
|
|
343
|
-
const apiResponses = Reflect.getMetadata(METADATA_KEYS.API_RESPONSES,
|
|
255
|
+
const apiResponses = Reflect.getMetadata(METADATA_KEYS.API_RESPONSES, controllerPrototype)?.[routeMeta.propertyKey] || [];
|
|
344
256
|
|
|
345
257
|
if (apiResponses.length > 0) {
|
|
346
258
|
// Use decorator-defined responses
|
|
@@ -367,7 +279,7 @@ export async function generateOpenApiSpec(scope: PluginScope, opts: any): Promis
|
|
|
367
279
|
}
|
|
368
280
|
} else {
|
|
369
281
|
// Fallback to return type detection
|
|
370
|
-
const returnType = Reflect.getMetadata('design:returntype',
|
|
282
|
+
const returnType = Reflect.getMetadata('design:returntype', controllerPrototype, routeMeta.propertyKey);
|
|
371
283
|
if (returnType) {
|
|
372
284
|
// Handle Promise<T> -> T
|
|
373
285
|
let resolvedType = returnType;
|
|
@@ -395,6 +307,94 @@ export async function generateOpenApiSpec(scope: PluginScope, opts: any): Promis
|
|
|
395
307
|
(spec.paths[path] as any)[method] = operation;
|
|
396
308
|
}
|
|
397
309
|
|
|
310
|
+
// Document registered WebSocket sockets as POST operations (one per event)
|
|
311
|
+
const prefix = RaitonThread.current?.application?.config?.prefix ?? '';
|
|
312
|
+
const sockets = getSocketRegistry();
|
|
313
|
+
|
|
314
|
+
for (const [, socketEntry] of sockets) {
|
|
315
|
+
const {namespace, construct, metadata} = socketEntry;
|
|
316
|
+
const classTags = Reflect.getMetadata(METADATA_KEYS.API_TAGS, construct) || [];
|
|
317
|
+
const socketPrototype = construct?.prototype ?? construct;
|
|
318
|
+
|
|
319
|
+
for (const event of metadata.events) {
|
|
320
|
+
if (event.type !== 'event' && event.type !== 'message') continue;
|
|
321
|
+
if (!event.name) continue;
|
|
322
|
+
|
|
323
|
+
const channelPath = `${prefix}${namespace}`.replace(/\/+/g, '/') || '/';
|
|
324
|
+
const eventPath = `${channelPath}/${event.name}`.replace(/\/+/g, '/');
|
|
325
|
+
|
|
326
|
+
if (!spec.paths[eventPath]) {
|
|
327
|
+
spec.paths[eventPath] = {};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const operation: any = {
|
|
331
|
+
parameters: [],
|
|
332
|
+
responses: {}
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
const operationMetadata = Reflect.getMetadata(METADATA_KEYS.API_OPERATION, socketPrototype, event.propertyKey);
|
|
336
|
+
if (operationMetadata) {
|
|
337
|
+
Object.assign(operation, operationMetadata);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const methodTags = Reflect.getMetadata(METADATA_KEYS.API_TAGS, socketPrototype, event.propertyKey);
|
|
341
|
+
operation.tags = [...(methodTags || []), ...classTags];
|
|
342
|
+
|
|
343
|
+
const apiResponses = Reflect.getMetadata(METADATA_KEYS.API_RESPONSES, socketPrototype)?.[event.propertyKey] || [];
|
|
344
|
+
|
|
345
|
+
if (apiResponses.length > 0) {
|
|
346
|
+
for (const response of apiResponses) {
|
|
347
|
+
const statusCode = String(response.status);
|
|
348
|
+
let responseContent = undefined;
|
|
349
|
+
|
|
350
|
+
if (response.type) {
|
|
351
|
+
const responseSchema = getSchemaFromType(response.type);
|
|
352
|
+
responseContent = response.isArray
|
|
353
|
+
? {type: 'array', items: responseSchema}
|
|
354
|
+
: responseSchema;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
operation.responses[statusCode] = {
|
|
358
|
+
description: response.description || '',
|
|
359
|
+
...(responseContent ? {content: {'application/json': {schema: responseContent}}} : {})
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
} else {
|
|
363
|
+
operation.responses['200'] = {
|
|
364
|
+
description: 'Successful response'
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const apiRequestBody = Reflect.getMetadata(METADATA_KEYS.API_REQUEST_BODY, socketPrototype, event.propertyKey);
|
|
369
|
+
const dataSchema = apiRequestBody?.type
|
|
370
|
+
? getSchemaFromType(apiRequestBody.type)
|
|
371
|
+
: {type: 'object'};
|
|
372
|
+
|
|
373
|
+
operation.requestBody = {
|
|
374
|
+
required: apiRequestBody?.required ?? true,
|
|
375
|
+
content: {
|
|
376
|
+
'application/json': {
|
|
377
|
+
schema: {
|
|
378
|
+
type: 'object',
|
|
379
|
+
properties: {
|
|
380
|
+
event: {type: 'string', enum: [event.name], example: event.name},
|
|
381
|
+
data: dataSchema
|
|
382
|
+
},
|
|
383
|
+
required: ['event', 'data']
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
operation['x-websocket'] = {
|
|
390
|
+
channel: channelPath,
|
|
391
|
+
event: event.name
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
(spec.paths[eventPath] as any).post = operation;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
398
|
return spec;
|
|
399
399
|
}
|
|
400
400
|
|
|
@@ -403,12 +403,16 @@ export async function generateOpenApiSpec(scope: PluginScope, opts: any): Promis
|
|
|
403
403
|
* This is a simplified implementation; for production use, consider using
|
|
404
404
|
* a library like @sinclair/typebox or class-transformer to generate schemas.
|
|
405
405
|
*/
|
|
406
|
-
export function getSchemaFromType(type: any): any {
|
|
406
|
+
export function getSchemaFromType(type: any, visited: Set<any> = new Set()): any {
|
|
407
407
|
if (typeof type !== 'function') {
|
|
408
408
|
// Not a constructor; fallback to basic type detection
|
|
409
409
|
return getPrimitiveTypeSchema(typeof type);
|
|
410
410
|
}
|
|
411
411
|
|
|
412
|
+
if (visited.has(type)) {
|
|
413
|
+
return {type: 'object', description: 'Circular reference'};
|
|
414
|
+
}
|
|
415
|
+
|
|
412
416
|
const typeName = type.name;
|
|
413
417
|
switch (typeName) {
|
|
414
418
|
case 'String':
|
|
@@ -430,9 +434,50 @@ export function getSchemaFromType(type: any): any {
|
|
|
430
434
|
format: 'date-time'
|
|
431
435
|
};
|
|
432
436
|
default:
|
|
433
|
-
// For custom classes (DTOs, entities, etc.),
|
|
434
|
-
//
|
|
435
|
-
|
|
437
|
+
// For custom classes (DTOs, entities, etc.), check for @ApiProperty
|
|
438
|
+
// We use Reflect.getMetadata to support inheritance of properties
|
|
439
|
+
const properties = Reflect.getMetadata(METADATA_KEYS.API_PROPERTY, type);
|
|
440
|
+
|
|
441
|
+
if (properties) {
|
|
442
|
+
visited.add(type);
|
|
443
|
+
const schema: any = {
|
|
444
|
+
type: 'object',
|
|
445
|
+
properties: {},
|
|
446
|
+
required: []
|
|
447
|
+
};
|
|
448
|
+
|
|
449
|
+
for (const [key, options] of Object.entries(properties) as [string, any][]) {
|
|
450
|
+
let propSchema: any;
|
|
451
|
+
const propType = options.type;
|
|
452
|
+
|
|
453
|
+
if (options.isArray) {
|
|
454
|
+
propSchema = {
|
|
455
|
+
type: 'array',
|
|
456
|
+
items: propType ? getSchemaFromType(propType, new Set(visited)) : {type: 'string'}
|
|
457
|
+
};
|
|
458
|
+
} else if (propType) {
|
|
459
|
+
propSchema = getSchemaFromType(propType, new Set(visited));
|
|
460
|
+
} else {
|
|
461
|
+
propSchema = {type: 'string'};
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
if (options.description) propSchema.description = options.description;
|
|
465
|
+
if (options.example) propSchema.example = options.example;
|
|
466
|
+
if (options.enum) propSchema.enum = options.enum;
|
|
467
|
+
|
|
468
|
+
schema.properties[key] = propSchema;
|
|
469
|
+
if (options.required) {
|
|
470
|
+
schema.required.push(key);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
if (schema.required.length === 0) {
|
|
475
|
+
delete schema.required;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
return schema;
|
|
479
|
+
}
|
|
480
|
+
|
|
436
481
|
return {type: 'object'};
|
|
437
482
|
}
|
|
438
483
|
}
|
package/source/types/raiton.ts
CHANGED
package/source/types/runtime.ts
CHANGED
|
@@ -8,6 +8,10 @@ export interface RuntimeServerInterface {
|
|
|
8
8
|
handle?(request: Request): Promise<Response>
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
export interface RuntimeServerOptionsInterface {
|
|
12
|
+
prefix?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
11
15
|
export interface RuntimeRequestInterface {
|
|
12
16
|
method: string
|
|
13
17
|
url: string
|
|
@@ -38,7 +42,7 @@ export interface RuntimeReplyInterface {
|
|
|
38
42
|
}
|
|
39
43
|
|
|
40
44
|
export interface RuntimeAdapterInterface {
|
|
41
|
-
createServer(handler: RuntimeHandlerCallable): RuntimeServerInterface
|
|
45
|
+
createServer(handler: RuntimeHandlerCallable, options?: RuntimeServerOptionsInterface): RuntimeServerInterface
|
|
42
46
|
}
|
|
43
47
|
|
|
44
48
|
|