From 349b019dcf5172182b2fd107975e9b997f9b7b42 Mon Sep 17 00:00:00 2001 From: dymok Date: Wed, 27 Nov 2024 21:34:11 +0100 Subject: [PATCH] implemented enviroment variables for production so se4rver doesnt use localhost --- apps/InstrumentRental-web/project.json | 6 ++++++ apps/data-api/src/main.ts | 2 +- libs/backend/dto/src/index.ts | 1 + .../src/lib/instrument/instrument.service.ts | 8 ++++++-- libs/shared/util-env/README.md | 7 +++++++ libs/shared/util-env/eslint.config.js | 3 +++ libs/shared/util-env/jest.config.ts | 10 ++++++++++ libs/shared/util-env/project.json | 16 ++++++++++++++++ libs/shared/util-env/src/index.ts | 2 ++ libs/shared/util-env/src/lib/env.development.ts | 6 ++++++ libs/shared/util-env/src/lib/env.interface.ts | 4 ++++ libs/shared/util-env/src/lib/env.production.ts | 6 ++++++ libs/shared/util-env/src/lib/env.ts | 3 +++ libs/shared/util-env/tsconfig.json | 16 ++++++++++++++++ libs/shared/util-env/tsconfig.lib.json | 11 +++++++++++ libs/shared/util-env/tsconfig.spec.json | 14 ++++++++++++++ tsconfig.base.json | 3 +++ 17 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 libs/shared/util-env/README.md create mode 100644 libs/shared/util-env/eslint.config.js create mode 100644 libs/shared/util-env/jest.config.ts create mode 100644 libs/shared/util-env/project.json create mode 100644 libs/shared/util-env/src/index.ts create mode 100644 libs/shared/util-env/src/lib/env.development.ts create mode 100644 libs/shared/util-env/src/lib/env.interface.ts create mode 100644 libs/shared/util-env/src/lib/env.production.ts create mode 100644 libs/shared/util-env/src/lib/env.ts create mode 100644 libs/shared/util-env/tsconfig.json create mode 100644 libs/shared/util-env/tsconfig.lib.json create mode 100644 libs/shared/util-env/tsconfig.spec.json diff --git a/apps/InstrumentRental-web/project.json b/apps/InstrumentRental-web/project.json index 904e4e3..3375fab 100644 --- a/apps/InstrumentRental-web/project.json +++ b/apps/InstrumentRental-web/project.json @@ -26,6 +26,12 @@ }, "configurations": { "production": { + "fileReplacements": [ + { + "replace": "libs/shared/util-env/src/lib/env.development.ts", + "with": "libs/shared/util-env/src/lib/env.production.ts" + } + ], "budgets": [ { "type": "initial", diff --git a/apps/data-api/src/main.ts b/apps/data-api/src/main.ts index 710e11d..f6a19d6 100644 --- a/apps/data-api/src/main.ts +++ b/apps/data-api/src/main.ts @@ -7,7 +7,7 @@ import { Logger } from '@nestjs/common'; import { NestFactory } from '@nestjs/core'; import { AppModule } from './app/app.module'; import { CorsOptions } from '@nestjs/common/interfaces/external/cors-options.interface'; -import { ApiResponseInterceptor } from '../../../libs/backend/dto/src/lib/api-response.interceptor'; +import { ApiResponseInterceptor } from '@InstrumentRental/backend/dto' async function bootstrap() { const app = await NestFactory.create(AppModule); diff --git a/libs/backend/dto/src/index.ts b/libs/backend/dto/src/index.ts index dc1de77..c295ede 100644 --- a/libs/backend/dto/src/index.ts +++ b/libs/backend/dto/src/index.ts @@ -1,2 +1,3 @@ export * from './lib/dto.module'; export * from './lib/instrument.dto' +export * from './lib/api-response.interceptor' diff --git a/libs/frontend/features/src/lib/instrument/instrument.service.ts b/libs/frontend/features/src/lib/instrument/instrument.service.ts index 78189cd..0fa9457 100644 --- a/libs/frontend/features/src/lib/instrument/instrument.service.ts +++ b/libs/frontend/features/src/lib/instrument/instrument.service.ts @@ -1,8 +1,9 @@ import { Observable, throwError } from 'rxjs'; import { HttpClient, HttpErrorResponse } from '@angular/common/http'; -import { map, catchError, tap } from 'rxjs/operators'; +import { catchError, map, tap } from 'rxjs/operators'; import { ApiResponse, IInstrument } from '@InstrumentRental/shared/api'; import { Injectable } from '@angular/core'; +import { env } from '@InstrumentRental/shared/util-env'; /** * See https://angular.io/guide/http#requesting-data-from-a-server @@ -18,7 +19,10 @@ export const httpOptions = { */ @Injectable() export class InstrumentService { - endpoint = 'http://localhost:3000/api/instrument'; + /* + endpoint = 'http://localhost:3000/api/instrument'; + */ + endpoint = env.dataApiUrl + '/instrument'; constructor(private readonly http: HttpClient) {} diff --git a/libs/shared/util-env/README.md b/libs/shared/util-env/README.md new file mode 100644 index 0000000..0f43663 --- /dev/null +++ b/libs/shared/util-env/README.md @@ -0,0 +1,7 @@ +# util-env + +This library was generated with [Nx](https://nx.dev). + +## Running unit tests + +Run `nx test util-env` to execute the unit tests via [Jest](https://jestjs.io). diff --git a/libs/shared/util-env/eslint.config.js b/libs/shared/util-env/eslint.config.js new file mode 100644 index 0000000..07e518f --- /dev/null +++ b/libs/shared/util-env/eslint.config.js @@ -0,0 +1,3 @@ +const baseConfig = require('../../../eslint.config.js'); + +module.exports = [...baseConfig]; diff --git a/libs/shared/util-env/jest.config.ts b/libs/shared/util-env/jest.config.ts new file mode 100644 index 0000000..9d9af24 --- /dev/null +++ b/libs/shared/util-env/jest.config.ts @@ -0,0 +1,10 @@ +export default { + displayName: 'util-env', + preset: '../../../jest.preset.js', + testEnvironment: 'node', + transform: { + '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '/tsconfig.spec.json' }], + }, + moduleFileExtensions: ['ts', 'js', 'html'], + coverageDirectory: '../../../coverage/libs/shared/util-env', +}; diff --git a/libs/shared/util-env/project.json b/libs/shared/util-env/project.json new file mode 100644 index 0000000..d6b3a6a --- /dev/null +++ b/libs/shared/util-env/project.json @@ -0,0 +1,16 @@ +{ + "name": "util-env", + "$schema": "../../../node_modules/nx/schemas/project-schema.json", + "sourceRoot": "libs/shared/util-env/src", + "projectType": "library", + "tags": [], + "targets": { + "test": { + "executor": "@nx/jest:jest", + "outputs": ["{workspaceRoot}/coverage/{projectRoot}"], + "options": { + "jestConfig": "libs/shared/util-env/jest.config.ts" + } + } + } +} diff --git a/libs/shared/util-env/src/index.ts b/libs/shared/util-env/src/index.ts new file mode 100644 index 0000000..db48589 --- /dev/null +++ b/libs/shared/util-env/src/index.ts @@ -0,0 +1,2 @@ +export * from './lib/env.development' +export * from './lib/env.interface' diff --git a/libs/shared/util-env/src/lib/env.development.ts b/libs/shared/util-env/src/lib/env.development.ts new file mode 100644 index 0000000..ac9ec96 --- /dev/null +++ b/libs/shared/util-env/src/lib/env.development.ts @@ -0,0 +1,6 @@ +import { IEnv } from './env.interface'; + +export const env: IEnv = { + production: false, + dataApiUrl: 'https://localhost:3000' +} diff --git a/libs/shared/util-env/src/lib/env.interface.ts b/libs/shared/util-env/src/lib/env.interface.ts new file mode 100644 index 0000000..f819fd7 --- /dev/null +++ b/libs/shared/util-env/src/lib/env.interface.ts @@ -0,0 +1,4 @@ +export interface IEnv { + production: boolean, + dataApiUrl: string +} diff --git a/libs/shared/util-env/src/lib/env.production.ts b/libs/shared/util-env/src/lib/env.production.ts new file mode 100644 index 0000000..ab305f9 --- /dev/null +++ b/libs/shared/util-env/src/lib/env.production.ts @@ -0,0 +1,6 @@ +import { IEnv } from './env.interface'; + +export const env: IEnv = { + production: false, + dataApiUrl: 'instrumentrental-api-d2f7ahfvh8d2ahd0.westeurope-01.azurewebsites.net' +} diff --git a/libs/shared/util-env/src/lib/env.ts b/libs/shared/util-env/src/lib/env.ts new file mode 100644 index 0000000..5f20517 --- /dev/null +++ b/libs/shared/util-env/src/lib/env.ts @@ -0,0 +1,3 @@ +export function env(): string { + return 'util-env'; +} diff --git a/libs/shared/util-env/tsconfig.json b/libs/shared/util-env/tsconfig.json new file mode 100644 index 0000000..25f7201 --- /dev/null +++ b/libs/shared/util-env/tsconfig.json @@ -0,0 +1,16 @@ +{ + "extends": "../../../tsconfig.base.json", + "compilerOptions": { + "module": "commonjs" + }, + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json" + }, + { + "path": "./tsconfig.spec.json" + } + ] +} diff --git a/libs/shared/util-env/tsconfig.lib.json b/libs/shared/util-env/tsconfig.lib.json new file mode 100644 index 0000000..e583571 --- /dev/null +++ b/libs/shared/util-env/tsconfig.lib.json @@ -0,0 +1,11 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "commonjs", + "outDir": "../../../dist/out-tsc", + "declaration": true, + "types": ["node"] + }, + "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"], + "include": ["src/**/*.ts"] +} diff --git a/libs/shared/util-env/tsconfig.spec.json b/libs/shared/util-env/tsconfig.spec.json new file mode 100644 index 0000000..69a251f --- /dev/null +++ b/libs/shared/util-env/tsconfig.spec.json @@ -0,0 +1,14 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../../dist/out-tsc", + "module": "commonjs", + "types": ["jest", "node"] + }, + "include": [ + "jest.config.ts", + "src/**/*.test.ts", + "src/**/*.spec.ts", + "src/**/*.d.ts" + ] +} diff --git a/tsconfig.base.json b/tsconfig.base.json index 94d48d5..1c8dab6 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -20,6 +20,9 @@ "libs/backend/features/src/index.ts" ], "@InstrumentRental/shared/api": ["libs/shared/api/src/index.ts"], + "@InstrumentRental/shared/util-env": [ + "libs/shared/util-env/src/index.ts" + ], "@avans-nx-workshop/shared/api": ["libs/shared/api/src/index.ts"], "@instrument-rental/common": ["libs/frontend/common/src/index.ts"], "@instrument-rental/features": ["libs/frontend/features/src/index.ts"],