@aurostack/stacks 0.1.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/LICENSE +21 -0
- package/README.md +209 -0
- package/cli/src/extract.mjs +165 -0
- package/cli/src/fsx.mjs +197 -0
- package/cli/src/generate.mjs +235 -0
- package/cli/src/hooks.mjs +55 -0
- package/cli/src/manifest.mjs +163 -0
- package/cli/src/strip.mjs +208 -0
- package/cli/src/tokens.mjs +102 -0
- package/cli/stack.mjs +672 -0
- package/package.json +37 -0
- package/templates/nest-api/files/.bin/db.ts +123 -0
- package/templates/nest-api/files/.bin/dc.ts +74 -0
- package/templates/nest-api/files/.bin/helpers.ts +49 -0
- package/templates/nest-api/files/.bin/secrets.ts +68 -0
- package/templates/nest-api/files/.bin/start.ts +47 -0
- package/templates/nest-api/files/.dockerignore +7 -0
- package/templates/nest-api/files/.env.example +91 -0
- package/templates/nest-api/files/.env.test.example +75 -0
- package/templates/nest-api/files/.forgejo/workflows/build-and-push.yml +41 -0
- package/templates/nest-api/files/.forgejo/workflows/test-e2e.yml +83 -0
- package/templates/nest-api/files/.forgejo/workflows/test.yml +53 -0
- package/templates/nest-api/files/.npmrc.example +1 -0
- package/templates/nest-api/files/.nvmrc +1 -0
- package/templates/nest-api/files/.prettierrc +8 -0
- package/templates/nest-api/files/.swcrc +15 -0
- package/templates/nest-api/files/.watchmanconfig +3 -0
- package/templates/nest-api/files/.yarnrc.yml +1 -0
- package/templates/nest-api/files/Dockerfile +40 -0
- package/templates/nest-api/files/__mocks__/activation-twofa.js +7 -0
- package/templates/nest-api/files/__mocks__/activation.js +7 -0
- package/templates/nest-api/files/__mocks__/forgot-password.js +7 -0
- package/templates/nest-api/files/__mocks__/main-layout.js +7 -0
- package/templates/nest-api/files/__mocks__/password-changed.js +7 -0
- package/templates/nest-api/files/__mocks__/reset-password.js +7 -0
- package/templates/nest-api/files/__mocks__/set-password.js +7 -0
- package/templates/nest-api/files/__mocks__/welcome.js +7 -0
- package/templates/nest-api/files/_gitignore +62 -0
- package/templates/nest-api/files/compose.test.yml +33 -0
- package/templates/nest-api/files/compose.yml +39 -0
- package/templates/nest-api/files/emails/activation-twofa.tsx +58 -0
- package/templates/nest-api/files/emails/activation.tsx +36 -0
- package/templates/nest-api/files/emails/forgot-password.tsx +29 -0
- package/templates/nest-api/files/emails/main-layout.tsx +134 -0
- package/templates/nest-api/files/emails/password-changed.tsx +24 -0
- package/templates/nest-api/files/emails/reset-password.tsx +24 -0
- package/templates/nest-api/files/emails/set-password.tsx +25 -0
- package/templates/nest-api/files/emails/welcome.tsx +39 -0
- package/templates/nest-api/files/eslint.config.mjs +28 -0
- package/templates/nest-api/files/global.d.ts +5 -0
- package/templates/nest-api/files/nest-cli.json +8 -0
- package/templates/nest-api/files/package.json +150 -0
- package/templates/nest-api/files/prisma/schema/auth.prisma +75 -0
- package/templates/nest-api/files/prisma/schema/base.prisma +17 -0
- package/templates/nest-api/files/prisma/schema/enum.prisma +17 -0
- package/templates/nest-api/files/prisma/schema/notification.prisma +14 -0
- package/templates/nest-api/files/prisma.config.ts +13 -0
- package/templates/nest-api/files/public/.gitkeep +0 -0
- package/templates/nest-api/files/seeders/index.ts +27 -0
- package/templates/nest-api/files/seeders/util.ts +88 -0
- package/templates/nest-api/files/src/__tests__/app.controller.spec.ts +29 -0
- package/templates/nest-api/files/src/app.controller.ts +40 -0
- package/templates/nest-api/files/src/app.module.ts +268 -0
- package/templates/nest-api/files/src/app.resolver.ts +32 -0
- package/templates/nest-api/files/src/app.setup.ts +178 -0
- package/templates/nest-api/files/src/common/__tests__/auth.service.spec.ts +31 -0
- package/templates/nest-api/files/src/common/__tests__/cache.service.spec.ts +214 -0
- package/templates/nest-api/files/src/common/__tests__/config.service.spec.ts +56 -0
- package/templates/nest-api/files/src/common/__tests__/generator.service.spec.ts +114 -0
- package/templates/nest-api/files/src/common/__tests__/health.controller.spec.ts +72 -0
- package/templates/nest-api/files/src/common/__tests__/helpers.spec.ts +124 -0
- package/templates/nest-api/files/src/common/__tests__/mail.service.spec.ts +75 -0
- package/templates/nest-api/files/src/common/__tests__/prisma.indicator.spec.ts +39 -0
- package/templates/nest-api/files/src/common/__tests__/prisma.service.spec.ts +15 -0
- package/templates/nest-api/files/src/common/__tests__/records.spec.ts +87 -0
- package/templates/nest-api/files/src/common/__tests__/schemas.spec.ts +142 -0
- package/templates/nest-api/files/src/common/__tests__/throttler-storage.service.spec.ts +80 -0
- package/templates/nest-api/files/src/common/common.module.ts +72 -0
- package/templates/nest-api/files/src/common/constants/index.ts +2 -0
- package/templates/nest-api/files/src/common/constants/options.ts +52 -0
- package/templates/nest-api/files/src/common/constants/pino.ts +24 -0
- package/templates/nest-api/files/src/common/controllers/health.controller.ts +54 -0
- package/templates/nest-api/files/src/common/controllers/index.ts +2 -0
- package/templates/nest-api/files/src/common/controllers/metrics.controller.ts +21 -0
- package/templates/nest-api/files/src/common/decorators/index.ts +2 -0
- package/templates/nest-api/files/src/common/decorators/misc.decorator.ts +49 -0
- package/templates/nest-api/files/src/common/decorators/multi-part.decorator.ts +6 -0
- package/templates/nest-api/files/src/common/dto/filters.dto.ts +25 -0
- package/templates/nest-api/files/src/common/dto/index.ts +1 -0
- package/templates/nest-api/files/src/common/entity/index.ts +2 -0
- package/templates/nest-api/files/src/common/entity/ok.entity.ts +11 -0
- package/templates/nest-api/files/src/common/entity/pagination.entity.ts +24 -0
- package/templates/nest-api/files/src/common/exceptions/index.ts +1 -0
- package/templates/nest-api/files/src/common/exceptions/prisma.exception.ts +57 -0
- package/templates/nest-api/files/src/common/guards/feature-flag.guard.ts +51 -0
- package/templates/nest-api/files/src/common/guards/index.ts +1 -0
- package/templates/nest-api/files/src/common/interceptors/index.ts +1 -0
- package/templates/nest-api/files/src/common/interceptors/metrics.interceptor.ts +37 -0
- package/templates/nest-api/files/src/common/misc/helpers.ts +26 -0
- package/templates/nest-api/files/src/common/misc/index.ts +3 -0
- package/templates/nest-api/files/src/common/misc/origin-builder.ts +56 -0
- package/templates/nest-api/files/src/common/misc/prisma.indicator.ts +24 -0
- package/templates/nest-api/files/src/common/modules/index.ts +3 -0
- package/templates/nest-api/files/src/common/modules/logger.module.ts +104 -0
- package/templates/nest-api/files/src/common/modules/metrics.module.ts +28 -0
- package/templates/nest-api/files/src/common/modules/queue.module.ts +23 -0
- package/templates/nest-api/files/src/common/processors/index.ts +1 -0
- package/templates/nest-api/files/src/common/processors/mail/index.ts +47 -0
- package/templates/nest-api/files/src/common/processors/mail/process.ts +225 -0
- package/templates/nest-api/files/src/common/schemas/fields.ts +76 -0
- package/templates/nest-api/files/src/common/schemas/index.ts +1 -0
- package/templates/nest-api/files/src/common/services/auth-rate-limit.storage.ts +79 -0
- package/templates/nest-api/files/src/common/services/auth.service.ts +16 -0
- package/templates/nest-api/files/src/common/services/cache.service.ts +118 -0
- package/templates/nest-api/files/src/common/services/config.service.ts +388 -0
- package/templates/nest-api/files/src/common/services/feature-flag.service.ts +117 -0
- package/templates/nest-api/files/src/common/services/generator.service.ts +54 -0
- package/templates/nest-api/files/src/common/services/index.ts +10 -0
- package/templates/nest-api/files/src/common/services/logger.service.ts +14 -0
- package/templates/nest-api/files/src/common/services/mail.service.ts +44 -0
- package/templates/nest-api/files/src/common/services/prisma.service.ts +106 -0
- package/templates/nest-api/files/src/common/services/throttler-storage.service.ts +112 -0
- package/templates/nest-api/files/src/common/types/email.ts +55 -0
- package/templates/nest-api/files/src/common/types/index.ts +3 -0
- package/templates/nest-api/files/src/common/types/misc.ts +20 -0
- package/templates/nest-api/files/src/common/types/prisma.ts +18 -0
- package/templates/nest-api/files/src/common/utils/enum.ts +39 -0
- package/templates/nest-api/files/src/common/utils/helpers.ts +139 -0
- package/templates/nest-api/files/src/common/utils/index.ts +5 -0
- package/templates/nest-api/files/src/common/utils/ip.ts +22 -0
- package/templates/nest-api/files/src/common/utils/records.ts +66 -0
- package/templates/nest-api/files/src/common/utils/redis.keys.ts +31 -0
- package/templates/nest-api/files/src/lib/__tests__/access.spec.ts +33 -0
- package/templates/nest-api/files/src/lib/access.ts +114 -0
- package/templates/nest-api/files/src/lib/auth.ts +76 -0
- package/templates/nest-api/files/src/lib/rate-limit.ts +20 -0
- package/templates/nest-api/files/src/main.ts +28 -0
- package/templates/nest-api/files/src/media/__tests__/media.service.spec.ts +60 -0
- package/templates/nest-api/files/src/media/__tests__/s3.service.spec.ts +159 -0
- package/templates/nest-api/files/src/media/entity/file.entity.ts +7 -0
- package/templates/nest-api/files/src/media/entity/index.ts +1 -0
- package/templates/nest-api/files/src/media/media.module.ts +20 -0
- package/templates/nest-api/files/src/media/processors/index.ts +44 -0
- package/templates/nest-api/files/src/media/services/index.ts +2 -0
- package/templates/nest-api/files/src/media/services/media.service.ts +29 -0
- package/templates/nest-api/files/src/media/services/s3.service.ts +168 -0
- package/templates/nest-api/files/src/notifications/controllers/index.ts +1 -0
- package/templates/nest-api/files/src/notifications/controllers/notifications.controller.ts +72 -0
- package/templates/nest-api/files/src/notifications/dto/index.ts +1 -0
- package/templates/nest-api/files/src/notifications/dto/notification.dto.ts +11 -0
- package/templates/nest-api/files/src/notifications/entity/index.ts +1 -0
- package/templates/nest-api/files/src/notifications/entity/notification.entity.ts +37 -0
- package/templates/nest-api/files/src/notifications/notifications.module.ts +11 -0
- package/templates/nest-api/files/src/notifications/services/index.ts +1 -0
- package/templates/nest-api/files/src/notifications/services/notifications.service.ts +98 -0
- package/templates/nest-api/files/src/realtime/__tests__/events.gateway.spec.ts +88 -0
- package/templates/nest-api/files/src/realtime/__tests__/rooms.spec.ts +29 -0
- package/templates/nest-api/files/src/realtime/__tests__/ws-permissions.guard.spec.ts +54 -0
- package/templates/nest-api/files/src/realtime/adapters/redis-io.adapter.ts +57 -0
- package/templates/nest-api/files/src/realtime/constants/index.ts +68 -0
- package/templates/nest-api/files/src/realtime/decorators/index.ts +1 -0
- package/templates/nest-api/files/src/realtime/decorators/ws-permissions.decorator.ts +14 -0
- package/templates/nest-api/files/src/realtime/gateways/events.gateway.ts +187 -0
- package/templates/nest-api/files/src/realtime/guards/index.ts +2 -0
- package/templates/nest-api/files/src/realtime/guards/ws-auth.guard.ts +20 -0
- package/templates/nest-api/files/src/realtime/guards/ws-permissions.guard.ts +35 -0
- package/templates/nest-api/files/src/realtime/misc/session-from-socket.ts +54 -0
- package/templates/nest-api/files/src/realtime/realtime.module.ts +26 -0
- package/templates/nest-api/files/src/realtime/services/presence.service.ts +56 -0
- package/templates/nest-api/files/src/realtime/services/realtime.service.ts +72 -0
- package/templates/nest-api/files/src/users/__tests__/current-user.controller.spec.ts +77 -0
- package/templates/nest-api/files/src/users/__tests__/current-user.service.spec.ts +159 -0
- package/templates/nest-api/files/src/users/__tests__/user.entity.spec.ts +175 -0
- package/templates/nest-api/files/src/users/__tests__/users.controller.spec.ts +89 -0
- package/templates/nest-api/files/src/users/__tests__/users.service.spec.ts +398 -0
- package/templates/nest-api/files/src/users/controllers/current-user.controller.ts +82 -0
- package/templates/nest-api/files/src/users/controllers/index.ts +2 -0
- package/templates/nest-api/files/src/users/controllers/users.controller.ts +40 -0
- package/templates/nest-api/files/src/users/decorators/index.ts +1 -0
- package/templates/nest-api/files/src/users/decorators/permissions.decorator.ts +24 -0
- package/templates/nest-api/files/src/users/dto/avatar.dto.ts +13 -0
- package/templates/nest-api/files/src/users/dto/filters.dto.ts +8 -0
- package/templates/nest-api/files/src/users/dto/index.ts +2 -0
- package/templates/nest-api/files/src/users/entity/index.ts +1 -0
- package/templates/nest-api/files/src/users/entity/user.entity.ts +53 -0
- package/templates/nest-api/files/src/users/services/current-user.service.ts +46 -0
- package/templates/nest-api/files/src/users/services/index.ts +2 -0
- package/templates/nest-api/files/src/users/services/users.service.ts +88 -0
- package/templates/nest-api/files/src/users/users.module.ts +10 -0
- package/templates/nest-api/files/test/app.spec.ts +102 -0
- package/templates/nest-api/files/test/auth.spec.ts +108 -0
- package/templates/nest-api/files/test/current-user.spec.ts +154 -0
- package/templates/nest-api/files/test/factory/app.ts +172 -0
- package/templates/nest-api/files/test/factory/constants.ts +22 -0
- package/templates/nest-api/files/test/factory/dto.ts +27 -0
- package/templates/nest-api/files/test/factory/entity.ts +70 -0
- package/templates/nest-api/files/test/factory/gen.ts +75 -0
- package/templates/nest-api/files/test/factory/util.ts +21 -0
- package/templates/nest-api/files/test/notifications.spec.ts +176 -0
- package/templates/nest-api/files/test/rate-limit.spec.ts +180 -0
- package/templates/nest-api/files/test/setup.ts +1 -0
- package/templates/nest-api/files/test/users.spec.ts +437 -0
- package/templates/nest-api/files/tsconfig.build.json +13 -0
- package/templates/nest-api/files/tsconfig.json +35 -0
- package/templates/nest-api/files/vitest.config.ts +53 -0
- package/templates/nest-api/template.json +452 -0
- package/templates/node-worker/files/.bin/db.ts +123 -0
- package/templates/node-worker/files/.bin/helpers.ts +49 -0
- package/templates/node-worker/files/.bin/secrets.ts +68 -0
- package/templates/node-worker/files/.dockerignore +5 -0
- package/templates/node-worker/files/.env.example +21 -0
- package/templates/node-worker/files/.forgejo/workflows/build-and-push.yml +41 -0
- package/templates/node-worker/files/.nvmrc +1 -0
- package/templates/node-worker/files/.prettierignore +4 -0
- package/templates/node-worker/files/.prettierrc +8 -0
- package/templates/node-worker/files/.yarnrc.yml +5 -0
- package/templates/node-worker/files/Dockerfile +63 -0
- package/templates/node-worker/files/Dockerfile.dev +59 -0
- package/templates/node-worker/files/_gitignore +48 -0
- package/templates/node-worker/files/ecosystem.config.cjs +10 -0
- package/templates/node-worker/files/eslint.config.js +37 -0
- package/templates/node-worker/files/package.json +67 -0
- package/templates/node-worker/files/prisma/_gitignore +1 -0
- package/templates/node-worker/files/prisma/schema/base.prisma +36 -0
- package/templates/node-worker/files/prisma.config.ts +13 -0
- package/templates/node-worker/files/src/config.ts +55 -0
- package/templates/node-worker/files/src/db.ts +118 -0
- package/templates/node-worker/files/src/index.ts +7 -0
- package/templates/node-worker/files/src/logger.ts +45 -0
- package/templates/node-worker/files/src/types.ts +23 -0
- package/templates/node-worker/files/src/utils/browser.ts +16 -0
- package/templates/node-worker/files/src/utils/date.ts +68 -0
- package/templates/node-worker/files/src/utils/helpers.ts +17 -0
- package/templates/node-worker/files/src/utils/number.ts +47 -0
- package/templates/node-worker/files/src/utils/worker.ts +29 -0
- package/templates/node-worker/files/src/workers/constants.ts +6 -0
- package/templates/node-worker/files/src/workers/example/index.ts +27 -0
- package/templates/node-worker/files/src/workers/index.ts +1 -0
- package/templates/node-worker/files/tsconfig.json +30 -0
- package/templates/node-worker/template.json +135 -0
- package/templates/py-worker/files/.dockerignore +176 -0
- package/templates/py-worker/files/.env.example +20 -0
- package/templates/py-worker/files/.forgejo/workflows/build-amd-push.yml +39 -0
- package/templates/py-worker/files/Dockerfile +10 -0
- package/templates/py-worker/files/README.md +3 -0
- package/templates/py-worker/files/_gitignore +176 -0
- package/templates/py-worker/files/config.py +89 -0
- package/templates/py-worker/files/models/decorators.py +42 -0
- package/templates/py-worker/files/models/enums.py +32 -0
- package/templates/py-worker/files/models/mixins.py +61 -0
- package/templates/py-worker/files/requirements.txt +18 -0
- package/templates/py-worker/files/utils/db.py +67 -0
- package/templates/py-worker/files/utils/logger.py +97 -0
- package/templates/py-worker/files/utils/misc.py +45 -0
- package/templates/py-worker/files/utils/worker.py +80 -0
- package/templates/py-worker/files/worker.py +41 -0
- package/templates/py-worker/files/workers/hello/process.py +22 -0
- package/templates/py-worker/template.json +87 -0
- package/templates/react-app/README.md +78 -0
- package/templates/react-app/derive.sh +128 -0
- package/templates/react-app/files/.bin/openapi.js +32 -0
- package/templates/react-app/files/.bin/secrets.js +47 -0
- package/templates/react-app/files/.dockerignore +16 -0
- package/templates/react-app/files/.editorconfig +12 -0
- package/templates/react-app/files/.env.example +34 -0
- package/templates/react-app/files/.forgejo/workflows/build-and-push.yml +55 -0
- package/templates/react-app/files/.forgejo/workflows/ci.yml +39 -0
- package/templates/react-app/files/.husky/pre-commit +1 -0
- package/templates/react-app/files/.nvmrc +1 -0
- package/templates/react-app/files/.prettierignore +6 -0
- package/templates/react-app/files/.prettierrc +9 -0
- package/templates/react-app/files/.yarnrc.yml +2 -0
- package/templates/react-app/files/Dockerfile +52 -0
- package/templates/react-app/files/_gitignore +33 -0
- package/templates/react-app/files/build.sh +53 -0
- package/templates/react-app/files/docker/nginx.conf +134 -0
- package/templates/react-app/files/eslint.config.mjs +75 -0
- package/templates/react-app/files/index.html +62 -0
- package/templates/react-app/files/openapi-ts.config.ts +22 -0
- package/templates/react-app/files/package.json +80 -0
- package/templates/react-app/files/src/app/nav.tsx +32 -0
- package/templates/react-app/files/src/app/providers.tsx +39 -0
- package/templates/react-app/files/src/app/router.tsx +142 -0
- package/templates/react-app/files/src/app/store.ts +20 -0
- package/templates/react-app/files/src/features/auth/components/auth-card.tsx +23 -0
- package/templates/react-app/files/src/features/auth/components/checkbox-field.tsx +32 -0
- package/templates/react-app/files/src/features/auth/components/google-button.tsx +41 -0
- package/templates/react-app/files/src/features/auth/components/or-divider.tsx +9 -0
- package/templates/react-app/files/src/features/auth/components/password-field.tsx +70 -0
- package/templates/react-app/files/src/features/auth/components/submit-button.tsx +16 -0
- package/templates/react-app/files/src/features/auth/lib/auth-actions.ts +68 -0
- package/templates/react-app/files/src/features/auth/lib/password-strength.ts +20 -0
- package/templates/react-app/files/src/features/auth/lib/redirect.ts +6 -0
- package/templates/react-app/files/src/features/auth/lib/schemas.ts +37 -0
- package/templates/react-app/files/src/features/seo/routes.ts +65 -0
- package/templates/react-app/files/src/lib/env.ts +28 -0
- package/templates/react-app/files/src/lib/paths.ts +58 -0
- package/templates/react-app/files/src/main.tsx +19 -0
- package/templates/react-app/files/src/routes/admin/home.tsx +24 -0
- package/templates/react-app/files/src/routes/auth/forgot-password.tsx +93 -0
- package/templates/react-app/files/src/routes/auth/login.tsx +87 -0
- package/templates/react-app/files/src/routes/auth/reset-password.tsx +83 -0
- package/templates/react-app/files/src/routes/auth/signup.tsx +117 -0
- package/templates/react-app/files/src/routes/auth/verified.tsx +31 -0
- package/templates/react-app/files/src/routes/auth/verify-email.tsx +97 -0
- package/templates/react-app/files/src/routes/home.tsx +17 -0
- package/templates/react-app/files/src/routes/legal/privacy.tsx +26 -0
- package/templates/react-app/files/src/routes/legal/terms.tsx +28 -0
- package/templates/react-app/files/src/routes/marketing/home.tsx +18 -0
- package/templates/react-app/files/src/routes/settings.tsx +10 -0
- package/templates/react-app/files/src/shared/api/base-api.ts +24 -0
- package/templates/react-app/files/src/shared/api/base-query.ts +29 -0
- package/templates/react-app/files/src/shared/api/config.ts +21 -0
- package/templates/react-app/files/src/shared/api/index.ts +15 -0
- package/templates/react-app/files/src/shared/api/realtime-events.ts +34 -0
- package/templates/react-app/files/src/shared/api/socket.ts +41 -0
- package/templates/react-app/files/src/shared/api/store.ts +39 -0
- package/templates/react-app/files/src/shared/api/upload-query.ts +115 -0
- package/templates/react-app/files/src/shared/auth/client.ts +27 -0
- package/templates/react-app/files/src/shared/auth/config.ts +28 -0
- package/templates/react-app/files/src/shared/auth/guards.tsx +75 -0
- package/templates/react-app/files/src/shared/auth/hooks.ts +13 -0
- package/templates/react-app/files/src/shared/auth/index.ts +20 -0
- package/templates/react-app/files/src/shared/auth/logout.ts +23 -0
- package/templates/react-app/files/src/shared/auth/provider.tsx +52 -0
- package/templates/react-app/files/src/shared/auth/redirect.ts +65 -0
- package/templates/react-app/files/src/shared/auth/slice.ts +54 -0
- package/templates/react-app/files/src/shared/hooks/index.ts +6 -0
- package/templates/react-app/files/src/shared/layouts/app-shell/app-shell.tsx +225 -0
- package/templates/react-app/files/src/shared/layouts/app-shell/user-menu.tsx +74 -0
- package/templates/react-app/files/src/shared/layouts/auth-layout.tsx +137 -0
- package/templates/react-app/files/src/shared/layouts/error-boundary.tsx +38 -0
- package/templates/react-app/files/src/shared/layouts/index.ts +23 -0
- package/templates/react-app/files/src/shared/layouts/marketing/marketing-layout.tsx +29 -0
- package/templates/react-app/files/src/shared/layouts/marketing/section-heading.tsx +38 -0
- package/templates/react-app/files/src/shared/layouts/marketing/section.tsx +30 -0
- package/templates/react-app/files/src/shared/layouts/states/app-shell-skeleton.tsx +47 -0
- package/templates/react-app/files/src/shared/layouts/states/empty.tsx +22 -0
- package/templates/react-app/files/src/shared/layouts/states/forbidden.tsx +14 -0
- package/templates/react-app/files/src/shared/layouts/states/loading.tsx +11 -0
- package/templates/react-app/files/src/shared/layouts/states/not-found.tsx +21 -0
- package/templates/react-app/files/src/shared/layouts/states/page-skeleton.tsx +39 -0
- package/templates/react-app/files/src/shared/layouts/states/route-fallback.tsx +26 -0
- package/templates/react-app/files/src/shared/layouts/states/state-screen.tsx +22 -0
- package/templates/react-app/files/src/shared/types/README.md +28 -0
- package/templates/react-app/files/src/shared/types/api/index.ts +9 -0
- package/templates/react-app/files/src/shared/types/api/zod.gen.ts +5 -0
- package/templates/react-app/files/src/shared/types/index.ts +4 -0
- package/templates/react-app/files/src/shared/ui/components/brand/logo.tsx +51 -0
- package/templates/react-app/files/src/shared/ui/components/brand/social-icons.tsx +37 -0
- package/templates/react-app/files/src/shared/ui/components/form/form.tsx +120 -0
- package/templates/react-app/files/src/shared/ui/components/ui/alert.tsx +25 -0
- package/templates/react-app/files/src/shared/ui/components/ui/button.tsx +43 -0
- package/templates/react-app/files/src/shared/ui/components/ui/calendar.tsx +78 -0
- package/templates/react-app/files/src/shared/ui/components/ui/card.tsx +44 -0
- package/templates/react-app/files/src/shared/ui/components/ui/checkbox.tsx +16 -0
- package/templates/react-app/files/src/shared/ui/components/ui/date-picker.tsx +76 -0
- package/templates/react-app/files/src/shared/ui/components/ui/input.tsx +22 -0
- package/templates/react-app/files/src/shared/ui/components/ui/label.tsx +19 -0
- package/templates/react-app/files/src/shared/ui/components/ui/modal.tsx +94 -0
- package/templates/react-app/files/src/shared/ui/components/ui/popover.tsx +31 -0
- package/templates/react-app/files/src/shared/ui/components/ui/progress.tsx +33 -0
- package/templates/react-app/files/src/shared/ui/components/ui/search-input.tsx +69 -0
- package/templates/react-app/files/src/shared/ui/components/ui/select.tsx +80 -0
- package/templates/react-app/files/src/shared/ui/components/ui/skeleton.tsx +12 -0
- package/templates/react-app/files/src/shared/ui/components/ui/sonner.tsx +23 -0
- package/templates/react-app/files/src/shared/ui/components/ui/spinner.tsx +8 -0
- package/templates/react-app/files/src/shared/ui/components/ui/textarea.tsx +22 -0
- package/templates/react-app/files/src/shared/ui/index.ts +45 -0
- package/templates/react-app/files/src/shared/ui/lib/utils.ts +7 -0
- package/templates/react-app/files/src/shared/ui/lib/zod-setup.ts +28 -0
- package/templates/react-app/files/src/shared/ui/meta/brand.ts +18 -0
- package/templates/react-app/files/src/shared/ui/meta/meta-context.ts +30 -0
- package/templates/react-app/files/src/shared/ui/meta/meta-provider.tsx +24 -0
- package/templates/react-app/files/src/shared/ui/meta/page-meta.tsx +69 -0
- package/templates/react-app/files/src/shared/ui/meta/use-meta-config.ts +11 -0
- package/templates/react-app/files/src/shared/ui/styles/globals.css +285 -0
- package/templates/react-app/files/src/shared/ui/styles/theme.css +48 -0
- package/templates/react-app/files/src/shared/ui/theme/init-script.ts +23 -0
- package/templates/react-app/files/src/shared/ui/theme/theme-colors.ts +14 -0
- package/templates/react-app/files/src/shared/ui/theme/theme-context.ts +60 -0
- package/templates/react-app/files/src/shared/ui/theme/theme-provider.tsx +101 -0
- package/templates/react-app/files/src/shared/ui/theme/theme-toggle.tsx +40 -0
- package/templates/react-app/files/src/shared/ui/theme/use-theme.ts +11 -0
- package/templates/react-app/files/src/vite-env.d.ts +13 -0
- package/templates/react-app/files/tsconfig.json +26 -0
- package/templates/react-app/files/tsconfig.node.json +25 -0
- package/templates/react-app/files/vite.config.ts +344 -0
- package/templates/react-app/overrides/src/routes/auth/forgot-password.tsx +93 -0
- package/templates/react-app/overrides/src/routes/auth/login.tsx +87 -0
- package/templates/react-app/overrides/src/routes/auth/signup.tsx +117 -0
- package/templates/react-app/overrides/src/routes/auth/verified.tsx +31 -0
- package/templates/react-app/overrides/src/routes/auth/verify-email.tsx +97 -0
- package/templates/react-app/overrides/src/shared/auth/config.ts +28 -0
- package/templates/react-app/overrides/src/shared/auth/guards.tsx +75 -0
- package/templates/react-app/overrides/src/shared/auth/index.ts +20 -0
- package/templates/react-app/overrides/src/shared/auth/redirect.ts +65 -0
- package/templates/react-app/overrides/src/shared/hooks/index.ts +6 -0
- package/templates/react-app/overrides/src/shared/types/README.md +28 -0
- package/templates/react-app/template.json +222 -0
- package/templates/react-monorepo/files/.dockerignore +19 -0
- package/templates/react-monorepo/files/.editorconfig +12 -0
- package/templates/react-monorepo/files/.forgejo/workflows/build-and-push.yml +98 -0
- package/templates/react-monorepo/files/.forgejo/workflows/ci.yml +35 -0
- package/templates/react-monorepo/files/.husky/pre-commit +1 -0
- package/templates/react-monorepo/files/.nvmrc +1 -0
- package/templates/react-monorepo/files/.prettierignore +6 -0
- package/templates/react-monorepo/files/.prettierrc +9 -0
- package/templates/react-monorepo/files/.yarnrc.yml +2 -0
- package/templates/react-monorepo/files/Dockerfile +90 -0
- package/templates/react-monorepo/files/_gitignore +35 -0
- package/templates/react-monorepo/files/apps/admin/.bin/secrets.js +57 -0
- package/templates/react-monorepo/files/apps/admin/.env.example +16 -0
- package/templates/react-monorepo/files/apps/admin/eslint.config.mjs +3 -0
- package/templates/react-monorepo/files/apps/admin/index.html +57 -0
- package/templates/react-monorepo/files/apps/admin/package.json +33 -0
- package/templates/react-monorepo/files/apps/admin/src/App.tsx +3 -0
- package/templates/react-monorepo/files/apps/admin/src/app/nav.tsx +14 -0
- package/templates/react-monorepo/files/apps/admin/src/app/providers.tsx +42 -0
- package/templates/react-monorepo/files/apps/admin/src/app/router.tsx +50 -0
- package/templates/react-monorepo/files/apps/admin/src/app/store.ts +15 -0
- package/templates/react-monorepo/files/apps/admin/src/lib/env.ts +12 -0
- package/templates/react-monorepo/files/apps/admin/src/main.tsx +14 -0
- package/templates/react-monorepo/files/apps/admin/src/routes/home.tsx +17 -0
- package/templates/react-monorepo/files/apps/admin/src/routes/settings.tsx +10 -0
- package/templates/react-monorepo/files/apps/admin/src/vite-env.d.ts +14 -0
- package/templates/react-monorepo/files/apps/admin/tsconfig.json +10 -0
- package/templates/react-monorepo/files/apps/admin/vite.config.ts +13 -0
- package/templates/react-monorepo/files/apps/auth/.bin/secrets.js +57 -0
- package/templates/react-monorepo/files/apps/auth/.env.example +16 -0
- package/templates/react-monorepo/files/apps/auth/eslint.config.mjs +3 -0
- package/templates/react-monorepo/files/apps/auth/index.html +57 -0
- package/templates/react-monorepo/files/apps/auth/package.json +32 -0
- package/templates/react-monorepo/files/apps/auth/src/App.tsx +3 -0
- package/templates/react-monorepo/files/apps/auth/src/app/providers.tsx +42 -0
- package/templates/react-monorepo/files/apps/auth/src/app/router.tsx +19 -0
- package/templates/react-monorepo/files/apps/auth/src/app/store.ts +15 -0
- package/templates/react-monorepo/files/apps/auth/src/components/auth-card.tsx +23 -0
- package/templates/react-monorepo/files/apps/auth/src/components/checkbox-field.tsx +32 -0
- package/templates/react-monorepo/files/apps/auth/src/components/google-button.tsx +41 -0
- package/templates/react-monorepo/files/apps/auth/src/components/or-divider.tsx +9 -0
- package/templates/react-monorepo/files/apps/auth/src/components/password-field.tsx +70 -0
- package/templates/react-monorepo/files/apps/auth/src/components/submit-button.tsx +16 -0
- package/templates/react-monorepo/files/apps/auth/src/lib/auth-actions.ts +68 -0
- package/templates/react-monorepo/files/apps/auth/src/lib/env.ts +8 -0
- package/templates/react-monorepo/files/apps/auth/src/lib/password-strength.ts +20 -0
- package/templates/react-monorepo/files/apps/auth/src/lib/redirect.ts +6 -0
- package/templates/react-monorepo/files/apps/auth/src/lib/schemas.ts +37 -0
- package/templates/react-monorepo/files/apps/auth/src/main.tsx +14 -0
- package/templates/react-monorepo/files/apps/auth/src/routes/forgot-password.tsx +91 -0
- package/templates/react-monorepo/files/apps/auth/src/routes/login.tsx +82 -0
- package/templates/react-monorepo/files/apps/auth/src/routes/reset-password.tsx +83 -0
- package/templates/react-monorepo/files/apps/auth/src/routes/signup.tsx +117 -0
- package/templates/react-monorepo/files/apps/auth/src/routes/verified.tsx +30 -0
- package/templates/react-monorepo/files/apps/auth/src/routes/verify-email.tsx +96 -0
- package/templates/react-monorepo/files/apps/auth/src/vite-env.d.ts +14 -0
- package/templates/react-monorepo/files/apps/auth/tsconfig.json +10 -0
- package/templates/react-monorepo/files/apps/auth/vite.config.ts +13 -0
- package/templates/react-monorepo/files/apps/client/.bin/secrets.js +57 -0
- package/templates/react-monorepo/files/apps/client/.env.example +16 -0
- package/templates/react-monorepo/files/apps/client/eslint.config.mjs +3 -0
- package/templates/react-monorepo/files/apps/client/index.html +57 -0
- package/templates/react-monorepo/files/apps/client/package.json +35 -0
- package/templates/react-monorepo/files/apps/client/src/App.tsx +3 -0
- package/templates/react-monorepo/files/apps/client/src/app/nav.tsx +14 -0
- package/templates/react-monorepo/files/apps/client/src/app/providers.tsx +42 -0
- package/templates/react-monorepo/files/apps/client/src/app/router.tsx +50 -0
- package/templates/react-monorepo/files/apps/client/src/app/store.ts +15 -0
- package/templates/react-monorepo/files/apps/client/src/lib/env.ts +12 -0
- package/templates/react-monorepo/files/apps/client/src/main.tsx +19 -0
- package/templates/react-monorepo/files/apps/client/src/routes/home.tsx +17 -0
- package/templates/react-monorepo/files/apps/client/src/routes/settings.tsx +10 -0
- package/templates/react-monorepo/files/apps/client/src/vite-env.d.ts +15 -0
- package/templates/react-monorepo/files/apps/client/tsconfig.json +10 -0
- package/templates/react-monorepo/files/apps/client/vite.config.ts +120 -0
- package/templates/react-monorepo/files/apps/landing/.bin/secrets.js +57 -0
- package/templates/react-monorepo/files/apps/landing/.env.example +20 -0
- package/templates/react-monorepo/files/apps/landing/eslint.config.mjs +3 -0
- package/templates/react-monorepo/files/apps/landing/index.html +51 -0
- package/templates/react-monorepo/files/apps/landing/package.json +33 -0
- package/templates/react-monorepo/files/apps/landing/src/App.tsx +3 -0
- package/templates/react-monorepo/files/apps/landing/src/app/providers.tsx +42 -0
- package/templates/react-monorepo/files/apps/landing/src/app/router.tsx +21 -0
- package/templates/react-monorepo/files/apps/landing/src/app/store.ts +18 -0
- package/templates/react-monorepo/files/apps/landing/src/features/seo/routes.ts +65 -0
- package/templates/react-monorepo/files/apps/landing/src/lib/env.ts +10 -0
- package/templates/react-monorepo/files/apps/landing/src/main.tsx +14 -0
- package/templates/react-monorepo/files/apps/landing/src/routes/home.tsx +18 -0
- package/templates/react-monorepo/files/apps/landing/src/vite-env.d.ts +15 -0
- package/templates/react-monorepo/files/apps/landing/tsconfig.json +10 -0
- package/templates/react-monorepo/files/apps/landing/vite.config.ts +169 -0
- package/templates/react-monorepo/files/build.sh +61 -0
- package/templates/react-monorepo/files/docker/nginx.conf +136 -0
- package/templates/react-monorepo/files/package.json +37 -0
- package/templates/react-monorepo/files/packages/.gitkeep +0 -0
- package/templates/react-monorepo/files/packages/api/eslint.config.mjs +3 -0
- package/templates/react-monorepo/files/packages/api/package.json +30 -0
- package/templates/react-monorepo/files/packages/api/src/base-api.ts +24 -0
- package/templates/react-monorepo/files/packages/api/src/base-query.ts +29 -0
- package/templates/react-monorepo/files/packages/api/src/config.ts +21 -0
- package/templates/react-monorepo/files/packages/api/src/index.ts +15 -0
- package/templates/react-monorepo/files/packages/api/src/realtime-events.ts +34 -0
- package/templates/react-monorepo/files/packages/api/src/socket.ts +41 -0
- package/templates/react-monorepo/files/packages/api/src/store.ts +39 -0
- package/templates/react-monorepo/files/packages/api/src/upload-query.ts +115 -0
- package/templates/react-monorepo/files/packages/api/tsconfig.json +4 -0
- package/templates/react-monorepo/files/packages/auth/eslint.config.mjs +3 -0
- package/templates/react-monorepo/files/packages/auth/package.json +33 -0
- package/templates/react-monorepo/files/packages/auth/src/client.ts +27 -0
- package/templates/react-monorepo/files/packages/auth/src/config.ts +22 -0
- package/templates/react-monorepo/files/packages/auth/src/guards.tsx +61 -0
- package/templates/react-monorepo/files/packages/auth/src/hooks.ts +13 -0
- package/templates/react-monorepo/files/packages/auth/src/index.ts +20 -0
- package/templates/react-monorepo/files/packages/auth/src/logout.ts +23 -0
- package/templates/react-monorepo/files/packages/auth/src/provider.tsx +52 -0
- package/templates/react-monorepo/files/packages/auth/src/redirect.ts +37 -0
- package/templates/react-monorepo/files/packages/auth/src/slice.ts +54 -0
- package/templates/react-monorepo/files/packages/auth/tsconfig.json +4 -0
- package/templates/react-monorepo/files/packages/config/README.md +47 -0
- package/templates/react-monorepo/files/packages/config/eslint/base.mjs +83 -0
- package/templates/react-monorepo/files/packages/config/package.json +37 -0
- package/templates/react-monorepo/files/packages/config/tailwind/theme.css +48 -0
- package/templates/react-monorepo/files/packages/config/tsconfig/base.json +22 -0
- package/templates/react-monorepo/files/packages/config/tsconfig/node.json +9 -0
- package/templates/react-monorepo/files/packages/config/tsconfig/react.json +9 -0
- package/templates/react-monorepo/files/packages/config/vite/index.d.ts +18 -0
- package/templates/react-monorepo/files/packages/config/vite/index.mjs +129 -0
- package/templates/react-monorepo/files/packages/hooks/eslint.config.mjs +3 -0
- package/templates/react-monorepo/files/packages/hooks/package.json +16 -0
- package/templates/react-monorepo/files/packages/hooks/src/index.ts +3 -0
- package/templates/react-monorepo/files/packages/hooks/tsconfig.json +4 -0
- package/templates/react-monorepo/files/packages/layouts/eslint.config.mjs +3 -0
- package/templates/react-monorepo/files/packages/layouts/package.json +30 -0
- package/templates/react-monorepo/files/packages/layouts/src/app-shell/app-shell.tsx +225 -0
- package/templates/react-monorepo/files/packages/layouts/src/app-shell/user-menu.tsx +74 -0
- package/templates/react-monorepo/files/packages/layouts/src/auth-layout.tsx +137 -0
- package/templates/react-monorepo/files/packages/layouts/src/error-boundary.tsx +38 -0
- package/templates/react-monorepo/files/packages/layouts/src/index.ts +23 -0
- package/templates/react-monorepo/files/packages/layouts/src/marketing/marketing-layout.tsx +29 -0
- package/templates/react-monorepo/files/packages/layouts/src/marketing/section-heading.tsx +38 -0
- package/templates/react-monorepo/files/packages/layouts/src/marketing/section.tsx +30 -0
- package/templates/react-monorepo/files/packages/layouts/src/states/app-shell-skeleton.tsx +47 -0
- package/templates/react-monorepo/files/packages/layouts/src/states/empty.tsx +22 -0
- package/templates/react-monorepo/files/packages/layouts/src/states/forbidden.tsx +14 -0
- package/templates/react-monorepo/files/packages/layouts/src/states/loading.tsx +11 -0
- package/templates/react-monorepo/files/packages/layouts/src/states/not-found.tsx +21 -0
- package/templates/react-monorepo/files/packages/layouts/src/states/page-skeleton.tsx +39 -0
- package/templates/react-monorepo/files/packages/layouts/src/states/route-fallback.tsx +26 -0
- package/templates/react-monorepo/files/packages/layouts/src/states/state-screen.tsx +22 -0
- package/templates/react-monorepo/files/packages/layouts/tsconfig.json +4 -0
- package/templates/react-monorepo/files/packages/types/.bin/openapi.js +32 -0
- package/templates/react-monorepo/files/packages/types/.bin/secrets.js +57 -0
- package/templates/react-monorepo/files/packages/types/README.md +28 -0
- package/templates/react-monorepo/files/packages/types/eslint.config.mjs +3 -0
- package/templates/react-monorepo/files/packages/types/openapi-ts.config.ts +22 -0
- package/templates/react-monorepo/files/packages/types/package.json +29 -0
- package/templates/react-monorepo/files/packages/types/src/index.ts +4 -0
- package/templates/react-monorepo/files/packages/types/src/types/api/index.ts +9 -0
- package/templates/react-monorepo/files/packages/types/src/types/api/zod.gen.ts +5 -0
- package/templates/react-monorepo/files/packages/types/tsconfig.json +7 -0
- package/templates/react-monorepo/files/packages/ui/components.json +21 -0
- package/templates/react-monorepo/files/packages/ui/eslint.config.mjs +13 -0
- package/templates/react-monorepo/files/packages/ui/package.json +44 -0
- package/templates/react-monorepo/files/packages/ui/src/components/brand/logo.tsx +51 -0
- package/templates/react-monorepo/files/packages/ui/src/components/brand/social-icons.tsx +37 -0
- package/templates/react-monorepo/files/packages/ui/src/components/form/form.tsx +120 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/alert.tsx +25 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/button.tsx +43 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/calendar.tsx +78 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/card.tsx +44 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/checkbox.tsx +16 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/date-picker.tsx +76 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/input.tsx +22 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/label.tsx +19 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/modal.tsx +94 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/popover.tsx +31 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/progress.tsx +33 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/search-input.tsx +69 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/select.tsx +80 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/skeleton.tsx +12 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/sonner.tsx +23 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/spinner.tsx +8 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/textarea.tsx +22 -0
- package/templates/react-monorepo/files/packages/ui/src/index.ts +45 -0
- package/templates/react-monorepo/files/packages/ui/src/lib/utils.ts +7 -0
- package/templates/react-monorepo/files/packages/ui/src/lib/zod-setup.ts +28 -0
- package/templates/react-monorepo/files/packages/ui/src/meta/brand.ts +18 -0
- package/templates/react-monorepo/files/packages/ui/src/meta/meta-context.ts +30 -0
- package/templates/react-monorepo/files/packages/ui/src/meta/meta-provider.tsx +24 -0
- package/templates/react-monorepo/files/packages/ui/src/meta/page-meta.tsx +69 -0
- package/templates/react-monorepo/files/packages/ui/src/meta/use-meta-config.ts +11 -0
- package/templates/react-monorepo/files/packages/ui/src/styles/globals.css +285 -0
- package/templates/react-monorepo/files/packages/ui/src/theme/init-script.ts +23 -0
- package/templates/react-monorepo/files/packages/ui/src/theme/theme-colors.ts +14 -0
- package/templates/react-monorepo/files/packages/ui/src/theme/theme-context.ts +60 -0
- package/templates/react-monorepo/files/packages/ui/src/theme/theme-provider.tsx +101 -0
- package/templates/react-monorepo/files/packages/ui/src/theme/theme-toggle.tsx +40 -0
- package/templates/react-monorepo/files/packages/ui/src/theme/use-theme.ts +11 -0
- package/templates/react-monorepo/files/packages/ui/tsconfig.json +9 -0
- package/templates/react-monorepo/files/turbo.json +24 -0
- package/templates/react-monorepo/template.json +230 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { createAccessControl } from 'better-auth/plugins/access';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Permission statements — the vocabulary every authorization check draws from.
|
|
5
|
+
*
|
|
6
|
+
* Add a resource here and it is immediately available to
|
|
7
|
+
* `@UserHasPermission({ permission: { <resource>: ['<action>'] } })` on HTTP
|
|
8
|
+
* routes, to `@WsPermissions(...)` on websocket handlers, and to
|
|
9
|
+
* {@link hasPermission} for offline checks. Grant it to a role below, or it is
|
|
10
|
+
* held by nobody but `superuser`.
|
|
11
|
+
*/
|
|
12
|
+
const statements = {
|
|
13
|
+
user: [
|
|
14
|
+
'create',
|
|
15
|
+
'list',
|
|
16
|
+
'set-role',
|
|
17
|
+
'ban',
|
|
18
|
+
'impersonate',
|
|
19
|
+
'delete',
|
|
20
|
+
'set-password',
|
|
21
|
+
'get',
|
|
22
|
+
'update'
|
|
23
|
+
],
|
|
24
|
+
session: ['list', 'revoke', 'delete']
|
|
25
|
+
} as const;
|
|
26
|
+
|
|
27
|
+
export type PermissionResource = keyof typeof statements;
|
|
28
|
+
export type PermissionAction<Resource extends PermissionResource> =
|
|
29
|
+
(typeof statements)[Resource][number];
|
|
30
|
+
export type Permission = Partial<{
|
|
31
|
+
[Resource in PermissionResource]: PermissionAction<Resource>[];
|
|
32
|
+
}>;
|
|
33
|
+
|
|
34
|
+
export const ac = createAccessControl(statements);
|
|
35
|
+
|
|
36
|
+
// Regular user — may manage their own sessions and nothing else. Ownership of a
|
|
37
|
+
// resource is checked in the service, not granted here, so a user acting on
|
|
38
|
+
// their own content needs no statement at all.
|
|
39
|
+
export const userAc = ac.newRole({
|
|
40
|
+
session: ['list', 'revoke']
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// Admin — user administration, but not role assignment or deletion.
|
|
44
|
+
export const adminAc = ac.newRole({
|
|
45
|
+
user: ['create', 'list', 'get', 'update', 'set-password', 'ban'],
|
|
46
|
+
session: ['list', 'revoke']
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Superuser — everything in `statements`.
|
|
50
|
+
export const superuserAc = ac.newRole(statements);
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The canonical role map. Consumed by the runtime auth instance (`app.module.ts`),
|
|
54
|
+
* the standalone tooling instance (`lib/auth.ts`) and {@link hasPermission}, so a
|
|
55
|
+
* new role only has to be added here.
|
|
56
|
+
*/
|
|
57
|
+
export const roles = {
|
|
58
|
+
user: userAc,
|
|
59
|
+
admin: adminAc,
|
|
60
|
+
superuser: superuserAc
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export type RoleName = keyof typeof roles;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The roles above each have a differently-typed `authorize` (their statements
|
|
67
|
+
* differ), so indexing the map yields a union that TypeScript will not let us
|
|
68
|
+
* call. Structurally they are all the same function.
|
|
69
|
+
*/
|
|
70
|
+
interface AuthorizableRole {
|
|
71
|
+
authorize: (permission: Permission) => { success: boolean };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const roleRegistry = roles as Record<string, AuthorizableRole | undefined>;
|
|
75
|
+
|
|
76
|
+
/** How a role arrives: a comma-separated string, an array, or absent. */
|
|
77
|
+
export type RoleInput = string | string[] | null | undefined;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Better-auth lets a user hold several roles, and hands them over as either a
|
|
81
|
+
* **comma-separated string** or an **array** (`UserSession['user'].role` is
|
|
82
|
+
* typed `string | string[]`). Anything inspecting a role has to normalise both,
|
|
83
|
+
* so that lives here. A missing role reads as the default role, not "no roles".
|
|
84
|
+
*/
|
|
85
|
+
export const rolesOf = (role: RoleInput): string[] =>
|
|
86
|
+
(Array.isArray(role) ? role : (role || 'user').split(','))
|
|
87
|
+
.map((name) => name.trim())
|
|
88
|
+
.filter(Boolean);
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Whether a (possibly multi-) role includes a specific one.
|
|
92
|
+
*
|
|
93
|
+
* Use this instead of `role === 'superuser'`: the equality silently fails for a
|
|
94
|
+
* user holding `"admin,superuser"`, and doesn't compile against the array form.
|
|
95
|
+
*/
|
|
96
|
+
export const hasRole = (role: RoleInput, name: string): boolean =>
|
|
97
|
+
rolesOf(role).includes(name);
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Check a role against a permission, without a database round-trip.
|
|
101
|
+
*
|
|
102
|
+
* Use where better-auth's `@UserHasPermission` decorator cannot reach — notably
|
|
103
|
+
* websocket message handlers, which never pass through the HTTP `AuthGuard`.
|
|
104
|
+
*
|
|
105
|
+
* A multi-role user is granted the union of their roles; a user with no role
|
|
106
|
+
* gets the plugin's `defaultRole`.
|
|
107
|
+
*/
|
|
108
|
+
export const hasPermission = (
|
|
109
|
+
role: RoleInput,
|
|
110
|
+
permission: Permission
|
|
111
|
+
): boolean =>
|
|
112
|
+
rolesOf(role)
|
|
113
|
+
// An unrecognized role grants nothing rather than everything.
|
|
114
|
+
.some((name) => roleRegistry[name]?.authorize(permission).success ?? false);
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import 'dotenv/config';
|
|
2
|
+
import { betterAuth } from 'better-auth';
|
|
3
|
+
import { prismaAdapter } from 'better-auth/adapters/prisma';
|
|
4
|
+
import { admin, openAPI } from 'better-auth/plugins';
|
|
5
|
+
import { PrismaPg } from '@prisma/adapter-pg';
|
|
6
|
+
import { PrismaClient } from '../../prisma/generated/client';
|
|
7
|
+
import { ac, roles } from './access';
|
|
8
|
+
import { AUTH_RATE_LIMIT_RULES } from './rate-limit'; // @feature rate-limit
|
|
9
|
+
import { OriginBuilder } from '../common/misc';
|
|
10
|
+
|
|
11
|
+
const { env } = process;
|
|
12
|
+
|
|
13
|
+
const db = new PrismaClient({
|
|
14
|
+
adapter: new PrismaPg({
|
|
15
|
+
connectionString: env.DATABASE_URL
|
|
16
|
+
})
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
export const auth = betterAuth({
|
|
20
|
+
appName: env.APP_NAME,
|
|
21
|
+
secret: env.BETTER_AUTH_SECRET,
|
|
22
|
+
baseURL: env.BETTER_AUTH_URL,
|
|
23
|
+
basePath: '/auth',
|
|
24
|
+
database: prismaAdapter(db, {
|
|
25
|
+
provider: 'postgresql'
|
|
26
|
+
}),
|
|
27
|
+
emailAndPassword: {
|
|
28
|
+
enabled: true,
|
|
29
|
+
requireEmailVerification: true // @feature mail
|
|
30
|
+
},
|
|
31
|
+
advanced: {
|
|
32
|
+
// Must match the runtime instance: the realtime gateway validates socket
|
|
33
|
+
// sessions through this one, so a different cookie name would reject them.
|
|
34
|
+
cookiePrefix: env.BETTER_AUTH_COOKIE_PREFIX,
|
|
35
|
+
crossSubDomainCookies: {
|
|
36
|
+
enabled: true,
|
|
37
|
+
domain: env.BETTER_AUTH_COOKIE_DOMAIN
|
|
38
|
+
},
|
|
39
|
+
database: {
|
|
40
|
+
joins: true
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
socialProviders: {
|
|
44
|
+
google: {
|
|
45
|
+
clientId: env.OAUTH_GOOGLE_CLIENT_ID || '',
|
|
46
|
+
clientSecret: env.OAUTH_GOOGLE_CLIENT_SECRET || '',
|
|
47
|
+
scope: ['email', 'profile'],
|
|
48
|
+
redirectURI: `${env.BETTER_AUTH_URL}/auth/callback/google`,
|
|
49
|
+
accessType: 'offline',
|
|
50
|
+
prompt: 'select_account consent'
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
// @feature:start rate-limit
|
|
54
|
+
// Mirrors the runtime config in `app.module.ts` so the two instances agree on
|
|
55
|
+
// the rules. This instance is tooling-only (CLI, schema generation) and never
|
|
56
|
+
// serves traffic, so it keeps better-auth's default in-memory storage — the
|
|
57
|
+
// Redis-backed `customStorage` lives with the Nest DI container.
|
|
58
|
+
rateLimit: {
|
|
59
|
+
customRules: AUTH_RATE_LIMIT_RULES
|
|
60
|
+
},
|
|
61
|
+
// @feature:end
|
|
62
|
+
plugins: [
|
|
63
|
+
admin({
|
|
64
|
+
defaultRole: 'user',
|
|
65
|
+
ac,
|
|
66
|
+
roles
|
|
67
|
+
}),
|
|
68
|
+
openAPI() // @feature openapi
|
|
69
|
+
],
|
|
70
|
+
trustedOrigins: [
|
|
71
|
+
env.FRONTEND_HOST!,
|
|
72
|
+
...OriginBuilder.build(env.MISC_CORS_ORIGINS)
|
|
73
|
+
]
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
export type BetterAuth = typeof auth;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** `window` is in seconds; `max` is requests allowed per window. */
|
|
2
|
+
export interface AuthRateLimitRule {
|
|
3
|
+
window: number;
|
|
4
|
+
max: number;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Stricter windows for the better-auth endpoints that actually get attacked.
|
|
9
|
+
* Paths are matched after normalization against `basePath` (`/auth`), so they
|
|
10
|
+
* are written without it.
|
|
11
|
+
*
|
|
12
|
+
* Shared by the runtime instance (`app.module.ts`) and the standalone tooling
|
|
13
|
+
* instance (`lib/auth.ts`) so the two cannot drift.
|
|
14
|
+
*/
|
|
15
|
+
export const AUTH_RATE_LIMIT_RULES: Record<string, AuthRateLimitRule> = {
|
|
16
|
+
'/sign-in/email': { window: 60, max: 5 },
|
|
17
|
+
'/sign-up/email': { window: 3600, max: 5 },
|
|
18
|
+
'/forget-password': { window: 3600, max: 3 },
|
|
19
|
+
'/reset-password': { window: 3600, max: 5 }
|
|
20
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { NestFactory } from '@nestjs/core';
|
|
2
|
+
import { AppModule } from 'app.module';
|
|
3
|
+
import * as setup from './app.setup';
|
|
4
|
+
|
|
5
|
+
(async () => {
|
|
6
|
+
// `bodyParser: false` is intentional — better-auth needs raw bodies on its
|
|
7
|
+
// own routes. JSON parsing is re-applied everywhere else by
|
|
8
|
+
// `enableJsonBodyParser` below. Do not re-enable it globally.
|
|
9
|
+
const app = await NestFactory.create(AppModule, {
|
|
10
|
+
bodyParser: false,
|
|
11
|
+
bufferLogs: true
|
|
12
|
+
});
|
|
13
|
+
setup.usePinoLogger(app);
|
|
14
|
+
setup.enableBasicAuth(app); // @feature openapi, queue, observability
|
|
15
|
+
setup.enableVersioning(app);
|
|
16
|
+
setup.setStatic(app);
|
|
17
|
+
setup.enableJsonBodyParser(app);
|
|
18
|
+
setup.useGlobalPipes(app);
|
|
19
|
+
setup.useGlobalInterceptors(app);
|
|
20
|
+
setup.useGlobalFilters(app);
|
|
21
|
+
await setup.enableOpenAPI(app); // @feature openapi
|
|
22
|
+
setup.enableCors(app);
|
|
23
|
+
setup.enableHelmet(app);
|
|
24
|
+
// Must run before `start()` — the websocket adapter has to be attached
|
|
25
|
+
// before the HTTP server begins listening.
|
|
26
|
+
await setup.enableRealtime(app); // @feature realtime
|
|
27
|
+
await setup.start(app);
|
|
28
|
+
})();
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { TestBed, type Mocked } from '@suites/unit';
|
|
2
|
+
import type { Mock } from 'vitest';
|
|
3
|
+
import { getQueueToken } from '@nestjs/bullmq';
|
|
4
|
+
import type { Queue } from 'bullmq';
|
|
5
|
+
import { Enum } from 'common/utils';
|
|
6
|
+
import { MediaService } from 'media/services';
|
|
7
|
+
import { S3Service } from 'media/services';
|
|
8
|
+
|
|
9
|
+
describe('MediaService', () => {
|
|
10
|
+
let service: MediaService;
|
|
11
|
+
// Only `add` is exercised; Mocked<Queue> is too deep a type for tsc.
|
|
12
|
+
let queue: { add: Mock<Queue['add']> };
|
|
13
|
+
let s3: Mocked<S3Service>;
|
|
14
|
+
|
|
15
|
+
beforeAll(async () => {
|
|
16
|
+
const { unit, unitRef } = await TestBed.solitary(MediaService).compile();
|
|
17
|
+
service = unit;
|
|
18
|
+
queue = unitRef.get(getQueueToken('media'));
|
|
19
|
+
s3 = unitRef.get(S3Service);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
queue.add.mockReset();
|
|
24
|
+
s3.uploadFile.mockReset();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should be defined', () => {
|
|
28
|
+
expect(service).toBeDefined();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('uploadAvatar', () => {
|
|
32
|
+
it('should delegate to S3Service.uploadFile with AVATARS folder', async () => {
|
|
33
|
+
const file = {
|
|
34
|
+
originalname: 'avatar.png',
|
|
35
|
+
buffer: Buffer.from('avatar'),
|
|
36
|
+
mimetype: 'image/png'
|
|
37
|
+
} as any;
|
|
38
|
+
const url = 'http://example.com/avatars/avatar.png';
|
|
39
|
+
s3.uploadFile.mockResolvedValue(url);
|
|
40
|
+
|
|
41
|
+
const result = await service.uploadAvatar(file);
|
|
42
|
+
|
|
43
|
+
expect(s3.uploadFile).toHaveBeenCalledWith(
|
|
44
|
+
file,
|
|
45
|
+
Enum.UploadFolder.AVATARS
|
|
46
|
+
);
|
|
47
|
+
expect(result).toBe(url);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('deleteFile', () => {
|
|
52
|
+
it('should enqueue delete-file job on media queue', async () => {
|
|
53
|
+
const url = 'http://example.com/avatars/avatar.png';
|
|
54
|
+
|
|
55
|
+
await service.deleteFile(url);
|
|
56
|
+
|
|
57
|
+
expect(queue.add).toHaveBeenCalledWith('delete-file', url);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
});
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { Test, TestingModule } from '@nestjs/testing';
|
|
2
|
+
import { vi } from 'vitest';
|
|
3
|
+
import {
|
|
4
|
+
S3Client,
|
|
5
|
+
PutObjectCommand,
|
|
6
|
+
DeleteObjectCommand
|
|
7
|
+
} from '@aws-sdk/client-s3';
|
|
8
|
+
import { BadRequestException } from '@nestjs/common';
|
|
9
|
+
import { Enum } from 'common/utils';
|
|
10
|
+
import {
|
|
11
|
+
CustomConfigService,
|
|
12
|
+
GeneratorService,
|
|
13
|
+
LoggerService
|
|
14
|
+
} from 'common/services';
|
|
15
|
+
import { S3Service } from 'media/services';
|
|
16
|
+
|
|
17
|
+
describe('S3Service', () => {
|
|
18
|
+
let service: S3Service;
|
|
19
|
+
let sendSpy: ReturnType<typeof vi.spyOn>;
|
|
20
|
+
|
|
21
|
+
const mockConfig: Partial<CustomConfigService> = {
|
|
22
|
+
s3: {
|
|
23
|
+
accessKeyId: 'ACCESS_KEY_ID',
|
|
24
|
+
secretAccessKey: 'SECRET_ACCESS_KEY',
|
|
25
|
+
endpoint: 'http://localhost:9000',
|
|
26
|
+
bucket: 'test-bucket',
|
|
27
|
+
region: 'us-east-1'
|
|
28
|
+
}
|
|
29
|
+
} as any;
|
|
30
|
+
|
|
31
|
+
const mockGen: Partial<GeneratorService> = {
|
|
32
|
+
cuid: () => 'test-uuid'
|
|
33
|
+
} as any;
|
|
34
|
+
|
|
35
|
+
/*
|
|
36
|
+
* S3Service gained a LoggerService dependency; without a stub Nest fails to
|
|
37
|
+
* construct it and vitest reports a failed *suite* with all 7 tests skipped —
|
|
38
|
+
* green-looking output hiding zero coverage. Only `error` is called (the
|
|
39
|
+
* upload catch), but the rest are stubbed so a new log line can't resurrect
|
|
40
|
+
* that failure mode.
|
|
41
|
+
*/
|
|
42
|
+
const mockLogger = {
|
|
43
|
+
error: vi.fn(),
|
|
44
|
+
warn: vi.fn(),
|
|
45
|
+
info: vi.fn(),
|
|
46
|
+
debug: vi.fn()
|
|
47
|
+
} as unknown as LoggerService;
|
|
48
|
+
|
|
49
|
+
beforeAll(async () => {
|
|
50
|
+
const module: TestingModule = await Test.createTestingModule({
|
|
51
|
+
providers: [
|
|
52
|
+
S3Service,
|
|
53
|
+
{
|
|
54
|
+
provide: CustomConfigService,
|
|
55
|
+
useValue: mockConfig
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
provide: GeneratorService,
|
|
59
|
+
useValue: mockGen
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
provide: LoggerService,
|
|
63
|
+
useValue: mockLogger
|
|
64
|
+
}
|
|
65
|
+
]
|
|
66
|
+
}).compile();
|
|
67
|
+
|
|
68
|
+
service = module.get(S3Service);
|
|
69
|
+
sendSpy = vi
|
|
70
|
+
.spyOn(S3Client.prototype as any, 'send')
|
|
71
|
+
.mockResolvedValue({} as any);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
beforeEach(() => {
|
|
75
|
+
sendSpy.mockClear();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
afterAll(() => {
|
|
79
|
+
vi.restoreAllMocks();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('should be defined', () => {
|
|
83
|
+
expect(service).toBeDefined();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe('getKey', () => {
|
|
87
|
+
it('should build key using folder, uuid and file extension', () => {
|
|
88
|
+
const file = { originalname: 'photo.jpg' } as any;
|
|
89
|
+
const key = service.getKey(Enum.UploadFolder.AVATARS, file);
|
|
90
|
+
expect(key).toBe('avatars/test-uuid.jpg');
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe('getUrl / getKeyFromUrl / isHostedUrl', () => {
|
|
95
|
+
it('should build and parse hosted URLs correctly', () => {
|
|
96
|
+
const key = 'avatars/test-uuid.jpg';
|
|
97
|
+
const url = service.getUrl(key);
|
|
98
|
+
expect(url).toBe(
|
|
99
|
+
'http://localhost:9000/test-bucket/avatars/test-uuid.jpg'
|
|
100
|
+
);
|
|
101
|
+
expect(service.getKeyFromUrl(url)).toBe(key);
|
|
102
|
+
expect(service.isHostedUrl(url)).toBe(true);
|
|
103
|
+
expect(
|
|
104
|
+
service.isHostedUrl('http://external.com/avatars/test-uuid.jpg')
|
|
105
|
+
).toBe(false);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe('uploadFile', () => {
|
|
110
|
+
it('should upload file and return its URL', async () => {
|
|
111
|
+
const file = {
|
|
112
|
+
originalname: 'photo.jpg',
|
|
113
|
+
buffer: Buffer.from('data'),
|
|
114
|
+
mimetype: 'image/jpeg'
|
|
115
|
+
} as any;
|
|
116
|
+
|
|
117
|
+
const result = await service.uploadFile(file, Enum.UploadFolder.AVATARS);
|
|
118
|
+
|
|
119
|
+
expect(sendSpy).toHaveBeenCalledTimes(1);
|
|
120
|
+
const command = sendSpy.mock.calls[0][0];
|
|
121
|
+
expect(command).toBeInstanceOf(PutObjectCommand);
|
|
122
|
+
const expectedUrl =
|
|
123
|
+
'http://localhost:9000/test-bucket/avatars/test-uuid.jpg';
|
|
124
|
+
expect(result).toBe(expectedUrl);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('should throw BadRequestException when upload fails', async () => {
|
|
128
|
+
sendSpy.mockRejectedValueOnce(new Error('upload failed'));
|
|
129
|
+
|
|
130
|
+
const file = {
|
|
131
|
+
originalname: 'photo.jpg',
|
|
132
|
+
buffer: Buffer.from('data'),
|
|
133
|
+
mimetype: 'image/jpeg'
|
|
134
|
+
} as any;
|
|
135
|
+
|
|
136
|
+
await expect(
|
|
137
|
+
service.uploadFile(file, Enum.UploadFolder.AVATARS)
|
|
138
|
+
).rejects.toThrow(BadRequestException);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
describe('deleteFile', () => {
|
|
143
|
+
it('should delete file without error', async () => {
|
|
144
|
+
await service.deleteFile('avatars/test-uuid.jpg');
|
|
145
|
+
|
|
146
|
+
expect(sendSpy).toHaveBeenCalledTimes(1);
|
|
147
|
+
const command = sendSpy.mock.calls[0][0];
|
|
148
|
+
expect(command).toBeInstanceOf(DeleteObjectCommand);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('should throw BadRequestException when delete fails', async () => {
|
|
152
|
+
sendSpy.mockRejectedValueOnce(new Error('delete failed'));
|
|
153
|
+
|
|
154
|
+
await expect(service.deleteFile('avatars/test-uuid.jpg')).rejects.toThrow(
|
|
155
|
+
BadRequestException
|
|
156
|
+
);
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './file.entity';
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Global, Module } from '@nestjs/common';
|
|
2
|
+
import { MulterModule } from '@nestjs/platform-express';
|
|
3
|
+
import { memoryStorage } from 'multer';
|
|
4
|
+
import { QueueModule } from 'common/modules';
|
|
5
|
+
import { S3Service, MediaService } from './services';
|
|
6
|
+
import { MediaProcessor } from './processors';
|
|
7
|
+
|
|
8
|
+
@Global()
|
|
9
|
+
@Module({
|
|
10
|
+
imports: [
|
|
11
|
+
MulterModule.register({
|
|
12
|
+
storage: memoryStorage() // use memory storage for having the buffer
|
|
13
|
+
}),
|
|
14
|
+
QueueModule.register('media')
|
|
15
|
+
],
|
|
16
|
+
controllers: [],
|
|
17
|
+
providers: [S3Service, MediaService, MediaProcessor],
|
|
18
|
+
exports: [MediaService]
|
|
19
|
+
})
|
|
20
|
+
export class MediaModule {}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Processor, WorkerHost, OnWorkerEvent } from '@nestjs/bullmq';
|
|
2
|
+
import { Injectable } from '@nestjs/common';
|
|
3
|
+
import { Job } from 'bullmq';
|
|
4
|
+
import { LoggerService } from 'common/services';
|
|
5
|
+
import { S3Service } from '../services';
|
|
6
|
+
|
|
7
|
+
@Injectable()
|
|
8
|
+
@Processor('media')
|
|
9
|
+
export class MediaProcessor extends WorkerHost {
|
|
10
|
+
constructor(
|
|
11
|
+
private readonly s3: S3Service,
|
|
12
|
+
private readonly logger: LoggerService
|
|
13
|
+
) {
|
|
14
|
+
super();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async process(job: Job<string, any, any>): Promise<void> {
|
|
18
|
+
if (job.name === 'delete-file') {
|
|
19
|
+
if (!this.s3.isHostedUrl(job.data)) {
|
|
20
|
+
this.logger.warn(
|
|
21
|
+
{ jobId: job.id, jobName: job.name },
|
|
22
|
+
'Not a hosted URL; skipping delete.'
|
|
23
|
+
);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const key = this.s3.getKeyFromUrl(job.data);
|
|
27
|
+
await this.s3.deleteFile(key);
|
|
28
|
+
await job.updateProgress(100);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
@OnWorkerEvent('completed')
|
|
33
|
+
onCompleted(job: Job<string, any, any>) {
|
|
34
|
+
this.logger.info({ jobId: job.id, jobName: job.name }, 'File deleted.');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
@OnWorkerEvent('failed')
|
|
38
|
+
onFailed(job: Job, err: Error) {
|
|
39
|
+
this.logger.error(
|
|
40
|
+
{ err, jobId: job.id, jobName: job.name },
|
|
41
|
+
'File delete failed.'
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Injectable } from '@nestjs/common';
|
|
2
|
+
import { InjectQueue } from '@nestjs/bullmq';
|
|
3
|
+
import { Queue } from 'bullmq';
|
|
4
|
+
import { Enum } from 'common/utils';
|
|
5
|
+
import { S3Service } from './s3.service';
|
|
6
|
+
|
|
7
|
+
@Injectable()
|
|
8
|
+
export class MediaService {
|
|
9
|
+
constructor(
|
|
10
|
+
@InjectQueue('media') private queue: Queue,
|
|
11
|
+
private s3: S3Service
|
|
12
|
+
) {}
|
|
13
|
+
|
|
14
|
+
async uploadAvatar(file: Express.Multer.File) {
|
|
15
|
+
return await this.s3.uploadFile(file, Enum.UploadFolder.AVATARS);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async uploadFile(file: Express.Multer.File, folder: Enum.UploadFolder) {
|
|
19
|
+
return await this.s3.uploadFile(file, folder);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async getFileBuffer(url: string): Promise<Buffer> {
|
|
23
|
+
return await this.s3.getFileBuffer(this.s3.getKeyFromUrl(url));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async deleteFile(data: string) {
|
|
27
|
+
this.queue.add('delete-file', data);
|
|
28
|
+
}
|
|
29
|
+
}
|