authkit-api 0.0.0-stage → 1.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/CHANGELOG.md +35 -0
- package/LICENSE +21 -0
- package/README.md +125 -2
- package/bin/cli.js +8 -0
- package/package.json +76 -4
- package/src/constants.js +29 -0
- package/src/generator.js +142 -0
- package/src/index.js +99 -0
- package/src/prompts.js +59 -0
- package/src/utils/fs.js +36 -0
- package/src/utils/logger.js +8 -0
- package/src/utils/packageJson.js +16 -0
- package/src/utils/validateProjectName.js +19 -0
- package/templates/base/Dockerfile +22 -0
- package/templates/base/README.md +108 -0
- package/templates/base/_dockerignore +16 -0
- package/templates/base/_env.example +25 -0
- package/templates/base/_gitignore +5 -0
- package/templates/base/_husky/pre-commit +1 -0
- package/templates/base/_prettierignore +5 -0
- package/templates/base/_prettierrc +1 -0
- package/templates/base/package.json +62 -0
- package/templates/base/scripts/docker-entrypoint.sh +9 -0
- package/templates/base/scripts/generate-postman.ts +102 -0
- package/templates/base/scripts/seed-admin.ts +62 -0
- package/templates/base/src/app.ts +46 -0
- package/templates/base/src/config/env.schema.ts +85 -0
- package/templates/base/src/config/env.ts +15 -0
- package/templates/base/src/config/logger.ts +20 -0
- package/templates/base/src/constants/examples.ts +3 -0
- package/templates/base/src/constants/roles.ts +4 -0
- package/templates/base/src/constants/user.rules.ts +16 -0
- package/templates/base/src/docs/openapi.registry.ts +90 -0
- package/templates/base/src/docs/openapi.ts +36 -0
- package/templates/base/src/docs/paths/auth.paths.ts +88 -0
- package/templates/base/src/docs/paths/user.paths.ts +88 -0
- package/templates/base/src/middlewares/authenticate.ts +20 -0
- package/templates/base/src/middlewares/authorize.ts +13 -0
- package/templates/base/src/middlewares/errorHandler.ts +52 -0
- package/templates/base/src/middlewares/notFound.ts +6 -0
- package/templates/base/src/middlewares/rateLimiter.ts +20 -0
- package/templates/base/src/middlewares/validate.ts +39 -0
- package/templates/base/src/modules/auth/auth.controller.ts +45 -0
- package/templates/base/src/modules/auth/auth.cookie.ts +22 -0
- package/templates/base/src/modules/auth/auth.routes.ts +12 -0
- package/templates/base/src/modules/auth/auth.service.ts +81 -0
- package/templates/base/src/modules/auth/auth.validation.ts +44 -0
- package/templates/base/src/modules/user/user.controller.ts +24 -0
- package/templates/base/src/modules/user/user.routes.ts +22 -0
- package/templates/base/src/modules/user/user.service.ts +57 -0
- package/templates/base/src/modules/user/user.validation.ts +27 -0
- package/templates/base/src/routes/docs.routes.ts +16 -0
- package/templates/base/src/routes/health.routes.ts +17 -0
- package/templates/base/src/routes/index.ts +13 -0
- package/templates/base/src/server.ts +51 -0
- package/templates/base/src/types/express.d.ts +14 -0
- package/templates/base/src/utils/ApiError.ts +35 -0
- package/templates/base/src/utils/apiResponse.ts +24 -0
- package/templates/base/src/utils/password.ts +14 -0
- package/templates/base/src/utils/token.ts +60 -0
- package/templates/base/src/utils/zod.ts +5 -0
- package/templates/base/tests/app.test.ts +67 -0
- package/templates/base/tests/auth.test.ts +194 -0
- package/templates/base/tests/authenticate.test.ts +61 -0
- package/templates/base/tests/docs.test.ts +127 -0
- package/templates/base/tests/env.test.ts +96 -0
- package/templates/base/tests/health.test.ts +21 -0
- package/templates/base/tests/helpers/auth.helper.ts +49 -0
- package/templates/base/tests/password.test.ts +18 -0
- package/templates/base/tests/postman.test.ts +32 -0
- package/templates/base/tests/rateLimiter.test.ts +27 -0
- package/templates/base/tests/seedAdmin.test.ts +42 -0
- package/templates/base/tests/token.test.ts +69 -0
- package/templates/base/tests/trustProxy.test.ts +24 -0
- package/templates/base/tests/user.repository.test.ts +211 -0
- package/templates/base/tests/user.test.ts +145 -0
- package/templates/base/tests/validate.test.ts +42 -0
- package/templates/base/tests/validation.test.ts +62 -0
- package/templates/base/vitest.config.ts +16 -0
- package/templates/databases/mongodb/README.part.md +23 -0
- package/templates/databases/mongodb/_github/workflows/ci.yml +29 -0
- package/templates/databases/mongodb/docker-compose.yml +30 -0
- package/templates/databases/mongodb/env.part +2 -0
- package/templates/databases/mongodb/package.part.json +4 -0
- package/templates/databases/mongodb/src/database/connection.ts +46 -0
- package/templates/databases/mongodb/src/database/env.schema.ts +3 -0
- package/templates/databases/mongodb/src/database/id.schema.ts +3 -0
- package/templates/databases/mongodb/src/database/models/refreshToken.model.ts +24 -0
- package/templates/databases/mongodb/src/database/models/user.model.ts +52 -0
- package/templates/databases/mongodb/src/modules/user/user.repository.ts +91 -0
- package/templates/databases/mongodb/tests/globalSetup.ts +13 -0
- package/templates/databases/mongodb/tests/setup.ts +17 -0
- package/templates/databases/postgres/README.part.md +31 -0
- package/templates/databases/postgres/_github/workflows/ci.yml +46 -0
- package/templates/databases/postgres/docker-compose.yml +34 -0
- package/templates/databases/postgres/env.part +2 -0
- package/templates/databases/postgres/package.part.json +15 -0
- package/templates/databases/postgres/prisma/migrations/20261030073256_init/migration.sql +39 -0
- package/templates/databases/postgres/prisma/migrations/migration_lock.toml +3 -0
- package/templates/databases/postgres/prisma/schema.prisma +39 -0
- package/templates/databases/postgres/prisma.config.ts +14 -0
- package/templates/databases/postgres/src/database/connection.ts +72 -0
- package/templates/databases/postgres/src/database/env.schema.ts +3 -0
- package/templates/databases/postgres/src/database/id.schema.ts +3 -0
- package/templates/databases/postgres/src/database/prisma.ts +9 -0
- package/templates/databases/postgres/src/modules/user/user.repository.ts +111 -0
- package/templates/databases/postgres/tests/connection.test.ts +19 -0
- package/templates/databases/postgres/tests/globalSetup.ts +66 -0
- package/templates/databases/postgres/tests/prismaSchema.test.ts +27 -0
- package/templates/databases/postgres/tests/setup.ts +15 -0
- package/templates/languages/js/eslint.config.js +23 -0
- package/templates/languages/ts/eslint.config.js +30 -0
- package/templates/languages/ts/package.part.json +21 -0
- package/templates/languages/ts/tsconfig.json +15 -0
- package/templates-js/base/Dockerfile +22 -0
- package/templates-js/base/README.md +108 -0
- package/templates-js/base/_dockerignore +16 -0
- package/templates-js/base/_env.example +25 -0
- package/templates-js/base/_gitignore +5 -0
- package/templates-js/base/_husky/pre-commit +1 -0
- package/templates-js/base/_prettierignore +5 -0
- package/templates-js/base/_prettierrc +1 -0
- package/templates-js/base/package.json +62 -0
- package/templates-js/base/scripts/docker-entrypoint.sh +9 -0
- package/templates-js/base/scripts/generate-postman.js +70 -0
- package/templates-js/base/scripts/seed-admin.js +61 -0
- package/templates-js/base/src/app.js +46 -0
- package/templates-js/base/src/config/env.js +15 -0
- package/templates-js/base/src/config/env.schema.js +85 -0
- package/templates-js/base/src/config/logger.js +20 -0
- package/templates-js/base/src/constants/examples.js +3 -0
- package/templates-js/base/src/constants/roles.js +2 -0
- package/templates-js/base/src/constants/user.rules.js +16 -0
- package/templates-js/base/src/docs/openapi.js +26 -0
- package/templates-js/base/src/docs/openapi.registry.js +81 -0
- package/templates-js/base/src/docs/paths/auth.paths.js +87 -0
- package/templates-js/base/src/docs/paths/user.paths.js +87 -0
- package/templates-js/base/src/middlewares/authenticate.js +19 -0
- package/templates-js/base/src/middlewares/authorize.js +11 -0
- package/templates-js/base/src/middlewares/errorHandler.js +35 -0
- package/templates-js/base/src/middlewares/notFound.js +5 -0
- package/templates-js/base/src/middlewares/rateLimiter.js +20 -0
- package/templates-js/base/src/middlewares/validate.js +31 -0
- package/templates-js/base/src/modules/auth/auth.controller.js +40 -0
- package/templates-js/base/src/modules/auth/auth.cookie.js +21 -0
- package/templates-js/base/src/modules/auth/auth.routes.js +12 -0
- package/templates-js/base/src/modules/auth/auth.service.js +79 -0
- package/templates-js/base/src/modules/auth/auth.validation.js +41 -0
- package/templates-js/base/src/modules/user/user.controller.js +21 -0
- package/templates-js/base/src/modules/user/user.routes.js +22 -0
- package/templates-js/base/src/modules/user/user.service.js +19 -0
- package/templates-js/base/src/modules/user/user.validation.js +24 -0
- package/templates-js/base/src/routes/docs.routes.js +16 -0
- package/templates-js/base/src/routes/health.routes.js +17 -0
- package/templates-js/base/src/routes/index.js +13 -0
- package/templates-js/base/src/server.js +50 -0
- package/templates-js/base/src/utils/ApiError.js +32 -0
- package/templates-js/base/src/utils/apiResponse.js +8 -0
- package/templates-js/base/src/utils/password.js +14 -0
- package/templates-js/base/src/utils/token.js +46 -0
- package/templates-js/base/src/utils/zod.js +5 -0
- package/templates-js/base/tests/app.test.js +67 -0
- package/templates-js/base/tests/auth.test.js +185 -0
- package/templates-js/base/tests/authenticate.test.js +61 -0
- package/templates-js/base/tests/docs.test.js +115 -0
- package/templates-js/base/tests/env.test.js +94 -0
- package/templates-js/base/tests/health.test.js +21 -0
- package/templates-js/base/tests/helpers/auth.helper.js +41 -0
- package/templates-js/base/tests/password.test.js +18 -0
- package/templates-js/base/tests/postman.test.js +31 -0
- package/templates-js/base/tests/rateLimiter.test.js +27 -0
- package/templates-js/base/tests/seedAdmin.test.js +42 -0
- package/templates-js/base/tests/token.test.js +69 -0
- package/templates-js/base/tests/trustProxy.test.js +24 -0
- package/templates-js/base/tests/user.repository.test.js +210 -0
- package/templates-js/base/tests/user.test.js +144 -0
- package/templates-js/base/tests/validate.test.js +35 -0
- package/templates-js/base/tests/validation.test.js +61 -0
- package/templates-js/base/vitest.config.js +16 -0
- package/templates-js/databases/mongodb/README.part.md +23 -0
- package/templates-js/databases/mongodb/_github/workflows/ci.yml +29 -0
- package/templates-js/databases/mongodb/docker-compose.yml +30 -0
- package/templates-js/databases/mongodb/env.part +2 -0
- package/templates-js/databases/mongodb/package.part.json +4 -0
- package/templates-js/databases/mongodb/src/database/connection.js +39 -0
- package/templates-js/databases/mongodb/src/database/env.schema.js +3 -0
- package/templates-js/databases/mongodb/src/database/id.schema.js +3 -0
- package/templates-js/databases/mongodb/src/database/models/refreshToken.model.js +15 -0
- package/templates-js/databases/mongodb/src/database/models/user.model.js +41 -0
- package/templates-js/databases/mongodb/src/modules/user/user.repository.js +68 -0
- package/templates-js/databases/mongodb/tests/globalSetup.js +13 -0
- package/templates-js/databases/mongodb/tests/setup.js +17 -0
- package/templates-js/databases/postgres/README.part.md +31 -0
- package/templates-js/databases/postgres/_github/workflows/ci.yml +46 -0
- package/templates-js/databases/postgres/docker-compose.yml +34 -0
- package/templates-js/databases/postgres/env.part +2 -0
- package/templates-js/databases/postgres/package.part.json +15 -0
- package/templates-js/databases/postgres/prisma/migrations/20261030073256_init/migration.sql +39 -0
- package/templates-js/databases/postgres/prisma/migrations/migration_lock.toml +3 -0
- package/templates-js/databases/postgres/prisma/schema.prisma +39 -0
- package/templates-js/databases/postgres/prisma.config.js +14 -0
- package/templates-js/databases/postgres/src/database/connection.js +61 -0
- package/templates-js/databases/postgres/src/database/env.schema.js +3 -0
- package/templates-js/databases/postgres/src/database/id.schema.js +3 -0
- package/templates-js/databases/postgres/src/database/prisma.js +9 -0
- package/templates-js/databases/postgres/src/modules/user/user.repository.js +88 -0
- package/templates-js/databases/postgres/tests/connection.test.js +17 -0
- package/templates-js/databases/postgres/tests/globalSetup.js +60 -0
- package/templates-js/databases/postgres/tests/prismaSchema.test.js +27 -0
- package/templates-js/databases/postgres/tests/setup.js +15 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { execSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { parseEnv } from 'node:util';
|
|
4
|
+
import pg from 'pg';
|
|
5
|
+
|
|
6
|
+
const DEFAULT_URL = 'postgresql://postgres:postgres@localhost:5432/app';
|
|
7
|
+
const ENV_FILE = new URL('../.env', import.meta.url);
|
|
8
|
+
const NOT_RUNNING_MESSAGE = `Cannot reach Postgres. Start it first: docker compose up -d --wait db
|
|
9
|
+
(or set DATABASE_URL to a running server).`;
|
|
10
|
+
|
|
11
|
+
// A real environment variable wins, then DATABASE_URL from .env. Only that one
|
|
12
|
+
// variable is read, so the rest of .env cannot change the test setup.
|
|
13
|
+
function databaseUrl() {
|
|
14
|
+
if (process.env.DATABASE_URL) return process.env.DATABASE_URL;
|
|
15
|
+
if (existsSync(ENV_FILE)) {
|
|
16
|
+
const fromFile = parseEnv(readFileSync(ENV_FILE, 'utf8')).DATABASE_URL;
|
|
17
|
+
if (fromFile) return fromFile;
|
|
18
|
+
}
|
|
19
|
+
return DEFAULT_URL;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function exitNotRunning() {
|
|
23
|
+
console.error(NOT_RUNNING_MESSAGE);
|
|
24
|
+
process.exit(1);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Creates `<db>_test` on the same server if needed, points DATABASE_URL at it, and applies migrations.
|
|
28
|
+
export async function setup() {
|
|
29
|
+
const url = new URL(databaseUrl());
|
|
30
|
+
const testDatabase = `${url.pathname.slice(1).replace(/_test$/, '')}_test`;
|
|
31
|
+
|
|
32
|
+
const admin = new URL(url);
|
|
33
|
+
admin.pathname = '/postgres';
|
|
34
|
+
const client = new pg.Client({ connectionString: admin.toString() });
|
|
35
|
+
try {
|
|
36
|
+
await client.connect();
|
|
37
|
+
const { rowCount } = await client.query('SELECT 1 FROM pg_database WHERE datname = $1', [
|
|
38
|
+
testDatabase,
|
|
39
|
+
]);
|
|
40
|
+
if (rowCount === 0) await client.query(`CREATE DATABASE "${testDatabase}"`);
|
|
41
|
+
} catch (err) {
|
|
42
|
+
if (err?.code === 'ECONNREFUSED' || err?.code === 'ENOTFOUND' || err?.code === 'ETIMEDOUT') {
|
|
43
|
+
exitNotRunning();
|
|
44
|
+
}
|
|
45
|
+
if (err instanceof AggregateError) exitNotRunning();
|
|
46
|
+
throw err;
|
|
47
|
+
} finally {
|
|
48
|
+
await client.end().catch(() => {});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
url.pathname = `/${testDatabase}`;
|
|
52
|
+
process.env.DATABASE_URL = url.toString();
|
|
53
|
+
execSync('npx prisma migrate deploy', {
|
|
54
|
+
stdio: 'pipe',
|
|
55
|
+
env: process.env,
|
|
56
|
+
cwd: new URL('..', import.meta.url),
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export async function teardown() {}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
import { ROLE_VALUES } from '../src/constants/roles.js';
|
|
4
|
+
import { USER_RULES } from '../src/constants/user.rules.js';
|
|
5
|
+
|
|
6
|
+
// Prisma's schema cannot import JS, so this guards against the limits drifting apart.
|
|
7
|
+
const schema = readFileSync(new URL('../prisma/schema.prisma', import.meta.url), 'utf8');
|
|
8
|
+
|
|
9
|
+
describe('prisma schema', () => {
|
|
10
|
+
it('uses the name length from user.rules.js', () => {
|
|
11
|
+
expect(schema).toMatch(
|
|
12
|
+
new RegExp(String.raw`\bname\s+String\s+@db\.VarChar\(${USER_RULES.NAME.MAX}\)`),
|
|
13
|
+
);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('uses the email length from user.rules.js and makes it unique', () => {
|
|
17
|
+
expect(schema).toMatch(
|
|
18
|
+
new RegExp(String.raw`\bemail\s+String\s+@unique\s+@db\.VarChar\(${USER_RULES.EMAIL.MAX}\)`),
|
|
19
|
+
);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('has a Role enum equal to ROLE_VALUES', () => {
|
|
23
|
+
const body = schema.match(/enum\s+Role\s*\{([^}]*)\}/)?.[1] ?? '';
|
|
24
|
+
const values = body.split(/\s+/).filter(Boolean);
|
|
25
|
+
expect(values).toEqual([...ROLE_VALUES]);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll } from 'vitest';
|
|
2
|
+
import { connectDatabase, disconnectDatabase } from '../src/database/connection.js';
|
|
3
|
+
import { prisma } from '../src/database/prisma.js';
|
|
4
|
+
|
|
5
|
+
beforeAll(async () => {
|
|
6
|
+
await connectDatabase();
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
afterEach(async () => {
|
|
10
|
+
await prisma.$executeRawUnsafe('TRUNCATE users, refresh_tokens CASCADE');
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
afterAll(async () => {
|
|
14
|
+
await disconnectDatabase();
|
|
15
|
+
});
|