@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.
- package/LICENSE +22 -0
- package/README.md +62 -2
- package/dist/cli.js +439 -0
- package/dist/options.js +98 -0
- package/dist/package-managers.js +23 -0
- package/dist/scaffold.js +158 -0
- package/dist/settings.js +52 -0
- package/dist/versions.js +33 -0
- package/package.json +47 -4
- package/templates/agent-files/11/AGENTS.md +24 -0
- package/templates/agent-files/12/AGENTS.md +39 -0
- package/templates/agent-files/common/CLAUDE.md +1 -0
- package/templates/base/README.md +70 -0
- package/templates/base/gitattributes +1 -0
- package/templates/base/gitignore +8 -0
- package/templates/base/nest-cli.json +11 -0
- package/templates/base/package.json +32 -0
- package/templates/base/src/app.controller.ts +12 -0
- package/templates/base/src/app.module.ts +10 -0
- package/templates/base/src/app.service.ts +8 -0
- package/templates/base/src/main.ts +13 -0
- package/templates/cjs-module/package.json +7 -0
- package/templates/code-quality/biome/biome.json +47 -0
- package/templates/code-quality/biome/package.json +14 -0
- package/templates/code-quality/eslint/eslint.config.mjs +35 -0
- package/templates/code-quality/eslint/package.json +14 -0
- package/templates/code-quality/oxlint/.oxlintrc.json +8 -0
- package/templates/code-quality/oxlint/package.json +10 -0
- package/templates/code-quality/prettier/.prettierignore +10 -0
- package/templates/code-quality/prettier/.prettierrc +7 -0
- package/templates/code-quality/prettier/package.json +9 -0
- package/templates/e2e-tests/cypress/gitignore +2 -0
- package/templates/e2e-tests/cypress/package.json +12 -0
- package/templates/e2e-tests/cypress/test/app.cy.ts +10 -0
- package/templates/e2e-tests/cypress/test/cypress.config.mjs +9 -0
- package/templates/e2e-tests/cypress/test/tsconfig.json +8 -0
- package/templates/e2e-tests/e2e-scripts/package.json +5 -0
- package/templates/e2e-tests/e2e-scripts/scripts/test-e2e.mjs +48 -0
- package/templates/e2e-tests/playwright/gitignore +2 -0
- package/templates/e2e-tests/playwright/package.json +8 -0
- package/templates/e2e-tests/playwright/test/app.e2e-spec.ts +8 -0
- package/templates/e2e-tests/playwright/test/playwright.config.mjs +9 -0
- package/templates/e2e-tests/runner-configs/jest/test/jest-e2e.json +9 -0
- package/templates/e2e-tests/runner-configs/mocha/cjs-mocha/test/.mocharc.e2e.json +5 -0
- package/templates/e2e-tests/runner-configs/mocha/esm-mocha/test/.mocharc.e2e.json +5 -0
- package/templates/e2e-tests/runner-configs/vitest/test/vitest.config.e2e.mjs +15 -0
- package/templates/e2e-tests/selenium/jest-selenium/package.json +5 -0
- package/templates/e2e-tests/selenium/jest-selenium/test/app.e2e-spec.ts +25 -0
- package/templates/e2e-tests/selenium/mocha-selenium/package.json +5 -0
- package/templates/e2e-tests/selenium/mocha-selenium/test/app.e2e-spec.ts +28 -0
- package/templates/e2e-tests/selenium/package.json +6 -0
- package/templates/e2e-tests/selenium/vitest-selenium/package.json +5 -0
- package/templates/e2e-tests/selenium/vitest-selenium/test/app.e2e-spec.ts +26 -0
- package/templates/e2e-tests/supertest/jest-supertest/package.json +5 -0
- package/templates/e2e-tests/supertest/jest-supertest/test/app.e2e-spec.ts +29 -0
- package/templates/e2e-tests/supertest/mocha-supertest/package.json +5 -0
- package/templates/e2e-tests/supertest/mocha-supertest/test/app.e2e-spec.ts +30 -0
- package/templates/e2e-tests/supertest/package.json +6 -0
- package/templates/e2e-tests/supertest/vitest-supertest/package.json +5 -0
- package/templates/e2e-tests/supertest/vitest-supertest/test/app.e2e-spec.ts +30 -0
- package/templates/environment/arktype/package.json +5 -0
- package/templates/environment/arktype/src/config/env/env.schema.ts +12 -0
- package/templates/environment/arktype/src/config/env/validate.ts +12 -0
- package/templates/environment/class-validator/package.json +6 -0
- package/templates/environment/class-validator/src/config/env/env.schema.ts +15 -0
- package/templates/environment/class-validator/src/config/env/validate.ts +15 -0
- package/templates/environment/common/package.json +5 -0
- package/templates/environment/common/src/app.module.ts +11 -0
- package/templates/environment/common/src/config/env/env.module.ts +17 -0
- package/templates/environment/common/src/config/env/env.provider.ts +17 -0
- package/templates/environment/common/src/main.ts +15 -0
- package/templates/environment/joi/package.json +5 -0
- package/templates/environment/joi/src/config/env/env.schema.ts +16 -0
- package/templates/environment/joi/src/config/env/validate.ts +16 -0
- package/templates/environment/valibot/package.json +5 -0
- package/templates/environment/valibot/src/config/env/env.schema.ts +20 -0
- package/templates/environment/valibot/src/config/env/validate.ts +16 -0
- package/templates/environment/vanilla/src/config/env/env.schema.ts +26 -0
- package/templates/environment/vanilla/src/config/env/validate.ts +18 -0
- package/templates/environment/zod/package.json +5 -0
- package/templates/environment/zod/src/config/env/env.schema.ts +11 -0
- package/templates/environment/zod/src/config/env/validate.ts +19 -0
- package/templates/esm-module/package.json +3 -0
- package/templates/test-runners/common/package.json +5 -0
- package/templates/test-runners/jest/eslint-jest/eslint.config.mjs +36 -0
- package/templates/test-runners/jest/package.json +7 -0
- package/templates/test-runners/mocha/package.json +8 -0
- package/templates/test-runners/vitest/eslint-vitest/eslint.config.mjs +43 -0
- package/templates/test-runners/vitest/package.json +7 -0
- package/templates/unit-tests/common/gitignore +1 -0
- package/templates/unit-tests/common/nest-cli.json +8 -0
- package/templates/unit-tests/jest/jest.config.json +10 -0
- package/templates/unit-tests/jest/package.json +7 -0
- package/templates/unit-tests/jest/src/app.controller.spec.ts +22 -0
- package/templates/unit-tests/mocha/cjs-mocha/.mocharc.json +5 -0
- package/templates/unit-tests/mocha/cjs-mocha/package.json +5 -0
- package/templates/unit-tests/mocha/esm-mocha/.mocharc.json +5 -0
- package/templates/unit-tests/mocha/package.json +9 -0
- package/templates/unit-tests/mocha/src/app.controller.spec.ts +23 -0
- package/templates/unit-tests/vitest/package.json +10 -0
- package/templates/unit-tests/vitest/src/app.controller.spec.ts +23 -0
- package/templates/unit-tests/vitest/vitest.config.mjs +14 -0
- package/templates/versions/11/tsconfig.build.json +4 -0
- package/templates/versions/11/tsconfig.json +26 -0
- package/templates/versions/12/tsconfig.build.json +8 -0
- package/templates/versions/12/tsconfig.json +27 -0
|
@@ -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,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,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,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,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,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,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,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,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,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,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,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,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
|
+
}
|