framework-do-dede 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,12 @@
1
+ export declare class ElysiaHttpServer extends HttpServer {
2
+
3
+ constructor(uses?: CallableFunction[]) {
4
+ super(new Elysia(), 'elysia')
5
+ uses?.forEach(use => this.framework.use(use))
6
+ }
7
+
8
+ listen(port: number): void {
9
+ super.listen(port)
10
+ console.log(`Server listening on port ${port}`)
11
+ }
12
+ }
@@ -0,0 +1,13 @@
1
+ declare const app: unknown;
2
+ export declare class ExpressHttpServer extends HttpServer {
3
+
4
+ constructor(uses?: CallableFunction[]) {
5
+ super(app, 'express')
6
+ uses?.forEach(use => this.framework.use(use))
7
+ }
8
+
9
+ listen(port: number): void {
10
+ super.listen(port)
11
+ console.log(`Server listening on port ${port}`)
12
+ }
13
+ }
@@ -36,9 +36,10 @@ export default abstract class HttpServer {
36
36
  return this;
37
37
  }
38
38
 
39
- register(httpServerParams: HttpServerParams, handler: CallableFunction) {
39
+ register(httpServerParams: HttpServerParams, handler: CallableFunction): void {
40
40
  const route = this.mountRoute(httpServerParams)
41
41
  if (this.frameworkName === 'elysia') return this.elysia(httpServerParams, route, handler)
42
+ return this.express(httpServerParams, route, handler)
42
43
  }
43
44
 
44
45
  listen(port: number): void {
@@ -84,4 +85,30 @@ export default abstract class HttpServer {
84
85
  }
85
86
  })
86
87
  }
88
+
89
+ private express (httpServerParams: HttpServerParams, route: string, handler: CallableFunction) {
90
+ const method = httpServerParams.method as AllowedMethods
91
+ this.framework[method](route, async (request: any, res: any) => {
92
+ try {
93
+ const output = await handler({
94
+ headers: request.headers,
95
+ query: request.query,
96
+ params: request.params,
97
+ body: request.body
98
+ })
99
+ return res.status(httpServerParams.statusCode ?? 200).json(output)
100
+ } catch (error: any) {
101
+ if (error instanceof ServerError) {
102
+ return res.status(error.getStatusCode()).json({
103
+ error: error.message,
104
+ statusCode: error.getStatusCode()
105
+ })
106
+ }
107
+ return res.status(500).json({
108
+ error: this.defaultMessageError,
109
+ statusCode: 500
110
+ })
111
+ }
112
+ })
113
+ }
87
114
  }
package/dist/dede.d.ts ADDED
@@ -0,0 +1,47 @@
1
+ export declare type Register = {
2
+ name: string,
3
+ classLoader: any,
4
+ autoLoad?: boolean
5
+ }
6
+
7
+ export type Options = {
8
+ framework: {
9
+ use: 'elysia' | 'express',
10
+ port?: number,
11
+ middlewares?: CallableFunction[]
12
+ },
13
+ registries: Register[]
14
+ }
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
+ }
@@ -0,0 +1,40 @@
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
+ })