feat: add nestjs app (#172)
This commit is contained in:
39
apps/nestjs/src/app.controller.spec.ts
Normal file
39
apps/nestjs/src/app.controller.spec.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
/* eslint-disable @typescript-eslint/unbound-method */
|
||||
import type { TestingModule } from "@nestjs/testing";
|
||||
import { Test } from "@nestjs/testing";
|
||||
import { beforeEach, describe, expect, it, vitest } from "vitest";
|
||||
|
||||
import { AppController } from "./app.controller";
|
||||
import { AppService } from "./app.service";
|
||||
import { DatabaseService } from "./db/database.service";
|
||||
|
||||
describe("AppController", () => {
|
||||
let appController: AppController;
|
||||
let appService: AppService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const app: TestingModule = await Test.createTestingModule({
|
||||
controllers: [AppController],
|
||||
providers: [AppService, DatabaseService],
|
||||
}).compile();
|
||||
|
||||
appController = app.get<AppController>(AppController);
|
||||
appService = app.get<AppService>(AppService);
|
||||
});
|
||||
|
||||
describe("root", () => {
|
||||
it('should return "Hello World!"', async () => {
|
||||
// arrange
|
||||
vitest
|
||||
.spyOn(appService, "getHello")
|
||||
.mockReturnValueOnce(Promise.resolve("ABC"));
|
||||
|
||||
// act
|
||||
const a = await appController.getHello();
|
||||
|
||||
// assert
|
||||
expect(a).toBe("ABC");
|
||||
expect(appService.getHello).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
19
apps/nestjs/src/app.controller.ts
Normal file
19
apps/nestjs/src/app.controller.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { Controller, Get, Inject } from "@nestjs/common";
|
||||
|
||||
import { AppService } from "./app.service";
|
||||
|
||||
@Controller()
|
||||
export class AppController {
|
||||
// @ts-expect-error decorators are not correctly handled yet
|
||||
constructor(@Inject(AppService) private readonly appService: AppService) {}
|
||||
|
||||
@Get()
|
||||
async getHello(): Promise<string> {
|
||||
return await this.appService.getHello();
|
||||
}
|
||||
|
||||
@Get("/random")
|
||||
getRandom(): string {
|
||||
return Math.random().toString();
|
||||
}
|
||||
}
|
||||
12
apps/nestjs/src/app.module.ts
Normal file
12
apps/nestjs/src/app.module.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
|
||||
import { AppController } from "./app.controller";
|
||||
import { AppService } from "./app.service";
|
||||
import { DatabaseService } from "./db/database.service";
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
controllers: [AppController],
|
||||
providers: [DatabaseService, AppService],
|
||||
})
|
||||
export class AppModule {}
|
||||
16
apps/nestjs/src/app.service.ts
Normal file
16
apps/nestjs/src/app.service.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { Inject, Injectable } from "@nestjs/common";
|
||||
|
||||
import { DatabaseService } from "./db/database.service";
|
||||
|
||||
@Injectable()
|
||||
export class AppService {
|
||||
constructor(
|
||||
// @ts-expect-error decorators are not correctly handled yet
|
||||
@Inject(DatabaseService) private readonly databaseService: DatabaseService,
|
||||
) {}
|
||||
|
||||
async getHello(): Promise<string> {
|
||||
const users = await this.databaseService.get().query.users.findMany();
|
||||
return JSON.stringify(users);
|
||||
}
|
||||
}
|
||||
10
apps/nestjs/src/db/database.service.ts
Normal file
10
apps/nestjs/src/db/database.service.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
|
||||
import { db } from "@homarr/db";
|
||||
|
||||
@Injectable()
|
||||
export class DatabaseService {
|
||||
get() {
|
||||
return db;
|
||||
}
|
||||
}
|
||||
27
apps/nestjs/src/main.ts
Normal file
27
apps/nestjs/src/main.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { NestFactory } from "@nestjs/core";
|
||||
import { WinstonModule } from "nest-winston";
|
||||
|
||||
import { logger } from "@homarr/log";
|
||||
|
||||
import { AppModule } from "./app.module";
|
||||
|
||||
const winstonLoggerModule = WinstonModule.createLogger({
|
||||
instance: logger,
|
||||
});
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule, {
|
||||
logger: winstonLoggerModule,
|
||||
});
|
||||
await app.listen(3100);
|
||||
}
|
||||
|
||||
// @ts-expect-error this has no type yet
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
if (import.meta.env.PROD) {
|
||||
void bootstrap();
|
||||
}
|
||||
|
||||
export const viteNodeApp = NestFactory.create(AppModule, {
|
||||
logger: winstonLoggerModule,
|
||||
});
|
||||
Reference in New Issue
Block a user