Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tsoa #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions __tests__/unit/modules/example/example.controller.test.ts

This file was deleted.

3,599 changes: 2,199 additions & 1,400 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
"scripts": {
"dev": "ts-node src/server.ts",
"start": "ts-node dist/src/server.js",
"build": "tsc -p . -w > /dev/null 2>&1 &",
"build": "tsc -p . -w > /dev/null 2>&1 & tsoa spec-and-routes",
"lint": "eslint . --ext .ts",
"test": "jest",
"test:unit": "NODE_ENV=test jest --testPathPattern=\"__tests__/unit/*/*/\"",
"test:e2e": "NODE_ENV=test jest --testPathPattern=\"__tests__/e2e/*/*/\"",
"prepare": "husky install",
"clean" : "npx depcheck"
"clean": "npx depcheck",
"routes": "tsoa spec-and-routes"
},
"author": "Mustafa Erdem Köşk",
"license": "ISC",
Expand All @@ -32,6 +33,7 @@
"swagger-jsdoc": "^6.1.0",
"swagger-ui-express": "^4.6.2",
"ts-node": "^8.4.1",
"tsoa": "^6.1.5",
"tsyringe": "^4.7.0",
"typescript": "^4.9.5",
"winston": "^3.8.2"
Expand Down
44 changes: 30 additions & 14 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import * as express from 'express';
import 'express-async-errors';
import { Application } from 'express';
import * as swaggerUi from 'swagger-ui-express';
import { swaggerSpec } from './util/swagger.util';
import express, {
Application,
Response as ExResponse,
Request as ExRequest,
} from 'express';
import swaggerUi from 'swagger-ui-express';
import Logger from './util/logger.util'
import { RegisterRoutes } from '../dist/routes';
// import { container } from './ioc';

class App {
public app: Application;

public port: number;

constructor(
appInit: { port: number; earlyMiddlewares: any; lateMiddlewares: any; routes: any; },
appInit: { port: number; earlyMiddlewares: any; lateMiddlewares: any;},
) {
this.app = express();
this.port = appInit.port;

this.modules();
this.middlewares(appInit.earlyMiddlewares);
this.routes(appInit.routes);
this.routes();
this.middlewares(appInit.lateMiddlewares);
this.assets();
this.template();
this.swagger();
}

private middlewares(middlewares: { forEach: (arg0: (middleware: any) => void) => void; }) {
Expand All @@ -30,14 +36,6 @@ class App {
});
}

private routes(routes: { forEach: (arg0: (route: any) => void) => void; }) {
routes.forEach((route) => {
this.app.use('/', route.router);
});

this.app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
}

// eslint-disable-next-line @typescript-eslint/no-empty-function
private assets() {
}
Expand All @@ -46,6 +44,24 @@ class App {
private template() {
}

private modules() {
// container.resolve<IMongoLoader>('IMongoLoader').ConnectWithRetry();
}

private routes() {
RegisterRoutes(this.app);
}

private swagger() {
this.app.use(
'/swagger',
swaggerUi.serve,
async (_req: ExRequest, res: ExResponse) => res.send(
swaggerUi.generateHTML(await import('../dist/swagger.json')),
),
);
}

public listen() {
this.app.listen(this.port, () => {
Logger.info(`App listening on the http://localhost:${this.port}`);
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions src/interfaces/repository/IExampleRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ExampleRequest, ExampleResponse } from '../../modules/example/dtos/Example';

export interface IExampleRepository {
getExample(id:number): Promise<ExampleResponse>;
}
5 changes: 5 additions & 0 deletions src/interfaces/service/IExampleService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ExampleRequest, ExampleResponse } from '../../modules/example/dtos/Example';

export interface IExampleService {
getExample(id:number): Promise<ExampleResponse>;
}
15 changes: 15 additions & 0 deletions src/ioc/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { IocContainer } from '@tsoa/runtime';
import { container } from 'tsyringe';

import ExampleRepository from '../modules/example/ExampleRepository';
import ExampleService from '../modules/example/ExampleService';

container.register('IExampleService', { useClass: ExampleService });
container.register('IExampleRepository', { useClass: ExampleRepository });

// eslint-disable-next-line max-len
export const iocContainer: IocContainer = { get: <T>(controller: { prototype: T }): T => container.resolve<T>(controller as never) };

export { container };

export default iocContainer;
33 changes: 33 additions & 0 deletions src/modules/example/ExampleController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { inject, injectable } from 'tsyringe';
import {
Body,
Controller,
Get,
Post,
Put,
Request,
Route,
Security,
Tags,
Response,
} from 'tsoa';
import { IExampleService } from '../../interfaces/service/IExampleService';
import { ExampleRequest, ExampleResponse } from './dtos/Example';

@injectable()
@Route('example')
@Tags('ExampleController')
export class ExampleController extends Controller {
constructor(
@inject('IExampleService') private exampleService: IExampleService,
) {
super();
}

@Get('/{id}')
get(id: number): Promise<ExampleResponse> {
return this.exampleService.getExample(id);
}
}

export default { ExampleController };
23 changes: 23 additions & 0 deletions src/modules/example/ExampleRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { IExampleRepository } from '../../interfaces/repository/IExampleRepository';
import { ExampleRequest, ExampleResponse, Example } from './dtos/Example';

export default class ExampleRepository implements IExampleRepository {
getExample(id:number): Promise<ExampleResponse> {
const examples: Example[] = [{
id: 1,
name: 'erdem',
},
{
id: 2,
name: 'kosk',
}];

const foundExample = examples.find((example) => example.id === id);

if (!foundExample) {
return Promise.reject(new Error('Example not found'));
}

return Promise.resolve(foundExample);
}
}
28 changes: 28 additions & 0 deletions src/modules/example/ExampleService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { inject, injectable } from 'tsyringe';
import { ExampleRequest, ExampleResponse } from './dtos/Example';

import { IExampleService } from '../../interfaces/service/IExampleService';
import { IExampleRepository } from '../../interfaces/repository/IExampleRepository';

import { ERROR_CLASSES } from '../../util/error.util';

@injectable()
export default class ExampleService implements IExampleService {
constructor(
@inject('IExampleRepository')
private repository: IExampleRepository,
) {
console.log('Example Service Loaded!')
}

async getExample(id:number): Promise<ExampleResponse> {
let example;
try {
example = await this.repository.getExample(id);
} catch (error) {
throw new ERROR_CLASSES.ExampleError();
}

return example;
}
}
13 changes: 13 additions & 0 deletions src/modules/example/dtos/Example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export type Example = {
id: number;
name: string;
};

export type ExampleResponse = {
id: number;
name: string;
};

export type ExampleRequest = {
id: number;
};
19 changes: 0 additions & 19 deletions src/modules/example/example.controller.ts

This file was deleted.

14 changes: 0 additions & 14 deletions src/modules/example/example.repository.ts

This file was deleted.

50 changes: 0 additions & 50 deletions src/modules/example/example.route.ts

This file was deleted.

23 changes: 0 additions & 23 deletions src/modules/example/example.service.ts

This file was deleted.

7 changes: 2 additions & 5 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import * as bodyParser from 'body-parser';
import * as cors from 'cors';
import bodyParser from 'body-parser';
import cors from 'cors';
import helmet from 'helmet';
import { errors } from 'celebrate';
import config from './config';
import App from './app';
import loggerMiddleware from './middleware/logger.middleware';
import errorMiddleware from './middleware/error.middleware';

import { ContainerLogic } from './logic/container.logic';

import('express-async-errors');

const app = new App({
Expand All @@ -20,7 +18,6 @@ const app = new App({
cors(),
helmet(),
],
routes: ContainerLogic.getRouteClasses(),
lateMiddlewares: [
errors({ statusCode: 422 }),
errorMiddleware,
Expand Down
Loading