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,91 @@
1
+ import { FrameworkError } from "./FrameworkError";
2
+ import { ServerError } from "./ServerError";
3
+ export default class HttpServer {
4
+ framework;
5
+ frameworkName;
6
+ defaultMessageError = 'Ops, An unexpected error occurred';
7
+ constructor(framework, frameworkName) {
8
+ if (frameworkName !== 'elysia' && frameworkName !== 'express')
9
+ throw new FrameworkError('Framework not supported');
10
+ this.framework = framework;
11
+ this.frameworkName = frameworkName;
12
+ }
13
+ use(middleware) {
14
+ this.framework.use(middleware);
15
+ return this;
16
+ }
17
+ register(httpServerParams, handler) {
18
+ const route = this.mountRoute(httpServerParams);
19
+ if (this.frameworkName === 'elysia')
20
+ return this.elysia(httpServerParams, route, handler);
21
+ return this.express(httpServerParams, route, handler);
22
+ }
23
+ listen(port) {
24
+ this.framework.listen(port);
25
+ }
26
+ mountRoute(httpServerParams) {
27
+ const params = httpServerParams.params?.map((param) => param.split('|')[0]);
28
+ if (params && params.length > 0) {
29
+ const paramsMounted = params.map((v) => {
30
+ return v.includes('_') ? `${v.replace('_', '/')}` : `/:${v}`;
31
+ }).join('');
32
+ return `${httpServerParams.route}${paramsMounted}`;
33
+ }
34
+ return httpServerParams.route;
35
+ }
36
+ elysia(httpServerParams, route, handler) {
37
+ const method = httpServerParams.method;
38
+ (this.framework[method])(route, async ({ headers, set, query, params, body, request, path }) => {
39
+ try {
40
+ set.status = httpServerParams.statusCode ?? 200;
41
+ const output = await handler({
42
+ headers,
43
+ query,
44
+ params,
45
+ body
46
+ });
47
+ return output;
48
+ }
49
+ catch (error) {
50
+ if (error instanceof ServerError) {
51
+ set.status = error.getStatusCode();
52
+ return {
53
+ error: error.message,
54
+ statusCode: error.getStatusCode()
55
+ };
56
+ }
57
+ set.status = 500;
58
+ return {
59
+ error: this.defaultMessageError,
60
+ statusCode: 500
61
+ };
62
+ }
63
+ });
64
+ }
65
+ express(httpServerParams, route, handler) {
66
+ const method = httpServerParams.method;
67
+ this.framework[method](route, async (request, res) => {
68
+ try {
69
+ const output = await handler({
70
+ headers: request.headers,
71
+ query: request.query,
72
+ params: request.params,
73
+ body: request.body
74
+ });
75
+ return res.status(httpServerParams.statusCode ?? 200).json(output);
76
+ }
77
+ catch (error) {
78
+ if (error instanceof ServerError) {
79
+ return res.status(error.getStatusCode()).json({
80
+ error: error.message,
81
+ statusCode: error.getStatusCode()
82
+ });
83
+ }
84
+ return res.status(500).json({
85
+ error: this.defaultMessageError,
86
+ statusCode: 500
87
+ });
88
+ }
89
+ });
90
+ }
91
+ }
@@ -0,0 +1,23 @@
1
+ export declare abstract class ServerError extends Error {
2
+ private statusCode;
3
+ constructor(message: string, statusCode: number);
4
+ getStatusCode(): number;
5
+ }
6
+ export declare class NotFound extends ServerError {
7
+ constructor(message: string);
8
+ }
9
+ export declare class Forbidden extends ServerError {
10
+ constructor(message: string);
11
+ }
12
+ export declare class UnprocessableEntity extends ServerError {
13
+ constructor(message: string);
14
+ }
15
+ export declare class Conflict extends ServerError {
16
+ constructor(message: string);
17
+ }
18
+ export declare class Unauthorized extends ServerError {
19
+ constructor(message: string);
20
+ }
21
+ export declare class BadRequest extends ServerError {
22
+ constructor(message: string);
23
+ }
@@ -0,0 +1,41 @@
1
+ export class ServerError extends Error {
2
+ statusCode;
3
+ constructor(message, statusCode) {
4
+ super(message);
5
+ this.name = this.constructor.name;
6
+ this.statusCode = statusCode;
7
+ }
8
+ getStatusCode() {
9
+ return this.statusCode;
10
+ }
11
+ }
12
+ export class NotFound extends ServerError {
13
+ constructor(message) {
14
+ super(message, 404);
15
+ }
16
+ }
17
+ export class Forbidden extends ServerError {
18
+ constructor(message) {
19
+ super(message, 403);
20
+ }
21
+ }
22
+ export class UnprocessableEntity extends ServerError {
23
+ constructor(message) {
24
+ super(message, 422);
25
+ }
26
+ }
27
+ export class Conflict extends ServerError {
28
+ constructor(message) {
29
+ super(message, 409);
30
+ }
31
+ }
32
+ export class Unauthorized extends ServerError {
33
+ constructor(message) {
34
+ super(message, 401);
35
+ }
36
+ }
37
+ export class BadRequest extends ServerError {
38
+ constructor(message) {
39
+ super(message, 400);
40
+ }
41
+ }
@@ -0,0 +1,3 @@
1
+ import HttpServer from "./HttpServer";
2
+ import { ServerError, NotFound, Forbidden, Conflict, Unauthorized, UnprocessableEntity, BadRequest } from "./ServerError";
3
+ export { ServerError, BadRequest, NotFound, Forbidden, Conflict, Unauthorized, UnprocessableEntity, HttpServer };
@@ -0,0 +1,3 @@
1
+ import HttpServer from "./HttpServer";
2
+ import { ServerError, NotFound, Forbidden, Conflict, Unauthorized, UnprocessableEntity, BadRequest } from "./ServerError";
3
+ export { ServerError, BadRequest, NotFound, Forbidden, Conflict, Unauthorized, UnprocessableEntity, HttpServer };
package/dist/index.d.ts CHANGED
@@ -1,37 +1,14 @@
1
- import { Auth, Controller, Delete, Get, Inject, Middleware, Patch, Post, Put, Validator } from './decorators';
2
- import { Dede, Options as DedeOptions, Register as DedeRegister } from './dede';
3
- import { ServerError } from './http';
4
- import { UseCaseHandler } from './handlers';
5
-
6
- declare interface HttpMiddleware {
7
- execute(input: any): Promise<any>
1
+ import { Dede, Register as DedeRegister, Options as DedeOptions } from './dede';
2
+ import { Controller, Post, Put, Get, Delete, Patch, Validator, Middleware, Auth, Inject } from './decorators';
3
+ import { BadRequest, Conflict, Forbidden, HttpServer, NotFound, ServerError, Unauthorized, UnprocessableEntity } from './http';
4
+ import { Validation, HttpMiddleware, UseCase } from './protocols';
5
+ declare class RequestData {
6
+ headers: any;
7
+ data: any;
8
+ middlewareData: any;
9
+ constructor(headers: any, data: any, middlewareData: any);
8
10
  }
9
- declare interface UseCase<Input, Output> {
10
- execute(input: Input): Promise<Output>
11
+ declare class UseCaseHandler {
12
+ static load<T extends UseCase<any, any>>(useCaseClass: new (...args: any[]) => T, request?: RequestData): T;
11
13
  }
12
- declare interface Validation {
13
- validate(input: any): any;
14
- }
15
- declare const instance: unknown;
16
-
17
- export {
18
- UseCase,
19
- HttpMiddleware,
20
- Validation,
21
- RequestData,
22
- Dede,
23
- DedeRegister,
24
- DedeOptions,
25
- UseCaseHandler,
26
- ServerError,
27
- Controller,
28
- Post,
29
- Put,
30
- Get,
31
- Delete,
32
- Patch,
33
- Validator,
34
- Middleware,
35
- Auth,
36
- Inject
37
- }
14
+ export { UseCase, HttpMiddleware, Validation, RequestData, Dede, DedeRegister, DedeOptions, UseCaseHandler, ServerError, BadRequest, Conflict, Forbidden, HttpServer, NotFound, Unauthorized, UnprocessableEntity, Controller, Post, Put, Get, Delete, Patch, Validator, Middleware, Auth, Inject };