framework-do-dede 2.0.21 → 3.0.1
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/dist/application/controller.d.ts +70 -0
- package/dist/{decorators → application}/controller.js +45 -33
- package/dist/application/entity.d.ts +21 -0
- package/dist/application/entity.js +161 -0
- package/dist/application/index.d.ts +6 -0
- package/dist/application/index.js +5 -0
- package/dist/{protocols/StorageGateway.d.ts → application/services.d.ts} +2 -0
- package/dist/application/services.js +20 -0
- package/dist/application/usecase.d.ts +11 -0
- package/dist/application/usecase.js +24 -0
- package/dist/dede.d.ts +5 -4
- package/dist/dede.js +22 -26
- package/dist/{handlers → http}/controller.handler.d.ts +2 -2
- package/dist/http/controller.handler.js +160 -0
- package/dist/http/{ExpressHttpServer.d.ts → elysia-server.adapter.d.ts} +2 -2
- package/dist/http/{ElysiaHttpServer.js → elysia-server.adapter.js} +2 -2
- package/dist/http/{ElysiaHttpServer.d.ts → express-server.adapter.d.ts} +2 -2
- package/dist/http/express-server.adapter.js +25 -0
- package/dist/http/{HttpServer.d.ts → http-server.d.ts} +13 -0
- package/dist/http/{HttpServer.js → http-server.js} +4 -2
- package/dist/http/index.d.ts +2 -2
- package/dist/http/index.js +2 -2
- package/dist/index.d.ts +2 -7
- package/dist/index.js +9 -6
- package/dist/infra/di/registry.d.ts +11 -0
- package/dist/infra/di/registry.js +32 -0
- package/package.json +5 -4
- package/dist/decorators/controller.d.ts +0 -38
- package/dist/decorators/di.d.ts +0 -1
- package/dist/decorators/di.js +0 -7
- package/dist/decorators/entity.d.ts +0 -12
- package/dist/decorators/entity.js +0 -65
- package/dist/decorators/index.d.ts +0 -6
- package/dist/decorators/index.js +0 -6
- package/dist/decorators/services.d.ts +0 -1
- package/dist/decorators/services.js +0 -27
- package/dist/decorators/usecase.d.ts +0 -4
- package/dist/decorators/usecase.js +0 -15
- package/dist/di/registry.d.ts +0 -17
- package/dist/di/registry.js +0 -51
- package/dist/domain/Entity.d.ts +0 -5
- package/dist/domain/Entity.js +0 -132
- package/dist/handlers/controller.handler.js +0 -215
- package/dist/handlers/index.d.ts +0 -3
- package/dist/handlers/index.js +0 -3
- package/dist/handlers/usecase.handler.d.ts +0 -4
- package/dist/handlers/usecase.handler.js +0 -43
- package/dist/http/ExpressHttpServer.js +0 -19
- package/dist/protocols/Controller.d.ts +0 -17
- package/dist/protocols/Controller.js +0 -1
- package/dist/protocols/CreateRepository.d.ts +0 -4
- package/dist/protocols/CreateRepository.js +0 -1
- package/dist/protocols/DeleteRepository.d.ts +0 -3
- package/dist/protocols/DeleteRepository.js +0 -1
- package/dist/protocols/DeleteRepositoryBy.d.ts +0 -3
- package/dist/protocols/DeleteRepositoryBy.js +0 -1
- package/dist/protocols/ExistsBy.d.ts +0 -3
- package/dist/protocols/ExistsBy.js +0 -1
- package/dist/protocols/HttpMiddleware.d.ts +0 -3
- package/dist/protocols/HttpMiddleware.js +0 -1
- package/dist/protocols/HttpServerError.d.ts +0 -5
- package/dist/protocols/HttpServerError.js +0 -1
- package/dist/protocols/NotExistsBy.d.ts +0 -3
- package/dist/protocols/NotExistsBy.js +0 -1
- package/dist/protocols/Request.d.ts +0 -5
- package/dist/protocols/Request.js +0 -1
- package/dist/protocols/RequestMetrics.d.ts +0 -18
- package/dist/protocols/RequestMetrics.js +0 -1
- package/dist/protocols/RequestMetricsHandler.d.ts +0 -5
- package/dist/protocols/RequestMetricsHandler.js +0 -1
- package/dist/protocols/RestoreManyRepository.d.ts +0 -9
- package/dist/protocols/RestoreManyRepository.js +0 -1
- package/dist/protocols/RestoreRepository.d.ts +0 -3
- package/dist/protocols/RestoreRepository.js +0 -1
- package/dist/protocols/RestoreRepositoryBy.d.ts +0 -3
- package/dist/protocols/RestoreRepositoryBy.js +0 -1
- package/dist/protocols/StorageGateway.js +0 -1
- package/dist/protocols/UpdateRepository.d.ts +0 -4
- package/dist/protocols/UpdateRepository.js +0 -1
- package/dist/protocols/UseCase.d.ts +0 -3
- package/dist/protocols/UseCase.js +0 -1
- package/dist/protocols/Validation.d.ts +0 -3
- package/dist/protocols/Validation.js +0 -1
- package/dist/protocols/index.d.ts +0 -18
- package/dist/protocols/index.js +0 -1
- package/dist/utils/Log.d.ts +0 -6
- package/dist/utils/Log.js +0 -42
- /package/dist/http/{FrameworkError.d.ts → errors/framework.d.ts} +0 -0
- /package/dist/http/{FrameworkError.js → errors/framework.js} +0 -0
- /package/dist/http/{ServerError.d.ts → errors/server.d.ts} +0 -0
- /package/dist/http/{ServerError.js → errors/server.js} +0 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { InternalServerError, ServerError } from "../http";
|
|
2
|
+
import { flushControllers, getControllers } from "../application/controller";
|
|
3
|
+
export default class ControllerHandler {
|
|
4
|
+
constructor(httpServer, port) {
|
|
5
|
+
for (const { handler, middlewares, method, route, statusCode, params, query, headers, responseType } of this.registryControllers()) {
|
|
6
|
+
httpServer.register({
|
|
7
|
+
method,
|
|
8
|
+
route,
|
|
9
|
+
handler,
|
|
10
|
+
statusCode,
|
|
11
|
+
params,
|
|
12
|
+
query,
|
|
13
|
+
headers,
|
|
14
|
+
middlewares,
|
|
15
|
+
responseType
|
|
16
|
+
}, async (input) => {
|
|
17
|
+
let requestedAt = new Date();
|
|
18
|
+
let startTime = 0;
|
|
19
|
+
let endTime = 0;
|
|
20
|
+
let mergedParams;
|
|
21
|
+
let request;
|
|
22
|
+
let capturedError;
|
|
23
|
+
let middlewaresExecuted = [];
|
|
24
|
+
try {
|
|
25
|
+
startTime = performance.now();
|
|
26
|
+
const filterParams = this.filter(input.params, params);
|
|
27
|
+
const filterQueryParams = this.filter(input.query, query);
|
|
28
|
+
const filterHeaders = this.filter(input.headers, headers);
|
|
29
|
+
mergedParams = { ...filterHeaders, ...filterParams, ...filterQueryParams, ...(input.body || {}) };
|
|
30
|
+
request = { data: mergedParams, context: {} };
|
|
31
|
+
middlewaresExecuted = await this.executeMiddlewares(middlewares, request);
|
|
32
|
+
const response = await handler.instance[handler.methodName](request);
|
|
33
|
+
endTime = performance.now();
|
|
34
|
+
return response;
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
capturedError = this.extractError(error, httpServer);
|
|
38
|
+
input.setStatus(capturedError.statusCode);
|
|
39
|
+
endTime = performance.now();
|
|
40
|
+
return {
|
|
41
|
+
message: capturedError.message,
|
|
42
|
+
statusCode: capturedError.statusCode
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
finally {
|
|
46
|
+
if (handler.tracer) {
|
|
47
|
+
const elapsedTime = `${(endTime - startTime).toFixed(2)} ms`;
|
|
48
|
+
const controllerData = {
|
|
49
|
+
requestedAt,
|
|
50
|
+
elapsedTime,
|
|
51
|
+
route,
|
|
52
|
+
method,
|
|
53
|
+
middlewares: middlewaresExecuted,
|
|
54
|
+
error: capturedError,
|
|
55
|
+
headers: input.headers
|
|
56
|
+
};
|
|
57
|
+
await handler.tracer.trace(controllerData);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
httpServer.listen(port);
|
|
63
|
+
}
|
|
64
|
+
async executeMiddlewares(middlewares = [], request) {
|
|
65
|
+
const executed = [];
|
|
66
|
+
if (middlewares && middlewares.length > 0) {
|
|
67
|
+
for (const middleware of middlewares) {
|
|
68
|
+
let startTime = 0;
|
|
69
|
+
let endTime = 0;
|
|
70
|
+
let elapsedTime;
|
|
71
|
+
try {
|
|
72
|
+
startTime = performance.now();
|
|
73
|
+
const middlewareResult = await middleware.execute(request);
|
|
74
|
+
request.context = Object.assign(request.context, middlewareResult);
|
|
75
|
+
endTime = performance.now();
|
|
76
|
+
elapsedTime = `${(endTime - startTime).toFixed(2)} ms`;
|
|
77
|
+
executed.push({
|
|
78
|
+
elapsedTime,
|
|
79
|
+
middleware: middleware.constructor.name
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
elapsedTime = `${(endTime - startTime).toFixed(2)} ms`;
|
|
84
|
+
executed.push({
|
|
85
|
+
elapsedTime,
|
|
86
|
+
middleware: middleware.constructor.name,
|
|
87
|
+
error
|
|
88
|
+
});
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return executed;
|
|
94
|
+
}
|
|
95
|
+
registryControllers() {
|
|
96
|
+
const controllers = [];
|
|
97
|
+
for (const controller of getControllers()) {
|
|
98
|
+
const basePath = Reflect.getMetadata('basePath', controller);
|
|
99
|
+
const methodNames = Object.getOwnPropertyNames(controller.prototype).filter(method => method !== 'constructor');
|
|
100
|
+
let tracer = Reflect.getMetadata('tracer', controller) || null;
|
|
101
|
+
const instance = new controller();
|
|
102
|
+
for (const methodName of methodNames) {
|
|
103
|
+
const routeConfig = Reflect.getMetadata('route', controller.prototype, methodName);
|
|
104
|
+
const middlewares = Reflect.getMetadata('middlewares', controller.prototype, methodName);
|
|
105
|
+
const responseType = Reflect.getMetadata('responseType', controller.prototype, methodName) || 'json';
|
|
106
|
+
tracer = Reflect.getMetadata('tracer', controller.prototype, methodName) || tracer;
|
|
107
|
+
controllers.push({
|
|
108
|
+
method: routeConfig.method,
|
|
109
|
+
route: basePath + routeConfig.path,
|
|
110
|
+
params: routeConfig.params,
|
|
111
|
+
query: routeConfig.query,
|
|
112
|
+
headers: routeConfig.headers,
|
|
113
|
+
statusCode: routeConfig.statusCode,
|
|
114
|
+
handler: {
|
|
115
|
+
instance,
|
|
116
|
+
methodName,
|
|
117
|
+
tracer
|
|
118
|
+
},
|
|
119
|
+
responseType,
|
|
120
|
+
middlewares: middlewares ? middlewares.map(middleware => new middleware()) : [],
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
flushControllers();
|
|
125
|
+
return controllers;
|
|
126
|
+
}
|
|
127
|
+
filter(params, filterParams) {
|
|
128
|
+
const filter = {};
|
|
129
|
+
for (const paramName of filterParams || []) {
|
|
130
|
+
const [paramNameFiltered, type] = paramName.split('|');
|
|
131
|
+
let value = params[paramName] || params[paramNameFiltered];
|
|
132
|
+
if (!value)
|
|
133
|
+
continue;
|
|
134
|
+
if (type === 'boolean')
|
|
135
|
+
value = value === 'true';
|
|
136
|
+
if (type === 'integer') {
|
|
137
|
+
value = value.replace(/[^0-9]/g, '');
|
|
138
|
+
value = value ? parseInt(value) : 0;
|
|
139
|
+
}
|
|
140
|
+
if (type === 'string')
|
|
141
|
+
value = value.toString();
|
|
142
|
+
filter[paramNameFiltered] = value;
|
|
143
|
+
}
|
|
144
|
+
return filter;
|
|
145
|
+
}
|
|
146
|
+
extractError(error, httpServer) {
|
|
147
|
+
if (error instanceof ServerError) {
|
|
148
|
+
return {
|
|
149
|
+
message: error.message,
|
|
150
|
+
statusCode: error.getStatusCode()
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
error = new InternalServerError(error.message, httpServer.getDefaultMessageError());
|
|
154
|
+
return {
|
|
155
|
+
message: error.message,
|
|
156
|
+
statusCode: error.getStatusCode(),
|
|
157
|
+
unexpectedError: error instanceof InternalServerError ? error.getUnexpectedError() : undefined,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import HttpServer from "./
|
|
2
|
-
export declare class
|
|
1
|
+
import HttpServer from "./http-server";
|
|
2
|
+
export declare class ElysiaServerAdapter extends HttpServer {
|
|
3
3
|
close(): Promise<void>;
|
|
4
4
|
constructor(uses?: CallableFunction[]);
|
|
5
5
|
listen(port: number): void;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @ts-ignore
|
|
2
2
|
import Elysia from "elysia";
|
|
3
|
-
import HttpServer from "./
|
|
4
|
-
export class
|
|
3
|
+
import HttpServer from "./http-server";
|
|
4
|
+
export class ElysiaServerAdapter extends HttpServer {
|
|
5
5
|
async close() {
|
|
6
6
|
await this.framework.stop();
|
|
7
7
|
console.log('server closed');
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import HttpServer from "./
|
|
2
|
-
export declare class
|
|
1
|
+
import HttpServer from "./http-server";
|
|
2
|
+
export declare class ExpressServerAdapter extends HttpServer {
|
|
3
3
|
close(): Promise<void>;
|
|
4
4
|
constructor(uses?: CallableFunction[]);
|
|
5
5
|
listen(port: number): void;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// @ts-ignore
|
|
2
|
+
import express from "express";
|
|
3
|
+
import HttpServer from "./http-server";
|
|
4
|
+
const app = express();
|
|
5
|
+
export class ExpressServerAdapter extends HttpServer {
|
|
6
|
+
async close() {
|
|
7
|
+
const shutDown = () => {
|
|
8
|
+
this.framework.close(() => {
|
|
9
|
+
console.log("Server closed. Cleanup complete.");
|
|
10
|
+
process.exit(0);
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
process.on("SIGINT", shutDown);
|
|
14
|
+
process.on("SIGTERM", shutDown);
|
|
15
|
+
}
|
|
16
|
+
constructor(uses) {
|
|
17
|
+
super(app, 'express');
|
|
18
|
+
this.framework.use(express.json());
|
|
19
|
+
uses?.forEach(use => this.framework.use(use));
|
|
20
|
+
}
|
|
21
|
+
listen(port) {
|
|
22
|
+
super.listen(port);
|
|
23
|
+
console.log(`Server listening on port ${port}`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -1,11 +1,24 @@
|
|
|
1
|
+
import { Middleware, Tracer } from "../application/controller";
|
|
2
|
+
export type Request = {
|
|
3
|
+
data: any;
|
|
4
|
+
context: any;
|
|
5
|
+
};
|
|
1
6
|
export type HttpStatusCode = 200 | 201 | 204 | 401 | 403 | 404 | 409 | 422 | 500;
|
|
2
7
|
export type AllowedMethods = 'get' | 'post' | 'put' | 'delete' | 'patch';
|
|
3
8
|
export type HttpServerParams = {
|
|
4
9
|
method: AllowedMethods;
|
|
5
10
|
route: string;
|
|
11
|
+
handler: {
|
|
12
|
+
instance: any;
|
|
13
|
+
methodName: string;
|
|
14
|
+
tracer?: Tracer;
|
|
15
|
+
};
|
|
16
|
+
responseType: 'json' | 'text' | 'html';
|
|
17
|
+
middlewares?: Middleware[];
|
|
6
18
|
statusCode?: number;
|
|
7
19
|
params?: string[];
|
|
8
20
|
query?: string[];
|
|
21
|
+
headers?: string[];
|
|
9
22
|
};
|
|
10
23
|
type FrameworkWeb = {
|
|
11
24
|
listen(port: number): void;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FrameworkError } from "./
|
|
1
|
+
import { FrameworkError } from "./errors/framework";
|
|
2
2
|
export default class HttpServer {
|
|
3
3
|
framework;
|
|
4
4
|
frameworkName;
|
|
@@ -65,7 +65,9 @@ export default class HttpServer {
|
|
|
65
65
|
params: request.params,
|
|
66
66
|
body: method !== 'get' ? request.body : {}
|
|
67
67
|
});
|
|
68
|
-
|
|
68
|
+
const type = httpServerParams.responseType === 'html' ? 'text' : httpServerParams.responseType;
|
|
69
|
+
return res[type](output);
|
|
69
70
|
});
|
|
70
71
|
}
|
|
72
|
+
;
|
|
71
73
|
}
|
package/dist/http/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import HttpServer from "./
|
|
2
|
-
import { ServerError, NotFound, Forbidden, Conflict, Unauthorized, UnprocessableEntity, BadRequest, InternalServerError } from "./
|
|
1
|
+
import HttpServer from "./http-server";
|
|
2
|
+
import { ServerError, NotFound, Forbidden, Conflict, Unauthorized, UnprocessableEntity, BadRequest, InternalServerError } from "./errors/server";
|
|
3
3
|
export { ServerError, BadRequest, NotFound, Forbidden, Conflict, Unauthorized, UnprocessableEntity, HttpServer, InternalServerError };
|
package/dist/http/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import HttpServer from "./
|
|
2
|
-
import { ServerError, NotFound, Forbidden, Conflict, Unauthorized, UnprocessableEntity, BadRequest, InternalServerError } from "./
|
|
1
|
+
import HttpServer from "./http-server";
|
|
2
|
+
import { ServerError, NotFound, Forbidden, Conflict, Unauthorized, UnprocessableEntity, BadRequest, InternalServerError } from "./errors/server";
|
|
3
3
|
export { ServerError, BadRequest, NotFound, Forbidden, Conflict, Unauthorized, UnprocessableEntity, HttpServer, InternalServerError };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import { BadRequest, Conflict, Forbidden, HttpServer, NotFound, ServerError, Unauthorized, UnprocessableEntity } from './http';
|
|
4
|
-
import { Validation, HttpMiddleware, UseCase, CreateRepository, ExistsBy, NotExistsBy, DeleteRepository, DeleteRepositoryBy, UpdateRepository, RestoreRepository, RestoreRepositoryBy, RestoreManyRepository, RequestMetricsHandler, Request, RequestMetrics, HttpServerError, StorageGateway } from './protocols';
|
|
5
|
-
import { Entity } from './domain/Entity';
|
|
6
|
-
import { UseCaseHandler } from './handlers';
|
|
7
|
-
export { UseCase, HttpMiddleware, Validation, RequestMetricsHandler, RequestMetrics, HttpServerError, CreateRepository, DeleteRepository, DeleteRepositoryBy, UpdateRepository, RestoreRepository, RestoreRepositoryBy, ExistsBy, NotExistsBy, RestoreManyRepository, Request, Dede, DedeRegister, DedeOptions, UseCaseHandler, ServerError, BadRequest, Conflict, Forbidden, HttpServer, NotFound, Unauthorized, UnprocessableEntity, Controller, Post, Put, Get, Delete, Patch, Validator, Middleware, Middlewares, Context, DecorateUseCase, Inject, Entity, Restrict, DbColumn, VirtualProperty, Metrics, OffConsoleLog, StorageGateway, Storage, Expose };
|
|
1
|
+
import { Post, Get, Put, Delete, Patch, Controller, Input, Middleware, UseMiddleware, UseMiddlewares, Tracer, Tracing, TracerData, EntityIdentifier, Entity, Restrict, VirtualProperty, GetterPrefix, Serialize, Id, UseCase, DecorateUseCase, Storage } from "./application";
|
|
2
|
+
export { Controller, Post, Get, Put, Delete, Patch, Input, Middleware, UseMiddleware, UseMiddlewares, Tracer, Tracing, TracerData, EntityIdentifier, Entity, Restrict, VirtualProperty, GetterPrefix, Serialize, Id, UseCase, DecorateUseCase, Storage };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import {
|
|
2
|
+
// controller
|
|
3
|
+
Post, Get, Put, Delete, Patch, Controller, UseMiddleware, UseMiddlewares, Tracing, Entity, Restrict, VirtualProperty, GetterPrefix, Serialize, Id, DecorateUseCase,
|
|
4
|
+
// usecase
|
|
5
|
+
// storage
|
|
6
|
+
Storage
|
|
7
|
+
// storage
|
|
8
|
+
} from "./application";
|
|
9
|
+
export { Controller, Post, Get, Put, Delete, Patch, UseMiddleware, UseMiddlewares, Tracing, Entity, Restrict, VirtualProperty, GetterPrefix, Serialize, Id, DecorateUseCase, Storage };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
declare class ComponentRegistry {
|
|
2
|
+
private static instance;
|
|
3
|
+
private dependencies;
|
|
4
|
+
static getInstance(): ComponentRegistry;
|
|
5
|
+
load(name: string, dependency: any): void;
|
|
6
|
+
inject(name: string): any;
|
|
7
|
+
remove(name: string): void;
|
|
8
|
+
}
|
|
9
|
+
export declare const Registry: ComponentRegistry;
|
|
10
|
+
export declare function Inject(name: string): (target: any, propertyKey: string) => void;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
class ComponentRegistry {
|
|
2
|
+
static instance;
|
|
3
|
+
dependencies = new Map();
|
|
4
|
+
static getInstance() {
|
|
5
|
+
if (!this.instance) {
|
|
6
|
+
this.instance = new ComponentRegistry();
|
|
7
|
+
}
|
|
8
|
+
return this.instance;
|
|
9
|
+
}
|
|
10
|
+
load(name, dependency) {
|
|
11
|
+
this.dependencies.set(name, dependency);
|
|
12
|
+
}
|
|
13
|
+
inject(name) {
|
|
14
|
+
if (!this.dependencies.get(name))
|
|
15
|
+
throw new Error(`Dependency not found ${name}`);
|
|
16
|
+
return this.dependencies.get(name);
|
|
17
|
+
}
|
|
18
|
+
remove(name) {
|
|
19
|
+
this.dependencies.delete(name);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export const Registry = ComponentRegistry.getInstance();
|
|
23
|
+
export function Inject(name) {
|
|
24
|
+
return function (target, propertyKey) {
|
|
25
|
+
target[propertyKey] = new Proxy({}, {
|
|
26
|
+
get(_, propertyKey) {
|
|
27
|
+
const dependency = Registry.inject(name);
|
|
28
|
+
return dependency[propertyKey];
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "framework-do-dede",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"require": "./dist/index.cjs"
|
|
13
13
|
},
|
|
14
14
|
"scripts": {
|
|
15
|
-
"test": "jest",
|
|
15
|
+
"test": "jest && jest ./example/test",
|
|
16
16
|
"test:watch": "jest --watch",
|
|
17
17
|
"clean": "rimraf dist",
|
|
18
18
|
"build": "npm run clean && tsc -p tsconfig.build.json && tsc-alias",
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
"@babel/core": "^7.26.10",
|
|
37
37
|
"@babel/preset-env": "^7.26.9",
|
|
38
38
|
"@babel/preset-typescript": "^7.27.0",
|
|
39
|
+
"@types/bun": "^1.2.16",
|
|
39
40
|
"@types/jest": "^29.5.14",
|
|
40
41
|
"@types/reflect-metadata": "^0.1.0",
|
|
41
42
|
"jest": "^29.7.0",
|
|
@@ -45,8 +46,8 @@
|
|
|
45
46
|
"typescript": "^5.8.2"
|
|
46
47
|
},
|
|
47
48
|
"peerDependencies": {
|
|
48
|
-
"elysia": "^1.
|
|
49
|
-
"express": "^
|
|
49
|
+
"elysia": "^1.3.5",
|
|
50
|
+
"express": "^5.1.0",
|
|
50
51
|
"typescript": "^5.8.2"
|
|
51
52
|
},
|
|
52
53
|
"dependencies": {
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { Validation } from "../protocols/Validation";
|
|
2
|
-
import { HttpMiddleware, RequestMetricsHandler } from "../protocols";
|
|
3
|
-
export declare function Controller(basePath: string): (target: any) => void;
|
|
4
|
-
export declare function Middleware(middlewareClass: new (...args: any[]) => HttpMiddleware): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
5
|
-
export declare function Middlewares(middlewareClasses: (new (...args: any[]) => HttpMiddleware)[]): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
6
|
-
export declare function Post(config?: {
|
|
7
|
-
path?: string;
|
|
8
|
-
statusCode?: number;
|
|
9
|
-
params?: string[];
|
|
10
|
-
query?: string[];
|
|
11
|
-
}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
12
|
-
export declare function Get(config?: {
|
|
13
|
-
path?: string;
|
|
14
|
-
statusCode?: number;
|
|
15
|
-
params?: string[];
|
|
16
|
-
query?: string[];
|
|
17
|
-
}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
18
|
-
export declare function Put(config?: {
|
|
19
|
-
path?: string;
|
|
20
|
-
statusCode?: number;
|
|
21
|
-
params?: string[];
|
|
22
|
-
query?: string[];
|
|
23
|
-
}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
24
|
-
export declare function Patch(config?: {
|
|
25
|
-
path?: string;
|
|
26
|
-
statusCode?: number;
|
|
27
|
-
params?: string[];
|
|
28
|
-
query?: string[];
|
|
29
|
-
}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
30
|
-
export declare function Delete(config?: {
|
|
31
|
-
path?: string;
|
|
32
|
-
statusCode?: number;
|
|
33
|
-
params?: string[];
|
|
34
|
-
query?: string[];
|
|
35
|
-
}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
36
|
-
export declare function Validator(validationClass: new () => Validation): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
37
|
-
export declare function Metrics(...handlers: (new (...args: any[]) => RequestMetricsHandler)[]): MethodDecorator;
|
|
38
|
-
export declare function OffConsoleLog(): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
package/dist/decorators/di.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function Inject(token: string): (target: any, propertyKey: string | symbol | undefined, parameterIndex: number) => void;
|
package/dist/decorators/di.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export declare function Restrict(): (target: any, propertyKey: string) => void;
|
|
2
|
-
export type ExposeConfig = {
|
|
3
|
-
mapping?: string | Record<string, string>;
|
|
4
|
-
deserialize?: (value: any) => any | Promise<any>;
|
|
5
|
-
};
|
|
6
|
-
export declare function Expose(configOrMapping: ExposeConfig | string): PropertyDecorator;
|
|
7
|
-
export declare function VirtualProperty(propertyName: string): (target: any, methodName: string, descriptor: PropertyDescriptor) => void;
|
|
8
|
-
export type DbColumnConfig<T = any> = {
|
|
9
|
-
column?: string | Record<string, string>;
|
|
10
|
-
serialize?: (value: T, data: any) => any | Promise<any>;
|
|
11
|
-
};
|
|
12
|
-
export declare function DbColumn<T>(configOrColumn: DbColumnConfig<T> | string): PropertyDecorator;
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
export function Restrict() {
|
|
2
|
-
return function (target, propertyKey) {
|
|
3
|
-
if (!target.constructor._restrictedProperties) {
|
|
4
|
-
target.constructor._restrictedProperties = new Set();
|
|
5
|
-
}
|
|
6
|
-
target.constructor._restrictedProperties.add(propertyKey);
|
|
7
|
-
};
|
|
8
|
-
}
|
|
9
|
-
export function Expose(configOrMapping) {
|
|
10
|
-
return function (target, propertyKey) {
|
|
11
|
-
const ctor = target.constructor;
|
|
12
|
-
const configs = ctor._exposeConfigs || (ctor._exposeConfigs = new Map());
|
|
13
|
-
if (typeof configOrMapping === "string") {
|
|
14
|
-
configs.set(propertyKey, [
|
|
15
|
-
...(configs.get(propertyKey) || []),
|
|
16
|
-
{
|
|
17
|
-
mapping: configOrMapping,
|
|
18
|
-
deserialize: (value) => value
|
|
19
|
-
}
|
|
20
|
-
]);
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
configs.set(propertyKey, [
|
|
24
|
-
...(configs.get(propertyKey) || []),
|
|
25
|
-
{
|
|
26
|
-
mapping: configOrMapping.mapping,
|
|
27
|
-
deserialize: configOrMapping.deserialize || ((value) => value)
|
|
28
|
-
}
|
|
29
|
-
]);
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
export function VirtualProperty(propertyName) {
|
|
34
|
-
return function (target, methodName, descriptor) {
|
|
35
|
-
const ctor = target.constructor;
|
|
36
|
-
if (!ctor._virtualProperties) {
|
|
37
|
-
ctor._virtualProperties = new Map();
|
|
38
|
-
}
|
|
39
|
-
ctor._virtualProperties.set(propertyName, methodName);
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
export function DbColumn(configOrColumn) {
|
|
43
|
-
return function (target, propertyKey) {
|
|
44
|
-
const ctor = target.constructor;
|
|
45
|
-
const configs = ctor._dbColumnConfigs || (ctor._dbColumnConfigs = new Map());
|
|
46
|
-
if (typeof configOrColumn === "string") {
|
|
47
|
-
configs.set(propertyKey, [
|
|
48
|
-
...(configs.get(propertyKey) || []),
|
|
49
|
-
{
|
|
50
|
-
column: configOrColumn,
|
|
51
|
-
serialize: (value, constructorProps) => value
|
|
52
|
-
}
|
|
53
|
-
]);
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
configs.set(propertyKey, [
|
|
57
|
-
...(configs.get(propertyKey) || []),
|
|
58
|
-
{
|
|
59
|
-
column: configOrColumn.column,
|
|
60
|
-
serialize: configOrColumn.serialize || ((value, constructorProps) => value)
|
|
61
|
-
}
|
|
62
|
-
]);
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { Controller, Post, Get, Put, Delete, Patch, Validator, Middleware, Middlewares, Metrics, OffConsoleLog } from './controller';
|
|
2
|
-
import { Context, DecorateUseCase } from './usecase';
|
|
3
|
-
import { Inject } from './di';
|
|
4
|
-
import { Restrict, DbColumn, VirtualProperty, Expose } from './entity';
|
|
5
|
-
import { Storage } from './services';
|
|
6
|
-
export { Controller, Middleware, Middlewares, Validator, Metrics, OffConsoleLog, Post, Get, Put, Delete, Patch, Context, DecorateUseCase, Inject, Restrict, DbColumn, Storage, Expose, VirtualProperty };
|
package/dist/decorators/index.js
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { Controller, Post, Get, Put, Delete, Patch, Validator, Middleware, Middlewares, Metrics, OffConsoleLog } from './controller';
|
|
2
|
-
import { Context, DecorateUseCase } from './usecase';
|
|
3
|
-
import { Inject } from './di';
|
|
4
|
-
import { Restrict, DbColumn, VirtualProperty, Expose } from './entity';
|
|
5
|
-
import { Storage } from './services';
|
|
6
|
-
export { Controller, Middleware, Middlewares, Validator, Metrics, OffConsoleLog, Post, Get, Put, Delete, Patch, Context, DecorateUseCase, Inject, Restrict, DbColumn, Storage, Expose, VirtualProperty };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function Storage(gatewayName: string): (target: any, propertyKey: string) => void;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { Registry } from "../di/registry";
|
|
2
|
-
export function Storage(gatewayName) {
|
|
3
|
-
return function (target, propertyKey) {
|
|
4
|
-
const instanceSymbol = Symbol();
|
|
5
|
-
Object.defineProperty(target, propertyKey, {
|
|
6
|
-
get: function () {
|
|
7
|
-
if (!this[instanceSymbol]) {
|
|
8
|
-
// Lazy load the gateway when the property is first accessed
|
|
9
|
-
if (!Registry.has(gatewayName)) {
|
|
10
|
-
throw new Error(`StorageGateway ${gatewayName} not registered`);
|
|
11
|
-
}
|
|
12
|
-
const GatewayClass = Registry.resolve(gatewayName);
|
|
13
|
-
if (!GatewayClass.save || !GatewayClass.get || !GatewayClass.delete) {
|
|
14
|
-
throw new Error(`${gatewayName} is not a valid StorageGateway`);
|
|
15
|
-
}
|
|
16
|
-
this[instanceSymbol] = GatewayClass;
|
|
17
|
-
}
|
|
18
|
-
return this[instanceSymbol];
|
|
19
|
-
},
|
|
20
|
-
set: () => {
|
|
21
|
-
throw new Error('Cannot assign new value to @Storage() property');
|
|
22
|
-
},
|
|
23
|
-
enumerable: true,
|
|
24
|
-
configurable: true
|
|
25
|
-
});
|
|
26
|
-
};
|
|
27
|
-
}
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { UseCase } from "../protocols";
|
|
2
|
-
export declare const USE_CASE_DECORATORS: unique symbol;
|
|
3
|
-
export declare function Context(middlewareKey: string): (target: any, propertyKey: string) => void;
|
|
4
|
-
export declare function DecorateUseCase(useCases: UseCase<any, any> | (new (...args: any[]) => UseCase<any, any>) | Array<UseCase<any, any> | (new (...args: any[]) => UseCase<any, any>)>): ClassDecorator;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export const USE_CASE_DECORATORS = Symbol('useCaseDecorators');
|
|
2
|
-
export function Context(middlewareKey) {
|
|
3
|
-
return function (target, propertyKey) {
|
|
4
|
-
const metadata = Reflect.getMetadata('context', target.constructor) || [];
|
|
5
|
-
metadata.push({ propertyKey, middlewareKey });
|
|
6
|
-
Reflect.defineMetadata('context', metadata, target.constructor);
|
|
7
|
-
};
|
|
8
|
-
}
|
|
9
|
-
export function DecorateUseCase(useCases) {
|
|
10
|
-
return function (target) {
|
|
11
|
-
const decorators = Array.isArray(useCases) ? useCases : [useCases];
|
|
12
|
-
Reflect.defineMetadata(USE_CASE_DECORATORS, decorators, target);
|
|
13
|
-
return target;
|
|
14
|
-
};
|
|
15
|
-
}
|
package/dist/di/registry.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import 'reflect-metadata';
|
|
2
|
-
declare class ComponentRegistry {
|
|
3
|
-
private static instance;
|
|
4
|
-
private dependencies;
|
|
5
|
-
private isLoading;
|
|
6
|
-
static getInstance(): ComponentRegistry;
|
|
7
|
-
register(token: string, dependency: any): void;
|
|
8
|
-
has(token: string): boolean;
|
|
9
|
-
addDependency(token: string, dependency: any): void;
|
|
10
|
-
resolve<T>(token: string): T;
|
|
11
|
-
clear(token: string): void;
|
|
12
|
-
classLoader<T>(target: new (...args: any[]) => T): T;
|
|
13
|
-
inject(token: string): (target: any, propertyKey: string | symbol | undefined, parameterIndex: number) => void;
|
|
14
|
-
loaded(): void;
|
|
15
|
-
}
|
|
16
|
-
export declare const Registry: ComponentRegistry;
|
|
17
|
-
export {};
|
package/dist/di/registry.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { Inject } from '../decorators';
|
|
2
|
-
import 'reflect-metadata';
|
|
3
|
-
class ComponentRegistry {
|
|
4
|
-
static instance;
|
|
5
|
-
dependencies = new Map();
|
|
6
|
-
isLoading = true;
|
|
7
|
-
static getInstance() {
|
|
8
|
-
if (!this.instance) {
|
|
9
|
-
this.instance = new ComponentRegistry();
|
|
10
|
-
}
|
|
11
|
-
return this.instance;
|
|
12
|
-
}
|
|
13
|
-
register(token, dependency) {
|
|
14
|
-
this.dependencies.set(token, dependency);
|
|
15
|
-
}
|
|
16
|
-
has(token) {
|
|
17
|
-
return this.dependencies.has(token);
|
|
18
|
-
}
|
|
19
|
-
addDependency(token, dependency) {
|
|
20
|
-
if (!this.dependencies.has(token))
|
|
21
|
-
throw new Error(`Dependency ${token} not registered`);
|
|
22
|
-
if (!Array.isArray(this.dependencies.get(token)))
|
|
23
|
-
throw new Error("Dependency must be an array");
|
|
24
|
-
this.dependencies.get(token).push(dependency);
|
|
25
|
-
}
|
|
26
|
-
resolve(token) {
|
|
27
|
-
const dependency = this.dependencies.get(token);
|
|
28
|
-
if (!dependency) {
|
|
29
|
-
throw new Error(`Dependency ${token} not registered`);
|
|
30
|
-
}
|
|
31
|
-
return dependency;
|
|
32
|
-
}
|
|
33
|
-
clear(token) {
|
|
34
|
-
const dependency = this.dependencies.get(token);
|
|
35
|
-
if (!dependency)
|
|
36
|
-
return;
|
|
37
|
-
this.dependencies.set(token, null);
|
|
38
|
-
}
|
|
39
|
-
classLoader(target) {
|
|
40
|
-
const paramtypes = Reflect.getMetadata('injections', target) || [];
|
|
41
|
-
const args = paramtypes.map((token) => ComponentRegistry.getInstance().resolve(token));
|
|
42
|
-
return new target(...args);
|
|
43
|
-
}
|
|
44
|
-
inject(token) {
|
|
45
|
-
return Inject(token);
|
|
46
|
-
}
|
|
47
|
-
loaded() {
|
|
48
|
-
this.isLoading = false;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
export const Registry = ComponentRegistry.getInstance();
|