Initial commit
This commit is contained in:
commit
0aabccf76a
|
|
@ -0,0 +1,17 @@
|
||||||
|
# dependencies
|
||||||
|
/node_modules
|
||||||
|
|
||||||
|
# IDE
|
||||||
|
/.idea
|
||||||
|
/.awcache
|
||||||
|
|
||||||
|
# misc
|
||||||
|
npm-debug.log
|
||||||
|
|
||||||
|
# example
|
||||||
|
/quick-start
|
||||||
|
|
||||||
|
# tests
|
||||||
|
/test
|
||||||
|
/coverage
|
||||||
|
/.nyc_output
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
require('ts-node/register');
|
||||||
|
require('./src/server');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
{
|
||||||
|
"name": "nest-typescript-starter",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Nest TypeScript starter repository",
|
||||||
|
"license": "ISC",
|
||||||
|
"scripts": {
|
||||||
|
"start": "node index.js"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"nest.js": "*",
|
||||||
|
"typescript": "^2.2.1"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^7.0.5",
|
||||||
|
"ts-node": "^2.1.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
import { NestApplication } from 'nest.js';
|
||||||
|
|
||||||
|
export class Application implements NestApplication {
|
||||||
|
constructor(private expressApp) {}
|
||||||
|
|
||||||
|
start() {
|
||||||
|
// do something before server start
|
||||||
|
this.expressApp.listen(3031, () => {
|
||||||
|
console.log('Application listen on port:', 3031);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
import { Module } from 'nest.js';
|
||||||
|
import { UsersModule } from './users/users.module';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
modules: [ UsersModule ]
|
||||||
|
})
|
||||||
|
export class ApplicationModule {}
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
import { UsersService } from "./users.service";
|
||||||
|
import { NestMiddleware, HttpException, Middleware } from 'nest.js';
|
||||||
|
|
||||||
|
@Middleware()
|
||||||
|
export class AuthMiddleware implements NestMiddleware {
|
||||||
|
|
||||||
|
constructor(private usersService: UsersService) {}
|
||||||
|
|
||||||
|
resolve() {
|
||||||
|
return (req, res, next) => {
|
||||||
|
const userName = req.headers["x-access-token"];
|
||||||
|
const users = this.usersService.getUsers();
|
||||||
|
|
||||||
|
const user = users.find((user) => user.name === userName);
|
||||||
|
if (!user) {
|
||||||
|
throw new HttpException('User not found.', 401);
|
||||||
|
}
|
||||||
|
req.user = user;
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
import { Subject } from 'rxjs/Subject';
|
||||||
|
import { SocketGateway, GatewayServer, SubscribeMessage } from 'nest.js/socket';
|
||||||
|
|
||||||
|
@SocketGateway({ port: 2000 })
|
||||||
|
export class ChatGateway {
|
||||||
|
private msg$ = new Subject<any>();
|
||||||
|
|
||||||
|
@GatewayServer server;
|
||||||
|
|
||||||
|
get msgStream() {
|
||||||
|
return this.msg$.asObservable();
|
||||||
|
}
|
||||||
|
|
||||||
|
@SubscribeMessage({ value: 'message' })
|
||||||
|
onMessage(client, data) {
|
||||||
|
this.msg$.next({ client, data });
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
import { ChatGateway } from './chat.gateway';
|
||||||
|
import { Component } from 'nest.js';
|
||||||
|
|
||||||
|
@Component()
|
||||||
|
export class ChatService {
|
||||||
|
|
||||||
|
constructor(private chatGateway: ChatGateway) {
|
||||||
|
const stream$ = this.chatGateway.msgStream;
|
||||||
|
stream$.subscribe(this.storeMessage.bind(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
storeMessage(data) {
|
||||||
|
// store data
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
import { Component } from 'nest.js';
|
||||||
|
|
||||||
|
@Component()
|
||||||
|
export class NotificationService {
|
||||||
|
storeNotification(data) {
|
||||||
|
const notification = this.mapDataToNotification(data);
|
||||||
|
// store notification
|
||||||
|
}
|
||||||
|
|
||||||
|
private mapDataToNotification(msg) {}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
import { UsersService } from "./users.service";
|
||||||
|
import { Controller, RequestMapping, RequestMethod } from 'nest.js';
|
||||||
|
|
||||||
|
@Controller({ path: 'users' })
|
||||||
|
export class UsersController {
|
||||||
|
|
||||||
|
constructor(private usersService: UsersService) {}
|
||||||
|
|
||||||
|
@RequestMapping()
|
||||||
|
async getAllUsers(req, res) {
|
||||||
|
const users = await this.usersService.getAllUsers();
|
||||||
|
res.status(200).json(users);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping({ path: '/:id' })
|
||||||
|
async getUser(req, res) {
|
||||||
|
const user = await this.usersService.getUser(req.params.id);
|
||||||
|
res.status(200).json(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping({ method: RequestMethod.POST })
|
||||||
|
async addUser(req, res) {
|
||||||
|
const msg = await this.usersService.getUser(req.body.user);
|
||||||
|
res.status(201).json(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
import { Module, MiddlewareBuilder } from 'nest.js';
|
||||||
|
import { UsersController } from './users.controller';
|
||||||
|
import { UsersService } from './users.service';
|
||||||
|
import { AuthMiddleware } from './auth.middleware';
|
||||||
|
import { ChatGateway } from './chat.gateway';
|
||||||
|
import { ChatService } from './chat.service';
|
||||||
|
import { NotificationService } from './notification.service';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
controllers: [ UsersController ],
|
||||||
|
components: [ UsersService, ChatGateway, ChatService, NotificationService ],
|
||||||
|
exports: [ UsersService ],
|
||||||
|
})
|
||||||
|
export class UsersModule {
|
||||||
|
configure(builder: MiddlewareBuilder) {
|
||||||
|
builder.use({
|
||||||
|
middlewares: [ AuthMiddleware ],
|
||||||
|
forRoutes: [ UsersController ],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,33 @@
|
||||||
|
import { Component, HttpException } from 'nest.js';
|
||||||
|
|
||||||
|
@Component()
|
||||||
|
export class UsersService {
|
||||||
|
|
||||||
|
private users = [
|
||||||
|
{ id: 1, name: 'John Doe' },
|
||||||
|
{ id: 2, name: 'Alice Caeiro' },
|
||||||
|
{ id: 3, name: 'Who Knows' },
|
||||||
|
];
|
||||||
|
|
||||||
|
getUsers() {
|
||||||
|
return this.users;
|
||||||
|
}
|
||||||
|
|
||||||
|
getAllUsers() {
|
||||||
|
return Promise.resolve(this.users);
|
||||||
|
}
|
||||||
|
|
||||||
|
getUser(id: string) {
|
||||||
|
const user = this.users.find((user) => user.id === +id);
|
||||||
|
if (!user) {
|
||||||
|
throw new HttpException('User not found', 404);
|
||||||
|
}
|
||||||
|
return Promise.resolve(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
addUser(user) {
|
||||||
|
this.users.push(user);
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
import { NestRunner } from 'nest.js';
|
||||||
|
import { Application } from './app';
|
||||||
|
import { ApplicationModule } from './modules/app.module';
|
||||||
|
|
||||||
|
NestRunner.run(Application, ApplicationModule);
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"module": "commonjs",
|
||||||
|
"declaration": false,
|
||||||
|
"noImplicitAny": false,
|
||||||
|
"removeComments": true,
|
||||||
|
"noLib": false,
|
||||||
|
"emitDecoratorMetadata": true,
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"target": "es6",
|
||||||
|
"sourceMap": true,
|
||||||
|
"allowJs": true
|
||||||
|
},
|
||||||
|
"exclude": [
|
||||||
|
"node_modules"
|
||||||
|
]
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue