Initial commit

This commit is contained in:
kamil.mysliwiec 2017-02-25 15:13:01 +01:00
commit 0aabccf76a
14 changed files with 241 additions and 0 deletions

17
.gitignore vendored Normal file
View File

@ -0,0 +1,17 @@
# dependencies
/node_modules
# IDE
/.idea
/.awcache
# misc
npm-debug.log
# example
/quick-start
# tests
/test
/coverage
/.nyc_output

14
index.js Normal file
View File

@ -0,0 +1,14 @@
require('ts-node/register');
require('./src/server');

17
package.json Normal file
View File

@ -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"
}
}

12
src/app.ts Normal file
View File

@ -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);
});
}
}

View File

@ -0,0 +1,7 @@
import { Module } from 'nest.js';
import { UsersModule } from './users/users.module';
@Module({
modules: [ UsersModule ]
})
export class ApplicationModule {}

View File

@ -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();
}
}
}

View File

@ -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 });
}
}

View File

@ -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
}
}

View File

@ -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) {}
}

View File

@ -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);
}
}

View File

@ -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 ],
})
}
}

View File

@ -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();
}
}

5
src/server.ts Normal file
View File

@ -0,0 +1,5 @@
import { NestRunner } from 'nest.js';
import { Application } from './app';
import { ApplicationModule } from './modules/app.module';
NestRunner.run(Application, ApplicationModule);

17
tsconfig.json Normal file
View File

@ -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"
]
}