From 64ae80863bc2c2f861f5286d753f1d9652f7e8a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Wed, 30 Mar 2022 17:10:18 -0300 Subject: [PATCH 1/3] feat: add console.clear on factory constructor --- package.json | 2 +- src/Factories/AthennaFactory.ts | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index b067f9b..1e91875 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/core", - "version": "1.0.1", + "version": "1.0.2", "description": "", "license": "MIT", "author": "João Lenon ", diff --git a/src/Factories/AthennaFactory.ts b/src/Factories/AthennaFactory.ts index 25c0d62..211ab07 100644 --- a/src/Factories/AthennaFactory.ts +++ b/src/Factories/AthennaFactory.ts @@ -68,6 +68,8 @@ export class AthennaFactory { } constructor(fileName: string) { + console.clear() + AthennaFactory.resolveNodeTs(fileName) const secConfig = new SecConfig() From 56b8c8f18faeec4a8187911283afba2423de197f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Wed, 30 Mar 2022 19:32:39 -0300 Subject: [PATCH 2/3] feat: add container folder and controller/middleware providers --- {src/Modules => container}/HttpRoute.ts | 0 {src/Modules => container}/HttpServer.ts | 0 index.ts | 6 +- package-lock.json | 253 ++--------------------- package.json | 13 +- src/Factories/AthennaFactory.ts | 22 +- src/Providers/ControllerProvider.ts | 30 +++ src/Providers/MiddlewareProvider.ts | 30 +++ src/Utils/AthennaErrorHandler.ts | 46 +++++ src/Utils/ResolveClassExport.ts | 26 +++ tests/Stubs/config/logging.ts | 4 + tests/Stubs/start/routes.ts | 2 +- tests/Unit/IgniteTest.ts | 2 +- tsconfig.json | 3 +- 14 files changed, 180 insertions(+), 257 deletions(-) rename {src/Modules => container}/HttpRoute.ts (100%) rename {src/Modules => container}/HttpServer.ts (100%) create mode 100644 src/Providers/ControllerProvider.ts create mode 100644 src/Providers/MiddlewareProvider.ts create mode 100644 src/Utils/AthennaErrorHandler.ts create mode 100644 src/Utils/ResolveClassExport.ts diff --git a/src/Modules/HttpRoute.ts b/container/HttpRoute.ts similarity index 100% rename from src/Modules/HttpRoute.ts rename to container/HttpRoute.ts diff --git a/src/Modules/HttpServer.ts b/container/HttpServer.ts similarity index 100% rename from src/Modules/HttpServer.ts rename to container/HttpServer.ts diff --git a/index.ts b/index.ts index 0c2fa13..c612b2e 100644 --- a/index.ts +++ b/index.ts @@ -7,5 +7,7 @@ * file that was distributed with this source code. */ -export * from 'src/Modules/HttpRoute' -export * from 'src/Modules/HttpServer' +export * from 'src/Ignite' + +export * from 'src/Utils/ResolveClassExport' +export * from 'src/Utils/AthennaErrorHandler' diff --git a/package-lock.json b/package-lock.json index b3f7eaf..ef7db1e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,18 +1,18 @@ { "name": "@athenna/core", - "version": "1.0.1", + "version": "1.0.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@athenna/core", - "version": "1.0.1", + "version": "1.0.2", "license": "MIT", "dependencies": { "@athenna/config": "1.0.5", - "@athenna/http": "1.0.4", - "@athenna/ioc": "1.0.3", - "@athenna/logger": "1.0.2", + "@athenna/http": "1.0.5", + "@athenna/ioc": "1.0.6", + "@athenna/logger": "1.0.4", "@secjs/utils": "1.8.0", "reflect-metadata": "0.1.13", "tscpaths": "0.0.9" @@ -71,9 +71,9 @@ } }, "node_modules/@athenna/http": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/@athenna/http/-/http-1.0.4.tgz", - "integrity": "sha512-6jAQgEZuI+PwXKXwTY5edSP/I2Zg4RZUvGyXP0tqRFlkJxChfEPPNXnNTy8NvvwWCgNdVpS7Um5GM1RsYlYgXA==", + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/@athenna/http/-/http-1.0.5.tgz", + "integrity": "sha512-tnjgksGrbzWPWeZI2UK/UZuMVGTdV1lxT/TSfnFDLKPSMySem6wMjxGhq7N8ZbjkPfFX1r8sVCcoXO4cHaEmCg==", "dependencies": { "@athenna/config": "1.0.5", "@athenna/ioc": "1.0.6", @@ -83,7 +83,7 @@ "tscpaths": "0.0.9" } }, - "node_modules/@athenna/http/node_modules/@athenna/ioc": { + "node_modules/@athenna/ioc": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/@athenna/ioc/-/ioc-1.0.6.tgz", "integrity": "sha512-vNlk7+penNKd61eFIHk7OSjor6HM1CO5c9ggUPcO37qvim9w7ZAyMmuMDJ1pSfgtwKsysOgxkMFyWtbpvkGWpA==", @@ -94,123 +94,6 @@ "tscpaths": "0.0.9" } }, - "node_modules/@athenna/http/node_modules/@athenna/ioc/node_modules/@secjs/utils": { - "version": "1.7.8", - "resolved": "https://registry.npmjs.org/@secjs/utils/-/utils-1.7.8.tgz", - "integrity": "sha512-DrcuJhcF2kdUJWoilniCFxbNWVbJJiTI/1NP779kvOSBD8g402tBzqgIjZdrL+tItjcEXMEUFURvmmZeI1l9VQ==", - "dependencies": { - "bytes": "3.1.2", - "chalk": "4.1.2", - "change-case": "4.1.2", - "http-status-codes": "2.2.0", - "kind-of": "6.0.3", - "lodash": "4.17.21", - "mime-types": "2.1.34", - "ms": "2.1.3", - "pluralize": "8.0.0", - "uuid": "8.3.2", - "validator-brazil": "1.2.2" - } - }, - "node_modules/@athenna/http/node_modules/ansi-styles": { - "version": "4.3.0", - "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", - "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", - "dependencies": { - "color-convert": "^2.0.1" - }, - "engines": { - "node": ">=8" - }, - "funding": { - "url": "https://github.com/chalk/ansi-styles?sponsor=1" - } - }, - "node_modules/@athenna/http/node_modules/chalk": { - "version": "4.1.2", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", - "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", - "dependencies": { - "ansi-styles": "^4.1.0", - "supports-color": "^7.1.0" - }, - "engines": { - "node": ">=10" - }, - "funding": { - "url": "https://github.com/chalk/chalk?sponsor=1" - } - }, - "node_modules/@athenna/http/node_modules/color-convert": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", - "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", - "dependencies": { - "color-name": "~1.1.4" - }, - "engines": { - "node": ">=7.0.0" - } - }, - "node_modules/@athenna/http/node_modules/color-name": { - "version": "1.1.4", - "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", - "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" - }, - "node_modules/@athenna/http/node_modules/has-flag": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", - "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", - "engines": { - "node": ">=8" - } - }, - "node_modules/@athenna/http/node_modules/mime-db": { - "version": "1.51.0", - "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.51.0.tgz", - "integrity": "sha512-5y8A56jg7XVQx2mbv1lu49NR4dokRnhZYTtL+KGfaa27uq4pSTXkwQkFJl4pkRMyNFz/EtYDSkiiEHx3F7UN6g==", - "engines": { - "node": ">= 0.6" - } - }, - "node_modules/@athenna/http/node_modules/mime-types": { - "version": "2.1.34", - "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.34.tgz", - "integrity": "sha512-6cP692WwGIs9XXdOO4++N+7qjqv0rqxxVvJ3VHPh/Sc9mVZcQP+ZGhkKiTvWMQRr2tbHkJP/Yn7Y0npb3ZBs4A==", - "dependencies": { - "mime-db": "1.51.0" - }, - "engines": { - "node": ">= 0.6" - } - }, - "node_modules/@athenna/http/node_modules/ms": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", - "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" - }, - "node_modules/@athenna/http/node_modules/supports-color": { - "version": "7.2.0", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", - "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", - "dependencies": { - "has-flag": "^4.0.0" - }, - "engines": { - "node": ">=8" - } - }, - "node_modules/@athenna/ioc": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/@athenna/ioc/-/ioc-1.0.3.tgz", - "integrity": "sha512-gU3r74DQu5pjOfeJsWKAja1FUw5xLjFXIwRMDPxJ2w0oNtkz5jdsTX/waqVe/uEIwMAlkQ8MbJiwEE94eMMZHA==", - "dependencies": { - "@secjs/utils": "1.7.8", - "awilix": "7.0.0", - "reflect-metadata": "0.1.13", - "tscpaths": "0.0.9" - } - }, "node_modules/@athenna/ioc/node_modules/@secjs/utils": { "version": "1.7.8", "resolved": "https://registry.npmjs.org/@secjs/utils/-/utils-1.7.8.tgz", @@ -318,9 +201,9 @@ } }, "node_modules/@athenna/logger": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/@athenna/logger/-/logger-1.0.2.tgz", - "integrity": "sha512-mS9NmzmMta1349fGZzfvT1DSR2geUV1qdMOLdVNnJ6h6VXtd/2tDBkK7OePnUZReM89FtDAzXzEn2Bkm5abORA==", + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@athenna/logger/-/logger-1.0.4.tgz", + "integrity": "sha512-m+8qSYzoCUFP0GwbIywaOheXJNbinAn5DIsngI3GT3T0dvY3AYjQ6Vx8qEDSeiNL31fFODpcVX61L9wp+ub93g==", "dependencies": { "@secjs/utils": "1.8.0", "reflect-metadata": "0.1.13", @@ -13621,9 +13504,9 @@ } }, "@athenna/http": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/@athenna/http/-/http-1.0.4.tgz", - "integrity": "sha512-6jAQgEZuI+PwXKXwTY5edSP/I2Zg4RZUvGyXP0tqRFlkJxChfEPPNXnNTy8NvvwWCgNdVpS7Um5GM1RsYlYgXA==", + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/@athenna/http/-/http-1.0.5.tgz", + "integrity": "sha512-tnjgksGrbzWPWeZI2UK/UZuMVGTdV1lxT/TSfnFDLKPSMySem6wMjxGhq7N8ZbjkPfFX1r8sVCcoXO4cHaEmCg==", "requires": { "@athenna/config": "1.0.5", "@athenna/ioc": "1.0.6", @@ -13631,106 +13514,12 @@ "fastify": "3.27.4", "reflect-metadata": "0.1.13", "tscpaths": "0.0.9" - }, - "dependencies": { - "@athenna/ioc": { - "version": "1.0.6", - "resolved": "https://registry.npmjs.org/@athenna/ioc/-/ioc-1.0.6.tgz", - "integrity": "sha512-vNlk7+penNKd61eFIHk7OSjor6HM1CO5c9ggUPcO37qvim9w7ZAyMmuMDJ1pSfgtwKsysOgxkMFyWtbpvkGWpA==", - "requires": { - "@secjs/utils": "1.7.8", - "awilix": "7.0.0", - "reflect-metadata": "0.1.13", - "tscpaths": "0.0.9" - }, - "dependencies": { - "@secjs/utils": { - "version": "1.7.8", - "resolved": "https://registry.npmjs.org/@secjs/utils/-/utils-1.7.8.tgz", - "integrity": "sha512-DrcuJhcF2kdUJWoilniCFxbNWVbJJiTI/1NP779kvOSBD8g402tBzqgIjZdrL+tItjcEXMEUFURvmmZeI1l9VQ==", - "requires": { - "bytes": "3.1.2", - "chalk": "4.1.2", - "change-case": "4.1.2", - "http-status-codes": "2.2.0", - "kind-of": "6.0.3", - "lodash": "4.17.21", - "mime-types": "2.1.34", - "ms": "2.1.3", - "pluralize": "8.0.0", - "uuid": "8.3.2", - "validator-brazil": "1.2.2" - } - } - } - }, - "ansi-styles": { - "version": "4.3.0", - "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", - "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", - "requires": { - "color-convert": "^2.0.1" - } - }, - "chalk": { - "version": "4.1.2", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", - "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", - "requires": { - "ansi-styles": "^4.1.0", - "supports-color": "^7.1.0" - } - }, - "color-convert": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", - "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", - "requires": { - "color-name": "~1.1.4" - } - }, - "color-name": { - "version": "1.1.4", - "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", - "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" - }, - "has-flag": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", - "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==" - }, - "mime-db": { - "version": "1.51.0", - "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.51.0.tgz", - "integrity": "sha512-5y8A56jg7XVQx2mbv1lu49NR4dokRnhZYTtL+KGfaa27uq4pSTXkwQkFJl4pkRMyNFz/EtYDSkiiEHx3F7UN6g==" - }, - "mime-types": { - "version": "2.1.34", - "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.34.tgz", - "integrity": "sha512-6cP692WwGIs9XXdOO4++N+7qjqv0rqxxVvJ3VHPh/Sc9mVZcQP+ZGhkKiTvWMQRr2tbHkJP/Yn7Y0npb3ZBs4A==", - "requires": { - "mime-db": "1.51.0" - } - }, - "ms": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", - "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" - }, - "supports-color": { - "version": "7.2.0", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", - "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", - "requires": { - "has-flag": "^4.0.0" - } - } } }, "@athenna/ioc": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/@athenna/ioc/-/ioc-1.0.3.tgz", - "integrity": "sha512-gU3r74DQu5pjOfeJsWKAja1FUw5xLjFXIwRMDPxJ2w0oNtkz5jdsTX/waqVe/uEIwMAlkQ8MbJiwEE94eMMZHA==", + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/@athenna/ioc/-/ioc-1.0.6.tgz", + "integrity": "sha512-vNlk7+penNKd61eFIHk7OSjor6HM1CO5c9ggUPcO37qvim9w7ZAyMmuMDJ1pSfgtwKsysOgxkMFyWtbpvkGWpA==", "requires": { "@secjs/utils": "1.7.8", "awilix": "7.0.0", @@ -13820,9 +13609,9 @@ } }, "@athenna/logger": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/@athenna/logger/-/logger-1.0.2.tgz", - "integrity": "sha512-mS9NmzmMta1349fGZzfvT1DSR2geUV1qdMOLdVNnJ6h6VXtd/2tDBkK7OePnUZReM89FtDAzXzEn2Bkm5abORA==", + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@athenna/logger/-/logger-1.0.4.tgz", + "integrity": "sha512-m+8qSYzoCUFP0GwbIywaOheXJNbinAn5DIsngI3GT3T0dvY3AYjQ6Vx8qEDSeiNL31fFODpcVX61L9wp+ub93g==", "requires": { "@secjs/utils": "1.8.0", "reflect-metadata": "0.1.13", diff --git a/package.json b/package.json index 1e91875..22a50f1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/core", - "version": "1.0.2", + "version": "1.0.3", "description": "", "license": "MIT", "author": "João Lenon ", @@ -128,6 +128,7 @@ "no-useless-constructor": "off", "@typescript-eslint/no-useless-constructor": "off", "@typescript-eslint/camelcase": "off", + "@typescript-eslint/no-empty-function": "off", "@typescript-eslint/explicit-module-boundary-types": "off", "@typescript-eslint/interface-name-prefix": "off", "@typescript-eslint/no-explicit-any": "off", @@ -145,6 +146,10 @@ "src/*.d.ts", "src/**/*.js", "src/**/*.d.ts", + "container/*.js", + "container/*.d.ts", + "container/**/*.js", + "container/**/*.d.ts", "*.js", "*.d.ts" ], @@ -155,9 +160,9 @@ }, "dependencies": { "@athenna/config": "1.0.5", - "@athenna/http": "1.0.4", - "@athenna/ioc": "1.0.3", - "@athenna/logger": "1.0.2", + "@athenna/ioc": "1.0.6", + "@athenna/logger": "1.0.4", + "@athenna/http": "1.0.5", "@secjs/utils": "1.8.0", "reflect-metadata": "0.1.13", "tscpaths": "0.0.9" diff --git a/src/Factories/AthennaFactory.ts b/src/Factories/AthennaFactory.ts index 211ab07..884e905 100644 --- a/src/Factories/AthennaFactory.ts +++ b/src/Factories/AthennaFactory.ts @@ -11,7 +11,9 @@ import { Logger } from '@athenna/logger' import { parse, normalize } from 'path' import { Http, Router } from '@athenna/http' import { resolveEnvFile } from '@athenna/config' -import { Path, Config as SecConfig, Is } from '@secjs/utils' +import { Path, Config as SecConfig } from '@secjs/utils' +import { ResolveClassExport } from 'src/Utils/ResolveClassExport' +import { AthennaErrorHandler } from 'src/Utils/AthennaErrorHandler' export class AthennaFactory { private static logger: Logger @@ -22,21 +24,7 @@ export class AthennaFactory { const providersNormalized: any[] = [] providers.forEach(Provider => { - if (Is.Class(Provider)) { - providersNormalized.push(Provider) - - return - } - - if (Is.Object(Provider) && !Provider.default) { - const firstProviderKey = Object.keys(Provider)[0] - - providersNormalized.push(Provider[firstProviderKey]) - - return - } - - providersNormalized.push(Provider.default()) + providersNormalized.push(ResolveClassExport.resolve(Provider)) }) providersNormalized.forEach(Provider => @@ -99,6 +87,8 @@ export class AthennaFactory { const http = ioc.use('Athenna/Core/HttpServer') const route = ioc.use('Athenna/Core/HttpRoute') + http.setErrorHandler(AthennaErrorHandler.http) + route.register() const port = Config.get('app.port') diff --git a/src/Providers/ControllerProvider.ts b/src/Providers/ControllerProvider.ts new file mode 100644 index 0000000..a8b4a06 --- /dev/null +++ b/src/Providers/ControllerProvider.ts @@ -0,0 +1,30 @@ +/** + * @athenna/core + * + * (c) João Lenon + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +import { Folder, Path } from '@secjs/utils' +import { ServiceProvider } from '@athenna/ioc' +import { ResolveClassExport } from 'src/Utils/ResolveClassExport' + +export class ControllerProvider extends ServiceProvider { + boot(): void { + const controllers = new Folder(Path.app('Http/Controllers')) + .loadSync() + // Get all .js and .ts files but not the .d.ts. + .getFilesByPattern('!(*.d)*.*(js|ts)') + + controllers.forEach(File => { + this.container.bind( + `App/Controllers/${File.name}`, + ResolveClassExport.resolve(require(File.path)), + ) + }) + } + + register(): void {} +} diff --git a/src/Providers/MiddlewareProvider.ts b/src/Providers/MiddlewareProvider.ts new file mode 100644 index 0000000..8003cf1 --- /dev/null +++ b/src/Providers/MiddlewareProvider.ts @@ -0,0 +1,30 @@ +/** + * @athenna/core + * + * (c) João Lenon + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +import { Folder, Path } from '@secjs/utils' +import { ServiceProvider } from '@athenna/ioc' +import { ResolveClassExport } from 'src/Utils/ResolveClassExport' + +export class MiddlewareProvider extends ServiceProvider { + boot(): void { + const middlewares = new Folder(Path.app('Http/Middlewares')) + .loadSync() + // Get all .js and .ts files but not the .d.ts. + .getFilesByPattern('!(*.d)*.*(js|ts)') + + middlewares.forEach(File => { + this.container.bind( + `App/Middlewares/${File.name}`, + ResolveClassExport.resolve(require(File.path)), + ) + }) + } + + register(): void {} +} diff --git a/src/Utils/AthennaErrorHandler.ts b/src/Utils/AthennaErrorHandler.ts new file mode 100644 index 0000000..e9ea8e1 --- /dev/null +++ b/src/Utils/AthennaErrorHandler.ts @@ -0,0 +1,46 @@ +/** + * @secjs/core + * + * (c) João Lenon + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +import { String } from '@secjs/utils' +import { Logger } from '@athenna/logger' +import { ErrorContextContract } from '@athenna/http' + +export class AthennaErrorHandler { + // TODO + // static worker() {} + + // TODO + // static command() {} + + static http({ error, request, response }: ErrorContextContract) { + const code = error.code || error.name + const statusCode = error.statusCode || error.status || 500 + + const body = { + code: String.toSnakeCase(code).toUpperCase(), + path: request.baseUrl, + method: request.method, + status: statusCode <= 399 ? 'SUCCESS' : 'ERROR', + statusCode: statusCode, + error: { + name: error.name, + message: error.message, + stack: error.stack, + }, + } + + new Logger() + .channel('application') + .error(`Error: ${JSON.stringify(body.error, null, 2)}`, { + context: AthennaErrorHandler.name, + }) + + response.status(statusCode).send(body) + } +} diff --git a/src/Utils/ResolveClassExport.ts b/src/Utils/ResolveClassExport.ts new file mode 100644 index 0000000..0a8a215 --- /dev/null +++ b/src/Utils/ResolveClassExport.ts @@ -0,0 +1,26 @@ +/** + * @secjs/core + * + * (c) João Lenon + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +import { Is } from '@secjs/utils' + +export class ResolveClassExport { + static resolve(module: any) { + if (Is.Class(module)) { + return module + } + + if (Is.Object(module) && !module.default) { + const firstProviderKey = Object.keys(module)[0] + + return module[firstProviderKey] + } + + return module.default() + } +} diff --git a/tests/Stubs/config/logging.ts b/tests/Stubs/config/logging.ts index 24e4d6c..ab4b802 100644 --- a/tests/Stubs/config/logging.ts +++ b/tests/Stubs/config/logging.ts @@ -29,17 +29,21 @@ export default { channels: { application: { driver: 'console', + level: 'INFO', context: 'Logger', formatter: 'context', + streamType: 'stdout', }, debug: { driver: 'debug', + level: 'DEBUG', context: 'Debugger', formatter: 'context', namespace: 'api:main', }, file: { driver: 'file', + level: 'INFO', context: 'Logger', formatter: 'log', filePath: Path.noBuild().logs('athenna.log'), diff --git a/tests/Stubs/start/routes.ts b/tests/Stubs/start/routes.ts index ab706e4..8f13a21 100644 --- a/tests/Stubs/start/routes.ts +++ b/tests/Stubs/start/routes.ts @@ -1,4 +1,4 @@ -import { Route } from 'src/Modules/HttpRoute' +import { Route } from 'container/HttpRoute' Route.get('/healthcheck', ({ response }) => { return response.status(200).send({ status: 'ok' }) diff --git a/tests/Unit/IgniteTest.ts b/tests/Unit/IgniteTest.ts index 5b97a6d..770dbf9 100644 --- a/tests/Unit/IgniteTest.ts +++ b/tests/Unit/IgniteTest.ts @@ -7,7 +7,7 @@ * file that was distributed with this source code. */ -import { Ignite } from '../../src/Ignite' +import { Ignite } from 'src/Ignite' import { File, Folder, Path } from '@secjs/utils' describe('\n IgniteTest', () => { diff --git a/tsconfig.json b/tsconfig.json index 958102d..030e295 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -19,7 +19,8 @@ "experimentalDecorators": true, "paths": { "src/*": ["src/*"], - "tests/*": ["tests/*"] + "tests/*": ["tests/*"], + "container/*": ["container/*"] }, "types": [ "jest", From 8b3b5c0757013e6926edaa6465a5ad8ea1c03697 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Wed, 30 Mar 2022 19:33:49 -0300 Subject: [PATCH 3/3] feat: add container folder and controller/middleware providers --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 22a50f1..aad4aa3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/core", - "version": "1.0.3", + "version": "1.0.2", "description": "", "license": "MIT", "author": "João Lenon ",