raiton 1.0.0 → 3.0.0
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/CHANGELOG.md +39 -0
- package/README.md +97 -1
- package/build/bin/index.mjs +7012 -47
- package/deno.json +9 -0
- package/package.json +22 -43
- package/scripts/update-version.ts +97 -0
- package/source/bin/bootstrapper.ts +15 -0
- package/source/bin/cli-tools.ts +68 -0
- package/source/bin/cli.ts +12 -0
- package/source/bin/constants.ts +5 -0
- package/source/bin/index.ts +6 -0
- package/source/commands/artifact.command.ts +28 -0
- package/source/commands/build.command.ts +31 -0
- package/source/commands/develop.command.ts +47 -0
- package/source/commands/grafts.command.ts +27 -0
- package/source/commands/start.command.ts +28 -0
- package/source/core/application.ts +163 -0
- package/source/core/builder.ts +144 -0
- package/source/core/bytes.util.ts +17 -0
- package/source/core/command.ts +24 -0
- package/source/core/commands.ts +44 -0
- package/source/core/config/config.ts +51 -0
- package/source/core/config/define.ts +13 -0
- package/source/core/config/index.ts +2 -0
- package/source/core/context.ts +33 -0
- package/source/core/controller/builder.ts +41 -0
- package/source/core/controller/compiler.ts +22 -0
- package/source/core/controller/index.ts +3 -0
- package/source/core/controller/metadata.ts +13 -0
- package/source/core/directories.ts +30 -0
- package/source/core/helpers/index.ts +1 -0
- package/source/core/helpers/raiton.ts +3 -0
- package/source/core/hooks.ts +28 -0
- package/source/core/index.ts +14 -0
- package/source/core/injection/index.ts +1 -0
- package/source/core/injection/injection.ts +219 -0
- package/source/core/middleware/compose.ts +40 -0
- package/source/core/middleware/index.ts +2 -0
- package/source/core/middleware/pipeline.ts +27 -0
- package/source/core/plugins/index.ts +2 -0
- package/source/core/plugins/plugin.ts +8 -0
- package/source/core/plugins/scope.ts +46 -0
- package/source/core/process.util.ts +12 -0
- package/source/core/raiton.ts +20 -0
- package/source/core/router/handler.ts +115 -0
- package/source/core/router/index.ts +4 -0
- package/source/core/router/matcher.ts +51 -0
- package/source/core/router/route.ts +63 -0
- package/source/core/router/router.ts +31 -0
- package/source/core/server.ts +26 -0
- package/source/core/thread.ts +112 -0
- package/source/env.d.ts +3 -0
- package/source/requirements.ts +27 -0
- package/source/sdk/artifacts.ts +79 -0
- package/source/sdk/constants/decorators.constant.ts +8 -0
- package/source/sdk/constants/index.ts +2 -0
- package/source/sdk/constants/microservices.constant.ts +4 -0
- package/source/sdk/controllers.ts +4 -0
- package/source/sdk/data-transfer-object.ts +15 -0
- package/source/sdk/decorators/access-guard.decorator.ts +9 -0
- package/source/sdk/decorators/controllable.decorator.ts +20 -0
- package/source/sdk/decorators/index.ts +5 -0
- package/source/sdk/decorators/injection.decorator.ts +46 -0
- package/source/sdk/decorators/middleware.decorator.ts +15 -0
- package/source/sdk/decorators/parametrable.ts +28 -0
- package/source/sdk/decorators/routable.decorator.ts +45 -0
- package/source/sdk/encryption.ts +157 -0
- package/source/sdk/enums/encrypted.enum.ts +23 -0
- package/source/sdk/enums/event.message.enum.ts +4 -0
- package/source/sdk/enums/http-method.enum.ts +10 -0
- package/{build/sdk/enums/http-parameters.enum.d.ts → source/sdk/enums/http-parameters.enum.ts} +3 -5
- package/source/sdk/enums/http-status.enum.ts +73 -0
- package/source/sdk/enums/index.ts +7 -0
- package/{build/sdk/enums/runtime.enum.d.ts → source/sdk/enums/runtime.enum.ts} +2 -4
- package/{build/sdk/enums/timestamp.enum.d.ts → source/sdk/enums/timestamp.enum.ts} +2 -4
- package/source/sdk/env.ts +40 -0
- package/source/sdk/exceptions/http-exception.ts +28 -0
- package/source/sdk/exceptions/index.ts +2 -0
- package/{build/sdk/throwable.d.ts → source/sdk/exceptions/throwable.ts} +35 -12
- package/source/sdk/index.ts +14 -0
- package/source/sdk/parameter-bag.ts +55 -0
- package/source/sdk/plugins/body-parser.plugin.ts +160 -0
- package/source/sdk/plugins/index.ts +2 -0
- package/source/sdk/plugins/security/body-limit.ts +19 -0
- package/source/sdk/plugins/security/cors.ts +44 -0
- package/source/sdk/plugins/security/headers.ts +14 -0
- package/source/sdk/plugins/security/index.ts +13 -0
- package/source/sdk/plugins/security/method-guard.ts +14 -0
- package/source/sdk/plugins/security/rate-limit.ts +42 -0
- package/source/sdk/repositories.ts +14 -0
- package/source/sdk/responses/error.ts +52 -0
- package/source/sdk/responses/helpers.ts +28 -0
- package/source/sdk/responses/http-throwable.ts +28 -0
- package/source/sdk/responses/http.ts +48 -0
- package/source/sdk/responses/index.ts +4 -0
- package/source/sdk/runtime/bun/server.ts +73 -0
- package/source/sdk/runtime/deno/server.ts +53 -0
- package/source/sdk/runtime/index.ts +47 -0
- package/source/sdk/runtime/node/server.ts +60 -0
- package/source/sdk/runtime/web/server.ts +57 -0
- package/source/sdk/services.ts +9 -0
- package/source/sdk/utilities/alias-path.util.ts +49 -0
- package/source/sdk/utilities/artifact.util.ts +18 -0
- package/source/sdk/utilities/callable.util.ts +27 -0
- package/source/sdk/utilities/index.ts +5 -0
- package/source/sdk/utilities/json.util.ts +21 -0
- package/source/sdk/utilities/path.util.ts +19 -0
- package/source/sdk/utilities/url.ts +5 -0
- package/source/sdk/utilities/utilities.util.ts +25 -0
- package/source/types/access-guards.ts +4 -0
- package/{build/types/application.d.ts → source/types/application.ts} +23 -7
- package/source/types/artifact.ts +44 -0
- package/source/types/builder.ts +44 -0
- package/source/types/config.ts +7 -0
- package/source/types/controller.ts +33 -0
- package/source/types/contruct.ts +3 -0
- package/source/types/core.ts +28 -0
- package/{build/types/directory.d.ts → source/types/directory.ts} +4 -4
- package/source/types/encryption.ts +17 -0
- package/source/types/generic.ts +3 -0
- package/source/types/http-responses.ts +8 -0
- package/source/types/index.ts +25 -0
- package/source/types/injection.ts +13 -0
- package/source/types/lifecycle.ts +11 -0
- package/source/types/middleware.ts +18 -0
- package/source/types/parseable.ts +7 -0
- package/source/types/plugin.ts +10 -0
- package/source/types/raiton.ts +7 -0
- package/source/types/responses.ts +20 -0
- package/source/types/router.ts +11 -0
- package/source/types/runtime.ts +56 -0
- package/{build/types/scheme.d.ts → source/types/scheme.ts} +33 -8
- package/source/types/server.ts +12 -0
- package/source/types/thread.ts +31 -0
- package/source/types/utilities.ts +4 -0
- package/source/types/values.ts +3 -0
- package/.releaserc.json +0 -39
- package/build/bin/bootstrapper.d.ts +0 -5
- package/build/bin/bootstrapper.mjs +0 -46
- package/build/bin/cli.d.ts +0 -5
- package/build/bin/cli.mjs +0 -6
- package/build/bin/index.d.ts +0 -1
- package/build/chunk-3PWMRP6G.mjs +0 -16
- package/build/chunk-52JR26TI.mjs +0 -29
- package/build/chunk-5LNOA4SK.mjs +0 -16
- package/build/chunk-AUGL35CF.mjs +0 -24
- package/build/chunk-BEV2YOPG.mjs +0 -14
- package/build/chunk-BYYJRCB4.mjs +0 -10
- package/build/chunk-FFRJ4AUA.mjs +0 -16
- package/build/chunk-GPJSLV3Q.mjs +0 -50
- package/build/chunk-HVFHDVAH.mjs +0 -20
- package/build/chunk-IOCHNQMF.mjs +0 -13
- package/build/chunk-JATYBWHL.mjs +0 -36
- package/build/chunk-JWNBO7XV.mjs +0 -53
- package/build/chunk-K4IQQ2KF.mjs +0 -55
- package/build/chunk-K4KLOLSO.mjs +0 -645
- package/build/chunk-M2RZZJQR.mjs +0 -27
- package/build/chunk-MLFGBJDV.mjs +0 -12
- package/build/chunk-NB62GSFI.mjs +0 -27
- package/build/chunk-O7R3NJQN.mjs +0 -19
- package/build/chunk-P3IRIFPT.mjs +0 -53
- package/build/chunk-QFSXFOEN.mjs +0 -56
- package/build/chunk-QHCQ4AUA.mjs +0 -14
- package/build/chunk-QI7OFSXB.mjs +0 -37
- package/build/chunk-RVH2YBNU.mjs +0 -111
- package/build/chunk-RXG7754R.mjs +0 -25
- package/build/chunk-S3ONGVNZ.mjs +0 -11
- package/build/chunk-SRR4467I.mjs +0 -29
- package/build/chunk-T6L55AUG.mjs +0 -32
- package/build/chunk-TOJFTPAY.mjs +0 -83
- package/build/chunk-UAPCBU3J.mjs +0 -12
- package/build/chunk-UCHBN3DV.mjs +0 -52
- package/build/chunk-UWM46HLZ.mjs +0 -18
- package/build/chunk-UZFCGVHP.mjs +0 -8
- package/build/chunk-VQINCGLQ.mjs +0 -59
- package/build/chunk-VRL4S7UF.mjs +0 -74
- package/build/chunk-WAWFDMOH.mjs +0 -72
- package/build/chunk-X3JFRRRQ.mjs +0 -45
- package/build/chunk-XFBLELHI.mjs +0 -34
- package/build/chunk-XIGE72LC.mjs +0 -34
- package/build/chunk-XW7EPAD7.mjs +0 -0
- package/build/chunk-YGIZFKLJ.mjs +0 -20
- package/build/chunk-YRVIAORI.mjs +0 -8
- package/build/chunk-YXU5W2CB.mjs +0 -12
- package/build/commands/artifact.command.d.ts +0 -14
- package/build/commands/artifact.command.mjs +0 -59
- package/build/commands/build.command.d.ts +0 -14
- package/build/commands/build.command.mjs +0 -65
- package/build/commands/develop.command.d.ts +0 -13
- package/build/commands/develop.command.mjs +0 -75
- package/build/commands/grafts.command.d.ts +0 -12
- package/build/commands/grafts.command.mjs +0 -58
- package/build/commands/start.command.d.ts +0 -12
- package/build/commands/start.command.mjs +0 -64
- package/build/core/application.d.ts +0 -27
- package/build/core/application.mjs +0 -124
- package/build/core/artifact.d.ts +0 -19
- package/build/core/artifact.mjs +0 -133
- package/build/core/builder.d.ts +0 -35
- package/build/core/builder.mjs +0 -45
- package/build/core/bytes.util.d.ts +0 -6
- package/build/core/bytes.util.mjs +0 -8
- package/build/core/command.d.ts +0 -15
- package/build/core/command.mjs +0 -6
- package/build/core/commands.d.ts +0 -13
- package/build/core/commands.mjs +0 -7
- package/build/core/config.d.ts +0 -10
- package/build/core/config.mjs +0 -7
- package/build/core/context.d.ts +0 -15
- package/build/core/context.mjs +0 -6
- package/build/core/controller/builder.d.ts +0 -10
- package/build/core/controller/builder.mjs +0 -45
- package/build/core/controller/compiler.d.ts +0 -6
- package/build/core/controller/compiler.mjs +0 -45
- package/build/core/controller/index.d.ts +0 -13
- package/build/core/controller/index.mjs +0 -50
- package/build/core/controller/metadata.d.ts +0 -10
- package/build/core/controller/metadata.mjs +0 -6
- package/build/core/directories.d.ts +0 -11
- package/build/core/directories.mjs +0 -8
- package/build/core/hmr.d.ts +0 -22
- package/build/core/hmr.mjs +0 -6
- package/build/core/hooks.d.ts +0 -12
- package/build/core/hooks.mjs +0 -6
- package/build/core/index.d.ts +0 -41
- package/build/core/index.mjs +0 -100
- package/build/core/middleware/compose.d.ts +0 -8
- package/build/core/middleware/compose.mjs +0 -6
- package/build/core/middleware/index.d.ts +0 -6
- package/build/core/middleware/index.mjs +0 -11
- package/build/core/middleware/pipeline.d.ts +0 -14
- package/build/core/middleware/pipeline.mjs +0 -7
- package/build/core/plugins/index.d.ts +0 -14
- package/build/core/plugins/index.mjs +0 -48
- package/build/core/plugins/plugin.d.ts +0 -17
- package/build/core/plugins/plugin.mjs +0 -6
- package/build/core/plugins/scope.d.ts +0 -24
- package/build/core/plugins/scope.mjs +0 -45
- package/build/core/process.util.d.ts +0 -3
- package/build/core/process.util.mjs +0 -6
- package/build/core/raiton.d.ts +0 -20
- package/build/core/raiton.mjs +0 -6
- package/build/core/router/handler.d.ts +0 -10
- package/build/core/router/handler.mjs +0 -45
- package/build/core/router/index.d.ts +0 -12
- package/build/core/router/index.mjs +0 -54
- package/build/core/router/matcher.d.ts +0 -21
- package/build/core/router/matcher.mjs +0 -6
- package/build/core/router/route.d.ts +0 -20
- package/build/core/router/route.mjs +0 -6
- package/build/core/router/router.d.ts +0 -16
- package/build/core/router/router.mjs +0 -8
- package/build/core/thread.d.ts +0 -30
- package/build/core/thread.mjs +0 -45
- package/build/env.d.d.ts +0 -2
- package/build/env.d.mjs +0 -0
- package/build/raiton-1.0.0.tgz +0 -0
- package/build/requirements.d.ts +0 -2
- package/build/requirements.mjs +0 -19
- package/build/sdk/artifacts.d.ts +0 -13
- package/build/sdk/artifacts.mjs +0 -45
- package/build/sdk/constants/decorators.constant.d.ts +0 -11
- package/build/sdk/constants/decorators.constant.mjs +0 -6
- package/build/sdk/constants/index.d.ts +0 -2
- package/build/sdk/constants/index.mjs +0 -11
- package/build/sdk/constants/microservices.constant.d.ts +0 -5
- package/build/sdk/constants/microservices.constant.mjs +0 -6
- package/build/sdk/controllers.d.ts +0 -4
- package/build/sdk/controllers.mjs +0 -6
- package/build/sdk/data-transfer-object.d.ts +0 -7
- package/build/sdk/data-transfer-object.mjs +0 -7
- package/build/sdk/decorators/access-guard.decorator.d.ts +0 -2
- package/build/sdk/decorators/access-guard.decorator.mjs +0 -0
- package/build/sdk/decorators/controllable.d.ts +0 -3
- package/build/sdk/decorators/controllable.mjs +0 -45
- package/build/sdk/decorators/controllers.decorator.d.ts +0 -2
- package/build/sdk/decorators/controllers.decorator.mjs +0 -0
- package/build/sdk/decorators/grafts.decorator.d.ts +0 -2
- package/build/sdk/decorators/grafts.decorator.mjs +0 -0
- package/build/sdk/decorators/index.d.ts +0 -3
- package/build/sdk/decorators/index.mjs +0 -75
- package/build/sdk/decorators/parameters.decorator.d.ts +0 -2
- package/build/sdk/decorators/parameters.decorator.mjs +0 -0
- package/build/sdk/decorators/parametrable.d.ts +0 -9
- package/build/sdk/decorators/parametrable.mjs +0 -57
- package/build/sdk/decorators/payload.decorator.d.ts +0 -2
- package/build/sdk/decorators/payload.decorator.mjs +0 -0
- package/build/sdk/decorators/routable.d.ts +0 -10
- package/build/sdk/decorators/routable.mjs +0 -59
- package/build/sdk/dependency-container.d.ts +0 -19
- package/build/sdk/dependency-container.mjs +0 -46
- package/build/sdk/encryption.d.ts +0 -27
- package/build/sdk/encryption.mjs +0 -141
- package/build/sdk/enums/encrypted.enum.d.ts +0 -15
- package/build/sdk/enums/encrypted.enum.mjs +0 -6
- package/build/sdk/enums/event.message.enum.d.ts +0 -6
- package/build/sdk/enums/event.message.enum.mjs +0 -6
- package/build/sdk/enums/http-parameters.enum.mjs +0 -6
- package/build/sdk/enums/http.enum.d.ts +0 -12
- package/build/sdk/enums/http.enum.mjs +0 -6
- package/build/sdk/enums/index.d.ts +0 -6
- package/build/sdk/enums/index.mjs +0 -27
- package/build/sdk/enums/runtime.enum.mjs +0 -6
- package/build/sdk/enums/timestamp.enum.mjs +0 -6
- package/build/sdk/env.d.ts +0 -6
- package/build/sdk/env.mjs +0 -74
- package/build/sdk/fastify.d.ts +0 -11
- package/build/sdk/fastify.mjs +0 -9
- package/build/sdk/grafts.d.ts +0 -15
- package/build/sdk/grafts.mjs +0 -71
- package/build/sdk/index.d.ts +0 -34
- package/build/sdk/index.mjs +0 -126
- package/build/sdk/json.d.ts +0 -17
- package/build/sdk/json.mjs +0 -87
- package/build/sdk/plugins/body-parser.plugin.d.ts +0 -17
- package/build/sdk/plugins/body-parser.plugin.mjs +0 -7
- package/build/sdk/plugins/index.d.ts +0 -17
- package/build/sdk/plugins/index.mjs +0 -48
- package/build/sdk/plugins/security/body-limit.d.ts +0 -17
- package/build/sdk/plugins/security/body-limit.mjs +0 -45
- package/build/sdk/plugins/security/cors.d.ts +0 -22
- package/build/sdk/plugins/security/cors.mjs +0 -45
- package/build/sdk/plugins/security/headers.d.ts +0 -17
- package/build/sdk/plugins/security/headers.mjs +0 -45
- package/build/sdk/plugins/security/index.d.ts +0 -25
- package/build/sdk/plugins/security/index.mjs +0 -45
- package/build/sdk/plugins/security/method-guard.d.ts +0 -17
- package/build/sdk/plugins/security/method-guard.mjs +0 -45
- package/build/sdk/plugins/security/rate-limit.d.ts +0 -21
- package/build/sdk/plugins/security/rate-limit.mjs +0 -45
- package/build/sdk/repositories.d.ts +0 -7
- package/build/sdk/repositories.mjs +0 -17
- package/build/sdk/request.d.ts +0 -4
- package/build/sdk/request.mjs +0 -6
- package/build/sdk/responses.d.ts +0 -8
- package/build/sdk/responses.mjs +0 -20
- package/build/sdk/routes.d.ts +0 -7
- package/build/sdk/routes.mjs +0 -61
- package/build/sdk/runtime/bun/server.d.ts +0 -6
- package/build/sdk/runtime/bun/server.mjs +0 -6
- package/build/sdk/runtime/deno/server.d.ts +0 -6
- package/build/sdk/runtime/deno/server.mjs +0 -6
- package/build/sdk/runtime/index.d.ts +0 -15
- package/build/sdk/runtime/index.mjs +0 -11
- package/build/sdk/runtime/node/reply.d.ts +0 -2
- package/build/sdk/runtime/node/reply.mjs +0 -0
- package/build/sdk/runtime/node/request.d.ts +0 -2
- package/build/sdk/runtime/node/request.mjs +0 -0
- package/build/sdk/runtime/node/server.d.ts +0 -6
- package/build/sdk/runtime/node/server.mjs +0 -6
- package/build/sdk/runtime/web/server.d.ts +0 -6
- package/build/sdk/runtime/web/server.mjs +0 -6
- package/build/sdk/schemes.d.ts +0 -120
- package/build/sdk/schemes.mjs +0 -129
- package/build/sdk/services.d.ts +0 -8
- package/build/sdk/services.mjs +0 -10
- package/build/sdk/throwable.mjs +0 -14
- package/build/sdk/utilities/alias-path.util.d.ts +0 -9
- package/build/sdk/utilities/alias-path.util.mjs +0 -6
- package/build/sdk/utilities/artifacts.util.d.ts +0 -3
- package/build/sdk/utilities/artifacts.util.mjs +0 -45
- package/build/sdk/utilities/callable.util.d.ts +0 -3
- package/build/sdk/utilities/callable.util.mjs +0 -6
- package/build/sdk/utilities/controller.util.d.ts +0 -3
- package/build/sdk/utilities/controller.util.mjs +0 -6
- package/build/sdk/utilities/index.d.ts +0 -7
- package/build/sdk/utilities/index.mjs +0 -62
- package/build/sdk/utilities/json.util.d.ts +0 -3
- package/build/sdk/utilities/json.util.mjs +0 -6
- package/build/sdk/utilities/parameters-arguments.util.d.ts +0 -2
- package/build/sdk/utilities/parameters-arguments.util.mjs +0 -0
- package/build/sdk/utilities/url.d.ts +0 -3
- package/build/sdk/utilities/url.mjs +0 -7
- package/build/sdk/utilities/utilities.util.d.ts +0 -6
- package/build/sdk/utilities/utilities.util.mjs +0 -8
- package/build/types/access-guards.d.ts +0 -6
- package/build/types/access-guards.mjs +0 -0
- package/build/types/application.mjs +0 -0
- package/build/types/artifact.d.ts +0 -21
- package/build/types/artifact.mjs +0 -0
- package/build/types/builder.d.ts +0 -32
- package/build/types/builder.mjs +0 -0
- package/build/types/config.d.ts +0 -6
- package/build/types/config.mjs +0 -0
- package/build/types/controller.d.ts +0 -25
- package/build/types/controller.mjs +0 -0
- package/build/types/contruct.d.ts +0 -3
- package/build/types/contruct.mjs +0 -0
- package/build/types/core.d.ts +0 -15
- package/build/types/core.mjs +0 -0
- package/build/types/data-transfer-object.d.ts +0 -5
- package/build/types/data-transfer-object.mjs +0 -0
- package/build/types/dependency-container.d.ts +0 -20
- package/build/types/dependency-container.mjs +0 -0
- package/build/types/directory.mjs +0 -0
- package/build/types/encryption.d.ts +0 -16
- package/build/types/encryption.mjs +0 -0
- package/build/types/generic.d.ts +0 -4
- package/build/types/generic.mjs +0 -0
- package/build/types/hmr.d.ts +0 -25
- package/build/types/hmr.mjs +0 -0
- package/build/types/http-responses.d.ts +0 -10
- package/build/types/http-responses.mjs +0 -0
- package/build/types/index.d.ts +0 -45
- package/build/types/index.mjs +0 -0
- package/build/types/middleware.d.ts +0 -12
- package/build/types/middleware.mjs +0 -0
- package/build/types/parameters.d.ts +0 -19
- package/build/types/parameters.mjs +0 -0
- package/build/types/parseable.d.ts +0 -5
- package/build/types/parseable.mjs +0 -0
- package/build/types/payload.d.ts +0 -6
- package/build/types/payload.mjs +0 -0
- package/build/types/plugin.d.ts +0 -20
- package/build/types/plugin.mjs +0 -0
- package/build/types/raiton.d.ts +0 -12
- package/build/types/raiton.mjs +0 -0
- package/build/types/repositories.d.ts +0 -8
- package/build/types/repositories.mjs +0 -0
- package/build/types/router.d.ts +0 -14
- package/build/types/router.mjs +0 -0
- package/build/types/runtime.d.ts +0 -37
- package/build/types/runtime.mjs +0 -0
- package/build/types/scheme.mjs +0 -0
- package/build/types/server.d.ts +0 -57
- package/build/types/server.mjs +0 -0
- package/build/types/services.d.ts +0 -8
- package/build/types/services.mjs +0 -0
- package/build/types/thread.d.ts +0 -24
- package/build/types/thread.mjs +0 -0
- package/build/types/utilities.d.ts +0 -6
- package/build/types/utilities.mjs +0 -0
- package/build/types/values.d.ts +0 -4
- package/build/types/values.mjs +0 -0
- /package/{build/chunk-4OWCGDUD.mjs → source/sdk/runtime/node/reply.ts} +0 -0
- /package/{build/chunk-DIWD7ZCJ.mjs → source/sdk/runtime/node/request.ts} +0 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import type {IConstructor, ContainerDefinitionInterface} from "@/types";
|
|
3
|
+
import {LifetimeEnum, TextUtility} from "@protorians/core";
|
|
4
|
+
import {Logger} from "@protorians/logger";
|
|
5
|
+
import {METADATA_KEYS} from "@/sdk/constants";
|
|
6
|
+
import {Throwable} from "@/sdk/exceptions";
|
|
7
|
+
|
|
8
|
+
const camelCase = TextUtility.camelCase;
|
|
9
|
+
|
|
10
|
+
export class Injection {
|
|
11
|
+
|
|
12
|
+
protected static _classes: Map<string, ContainerDefinitionInterface> = new Map();
|
|
13
|
+
protected static _instances: Map<string, Map<any, any>> = new Map();
|
|
14
|
+
protected static _resolutionStack: string[] = [];
|
|
15
|
+
protected static _dependents: Map<string, Set<string>> = new Map();
|
|
16
|
+
protected static _artifactPaths: Map<string, string> = new Map();
|
|
17
|
+
|
|
18
|
+
static get classes(): Map<string, ContainerDefinitionInterface> {
|
|
19
|
+
return this._classes;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
static defaultScope = Symbol('default');
|
|
23
|
+
|
|
24
|
+
static get instances(): Map<string, Map<any, any>> {
|
|
25
|
+
return this._instances;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
static has(name: string): boolean {
|
|
29
|
+
return this._classes.has(this.normalizeName(name));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
static clear(): void {
|
|
33
|
+
this._classes.clear();
|
|
34
|
+
this._instances.clear();
|
|
35
|
+
this._dependents.clear();
|
|
36
|
+
this._artifactPaths.clear();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static normalizeName(name: string): string {
|
|
40
|
+
const stableName = camelCase(name);
|
|
41
|
+
return stableName[0].toLowerCase() + stableName.slice(1);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static registry(
|
|
45
|
+
name: string,
|
|
46
|
+
construct: IConstructor,
|
|
47
|
+
lifetime: LifetimeEnum = LifetimeEnum.SINGLETON,
|
|
48
|
+
scope?: Symbol
|
|
49
|
+
): typeof this {
|
|
50
|
+
if (!construct.name)
|
|
51
|
+
throw new Error('Le constructeur doit avoir un nom valide pour être enregistré dans le conteneur.');
|
|
52
|
+
|
|
53
|
+
this._classes.set(this.normalizeName(name), {name, construct, lifetime, scope});
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
static updateConstruct(name: string, construct: IConstructor): typeof this {
|
|
58
|
+
const name_ = this.normalizeName(name);
|
|
59
|
+
this._classes.set(name_, {...this._classes.get(this.normalizeName(name))!, construct});
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static getDependents(name: string): string[] {
|
|
64
|
+
return Array.from(this._dependents.get(this.normalizeName(name)) || []);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
static registerArtifactPath(name: string, path: string): typeof this {
|
|
68
|
+
this._artifactPaths.set(this.normalizeName(name), path);
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
static getArtifactPath(name: string): string | undefined {
|
|
73
|
+
return this._artifactPaths.get(this.normalizeName(name));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
static resolveArguments(definition: ContainerDefinitionInterface, scope?: any): any[] {
|
|
77
|
+
try {
|
|
78
|
+
const parameters = Reflect.getMetadata(METADATA_KEYS.INJECT_PARAMETERS, definition.construct) || [];
|
|
79
|
+
const designParameters = Reflect.getMetadata('design:paramtypes', definition.construct) || [];
|
|
80
|
+
const effectiveScope = scope || definition.scope || this.defaultScope;
|
|
81
|
+
|
|
82
|
+
const maxLen = Math.max(parameters.length, designParameters.length);
|
|
83
|
+
const args = [];
|
|
84
|
+
|
|
85
|
+
for (let i = 0; i < maxLen; i++) {
|
|
86
|
+
const param = parameters[i];
|
|
87
|
+
const designParam = designParameters[i];
|
|
88
|
+
|
|
89
|
+
if (param && param !== true) {
|
|
90
|
+
const token = typeof param === 'function' ? (param.name || param) : param;
|
|
91
|
+
if (typeof token === 'string') {
|
|
92
|
+
this.addDependent(token, definition.name);
|
|
93
|
+
args.push(this.get(token, effectiveScope));
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (typeof param === 'function') {
|
|
97
|
+
const metadata: ContainerDefinitionInterface = Reflect.getMetadata(METADATA_KEYS.CONTAINER, param);
|
|
98
|
+
const token = metadata?.name || param.name;
|
|
99
|
+
this.addDependent(token, definition.name);
|
|
100
|
+
args.push(this.get(token, effectiveScope));
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (designParam && typeof designParam === 'function' && designParam.name) {
|
|
106
|
+
const metadata: ContainerDefinitionInterface = Reflect.getMetadata(METADATA_KEYS.CONTAINER, designParam);
|
|
107
|
+
const token = metadata?.name || designParam.name;
|
|
108
|
+
this.addDependent(token, definition.name);
|
|
109
|
+
args.push(this.get(token, effectiveScope));
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
args.push(undefined);
|
|
114
|
+
}
|
|
115
|
+
return args;
|
|
116
|
+
} catch (e) {
|
|
117
|
+
Logger.error('Resolve', e);
|
|
118
|
+
return [];
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
protected static addDependent(dependencyName: string, dependentName: string) {
|
|
123
|
+
const dep = this.normalizeName(dependencyName);
|
|
124
|
+
if (!this._dependents.has(dep)) {
|
|
125
|
+
this._dependents.set(dep, new Set());
|
|
126
|
+
}
|
|
127
|
+
this._dependents.get(dep)!.add(this.normalizeName(dependentName));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
static get<T>(name: string, scope?: Symbol): T | undefined {
|
|
131
|
+
const name_ = this.normalizeName(name);
|
|
132
|
+
const cls = this._classes.get(name_);
|
|
133
|
+
if (!cls) throw new Throwable(`Dependency ${name} not registered`);
|
|
134
|
+
|
|
135
|
+
const effectiveScope = scope || cls.scope || this.defaultScope;
|
|
136
|
+
if (this._resolutionStack.includes(name_)) {
|
|
137
|
+
throw new Throwable(`Circular dependency detected: ${this._resolutionStack.join(' -> ')} -> ${name}`);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
this._resolutionStack.push(name_);
|
|
141
|
+
|
|
142
|
+
try {
|
|
143
|
+
let instance: any;
|
|
144
|
+
if (cls.lifetime === LifetimeEnum.SINGLETON) {
|
|
145
|
+
if (!this._instances.has(name_)) {
|
|
146
|
+
this._instances.set(name_, new Map());
|
|
147
|
+
}
|
|
148
|
+
const scopeInstances = this._instances.get(name_)!;
|
|
149
|
+
|
|
150
|
+
if (!scopeInstances.has(effectiveScope)) {
|
|
151
|
+
instance = new cls.construct(...this.resolveArguments(cls, effectiveScope));
|
|
152
|
+
scopeInstances.set(effectiveScope, instance);
|
|
153
|
+
this.injectProperties(instance, cls, effectiveScope);
|
|
154
|
+
this.triggerLifecycle(instance);
|
|
155
|
+
}
|
|
156
|
+
return scopeInstances.get(effectiveScope);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (cls.lifetime === LifetimeEnum.TRANSIENT) {
|
|
160
|
+
instance = new cls.construct(...this.resolveArguments(cls, effectiveScope));
|
|
161
|
+
this.injectProperties(instance, cls, effectiveScope);
|
|
162
|
+
this.triggerLifecycle(instance);
|
|
163
|
+
return instance as any;
|
|
164
|
+
}
|
|
165
|
+
} finally {
|
|
166
|
+
this._resolutionStack.pop();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return undefined;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
protected static injectProperties(instance: any, definition: ContainerDefinitionInterface, scope?: any): void {
|
|
173
|
+
const properties: Map<string | symbol, any> = Reflect.getMetadata(METADATA_KEYS.INJECT_PROPERTIES, definition.construct);
|
|
174
|
+
|
|
175
|
+
if (properties) {
|
|
176
|
+
for (const [propertyKey, type] of properties) {
|
|
177
|
+
const token = typeof type === 'function' ? (type.name || type) : type;
|
|
178
|
+
if (typeof token === 'string') {
|
|
179
|
+
this.addDependent(token, definition.name);
|
|
180
|
+
instance[propertyKey] = this.get(token, scope);
|
|
181
|
+
} else if (typeof type === 'function') {
|
|
182
|
+
const metadata: ContainerDefinitionInterface = Reflect.getMetadata(METADATA_KEYS.CONTAINER, type);
|
|
183
|
+
const token_ = metadata?.name || type.name;
|
|
184
|
+
this.addDependent(token_, definition.name);
|
|
185
|
+
instance[propertyKey] = this.get(token_, scope);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
protected static triggerLifecycle(instance: any): void {
|
|
192
|
+
if (typeof instance.onInit === 'function') {
|
|
193
|
+
instance.onInit();
|
|
194
|
+
}
|
|
195
|
+
if (typeof instance.onMount === 'function') {
|
|
196
|
+
instance.onMount();
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
static async shutdown(): Promise<void> {
|
|
201
|
+
for (const scopeInstances of this._instances.values()) {
|
|
202
|
+
for (const instance of scopeInstances.values()) {
|
|
203
|
+
if (typeof instance.onUnmount === 'function') {
|
|
204
|
+
await instance.onUnmount();
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
this.clear();
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
static resolve<T>(construct: IConstructor<T>): T {
|
|
212
|
+
const metadata: ContainerDefinitionInterface = Reflect.getMetadata(METADATA_KEYS.CONTAINER, construct);
|
|
213
|
+
|
|
214
|
+
if (!metadata)
|
|
215
|
+
throw new Throwable(`Cannot resolve ${construct.name} as dependency`);
|
|
216
|
+
|
|
217
|
+
return this.get(metadata.name) as T;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import {MiddlewareType} from '@/types'
|
|
2
|
+
import {Throwable} from "@/sdk/exceptions";
|
|
3
|
+
|
|
4
|
+
export function middlewareCompose(middlewares: MiddlewareType[]) {
|
|
5
|
+
return function (request: any) {
|
|
6
|
+
let index = -1
|
|
7
|
+
|
|
8
|
+
return dispatch(0)
|
|
9
|
+
|
|
10
|
+
async function dispatch(i: number): Promise<void> {
|
|
11
|
+
if (i <= index) {
|
|
12
|
+
return Promise.reject(
|
|
13
|
+
new Throwable('next() called multiple times')
|
|
14
|
+
)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
index = i
|
|
18
|
+
const fn = middlewares[i];
|
|
19
|
+
try {
|
|
20
|
+
if (!fn) return Promise.resolve()
|
|
21
|
+
|
|
22
|
+
if (typeof fn === 'function') {
|
|
23
|
+
return Promise.resolve(fn({context: request, next: () => dispatch(i + 1)}))
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (typeof fn === 'object' && 'setup' in fn && typeof fn.setup == 'function') {
|
|
27
|
+
if (fn.setup.length === 1) {
|
|
28
|
+
await Promise.resolve(fn.setup(request))
|
|
29
|
+
return await dispatch(i + 1)
|
|
30
|
+
}
|
|
31
|
+
return Promise.resolve(fn.setup(request, () => dispatch(i + 1)))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return Promise.resolve()
|
|
35
|
+
} catch (err) {
|
|
36
|
+
return Promise.reject(err)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { MiddlewareType } from '@/types'
|
|
2
|
+
import { middlewareCompose } from '@/core'
|
|
3
|
+
|
|
4
|
+
export class MiddlewarePipeline {
|
|
5
|
+
private stack: MiddlewareType[] = []
|
|
6
|
+
|
|
7
|
+
use(mw: MiddlewareType): this {
|
|
8
|
+
this.stack.push(mw)
|
|
9
|
+
return this;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
clear(): this {
|
|
13
|
+
this.stack = []
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
run(ctx: any) {
|
|
18
|
+
const fn = middlewareCompose(this.stack)
|
|
19
|
+
return typeof fn == 'function' ? fn(ctx) : undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
clone() {
|
|
23
|
+
const pipeline = new MiddlewarePipeline()
|
|
24
|
+
pipeline.stack = [...this.stack]
|
|
25
|
+
return pipeline
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import {HookStore} from '@/core/hooks'
|
|
2
|
+
import {MiddlewarePipeline} from '@/core/middleware'
|
|
3
|
+
import {Route, Router} from '@/core/router'
|
|
4
|
+
|
|
5
|
+
export class PluginScope {
|
|
6
|
+
public hooks: HookStore
|
|
7
|
+
public middleware: MiddlewarePipeline
|
|
8
|
+
public router: Router
|
|
9
|
+
|
|
10
|
+
constructor(parent?: PluginScope) {
|
|
11
|
+
this.hooks = parent ? parent.hooks.clone() : new HookStore()
|
|
12
|
+
this.middleware = parent
|
|
13
|
+
? parent.middleware.clone()
|
|
14
|
+
: new MiddlewarePipeline()
|
|
15
|
+
this.router = parent ? parent.router : new Router()
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
addHook(name: any, fn: any): this {
|
|
19
|
+
this.hooks.add(name, fn)
|
|
20
|
+
return this;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
use(mw: any): this {
|
|
24
|
+
if (typeof mw === 'object' && mw !== null && 'setup' in mw && typeof mw.setup === 'function' && mw.setup.length === 1) {
|
|
25
|
+
mw.setup(this)
|
|
26
|
+
return this
|
|
27
|
+
}
|
|
28
|
+
this.middleware.use(mw)
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
route(
|
|
33
|
+
method: any,
|
|
34
|
+
path: string,
|
|
35
|
+
handler: any,
|
|
36
|
+
version?: string
|
|
37
|
+
): Route {
|
|
38
|
+
return this.router.add(method, path, handler, version)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
register(plugin: any): PluginScope {
|
|
42
|
+
const child = new PluginScope(this)
|
|
43
|
+
plugin.setup(child)
|
|
44
|
+
return child
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
|
|
2
|
+
export async function until(condition: () => boolean | Promise<boolean>, interval = 50): Promise<void> {
|
|
3
|
+
return new Promise<void>((resolve) => {
|
|
4
|
+
const check = () => {
|
|
5
|
+
const result = condition();
|
|
6
|
+
if (result instanceof Promise)
|
|
7
|
+
result.then((res) => res ? resolve() : setTimeout(check, interval));
|
|
8
|
+
else ((result) ? resolve() : setTimeout(check, interval))
|
|
9
|
+
}
|
|
10
|
+
check();
|
|
11
|
+
});
|
|
12
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {RaitonSignalMap, ThreadInterface} from "@/types";
|
|
2
|
+
import {ISignalStack, Signal} from "@protorians/core";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
export class Raiton {
|
|
6
|
+
protected static _thread: ThreadInterface | undefined;
|
|
7
|
+
|
|
8
|
+
static readonly signals: ISignalStack<RaitonSignalMap> = new Signal.Stack<RaitonSignalMap>()
|
|
9
|
+
static title: string = 'Protorians Raiton';
|
|
10
|
+
static identifier: string = 'raiton';
|
|
11
|
+
|
|
12
|
+
static get thread(): ThreadInterface| undefined {
|
|
13
|
+
// if (!this._thread) throw new Error(`${Raiton.title} Thread instance not initialized`);
|
|
14
|
+
return this._thread;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static set thread(thread: ThreadInterface | undefined) {
|
|
18
|
+
this._thread = this._thread || thread;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import {ControllerMetaInterface, MiddlewareCallable, ParamMetaInterface, RouteMetaInterface} from "@/types";
|
|
2
|
+
import {METADATA_KEYS, Parametrable} from "@/sdk";
|
|
3
|
+
import {Logger} from "@protorians/logger";
|
|
4
|
+
import {middlewareCompose, Raiton} from "@/core";
|
|
5
|
+
import {DataTransferObject} from "@/sdk/data-transfer-object";
|
|
6
|
+
import {Throwable} from "@/sdk/exceptions/throwable";
|
|
7
|
+
import {HttpException} from "@/sdk/exceptions";
|
|
8
|
+
import {ThrowableResponse} from "@/sdk/responses/http-throwable";
|
|
9
|
+
|
|
10
|
+
export function createHandler(
|
|
11
|
+
instance: any,
|
|
12
|
+
routeMeta: RouteMetaInterface,
|
|
13
|
+
controllerMeta: ControllerMetaInterface,
|
|
14
|
+
) {
|
|
15
|
+
return async (ctx: any) => {
|
|
16
|
+
const isDevelopment = Raiton.thread?.builder?.options?.development || false;
|
|
17
|
+
const handlerName = `${instance.constructor.name}.${routeMeta.propertyKey}`
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
const args: any[] = []
|
|
21
|
+
const params: ParamMetaInterface[] = Reflect.getMetadata(METADATA_KEYS.ROUTE_PARAMETERS, instance.constructor)?.[routeMeta.propertyKey] || []
|
|
22
|
+
|
|
23
|
+
for (const p of params) {
|
|
24
|
+
|
|
25
|
+
switch (p.type) {
|
|
26
|
+
case Parametrable.QUERY:
|
|
27
|
+
args[p.index] =
|
|
28
|
+
ctx.req.query?.[p.key!]
|
|
29
|
+
break
|
|
30
|
+
case Parametrable.PARAM:
|
|
31
|
+
args[p.index] =
|
|
32
|
+
ctx.req.params?.[p.key!] ?? ctx.params?.[p.key!]
|
|
33
|
+
break
|
|
34
|
+
case Parametrable.BODY:
|
|
35
|
+
if (p.metatype && 'prototype' in p.metatype) {
|
|
36
|
+
ctx.req.body = new p.metatype(ctx.req.body)
|
|
37
|
+
}
|
|
38
|
+
args[p.index] = ctx.req.body
|
|
39
|
+
break
|
|
40
|
+
case Parametrable.HEADER:
|
|
41
|
+
args[p.index] = ctx.req.headers[p.key!.toLowerCase()] as any;
|
|
42
|
+
break
|
|
43
|
+
case Parametrable.REQ:
|
|
44
|
+
args[p.index] = ctx.req
|
|
45
|
+
break
|
|
46
|
+
case Parametrable.REPLY:
|
|
47
|
+
args[p.index] = ctx.REPLY
|
|
48
|
+
break
|
|
49
|
+
case Parametrable.UPLOAD_FILE:
|
|
50
|
+
args[p.index] = ctx.req.file || ctx.req.files?.[p.key!]
|
|
51
|
+
break
|
|
52
|
+
case Parametrable.CUSTOM:
|
|
53
|
+
args[p.index] = p.callable?.(ctx) ?? null;
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (!(routeMeta.propertyKey in instance))
|
|
59
|
+
throw new Throwable(`${routeMeta.propertyKey} does not exist`)
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Middlewares running order
|
|
63
|
+
*/
|
|
64
|
+
const middlewares: MiddlewareCallable[] = [
|
|
65
|
+
...controllerMeta.middlewares['@'] || [],
|
|
66
|
+
...controllerMeta.middlewares[routeMeta.propertyKey] || []
|
|
67
|
+
]
|
|
68
|
+
if (middlewares.length > 0) await middlewareCompose(middlewares)(ctx)
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Vérification des arguments
|
|
72
|
+
*/
|
|
73
|
+
for (const input of args) {
|
|
74
|
+
/** DTO validation */
|
|
75
|
+
if (input instanceof DataTransferObject) {
|
|
76
|
+
const errors = await input.validation(false);
|
|
77
|
+
if (errors && errors.length) {
|
|
78
|
+
const stack = Object.values(errors[0].constraints || {})
|
|
79
|
+
throw new Throwable(stack.join(';'), 500)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
let responses = instance[routeMeta.propertyKey](...args)
|
|
85
|
+
if (responses instanceof Promise) responses = await responses
|
|
86
|
+
|
|
87
|
+
return responses;
|
|
88
|
+
} catch (err: any) {
|
|
89
|
+
|
|
90
|
+
if (!(err instanceof ThrowableResponse)) {
|
|
91
|
+
Logger.error(`Failed to execute ${handlerName} handler`, err.message ?? err);
|
|
92
|
+
ctx.reply.status(500)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (err instanceof ThrowableResponse) {
|
|
96
|
+
ctx.reply.status(err.statusCode || 201)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (err instanceof HttpException || err instanceof ThrowableResponse)
|
|
100
|
+
return err.render()
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
statusCode: 500,
|
|
104
|
+
error: true,
|
|
105
|
+
message: err.message ?? err,
|
|
106
|
+
data: null,
|
|
107
|
+
stack: isDevelopment
|
|
108
|
+
? (typeof err.stack === 'string' ? err.stack.split('\n')
|
|
109
|
+
: [String(err.stack || err.toString() || err.message || err.name || 'Unknown error')])
|
|
110
|
+
.map((l: any) => typeof l === 'string' ? l.trim() : String(l)) : undefined
|
|
111
|
+
,
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import {Route} from '@/core/router'
|
|
2
|
+
|
|
3
|
+
export class RouteMatcher {
|
|
4
|
+
protected routes = new Map<string, Route>()
|
|
5
|
+
|
|
6
|
+
protected getKey(route: Route): string {
|
|
7
|
+
return `${route.method}:${route.path}`
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
add(route: Route): this {
|
|
11
|
+
this.routes.set(this.getKey(route), route)
|
|
12
|
+
return this;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
has(route: Route): boolean {
|
|
16
|
+
return this.routes.has(this.getKey(route))
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
find(route: Route): Route | undefined {
|
|
20
|
+
return this.routes.get(this.getKey(route))
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
replace(route: Route): this {
|
|
24
|
+
if (this.has(route)) {
|
|
25
|
+
this.routes.set(this.getKey(route), route)
|
|
26
|
+
}
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
upsert(route: Route): this {
|
|
31
|
+
this.routes.set(this.getKey(route), route)
|
|
32
|
+
return this
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
remove(route: Route): this {
|
|
36
|
+
this.routes.delete(this.getKey(route))
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
match(method: string, url: string): Route | null {
|
|
41
|
+
for (const route of this.routes.values())
|
|
42
|
+
if (route.match(method, url))
|
|
43
|
+
return route.extractParams(url)
|
|
44
|
+
return null
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
clear(): this {
|
|
48
|
+
this.routes.clear()
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type {RouteDefinition} from '@/types/router'
|
|
2
|
+
|
|
3
|
+
export class Route {
|
|
4
|
+
method: string
|
|
5
|
+
path: string
|
|
6
|
+
version?: string
|
|
7
|
+
handler: RouteDefinition['handler']
|
|
8
|
+
parameters: Record<string, string> = {}
|
|
9
|
+
|
|
10
|
+
constructor(def: RouteDefinition) {
|
|
11
|
+
this.method = def.method
|
|
12
|
+
this.path = def.path
|
|
13
|
+
this.version = def.version
|
|
14
|
+
this.handler = def.handler
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get params(): string[] {
|
|
18
|
+
const fullPath = this.version ? `/${this.version}${this.path}` : this.path
|
|
19
|
+
const matches = fullPath.match(/:[a-zA-Z0-9_]+/g)
|
|
20
|
+
return matches ? matches.map(m => m.substring(1)) : []
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get name(): string {
|
|
24
|
+
return this.handler.name
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
extractParams(url: string): this {
|
|
28
|
+
const fullPath = (this.version ? `/${this.version}${this.path}` : this.path)
|
|
29
|
+
.replace(/\/+$/, '')
|
|
30
|
+
const paramNames = this.params
|
|
31
|
+
if (paramNames.length === 0) return this
|
|
32
|
+
|
|
33
|
+
const regexPath = fullPath.replace(/:[a-zA-Z0-9_]+/g, '([^/]+)')
|
|
34
|
+
const regex = new RegExp(`^${regexPath || '/'}/?$`)
|
|
35
|
+
const matches = url.match(regex)
|
|
36
|
+
|
|
37
|
+
if (matches) {
|
|
38
|
+
const params: Record<string, string> = {}
|
|
39
|
+
paramNames.forEach((name, index) => {
|
|
40
|
+
params[name] = matches[index + 1]
|
|
41
|
+
})
|
|
42
|
+
this.parameters = params
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return this
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
match(method: string, url: string): boolean {
|
|
49
|
+
if (method !== this.method) return false
|
|
50
|
+
|
|
51
|
+
const fullPath = (this.version ? `/${this.version}${this.path}` : this.path)
|
|
52
|
+
.replace(/\/+$/, '')
|
|
53
|
+
const cleanUrl = url.replace(/\/+$/, '')
|
|
54
|
+
|
|
55
|
+
if (!fullPath.includes(':')) {
|
|
56
|
+
return (fullPath || '/') === (cleanUrl || '/')
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const regexPath = fullPath.replace(/:[a-zA-Z0-9_]+/g, '([^/]+)')
|
|
60
|
+
const regex = new RegExp(`^${regexPath || '/'}/?$`)
|
|
61
|
+
return regex.test(url)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import {RouteHandler} from '@/types'
|
|
2
|
+
import {HttpMethod} from "@/sdk/enums";
|
|
3
|
+
import {Route} from '@/core/router/route'
|
|
4
|
+
import {RouteMatcher} from '@/core/router/matcher'
|
|
5
|
+
|
|
6
|
+
export class Router {
|
|
7
|
+
private matcher = new RouteMatcher()
|
|
8
|
+
|
|
9
|
+
add(method: HttpMethod, path: string, handler: RouteHandler, version?: string) {
|
|
10
|
+
const route = new Route({
|
|
11
|
+
method,
|
|
12
|
+
path,
|
|
13
|
+
handler,
|
|
14
|
+
version
|
|
15
|
+
})
|
|
16
|
+
this.matcher.add(route)
|
|
17
|
+
return route
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
remove(route: Route) {
|
|
21
|
+
this.matcher.remove(route)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
match(method: string, url: string) {
|
|
25
|
+
return this.matcher.match(method, url)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
reset() {
|
|
29
|
+
this.matcher.clear()
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type {ServerInterface, ServerOptions} from "@/types";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export class Server implements ServerInterface {
|
|
5
|
+
|
|
6
|
+
protected static _instance?: Server;
|
|
7
|
+
|
|
8
|
+
static get instance(): Server | undefined {
|
|
9
|
+
return this._instance
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
static set instance(value: Server) {
|
|
13
|
+
this._instance = this._instance || value;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
constructor(
|
|
17
|
+
public readonly options: ServerOptions
|
|
18
|
+
) {
|
|
19
|
+
Server.instance = this;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
option<K extends keyof ServerOptions>(key: K): ServerOptions[K] {
|
|
23
|
+
return this.options[key];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
}
|