framework-do-dede 0.0.12 → 0.0.14
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/decorators/controller.d.ts +35 -0
- package/dist/decorators/controller.js +88 -0
- package/dist/decorators/di.d.ts +1 -0
- package/dist/decorators/di.js +7 -0
- package/dist/decorators/index.d.ts +4 -0
- package/dist/decorators/index.js +4 -0
- package/dist/decorators/usecase.d.ts +1 -0
- package/dist/decorators/usecase.js +5 -0
- package/dist/dede.d.ts +15 -44
- package/dist/dede.js +29 -0
- package/dist/di/registry.d.ts +15 -0
- package/dist/di/registry.js +47 -0
- package/dist/example.d.ts +0 -40
- package/dist/example.js +102 -0
- package/dist/handlers/controller.handler.d.ts +6 -0
- package/dist/handlers/controller.handler.js +77 -0
- package/dist/handlers/index.d.ts +2 -0
- package/dist/handlers/index.js +2 -0
- package/dist/handlers/usecase.handler.d.ts +5 -0
- package/dist/handlers/usecase.handler.js +12 -0
- package/dist/http/ElysiaHttpServer.d.ts +5 -0
- package/dist/http/ElysiaHttpServer.js +13 -0
- package/dist/http/ExpressHttpServer.d.ts +5 -0
- package/dist/http/ExpressHttpServer.js +14 -0
- package/dist/http/FrameworkError.d.ts +3 -0
- package/dist/http/FrameworkError.js +5 -0
- package/dist/http/HttpServer.d.ts +31 -0
- package/dist/http/HttpServer.js +91 -0
- package/dist/http/ServerError.d.ts +23 -0
- package/dist/http/ServerError.js +41 -0
- package/dist/http/index.d.ts +3 -0
- package/dist/http/index.js +3 -0
- package/dist/index.d.ts +14 -42
- package/dist/index.js +26 -15466
- package/dist/protocols/Controller.d.ts +14 -0
- package/dist/protocols/Controller.js +1 -0
- package/dist/protocols/HttpMiddleware.d.ts +3 -0
- package/dist/protocols/HttpMiddleware.js +1 -0
- package/dist/protocols/UseCase.d.ts +3 -0
- package/dist/protocols/UseCase.js +1 -0
- package/dist/protocols/Validation.d.ts +3 -0
- package/dist/protocols/Validation.js +1 -0
- package/dist/protocols/index.d.ts +4 -0
- package/dist/protocols/index.js +1 -0
- package/package.json +16 -6
- package/dist/Controller.d.ts +0 -13
- package/dist/ElysiaHttpServer.d.ts +0 -12
- package/dist/ExpressHttpServer.d.ts +0 -13
- package/dist/FrameworkError.d.ts +0 -5
- package/dist/HttpMiddleware.d.ts +0 -3
- package/dist/HttpServer.d.ts +0 -114
- package/dist/ServerError.d.ts +0 -42
- package/dist/UseCase.d.ts +0 -3
- package/dist/Validation.d.ts +0 -3
- package/dist/controller.d.ts +0 -11
- package/dist/controller.handler.d.ts +0 -86
- package/dist/di.d.ts +0 -1
- package/dist/index.cjs +0 -15485
- package/dist/registry.d.ts +0 -4
- package/dist/usecase.d.ts +0 -1
- package/dist/usecase.handler.d.ts +0 -23
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Validation } from "@/protocols/Validation";
|
|
2
|
+
import { HttpMiddleware } from "@/protocols";
|
|
3
|
+
export declare function Controller(basePath: string): (target: any) => void;
|
|
4
|
+
export declare function Middleware(middlewareClass: new () => HttpMiddleware): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
5
|
+
export declare function Post(config?: {
|
|
6
|
+
path?: string;
|
|
7
|
+
statusCode?: number;
|
|
8
|
+
params?: string[];
|
|
9
|
+
query?: string[];
|
|
10
|
+
}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
11
|
+
export declare function Get(config?: {
|
|
12
|
+
path?: string;
|
|
13
|
+
statusCode?: number;
|
|
14
|
+
params?: string[];
|
|
15
|
+
query?: string[];
|
|
16
|
+
}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
17
|
+
export declare function Put(config?: {
|
|
18
|
+
path?: string;
|
|
19
|
+
statusCode?: number;
|
|
20
|
+
params?: string[];
|
|
21
|
+
query?: string[];
|
|
22
|
+
}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
23
|
+
export declare function Patch(config?: {
|
|
24
|
+
path?: string;
|
|
25
|
+
statusCode?: number;
|
|
26
|
+
params?: string[];
|
|
27
|
+
query?: string[];
|
|
28
|
+
}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
29
|
+
export declare function Delete(config?: {
|
|
30
|
+
path?: string;
|
|
31
|
+
statusCode?: number;
|
|
32
|
+
params?: string[];
|
|
33
|
+
query?: string[];
|
|
34
|
+
}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
35
|
+
export declare function Validator(validationClass: new () => Validation): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { FrameworkError } from "@/http/FrameworkError";
|
|
2
|
+
import { Registry } from "@/di/registry";
|
|
3
|
+
const middlewares = new Map();
|
|
4
|
+
export function Controller(basePath) {
|
|
5
|
+
return function (target) {
|
|
6
|
+
Reflect.defineMetadata('basePath', basePath, target);
|
|
7
|
+
if (!Registry.has('controllers'))
|
|
8
|
+
Registry.register('controllers', []);
|
|
9
|
+
Registry.addDependency('controllers', target);
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export function Middleware(middlewareClass) {
|
|
13
|
+
return function (target, propertyKey, descriptor) {
|
|
14
|
+
if (typeof middlewareClass.prototype.execute !== 'function') {
|
|
15
|
+
throw new FrameworkError('The concrete class does not implement the Middleware interface.');
|
|
16
|
+
}
|
|
17
|
+
// Retrieve existing middlewares for this method or initialize an empty array
|
|
18
|
+
const middlewares = Reflect.getMetadata('middlewares', target, propertyKey) || [];
|
|
19
|
+
// Create a new instance of the middleware and add it to the array
|
|
20
|
+
const middlewareInstance = new middlewareClass();
|
|
21
|
+
middlewares.push(middlewareInstance);
|
|
22
|
+
// Update the metadata with the new array of middleware instances
|
|
23
|
+
Reflect.defineMetadata('middlewares', middlewares, target, propertyKey);
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export function Post(config = {}) {
|
|
27
|
+
return function (target, propertyKey, descriptor) {
|
|
28
|
+
Reflect.defineMetadata('route', {
|
|
29
|
+
method: 'post',
|
|
30
|
+
path: config.path || '',
|
|
31
|
+
params: config.params,
|
|
32
|
+
query: config.query,
|
|
33
|
+
statusCode: config.statusCode || 200
|
|
34
|
+
}, target, propertyKey);
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export function Get(config = {}) {
|
|
38
|
+
return function (target, propertyKey, descriptor) {
|
|
39
|
+
Reflect.defineMetadata('route', {
|
|
40
|
+
method: 'get',
|
|
41
|
+
path: config.path || '',
|
|
42
|
+
params: config.params,
|
|
43
|
+
query: config.query,
|
|
44
|
+
statusCode: config.statusCode || 200
|
|
45
|
+
}, target, propertyKey);
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export function Put(config = {}) {
|
|
49
|
+
return function (target, propertyKey, descriptor) {
|
|
50
|
+
Reflect.defineMetadata('route', {
|
|
51
|
+
method: 'put',
|
|
52
|
+
path: config.path || '',
|
|
53
|
+
params: config.params,
|
|
54
|
+
query: config.query,
|
|
55
|
+
statusCode: config.statusCode || 200
|
|
56
|
+
}, target, propertyKey);
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export function Patch(config = {}) {
|
|
60
|
+
return function (target, propertyKey, descriptor) {
|
|
61
|
+
Reflect.defineMetadata('route', {
|
|
62
|
+
method: 'patch',
|
|
63
|
+
path: config.path || '',
|
|
64
|
+
params: config.params,
|
|
65
|
+
query: config.query,
|
|
66
|
+
statusCode: config.statusCode || 200
|
|
67
|
+
}, target, propertyKey);
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
export function Delete(config = {}) {
|
|
71
|
+
return function (target, propertyKey, descriptor) {
|
|
72
|
+
Reflect.defineMetadata('route', {
|
|
73
|
+
method: 'delete',
|
|
74
|
+
path: config.path || '',
|
|
75
|
+
params: config.params,
|
|
76
|
+
query: config.query,
|
|
77
|
+
statusCode: config.statusCode || 200
|
|
78
|
+
}, target, propertyKey);
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
export function Validator(validationClass) {
|
|
82
|
+
return function (target, propertyKey, descriptor) {
|
|
83
|
+
if (typeof validationClass.prototype.validate !== 'function') {
|
|
84
|
+
throw new FrameworkError('the concrete class does not implement Validation class".');
|
|
85
|
+
}
|
|
86
|
+
Reflect.defineMetadata('validation', new validationClass(), target, propertyKey);
|
|
87
|
+
};
|
|
88
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function Inject(token: string): (target: any, propertyKey: string | symbol | undefined, parameterIndex: number) => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function Auth(propertyName?: string): Function;
|
package/dist/dede.d.ts
CHANGED
|
@@ -1,47 +1,18 @@
|
|
|
1
|
-
export
|
|
2
|
-
name: string
|
|
3
|
-
classLoader: any
|
|
4
|
-
autoLoad?: boolean
|
|
5
|
-
}
|
|
6
|
-
|
|
1
|
+
export type Register = {
|
|
2
|
+
name: string;
|
|
3
|
+
classLoader: any;
|
|
4
|
+
autoLoad?: boolean;
|
|
5
|
+
};
|
|
7
6
|
export type Options = {
|
|
8
7
|
framework: {
|
|
9
|
-
use: 'elysia' | 'express'
|
|
10
|
-
port?: number
|
|
11
|
-
middlewares?: CallableFunction[]
|
|
12
|
-
}
|
|
13
|
-
registries: Register[]
|
|
8
|
+
use: 'elysia' | 'express';
|
|
9
|
+
port?: number;
|
|
10
|
+
middlewares?: CallableFunction[];
|
|
11
|
+
};
|
|
12
|
+
registries: Register[];
|
|
13
|
+
};
|
|
14
|
+
export declare class Dede {
|
|
15
|
+
static init({ framework, registries }: Options): Promise<void>;
|
|
16
|
+
private static clearControllers;
|
|
17
|
+
private static loadRegistries;
|
|
14
18
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
export class Dede {
|
|
18
|
-
static async init ({ framework, registries }: Options): Promise<void> {
|
|
19
|
-
this.registerControllers();
|
|
20
|
-
await this.loadRegistries(registries);
|
|
21
|
-
let httpServer!: HttpServer
|
|
22
|
-
if (framework.use === 'elysia') {
|
|
23
|
-
httpServer = new ElysiaHttpServer(framework.middlewares || [])
|
|
24
|
-
}
|
|
25
|
-
if (framework.use === 'express') {
|
|
26
|
-
httpServer = new ExpressHttpServer(framework.middlewares || [])
|
|
27
|
-
}
|
|
28
|
-
new ControllerHandler(httpServer, framework.port || 80)
|
|
29
|
-
this.clearControllers()
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
private static registerControllers() {
|
|
34
|
-
Registry.register('controllers', []);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
private static clearControllers() {
|
|
38
|
-
Registry.clear('controllers');
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
private static async loadRegistries(registries: Register []) {
|
|
42
|
-
registries.forEach(({ classLoader, name, autoLoad = true}) => {
|
|
43
|
-
if (autoLoad) Registry.register(name, Registry.classLoader(classLoader));
|
|
44
|
-
else Registry.register(name, classLoader);
|
|
45
|
-
})
|
|
46
|
-
}
|
|
47
|
-
}
|
package/dist/dede.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Registry } from "./di/registry";
|
|
2
|
+
import { ControllerHandler } from "./handlers";
|
|
3
|
+
import { ElysiaHttpServer } from "./http/ElysiaHttpServer";
|
|
4
|
+
import { ExpressHttpServer } from "./http/ExpressHttpServer";
|
|
5
|
+
export class Dede {
|
|
6
|
+
static async init({ framework, registries }) {
|
|
7
|
+
await this.loadRegistries(registries);
|
|
8
|
+
let httpServer;
|
|
9
|
+
if (framework.use === 'elysia') {
|
|
10
|
+
httpServer = new ElysiaHttpServer(framework.middlewares || []);
|
|
11
|
+
}
|
|
12
|
+
if (framework.use === 'express') {
|
|
13
|
+
httpServer = new ExpressHttpServer(framework.middlewares || []);
|
|
14
|
+
}
|
|
15
|
+
new ControllerHandler(httpServer, framework.port || 80);
|
|
16
|
+
this.clearControllers();
|
|
17
|
+
}
|
|
18
|
+
static clearControllers() {
|
|
19
|
+
Registry.clear('controllers');
|
|
20
|
+
}
|
|
21
|
+
static async loadRegistries(registries) {
|
|
22
|
+
registries.forEach(({ classLoader, name, autoLoad = true }) => {
|
|
23
|
+
if (autoLoad)
|
|
24
|
+
Registry.register(name, Registry.classLoader(classLoader));
|
|
25
|
+
else
|
|
26
|
+
Registry.register(name, classLoader);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
declare class ComponentRegistry {
|
|
3
|
+
private static instance;
|
|
4
|
+
private dependencies;
|
|
5
|
+
static getInstance(): ComponentRegistry;
|
|
6
|
+
register(token: string, dependency: any): void;
|
|
7
|
+
has(token: string): boolean;
|
|
8
|
+
addDependency(token: string, dependency: any): void;
|
|
9
|
+
resolve<T>(token: string): T;
|
|
10
|
+
clear(token: string): void;
|
|
11
|
+
classLoader<T>(target: new (...args: any[]) => T): T;
|
|
12
|
+
inject(token: string): (target: any, propertyKey: string | symbol | undefined, parameterIndex: number) => void;
|
|
13
|
+
}
|
|
14
|
+
export declare const Registry: ComponentRegistry;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Inject } from '@/decorators';
|
|
2
|
+
import 'reflect-metadata';
|
|
3
|
+
class ComponentRegistry {
|
|
4
|
+
static instance;
|
|
5
|
+
dependencies = new Map();
|
|
6
|
+
static getInstance() {
|
|
7
|
+
if (!this.instance) {
|
|
8
|
+
this.instance = new ComponentRegistry();
|
|
9
|
+
}
|
|
10
|
+
return this.instance;
|
|
11
|
+
}
|
|
12
|
+
register(token, dependency) {
|
|
13
|
+
this.dependencies.set(token, dependency);
|
|
14
|
+
}
|
|
15
|
+
has(token) {
|
|
16
|
+
return this.dependencies.has(token);
|
|
17
|
+
}
|
|
18
|
+
addDependency(token, dependency) {
|
|
19
|
+
if (!this.dependencies.has(token))
|
|
20
|
+
throw new Error(`Dependency ${token} not registered`);
|
|
21
|
+
if (!Array.isArray(this.dependencies.get(token)))
|
|
22
|
+
throw new Error("Dependency must be an array");
|
|
23
|
+
this.dependencies.get(token).push(dependency);
|
|
24
|
+
}
|
|
25
|
+
resolve(token) {
|
|
26
|
+
const dependency = this.dependencies.get(token);
|
|
27
|
+
if (!dependency) {
|
|
28
|
+
throw new Error(`Dependency ${token} not registered`);
|
|
29
|
+
}
|
|
30
|
+
return dependency;
|
|
31
|
+
}
|
|
32
|
+
clear(token) {
|
|
33
|
+
const dependency = this.dependencies.get(token);
|
|
34
|
+
if (!dependency)
|
|
35
|
+
return;
|
|
36
|
+
this.dependencies.set(token, null);
|
|
37
|
+
}
|
|
38
|
+
classLoader(target) {
|
|
39
|
+
const paramtypes = Reflect.getMetadata('injections', target) || [];
|
|
40
|
+
const args = paramtypes.map((token) => ComponentRegistry.getInstance().resolve(token));
|
|
41
|
+
return new target(...args);
|
|
42
|
+
}
|
|
43
|
+
inject(token) {
|
|
44
|
+
return Inject(token);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
export const Registry = ComponentRegistry.getInstance();
|
package/dist/example.d.ts
CHANGED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
declare type Input = { name: string, age: number, email: string, password: string };
|
|
2
|
-
type Output = void;
|
|
3
|
-
declare interface UserRepository {
|
|
4
|
-
create: (input: any) => Promise<void>
|
|
5
|
-
}
|
|
6
|
-
declare type RequestData = {
|
|
7
|
-
headers: any,
|
|
8
|
-
data: any,
|
|
9
|
-
middlewareData: any
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
@Controller('/users')
|
|
14
|
-
class UserController {
|
|
15
|
-
|
|
16
|
-
@Post({ statusCode: 201 })
|
|
17
|
-
@Validator(ValidateUser)
|
|
18
|
-
@Middleware(UserAuth)
|
|
19
|
-
createUser(input: any, request: RequestData) {
|
|
20
|
-
console.log('request', request)
|
|
21
|
-
return UseCaseHandler.load(CreateUser, request).execute(input)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
Dede.init({
|
|
30
|
-
framework: {
|
|
31
|
-
port: 3000,
|
|
32
|
-
use: 'elysia'
|
|
33
|
-
},
|
|
34
|
-
registries: [
|
|
35
|
-
{
|
|
36
|
-
name: 'UserRepository',
|
|
37
|
-
classLoader: UserRepositoryDatabase
|
|
38
|
-
}
|
|
39
|
-
]
|
|
40
|
-
})
|
package/dist/example.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// import { Auth, Controller, Get, Inject, Middleware, Post, Validator } from "@/decorators";
|
|
3
|
+
// import ControllerHandler from "@/handlers/controller.handler";
|
|
4
|
+
// import { UseCaseHandler } from "@/handlers/usecase.handler";
|
|
5
|
+
// import HttpServer from "@/http/HttpServer";
|
|
6
|
+
// import { BadRequest } from "@/http/ServerError";
|
|
7
|
+
// import type { HttpMiddleware } from "@/protocols/HttpMiddleware";
|
|
8
|
+
// import type { UseCase } from "@/protocols/UseCase";
|
|
9
|
+
// import { Registry } from "@/di/registry";
|
|
10
|
+
// import Elysia from "elysia";
|
|
11
|
+
// import type { Validation } from "@/protocols";
|
|
12
|
+
// Registry.register('controllers', []);
|
|
13
|
+
// class ValidateUser implements Validation {
|
|
14
|
+
// validate(input: any) {
|
|
15
|
+
// if (!input.name) {
|
|
16
|
+
// throw new BadRequest('name is required')
|
|
17
|
+
// }
|
|
18
|
+
// return input
|
|
19
|
+
// }
|
|
20
|
+
// }
|
|
21
|
+
// class UserAuth implements HttpMiddleware {
|
|
22
|
+
// async execute(input: any) {
|
|
23
|
+
// return {
|
|
24
|
+
// auth: true
|
|
25
|
+
// }
|
|
26
|
+
// }
|
|
27
|
+
// }
|
|
28
|
+
// class Test implements HttpMiddleware {
|
|
29
|
+
// async execute(input: any) {
|
|
30
|
+
// return {
|
|
31
|
+
// test: true
|
|
32
|
+
// }
|
|
33
|
+
// }
|
|
34
|
+
// }
|
|
35
|
+
// type Input = { name: string, age: number, email: string, password: string };
|
|
36
|
+
// type Output = void;
|
|
37
|
+
// interface UserRepository {
|
|
38
|
+
// create: (input: any) => Promise<void>
|
|
39
|
+
// }
|
|
40
|
+
// class UserRepositoryDatabase implements UserRepository {
|
|
41
|
+
// async create(data: any): Promise<void> {
|
|
42
|
+
// console.log('creating user', data)
|
|
43
|
+
// }
|
|
44
|
+
// }
|
|
45
|
+
// Registry.register('UserRepository', Registry.classLoader(UserRepositoryDatabase))
|
|
46
|
+
// class CreateUser implements UseCase<Input, Output> {
|
|
47
|
+
// @Auth()
|
|
48
|
+
// private auth: any;
|
|
49
|
+
// constructor(
|
|
50
|
+
// @Inject('UserRepository')
|
|
51
|
+
// private readonly userRepository: UserRepository
|
|
52
|
+
// ) {}
|
|
53
|
+
// async execute(input: Input): Promise<Output> {
|
|
54
|
+
// this.userRepository.create(input)
|
|
55
|
+
// }
|
|
56
|
+
// }
|
|
57
|
+
// class GetUser implements UseCase<Input, Output> {
|
|
58
|
+
// @Auth()
|
|
59
|
+
// private auth!: boolean
|
|
60
|
+
// constructor(
|
|
61
|
+
// @Inject('UserRepository')
|
|
62
|
+
// private readonly userRepository: UserRepository
|
|
63
|
+
// ) {}
|
|
64
|
+
// async execute(input: Input): Promise<Output> {
|
|
65
|
+
// console.log(' testing', this.auth)
|
|
66
|
+
// await this.userRepository.create(input)
|
|
67
|
+
// }
|
|
68
|
+
// }
|
|
69
|
+
// type RequestData = {
|
|
70
|
+
// headers: any,
|
|
71
|
+
// data: any,
|
|
72
|
+
// middlewareData: any
|
|
73
|
+
// }
|
|
74
|
+
// @Controller('/users')
|
|
75
|
+
// class UserController {
|
|
76
|
+
// @Post({ statusCode: 201 })
|
|
77
|
+
// @Validator(ValidateUser)
|
|
78
|
+
// @Middleware(UserAuth)
|
|
79
|
+
// // @Middleware(Test)
|
|
80
|
+
// createUser(input: any, request: RequestData) {
|
|
81
|
+
// console.log('request', request)
|
|
82
|
+
// return UseCaseHandler.load(CreateUser, request).execute(input)
|
|
83
|
+
// }
|
|
84
|
+
// @Get({ query: ['time|string'], statusCode: 200 })
|
|
85
|
+
// @Middleware(UserAuth)
|
|
86
|
+
// // @Middleware(Test)
|
|
87
|
+
// getUser(input: any, request: RequestData) {
|
|
88
|
+
// console.log('middlewareData', request.middlewareData)
|
|
89
|
+
// return UseCaseHandler.load(GetUser, request).execute(input)
|
|
90
|
+
// }
|
|
91
|
+
// }
|
|
92
|
+
// class ExpressHttpServer extends HttpServer {
|
|
93
|
+
// constructor() {
|
|
94
|
+
// super(new Elysia(), 'elysia')
|
|
95
|
+
// }
|
|
96
|
+
// listen(port: number): void {
|
|
97
|
+
// super.listen(port)
|
|
98
|
+
// console.log(`Server listening on port ${port}`)
|
|
99
|
+
// }
|
|
100
|
+
// }
|
|
101
|
+
// const server = new ExpressHttpServer()
|
|
102
|
+
// new ControllerHandler(server, 3000)
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Registry } from "@/di/registry";
|
|
2
|
+
export default class ControllerHandler {
|
|
3
|
+
constructor(httpServer, port) {
|
|
4
|
+
for (const { instance, instanceMethod, middlewares, method, route, statusCode, params, query, validation } of this.registryControllers()) {
|
|
5
|
+
httpServer.register({
|
|
6
|
+
method,
|
|
7
|
+
route,
|
|
8
|
+
statusCode,
|
|
9
|
+
params,
|
|
10
|
+
query
|
|
11
|
+
}, async (input) => {
|
|
12
|
+
const filterParams = this.filter(input.params, params);
|
|
13
|
+
const queryParams = this.filter(input.query, query);
|
|
14
|
+
let mergedParams = { ...filterParams, ...queryParams, ...(input.body || {}) };
|
|
15
|
+
if (validation)
|
|
16
|
+
mergedParams = validation.validate({ ...filterParams, ...queryParams, ...(input.body || {}) });
|
|
17
|
+
let middlewareData = {};
|
|
18
|
+
if (middlewares) {
|
|
19
|
+
for (const middleware of middlewares) {
|
|
20
|
+
const middlewareResult = await middleware.execute({ headers: input.headers, ...mergedParams });
|
|
21
|
+
middlewareData = { ...middlewareResult, ...middlewareData };
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const request = { headers: input.headers, data: mergedParams, middlewareData };
|
|
25
|
+
return await instance[instanceMethod](mergedParams, request);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
httpServer.listen(port);
|
|
29
|
+
}
|
|
30
|
+
registryControllers() {
|
|
31
|
+
const registryControllers = Registry.resolve('controllers');
|
|
32
|
+
const controllers = [];
|
|
33
|
+
for (const controller of registryControllers) {
|
|
34
|
+
const basePath = Reflect.getMetadata('basePath', controller);
|
|
35
|
+
const injections = Reflect.getMetadata('injections', controller) || [];
|
|
36
|
+
const args = injections.map((token) => Registry.resolve(token));
|
|
37
|
+
const instance = new controller(...args);
|
|
38
|
+
const methodNames = Object.getOwnPropertyNames(controller.prototype).filter(method => method !== 'constructor');
|
|
39
|
+
for (const methodName of methodNames) {
|
|
40
|
+
const validation = Reflect.getMetadata('validation', controller.prototype, methodName);
|
|
41
|
+
const routeConfig = Reflect.getMetadata('route', controller.prototype, methodName);
|
|
42
|
+
const middlewares = Reflect.getMetadata('middlewares', controller.prototype, methodName);
|
|
43
|
+
controllers.push({
|
|
44
|
+
method: routeConfig.method,
|
|
45
|
+
route: basePath + routeConfig.path,
|
|
46
|
+
params: routeConfig.params,
|
|
47
|
+
query: routeConfig.query,
|
|
48
|
+
statusCode: routeConfig.statusCode,
|
|
49
|
+
instance,
|
|
50
|
+
instanceMethod: methodName,
|
|
51
|
+
middlewares,
|
|
52
|
+
validation
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return controllers;
|
|
57
|
+
}
|
|
58
|
+
filter(params, filterParams) {
|
|
59
|
+
const filter = {};
|
|
60
|
+
for (const paramName of filterParams || []) {
|
|
61
|
+
const [paramNameFiltered, type] = paramName.split('|');
|
|
62
|
+
let value = params[paramName] || params[paramNameFiltered];
|
|
63
|
+
if (!value)
|
|
64
|
+
return;
|
|
65
|
+
if (type === 'boolean')
|
|
66
|
+
value = value === 'true';
|
|
67
|
+
if (type === 'integer') {
|
|
68
|
+
value = value.replace(/[^0-9]/g, '');
|
|
69
|
+
value = value ? parseInt(value) : 0;
|
|
70
|
+
}
|
|
71
|
+
if (type === 'string')
|
|
72
|
+
value = value.toString();
|
|
73
|
+
filter[paramNameFiltered] = value;
|
|
74
|
+
}
|
|
75
|
+
return filter;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Registry } from "@/di/registry";
|
|
2
|
+
export class UseCaseHandler {
|
|
3
|
+
static load(useCaseClass, request) {
|
|
4
|
+
const instance = Registry.classLoader(useCaseClass);
|
|
5
|
+
const auth = Reflect.getMetadata("auth", useCaseClass);
|
|
6
|
+
const context = request;
|
|
7
|
+
if (auth && context?.middlewareData) {
|
|
8
|
+
instance[auth] = context.middlewareData[auth];
|
|
9
|
+
}
|
|
10
|
+
return instance;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// @ts-ignore
|
|
2
|
+
import Elysia from "elysia";
|
|
3
|
+
import HttpServer from "./HttpServer";
|
|
4
|
+
export class ElysiaHttpServer extends HttpServer {
|
|
5
|
+
constructor(uses) {
|
|
6
|
+
super(new Elysia(), 'elysia');
|
|
7
|
+
uses?.forEach(use => this.framework.use(use));
|
|
8
|
+
}
|
|
9
|
+
listen(port) {
|
|
10
|
+
super.listen(port);
|
|
11
|
+
console.log(`Server listening on port ${port}`);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// @ts-ignore
|
|
2
|
+
import express from "express";
|
|
3
|
+
import HttpServer from "./HttpServer";
|
|
4
|
+
const app = express();
|
|
5
|
+
export class ExpressHttpServer extends HttpServer {
|
|
6
|
+
constructor(uses) {
|
|
7
|
+
super(app, 'express');
|
|
8
|
+
uses?.forEach(use => this.framework.use(use));
|
|
9
|
+
}
|
|
10
|
+
listen(port) {
|
|
11
|
+
super.listen(port);
|
|
12
|
+
console.log(`Server listening on port ${port}`);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export type HttpStatusCode = 200 | 201 | 204 | 401 | 403 | 404 | 409 | 422 | 500;
|
|
2
|
+
export type AllowedMethods = 'get' | 'post' | 'put' | 'delete' | 'patch';
|
|
3
|
+
export type HttpServerParams = {
|
|
4
|
+
method: AllowedMethods;
|
|
5
|
+
route: string;
|
|
6
|
+
statusCode?: number;
|
|
7
|
+
params?: string[];
|
|
8
|
+
query?: string[];
|
|
9
|
+
};
|
|
10
|
+
type FrameworkWeb = {
|
|
11
|
+
listen(port: number): void;
|
|
12
|
+
use(middleware: CallableFunction): void;
|
|
13
|
+
get(route: string, handler: CallableFunction): void;
|
|
14
|
+
post(route: string, handler: CallableFunction): void;
|
|
15
|
+
put(route: string, handler: CallableFunction): void;
|
|
16
|
+
delete(route: string, handler: CallableFunction): void;
|
|
17
|
+
patch(route: string, handler: CallableFunction): void;
|
|
18
|
+
};
|
|
19
|
+
export default abstract class HttpServer {
|
|
20
|
+
protected framework: FrameworkWeb;
|
|
21
|
+
protected frameworkName: string;
|
|
22
|
+
protected defaultMessageError: string;
|
|
23
|
+
constructor(framework: FrameworkWeb, frameworkName: 'elysia' | 'express');
|
|
24
|
+
use(middleware: CallableFunction): HttpServer;
|
|
25
|
+
register(httpServerParams: HttpServerParams, handler: CallableFunction): void;
|
|
26
|
+
listen(port: number): void;
|
|
27
|
+
private mountRoute;
|
|
28
|
+
private elysia;
|
|
29
|
+
private express;
|
|
30
|
+
}
|
|
31
|
+
export {};
|