Update to RC
This commit is contained in:
parent
f52644426d
commit
3b40dfc28d
|
|
@ -1,7 +1,6 @@
|
|||
import { Module } from 'nest.js';
|
||||
import { UsersModule } from './users/users.module';
|
||||
|
||||
@Module({
|
||||
modules: [ UsersModule ]
|
||||
modules: []
|
||||
})
|
||||
export class ApplicationModule {}
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
import { Subject } from 'rxjs/Subject';
|
||||
import { WebSocketGateway, WebSocketServer, SubscribeMessage } from 'nest.js/websockets';
|
||||
|
||||
@WebSocketGateway({ port: 2000 })
|
||||
export class ChatGateway {
|
||||
private msg$ = new Subject<any>();
|
||||
|
||||
@WebSocketServer()
|
||||
server;
|
||||
|
||||
get msgStream() {
|
||||
return this.msg$.asObservable();
|
||||
}
|
||||
|
||||
@SubscribeMessage({ value: 'message' })
|
||||
onMessage(client, data) {
|
||||
this.msg$.next({ client, data });
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
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
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
import { Component } from 'nest.js';
|
||||
|
||||
@Component()
|
||||
export class NotificationService {
|
||||
storeNotification(data) {
|
||||
const notification = this.mapDataToNotification(data);
|
||||
// store notification
|
||||
}
|
||||
|
||||
private mapDataToNotification(msg) {}
|
||||
}
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
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 ],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
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();
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue