Switched to NestJS
This commit is contained in:
@@ -1,54 +0,0 @@
|
||||
import {join} from "path";
|
||||
import {Configuration, Inject} from "@tsed/di";
|
||||
import {PlatformApplication} from "@tsed/common";
|
||||
import "@tsed/platform-express"; // /!\ keep this import
|
||||
import bodyParser from "body-parser";
|
||||
import compress from "compression";
|
||||
import cookieParser from "cookie-parser";
|
||||
import methodOverride from "method-override";
|
||||
import cors from "cors";
|
||||
import "@tsed/ajv";
|
||||
import "@tsed/typegraphql";
|
||||
import "./datasources/index";
|
||||
import "./resolvers/index";
|
||||
import {config} from "./config/index";
|
||||
import * as rest from "./controllers/rest/index";
|
||||
|
||||
@Configuration({
|
||||
...config,
|
||||
acceptMimes: ["application/json"],
|
||||
httpPort: process.env.PORT || 8083,
|
||||
httpsPort: false, // CHANGE
|
||||
componentsScan: false,
|
||||
mount: {
|
||||
"/rest": [
|
||||
...Object.values(rest)
|
||||
]
|
||||
},
|
||||
middlewares: [
|
||||
cors(),
|
||||
cookieParser(),
|
||||
compress({}),
|
||||
methodOverride(),
|
||||
bodyParser.json(),
|
||||
bodyParser.urlencoded({
|
||||
extended: true
|
||||
})
|
||||
],
|
||||
views: {
|
||||
root: join(process.cwd(), "../views"),
|
||||
extensions: {
|
||||
ejs: "ejs"
|
||||
}
|
||||
},
|
||||
exclude: [
|
||||
"**/*.spec.ts"
|
||||
]
|
||||
})
|
||||
export class Server {
|
||||
@Inject()
|
||||
protected app: PlatformApplication;
|
||||
|
||||
@Configuration()
|
||||
protected settings: Configuration;
|
||||
}
|
||||
22
backend/src/app.controller.spec.ts
Normal file
22
backend/src/app.controller.spec.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
|
||||
describe('AppController', () => {
|
||||
let appController: AppController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const app: TestingModule = await Test.createTestingModule({
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
}).compile();
|
||||
|
||||
appController = app.get<AppController>(AppController);
|
||||
});
|
||||
|
||||
describe('root', () => {
|
||||
it('should return "Hello World!"', () => {
|
||||
expect(appController.getHello()).toBe('Hello World!');
|
||||
});
|
||||
});
|
||||
});
|
||||
12
backend/src/app.controller.ts
Normal file
12
backend/src/app.controller.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { AppService } from './app.service';
|
||||
|
||||
@Controller()
|
||||
export class AppController {
|
||||
constructor(private readonly appService: AppService) {}
|
||||
|
||||
@Get()
|
||||
getHello(): string {
|
||||
return this.appService.getHello();
|
||||
}
|
||||
}
|
||||
10
backend/src/app.module.ts
Normal file
10
backend/src/app.module.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
})
|
||||
export class AppModule {}
|
||||
8
backend/src/app.service.ts
Normal file
8
backend/src/app.service.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class AppService {
|
||||
getHello(): string {
|
||||
return 'Hello World!';
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
import dotenv from "dotenv"
|
||||
|
||||
export const envs = {
|
||||
...process.env,
|
||||
...dotenv.config().parsed
|
||||
};
|
||||
export const isProduction = process.env.NODE_ENV === "production";
|
||||
@@ -1,18 +0,0 @@
|
||||
import {readFileSync} from "fs";
|
||||
import {envs} from "./envs/index";
|
||||
import loggerConfig from "./logger/index";
|
||||
const pkg = JSON.parse(readFileSync("./package.json", {encoding: "utf8"}));
|
||||
|
||||
export const config: Partial<TsED.Configuration> = {
|
||||
version: pkg.version,
|
||||
envs,
|
||||
logger: loggerConfig,
|
||||
graphql: {
|
||||
default: {
|
||||
path: "/graphql",
|
||||
buildSchemaOptions: {
|
||||
}
|
||||
}
|
||||
},
|
||||
// additional shared configuration
|
||||
};
|
||||
@@ -1,24 +0,0 @@
|
||||
import {$log, PlatformLoggerSettings} from "@tsed/common";
|
||||
import {isProduction} from "../envs/index";
|
||||
|
||||
if (isProduction) {
|
||||
$log.appenders.set("stdout", {
|
||||
type: "stdout",
|
||||
levels: ["info", "debug"],
|
||||
layout: {
|
||||
type: "json"
|
||||
}
|
||||
});
|
||||
|
||||
$log.appenders.set("stderr", {
|
||||
levels: ["trace", "fatal", "error", "warn"],
|
||||
type: "stderr",
|
||||
layout: {
|
||||
type: "json"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export default <PlatformLoggerSettings> {
|
||||
disableRoutesSummary: isProduction
|
||||
};
|
||||
@@ -1,10 +0,0 @@
|
||||
import {Controller} from "@tsed/di";
|
||||
import {Get} from "@tsed/schema";
|
||||
|
||||
@Controller("/hello-world")
|
||||
export class HelloWorldController {
|
||||
@Get("/")
|
||||
get() {
|
||||
return "hello";
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
/**
|
||||
* @file Automatically generated by barrelsby.
|
||||
*/
|
||||
|
||||
export * from "./HelloWorldController";
|
||||
@@ -1,18 +0,0 @@
|
||||
import {DataSourceService} from "@tsed/typegraphql";
|
||||
import {RESTDataSource} from "apollo-datasource-rest";
|
||||
|
||||
@DataSourceService()
|
||||
export class MyDataSource extends RESTDataSource {
|
||||
constructor() {
|
||||
super();
|
||||
this.baseURL = "http://localhost:8001";
|
||||
}
|
||||
|
||||
willSendRequest(request: any) {
|
||||
request.headers.set("Authorization", this.context.token);
|
||||
}
|
||||
|
||||
getMyData(id: string) {
|
||||
return this.get(`/rest/calendars/${id}`);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
/**
|
||||
* @file Automatically generated by barrelsby.
|
||||
*/
|
||||
|
||||
export * from "./MyDataSource";
|
||||
@@ -1,18 +0,0 @@
|
||||
import {$log} from "@tsed/common";
|
||||
import { PlatformExpress } from "@tsed/platform-express";
|
||||
import {Server} from "./Server";
|
||||
|
||||
async function bootstrap() {
|
||||
try {
|
||||
const platform = await PlatformExpress.bootstrap(Server);
|
||||
await platform.listen();
|
||||
|
||||
process.on("SIGINT", () => {
|
||||
platform.stop();
|
||||
});
|
||||
} catch (error) {
|
||||
$log.error({event: "SERVER_BOOTSTRAP_ERROR", message: error.message, stack: error.stack});
|
||||
}
|
||||
}
|
||||
|
||||
bootstrap();
|
||||
8
backend/src/main.ts
Normal file
8
backend/src/main.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
await app.listen(3000);
|
||||
}
|
||||
bootstrap();
|
||||
@@ -1,7 +0,0 @@
|
||||
/**
|
||||
* @file Automatically generated by barrelsby.
|
||||
*/
|
||||
|
||||
export * from "./recipes/Recipe";
|
||||
export * from "./recipes/RecipeNotFoundError";
|
||||
export * from "./recipes/RecipeResolver";
|
||||
@@ -1,27 +0,0 @@
|
||||
import {Field, ID, ObjectType} from "type-graphql";
|
||||
|
||||
@ObjectType({description: "Object representing cooking recipe"})
|
||||
export class Recipe {
|
||||
@Field((type) => ID)
|
||||
id: string;
|
||||
|
||||
@Field()
|
||||
title: string;
|
||||
|
||||
@Field({nullable: true})
|
||||
description?: string;
|
||||
|
||||
@Field()
|
||||
creationDate: Date;
|
||||
|
||||
@Field((type) => [String])
|
||||
ingredients: string[];
|
||||
|
||||
constructor(options: Partial<Recipe> = {}) {
|
||||
options.id && (this.id = options.id);
|
||||
options.title && (this.title = options.title);
|
||||
options.description && (this.description = options.description);
|
||||
options.creationDate && (this.creationDate = options.creationDate);
|
||||
options.ingredients && (this.ingredients = options.ingredients);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
import {NotFound} from "@tsed/exceptions";
|
||||
|
||||
export class RecipeNotFoundError extends NotFound {
|
||||
constructor(private id: string) {
|
||||
super("Recipe not found");
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
import {ResolverService} from "@tsed/typegraphql";
|
||||
import {Arg, Query} from "type-graphql";
|
||||
import {RecipeService} from "../../services/RecipeService";
|
||||
import {Recipe} from "./Recipe";
|
||||
import {RecipeNotFoundError} from "./RecipeNotFoundError";
|
||||
|
||||
@ResolverService(Recipe)
|
||||
export class RecipeResolver {
|
||||
constructor(private recipeService: RecipeService) {}
|
||||
|
||||
@Query((returns) => Recipe)
|
||||
async recipe(@Arg("id") id: string) {
|
||||
const recipe = await this.recipeService.findById(id);
|
||||
|
||||
if (recipe === undefined) {
|
||||
throw new RecipeNotFoundError(id);
|
||||
}
|
||||
|
||||
return recipe;
|
||||
}
|
||||
|
||||
@Query((returns) => [Recipe], {description: "Get all the recipes from around the world "})
|
||||
async recipes(): Promise<Recipe[]> {
|
||||
return this.recipeService.findAll({});
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
import {Injectable} from "@tsed/di";
|
||||
import {Recipe} from "../resolvers";
|
||||
|
||||
@Injectable()
|
||||
export class RecipeService {
|
||||
async findById(id: string) {
|
||||
return new Recipe({id})
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
async findAll(query: any) {
|
||||
return []
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user