Skip to content

Commit

Permalink
feat: add config lib
Browse files Browse the repository at this point in the history
  • Loading branch information
drazisil committed Jul 26, 2024
1 parent c592226 commit eb1d377
Show file tree
Hide file tree
Showing 13 changed files with 198 additions and 6 deletions.
24 changes: 24 additions & 0 deletions libs/config/eslint.config.js
Original file line number Diff line number Diff line change
@@ -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/*"],
}
);
1 change: 1 addition & 0 deletions libs/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { getServerURL } from "./src/index.js";
42 changes: 42 additions & 0 deletions libs/config/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
4 changes: 4 additions & 0 deletions libs/config/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function getServerURL(): string {
const serverURL = process.env.SERVER_URL ?? "https://rusty-motors.com";
return serverURL;
}

Check warning on line 4 in libs/config/src/config.ts

View check run for this annotation

Codecov / codecov/patch

libs/config/src/config.ts#L2-L4

Added lines #L2 - L4 were not covered by tests
1 change: 1 addition & 0 deletions libs/config/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { getServerURL } from "./config.js";
24 changes: 24 additions & 0 deletions libs/config/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export type rawHttpRequestData = {
headers: Record<string, string>;
remoteAddress: string;
method: string;
url: string;
};

export type parsedHttpRequestData = {
headers: Record<string, string>;
remoteAddress: string;
method: string;
pathname: string;
searchParams: URLSearchParams;
};
export type RequestResponse = {
statusCode: number;
body: string;
headers: Record<string, string>;
};

export type User = {
username: string;
password: string;
};
13 changes: 13 additions & 0 deletions libs/config/test/config.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});
11 changes: 11 additions & 0 deletions libs/config/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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" }]
}
20 changes: 20 additions & 0 deletions libs/config/vitest.config.js
Original file line number Diff line number Diff line change
@@ -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,
},
});
1 change: 1 addition & 0 deletions libs/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 3 additions & 5 deletions libs/web/src/processors/processLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { log } from "@rusty/util";
import { extractCredentials, validateCredentials } from "../helpers.js";
import { userLogin } from "../services/AuthLogin.js";

Check warning on line 3 in libs/web/src/processors/processLogin.ts

View check run for this annotation

Codecov / codecov/patch

libs/web/src/processors/processLogin.ts#L2-L3

Added lines #L2 - L3 were not covered by tests
import type { parsedHttpRequestData, RequestResponse } from "../types.js";
import { getServerURL } from "@rusty/config";

Check warning on line 5 in libs/web/src/processors/processLogin.ts

View check run for this annotation

Codecov / codecov/patch

libs/web/src/processors/processLogin.ts#L5

Added line #L5 was not covered by tests

/**
* Handles the authentication login process.
Expand All @@ -19,16 +20,13 @@ export async function authenticateUser(

await userLogin(username, password);

Check warning on line 21 in libs/web/src/processors/processLogin.ts

View check run for this annotation

Codecov / codecov/patch

libs/web/src/processors/processLogin.ts#L21

Added line #L21 was not covered by tests

// TODO: Implement token generation
const token = "abc123";

Check warning on line 24 in libs/web/src/processors/processLogin.ts

View check run for this annotation

Codecov / codecov/patch

libs/web/src/processors/processLogin.ts#L24

Added line #L24 was not covered by tests

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());
}
}

Check warning on line 31 in libs/web/src/processors/processLogin.ts

View check run for this annotation

Codecov / codecov/patch

libs/web/src/processors/processLogin.ts#L26-L31

Added lines #L26 - L31 were not covered by tests

Expand Down
52 changes: 52 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"references": [
{ "path": "./apps/server" },
{ "path": "./libs/util" },
{ "path": "./libs/web" }
{ "path": "./libs/web" },
{ "path": "./libs/config" }
]
}

0 comments on commit eb1d377

Please sign in to comment.