@koalarx/nest 3.1.50 → 4.0.2
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/README.md +126 -439
- package/cli/commands/add/index.js +97 -0
- package/cli/commands/help.js +44 -0
- package/cli/commands/new/configure-test-runner.js +13 -0
- package/cli/commands/new/create-ddd-structure.js +52 -0
- package/cli/commands/new/create-empty-nest-project.js +58 -0
- package/cli/commands/new/fix-lint-config.js +24 -0
- package/cli/commands/new/index.js +202 -0
- package/cli/commands/version.js +5 -0
- package/cli/constants/auth-strategy-artifacts.js +56 -0
- package/cli/constants/auth-strategy-checklist.js +314 -0
- package/cli/constants/cli-commands.js +24 -0
- package/cli/constants/cli-project-checklist.js +289 -0
- package/cli/constants/core-packages.js +34 -0
- package/cli/constants/domain.js +171 -0
- package/cli/constants/package-manager.js +5 -0
- package/cli/constants/version.js +5 -0
- package/cli/index.js +55 -0
- package/cli/types/index.js +0 -0
- package/cli/utils/add-project-features.js +170 -0
- package/cli/utils/apply-optional-features.js +48 -0
- package/cli/utils/auth-strategy-validation.js +179 -0
- package/cli/utils/cancel.js +8 -0
- package/cli/utils/cli-options.js +27 -0
- package/cli/utils/cli-project-validation.js +157 -0
- package/cli/utils/detect-project-state.js +131 -0
- package/cli/utils/format-code.js +9 -0
- package/cli/utils/get-package-manager.js +12 -0
- package/cli/utils/get-package-root.js +19 -0
- package/cli/utils/get-source-code-path.js +15 -0
- package/cli/utils/install-module.js +258 -0
- package/cli/utils/normalize-add-args.js +25 -0
- package/cli/utils/parse-new-args.js +127 -0
- package/cli/utils/patch-auth-install.js +224 -0
- package/cli/utils/patch-define-documentation.js +222 -0
- package/cli/utils/patch-env.js +106 -0
- package/cli/utils/patch-generated-project.js +21 -0
- package/cli/utils/patch-health-module.js +80 -0
- package/cli/utils/patch-infra-module.js +62 -0
- package/cli/utils/patch-jobs-module.js +103 -0
- package/cli/utils/patch-main.js +15 -0
- package/cli/utils/patch-person-features.js +127 -0
- package/cli/utils/project-files.js +11 -0
- package/cli/utils/prune-auth-strategies.js +116 -0
- package/cli/utils/prune-core-auth.js +17 -0
- package/cli/utils/remove-sample-parts.js +203 -0
- package/cli/utils/resolve-project-path.js +19 -0
- package/cli/utils/restore-person-features.js +64 -0
- package/cli/utils/run-command.js +31 -0
- package/cli/utils/sync-auth-strategy-files.js +63 -0
- package/koala-nest/.env.example +34 -0
- package/koala-nest/.prettierrc +4 -0
- package/koala-nest/README.md +51 -0
- package/koala-nest/bunfig.toml +7 -0
- package/koala-nest/eslint.config.mjs +55 -0
- package/koala-nest/nest-cli.json +9 -0
- package/koala-nest/package.json +79 -0
- package/koala-nest/src/application/auth/common/auth-token.response.ts +15 -0
- package/koala-nest/src/application/auth/common/user-claims.ts +6 -0
- package/koala-nest/src/application/auth/login/login.handler.ts +42 -0
- package/koala-nest/src/application/auth/login/login.request.ts +9 -0
- package/koala-nest/src/application/auth/login/login.response.ts +3 -0
- package/koala-nest/src/application/auth/login/login.validator.ts +12 -0
- package/koala-nest/src/application/auth/oauth2/auth-link/auth-link.handler.ts +24 -0
- package/koala-nest/src/application/auth/oauth2/auth-link/auth-link.request.ts +10 -0
- package/koala-nest/src/application/auth/oauth2/auth-link/auth-link.response.ts +6 -0
- package/koala-nest/src/application/auth/oauth2/auth-link/auth-link.validator.ts +12 -0
- package/koala-nest/src/application/auth/oauth2/callback/oauth-callback.response.ts +10 -0
- package/koala-nest/src/application/auth/oauth2/exchange-code/exchange-code.handler.ts +73 -0
- package/koala-nest/src/application/auth/oauth2/exchange-code/exchange-code.request.ts +16 -0
- package/koala-nest/src/application/auth/oauth2/exchange-code/exchange-code.validator.ts +14 -0
- package/koala-nest/src/application/auth/oauth2/scalar-token/scalar-oauth-token.handler.ts +35 -0
- package/koala-nest/src/application/auth/oauth2/scalar-token/scalar-token.types.ts +1 -0
- package/koala-nest/src/application/auth/refresh-token/refresh-token.handler.ts +26 -0
- package/koala-nest/src/application/auth/user-info/user-info.handler.ts +30 -0
- package/koala-nest/src/application/auth/user-info/user-info.response.ts +20 -0
- package/koala-nest/src/application/common/created-registre.response.ts +20 -0
- package/koala-nest/src/application/common/pagination.request.ts +41 -0
- package/koala-nest/src/application/common/request-handler.base.ts +3 -0
- package/koala-nest/src/application/common/request-validator.base.ts +33 -0
- package/koala-nest/src/application/mapping/mapping.provider.ts +9 -0
- package/koala-nest/src/application/mapping/person.mapper.ts +44 -0
- package/koala-nest/src/application/person/create/create-person.handler.ts +34 -0
- package/koala-nest/src/application/person/create/create-person.request.ts +29 -0
- package/koala-nest/src/application/person/create/create-person.response.ts +3 -0
- package/koala-nest/src/application/person/create/create-person.validator.ts +9 -0
- package/koala-nest/src/application/person/delete/delete-person.handler.ts +22 -0
- package/koala-nest/src/application/person/find-person-or-throw.ts +16 -0
- package/koala-nest/src/application/person/jobs/cron/create-person.job.ts +58 -0
- package/koala-nest/src/application/person/jobs/cron/delete-inactive.job.ts +57 -0
- package/koala-nest/src/application/person/jobs/events/person/inactive-person/inactive-person.event.ts +4 -0
- package/koala-nest/src/application/person/jobs/events/person/inactive-person/inactive-person.handler.ts +54 -0
- package/koala-nest/src/application/person/jobs/events/person/person-event.job.ts +19 -0
- package/koala-nest/src/application/person/person.schemas.ts +21 -0
- package/koala-nest/src/application/person/read/read-person.handler.ts +22 -0
- package/koala-nest/src/application/person/read/read-person.response.ts +44 -0
- package/koala-nest/src/application/person/read-many/read-many-person.handler.ts +63 -0
- package/koala-nest/src/application/person/read-many/read-many-person.request.ts +13 -0
- package/koala-nest/src/application/person/read-many/read-many-person.response.ts +30 -0
- package/koala-nest/src/application/person/read-many/read-many-person.validator.ts +15 -0
- package/koala-nest/src/application/person/update/update-person.handler.ts +51 -0
- package/koala-nest/src/application/person/update/update-person.request.ts +41 -0
- package/koala-nest/src/application/person/update/update-person.validator.ts +9 -0
- package/koala-nest/src/core/auth/assert-user-active.ts +18 -0
- package/koala-nest/src/core/auth/auth-profile.enum.ts +7 -0
- package/koala-nest/src/core/auth/auth-routes.ts +5 -0
- package/koala-nest/src/core/auth/auth.constants.ts +6 -0
- package/koala-nest/src/core/auth/jwt-claims.ts +23 -0
- package/koala-nest/src/core/auth/oauth-provider.registry.ts +94 -0
- package/koala-nest/src/core/auth/parse-jwt-expires-in.ts +30 -0
- package/koala-nest/src/core/auth/parse-oauth2-provider-env.ts +56 -0
- package/koala-nest/src/core/auth/resolve-refresh-token.ts +40 -0
- package/koala-nest/src/core/background-services/cron-service/cron-job.handler.base.ts +65 -0
- package/koala-nest/src/core/background-services/event-service/event-class.ts +5 -0
- package/koala-nest/src/core/background-services/event-service/event-handler.base.ts +17 -0
- package/koala-nest/src/core/background-services/event-service/event-job.ts +31 -0
- package/koala-nest/src/core/background-services/event-service/event-queue.ts +107 -0
- package/koala-nest/src/core/base/entity.base.ts +3 -0
- package/koala-nest/src/core/base/object-class.ts +14 -0
- package/koala-nest/src/core/constants/cache.constants.ts +16 -0
- package/koala-nest/src/core/constants/cron.constants.ts +14 -0
- package/koala-nest/src/core/constants/query-params.ts +7 -0
- package/koala-nest/src/core/env.ts +42 -0
- package/koala-nest/src/core/schemas/boolean.schema.ts +24 -0
- package/koala-nest/src/core/schemas/document-number-mask.ts +22 -0
- package/koala-nest/src/core/schemas/document-number.schema.ts +21 -0
- package/koala-nest/src/core/schemas/document-number.utils.ts +15 -0
- package/koala-nest/src/core/schemas/email.schema.ts +13 -0
- package/koala-nest/src/core/schemas/index.ts +6 -0
- package/koala-nest/src/core/schemas/list-query.schema.ts +18 -0
- package/koala-nest/src/core/schemas/native-enum.schema.ts +36 -0
- package/koala-nest/src/core/tools/mapping/auto-map.ts +14 -0
- package/koala-nest/src/core/tools/mapping/auto-mapper.ts +102 -0
- package/koala-nest/src/core/tools/mapping/create-map.ts +11 -0
- package/koala-nest/src/core/tools/mapping/for-member.ts +16 -0
- package/koala-nest/src/core/tools/mapping/index.ts +4 -0
- package/koala-nest/src/core/tools/mapping/mapping-store.ts +121 -0
- package/koala-nest/src/core/types/auth-provider-config-response.type.ts +5 -0
- package/koala-nest/src/core/types/index.ts +5 -0
- package/koala-nest/src/core/utils/build-list-cache-key.ts +19 -0
- package/koala-nest/src/core/utils/cron-expression-to-boolean.ts +65 -0
- package/koala-nest/src/core/utils/env.config.ts +17 -0
- package/koala-nest/src/core/utils/filter-request-params.ts +29 -0
- package/koala-nest/src/core/utils/format-typeorm-error.ts +173 -0
- package/koala-nest/src/core/utils/format-zod-error.ts +201 -0
- package/koala-nest/src/core/utils/hash-password.ts +5 -0
- package/koala-nest/src/core/utils/icomparable.ts +1 -0
- package/koala-nest/src/core/utils/is-provider-registered.ts +40 -0
- package/koala-nest/src/core/utils/name-to-login.ts +25 -0
- package/koala-nest/src/core/utils/person-list-cache.ts +7 -0
- package/koala-nest/src/core/utils/report-error.ts +18 -0
- package/koala-nest/src/core/utils/resolve-api-host.ts +8 -0
- package/koala-nest/src/core/utils/time.constants.ts +5 -0
- package/koala-nest/src/domain/auth/dtos/auth-provider-config.dto.ts +12 -0
- package/koala-nest/src/domain/auth/dtos/oauth-user-info.dto.ts +9 -0
- package/koala-nest/src/domain/auth/services/iauth.service.ts +37 -0
- package/koala-nest/src/domain/common/icache.service.ts +11 -0
- package/koala-nest/src/domain/common/ilogging.service.ts +9 -0
- package/koala-nest/src/domain/common/ired-lock.service.ts +4 -0
- package/koala-nest/src/domain/dtos/logged-user-info.dto.ts +30 -0
- package/koala-nest/src/domain/dtos/pagination.dto.ts +35 -0
- package/koala-nest/src/domain/dtos/person-query.dto.ts +10 -0
- package/koala-nest/src/domain/entities/person/person-address.ts +14 -0
- package/koala-nest/src/domain/entities/person/person-contact.ts +28 -0
- package/koala-nest/src/domain/entities/person/person.ts +44 -0
- package/koala-nest/src/domain/entities/user/enums/user-status.enum.ts +6 -0
- package/koala-nest/src/domain/entities/user/user.ts +49 -0
- package/koala-nest/src/domain/repositories/iperson.repository.ts +10 -0
- package/koala-nest/src/domain/repositories/iuser.repository.ts +8 -0
- package/koala-nest/src/domain/services/ilogged-user-info.service.ts +5 -0
- package/koala-nest/src/host/app.module.ts +27 -0
- package/koala-nest/src/host/controllers/auth/auth.module.ts +38 -0
- package/koala-nest/src/host/controllers/auth/login.controller.ts +41 -0
- package/koala-nest/src/host/controllers/auth/refresh-token.controller.ts +24 -0
- package/koala-nest/src/host/controllers/auth/router.config.ts +9 -0
- package/koala-nest/src/host/controllers/auth/user-info.controller.ts +19 -0
- package/koala-nest/src/host/controllers/common/controller.base.ts +9 -0
- package/koala-nest/src/host/controllers/common/controller.module.ts +10 -0
- package/koala-nest/src/host/controllers/common/router-config.base.ts +14 -0
- package/koala-nest/src/host/controllers/health-check/health-check.controller.ts +29 -0
- package/koala-nest/src/host/controllers/health-check/health-check.module.ts +22 -0
- package/koala-nest/src/host/controllers/oauth2/auth-link.controller.ts +29 -0
- package/koala-nest/src/host/controllers/oauth2/exchange-code.controller.ts +29 -0
- package/koala-nest/src/host/controllers/oauth2/oauth-callback.controller.ts +28 -0
- package/koala-nest/src/host/controllers/oauth2/router.config.ts +9 -0
- package/koala-nest/src/host/controllers/oauth2/scalar-token.controller.ts +16 -0
- package/koala-nest/src/host/controllers/person/create-person.controller.ts +23 -0
- package/koala-nest/src/host/controllers/person/delete-person.controller.ts +23 -0
- package/koala-nest/src/host/controllers/person/person.module.ts +39 -0
- package/koala-nest/src/host/controllers/person/read-many-person.controller.ts +24 -0
- package/koala-nest/src/host/controllers/person/read-person.controller.ts +21 -0
- package/koala-nest/src/host/controllers/person/router.config.ts +9 -0
- package/koala-nest/src/host/controllers/person/update-person.controller.ts +22 -0
- package/koala-nest/src/host/decorators/api-exclude-endpoint-diff-develop.decorator.ts +9 -0
- package/koala-nest/src/host/decorators/api-property-enum.decorator.ts +49 -0
- package/koala-nest/src/host/decorators/api-property-only-develop.decorator.ts +19 -0
- package/koala-nest/src/host/decorators/controller.decorator.ts +10 -0
- package/koala-nest/src/host/decorators/is-public.decorator.ts +82 -0
- package/koala-nest/src/host/decorators/restriction-by-profile.decorator.ts +13 -0
- package/koala-nest/src/host/decorators/scalar-token-endpoint.decorator.ts +14 -0
- package/koala-nest/src/host/filters/errors.filter.ts +99 -0
- package/koala-nest/src/host/jobs/jobs-bootstrap.service.ts +61 -0
- package/koala-nest/src/host/jobs/jobs.module.ts +41 -0
- package/koala-nest/src/host/jobs/jobs.tokens.ts +2 -0
- package/koala-nest/src/host/main.ts +43 -0
- package/koala-nest/src/host/open-api/define-documentation.ts +210 -0
- package/koala-nest/src/host/security/guards/auth.guard.ts +107 -0
- package/koala-nest/src/host/security/guards/profiles.guard.ts +32 -0
- package/koala-nest/src/host/security/security.module.ts +62 -0
- package/koala-nest/src/host/security/strategies/jwt.strategy.ts +61 -0
- package/koala-nest/src/infra/auth/jwt-token.service.ts +48 -0
- package/koala-nest/src/infra/auth/oauth2-auth.service.ts +208 -0
- package/koala-nest/src/infra/common/cache-service.provider.ts +47 -0
- package/koala-nest/src/infra/common/env.service.ts +12 -0
- package/koala-nest/src/infra/common/in-memory-cache.service.ts +71 -0
- package/koala-nest/src/infra/common/logging.service.ts +19 -0
- package/koala-nest/src/infra/common/red-lock.service.ts +44 -0
- package/koala-nest/src/infra/common/redis-cache.service.ts +72 -0
- package/koala-nest/src/infra/database/data-source-factory.ts +23 -0
- package/koala-nest/src/infra/database/database.module.ts +19 -0
- package/koala-nest/src/infra/database/migrations/1781281330533-Init.ts +67 -0
- package/koala-nest/src/infra/database/migrations/generate-migration.ts +26 -0
- package/koala-nest/src/infra/database/migrations/migration-datasource.ts +14 -0
- package/koala-nest/src/infra/infra.module.ts +29 -0
- package/koala-nest/src/infra/repositories/person.repository.ts +49 -0
- package/koala-nest/src/infra/repositories/repository.base.ts +20 -0
- package/koala-nest/src/infra/repositories/repository.module.ts +16 -0
- package/koala-nest/src/infra/repositories/user.repository.ts +38 -0
- package/koala-nest/src/infra/services/database.indicator.service.ts +17 -0
- package/koala-nest/src/infra/services/logged-user-info.service.ts +32 -0
- package/koala-nest/src/infra/services/redis.indicator.service.ts +66 -0
- package/koala-nest/src/test/app-auth-test.module.ts +32 -0
- package/koala-nest/src/test/app-test.module.ts +22 -0
- package/koala-nest/src/test/application/auth-link.handler.spec.ts +22 -0
- package/koala-nest/src/test/application/create-person.handler.spec.ts +39 -0
- package/koala-nest/src/test/application/create-person.job.spec.ts +55 -0
- package/koala-nest/src/test/application/delete-inactive.job.spec.ts +35 -0
- package/koala-nest/src/test/application/delete-person.handler.spec.ts +46 -0
- package/koala-nest/src/test/application/exchange-code.handler.spec.ts +67 -0
- package/koala-nest/src/test/application/inactive-person.handler.spec.ts +48 -0
- package/koala-nest/src/test/application/login.handler.spec.ts +55 -0
- package/koala-nest/src/test/application/read-many-person.handler.spec.ts +64 -0
- package/koala-nest/src/test/application/read-person.handler.spec.ts +44 -0
- package/koala-nest/src/test/application/refresh-token.handler.spec.ts +34 -0
- package/koala-nest/src/test/application/scalar-oauth-token.handler.spec.ts +63 -0
- package/koala-nest/src/test/application/update-person.handler.spec.ts +78 -0
- package/koala-nest/src/test/bun-test-globals.d.ts +1 -0
- package/koala-nest/src/test/core/auth.guard.spec.ts +77 -0
- package/koala-nest/src/test/core/build-list-cache-key.spec.ts +29 -0
- package/koala-nest/src/test/core/cron-expression-to-boolean.spec.ts +22 -0
- package/koala-nest/src/test/core/cron-job.handler.spec.ts +40 -0
- package/koala-nest/src/test/core/env.config.spec.ts +14 -0
- package/koala-nest/src/test/core/env.spec.ts +61 -0
- package/koala-nest/src/test/core/event-queue.spec.ts +53 -0
- package/koala-nest/src/test/core/format-typeorm-error.spec.ts +42 -0
- package/koala-nest/src/test/core/format-zod-error.spec.ts +38 -0
- package/koala-nest/src/test/core/is-provider-registered.spec.ts +35 -0
- package/koala-nest/src/test/core/jwt.strategy.spec.ts +38 -0
- package/koala-nest/src/test/core/mapping.spec.ts +177 -0
- package/koala-nest/src/test/core/oauth-provider.registry.spec.ts +79 -0
- package/koala-nest/src/test/core/profiles.guard.spec.ts +42 -0
- package/koala-nest/src/test/core/resolve-api-host.spec.ts +20 -0
- package/koala-nest/src/test/core/schemas.spec.ts +95 -0
- package/koala-nest/src/test/create-auth-e2e-test-app.ts +22 -0
- package/koala-nest/src/test/create-e2e-test-app.ts +14 -0
- package/koala-nest/src/test/e2e-context.ts +7 -0
- package/koala-nest/src/test/host/controllers/auth/auth.controller.e2e.spec.ts +87 -0
- package/koala-nest/src/test/host/controllers/person/lazy-loading.e2e.spec.ts +181 -0
- package/koala-nest/src/test/host/controllers/person/person.controller.e2e.spec.ts +148 -0
- package/koala-nest/src/test/host/errors.filter.spec.ts +75 -0
- package/koala-nest/src/test/host/is-public-open-api.spec.ts +60 -0
- package/koala-nest/src/test/host/oauth-callback.controller.spec.ts +18 -0
- package/koala-nest/src/test/infra/cache-service.provider.spec.ts +21 -0
- package/koala-nest/src/test/infra/in-memory-cache.service.spec.ts +49 -0
- package/koala-nest/src/test/infra/jwt-token.service.spec.ts +28 -0
- package/koala-nest/src/test/infra/logged-user-info.service.spec.ts +33 -0
- package/koala-nest/src/test/infra/oauth2-auth.service.spec.ts +112 -0
- package/koala-nest/src/test/infra/red-lock.service.spec.ts +43 -0
- package/koala-nest/src/test/infra/redis-cache.service.spec.ts +100 -0
- package/koala-nest/src/test/infra/redis-indicator.service.spec.ts +75 -0
- package/koala-nest/src/test/mockup/person/person.entities.ts +35 -0
- package/koala-nest/src/test/mockup/person/person.requests.ts +23 -0
- package/koala-nest/src/test/mockup/person/person.responses.ts +31 -0
- package/koala-nest/src/test/services/cache.stub.ts +41 -0
- package/koala-nest/src/test/services/fake-logging.service.ts +13 -0
- package/koala-nest/src/test/services/fake-red-lock.service.ts +11 -0
- package/koala-nest/src/test/services/logged-user-info.fake-service.ts +18 -0
- package/koala-nest/src/test/setup-e2e.ts +66 -0
- package/koala-nest/src/test/setup.ts +1 -0
- package/koala-nest/src/test/utils/configure-test-app.ts +24 -0
- package/koala-nest/src/test/utils/create-e2e-database.ts +50 -0
- package/koala-nest/src/test/utils/e2e-database-client.ts +9 -0
- package/koala-nest/src/test/utils/guard-test-context.ts +28 -0
- package/koala-nest/src/test/utils/jwt-test-keys.ts +27 -0
- package/koala-nest/tsconfig.build.json +8 -0
- package/koala-nest/tsconfig.json +36 -0
- package/koala-nest/tsconfig.spec.json +11 -0
- package/package.json +19 -29
- package/core/backgroud-services/cron-service/cron-job.handler.base.d.ts +0 -16
- package/core/backgroud-services/cron-service/cron-job.handler.base.js +0 -49
- package/core/backgroud-services/event-service/event-class.d.ts +0 -5
- package/core/backgroud-services/event-service/event-class.js +0 -11
- package/core/backgroud-services/event-service/event-handler.base.d.ts +0 -8
- package/core/backgroud-services/event-service/event-handler.base.js +0 -14
- package/core/backgroud-services/event-service/event-is-trigger.d.ts +0 -3
- package/core/backgroud-services/event-service/event-is-trigger.js +0 -7
- package/core/backgroud-services/event-service/event-job.d.ts +0 -13
- package/core/backgroud-services/event-service/event-job.js +0 -21
- package/core/backgroud-services/event-service/event-queue.d.ts +0 -18
- package/core/backgroud-services/event-service/event-queue.js +0 -65
- package/core/constants/query-params.d.ts +0 -6
- package/core/constants/query-params.js +0 -8
- package/core/controllers/base.controller.d.ts +0 -4
- package/core/controllers/base.controller.js +0 -6
- package/core/controllers/controller.decorator.d.ts +0 -2
- package/core/controllers/controller.decorator.js +0 -11
- package/core/controllers/created-registre-response.base.d.ts +0 -10
- package/core/controllers/created-registre-response.base.js +0 -35
- package/core/controllers/list-response.base.d.ts +0 -4
- package/core/controllers/list-response.base.js +0 -21
- package/core/controllers/pagination.request.d.ts +0 -10
- package/core/controllers/pagination.request.js +0 -56
- package/core/controllers/router-config.base.d.ts +0 -7
- package/core/controllers/router-config.base.js +0 -18
- package/core/controllers/schemas/boolean.schema.d.ts +0 -2
- package/core/controllers/schemas/boolean.schema.js +0 -12
- package/core/controllers/schemas/document-number.schema.d.ts +0 -1
- package/core/controllers/schemas/document-number.schema.js +0 -26
- package/core/controllers/schemas/email.schema.d.ts +0 -1
- package/core/controllers/schemas/email.schema.js +0 -13
- package/core/controllers/schemas/list-query.schema.d.ts +0 -17
- package/core/controllers/schemas/list-query.schema.js +0 -19
- package/core/controllers/schemas/native-enum.schema.d.ts +0 -7
- package/core/controllers/schemas/native-enum.schema.js +0 -28
- package/core/controllers/schemas/set-mask-document-number.schema.d.ts +0 -1
- package/core/controllers/schemas/set-mask-document-number.schema.js +0 -13
- package/core/database/entity.base.d.ts +0 -27
- package/core/database/entity.base.js +0 -145
- package/core/database/entity.decorator.d.ts +0 -12
- package/core/database/entity.decorator.js +0 -37
- package/core/database/prisma-client-with-custom-transaction.interface.d.ts +0 -8
- package/core/database/prisma-client-with-custom-transaction.interface.js +0 -2
- package/core/database/prisma-resolver.d.ts +0 -2
- package/core/database/prisma-resolver.js +0 -74
- package/core/database/prisma-transactional-client.d.ts +0 -11
- package/core/database/prisma-transactional-client.js +0 -25
- package/core/database/prisma.service.d.ts +0 -24
- package/core/database/prisma.service.js +0 -104
- package/core/database/repository.base.d.ts +0 -83
- package/core/database/repository.base.js +0 -668
- package/core/dtos/pagination.dto.d.ts +0 -9
- package/core/dtos/pagination.dto.js +0 -49
- package/core/errors/bad-request.error.d.ts +0 -5
- package/core/errors/bad-request.error.js +0 -10
- package/core/errors/conflict.error.d.ts +0 -4
- package/core/errors/conflict.error.js +0 -10
- package/core/errors/error.base.d.ts +0 -4
- package/core/errors/error.base.js +0 -11
- package/core/errors/no-content.error.d.ts +0 -5
- package/core/errors/no-content.error.js +0 -10
- package/core/errors/not-allowed.error.d.ts +0 -5
- package/core/errors/not-allowed.error.js +0 -10
- package/core/errors/resource-not-found.error.d.ts +0 -5
- package/core/errors/resource-not-found.error.js +0 -10
- package/core/errors/use-case-error.d.ts +0 -3
- package/core/errors/use-case-error.js +0 -2
- package/core/errors/user-already-exist.error.d.ts +0 -4
- package/core/errors/user-already-exist.error.js +0 -10
- package/core/errors/wrong-credentials.error.d.ts +0 -4
- package/core/errors/wrong-credentials.error.js +0 -10
- package/core/health-check/health-check.controller.d.ts +0 -5
- package/core/health-check/health-check.controller.js +0 -32
- package/core/health-check/health-check.module.d.ts +0 -2
- package/core/health-check/health-check.module.js +0 -19
- package/core/index.d.ts +0 -18
- package/core/index.js +0 -7
- package/core/koala-app.d.ts +0 -64
- package/core/koala-app.js +0 -258
- package/core/koala-global-vars.d.ts +0 -7
- package/core/koala-global-vars.js +0 -9
- package/core/koala-nest-database.module.d.ts +0 -16
- package/core/koala-nest-database.module.js +0 -52
- package/core/koala-nest-http.module.d.ts +0 -13
- package/core/koala-nest-http.module.js +0 -37
- package/core/koala-nest.module.d.ts +0 -17
- package/core/koala-nest.module.js +0 -66
- package/core/mapping/auto-mapping-class-context.d.ts +0 -16
- package/core/mapping/auto-mapping-class-context.js +0 -18
- package/core/mapping/auto-mapping-context.d.ts +0 -11
- package/core/mapping/auto-mapping-context.js +0 -24
- package/core/mapping/auto-mapping-list.d.ts +0 -28
- package/core/mapping/auto-mapping-list.js +0 -99
- package/core/mapping/auto-mapping-profile.d.ts +0 -3
- package/core/mapping/auto-mapping-profile.js +0 -6
- package/core/mapping/auto-mapping.decorator.d.ts +0 -9
- package/core/mapping/auto-mapping.decorator.js +0 -27
- package/core/mapping/auto-mapping.module.d.ts +0 -5
- package/core/mapping/auto-mapping.module.js +0 -29
- package/core/mapping/auto-mapping.service.d.ts +0 -20
- package/core/mapping/auto-mapping.service.js +0 -197
- package/core/mapping/create-map.d.ts +0 -3
- package/core/mapping/create-map.js +0 -7
- package/core/mapping/for-member.d.ts +0 -5
- package/core/mapping/for-member.js +0 -8
- package/core/request-overflow/request-handler.base.d.ts +0 -4
- package/core/request-overflow/request-handler.base.js +0 -6
- package/core/request-overflow/request-result.d.ts +0 -15
- package/core/request-overflow/request-result.js +0 -37
- package/core/request-overflow/request-validator.base.d.ts +0 -7
- package/core/request-overflow/request-validator.base.js +0 -26
- package/core/security/strategies/api-key.strategy.d.ts +0 -16
- package/core/security/strategies/api-key.strategy.js +0 -31
- package/core/utils/assing-object.d.ts +0 -5
- package/core/utils/assing-object.js +0 -6
- package/core/utils/automap-cycle-context.d.ts +0 -6
- package/core/utils/automap-cycle-context.js +0 -33
- package/core/utils/env.config.d.ts +0 -6
- package/core/utils/env.config.js +0 -18
- package/core/utils/filter-request-params.d.ts +0 -13
- package/core/utils/filter-request-params.js +0 -22
- package/core/utils/find-on-list.d.ts +0 -2
- package/core/utils/find-on-list.js +0 -13
- package/core/utils/generate-prisma-include-schema.d.ts +0 -9
- package/core/utils/generate-prisma-include-schema.js +0 -60
- package/core/utils/get-type-by-prop.d.ts +0 -2
- package/core/utils/get-type-by-prop.js +0 -11
- package/core/utils/hydrate-entity-from-cache.d.ts +0 -22
- package/core/utils/hydrate-entity-from-cache.js +0 -76
- package/core/utils/instanciate-class-with-dependencies-injection.d.ts +0 -2
- package/core/utils/instanciate-class-with-dependencies-injection.js +0 -10
- package/core/utils/interfaces/icomparable.d.ts +0 -5
- package/core/utils/interfaces/icomparable.js +0 -6
- package/core/utils/is-plain-object.d.ts +0 -1
- package/core/utils/is-plain-object.js +0 -8
- package/core/utils/list.d.ts +0 -39
- package/core/utils/list.js +0 -167
- package/core/utils/promise-all.d.ts +0 -7
- package/core/utils/promise-all.js +0 -19
- package/core/utils/proxy.d.ts +0 -1
- package/core/utils/proxy.js +0 -27
- package/core/utils/set-mask-document-number.d.ts +0 -1
- package/core/utils/set-mask-document-number.js +0 -13
- package/core/validators/file-validator.d.ts +0 -27
- package/core/validators/file-validator.js +0 -94
- package/decorators/api-exclude-endpoint-diff-develop.decorator.d.ts +0 -1
- package/decorators/api-exclude-endpoint-diff-develop.decorator.js +0 -9
- package/decorators/api-property-enum.decorator.d.ts +0 -8
- package/decorators/api-property-enum.decorator.js +0 -21
- package/decorators/api-property-only-develop.decorator.d.ts +0 -2
- package/decorators/api-property-only-develop.decorator.js +0 -9
- package/decorators/cookies.decorator.d.ts +0 -1
- package/decorators/cookies.decorator.js +0 -8
- package/decorators/is-public.decorator.d.ts +0 -2
- package/decorators/is-public.decorator.js +0 -7
- package/decorators/upload.decorator.d.ts +0 -1
- package/decorators/upload.decorator.js +0 -18
- package/env/env.d.ts +0 -25
- package/env/env.js +0 -14
- package/env/env.module.d.ts +0 -2
- package/env/env.module.js +0 -20
- package/env/env.service.d.ts +0 -7
- package/env/env.service.js +0 -28
- package/filters/domain-errors.filter.d.ts +0 -18
- package/filters/domain-errors.filter.js +0 -92
- package/filters/global-exception.filter.d.ts +0 -8
- package/filters/global-exception.filter.js +0 -68
- package/filters/prisma-validation-exception.filter.d.ts +0 -10
- package/filters/prisma-validation-exception.filter.js +0 -82
- package/filters/zod-errors.filter.d.ts +0 -9
- package/filters/zod-errors.filter.js +0 -60
- package/services/logging/ilogging.service.d.ts +0 -16
- package/services/logging/ilogging.service.js +0 -6
- package/services/logging/logging.service.d.ts +0 -4
- package/services/logging/logging.service.js +0 -20
- package/services/redis/iredis.service.d.ts +0 -6
- package/services/redis/iredis.service.js +0 -6
- package/services/redis/redis.service.d.ts +0 -14
- package/services/redis/redis.service.js +0 -65
- package/services/redlock/ired-lock.service.d.ts +0 -4
- package/services/redlock/ired-lock.service.js +0 -6
- package/services/redlock/red-lock.service.d.ts +0 -9
- package/services/redlock/red-lock.service.js +0 -46
- package/test/koala-app-test-dependencies.d.ts +0 -10
- package/test/koala-app-test-dependencies.js +0 -13
- package/test/koala-app-test.d.ts +0 -22
- package/test/koala-app-test.js +0 -77
- package/test/repositories/in-memory-base.repository.d.ts +0 -17
- package/test/repositories/in-memory-base.repository.js +0 -67
- package/test/services/fake-logging.service.d.ts +0 -4
- package/test/services/fake-logging.service.js +0 -9
- package/test/services/fake-red-lock.service.d.ts +0 -5
- package/test/services/fake-red-lock.service.js +0 -11
- package/test/utils/create-e2e-database.d.ts +0 -7
- package/test/utils/create-e2e-database.js +0 -38
- package/test/utils/e2e-database-client.d.ts +0 -7
- package/test/utils/e2e-database-client.js +0 -12
- package/test/utils/wait-for.d.ts +0 -1
- package/test/utils/wait-for.js +0 -21
- package/tsconfig.lib.tsbuildinfo +0 -1
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { applyDecorators, SetMetadata } from '@nestjs/common';
|
|
2
|
+
import { OpenAPIObject } from '@nestjs/swagger';
|
|
3
|
+
|
|
4
|
+
export const IS_PUBLIC_KEY = 'isPublic';
|
|
5
|
+
|
|
6
|
+
/** Marcador interno lido pelo Swagger e removido em `syncIsPublicRoutesInOpenApi`. */
|
|
7
|
+
export const SWAGGER_PUBLIC_ROUTE = 'public';
|
|
8
|
+
|
|
9
|
+
const OPENAPI_HTTP_METHODS = new Set([
|
|
10
|
+
'get',
|
|
11
|
+
'post',
|
|
12
|
+
'put',
|
|
13
|
+
'patch',
|
|
14
|
+
'delete',
|
|
15
|
+
'options',
|
|
16
|
+
'head',
|
|
17
|
+
'trace',
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Libera a rota na API e na documentação OpenAPI (sem cadeado no Scalar).
|
|
22
|
+
*
|
|
23
|
+
* O Nest não aplica `security: []` via decorator quando há security global;
|
|
24
|
+
* por isso o metadata `swagger/apiSecurity` é sincronizado no documento gerado.
|
|
25
|
+
*/
|
|
26
|
+
export const IsPublic = () =>
|
|
27
|
+
applyDecorators(
|
|
28
|
+
SetMetadata(IS_PUBLIC_KEY, true),
|
|
29
|
+
SetMetadata('swagger/apiSecurity', [SWAGGER_PUBLIC_ROUTE]),
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
export function syncIsPublicRoutesInOpenApi(document: OpenAPIObject) {
|
|
33
|
+
for (const pathItem of Object.values(document.paths ?? {})) {
|
|
34
|
+
for (const [method, operation] of Object.entries(pathItem)) {
|
|
35
|
+
if (!OPENAPI_HTTP_METHODS.has(method) || !operation?.security) {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const isPublicRoute = operation.security.some((requirement) =>
|
|
40
|
+
requirement === SWAGGER_PUBLIC_ROUTE
|
|
41
|
+
? true
|
|
42
|
+
: typeof requirement === 'object' &&
|
|
43
|
+
SWAGGER_PUBLIC_ROUTE in requirement,
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
if (isPublicRoute) {
|
|
47
|
+
operation.security = [];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Aplica os esquemas de auth em cada operação protegida (Scalar exige `operation.security` explícito). */
|
|
54
|
+
export function applyAuthSecurityToProtectedRoutes(
|
|
55
|
+
document: OpenAPIObject,
|
|
56
|
+
schemeNames: string[],
|
|
57
|
+
) {
|
|
58
|
+
if (schemeNames.length === 0) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const securityRequirements = schemeNames.map((name) => ({ [name]: [] }));
|
|
63
|
+
|
|
64
|
+
document.security = securityRequirements;
|
|
65
|
+
|
|
66
|
+
for (const pathItem of Object.values(document.paths ?? {})) {
|
|
67
|
+
for (const [method, operation] of Object.entries(pathItem)) {
|
|
68
|
+
if (!OPENAPI_HTTP_METHODS.has(method) || !operation) {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (
|
|
73
|
+
Array.isArray(operation.security) &&
|
|
74
|
+
operation.security.length === 0
|
|
75
|
+
) {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
operation.security = securityRequirements;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AuthProfile } from '@/core/auth/auth-profile.enum';
|
|
2
|
+
import { SetMetadata } from '@nestjs/common';
|
|
3
|
+
|
|
4
|
+
export const PROFILES_KEY = 'profiles';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Restringe o endpoint aos perfis informados.
|
|
8
|
+
* O valor de `profile` deve existir no payload JWT.
|
|
9
|
+
*
|
|
10
|
+
* @example RestrictionByProfile([AuthProfile.admin])
|
|
11
|
+
*/
|
|
12
|
+
export const RestrictionByProfile = (profiles: AuthProfile[]) =>
|
|
13
|
+
SetMetadata(PROFILES_KEY, profiles);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AuthTokenResponse } from '@/application/auth/common/auth-token.response';
|
|
2
|
+
import { IsPublic } from '@/host/decorators/is-public.decorator';
|
|
3
|
+
import { applyDecorators, HttpCode, HttpStatus, Post } from '@nestjs/common';
|
|
4
|
+
import { ApiExcludeEndpoint, ApiOkResponse } from '@nestjs/swagger';
|
|
5
|
+
|
|
6
|
+
/** Rota interna do Scalar — não aparece na documentação OpenAPI. */
|
|
7
|
+
export const ScalarTokenEndpoint = () =>
|
|
8
|
+
applyDecorators(
|
|
9
|
+
Post('scalar-token'),
|
|
10
|
+
IsPublic(),
|
|
11
|
+
HttpCode(HttpStatus.OK),
|
|
12
|
+
ApiExcludeEndpoint(),
|
|
13
|
+
ApiOkResponse({ type: AuthTokenResponse }),
|
|
14
|
+
);
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { FilterRequestParams } from '@/core/utils/filter-request-params';
|
|
2
|
+
import {
|
|
3
|
+
formatTypeOrmError,
|
|
4
|
+
isTypeOrmError,
|
|
5
|
+
} from '@/core/utils/format-typeorm-error';
|
|
6
|
+
import { formatZodError, ZodFieldError } from '@/core/utils/format-zod-error';
|
|
7
|
+
import { ILoggingService } from '@/domain/common/ilogging.service';
|
|
8
|
+
import { reportErrorToLogging } from '@/core/utils/report-error';
|
|
9
|
+
import {
|
|
10
|
+
ArgumentsHost,
|
|
11
|
+
Catch,
|
|
12
|
+
HttpException,
|
|
13
|
+
HttpStatus,
|
|
14
|
+
Logger,
|
|
15
|
+
} from '@nestjs/common';
|
|
16
|
+
import { BaseExceptionFilter, AbstractHttpAdapter } from '@nestjs/core';
|
|
17
|
+
import { ZodError } from 'zod';
|
|
18
|
+
|
|
19
|
+
interface ErrorResponse {
|
|
20
|
+
statusCode: HttpStatus;
|
|
21
|
+
message: string;
|
|
22
|
+
errors?: ZodFieldError[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
@Catch()
|
|
26
|
+
export class ErrorsFilter extends BaseExceptionFilter {
|
|
27
|
+
private readonly logger = new Logger(ErrorsFilter.name);
|
|
28
|
+
|
|
29
|
+
constructor(
|
|
30
|
+
httpAdapter: AbstractHttpAdapter | undefined,
|
|
31
|
+
private readonly loggingService: ILoggingService,
|
|
32
|
+
) {
|
|
33
|
+
super(httpAdapter);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
private handleZodError(exception: ZodError): ErrorResponse {
|
|
37
|
+
const formatted = formatZodError(exception);
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
statusCode: HttpStatus.BAD_REQUEST,
|
|
41
|
+
message: formatted.message,
|
|
42
|
+
errors: formatted.errors,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private handleTypeOrmError(exception: Error): ErrorResponse {
|
|
47
|
+
const formatted = formatTypeOrmError(exception);
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
statusCode: formatted.statusCode,
|
|
51
|
+
message: formatted.message,
|
|
52
|
+
errors: formatted.errors,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private sendErrorResponse(host: ArgumentsHost, errorResponse: ErrorResponse) {
|
|
57
|
+
return FilterRequestParams.get(host)
|
|
58
|
+
.response.status(errorResponse.statusCode)
|
|
59
|
+
.json(errorResponse);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private async reportError(error: Error, host: ArgumentsHost): Promise<void> {
|
|
63
|
+
const { loggedUserName } = FilterRequestParams.get(host);
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
await reportErrorToLogging(
|
|
67
|
+
this.loggingService,
|
|
68
|
+
error,
|
|
69
|
+
loggedUserName || undefined,
|
|
70
|
+
);
|
|
71
|
+
} catch {
|
|
72
|
+
this.logger.error(error.message, error.stack);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
catch(exception: unknown, host: ArgumentsHost) {
|
|
77
|
+
if (exception instanceof ZodError) {
|
|
78
|
+
return this.sendErrorResponse(host, this.handleZodError(exception));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (exception instanceof Error && isTypeOrmError(exception)) {
|
|
82
|
+
return this.sendErrorResponse(host, this.handleTypeOrmError(exception));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (exception instanceof HttpException) {
|
|
86
|
+
return super.catch(exception, host);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const error =
|
|
90
|
+
exception instanceof Error ? exception : new Error(String(exception));
|
|
91
|
+
|
|
92
|
+
void this.reportError(error, host);
|
|
93
|
+
|
|
94
|
+
return this.sendErrorResponse(host, {
|
|
95
|
+
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
96
|
+
message: 'Erro interno do servidor.',
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Env } from '@/core/env';
|
|
2
|
+
import { delay } from '@koalarx/utils/KlDelay';
|
|
3
|
+
import { Inject, Injectable, Logger, OnModuleInit, Type } from '@nestjs/common';
|
|
4
|
+
import { ConfigService } from '@nestjs/config';
|
|
5
|
+
import { ModuleRef } from '@nestjs/core';
|
|
6
|
+
import { CRON_JOBS, EVENT_JOB_HANDLERS } from './jobs.tokens';
|
|
7
|
+
|
|
8
|
+
type EventJobHandler = {
|
|
9
|
+
setupSubscriptions(): void;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type CronJobHandler = {
|
|
13
|
+
start(): Promise<void>;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
@Injectable()
|
|
17
|
+
export class JobsBootstrapService implements OnModuleInit {
|
|
18
|
+
private readonly logger = new Logger(JobsBootstrapService.name);
|
|
19
|
+
|
|
20
|
+
constructor(
|
|
21
|
+
private readonly moduleRef: ModuleRef,
|
|
22
|
+
private readonly config: ConfigService<Env, true>,
|
|
23
|
+
@Inject(EVENT_JOB_HANDLERS)
|
|
24
|
+
private readonly eventHandlers: Type<EventJobHandler>[],
|
|
25
|
+
@Inject(CRON_JOBS)
|
|
26
|
+
private readonly cronJobs: Type<CronJobHandler>[],
|
|
27
|
+
) {}
|
|
28
|
+
|
|
29
|
+
async onModuleInit() {
|
|
30
|
+
for (const handlerType of this.eventHandlers) {
|
|
31
|
+
const handler = this.moduleRef.get(handlerType, { strict: false });
|
|
32
|
+
handler.setupSubscriptions();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (this.cronJobs.length === 0) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!this.config.get('CRON_JOBS_ENABLED', { infer: true })) {
|
|
40
|
+
this.logger.log(
|
|
41
|
+
'Cron jobs não iniciados (CRON_JOBS_ENABLED=false). Defina true no .env para habilitar.',
|
|
42
|
+
);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
this.logger.log(`Iniciando ${this.cronJobs.length} cron job(s)...`);
|
|
47
|
+
|
|
48
|
+
const bootstrapDelayMs = this.config.get('BOOTSTRAP_DELAY_MS', {
|
|
49
|
+
infer: true,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
if (bootstrapDelayMs > 0) {
|
|
53
|
+
await delay(bootstrapDelayMs);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
for (const jobType of this.cronJobs) {
|
|
57
|
+
const job = this.moduleRef.get(jobType, { strict: false });
|
|
58
|
+
void job.start();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { DynamicModule, ForwardReference, Module, Type } from '@nestjs/common';
|
|
2
|
+
import { JobsBootstrapService } from './jobs-bootstrap.service';
|
|
3
|
+
import { CRON_JOBS, EVENT_JOB_HANDLERS } from './jobs.tokens';
|
|
4
|
+
|
|
5
|
+
type JobsModuleImport =
|
|
6
|
+
| Type<unknown>
|
|
7
|
+
| DynamicModule
|
|
8
|
+
| ForwardReference
|
|
9
|
+
| Promise<DynamicModule>;
|
|
10
|
+
|
|
11
|
+
export type JobsModuleOptions = {
|
|
12
|
+
imports?: JobsModuleImport[];
|
|
13
|
+
eventHandlers?: Type<unknown>[];
|
|
14
|
+
cronJobs?: Type<unknown>[];
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
@Module({})
|
|
18
|
+
export class JobsModule {
|
|
19
|
+
static register(options: JobsModuleOptions = {}): DynamicModule {
|
|
20
|
+
const eventHandlers = options.eventHandlers ?? [];
|
|
21
|
+
const cronJobs = options.cronJobs ?? [];
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
module: JobsModule,
|
|
25
|
+
imports: options.imports ?? [],
|
|
26
|
+
providers: [
|
|
27
|
+
{
|
|
28
|
+
provide: EVENT_JOB_HANDLERS,
|
|
29
|
+
useValue: eventHandlers,
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
provide: CRON_JOBS,
|
|
33
|
+
useValue: cronJobs,
|
|
34
|
+
},
|
|
35
|
+
...eventHandlers,
|
|
36
|
+
...cronJobs,
|
|
37
|
+
JobsBootstrapService,
|
|
38
|
+
],
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import 'dotenv/config';
|
|
2
|
+
|
|
3
|
+
import { NestFactory } from '@nestjs/core';
|
|
4
|
+
import cookieParser from 'cookie-parser';
|
|
5
|
+
import { AppModule } from './app.module';
|
|
6
|
+
import { defineDocumentation } from './open-api/define-documentation';
|
|
7
|
+
import { ErrorsFilter } from './filters/errors.filter';
|
|
8
|
+
import { HttpAdapterHost } from '@nestjs/core';
|
|
9
|
+
import { AuthGuard } from './security/guards/auth.guard';
|
|
10
|
+
import { ProfilesGuard } from './security/guards/profiles.guard';
|
|
11
|
+
import { ILoggingService } from '@/domain/common/ilogging.service';
|
|
12
|
+
|
|
13
|
+
async function bootstrap() {
|
|
14
|
+
const app = await NestFactory.create(AppModule);
|
|
15
|
+
|
|
16
|
+
app.use(cookieParser());
|
|
17
|
+
|
|
18
|
+
app.enableCors({
|
|
19
|
+
credentials: true,
|
|
20
|
+
origin: true,
|
|
21
|
+
optionsSuccessStatus: 200,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
await defineDocumentation(app);
|
|
25
|
+
|
|
26
|
+
const { httpAdapter } = app.get(HttpAdapterHost);
|
|
27
|
+
const loggingService = app.get(ILoggingService);
|
|
28
|
+
app.useGlobalFilters(new ErrorsFilter(httpAdapter, loggingService));
|
|
29
|
+
|
|
30
|
+
app.useGlobalGuards(
|
|
31
|
+
await app.resolve(AuthGuard),
|
|
32
|
+
await app.resolve(ProfilesGuard),
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
await app.listen(process.env.PORT || 3000);
|
|
36
|
+
|
|
37
|
+
console.log(`Server is running on port ${process.env.PORT || 3000}`);
|
|
38
|
+
console.log(
|
|
39
|
+
`Documentation is available at http://localhost:${process.env.PORT || 3000}/doc`,
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
bootstrap();
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { OAuthProviderRegistry } from '@/core/auth/oauth-provider.registry';
|
|
2
|
+
import { EnvConfig } from '@/core/utils/env.config';
|
|
3
|
+
import {
|
|
4
|
+
isProviderRegistered,
|
|
5
|
+
resolveAppProvider,
|
|
6
|
+
} from '@/core/utils/is-provider-registered';
|
|
7
|
+
import { resolveApiHost } from '@/core/utils/resolve-api-host';
|
|
8
|
+
import {
|
|
9
|
+
IJwtTokenService,
|
|
10
|
+
IOAuth2Service,
|
|
11
|
+
} from '@/domain/auth/services/iauth.service';
|
|
12
|
+
import {
|
|
13
|
+
applyAuthSecurityToProtectedRoutes,
|
|
14
|
+
syncIsPublicRoutesInOpenApi,
|
|
15
|
+
} from '@/host/decorators/is-public.decorator';
|
|
16
|
+
import { SecurityModule } from '@/host/security/security.module';
|
|
17
|
+
import { EnvService } from '@/infra/common/env.service';
|
|
18
|
+
import { INestApplication } from '@nestjs/common';
|
|
19
|
+
import { ModulesContainer } from '@nestjs/core';
|
|
20
|
+
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
|
21
|
+
import { apiReference } from '@scalar/nestjs-api-reference';
|
|
22
|
+
import packageJson from '../../../package.json';
|
|
23
|
+
|
|
24
|
+
const DOC_ENDPOINT = '/doc';
|
|
25
|
+
const ACCESS_TOKEN_FIELD = 'accessToken';
|
|
26
|
+
|
|
27
|
+
type DocAuthorization = {
|
|
28
|
+
name: string;
|
|
29
|
+
config: Record<string, unknown>;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
function capitalizeProvider(key: string) {
|
|
33
|
+
return key.charAt(0).toUpperCase() + key.slice(1);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function hasController(app: INestApplication, controllerName: string) {
|
|
37
|
+
const modulesContainer = app.get(ModulesContainer);
|
|
38
|
+
|
|
39
|
+
for (const moduleRef of modulesContainer.values()) {
|
|
40
|
+
for (const wrapper of moduleRef.controllers.values()) {
|
|
41
|
+
if (wrapper.metatype?.name === controllerName) {
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function resolveOAuthProviderRegistry(app: INestApplication) {
|
|
51
|
+
const fromContainer = resolveAppProvider<OAuthProviderRegistry>(
|
|
52
|
+
app,
|
|
53
|
+
OAuthProviderRegistry,
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
if (fromContainer) {
|
|
57
|
+
return fromContainer;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
return app.select(SecurityModule).get(OAuthProviderRegistry, {
|
|
62
|
+
strict: false,
|
|
63
|
+
});
|
|
64
|
+
} catch {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function buildDocAuthorizations(
|
|
70
|
+
app: INestApplication,
|
|
71
|
+
): Promise<DocAuthorization[]> {
|
|
72
|
+
if (!isProviderRegistered(app, IJwtTokenService)) {
|
|
73
|
+
return [];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const env = app.get(EnvService);
|
|
77
|
+
const hostAddress = resolveApiHost(env.get('API_HOST'), env.get('PORT'));
|
|
78
|
+
const isDevelop = EnvConfig.isEnvDevelop;
|
|
79
|
+
const refreshUrl = `${hostAddress}/auth/refresh`;
|
|
80
|
+
const authorizations: DocAuthorization[] = [];
|
|
81
|
+
|
|
82
|
+
if (hasController(app, 'LoginController')) {
|
|
83
|
+
authorizations.push({
|
|
84
|
+
name: 'JWT',
|
|
85
|
+
config: {
|
|
86
|
+
type: 'oauth2',
|
|
87
|
+
flows: {
|
|
88
|
+
password: {
|
|
89
|
+
tokenUrl: `${hostAddress}/auth/login`,
|
|
90
|
+
refreshUrl,
|
|
91
|
+
username: isDevelop ? 'admin@example.com' : '',
|
|
92
|
+
password: isDevelop ? 'admin123' : '',
|
|
93
|
+
'x-tokenName': ACCESS_TOKEN_FIELD,
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (
|
|
101
|
+
isProviderRegistered(app, IOAuth2Service) &&
|
|
102
|
+
hasController(app, 'OAuthExchangeCodeController')
|
|
103
|
+
) {
|
|
104
|
+
const oauth2Service = app.get(IOAuth2Service);
|
|
105
|
+
const providerRegistry = resolveOAuthProviderRegistry(app);
|
|
106
|
+
|
|
107
|
+
if (providerRegistry) {
|
|
108
|
+
for (const providerKey of providerRegistry.listConfiguredProviders()) {
|
|
109
|
+
const flow = await oauth2Service.resolveScalarOAuthFlow(providerKey);
|
|
110
|
+
|
|
111
|
+
authorizations.push({
|
|
112
|
+
name: capitalizeProvider(providerKey),
|
|
113
|
+
config: {
|
|
114
|
+
type: 'oauth2',
|
|
115
|
+
flows: {
|
|
116
|
+
authorizationCode: {
|
|
117
|
+
authorizationUrl: flow.authorizationUrl,
|
|
118
|
+
tokenUrl: `${hostAddress}/oauth2/scalar-token`,
|
|
119
|
+
refreshUrl,
|
|
120
|
+
'x-tokenName': ACCESS_TOKEN_FIELD,
|
|
121
|
+
'x-usePkce': 'no',
|
|
122
|
+
'x-scalar-redirect-uri': flow.redirectUri,
|
|
123
|
+
'x-scalar-security-body': {
|
|
124
|
+
audience: 'code',
|
|
125
|
+
provider: providerKey,
|
|
126
|
+
},
|
|
127
|
+
...(isDevelop
|
|
128
|
+
? {
|
|
129
|
+
'x-scalar-client-id': flow.clientId,
|
|
130
|
+
clientSecret: flow.clientSecret,
|
|
131
|
+
}
|
|
132
|
+
: {}),
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return authorizations;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export async function defineDocumentation(app: INestApplication) {
|
|
145
|
+
const authorizations = await buildDocAuthorizations(app);
|
|
146
|
+
|
|
147
|
+
const documentBuilder = new DocumentBuilder()
|
|
148
|
+
.setTitle(packageJson.name)
|
|
149
|
+
.setVersion(packageJson.version);
|
|
150
|
+
|
|
151
|
+
for (const authorization of authorizations) {
|
|
152
|
+
documentBuilder.addBearerAuth(
|
|
153
|
+
authorization.config as never,
|
|
154
|
+
authorization.name,
|
|
155
|
+
);
|
|
156
|
+
documentBuilder.addSecurityRequirements(authorization.name);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const document = SwaggerModule.createDocument(app, documentBuilder.build());
|
|
160
|
+
|
|
161
|
+
if (authorizations.length > 0) {
|
|
162
|
+
syncIsPublicRoutesInOpenApi(document);
|
|
163
|
+
applyAuthSecurityToProtectedRoutes(
|
|
164
|
+
document,
|
|
165
|
+
authorizations.map((authorization) => authorization.name),
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
SwaggerModule.setup(DOC_ENDPOINT, app, document, { swaggerUiEnabled: false });
|
|
170
|
+
|
|
171
|
+
app.use(
|
|
172
|
+
DOC_ENDPOINT,
|
|
173
|
+
apiReference({
|
|
174
|
+
darkMode: true,
|
|
175
|
+
spec: { content: document },
|
|
176
|
+
metaData: {
|
|
177
|
+
title: packageJson.name,
|
|
178
|
+
version: packageJson.version,
|
|
179
|
+
},
|
|
180
|
+
hideModels: true,
|
|
181
|
+
hideDownloadButton: true,
|
|
182
|
+
hideClientButton: true,
|
|
183
|
+
hiddenClients: [
|
|
184
|
+
'libcurl',
|
|
185
|
+
'clj_http',
|
|
186
|
+
'restsharp',
|
|
187
|
+
'native',
|
|
188
|
+
'http1.1',
|
|
189
|
+
'asynchttp',
|
|
190
|
+
'nethttp',
|
|
191
|
+
'okhttp',
|
|
192
|
+
'unirest',
|
|
193
|
+
'xhr',
|
|
194
|
+
'request',
|
|
195
|
+
'nsurlsession',
|
|
196
|
+
'cohttp',
|
|
197
|
+
'guzzle',
|
|
198
|
+
'http1',
|
|
199
|
+
'http2',
|
|
200
|
+
'webrequest',
|
|
201
|
+
'restmethod',
|
|
202
|
+
'requests',
|
|
203
|
+
'httr',
|
|
204
|
+
'httpie',
|
|
205
|
+
'wget',
|
|
206
|
+
'undici',
|
|
207
|
+
],
|
|
208
|
+
}),
|
|
209
|
+
);
|
|
210
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { AuthenticatedUser } from '@/core/auth/jwt-claims';
|
|
2
|
+
import { assertUserIsActive } from '@/core/auth/assert-user-active';
|
|
3
|
+
import { applyRefreshTokenForRefreshRoute } from '@/core/auth/resolve-refresh-token';
|
|
4
|
+
import { IUserRepository } from '@/domain/repositories/iuser.repository';
|
|
5
|
+
import { IS_PUBLIC_KEY } from '@/host/decorators/is-public.decorator';
|
|
6
|
+
import {
|
|
7
|
+
ExecutionContext,
|
|
8
|
+
Injectable,
|
|
9
|
+
UnauthorizedException,
|
|
10
|
+
} from '@nestjs/common';
|
|
11
|
+
import { Reflector } from '@nestjs/core';
|
|
12
|
+
import { AuthGuard as NestAuthGuard } from '@nestjs/passport';
|
|
13
|
+
import { isObservable, lastValueFrom, Observable } from 'rxjs';
|
|
14
|
+
|
|
15
|
+
type TokenUser = Pick<AuthenticatedUser, 'sub' | 'refreshToken'>;
|
|
16
|
+
|
|
17
|
+
type AuthRequest = Parameters<typeof applyRefreshTokenForRefreshRoute>[0] & {
|
|
18
|
+
user?: AuthenticatedUser | TokenUser;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
@Injectable()
|
|
22
|
+
export class AuthGuard extends NestAuthGuard('jwt') {
|
|
23
|
+
constructor(
|
|
24
|
+
private readonly reflector: Reflector,
|
|
25
|
+
private readonly userRepository: IUserRepository,
|
|
26
|
+
) {
|
|
27
|
+
super();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
private resolveCanActivate(
|
|
31
|
+
result: boolean | Promise<boolean> | Observable<boolean>,
|
|
32
|
+
): Promise<boolean> {
|
|
33
|
+
if (typeof result === 'boolean') {
|
|
34
|
+
return Promise.resolve(result);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (isObservable(result)) {
|
|
38
|
+
return lastValueFrom(result);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private async loadUserFromDatabase(request: AuthRequest) {
|
|
45
|
+
const tokenUser = request.user;
|
|
46
|
+
|
|
47
|
+
if (!tokenUser?.sub) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if ('name' in tokenUser && tokenUser.name !== undefined) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const user = await this.userRepository.getById(tokenUser.sub);
|
|
56
|
+
|
|
57
|
+
if (!user) {
|
|
58
|
+
throw new UnauthorizedException();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
assertUserIsActive(user);
|
|
62
|
+
|
|
63
|
+
request.user = {
|
|
64
|
+
sub: user.id,
|
|
65
|
+
name: user.name,
|
|
66
|
+
profile: user.profile,
|
|
67
|
+
login: user.login,
|
|
68
|
+
email: user.email,
|
|
69
|
+
refreshToken: tokenUser.refreshToken,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
74
|
+
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
|
|
75
|
+
context.getHandler(),
|
|
76
|
+
context.getClass(),
|
|
77
|
+
]);
|
|
78
|
+
|
|
79
|
+
if (isPublic) {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const request = context.switchToHttp().getRequest<AuthRequest>();
|
|
84
|
+
|
|
85
|
+
applyRefreshTokenForRefreshRoute(request);
|
|
86
|
+
|
|
87
|
+
const activated = await this.resolveCanActivate(super.canActivate(context));
|
|
88
|
+
|
|
89
|
+
if (!activated) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
await this.loadUserFromDatabase(request);
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
handleRequest<TUser = AuthenticatedUser>(
|
|
98
|
+
err: Error | null,
|
|
99
|
+
user: TUser | false,
|
|
100
|
+
): TUser {
|
|
101
|
+
if (err || !user) {
|
|
102
|
+
throw err ?? new UnauthorizedException();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return user;
|
|
106
|
+
}
|
|
107
|
+
}
|