framework-do-dede 0.0.13 → 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.
Files changed (61) hide show
  1. package/dist/decorators/controller.d.ts +35 -0
  2. package/dist/decorators/controller.js +88 -0
  3. package/dist/decorators/di.d.ts +1 -0
  4. package/dist/decorators/di.js +7 -0
  5. package/dist/decorators/index.d.ts +4 -0
  6. package/dist/decorators/index.js +4 -0
  7. package/dist/decorators/usecase.d.ts +1 -0
  8. package/dist/decorators/usecase.js +5 -0
  9. package/dist/dede.d.ts +15 -44
  10. package/dist/dede.js +29 -0
  11. package/dist/di/registry.d.ts +15 -0
  12. package/dist/di/registry.js +47 -0
  13. package/dist/example.d.ts +0 -40
  14. package/dist/example.js +102 -0
  15. package/dist/handlers/controller.handler.d.ts +6 -0
  16. package/dist/handlers/controller.handler.js +77 -0
  17. package/dist/handlers/index.d.ts +2 -0
  18. package/dist/handlers/index.js +2 -0
  19. package/dist/handlers/usecase.handler.d.ts +5 -0
  20. package/dist/handlers/usecase.handler.js +12 -0
  21. package/dist/http/ElysiaHttpServer.d.ts +5 -0
  22. package/dist/http/ElysiaHttpServer.js +13 -0
  23. package/dist/http/ExpressHttpServer.d.ts +5 -0
  24. package/dist/http/ExpressHttpServer.js +14 -0
  25. package/dist/http/FrameworkError.d.ts +3 -0
  26. package/dist/http/FrameworkError.js +5 -0
  27. package/dist/http/HttpServer.d.ts +31 -0
  28. package/dist/http/HttpServer.js +91 -0
  29. package/dist/http/ServerError.d.ts +23 -0
  30. package/dist/http/ServerError.js +41 -0
  31. package/dist/http/index.d.ts +3 -0
  32. package/dist/http/index.js +3 -0
  33. package/dist/index.d.ts +12 -35
  34. package/dist/index.js +26 -15466
  35. package/dist/protocols/Controller.d.ts +14 -0
  36. package/dist/protocols/Controller.js +1 -0
  37. package/dist/protocols/HttpMiddleware.d.ts +3 -0
  38. package/dist/protocols/HttpMiddleware.js +1 -0
  39. package/dist/protocols/UseCase.d.ts +3 -0
  40. package/dist/protocols/UseCase.js +1 -0
  41. package/dist/protocols/Validation.d.ts +3 -0
  42. package/dist/protocols/Validation.js +1 -0
  43. package/dist/protocols/index.d.ts +4 -0
  44. package/dist/protocols/index.js +1 -0
  45. package/package.json +15 -6
  46. package/dist/Controller.d.ts +0 -13
  47. package/dist/ElysiaHttpServer.d.ts +0 -12
  48. package/dist/ExpressHttpServer.d.ts +0 -13
  49. package/dist/FrameworkError.d.ts +0 -5
  50. package/dist/HttpMiddleware.d.ts +0 -3
  51. package/dist/HttpServer.d.ts +0 -114
  52. package/dist/ServerError.d.ts +0 -42
  53. package/dist/UseCase.d.ts +0 -3
  54. package/dist/Validation.d.ts +0 -3
  55. package/dist/controller.d.ts +0 -11
  56. package/dist/controller.handler.d.ts +0 -86
  57. package/dist/di.d.ts +0 -1
  58. package/dist/index.cjs +0 -15485
  59. package/dist/registry.d.ts +0 -4
  60. package/dist/usecase.d.ts +0 -1
  61. package/dist/usecase.handler.d.ts +0 -23
@@ -0,0 +1,14 @@
1
+ import type { AllowedMethods, HttpStatusCode } from "@/http/HttpServer";
2
+ import type { Validation } from "./Validation";
3
+ import type { HttpMiddleware } from "./HttpMiddleware";
4
+ export type Controller = {
5
+ instance: any;
6
+ instanceMethod: string;
7
+ route: string;
8
+ method: AllowedMethods;
9
+ middlewares?: HttpMiddleware[];
10
+ statusCode?: HttpStatusCode;
11
+ validation?: Validation;
12
+ params?: any;
13
+ query?: any;
14
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ export interface HttpMiddleware {
2
+ execute(input: any): Promise<any>;
3
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ export interface UseCase<Input, Output> {
2
+ execute(input: Input): Promise<Output>;
3
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ export interface Validation {
2
+ validate(input: any): any;
3
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ import type { HttpMiddleware } from './HttpMiddleware';
2
+ import type { UseCase } from './UseCase';
3
+ import type { Validation } from './Validation';
4
+ export type { HttpMiddleware, UseCase, Validation };
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "framework-do-dede",
3
- "version": "0.0.13",
3
+ "version": "0.0.14",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -12,8 +12,10 @@
12
12
  "require": "./dist/index.cjs"
13
13
  },
14
14
  "scripts": {
15
- "build": "bun run build.ts",
16
- "prepare": "bun run build"
15
+ "test": "jest",
16
+ "test:watch": "jest --watch",
17
+ "build": "tsc -p tsconfig.build.json",
18
+ "prepare": "npm run build"
17
19
  },
18
20
  "files": [
19
21
  "dist"
@@ -30,14 +32,21 @@
30
32
  "bugs": "https://github.com/marcossaore/framework-do-dede/issues",
31
33
  "author": "Marcos Soares",
32
34
  "devDependencies": {
35
+ "@babel/core": "^7.26.10",
36
+ "@babel/preset-env": "^7.26.9",
37
+ "@babel/preset-typescript": "^7.27.0",
38
+ "@types/jest": "^29.5.14",
33
39
  "@types/reflect-metadata": "^0.1.0",
34
- "bun-plugin-dtsx": "^0.21.9"
40
+ "jest": "^29.7.0",
41
+ "ts-jest": "^29.3.0",
42
+ "typescript": "^5.8.2"
35
43
  },
36
44
  "peerDependencies": {
45
+ "elysia": "^1.2.25",
46
+ "express": "^4.21.2",
37
47
  "typescript": "^5.8.2"
38
48
  },
39
49
  "dependencies": {
40
- "@types/bun": "^1.2.5",
41
50
  "reflect-metadata": "^0.2.2"
42
51
  }
43
- }
52
+ }
@@ -1,13 +0,0 @@
1
- import type { AllowedMethods, HttpStatusCode } from '@/http/HttpServer';
2
-
3
- export declare type Controller = {
4
- instance: any
5
- instanceMethod: string
6
- route: string
7
- method: AllowedMethods
8
- middlewares?: HttpMiddleware[]
9
- statusCode?: HttpStatusCode
10
- validation?: Validation
11
- params?: any
12
- query?: any
13
- }
@@ -1,12 +0,0 @@
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
- }
@@ -1,13 +0,0 @@
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
- }
@@ -1,5 +0,0 @@
1
- export declare class FrameworkError extends Error {
2
- constructor(message: string) {
3
- super(message)
4
- }
5
- }
@@ -1,3 +0,0 @@
1
- export declare interface HttpMiddleware {
2
- execute(input: any): Promise<any>
3
- }
@@ -1,114 +0,0 @@
1
- export declare type HttpStatusCode = 200 | 201 | 204 | 401 | 403 | 404 | 409 | 422 | 500
2
-
3
- export type AllowedMethods = 'get' | 'post' | 'put' | 'delete' | 'patch'
4
-
5
- export type HttpServerParams = {
6
- method: AllowedMethods,
7
- route: string,
8
- statusCode?: number,
9
- params?: string[],
10
- query?: string[]
11
- }
12
-
13
- type FrameworkWeb = {
14
- listen(port: number): void;
15
- use(middleware: CallableFunction): void
16
- get(route: string, handler: CallableFunction): void
17
- post(route: string, handler: CallableFunction): void
18
- put(route: string, handler: CallableFunction): void
19
- delete(route: string, handler: CallableFunction): void
20
- patch(route: string, handler: CallableFunction): void
21
- }
22
-
23
- export default abstract class HttpServer {
24
- protected framework: FrameworkWeb
25
- protected frameworkName: string;
26
- protected defaultMessageError = 'Ops, An unexpected error occurred';
27
-
28
- constructor(framework: FrameworkWeb, frameworkName: 'elysia' | 'express') {
29
- if (frameworkName !== 'elysia' && frameworkName !== 'express') throw new FrameworkError('Framework not supported')
30
- this.framework = framework
31
- this.frameworkName = frameworkName
32
- }
33
-
34
- use(middleware: CallableFunction): HttpServer {
35
- this.framework.use(middleware)
36
- return this;
37
- }
38
-
39
- register(httpServerParams: HttpServerParams, handler: CallableFunction): void {
40
- const route = this.mountRoute(httpServerParams)
41
- if (this.frameworkName === 'elysia') return this.elysia(httpServerParams, route, handler)
42
- return this.express(httpServerParams, route, handler)
43
- }
44
-
45
- listen(port: number): void {
46
- this.framework.listen(port)
47
- }
48
-
49
- private mountRoute(httpServerParams: HttpServerParams) {
50
- const params = httpServerParams.params?.map((param) => param.split('|')[0])
51
- if (params && params.length > 0) {
52
- const paramsMounted = params.map((v) => {
53
- return v.includes('_') ? `${v.replace('_', '/')}` : `/:${v}`
54
- }).join('')
55
- return `${httpServerParams.route}${paramsMounted}`
56
- }
57
- return httpServerParams.route
58
- }
59
-
60
- private elysia (httpServerParams: HttpServerParams, route: string, handler: CallableFunction) {
61
- const method = httpServerParams.method as AllowedMethods
62
- (this.framework[method])(route, async ({ headers, set, query, params, body, request, path }: any) => {
63
- try {
64
- set.status = httpServerParams.statusCode ?? 200
65
- const output = await handler({
66
- headers,
67
- query,
68
- params,
69
- body
70
- })
71
- return output
72
- } catch (error: any) {
73
- if (error instanceof ServerError) {
74
- set.status = error.getStatusCode()
75
- return {
76
- error: error.message,
77
- statusCode: error.getStatusCode()
78
- }
79
- }
80
- set.status = 500
81
- return {
82
- error: this.defaultMessageError,
83
- statusCode: 500
84
- }
85
- }
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
- }
114
- }
@@ -1,42 +0,0 @@
1
- export declare abstract class ServerError extends Error {
2
- private statusCode: number
3
- constructor(message: string, statusCode: number) {
4
- super(message)
5
- this.name = this.constructor.name
6
- this.statusCode = statusCode
7
- }
8
-
9
- getStatusCode() {
10
- return this.statusCode
11
- }
12
- }
13
- export declare class NotFound extends ServerError {
14
- constructor(message: string) {
15
- super(message, 404)
16
- }
17
- }
18
- export declare class Forbidden extends ServerError {
19
- constructor(message: string) {
20
- super(message, 403)
21
- }
22
- }
23
- export declare class UnprocessableEntity extends ServerError {
24
- constructor(message: string) {
25
- super(message, 422)
26
- }
27
- }
28
- export declare class Conflict extends ServerError {
29
- constructor(message: string) {
30
- super(message, 409)
31
- }
32
- }
33
- export declare class Unauthorized extends ServerError {
34
- constructor(message: string) {
35
- super(message, 401)
36
- }
37
- }
38
- export declare class BadRequest extends ServerError {
39
- constructor(message: string) {
40
- super(message, 400)
41
- }
42
- }
package/dist/UseCase.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export declare interface UseCase<Input, Output> {
2
- execute(input: Input): Promise<Output>
3
- }
@@ -1,3 +0,0 @@
1
- export declare interface Validation {
2
- validate(input: any): any;
3
- }
@@ -1,11 +0,0 @@
1
- import type { HttpMiddleware } from '@/protocols';
2
-
3
- declare const middlewares: Map<string, HttpMiddleware>;
4
- export declare function Controller(basePath: string): void;
5
- export declare function Middleware(middlewareClass: new ()): void;
6
- export declare function Post(config: { path?: string,, statusCode?: number,, params?: string[],, query?: string[] }): void;
7
- export declare function Get(config: { path?: string,, statusCode?: number,, params?: string[],, query?: string[] }): void;
8
- export declare function Put(config: { path?: string,, statusCode?: number,, params?: string[],, query?: string[] }): void;
9
- export declare function Patch(config: { path?: string,, statusCode?: number,, params?: string[],, query?: string[] }): void;
10
- export declare function Delete(config: { path?: string,, statusCode?: number,, params?: string[],, query?: string[] }): void;
11
- export declare function Validator(validationClass: new ()): void;
@@ -1,86 +0,0 @@
1
- import type { Controller } from '@/protocols/Controller';
2
-
3
- declare type Input = {
4
- headers: any
5
- body: any
6
- params: any
7
- query: any
8
- }
9
-
10
- export default class ControllerHandler {
11
- constructor(httpServer: HttpServer, port: number) {
12
- for (const { instance, instanceMethod, middlewares, method, route, statusCode, params, query, validation } of this.registryControllers()) {
13
- httpServer.register(
14
- {
15
- method,
16
- route,
17
- statusCode,
18
- params,
19
- query
20
- },
21
- async (input: Input) => {
22
- const filterParams = this.filter(input.params, params)
23
- const queryParams = this.filter(input.query, query)
24
- let mergedParams = { ...filterParams, ...queryParams, ...(input.body || {}) }
25
- if (validation) mergedParams = validation.validate({ ...filterParams, ...queryParams, ...(input.body || {}) });
26
- let middlewareData = {}
27
- if (middlewares) {
28
- for (const middleware of middlewares) {
29
- const middlewareResult = await middleware.execute({ headers: input.headers, ...mergedParams })
30
- middlewareData = { ...middlewareResult, ...middlewareData }
31
- }
32
- }
33
- const request = { headers: input.headers, data: mergedParams, middlewareData }
34
- return await instance[instanceMethod](mergedParams, request)
35
- }
36
- )
37
- }
38
- httpServer.listen(port)
39
- }
40
-
41
- private registryControllers() {
42
- const registryControllers = Registry.resolve<any[]>('controllers');
43
- const controllers: Controller[] = []
44
- for (const controller of registryControllers) {
45
- const basePath = Reflect.getMetadata('basePath', controller);
46
- const injections = Reflect.getMetadata('injections', controller) || [];
47
- const args = injections.map((token: string) => Registry.resolve(token))
48
- const instance = new controller(...args)
49
- const methodNames = Object.getOwnPropertyNames(controller.prototype).filter(method => method !== 'constructor')
50
- for (const methodName of methodNames) {
51
- const validation = Reflect.getMetadata('validation', controller.prototype, methodName);
52
- const routeConfig = Reflect.getMetadata('route', controller.prototype, methodName);
53
- const middlewares = Reflect.getMetadata('middlewares', controller.prototype, methodName);
54
- controllers.push({
55
- method: routeConfig.method,
56
- route: basePath + routeConfig.path,
57
- params: routeConfig.params,
58
- query: routeConfig.query,
59
- statusCode: routeConfig.statusCode,
60
- instance,
61
- instanceMethod: methodName,
62
- middlewares,
63
- validation
64
- });
65
- }
66
- }
67
- return controllers
68
- }
69
-
70
- private filter(params: any, filterParams?: string[]): any {
71
- const filter: any = {}
72
- for (const paramName of filterParams || []) {
73
- const [paramNameFiltered, type] = paramName.split('|')
74
- let value = params[paramName] || params[paramNameFiltered]
75
- if (!value) return
76
- if (type === 'boolean') value = value === 'true'
77
- if (type === 'integer') {
78
- value = value.replace(/[^0-9]/g, '')
79
- value = value ? parseInt(value) : 0
80
- }
81
- if (type === 'string') value = value.toString()
82
- filter[paramNameFiltered] = value
83
- }
84
- return filter
85
- }
86
- }
package/dist/di.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare function Inject(token: string): void;