@absterr/nestkit 0.0.0-stage → 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.
Files changed (106) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +62 -2
  3. package/dist/cli.js +439 -0
  4. package/dist/options.js +98 -0
  5. package/dist/package-managers.js +23 -0
  6. package/dist/scaffold.js +158 -0
  7. package/dist/settings.js +52 -0
  8. package/dist/versions.js +33 -0
  9. package/package.json +47 -4
  10. package/templates/agent-files/11/AGENTS.md +24 -0
  11. package/templates/agent-files/12/AGENTS.md +39 -0
  12. package/templates/agent-files/common/CLAUDE.md +1 -0
  13. package/templates/base/README.md +70 -0
  14. package/templates/base/gitattributes +1 -0
  15. package/templates/base/gitignore +8 -0
  16. package/templates/base/nest-cli.json +11 -0
  17. package/templates/base/package.json +32 -0
  18. package/templates/base/src/app.controller.ts +12 -0
  19. package/templates/base/src/app.module.ts +10 -0
  20. package/templates/base/src/app.service.ts +8 -0
  21. package/templates/base/src/main.ts +13 -0
  22. package/templates/cjs-module/package.json +7 -0
  23. package/templates/code-quality/biome/biome.json +47 -0
  24. package/templates/code-quality/biome/package.json +14 -0
  25. package/templates/code-quality/eslint/eslint.config.mjs +35 -0
  26. package/templates/code-quality/eslint/package.json +14 -0
  27. package/templates/code-quality/oxlint/.oxlintrc.json +8 -0
  28. package/templates/code-quality/oxlint/package.json +10 -0
  29. package/templates/code-quality/prettier/.prettierignore +10 -0
  30. package/templates/code-quality/prettier/.prettierrc +7 -0
  31. package/templates/code-quality/prettier/package.json +9 -0
  32. package/templates/e2e-tests/cypress/gitignore +2 -0
  33. package/templates/e2e-tests/cypress/package.json +12 -0
  34. package/templates/e2e-tests/cypress/test/app.cy.ts +10 -0
  35. package/templates/e2e-tests/cypress/test/cypress.config.mjs +9 -0
  36. package/templates/e2e-tests/cypress/test/tsconfig.json +8 -0
  37. package/templates/e2e-tests/e2e-scripts/package.json +5 -0
  38. package/templates/e2e-tests/e2e-scripts/scripts/test-e2e.mjs +48 -0
  39. package/templates/e2e-tests/playwright/gitignore +2 -0
  40. package/templates/e2e-tests/playwright/package.json +8 -0
  41. package/templates/e2e-tests/playwright/test/app.e2e-spec.ts +8 -0
  42. package/templates/e2e-tests/playwright/test/playwright.config.mjs +9 -0
  43. package/templates/e2e-tests/runner-configs/jest/test/jest-e2e.json +9 -0
  44. package/templates/e2e-tests/runner-configs/mocha/cjs-mocha/test/.mocharc.e2e.json +5 -0
  45. package/templates/e2e-tests/runner-configs/mocha/esm-mocha/test/.mocharc.e2e.json +5 -0
  46. package/templates/e2e-tests/runner-configs/vitest/test/vitest.config.e2e.mjs +15 -0
  47. package/templates/e2e-tests/selenium/jest-selenium/package.json +5 -0
  48. package/templates/e2e-tests/selenium/jest-selenium/test/app.e2e-spec.ts +25 -0
  49. package/templates/e2e-tests/selenium/mocha-selenium/package.json +5 -0
  50. package/templates/e2e-tests/selenium/mocha-selenium/test/app.e2e-spec.ts +28 -0
  51. package/templates/e2e-tests/selenium/package.json +6 -0
  52. package/templates/e2e-tests/selenium/vitest-selenium/package.json +5 -0
  53. package/templates/e2e-tests/selenium/vitest-selenium/test/app.e2e-spec.ts +26 -0
  54. package/templates/e2e-tests/supertest/jest-supertest/package.json +5 -0
  55. package/templates/e2e-tests/supertest/jest-supertest/test/app.e2e-spec.ts +29 -0
  56. package/templates/e2e-tests/supertest/mocha-supertest/package.json +5 -0
  57. package/templates/e2e-tests/supertest/mocha-supertest/test/app.e2e-spec.ts +30 -0
  58. package/templates/e2e-tests/supertest/package.json +6 -0
  59. package/templates/e2e-tests/supertest/vitest-supertest/package.json +5 -0
  60. package/templates/e2e-tests/supertest/vitest-supertest/test/app.e2e-spec.ts +30 -0
  61. package/templates/environment/arktype/package.json +5 -0
  62. package/templates/environment/arktype/src/config/env/env.schema.ts +12 -0
  63. package/templates/environment/arktype/src/config/env/validate.ts +12 -0
  64. package/templates/environment/class-validator/package.json +6 -0
  65. package/templates/environment/class-validator/src/config/env/env.schema.ts +15 -0
  66. package/templates/environment/class-validator/src/config/env/validate.ts +15 -0
  67. package/templates/environment/common/package.json +5 -0
  68. package/templates/environment/common/src/app.module.ts +11 -0
  69. package/templates/environment/common/src/config/env/env.module.ts +17 -0
  70. package/templates/environment/common/src/config/env/env.provider.ts +17 -0
  71. package/templates/environment/common/src/main.ts +15 -0
  72. package/templates/environment/joi/package.json +5 -0
  73. package/templates/environment/joi/src/config/env/env.schema.ts +16 -0
  74. package/templates/environment/joi/src/config/env/validate.ts +16 -0
  75. package/templates/environment/valibot/package.json +5 -0
  76. package/templates/environment/valibot/src/config/env/env.schema.ts +20 -0
  77. package/templates/environment/valibot/src/config/env/validate.ts +16 -0
  78. package/templates/environment/vanilla/src/config/env/env.schema.ts +26 -0
  79. package/templates/environment/vanilla/src/config/env/validate.ts +18 -0
  80. package/templates/environment/zod/package.json +5 -0
  81. package/templates/environment/zod/src/config/env/env.schema.ts +11 -0
  82. package/templates/environment/zod/src/config/env/validate.ts +19 -0
  83. package/templates/esm-module/package.json +3 -0
  84. package/templates/test-runners/common/package.json +5 -0
  85. package/templates/test-runners/jest/eslint-jest/eslint.config.mjs +36 -0
  86. package/templates/test-runners/jest/package.json +7 -0
  87. package/templates/test-runners/mocha/package.json +8 -0
  88. package/templates/test-runners/vitest/eslint-vitest/eslint.config.mjs +43 -0
  89. package/templates/test-runners/vitest/package.json +7 -0
  90. package/templates/unit-tests/common/gitignore +1 -0
  91. package/templates/unit-tests/common/nest-cli.json +8 -0
  92. package/templates/unit-tests/jest/jest.config.json +10 -0
  93. package/templates/unit-tests/jest/package.json +7 -0
  94. package/templates/unit-tests/jest/src/app.controller.spec.ts +22 -0
  95. package/templates/unit-tests/mocha/cjs-mocha/.mocharc.json +5 -0
  96. package/templates/unit-tests/mocha/cjs-mocha/package.json +5 -0
  97. package/templates/unit-tests/mocha/esm-mocha/.mocharc.json +5 -0
  98. package/templates/unit-tests/mocha/package.json +9 -0
  99. package/templates/unit-tests/mocha/src/app.controller.spec.ts +23 -0
  100. package/templates/unit-tests/vitest/package.json +10 -0
  101. package/templates/unit-tests/vitest/src/app.controller.spec.ts +23 -0
  102. package/templates/unit-tests/vitest/vitest.config.mjs +14 -0
  103. package/templates/versions/11/tsconfig.build.json +4 -0
  104. package/templates/versions/11/tsconfig.json +26 -0
  105. package/templates/versions/12/tsconfig.build.json +8 -0
  106. package/templates/versions/12/tsconfig.json +27 -0
@@ -0,0 +1,8 @@
1
+ {
2
+ "$schema": "./node_modules/oxlint/configuration_schema.json",
3
+ "plugins": ["typescript", "oxc"],
4
+ "rules": {
5
+ "typescript/no-explicit-any": "warn",
6
+ "typescript/no-floating-promises": "error"
7
+ }
8
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "scripts": {
3
+ "lint": "oxlint --type-aware",
4
+ "lint:fix": "oxlint --type-aware --fix"
5
+ },
6
+ "devDependencies": {
7
+ "oxlint": "^1.90.0",
8
+ "oxlint-tsgolint": "^7.0.2001"
9
+ }
10
+ }
@@ -0,0 +1,10 @@
1
+ # Dependencies
2
+ node_modules
3
+
4
+ # Build output
5
+ dist
6
+ build
7
+ coverage
8
+
9
+ # Generated files
10
+ *.generated.*
@@ -0,0 +1,7 @@
1
+ {
2
+ "semi": true,
3
+ "singleQuote": false,
4
+ "tabWidth": 2,
5
+ "trailingComma": "es5",
6
+ "printWidth": 80
7
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "scripts": {
3
+ "format": "prettier \"{src,test}/**/*.{js,ts,json}\" --write",
4
+ "format:check": "prettier \"{src,test}/**/*.{js,ts,json}\" --check"
5
+ },
6
+ "devDependencies": {
7
+ "prettier": "^3.4.2"
8
+ }
9
+ }
@@ -0,0 +1,2 @@
1
+ /cypress/screenshots
2
+ /cypress/videos
@@ -0,0 +1,12 @@
1
+ {
2
+ "scripts": {
3
+ "test:e2e": "node scripts/test-e2e.mjs \"cypress run --config-file test/cypress.config.mjs\"",
4
+ "test:e2e:cypress:open": "node scripts/test-e2e.mjs \"cypress open --config-file test/cypress.config.mjs\""
5
+ },
6
+ "devDependencies": {
7
+ "cypress": "^16.3.0"
8
+ },
9
+ "trustedDependencies": [
10
+ "cypress"
11
+ ]
12
+ }
@@ -0,0 +1,10 @@
1
+ /// <reference types="cypress" />
2
+
3
+ describe("GET /", () => {
4
+ it("returns Hello World!", () => {
5
+ cy.request("/").then((response) => {
6
+ expect(response.status).to.eq(200);
7
+ expect(response.body).to.eq("Hello World!");
8
+ });
9
+ });
10
+ });
@@ -0,0 +1,9 @@
1
+ import { defineConfig } from "cypress";
2
+
3
+ export default defineConfig({
4
+ e2e: {
5
+ baseUrl: process.env.BASE_URL,
6
+ specPattern: "test/**/*.cy.ts",
7
+ supportFile: false,
8
+ },
9
+ });
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../tsconfig.json",
3
+ "compilerOptions": {
4
+ "types": ["cypress", "node"],
5
+ "noEmit": true
6
+ },
7
+ "include": ["./**/*.ts"]
8
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "devDependencies": {
3
+ "start-server-and-test": "^3.0.13"
4
+ }
5
+ }
@@ -0,0 +1,48 @@
1
+ import { execFileSync } from "node:child_process";
2
+ import { createRequire } from "node:module";
3
+ import { dirname, resolve } from "node:path";
4
+
5
+ let binPath;
6
+ try {
7
+ const require = createRequire(import.meta.url);
8
+ const pkgPath = require.resolve("start-server-and-test/package.json");
9
+ const pkg = require(pkgPath);
10
+ binPath = resolve(dirname(pkgPath), pkg.bin["start-server-and-test"]);
11
+ } catch {
12
+ console.error('Error: "start-server-and-test" is not installed.');
13
+ process.exit(1);
14
+ }
15
+
16
+ const testCmd = process.argv.slice(2).join(" ");
17
+ if (!testCmd) {
18
+ console.error("Error: No test command provided.");
19
+ process.exit(1);
20
+ }
21
+
22
+ const port = process.env.PORT ?? "3000";
23
+ const baseUrl = process.env.BASE_URL ?? `http://127.0.0.1:${port}`;
24
+ const probeUrl = baseUrl.replace(/^http(s?):\/\//, "http$1-get://");
25
+ const isWin = process.platform === "win32";
26
+
27
+ /**
28
+ * Spawns a test server and executes E2E tests against dynamic ports.
29
+ * NOTE: Fails if the app or test command hardcodes a different port instead of using BASE_URL/PORT.
30
+ */
31
+
32
+ try {
33
+ execFileSync(
34
+ process.execPath,
35
+ [binPath, "start", probeUrl, isWin ? `"${testCmd}"` : testCmd],
36
+ {
37
+ stdio: "inherit",
38
+ shell: isWin,
39
+ env: {
40
+ ...process.env,
41
+ PORT: port,
42
+ BASE_URL: baseUrl,
43
+ },
44
+ }
45
+ );
46
+ } catch (error) {
47
+ process.exit(error.status ?? 1);
48
+ }
@@ -0,0 +1,2 @@
1
+ /test-results
2
+ /playwright-report
@@ -0,0 +1,8 @@
1
+ {
2
+ "scripts": {
3
+ "test:e2e": "node scripts/test-e2e.mjs \"playwright test -c test/playwright.config.mjs\""
4
+ },
5
+ "devDependencies": {
6
+ "@playwright/test": "^1.65.1"
7
+ }
8
+ }
@@ -0,0 +1,8 @@
1
+ import { expect, test } from "@playwright/test";
2
+
3
+ test("GET /", async ({ request }) => {
4
+ const response = await request.get("/");
5
+
6
+ expect(response.status()).toBe(200);
7
+ expect(await response.text()).toBe("Hello World!");
8
+ });
@@ -0,0 +1,9 @@
1
+ import { defineConfig } from "@playwright/test";
2
+
3
+ export default defineConfig({
4
+ testDir: ".",
5
+ testMatch: "**/*.e2e-spec.ts",
6
+ use: {
7
+ baseURL: process.env.BASE_URL,
8
+ },
9
+ });
@@ -0,0 +1,9 @@
1
+ {
2
+ "moduleFileExtensions": ["js", "json", "ts"],
3
+ "rootDir": ".",
4
+ "testEnvironment": "node",
5
+ "testRegex": ".e2e-spec.ts$",
6
+ "transform": {
7
+ "^.+\\.(t|j)s$": "ts-jest"
8
+ }
9
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "require": ["reflect-metadata", "@swc-node/register"],
3
+ "extension": ["ts"],
4
+ "spec": ["test/**/*.e2e-spec.ts"]
5
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "node-option": ["import=@swc-node/register/esm-register"],
3
+ "extension": ["ts"],
4
+ "spec": ["test/**/*.e2e-spec.ts"]
5
+ }
@@ -0,0 +1,15 @@
1
+ import swc from "unplugin-swc";
2
+ import { defineConfig } from "vitest/config";
3
+
4
+ export default defineConfig({
5
+ plugins: [
6
+ swc.vite({
7
+ module: { type: "es6" },
8
+ }),
9
+ ],
10
+ test: {
11
+ root: "./",
12
+ environment: "node",
13
+ include: ["test/**/*.e2e-spec.ts"],
14
+ },
15
+ });
@@ -0,0 +1,5 @@
1
+ {
2
+ "scripts": {
3
+ "test:e2e": "node scripts/test-e2e.mjs \"jest --config ./test/jest-e2e.json\""
4
+ }
5
+ }
@@ -0,0 +1,25 @@
1
+ import { Browser, Builder, By, type WebDriver } from "selenium-webdriver";
2
+
3
+ describe("App (Selenium E2E)", () => {
4
+ let driver: WebDriver | undefined;
5
+
6
+ beforeAll(async () => {
7
+ driver = await new Builder().forBrowser(Browser.CHROME).build();
8
+ }, 60_000);
9
+
10
+ afterAll(async () => {
11
+ await driver?.quit();
12
+ });
13
+
14
+ it("GET /", async () => {
15
+ if (!driver) throw new Error("WebDriver was not initialized");
16
+
17
+ const baseURL =
18
+ process.env.BASE_URL ?? `http://127.0.0.1:${process.env.PORT ?? "3000"}`;
19
+ await driver.get(baseURL);
20
+
21
+ const body = await driver.findElement(By.css("body")).getText();
22
+
23
+ expect(body).toBe("Hello World!");
24
+ });
25
+ });
@@ -0,0 +1,5 @@
1
+ {
2
+ "scripts": {
3
+ "test:e2e": "node scripts/test-e2e.mjs \"mocha --config ./test/.mocharc.e2e.json\""
4
+ }
5
+ }
@@ -0,0 +1,28 @@
1
+ import { Browser, Builder, By, type WebDriver } from "selenium-webdriver";
2
+ import { strict as assert } from "node:assert";
3
+
4
+ describe("App (Selenium E2E)", function () {
5
+ let driver: WebDriver | undefined;
6
+
7
+ this.timeout(60_000);
8
+
9
+ before(async () => {
10
+ driver = await new Builder().forBrowser(Browser.CHROME).build();
11
+ });
12
+
13
+ after(async () => {
14
+ await driver?.quit();
15
+ });
16
+
17
+ it("GET /", async () => {
18
+ if (!driver) throw new Error("WebDriver was not initialized");
19
+
20
+ const baseURL =
21
+ process.env.BASE_URL ?? `http://127.0.0.1:${process.env.PORT ?? "3000"}`;
22
+ await driver.get(baseURL);
23
+
24
+ const body = await driver.findElement(By.css("body")).getText();
25
+
26
+ assert.equal(body, "Hello World!");
27
+ });
28
+ });
@@ -0,0 +1,6 @@
1
+ {
2
+ "devDependencies": {
3
+ "@types/selenium-webdriver": "^4.35.7",
4
+ "selenium-webdriver": "^4.51.0"
5
+ }
6
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "scripts": {
3
+ "test:e2e": "node scripts/test-e2e.mjs \"vitest run --config ./test/vitest.config.e2e.mjs\""
4
+ }
5
+ }
@@ -0,0 +1,26 @@
1
+ import { Browser, Builder, By, type WebDriver } from "selenium-webdriver";
2
+ import { afterAll, beforeAll, describe, expect, it } from "vitest";
3
+
4
+ describe("App (Selenium E2E)", () => {
5
+ let driver: WebDriver | undefined;
6
+
7
+ beforeAll(async () => {
8
+ driver = await new Builder().forBrowser(Browser.CHROME).build();
9
+ }, 60_000);
10
+
11
+ afterAll(async () => {
12
+ await driver?.quit();
13
+ });
14
+
15
+ it("GET /", async () => {
16
+ if (!driver) throw new Error("WebDriver was not initialized");
17
+
18
+ const baseURL =
19
+ process.env.BASE_URL ?? `http://127.0.0.1:${process.env.PORT ?? "3000"}`;
20
+ await driver.get(baseURL);
21
+
22
+ const body = await driver.findElement(By.css("body")).getText();
23
+
24
+ expect(body).toBe("Hello World!");
25
+ });
26
+ });
@@ -0,0 +1,5 @@
1
+ {
2
+ "scripts": {
3
+ "test:e2e": "jest --config ./test/jest-e2e.json"
4
+ }
5
+ }
@@ -0,0 +1,29 @@
1
+ import { Test, TestingModule } from "@nestjs/testing";
2
+ import { INestApplication } from "@nestjs/common";
3
+ import request from "supertest";
4
+ import { App } from "supertest/types";
5
+ import { AppModule } from "../src/app.module";
6
+
7
+ describe("AppController (e2e)", () => {
8
+ let app: INestApplication<App>;
9
+
10
+ beforeAll(async () => {
11
+ const moduleFixture: TestingModule = await Test.createTestingModule({
12
+ imports: [AppModule],
13
+ }).compile();
14
+
15
+ app = moduleFixture.createNestApplication();
16
+ await app.init();
17
+ });
18
+
19
+ afterAll(async () => {
20
+ await app.close();
21
+ });
22
+
23
+ it("GET /", async () => {
24
+ await request(app.getHttpServer())
25
+ .get("/")
26
+ .expect(200)
27
+ .expect("Hello World!");
28
+ });
29
+ });
@@ -0,0 +1,5 @@
1
+ {
2
+ "scripts": {
3
+ "test:e2e": "mocha --config ./test/.mocharc.e2e.json"
4
+ }
5
+ }
@@ -0,0 +1,30 @@
1
+ import { INestApplication } from "@nestjs/common";
2
+ import { Test, TestingModule } from "@nestjs/testing";
3
+ import { after, before, describe, it } from "mocha";
4
+ import request from "supertest";
5
+ import { App } from "supertest/types";
6
+ import { AppModule } from "../src/app.module";
7
+
8
+ describe("AppController (e2e)", () => {
9
+ let app: INestApplication<App>;
10
+
11
+ before(async () => {
12
+ const moduleFixture: TestingModule = await Test.createTestingModule({
13
+ imports: [AppModule],
14
+ }).compile();
15
+
16
+ app = moduleFixture.createNestApplication();
17
+ await app.init();
18
+ });
19
+
20
+ after(async () => {
21
+ await app.close();
22
+ });
23
+
24
+ it("GET /", async () => {
25
+ await request(app.getHttpServer())
26
+ .get("/")
27
+ .expect(200)
28
+ .expect("Hello World!");
29
+ });
30
+ });
@@ -0,0 +1,6 @@
1
+ {
2
+ "devDependencies": {
3
+ "@types/supertest": "^7.2.1",
4
+ "supertest": "^7.3.2"
5
+ }
6
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "scripts": {
3
+ "test:e2e": "vitest run --config ./test/vitest.config.e2e.mjs"
4
+ }
5
+ }
@@ -0,0 +1,30 @@
1
+ import { INestApplication } from "@nestjs/common";
2
+ import { Test, TestingModule } from "@nestjs/testing";
3
+ import request from "supertest";
4
+ import { App } from "supertest/types";
5
+ import { afterAll, beforeAll, describe, it } from "vitest";
6
+ import { AppModule } from "../src/app.module";
7
+
8
+ describe("AppController (e2e)", () => {
9
+ let app: INestApplication<App>;
10
+
11
+ beforeAll(async () => {
12
+ const moduleFixture: TestingModule = await Test.createTestingModule({
13
+ imports: [AppModule],
14
+ }).compile();
15
+
16
+ app = moduleFixture.createNestApplication();
17
+ await app.init();
18
+ });
19
+
20
+ afterAll(async () => {
21
+ await app.close();
22
+ });
23
+
24
+ it("GET /", async () => {
25
+ await request(app.getHttpServer())
26
+ .get("/")
27
+ .expect(200)
28
+ .expect("Hello World!");
29
+ });
30
+ });
@@ -0,0 +1,5 @@
1
+ {
2
+ "dependencies": {
3
+ "arktype": "^2.2.10"
4
+ }
5
+ }
@@ -0,0 +1,12 @@
1
+ import { type } from "arktype";
2
+
3
+ // Add environment variables types here
4
+ export const envSchema = type({
5
+ "+": "delete",
6
+ NODE_ENV: "'development' | 'production' | 'test' = 'development'",
7
+ PORT: type("string.integer.parse")
8
+ .to("1 <= number.integer <= 65535")
9
+ .default("3000"),
10
+ });
11
+
12
+ export type EnvironmentVariables = typeof envSchema.infer;
@@ -0,0 +1,12 @@
1
+ import { type } from "arktype";
2
+ import { envSchema } from "./env.schema";
3
+
4
+ export default function validate(config: Record<string, unknown>) {
5
+ const parsed = envSchema(config);
6
+
7
+ if (parsed instanceof type.errors) {
8
+ throw new Error(`Config validation error:\n${parsed.summary}`);
9
+ }
10
+
11
+ return parsed;
12
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "dependencies": {
3
+ "class-transformer": "^0.5.1",
4
+ "class-validator": "^0.15.1"
5
+ }
6
+ }
@@ -0,0 +1,15 @@
1
+ import { Type } from "class-transformer";
2
+ import { IsIn, IsNumber, IsOptional, Max, Min } from "class-validator";
3
+
4
+ // Add environment variables types here
5
+ export class EnvironmentVariables {
6
+ @IsIn(["development", "production", "test"])
7
+ NODE_ENV: "development" | "production" | "test" = "development";
8
+
9
+ @Type(() => Number)
10
+ @IsNumber()
11
+ @IsOptional()
12
+ @Min(1)
13
+ @Max(65535)
14
+ PORT: number = 3000;
15
+ }
@@ -0,0 +1,15 @@
1
+ import { plainToInstance } from "class-transformer";
2
+ import { validateSync } from "class-validator";
3
+ import { EnvironmentVariables } from "./env.schema";
4
+
5
+ export default function validate(config: Record<string, unknown>) {
6
+ const validatedConfig = plainToInstance(EnvironmentVariables, config, {
7
+ enableImplicitConversion: true,
8
+ });
9
+ const errors = validateSync(validatedConfig, {
10
+ skipMissingProperties: false,
11
+ });
12
+
13
+ if (errors.length > 0) throw new Error(errors.toString());
14
+ return validatedConfig;
15
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "dependencies": {
3
+ "@nestjs/config": "^4.0.4"
4
+ }
5
+ }
@@ -0,0 +1,11 @@
1
+ import { Module } from "@nestjs/common";
2
+ import { EnvModule } from "./config/env/env.module";
3
+ import { AppController } from "./app.controller";
4
+ import { AppService } from "./app.service";
5
+
6
+ @Module({
7
+ imports: [EnvModule],
8
+ controllers: [AppController],
9
+ providers: [AppService],
10
+ })
11
+ export class AppModule {}
@@ -0,0 +1,17 @@
1
+ import { Module } from "@nestjs/common";
2
+ import { ConfigModule } from "@nestjs/config";
3
+ import { EnvProvider } from "./env.provider";
4
+ import validate from "./validate";
5
+
6
+ @Module({
7
+ imports: [
8
+ ConfigModule.forRoot({
9
+ validate,
10
+ isGlobal: true,
11
+ envFilePath: [".env.local", ".env"],
12
+ }),
13
+ ],
14
+ providers: [EnvProvider],
15
+ exports: [EnvProvider],
16
+ })
17
+ export class EnvModule {}
@@ -0,0 +1,17 @@
1
+ import { Injectable } from "@nestjs/common";
2
+ import { ConfigService } from "@nestjs/config";
3
+ import { type EnvironmentVariables } from "./env.schema";
4
+
5
+ @Injectable()
6
+ export class EnvProvider {
7
+ constructor(private configService: ConfigService<EnvironmentVariables>) {}
8
+
9
+ get<T extends keyof EnvironmentVariables>(key: T) {
10
+ const value = this.configService.get(key, { infer: true });
11
+ if (value === undefined) {
12
+ throw new Error(`Config error: ${key} is undefined`);
13
+ }
14
+
15
+ return value;
16
+ }
17
+ }
@@ -0,0 +1,15 @@
1
+ import { NestFactory } from "@nestjs/core";
2
+ import { AppModule } from "./app.module";
3
+ import { EnvProvider } from "./config/env/env.provider";
4
+
5
+ async function bootstrap() {
6
+ const app = await NestFactory.create(AppModule);
7
+ const env = app.get(EnvProvider);
8
+ const PORT = env.get("PORT");
9
+ await app.listen(PORT);
10
+ }
11
+
12
+ bootstrap().catch((err) => {
13
+ console.error(err);
14
+ process.exit(1);
15
+ });
@@ -0,0 +1,5 @@
1
+ {
2
+ "dependencies": {
3
+ "joi": "^18.3.2"
4
+ }
5
+ }
@@ -0,0 +1,16 @@
1
+ import Joi from "joi";
2
+
3
+ // Include the env type in this interface
4
+ export interface EnvironmentVariables {
5
+ NODE_ENV: "development" | "production" | "test";
6
+ PORT: number;
7
+ }
8
+
9
+ // Add environment variables types here
10
+ export const envSchema = Joi.object({
11
+ NODE_ENV: Joi.string()
12
+ .valid("development", "production", "test")
13
+ .default("development"),
14
+
15
+ PORT: Joi.number().integer().min(1).max(65535).default(3000),
16
+ });
@@ -0,0 +1,16 @@
1
+ import Joi from "joi";
2
+ import { envSchema } from "./env.schema";
3
+
4
+ export default function validate(config: Record<string, unknown>) {
5
+ const result: Joi.ValidationResult<Record<string, unknown>> =
6
+ envSchema.validate(config, {
7
+ allowUnknown: true,
8
+ abortEarly: false,
9
+ });
10
+
11
+ if (result.error) {
12
+ throw new Error(`Config validation error:\n${result.error.message}`);
13
+ }
14
+
15
+ return result.value;
16
+ }