Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil.mysliwiec committed Feb 4, 2017
1 parent 73997f5 commit d3abc2d
Show file tree
Hide file tree
Showing 152 changed files with 3,496 additions and 280 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

# IDE
/.idea
/.awcache

# misc
npm-debug.log
npm-debug.log

# example
/.example
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(The MIT License)

Copyright (c) 2017 Kamil Myśliwiec <http://kamilmysliwiec.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 changes: 27 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[![Nest Logo](http://kamilmysliwiec.com/public/nest-logo.png)](http://kamilmysliwiec.com/)

Modern, fast, powerful web framework for [node](http://nodejs.org).

[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]
[![Linux Build][travis-image]][travis-url]
[![Windows Build][appveyor-image]][appveyor-url]

## License

[MIT](LICENSE)

[npm-image]: https://img.shields.io/npm/v/nest.js.svg
[npm-url]: https://npmjs.org/package/nest.js
[downloads-image]: https://img.shields.io/npm/dm/nest.js.svg
[downloads-url]: https://npmjs.org/package/nest.js
[travis-image]: https://img.shields.io/travis/nest.jsjs/nest.js/master.svg?label=linux
[travis-url]: https://travis-ci.org/nest.jsjs/nest.js
[appveyor-image]: https://img.shields.io/appveyor/ci/dougwilson/nest.js/master.svg?label=windows
[appveyor-url]: https://ci.appveyor.com/project/dougwilson/nest.js
[coveralls-image]: https://img.shields.io/coveralls/nest.jsjs/nest.js/master.svg
[coveralls-url]: https://coveralls.io/r/nest.jsjs/nest.js?branch=master
[gratipay-image-visionmedia]: https://img.shields.io/gratipay/visionmedia.svg
[gratipay-url-visionmedia]: https://gratipay.com/visionmedia/
[gratipay-image-dougwilson]: https://img.shields.io/gratipay/dougwilson.svg
[gratipay-url-dougwilson]: https://gratipay.com/dougwilson/
19 changes: 19 additions & 0 deletions example/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ExpressConfig } from "./config";
import { PassportJWTConfig } from "./config/passport-jwt.config";
import { NestApplication } from "./../src/";

export class Application implements NestApplication {

constructor(private app) {
ExpressConfig.setupConfig(this.app);
PassportJWTConfig.setupConfig(this.app);
}

public start() {
console.log("star t");
this.app.listen(3030, () => {
console.log("Nest Application listen on port:", 3030);
});
}

}
3 changes: 3 additions & 0 deletions example/config/app.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class ApplicationConfig {

}
21 changes: 21 additions & 0 deletions example/config/express.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Application } from "express";
import * as bodyParser from "body-parser";
import * as logger from "morgan";

export class ExpressConfig {

static setupConfig(app: Application) {
app.use(logger("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// Add headers
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
next();
});
}

}
3 changes: 3 additions & 0 deletions example/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./app.config";
export * from "./express.config";
export * from "./passport-jwt.config";
45 changes: 45 additions & 0 deletions example/config/passport-jwt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as _ from "lodash";
import { Application } from "express";
import * as passport from "passport";
import { ExtractJwt, Strategy, StrategyOptions } from "passport-jwt";

var users = [
{
id: 1,
name: 'jonathanmh',
password: '%2yx4'
},
{
id: 2,
name: 'test',
password: 'test'
}
];

export class PassportJWTConfig {
static readonly secretKey = "XD";

static readonly jwtOptions: StrategyOptions = {
jwtFromRequest: ExtractJwt.fromAuthHeader(),
secretOrKey: PassportJWTConfig.secretKey,
};

static setupConfig(app: Application) {
this.init();
app.use(passport.initialize());
}

static init() {

var strategy = new Strategy(this.jwtOptions, (payload, next) => {
console.log('payload received', payload);

var user = users[_.findIndex(users, {id: payload.id})];
console.log(user);
next(null, user || false);
});

passport.use(strategy);
}

}
10 changes: 10 additions & 0 deletions example/models/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as mongoose from "mongoose";
mongoose.connect("mongodb://localhost:27017/tracker", {
server: {
socketOptions: {
socketTimeoutMS: 0,
connectTimeoutMS: 0
}
}
});
export { mongoose as db };
13 changes: 13 additions & 0 deletions example/models/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as mongoose from "mongoose";
import { db } from "./db";

interface IUser extends mongoose.Document {
name: string;
heh: any;
}

type UserType = IUser & mongoose.Document;

export const User = db.model<UserType>('User', new mongoose.Schema({
name : {type : String, required : true},
}));
13 changes: 13 additions & 0 deletions example/modules/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { UsersModule } from "./users/users.module";
import { AuthModule } from "./auth/auth.module";
import { Module } from "./../../src/";

@Module({
modules: [
UsersModule,
AuthModule
]
})
export class ApplicationModule {
configure() {}
}
19 changes: 19 additions & 0 deletions example/modules/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { AuthRoute } from "./login.route";
import { SharedModule } from "../shared.module";
import { Module } from "./../../../src/";

@Module({
modules: [
SharedModule,
],
controllers: [
AuthRoute
],
components: [
]
})
export class AuthModule {
configure() {
console.log("auth configured");
}
}
45 changes: 45 additions & 0 deletions example/modules/auth/login.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as _ from "lodash";
import * as jwt from "jsonwebtoken";
import { Request, Response, NextFunction } from "express";
import { PassportJWTConfig } from "../../config/passport-jwt.config";
import { Controller, RequestMethod, RequestMapping } from "./../../../src/";

var users = [
{
id: 1,
name: 'jonathanmh',
password: '%2yx4'
},
{
id: 2,
name: 'test',
password: 'test'
}
];

@Controller({ path: "login" })
export class AuthRoute {

constructor() {}

@RequestMapping({ path: "/", method: RequestMethod.POST })
fetchToken(req: Request, res: Response, next: NextFunction) {
const { name, password } = req.body;

const user = users[_.findIndex(users, {name: name})];
if (!user){
res.status(401).json({message:"no such user found"});
}

if(user.password === password) {
const payload = {id: user.id};
const token = jwt.sign(payload, PassportJWTConfig.secretKey);
res.json({message: "ok", token: token});
}
else {
res.status(401).json({message:"passwords did not match"});
}
}

}

15 changes: 15 additions & 0 deletions example/modules/shared.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

import { SharedService } from "./shared.service";
import { Module } from "./../../src/";

@Module({
components: [
SharedService,
],
exports: [
SharedService,
]
})
export class SharedModule {
configure() {}
}
12 changes: 12 additions & 0 deletions example/modules/shared.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Component } from "./../../src/";
import { Subject } from "rxjs";

@Component()
export class SharedService {
public stream$ = new Subject<string>();
constructor() {
setTimeout(() => {
this.stream$.next("XDDD");
}, 3000);
}
}
34 changes: 34 additions & 0 deletions example/modules/users/users-query.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//import { User } from "../../models/user";
import { SharedService } from "../shared.service";
import { Component, RequestMapping } from "./../../../src/";
import { UsersGateway } from "./users.gateway";

import { Subject } from "rxjs";

@Component()
export class UsersQueryService {

public stream$ = new Subject<string>();
static get dependencies() {
return [ UsersGateway, SharedService ];
}

constructor(private usersGateway: UsersGateway, private shared: SharedService) {
this.shared.stream$.subscribe((xd) => {
console.log("ONCE");
this.stream$.next(xd);
});
}
/*
getAllUsers(): Promise<any> {
return new Promise((resolve) => {
User.find((err, res) => {
if(err) {
throw new Error(err);
}
resolve(res);
});
});
}*/
}
31 changes: 31 additions & 0 deletions example/modules/users/users-sec.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Request, Response, NextFunction } from "express";
import { RequestMapping, RequestMethod} from "./../../../src/";
import { UsersQueryService } from "./users-query.service";
import { Controller } from "./../../../src/";

@Controller({ path: "users" })
export class UsersSecRoute {

constructor(private usersQueryService: UsersQueryService) {
this.usersQueryService.stream$.subscribe((xd) => {
console.log(xd);
});
}

/*@RequestMapping({
path: "/",
method: RequestMethod.GET
})
async getAllUsers(req: Request, res: Response, next: NextFunction) {
try {
const users = await this.usersQueryService.getAllUsers();
res.status(201).json(users);
next();
}
catch(e) {
next(e.message);
}
}*/

}

Loading

0 comments on commit d3abc2d

Please sign in to comment.