From eb1d3777abccd6066e307af077b7b4c8674e2416 Mon Sep 17 00:00:00 2001 From: Molly Draven Date: Fri, 26 Jul 2024 18:42:56 -0400 Subject: [PATCH] feat: add config lib --- libs/config/eslint.config.js | 24 ++++++++++++ libs/config/index.ts | 1 + libs/config/package.json | 42 ++++++++++++++++++++ libs/config/src/config.ts | 4 ++ libs/config/src/index.ts | 1 + libs/config/src/types.ts | 24 ++++++++++++ libs/config/test/config.test.ts | 13 +++++++ libs/config/tsconfig.json | 11 ++++++ libs/config/vitest.config.js | 20 ++++++++++ libs/web/package.json | 1 + libs/web/src/processors/processLogin.ts | 8 ++-- pnpm-lock.yaml | 52 +++++++++++++++++++++++++ tsconfig.json | 3 +- 13 files changed, 198 insertions(+), 6 deletions(-) create mode 100644 libs/config/eslint.config.js create mode 100644 libs/config/index.ts create mode 100644 libs/config/package.json create mode 100644 libs/config/src/config.ts create mode 100644 libs/config/src/index.ts create mode 100644 libs/config/src/types.ts create mode 100644 libs/config/test/config.test.ts create mode 100644 libs/config/tsconfig.json create mode 100644 libs/config/vitest.config.js diff --git a/libs/config/eslint.config.js b/libs/config/eslint.config.js new file mode 100644 index 0000000..ec3eb89 --- /dev/null +++ b/libs/config/eslint.config.js @@ -0,0 +1,24 @@ +// @ts-check + +import eslint from "@eslint/js"; +import tseslint from "typescript-eslint"; + +export default tseslint.config( + eslint.configs.recommended, + ...tseslint.configs.recommendedTypeChecked, + { + languageOptions: { + parserOptions: { + project: true, + tsconfigRootDir: import.meta.dirname, + }, + }, + }, + { + files: ["eslint.config.js", "vitest.config.js", "**/*.d.ts"], + ...tseslint.configs.disableTypeChecked, + }, + { + ignores: ["**/coverage/*", "**/dist/*"], + } +); diff --git a/libs/config/index.ts b/libs/config/index.ts new file mode 100644 index 0000000..76d3657 --- /dev/null +++ b/libs/config/index.ts @@ -0,0 +1 @@ +export { getServerURL } from "./src/index.js"; \ No newline at end of file diff --git a/libs/config/package.json b/libs/config/package.json new file mode 100644 index 0000000..457a2ca --- /dev/null +++ b/libs/config/package.json @@ -0,0 +1,42 @@ +{ + "name": "@rusty/config", + "version": "1.0.0", + "private": true, + "type": "module", + "imports": { + "#internal": "./dist/src/index.js" + }, + "exports": { + ".": "./dist/index.js" + }, + "types": "./dist/index.d.ts", + "scripts": { + "clean": "rm -rf dist", + "build": "tsc --build --verbose", + "test": "vitest", + "check-types": "tsc --noEmit", + "lint": "eslint src" + }, + "keywords": [], + "author": "", + "license": "GPL-3.0-only", + "devDependencies": { + "@types/node": "20.14.12", + "@types/pino": "7.0.5", + "@vitest/coverage-v8": "^2.0.4", + "@vitest/ui": "^2.0.4", + "eslint": "^8.57.0", + "nx": "19.5.1", + "ts-node": "^10.9.2", + "tslib": "^2.6.3", + "typescript": "^5.5.4", + "vite": "2.0.4", + "vitest": "^2.0.4" + }, + "dependencies": { + "@rusty/util": "workspace:*", + "@sentry/node": "^8.20.0", + "@sentry/profiling-node": "^8.20.0", + "pino": "9.3.1" + } +} diff --git a/libs/config/src/config.ts b/libs/config/src/config.ts new file mode 100644 index 0000000..27e5de8 --- /dev/null +++ b/libs/config/src/config.ts @@ -0,0 +1,4 @@ +export function getServerURL(): string { + const serverURL = process.env.SERVER_URL ?? "https://rusty-motors.com"; + return serverURL; +} diff --git a/libs/config/src/index.ts b/libs/config/src/index.ts new file mode 100644 index 0000000..5ffa937 --- /dev/null +++ b/libs/config/src/index.ts @@ -0,0 +1 @@ +export { getServerURL } from "./config.js"; \ No newline at end of file diff --git a/libs/config/src/types.ts b/libs/config/src/types.ts new file mode 100644 index 0000000..c60f7d1 --- /dev/null +++ b/libs/config/src/types.ts @@ -0,0 +1,24 @@ +export type rawHttpRequestData = { + headers: Record; + remoteAddress: string; + method: string; + url: string; +}; + +export type parsedHttpRequestData = { + headers: Record; + remoteAddress: string; + method: string; + pathname: string; + searchParams: URLSearchParams; +}; +export type RequestResponse = { + statusCode: number; + body: string; + headers: Record; +}; + +export type User = { + username: string; + password: string; +}; diff --git a/libs/config/test/config.test.ts b/libs/config/test/config.test.ts new file mode 100644 index 0000000..cea153f --- /dev/null +++ b/libs/config/test/config.test.ts @@ -0,0 +1,13 @@ +import { describe, expect, it } from "vitest"; +import { getServerURL } from "@rusty/config"; + +describe("config", () => { + it("should have a default serverURL if SERVER_URL environment variable is not set", () => { + expect(getServerURL()).toBe("https://rusty-motors.com"); + }); + + it("should use the SERVER_URL environment variable if set", () => { + process.env.SERVER_URL = "https://example.com"; + expect(getServerURL()).toBe("https://example.com"); + }); +}); diff --git a/libs/config/tsconfig.json b/libs/config/tsconfig.json new file mode 100644 index 0000000..6572291 --- /dev/null +++ b/libs/config/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "outDir": "./dist", + "declarationDir": "./dist", + "composite": true + }, + "include": ["index.ts", "src/**/*.ts", "test/**/*.ts"], + "exclude": ["node_modules", "dist"], + "references": [{ "path": "../util" }] +} diff --git a/libs/config/vitest.config.js b/libs/config/vitest.config.js new file mode 100644 index 0000000..c1aed66 --- /dev/null +++ b/libs/config/vitest.config.js @@ -0,0 +1,20 @@ +import { defineConfig } from "vitest/config"; + +export default defineConfig({ + test: { + coverage: { + enabled: true, + all: true, + exclude: [ + "dist/**", + "eslint.config.js", + "vitest.config.js" + ], + reporter: ["lcov", "text", "cobertura"], + }, + reporters: ["junit", "default", "hanging-process"], + outputFile: "mcos.junit.xml", + pool: "forks", + watch: false, + }, +}); \ No newline at end of file diff --git a/libs/web/package.json b/libs/web/package.json index 56b8f07..174b5a1 100644 --- a/libs/web/package.json +++ b/libs/web/package.json @@ -37,6 +37,7 @@ "@rusty/util": "workspace:*", "@sentry/node": "^8.20.0", "@sentry/profiling-node": "^8.20.0", + "@rusty/config": "workspace:*", "pino": "9.3.1", "sequelize": "6.37.3", "sqlite3": "5.1.7" diff --git a/libs/web/src/processors/processLogin.ts b/libs/web/src/processors/processLogin.ts index c4e3db3..a29f2f0 100644 --- a/libs/web/src/processors/processLogin.ts +++ b/libs/web/src/processors/processLogin.ts @@ -2,6 +2,7 @@ import { log } from "@rusty/util"; import { extractCredentials, validateCredentials } from "../helpers.js"; import { userLogin } from "../services/AuthLogin.js"; import type { parsedHttpRequestData, RequestResponse } from "../types.js"; +import { getServerURL } from "@rusty/config"; /** * Handles the authentication login process. @@ -19,16 +20,13 @@ export async function authenticateUser( await userLogin(username, password); + // TODO: Implement token generation const token = "abc123"; return constructLoginResponse(`Valid=TRUE\nTicket=${token}`); } catch (error: unknown) { log.error(`Error validating credentials: ${(error as Error).message}`); - return generateLoginError( - "INV-200", - "Unable to login", - "https://rusty-motors.com" - ); + return generateLoginError("INV-200", "Unable to login", getServerURL()); } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1b59ab5..1866027 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -94,6 +94,55 @@ importers: specifier: ^2.0.4 version: 2.0.4(@types/node@20.14.12)(@vitest/ui@2.0.4) + libs/config: + dependencies: + '@rusty/util': + specifier: workspace:* + version: link:../util + '@sentry/node': + specifier: ^8.20.0 + version: 8.20.0 + '@sentry/profiling-node': + specifier: ^8.20.0 + version: 8.20.0 + pino: + specifier: 9.3.1 + version: 9.3.1 + devDependencies: + '@types/node': + specifier: 20.14.12 + version: 20.14.12 + '@types/pino': + specifier: 7.0.5 + version: 7.0.5 + '@vitest/coverage-v8': + specifier: ^2.0.4 + version: 2.0.4(vitest@2.0.4(@types/node@20.14.12)(@vitest/ui@2.0.4)) + '@vitest/ui': + specifier: ^2.0.4 + version: 2.0.4(vitest@2.0.4) + eslint: + specifier: ^8.57.0 + version: 8.57.0 + nx: + specifier: 19.5.1 + version: 19.5.1 + ts-node: + specifier: ^10.9.2 + version: 10.9.2(@types/node@20.14.12)(typescript@5.5.4) + tslib: + specifier: ^2.6.3 + version: 2.6.3 + typescript: + specifier: ^5.5.4 + version: 5.5.4 + vite: + specifier: 2.0.4 + version: 2.0.4 + vitest: + specifier: ^2.0.4 + version: 2.0.4(@types/node@20.14.12)(@vitest/ui@2.0.4) + libs/util: dependencies: '@sentry/node': @@ -142,6 +191,9 @@ importers: libs/web: dependencies: + '@rusty/config': + specifier: workspace:* + version: link:../config '@rusty/util': specifier: workspace:* version: link:../util diff --git a/tsconfig.json b/tsconfig.json index 05fa097..c934dfb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -22,6 +22,7 @@ "references": [ { "path": "./apps/server" }, { "path": "./libs/util" }, - { "path": "./libs/web" } + { "path": "./libs/web" }, + { "path": "./libs/config" } ] }