├── .gitignore
├── CHANGELOG.md
├── CONTRIBUTING.md
├── ISSUE_TEMPLATE.md
├── LICENSE
├── PULL_REQUEST-TEMPLATE.md
├── README.md
├── TODO.md
├── generators
├── app
│ ├── USAGE
│ ├── app-types.js
│ ├── index.js
│ └── templates
│ │ ├── .gitignore
│ │ ├── 01-cats-app
│ │ ├── .gitignore
│ │ ├── README.md
│ │ ├── e2e
│ │ │ ├── cats
│ │ │ │ └── cats.e2e-spec.ts
│ │ │ └── jest-e2e.json
│ │ ├── index.js
│ │ ├── jest.json
│ │ ├── package.json
│ │ ├── src
│ │ │ ├── modules
│ │ │ │ ├── app.module.ts
│ │ │ │ ├── cats
│ │ │ │ │ ├── cats.controller.spec.ts
│ │ │ │ │ ├── cats.controller.ts
│ │ │ │ │ ├── cats.module.ts
│ │ │ │ │ ├── cats.service.ts
│ │ │ │ │ ├── dto
│ │ │ │ │ │ └── create-cat.dto.ts
│ │ │ │ │ └── interfaces
│ │ │ │ │ │ └── cat.interface.ts
│ │ │ │ └── common
│ │ │ │ │ ├── decorators
│ │ │ │ │ └── roles.decorator.ts
│ │ │ │ │ ├── exceptions
│ │ │ │ │ └── forbidden.exception.ts
│ │ │ │ │ ├── filters
│ │ │ │ │ └── http-exception.filter.ts
│ │ │ │ │ ├── guards
│ │ │ │ │ └── roles.guard.ts
│ │ │ │ │ ├── interceptors
│ │ │ │ │ ├── cache.interceptor.ts
│ │ │ │ │ ├── exception.interceptor.ts
│ │ │ │ │ ├── logging.interceptor.ts
│ │ │ │ │ ├── mixin-cache.interceptor.ts
│ │ │ │ │ └── transform.interceptor.ts
│ │ │ │ │ ├── middlewares
│ │ │ │ │ └── logger.middleware.ts
│ │ │ │ │ └── pipes
│ │ │ │ │ ├── parse-int.pipe.ts
│ │ │ │ │ └── validation.pipe.ts
│ │ │ └── server.ts
│ │ ├── tsconfig.json
│ │ └── tslint.json
│ │ ├── 02-gateways
│ │ ├── .gitignore
│ │ ├── README.md
│ │ ├── client
│ │ │ ├── index.html
│ │ │ ├── socket.io.js
│ │ │ └── ws.html
│ │ ├── index.js
│ │ ├── package.json
│ │ ├── src
│ │ │ ├── modules
│ │ │ │ ├── app.module.ts
│ │ │ │ ├── common
│ │ │ │ │ ├── adapters
│ │ │ │ │ │ └── ws-adapter.ts
│ │ │ │ │ ├── decorators
│ │ │ │ │ │ └── roles.decorator.ts
│ │ │ │ │ ├── filters
│ │ │ │ │ │ └── ws-exception.filter.ts
│ │ │ │ │ ├── guards
│ │ │ │ │ │ └── roles.guard.ts
│ │ │ │ │ ├── interceptors
│ │ │ │ │ │ ├── cache.interceptor.ts
│ │ │ │ │ │ ├── logging.interceptor.ts
│ │ │ │ │ │ └── transform.interceptor.ts
│ │ │ │ │ └── pipes
│ │ │ │ │ │ └── validation.pipe.ts
│ │ │ │ └── events
│ │ │ │ │ ├── events.gateway.ts
│ │ │ │ │ └── events.module.ts
│ │ │ └── server.ts
│ │ ├── tsconfig.json
│ │ └── tslint.json
│ │ ├── 03-microservices
│ │ ├── .gitignore
│ │ ├── README.md
│ │ ├── index.js
│ │ ├── package.json
│ │ ├── src
│ │ │ ├── modules
│ │ │ │ ├── app.module.ts
│ │ │ │ ├── common
│ │ │ │ │ ├── adapters
│ │ │ │ │ │ ├── rabbitmq-client.ts
│ │ │ │ │ │ └── rabbitmq-server.ts
│ │ │ │ │ ├── decorators
│ │ │ │ │ │ └── roles.decorator.ts
│ │ │ │ │ ├── filters
│ │ │ │ │ │ └── rpc-exception.filter.ts
│ │ │ │ │ ├── guards
│ │ │ │ │ │ └── roles.guard.ts
│ │ │ │ │ ├── interceptors
│ │ │ │ │ │ ├── cache.interceptor.ts
│ │ │ │ │ │ ├── logging.interceptor.ts
│ │ │ │ │ │ └── transform.interceptor.ts
│ │ │ │ │ └── pipes
│ │ │ │ │ │ └── validation.pipe.ts
│ │ │ │ └── math
│ │ │ │ │ ├── math.controller.ts
│ │ │ │ │ └── math.module.ts
│ │ │ └── server.ts
│ │ ├── tsconfig.json
│ │ └── tslint.json
│ │ ├── 04-injector
│ │ ├── .gitignore
│ │ ├── README.md
│ │ ├── index.js
│ │ ├── package.json
│ │ ├── src
│ │ │ ├── modules
│ │ │ │ ├── app.module.ts
│ │ │ │ ├── common
│ │ │ │ │ ├── common.module.ts
│ │ │ │ │ └── common.service.ts
│ │ │ │ ├── core
│ │ │ │ │ ├── context.service.ts
│ │ │ │ │ ├── core.module.ts
│ │ │ │ │ └── core.service.ts
│ │ │ │ └── feature
│ │ │ │ │ ├── feature.module.ts
│ │ │ │ │ └── feature.service.ts
│ │ │ └── server.ts
│ │ ├── tsconfig.json
│ │ └── tslint.json
│ │ ├── 05-sql-typeorm
│ │ ├── .gitignore
│ │ ├── README.md
│ │ ├── index.js
│ │ ├── ormconfig.json
│ │ ├── package.json
│ │ ├── src
│ │ │ ├── modules
│ │ │ │ ├── app.module.ts
│ │ │ │ └── photo
│ │ │ │ │ ├── photo.controller.ts
│ │ │ │ │ ├── photo.entity.ts
│ │ │ │ │ ├── photo.module.ts
│ │ │ │ │ └── photo.service.ts
│ │ │ └── server.ts
│ │ ├── tsconfig.json
│ │ └── tslint.json
│ │ ├── 06-mongoose
│ │ ├── .gitignore
│ │ ├── README.md
│ │ ├── index.js
│ │ ├── package.json
│ │ ├── src
│ │ │ ├── modules
│ │ │ │ ├── app.module.ts
│ │ │ │ ├── cats
│ │ │ │ │ ├── cats.controller.ts
│ │ │ │ │ ├── cats.module.ts
│ │ │ │ │ ├── cats.providers.ts
│ │ │ │ │ ├── cats.service.ts
│ │ │ │ │ ├── dto
│ │ │ │ │ │ └── create-cat.dto.ts
│ │ │ │ │ ├── interfaces
│ │ │ │ │ │ └── cat.interface.ts
│ │ │ │ │ └── schemas
│ │ │ │ │ │ └── cat.schema.ts
│ │ │ │ └── database
│ │ │ │ │ ├── database.module.ts
│ │ │ │ │ └── database.providers.ts
│ │ │ └── server.ts
│ │ ├── tsconfig.json
│ │ └── tslint.json
│ │ ├── 07-sequelize
│ │ ├── .gitignore
│ │ ├── README.md
│ │ ├── index.js
│ │ ├── package.json
│ │ ├── src
│ │ │ ├── modules
│ │ │ │ ├── app.module.ts
│ │ │ │ ├── cats
│ │ │ │ │ ├── cat.entity.ts
│ │ │ │ │ ├── cats.controller.ts
│ │ │ │ │ ├── cats.module.ts
│ │ │ │ │ ├── cats.providers.ts
│ │ │ │ │ ├── cats.service.ts
│ │ │ │ │ └── dto
│ │ │ │ │ │ └── create-cat.dto.ts
│ │ │ │ └── database
│ │ │ │ │ ├── database.module.ts
│ │ │ │ │ └── database.providers.ts
│ │ │ └── server.ts
│ │ ├── tsconfig.json
│ │ └── tslint.json
│ │ ├── 08-passport
│ │ ├── .gitignore
│ │ ├── README.md
│ │ ├── index.js
│ │ ├── package.json
│ │ ├── src
│ │ │ ├── modules
│ │ │ │ ├── app.module.ts
│ │ │ │ └── auth
│ │ │ │ │ ├── auth.controller.ts
│ │ │ │ │ ├── auth.module.ts
│ │ │ │ │ ├── auth.service.ts
│ │ │ │ │ └── passport
│ │ │ │ │ └── jwt.strategy.ts
│ │ │ └── server.ts
│ │ ├── tsconfig.json
│ │ └── tslint.json
│ │ ├── 09-babel-example
│ │ ├── .babelrc
│ │ ├── .gitignore
│ │ ├── README.md
│ │ ├── index.js
│ │ ├── jsconfig.json
│ │ ├── package.json
│ │ └── src
│ │ │ ├── modules
│ │ │ ├── app.module.js
│ │ │ └── cats
│ │ │ │ ├── cats.controller.js
│ │ │ │ ├── cats.module.js
│ │ │ │ └── cats.service.js
│ │ │ └── server.js
│ │ ├── 10-mockgoose
│ │ ├── README.md
│ │ ├── e2e
│ │ │ ├── cats
│ │ │ │ └── cats.e2e-spec.ts
│ │ │ └── jest-e2e.json
│ │ ├── index.js
│ │ ├── package.json
│ │ ├── src
│ │ │ ├── modules
│ │ │ │ ├── app.module.ts
│ │ │ │ ├── cats
│ │ │ │ │ ├── cats.controller.ts
│ │ │ │ │ ├── cats.module.ts
│ │ │ │ │ ├── cats.providers.ts
│ │ │ │ │ ├── cats.service.ts
│ │ │ │ │ ├── dto
│ │ │ │ │ │ └── create-cat.dto.ts
│ │ │ │ │ ├── interfaces
│ │ │ │ │ │ └── cat.interface.ts
│ │ │ │ │ └── schemas
│ │ │ │ │ │ └── cat.schema.ts
│ │ │ │ └── database
│ │ │ │ │ ├── database.module.ts
│ │ │ │ │ └── database.providers.ts
│ │ │ └── server.ts
│ │ ├── tsconfig.json
│ │ └── tslint.json
│ │ ├── 11-swagger
│ │ ├── .gitignore
│ │ ├── README.md
│ │ ├── index.js
│ │ ├── jest.json
│ │ ├── package.json
│ │ ├── src
│ │ │ ├── modules
│ │ │ │ ├── app.module.ts
│ │ │ │ └── cats
│ │ │ │ │ ├── cats.controller.ts
│ │ │ │ │ ├── cats.module.ts
│ │ │ │ │ ├── cats.service.ts
│ │ │ │ │ ├── dto
│ │ │ │ │ └── create-cat.dto.ts
│ │ │ │ │ └── interfaces
│ │ │ │ │ └── cat.interface.ts
│ │ │ └── server.ts
│ │ ├── tsconfig.json
│ │ └── tslint.json
│ │ ├── 12-graphql-apollo
│ │ ├── .gitignore
│ │ ├── README.md
│ │ ├── index.js
│ │ ├── package.json
│ │ ├── src
│ │ │ ├── modules
│ │ │ │ ├── app.module.ts
│ │ │ │ └── cats
│ │ │ │ │ ├── cats.guard.ts
│ │ │ │ │ ├── cats.module.ts
│ │ │ │ │ ├── cats.resolvers.ts
│ │ │ │ │ ├── cats.service.ts
│ │ │ │ │ ├── cats.types.graphql
│ │ │ │ │ └── interfaces
│ │ │ │ │ └── cat.interface.ts
│ │ │ └── server.ts
│ │ ├── tsconfig.json
│ │ └── tslint.json
│ │ ├── 13-mongo-typeorm
│ │ ├── .gitignore
│ │ ├── README.md
│ │ ├── index.js
│ │ ├── ormconfig.json
│ │ ├── package.json
│ │ ├── src
│ │ │ ├── modules
│ │ │ │ ├── app.module.ts
│ │ │ │ └── photo
│ │ │ │ │ ├── photo.controller.ts
│ │ │ │ │ ├── photo.entity.ts
│ │ │ │ │ ├── photo.module.ts
│ │ │ │ │ └── photo.service.ts
│ │ │ └── server.ts
│ │ ├── tsconfig.json
│ │ └── tslint.json
│ │ ├── 14-mongoose-module
│ │ ├── .gitignore
│ │ ├── README.md
│ │ ├── index.js
│ │ ├── package.json
│ │ ├── src
│ │ │ ├── modules
│ │ │ │ ├── app.module.ts
│ │ │ │ └── cats
│ │ │ │ │ ├── cats.controller.ts
│ │ │ │ │ ├── cats.module.ts
│ │ │ │ │ ├── cats.service.ts
│ │ │ │ │ ├── dto
│ │ │ │ │ └── create-cat.dto.ts
│ │ │ │ │ ├── interfaces
│ │ │ │ │ └── cat.interface.ts
│ │ │ │ │ └── schemas
│ │ │ │ │ └── cat.schema.ts
│ │ │ └── server.ts
│ │ ├── tsconfig.json
│ │ └── tslint.json
│ │ └── README.md
├── decorator
│ ├── USAGE
│ ├── index.js
│ └── templates
│ │ └── index.decorator.ts
├── exception
│ ├── USAGE
│ ├── index.js
│ └── templates
│ │ └── index.exception.ts
├── filter
│ ├── USAGE
│ ├── index.js
│ └── templates
│ │ └── index.filter.ts
├── guard
│ ├── USAGE
│ ├── index.js
│ └── templates
│ │ └── index.guard.ts
├── interceptor
│ ├── USAGE
│ ├── index.js
│ └── templates
│ │ ├── exception.interceptor.ts
│ │ ├── index.interceptor.ts
│ │ ├── logging.interceptor.ts
│ │ ├── mixin-cache.interceptor.ts
│ │ └── transform.interceptor.ts
├── middleware
│ ├── USAGE
│ ├── index.js
│ └── templates
│ │ └── index.middleware.ts
├── module
│ ├── USAGE
│ ├── index.js
│ ├── module-types.js
│ └── templates
│ │ ├── graphql-module
│ │ └── cats
│ │ │ ├── cats.guard.ts
│ │ │ ├── cats.module.ts
│ │ │ ├── cats.resolvers.ts
│ │ │ ├── cats.service.ts
│ │ │ ├── cats.types.graphql
│ │ │ └── interfaces
│ │ │ └── cat.interface.ts
│ │ ├── mongo-typeorm-module
│ │ └── cats
│ │ │ ├── cat.entity.ts
│ │ │ ├── cats.controller.ts
│ │ │ ├── cats.module.ts
│ │ │ └── cats.service.ts
│ │ ├── mongoose-module
│ │ └── cats
│ │ │ ├── cats.controller.ts
│ │ │ ├── cats.module.ts
│ │ │ ├── cats.service.ts
│ │ │ ├── dto
│ │ │ └── create-cat.dto.ts
│ │ │ ├── interfaces
│ │ │ └── cat.interface.ts
│ │ │ └── schemas
│ │ │ └── cat.schema.ts
│ │ ├── sequelize-module
│ │ └── cats
│ │ │ ├── cat.entity.ts
│ │ │ ├── cats.controller.ts
│ │ │ ├── cats.module.ts
│ │ │ ├── cats.providers.ts
│ │ │ ├── cats.service.ts
│ │ │ └── dto
│ │ │ └── create-cat.dto.ts
│ │ └── sql-typeorm-module
│ │ └── cats
│ │ ├── cat.entity.ts
│ │ ├── cats.controller.ts
│ │ ├── cats.module.ts
│ │ └── cats.service.ts
└── pipe
│ ├── USAGE
│ ├── index.js
│ └── templates
│ ├── index.pipe.ts
│ └── validation.pipe.ts
├── nestjs.jpg
├── package-lock.json
├── package.json
├── scripts
├── rename-template.js
├── write-index.js
├── write-readme.js
└── write-usage.js
├── test.js
├── utils
├── case-change.js
└── walk-dir.js
└── yeoman.png
/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/ignore-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 |
6 | # testing
7 | /coverage
8 |
9 | # example
10 | /example
11 |
12 | # production
13 | /build
14 |
15 | # misc
16 | .DS_Store
17 | .env.local
18 | .env.development.local
19 | .env.test.local
20 | .env.production.local
21 |
22 | /example
23 |
24 | npm-debug.log*
25 | yarn-debug.log*
26 | yarn-error.log*
27 |
28 | yarn.lock
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Change Log
2 | All notable changes to generator-nesjs-app would be documented here
3 |
4 |
5 | ## 0.0.3 (2018-01-20)
6 |
7 | * Open source
8 |
9 |
10 | ## 0.0.2 (2018-01-20)
11 |
12 | * Open source
13 |
14 |
15 | ## 0.0.1 (2018-01-20)
16 |
17 | * Background test
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contributing to generator-nestjs-app
2 |
3 | We would love for you to contribute and help make it even better
4 | than it is today! As a contributor, here are the guidelines we would like you
5 | to follow:
6 |
7 | -[Code of Conduct](#coc)
8 | - [Issues and Bugs](#issue)
9 | - [Feature Requests](#feature)
10 | - [Submission Guidelines](#submit)
11 |
12 | ## Code of Conduct
13 | Help us keep this project open and inclusive.Please read and follow our [Code of Conduct](code_of_conduct /).
14 |
15 | ## Found an Issue?
16 | If you find a bug in the source code or a mistake in the documentation, you can help us by
17 | [submitting an issue](#submit - issue) to our [GitHub Repository](https://github.com/ashinzekene/generator-nestjs-app). Even better, you can
18 | [submit a Pull Request](#submit-pr) with a fix.
19 |
20 | ## Want a Feature?
21 | You can * request * a new feature by [submitting an issue](#submit - issue) to our [GitHub
22 | Repository][github].If you would like to * implement * a new feature, please submit an issue with
23 | a proposal for your work first, to be sure that we can use it.
24 |
25 | * **Small Features** can be crafted and directly [submitted as a Pull Request](#submit - pr).
26 |
27 | ## Submission Guidelines
28 |
29 | ### Submitting an Issue
30 | Before you submit an issue, search the archive, maybe your question was already answered.
31 |
32 | If your issue appears to be a bug, and hasn't been reported, open a new issue.
33 | Help us to maximize the effort we can spend fixing issues and adding new
34 | features, by not reporting duplicate issues.Providing the following information will increase the
35 | chances of your issue being dealt with quickly:
36 |
37 | * **Overview of the Issue** - if an error is being thrown a non- minified stack trace helps
38 | * **Version ** - what version is affected (e.g. 0.1.2)
39 | * **Motivation for or Use Case** - explain what are you trying to do and why the current behavior is a bug for you
40 | * **Browsers and Operating System** - is this a problem with all browsers?
41 | * **Reproduce the Error** - provide a live example [Runnable][runnable]) or a unambiguous set of steps
42 | * **Related Issues** - has a similar issue been reported before?
43 | * **Suggest a Fix** - if you can't fix the bug yourself, perhaps you can point to what might be
44 | causing the problem (line of code or commit)
45 |
46 | You can file new issues by providing the above information [here](https://github.com/ashinzekene/generator-nestjs-app/issues/new).
47 |
48 | ### Submitting a Pull Request (PR)
49 | Before you submit your Pull Request (PR) consider the following guidelines:
50 |
51 | * Search[GitHub](https://github.com/ashinzekene/generator-nestjs-app/pulls) for an open or closed PR
52 | that relates to your submission.You don't want to duplicate effort.
53 |
54 | * Make your changes in a new git fork:
55 |
56 | * Commit your changes using a descriptive commit message
57 | * Push your fork to GitHub:
58 | * In GitHub, send a pull request
59 | * If we suggest changes then:
60 | * Make the required updates.
61 | * Rebase your fork and force push to your GitHub repository (this will update your Pull Request):
62 |
63 | ```shell
64 | git rebase master -i
65 | git push -f
66 | ```
67 |
68 | That's it! Thank you for your contribution!
69 |
--------------------------------------------------------------------------------
/ISSUE_TEMPLATE.md:
--------------------------------------------------------------------------------
1 | > Please provide us with the following information:
2 | > ---------------------------------------------------------------
3 |
4 | ### OS and Version?
5 | > Windows 7, 8 or 10. Linux (which distribution).macOS(Yosemite ? El Capitan? Sierra ?)
6 |
7 | ### Versions
8 | >
9 |
10 | ### Repro steps
11 | >
12 |
13 | ### The log given by the failure.
14 | >
15 |
16 | ### Mention any other details that might be useful.
17 |
18 | > ---------------------------------------------------------------
19 | > Thanks! We'll be in touch soon.
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2018 Ashinze Ekene
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of
6 | this software and associated documentation files (the "Software"), to deal in
7 | the Software without restriction, including without limitation the rights to
8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 | the Software, and to permit persons to whom the Software is furnished to do so,
10 | subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--------------------------------------------------------------------------------
/PULL_REQUEST-TEMPLATE.md:
--------------------------------------------------------------------------------
1 | ## Purpose
2 | * ...
3 |
4 | ## What
5 | * ...
6 |
7 | ## How to Test
8 | * ...
9 |
10 | ## What to Check
11 | Verify that the following are valid
12 | * ...
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
NESTJS GENERATOR
5 | A yeoman generator for nestjs apps.
6 |
7 |
8 | ### ABOUT
9 | This generator helps you create a [NestJS](https://github.com/nestjs/nest) app with ease. It also comes with support for the following
10 |
11 | - Decorators
12 | - Exceptions
13 | - Guards
14 | - Interceptors
15 | - Middlewares
16 | - Modules
17 | ### OPTIONS
18 |
19 | NestJS app options available
20 |
21 | - SQL Typeorm app
22 | - Mongoose app
23 | - Seqelize app
24 | - Passport app
25 | - Graph QL apollo app
26 | - Mongo Typeorm app
27 | - Configurable Mongoose app
28 |
29 | ### USAGE
30 |
31 | Install dependencies
32 |
33 | ```sh
34 | $ npm install -g yo
35 | $ npm install -g generator-nestjs-app
36 | ```
37 | __The to create an app, run__
38 |
39 | ```sh
40 | $ yo nestjs-app
41 | ```
42 | __For other components run__
43 |
44 | ```sh
45 | $ yo nestjs-app:[COMPONENT] [NAME]
46 | ```
47 | For example
48 |
49 | ```sh
50 | $ yo nestjs-app:middleware app-auth
51 | ```
52 | ```sh
53 | $ yo nestjs-app:decorator roles
54 | ```
55 |
56 | ### OPTIONS
57 |
58 | #### `yo nestjs-app` (For creating a NestJS app)
59 |
60 |
61 | `--skip-install` - Skips installation of dependencies when bootstrappping an app
62 |
63 | `--npm` - Install dependencies with npm
64 |
65 | `--yarn` - Install dependencies with yarn
66 |
67 | #### `yo nestjs-app:[COMPONENT] [NAME]` (For creating components)
68 |
69 |
70 | `[COMPONENT]` - The type of the component to be created
71 |
72 | `[NAME]` - The name of the component to be created
73 |
74 | #### `yo nestjs-app:module [NAME]` (For creating a nestjs module)
75 |
76 |
77 | `[NAME]` - The name of the module to be created (required)
78 |
79 | ARGUMENTS
80 |
81 | --mongoose-module
82 |
83 | --sequelize-module
84 |
85 | --sql-typeorm
86 |
87 | --mongo-typeorm
88 |
89 | --graphql-module
90 |
91 |
92 | ### FILE AND COMPONENT NAMING
93 |
94 | By [Angular File Naming Convention](https://angular.io/guide/styleguide), names for components should be in kebab case. For example
95 |
96 | ```
97 | yo nestjs-app:decorator app-user-routes
98 | ```
99 |
100 | This creates a decorator with filename `app-user-routes.decorator.ts` decorator name `AppUserRoutesDecorator`
101 |
102 | ### CONTRIBUTIONS AND ISSUES
103 |
104 | Contributions and filing of issues are gladly welcome. Before contributing, be sure to read the [CONTRIBUTING GUIDE](changelog.md)
105 |
106 | - For making pull requests, you can use the [PR template](PULL_REQUEST-TEMPLATE.md)
107 |
108 | - For submitting, you can use the [ISSUES template](ISSUE_TEMPLATE.md)
109 |
110 | ### LICENSE
111 |
112 | [MIT LICENSE](LICENSE.md)
113 |
--------------------------------------------------------------------------------
/TODO.md:
--------------------------------------------------------------------------------
1 | # WHAT TO DO
2 |
3 | - [x] add readme
4 | - [x] Write function to change kebab-case to PascalCase
5 | - [x] Write function to change kebab-case to camelCase
6 | - [x] Write function to change kebab-case to CONSTAT_CASE
7 | - [x] add module generator
8 | - [ ] remove useless files (interceptors, exception, decorators e.t.c) from app generator templates
9 | - [x] add yeoman and nestjs image to readme
10 | - [x] `Usage` file
11 | - [ ] tests
12 |
13 | ### Generators For
14 | - [x] Decorator
15 | - [x] Exception
16 | - [ ] Filter
17 | - [x] Guard
18 | - [x] Interceptor
19 | - [x] Middleware
20 | - [ ] Pipe
--------------------------------------------------------------------------------
/generators/app/USAGE:
--------------------------------------------------------------------------------
1 |
2 |
3 | Description:
4 | Bootstraps a NestJS app for you depending on the options you select.
5 |
6 | Example:
7 | yo nestjs
8 |
9 | This will create:
10 | All the necessary files need to run your NestJS scaffolded app.
11 |
12 |
--------------------------------------------------------------------------------
/generators/app/app-types.js:
--------------------------------------------------------------------------------
1 | module.exports = [
2 | // {
3 | // name: 'Basic app with Interceptors and Middlewares',
4 | // value: '01-cats-app'
5 | // },
6 | // {
7 | // name: 'A Socket app with both client and server',
8 | // value: '02-gateways'
9 | // },
10 | // {
11 | // name: 'A microservices app',
12 | // value: '03-microservices'
13 | // },
14 | // {
15 | // name: 'An Injector app',
16 | // value: '04-injector'
17 | // },
18 | {
19 | name: 'SQL Typeorm',
20 | value: '05-sql-typeorm'
21 | },
22 | {
23 | name: 'Mongoose',
24 | value: '06-mongoose'
25 | },
26 | {
27 | name: 'Seqelize',
28 | value: '07-sequelize'
29 | },
30 | {
31 | name: 'Passport',
32 | value: '08-passport'
33 | },
34 | // {
35 | // name: 'Babel JS',
36 | // value: '09-babel-example'
37 | // },
38 | // {
39 | // {
40 | // name: 'Swagger',
41 | // value: '11-swagger'
42 | // },
43 | {
44 | name: 'Graph QL apollo',
45 | value: '12-graphql-apollo'
46 | },
47 | {
48 | name: 'Mongo Typeorm',
49 | value: '13-mongo-typeorm'
50 | },
51 | {
52 | name: 'Configurable Mongoose',
53 | value: '14-mongoose-module'
54 | }]
--------------------------------------------------------------------------------
/generators/app/index.js:
--------------------------------------------------------------------------------
1 | const path = require('path')
2 | const os = require('os')
3 | const chalk = require('chalk')
4 | const Generator = require('yeoman-generator');
5 | const yosay = require('yosay')
6 | const walkDir = require('../../utils/walk-dir')
7 | const appTypes = require('./app-types')
8 |
9 | module.exports = class extends Generator {
10 | constructor(args, opt) {
11 | super(args, opt)
12 | this.option('skip-install', {
13 | default: false,
14 | type: Boolean
15 | });
16 | this.option('yarn');
17 | this.option('npm');
18 | }
19 | initializing() {
20 | this.log(yosay(`Welcome to the \n ${chalk.bgRed.white.bold("NESTJS Generator!")} \n Let's scaffold a new ${chalk.bgRed.white('NESTJS APP')}`))
21 | }
22 |
23 | prompting() {
24 | return this.prompt([
25 | {
26 | type: 'input',
27 | name: 'name',
28 | message: 'Enter a name for your app',
29 | default: this.appname,
30 | },
31 | {
32 | type: 'input',
33 | name: 'identifier',
34 | message: 'Enter your app\'s identifier',
35 | default: this.appname,
36 | validate: function (name) {
37 | return /^[a-z0-9][a-z0-9\-]*$/.test(name) || "App identifier can only contain lowercase letters, numbers and -"
38 | }
39 | },
40 | {
41 | type: 'input',
42 | name: 'description',
43 | message: 'Enter a description for your app',
44 | },
45 | {
46 | type: 'input',
47 | name: 'publisher',
48 | message: 'Enter publisher\'s name',
49 | default: os.userInfo().username
50 | },
51 | {
52 | type: 'list',
53 | name: 'type',
54 | message: 'What type of NESTJS app do you want to start?',
55 | choices: appTypes
56 | }
57 | ]).then(res => {
58 | this.appConfig = {}
59 | this.appConfig.name = res.name
60 | this.appConfig.description = res.description
61 | this.appConfig.identifier = res.identifier
62 | this.appConfig.publisher = res.publisher
63 | this.appConfig.type = res.type
64 | this.log(chalk.bgWhite.red(`
65 | Name: ${this.appConfig.name}
66 | Identifier: ${this.appConfig.identifier}
67 | Description: ${this.appConfig.description}
68 | Publisher: ${this.appConfig.publisher}
69 | Type: ${appTypes.find(type => type.value === this.appConfig.type).name} `))
70 | })
71 | }
72 |
73 | writing() {
74 | let files = walkDir(path.join(__dirname, "./templates/", this.appConfig.type))
75 | let newFiles = files.map(pth => pth.replace(RegExp(`.+${this.appConfig.type}`), ""))
76 |
77 | newFiles.forEach(file => {
78 | this.fs.copyTpl(
79 | this.templatePath(this.appConfig.type + "/"),
80 | this.destinationPath(this.appConfig.identifier + "/"),
81 | { config: this.appConfig }
82 | );
83 | })
84 | }
85 |
86 | install() {
87 | if (this.options['skip-install']) {
88 | this.log(chalk.green(`
89 | To install dependencies, run
90 |
91 | ${chalk.white('$')} cd ${this.appConfig.identifier}/
92 | ${chalk.white('$')} npm install
93 | `))
94 | } else {
95 | if (this.options['yarn']) {
96 | this.installDependencies({
97 | yarn: true,
98 | bower: false,
99 | npm: false,
100 | })
101 | } else {
102 | this.installDependencies({
103 | npm: true,
104 | yarn: false,
105 | bower: false,
106 | })
107 |
108 | }
109 | }
110 | }
111 |
112 | };
--------------------------------------------------------------------------------
/generators/app/templates/.gitignore:
--------------------------------------------------------------------------------
1 | Nothing inint
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/.gitignore:
--------------------------------------------------------------------------------
1 | # dependencies
2 | /node_modules
3 |
4 | # IDE
5 | /.idea
6 | /.awcache
7 | /.vscode
8 |
9 | # misc
10 | npm-debug.log
11 |
12 | # example
13 | /quick-start
14 |
15 | # tests
16 | /test
17 | /coverage
18 | /.nyc_output
19 |
20 | # dist
21 | /dist
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/README.md:
--------------------------------------------------------------------------------
1 | # <%= config.name %>
2 |
3 | <%= config.description %>
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/e2e/cats/cats.e2e-spec.ts:
--------------------------------------------------------------------------------
1 | import * as express from 'express';
2 | import * as request from 'supertest';
3 | import { Test } from '@nestjs/testing';
4 | import { CatsModule } from '../../src/modules/cats/cats.module';
5 | import { CatsService } from '../../src/modules/cats/cats.service';
6 | import { INestApplication } from '@nestjs/common';
7 |
8 | describe('Cats', () => {
9 | let server;
10 | let app: INestApplication;
11 |
12 | const catsService = { findAll: () => ['test'] };
13 |
14 | beforeAll(async () => {
15 | const module = await Test.createTestingModule({
16 | modules: [CatsModule],
17 | })
18 | .overrideComponent(CatsService)
19 | .useValue(catsService)
20 | .compile();
21 |
22 | server = express();
23 | app = module.createNestApplication(server);
24 | await app.init();
25 | });
26 |
27 | it(`/GET cats`, () => {
28 | return request(server)
29 | .get('/cats')
30 | .expect(200)
31 | .expect({
32 | data: catsService.findAll(),
33 | });
34 | });
35 |
36 | afterAll(async () => {
37 | await app.close();
38 | });
39 | });
40 |
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/e2e/jest-e2e.json:
--------------------------------------------------------------------------------
1 | {
2 | "moduleFileExtensions": [
3 | "ts",
4 | "tsx",
5 | "js",
6 | "json"
7 | ],
8 | "transform": {
9 | "^.+\\.tsx?$": "/../node_modules/ts-jest/preprocessor.js"
10 | },
11 | "testRegex": "/e2e/.*\\.(e2e-test|e2e-spec).(ts|tsx|js)$",
12 | "collectCoverageFrom" : ["src/**/*.{js,jsx,tsx,ts}", "!**/node_modules/**", "!**/vendor/**"],
13 | "coverageReporters": ["json", "lcov"]
14 | }
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/index.js:
--------------------------------------------------------------------------------
1 | require('ts-node/register');
2 | require('./src/server');
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/jest.json:
--------------------------------------------------------------------------------
1 | {
2 | "moduleFileExtensions": [
3 | "ts",
4 | "tsx",
5 | "js",
6 | "json"
7 | ],
8 | "transform": {
9 | "^.+\\.tsx?$": "/node_modules/ts-jest/preprocessor.js"
10 | },
11 | "testRegex": "/src/.*\\.(test|spec).(ts|tsx|js)$",
12 | "collectCoverageFrom" : ["src/**/*.{js,jsx,tsx,ts}", "!**/node_modules/**", "!**/vendor/**"],
13 | "coverageReporters": ["json", "lcov"]
14 | }
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "<%= config.identifier %>",
3 | "version": "1.0.0",
4 | "description": "<%= config.description %>",
5 | "license": "MIT",
6 | "publisher": "<%= config.publisher %>",
7 | "scripts": {
8 | "start": "node index.js",
9 | "prestart:prod": "tsc",
10 | "start:prod": "node dist/server.js",
11 | "test": "jest --config=jest.json",
12 | "test:watch": "jest --watch --config=jest.json",
13 | "test:coverage": "jest --config=jest.json --coverage --coverageDirectory=coverage",
14 | "e2e": "jest --config=e2e/jest-e2e.json --forceExit",
15 | "e2e:watch": "jest --watch --config=e2e/jest-e2e.json"
16 | },
17 | "dependencies": {
18 | "@nestjs/common": "^4.3.0",
19 | "@nestjs/core": "^4.3.0",
20 | "@nestjs/microservices": "^4.3.0",
21 | "@nestjs/testing": "^4.3.0",
22 | "@nestjs/websockets": "^4.3.0",
23 | "class-transformer": "^0.1.7",
24 | "class-validator": "^0.7.2",
25 | "redis": "^2.7.1",
26 | "reflect-metadata": "^0.1.10",
27 | "rxjs": "^5.4.3",
28 | "typescript": "^2.4.2"
29 | },
30 | "devDependencies": {
31 | "@types/jest": "^20.0.8",
32 | "@types/node": "^7.0.41",
33 | "jest": "^20.0.4",
34 | "supertest": "^3.0.0",
35 | "ts-jest": "^20.0.14",
36 | "ts-node": "^3.3.0"
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/src/modules/app.module.ts:
--------------------------------------------------------------------------------
1 | import { Module, NestModule, MiddlewaresConsumer } from '@nestjs/common';
2 | import { LoggerMiddleware } from './common/middlewares/logger.middleware';
3 | import { CatsModule } from './cats/cats.module';
4 | import { CatsController } from './cats/cats.controller';
5 |
6 | @Module({
7 | modules: [CatsModule],
8 | })
9 | export class ApplicationModule implements NestModule {
10 | configure(consumer: MiddlewaresConsumer): void {
11 | consumer.apply(LoggerMiddleware)
12 | .with('ApplicationModule')
13 | .forRoutes(CatsController);
14 | }
15 | }
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/src/modules/cats/cats.controller.spec.ts:
--------------------------------------------------------------------------------
1 | import { Test } from '@nestjs/testing';
2 | import { CatsController } from './cats.controller';
3 | import { CatsService } from './cats.service';
4 |
5 | describe('CatsController', () => {
6 | let catsController: CatsController;
7 | let catsService: CatsService;
8 |
9 | beforeEach(async () => {
10 | const module = await Test.createTestingModule({
11 | controllers: [CatsController],
12 | components: [CatsService],
13 | }).compile();
14 |
15 | catsService = module.get(CatsService);
16 | catsController = module.get(CatsController);
17 | });
18 |
19 | describe('findAll', () => {
20 | it('should return an array of cats', async () => {
21 | const result = ['test'];
22 | jest.spyOn(catsService, 'findAll').mockImplementation(() => result);
23 |
24 | expect(await catsController.findAll()).toBe(result);
25 | });
26 | });
27 | });
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/src/modules/cats/cats.controller.ts:
--------------------------------------------------------------------------------
1 | import { Controller, Get, Post, Body, UseGuards, ReflectMetadata, UseInterceptors, Param } from '@nestjs/common';
2 | import { CreateCatDto } from './dto/create-cat.dto';
3 | import { CatsService } from './cats.service';
4 | import { Cat } from './interfaces/cat.interface';
5 | import { RolesGuard } from '../common/guards/roles.guard';
6 | import { Roles } from '../common/decorators/roles.decorator';
7 | import { LoggingInterceptor } from '../common/interceptors/logging.interceptor';
8 | import { TransformInterceptor } from '../common/interceptors/transform.interceptor';
9 | import { ParseIntPipe } from '../common/pipes/parse-int.pipe';
10 |
11 | @Controller('cats')
12 | @UseGuards(RolesGuard)
13 | @UseInterceptors(LoggingInterceptor, TransformInterceptor)
14 | export class CatsController {
15 | constructor(private readonly catsService: CatsService) {}
16 |
17 | @Post()
18 | @Roles('admin')
19 | async create(@Body() createCatDto: CreateCatDto) {
20 | this.catsService.create(createCatDto);
21 | }
22 |
23 | @Get()
24 | async findAll(): Promise {
25 | return this.catsService.findAll();
26 | }
27 |
28 | @Get(':id')
29 | findOne(@Param('id', new ParseIntPipe()) id) {
30 | // logic
31 | }
32 | }
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/src/modules/cats/cats.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { CatsController } from './cats.controller';
3 | import { CatsService } from './cats.service';
4 |
5 | @Module({
6 | controllers: [CatsController],
7 | components: [CatsService],
8 | })
9 | export class CatsModule {}
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/src/modules/cats/cats.service.ts:
--------------------------------------------------------------------------------
1 | import { Component } from '@nestjs/common';
2 | import { Cat } from './interfaces/cat.interface';
3 | import { CatsModule } from './cats.module';
4 |
5 | @Component()
6 | export class CatsService {
7 | private readonly cats: Cat[] = [];
8 |
9 | create(cat: Cat) {
10 | this.cats.push(cat);
11 | }
12 |
13 | findAll(): Cat[] {
14 | return this.cats;
15 | }
16 | }
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/src/modules/cats/dto/create-cat.dto.ts:
--------------------------------------------------------------------------------
1 | import { IsString, IsInt } from 'class-validator';
2 |
3 | export class CreateCatDto {
4 | @IsString()
5 | readonly name: string;
6 |
7 | @IsInt()
8 | readonly age: number;
9 |
10 | @IsString()
11 | readonly breed: string;
12 | }
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/src/modules/cats/interfaces/cat.interface.ts:
--------------------------------------------------------------------------------
1 | export interface Cat {
2 | readonly name: string;
3 | readonly age: number;
4 | readonly breed: string;
5 | }
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/src/modules/common/decorators/roles.decorator.ts:
--------------------------------------------------------------------------------
1 | import { ReflectMetadata } from '@nestjs/common';
2 |
3 | export const Roles = (...roles: string[]) => ReflectMetadata('roles', roles);
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/src/modules/common/exceptions/forbidden.exception.ts:
--------------------------------------------------------------------------------
1 | import { HttpException } from '@nestjs/common';
2 | import { HttpStatus } from '@nestjs/common';
3 |
4 | export class ForbiddenException extends HttpException {
5 | constructor() {
6 | super('Forbidden', HttpStatus.FORBIDDEN);
7 | }
8 | }
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/src/modules/common/filters/http-exception.filter.ts:
--------------------------------------------------------------------------------
1 | import { ExceptionFilter, Catch } from '@nestjs/common';
2 | import { HttpException } from '@nestjs/common';
3 |
4 | @Catch(HttpException)
5 | export class HttpExceptionFilter implements ExceptionFilter {
6 | catch(exception: HttpException, response) {
7 | const status = exception.getStatus();
8 |
9 | response.status(status).json({
10 | statusCode: status,
11 | message: `It's a message from the exception filter`,
12 | });
13 | }
14 | }
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/src/modules/common/guards/roles.guard.ts:
--------------------------------------------------------------------------------
1 | import { Guard, CanActivate, ExecutionContext } from '@nestjs/common';
2 | import { Observable } from 'rxjs/Observable';
3 | import { Reflector } from '@nestjs/core';
4 |
5 | @Guard()
6 | export class RolesGuard implements CanActivate {
7 | constructor(private readonly reflector: Reflector) {}
8 |
9 | canActivate(req, context: ExecutionContext): boolean {
10 | const { parent, handler } = context;
11 | const roles = this.reflector.get('roles', handler);
12 | if (!roles) {
13 | return true;
14 | }
15 |
16 | const user = req.user;
17 | const hasRole = () => !!user.roles.find((role) => !!roles.find((item) => item === role));
18 | return user && user.roles && hasRole();
19 | }
20 | }
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/src/modules/common/interceptors/cache.interceptor.ts:
--------------------------------------------------------------------------------
1 | import { Interceptor, NestInterceptor, ExecutionContext } from '@nestjs/common';
2 | import { Observable } from 'rxjs/Observable';
3 | import 'rxjs/add/observable/of';
4 |
5 | @Interceptor()
6 | export abstract class CacheInterceptor implements NestInterceptor {
7 | protected abstract readonly isCached: () => boolean;
8 |
9 | intercept(dataOrRequest, context: ExecutionContext, stream$: Observable): Observable {
10 | if (this.isCached()) {
11 | return Observable.of([]);
12 | }
13 | return stream$;
14 | }
15 | }
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/src/modules/common/interceptors/exception.interceptor.ts:
--------------------------------------------------------------------------------
1 | import { Interceptor, NestInterceptor, ExecutionContext, HttpStatus } from '@nestjs/common';
2 | import { HttpException } from '@nestjs/common';
3 | import { Observable } from 'rxjs/Observable';
4 | import 'rxjs/add/operator/catch';
5 | import 'rxjs/add/observable/throw';
6 |
7 | @Interceptor()
8 | export class ExceptionInterceptor implements NestInterceptor {
9 | intercept(dataOrRequest, context: ExecutionContext, stream$: Observable): Observable {
10 | return stream$.catch((err) => Observable.throw(
11 | new HttpException('Exception interceptor message', HttpStatus.BAD_GATEWAY),
12 | ));
13 | }
14 | }
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/src/modules/common/interceptors/logging.interceptor.ts:
--------------------------------------------------------------------------------
1 | import { Interceptor, NestInterceptor, ExecutionContext } from '@nestjs/common';
2 | import { Observable } from 'rxjs/Observable';
3 | import 'rxjs/add/operator/do';
4 |
5 | @Interceptor()
6 | export class LoggingInterceptor implements NestInterceptor {
7 | intercept(dataOrRequest, context: ExecutionContext, stream$: Observable): Observable {
8 | console.log('Before...');
9 | const now = Date.now();
10 |
11 | return stream$.do(
12 | () => console.log(`After... ${Date.now() - now}ms`),
13 | );
14 | }
15 | }
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/src/modules/common/interceptors/mixin-cache.interceptor.ts:
--------------------------------------------------------------------------------
1 | import { mixin } from '@nestjs/common';
2 | import { CacheInterceptor } from './cache.interceptor';
3 |
4 | export function mixinCacheInterceptor(isCached: () => boolean) {
5 | return mixin(class extends CacheInterceptor {
6 | protected readonly isCached = isCached;
7 | });
8 | }
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/src/modules/common/interceptors/transform.interceptor.ts:
--------------------------------------------------------------------------------
1 | import { Interceptor, NestInterceptor, ExecutionContext } from '@nestjs/common';
2 | import { Observable } from 'rxjs/Observable';
3 | import 'rxjs/add/operator/map';
4 |
5 | @Interceptor()
6 | export class TransformInterceptor implements NestInterceptor {
7 | intercept(dataOrRequest, context: ExecutionContext, stream$: Observable): Observable {
8 | return stream$.map((data) => ({ data }));
9 | }
10 | }
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/src/modules/common/middlewares/logger.middleware.ts:
--------------------------------------------------------------------------------
1 | import { Middleware, NestMiddleware, ExpressMiddleware } from '@nestjs/common';
2 |
3 | @Middleware()
4 | export class LoggerMiddleware implements NestMiddleware {
5 | resolve(name: string): ExpressMiddleware {
6 | return (req, res, next) => {
7 | console.log(`[${name}] Request...`); // [ApplicationModule] Request...
8 | next();
9 | };
10 | }
11 | }
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/src/modules/common/pipes/parse-int.pipe.ts:
--------------------------------------------------------------------------------
1 | import { HttpException } from '@nestjs/common';
2 | import { PipeTransform, Pipe, ArgumentMetadata, HttpStatus } from '@nestjs/common';
3 |
4 | @Pipe()
5 | export class ParseIntPipe implements PipeTransform {
6 | async transform(value: string, metadata: ArgumentMetadata) {
7 | const val = parseInt(value, 10);
8 | if (isNaN(val)) {
9 | throw new HttpException('Validation failed', HttpStatus.BAD_REQUEST);
10 | }
11 | return val;
12 | }
13 | }
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/src/modules/common/pipes/validation.pipe.ts:
--------------------------------------------------------------------------------
1 | import { HttpException } from '@nestjs/common';
2 | import { PipeTransform, Pipe, ArgumentMetadata, HttpStatus } from '@nestjs/common';
3 | import { validate } from 'class-validator';
4 | import { plainToClass } from 'class-transformer';
5 |
6 | @Pipe()
7 | export class ValidationPipe implements PipeTransform {
8 | async transform(value, metadata: ArgumentMetadata) {
9 | const { metatype } = metadata;
10 | if (!metatype || !this.toValidate(metatype)) {
11 | return value;
12 | }
13 | const object = plainToClass(metatype, value);
14 | const errors = await validate(object);
15 | if (errors.length > 0) {
16 | throw new HttpException('Validation failed', HttpStatus.BAD_REQUEST);
17 | }
18 | return value;
19 | }
20 |
21 | private toValidate(metatype): boolean {
22 | const types = [String, Boolean, Number, Array, Object];
23 | return !types.find((type) => metatype === type);
24 | }
25 | }
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/src/server.ts:
--------------------------------------------------------------------------------
1 | import { NestFactory } from '@nestjs/core';
2 | import { ApplicationModule } from './modules/app.module';
3 | import { ValidationPipe } from './modules/common/pipes/validation.pipe';
4 |
5 | async function bootstrap() {
6 | const app = await NestFactory.create(ApplicationModule);
7 | app.useGlobalPipes(new ValidationPipe());
8 | await app.listen(3000);
9 | }
10 | bootstrap();
11 |
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "declaration": false,
5 | "noImplicitAny": false,
6 | "removeComments": true,
7 | "noLib": false,
8 | "emitDecoratorMetadata": true,
9 | "experimentalDecorators": true,
10 | "target": "es6",
11 | "sourceMap": true,
12 | "allowJs": true,
13 | "outDir": "./dist"
14 | },
15 | "include": [
16 | "src/**/*"
17 | ],
18 | "exclude": [
19 | "node_modules",
20 | "**/*.spec.ts"
21 | ]
22 | }
--------------------------------------------------------------------------------
/generators/app/templates/01-cats-app/tslint.json:
--------------------------------------------------------------------------------
1 | {
2 | "defaultSeverity": "error",
3 | "extends": [
4 | "tslint:recommended"
5 | ],
6 | "jsRules": {
7 | "no-unused-expression": true
8 | },
9 | "rules": {
10 | "eofline": false,
11 | "quotemark": [
12 | true,
13 | "single"
14 | ],
15 | "ordered-imports": [
16 | false
17 | ],
18 | "max-line-length": [
19 | 150
20 | ],
21 | "member-ordering": [
22 | false
23 | ],
24 | "curly": false,
25 | "interface-name": [
26 | false
27 | ],
28 | "array-type": [
29 | false
30 | ],
31 | "member-access": [
32 | false
33 | ],
34 | "no-empty-interface": false,
35 | "no-empty": false,
36 | "arrow-parens": false,
37 | "object-literal-sort-keys": false,
38 | "no-unused-expression": false,
39 | "max-classes-per-file": [
40 | false
41 | ],
42 | "variable-name": [
43 | false
44 | ],
45 | "one-line": [
46 | false
47 | ],
48 | "one-variable-per-declaration": [
49 | false
50 | ]
51 | },
52 | "rulesDirectory": []
53 | }
--------------------------------------------------------------------------------
/generators/app/templates/02-gateways/.gitignore:
--------------------------------------------------------------------------------
1 | # dependencies
2 | /node_modules
3 |
4 | # IDE
5 | /.idea
6 | /.awcache
7 | /.vscode
8 |
9 | # misc
10 | npm-debug.log
11 |
12 | # example
13 | /quick-start
14 |
15 | # tests
16 | /test
17 | /coverage
18 | /.nyc_output
19 |
20 | # dist
21 | /dist
--------------------------------------------------------------------------------
/generators/app/templates/02-gateways/README.md:
--------------------------------------------------------------------------------
1 | # <%= config.name %>
2 |
3 | <%= config.description %>
--------------------------------------------------------------------------------
/generators/app/templates/02-gateways/client/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/generators/app/templates/02-gateways/client/ws.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/generators/app/templates/02-gateways/index.js:
--------------------------------------------------------------------------------
1 | require('ts-node/register');
2 | require('./src/server');
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/generators/app/templates/02-gateways/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "<%= config.identifier %>",
3 | "version": "1.0.0",
4 | "description": "<%= config.description %>",
5 | "license": "MIT",
6 | "publisher": "<%= config.publisher %>",
7 | "scripts": {
8 | "start": "node index.js",
9 | "prestart:prod": "tsc",
10 | "start:prod": "node dist/server.js"
11 | },
12 | "dependencies": {
13 | "@nestjs/common": "^4.3.0",
14 | "@nestjs/core": "^4.3.0",
15 | "@nestjs/microservices": "^4.3.0",
16 | "@nestjs/testing": "^4.3.0",
17 | "@nestjs/websockets": "^4.3.0",
18 | "class-transformer": "^0.1.7",
19 | "class-validator": "^0.7.2",
20 | "redis": "^2.7.1",
21 | "reflect-metadata": "^0.1.10",
22 | "rxjs": "^5.4.3",
23 | "typescript": "^2.4.2",
24 | "ws": "^3.1.0"
25 | },
26 | "devDependencies": {
27 | "@types/node": "^7.0.41",
28 | "@types/ws": "^3.0.2",
29 | "ts-node": "^3.3.0"
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/generators/app/templates/02-gateways/src/modules/app.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { EventsModule } from './events/events.module';
3 |
4 | @Module({
5 | modules: [EventsModule],
6 | })
7 | export class ApplicationModule {}
--------------------------------------------------------------------------------
/generators/app/templates/02-gateways/src/modules/common/adapters/ws-adapter.ts:
--------------------------------------------------------------------------------
1 | import * as WebSocket from 'ws';
2 | import { WebSocketAdapter } from '@nestjs/common';
3 | import { MessageMappingProperties } from '@nestjs/websockets';
4 | import { Observable } from 'rxjs/Observable';
5 | import 'rxjs/add/observable/fromEvent';
6 | import 'rxjs/add/observable/empty';
7 | import 'rxjs/add/operator/switchMap';
8 | import 'rxjs/add/operator/filter';
9 |
10 | export class WsAdapter implements WebSocketAdapter {
11 | public create(port: number) {
12 | return new WebSocket.Server({ port });
13 | }
14 |
15 | public bindClientConnect(server, callback: (...args: any[]) => void) {
16 | server.on('connection', callback);
17 | }
18 |
19 | public bindMessageHandlers(
20 | client: WebSocket,
21 | handlers: MessageMappingProperties[],
22 | process: (data: any) => Observable,
23 | ) {
24 | Observable.fromEvent(client, 'message')
25 | .switchMap((buffer) => this.bindMessageHandler(buffer, handlers, process))
26 | .filter((result) => !!result)
27 | .subscribe((response) => client.send(JSON.stringify(response)));
28 | }
29 |
30 | public bindMessageHandler(
31 | buffer,
32 | handlers: MessageMappingProperties[],
33 | process: (data: any) => Observable,
34 | ): Observable {
35 | const data = JSON.parse(buffer.data);
36 | const messageHandler = handlers.find((handler) => handler.message === data.type);
37 | if (!messageHandler) {
38 | return Observable.empty();
39 | }
40 | const { callback } = messageHandler;
41 | return process(callback(data));
42 | }
43 | }
--------------------------------------------------------------------------------
/generators/app/templates/02-gateways/src/modules/common/decorators/roles.decorator.ts:
--------------------------------------------------------------------------------
1 | import { ReflectMetadata } from '@nestjs/common';
2 |
3 | export const Roles = (...roles: string[]) => ReflectMetadata('roles', roles);
--------------------------------------------------------------------------------
/generators/app/templates/02-gateways/src/modules/common/filters/ws-exception.filter.ts:
--------------------------------------------------------------------------------
1 | import { Catch, WsExceptionFilter } from '@nestjs/common';
2 | import { WsException } from '@nestjs/websockets';
3 |
4 | @Catch(WsException)
5 | export class ExceptionFilter implements WsExceptionFilter {
6 | catch(exception: WsException, client) {
7 | client.emit('exception', {
8 | status: 'error',
9 | message: `It's a message from the exception filter`,
10 | });
11 | }
12 | }
--------------------------------------------------------------------------------
/generators/app/templates/02-gateways/src/modules/common/guards/roles.guard.ts:
--------------------------------------------------------------------------------
1 | import { Guard, CanActivate, ExecutionContext } from '@nestjs/common';
2 | import { Observable } from 'rxjs/Observable';
3 | import { Reflector } from '@nestjs/core';
4 |
5 | @Guard()
6 | export class RolesGuard implements CanActivate {
7 | constructor(private readonly reflector: Reflector) {}
8 |
9 | canActivate(data, context: ExecutionContext): boolean {
10 | const { parent, handler } = context;
11 | const roles = this.reflector.get('roles', handler);
12 | if (!roles) {
13 | return true;
14 | }
15 |
16 | const user = data.user;
17 | const hasRole = () => !!user.roles.find((role) => !!roles.find((item) => item === role));
18 | return user && user.roles && hasRole();
19 | }
20 | }
--------------------------------------------------------------------------------
/generators/app/templates/02-gateways/src/modules/common/interceptors/cache.interceptor.ts:
--------------------------------------------------------------------------------
1 | import { Interceptor, NestInterceptor, ExecutionContext } from '@nestjs/common';
2 | import { Observable } from 'rxjs/Observable';
3 | import 'rxjs/add/observable/of';
4 |
5 | @Interceptor()
6 | export class CacheInterceptor implements NestInterceptor {
7 | intercept(dataOrRequest, context: ExecutionContext, stream$: Observable): Observable {
8 | const isCached = true;
9 | if (isCached) {
10 | return Observable.of([]);
11 | }
12 | return stream$;
13 | }
14 | }
--------------------------------------------------------------------------------
/generators/app/templates/02-gateways/src/modules/common/interceptors/logging.interceptor.ts:
--------------------------------------------------------------------------------
1 | import { Interceptor, NestInterceptor, ExecutionContext } from '@nestjs/common';
2 | import { Observable } from 'rxjs/Observable';
3 | import 'rxjs/add/operator/do';
4 |
5 | @Interceptor()
6 | export class LoggingInterceptor implements NestInterceptor {
7 | intercept(dataOrRequest, context: ExecutionContext, stream$: Observable): Observable {
8 | console.log('Before...');
9 | const now = Date.now();
10 |
11 | return stream$.do(
12 | () => console.log(`After... ${Date.now() - now}ms`),
13 | );
14 | }
15 | }
--------------------------------------------------------------------------------
/generators/app/templates/02-gateways/src/modules/common/interceptors/transform.interceptor.ts:
--------------------------------------------------------------------------------
1 | import { Interceptor, NestInterceptor, ExecutionContext } from '@nestjs/common';
2 | import { Observable } from 'rxjs/Observable';
3 | import 'rxjs/add/operator/map';
4 |
5 | @Interceptor()
6 | export class TransformInterceptor implements NestInterceptor {
7 | intercept(dataOrRequest, context: ExecutionContext, stream$: Observable): Observable {
8 | return stream$.map((data) => ({ data }));
9 | }
10 | }
--------------------------------------------------------------------------------
/generators/app/templates/02-gateways/src/modules/common/pipes/validation.pipe.ts:
--------------------------------------------------------------------------------
1 | import { PipeTransform, Pipe, ArgumentMetadata, HttpStatus } from '@nestjs/common';
2 | import { validate } from 'class-validator';
3 | import { plainToClass } from 'class-transformer';
4 | import { WsException } from '@nestjs/websockets';
5 |
6 | @Pipe()
7 | export class ValidationPipe implements PipeTransform {
8 | async transform(value, metadata: ArgumentMetadata) {
9 | const { metatype } = metadata;
10 | if (!metatype || !this.toValidate(metatype)) {
11 | return value;
12 | }
13 | const object = plainToClass(metatype, value);
14 | const errors = await validate(object);
15 | if (errors.length > 0) {
16 | throw new WsException('Validation failed');
17 | }
18 | return value;
19 | }
20 |
21 | private toValidate(metatype): boolean {
22 | const types = [String, Boolean, Number, Array, Object];
23 | return !types.find((type) => metatype === type);
24 | }
25 | }
--------------------------------------------------------------------------------
/generators/app/templates/02-gateways/src/modules/events/events.gateway.ts:
--------------------------------------------------------------------------------
1 | import { WebSocketGateway, SubscribeMessage, WsResponse, WebSocketServer, WsException } from '@nestjs/websockets';
2 | import { Observable } from 'rxjs/Observable';
3 | import 'rxjs/add/observable/from';
4 | import 'rxjs/add/operator/map';
5 |
6 | @WebSocketGateway(81)
7 | export class EventsGateway {
8 | @WebSocketServer() server;
9 |
10 | @SubscribeMessage('events')
11 | onEvent(client, data): Observable> {
12 | const event = 'events';
13 | const response = [1, 2, 3];
14 |
15 | return Observable.from(response)
16 | .map((res) => ({ event, data: res }));
17 | }
18 | }
--------------------------------------------------------------------------------
/generators/app/templates/02-gateways/src/modules/events/events.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { EventsGateway } from './events.gateway';
3 |
4 | @Module({
5 | components: [EventsGateway],
6 | })
7 | export class EventsModule {}
--------------------------------------------------------------------------------
/generators/app/templates/02-gateways/src/server.ts:
--------------------------------------------------------------------------------
1 | import { NestFactory } from '@nestjs/core';
2 | import { ApplicationModule } from './modules/app.module';
3 |
4 | async function bootstrap() {
5 | const app = await NestFactory.create(ApplicationModule);
6 | await app.listen(3000);
7 | }
8 | bootstrap();
9 |
--------------------------------------------------------------------------------
/generators/app/templates/02-gateways/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "declaration": false,
5 | "noImplicitAny": false,
6 | "removeComments": true,
7 | "noLib": false,
8 | "emitDecoratorMetadata": true,
9 | "experimentalDecorators": true,
10 | "target": "es6",
11 | "sourceMap": true,
12 | "allowJs": true,
13 | "outDir": "./dist"
14 | },
15 | "include": [
16 | "src/**/*"
17 | ],
18 | "exclude": [
19 | "node_modules",
20 | "**/*.spec.ts"
21 | ]
22 | }
--------------------------------------------------------------------------------
/generators/app/templates/02-gateways/tslint.json:
--------------------------------------------------------------------------------
1 | {
2 | "defaultSeverity": "error",
3 | "extends": [
4 | "tslint:recommended"
5 | ],
6 | "jsRules": {
7 | "no-unused-expression": true
8 | },
9 | "rules": {
10 | "eofline": false,
11 | "quotemark": [
12 | true,
13 | "single"
14 | ],
15 | "indent": false,
16 | "ordered-imports": [
17 | false
18 | ],
19 | "max-line-length": [
20 | 150
21 | ],
22 | "member-ordering": [
23 | false
24 | ],
25 | "curly": false,
26 | "interface-name": [
27 | false
28 | ],
29 | "array-type": [
30 | false
31 | ],
32 | "member-access": [
33 | false
34 | ],
35 | "no-empty-interface": false,
36 | "no-empty": false,
37 | "arrow-parens": false,
38 | "object-literal-sort-keys": false,
39 | "no-unused-expression": false,
40 | "max-classes-per-file": [
41 | false
42 | ],
43 | "variable-name": [
44 | false
45 | ],
46 | "one-line": [
47 | false
48 | ],
49 | "one-variable-per-declaration": [
50 | false
51 | ]
52 | },
53 | "rulesDirectory": []
54 | }
--------------------------------------------------------------------------------
/generators/app/templates/03-microservices/.gitignore:
--------------------------------------------------------------------------------
1 | # dependencies
2 | /node_modules
3 |
4 | # IDE
5 | /.idea
6 | /.awcache
7 | /.vscode
8 |
9 | # misc
10 | npm-debug.log
11 |
12 | # example
13 | /quick-start
14 |
15 | # tests
16 | /test
17 | /coverage
18 | /.nyc_output
19 |
20 | # dist
21 | /dist
--------------------------------------------------------------------------------
/generators/app/templates/03-microservices/README.md:
--------------------------------------------------------------------------------
1 | # <%= config.name %>
2 |
3 | <%= config.description %>
--------------------------------------------------------------------------------
/generators/app/templates/03-microservices/index.js:
--------------------------------------------------------------------------------
1 | require('ts-node/register');
2 | require('./src/server');
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/generators/app/templates/03-microservices/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "<%= config.identifier %>",
3 | "version": "1.0.0",
4 | "description": "<%= config.description %>",
5 | "license": "MIT",
6 | "publisher": "<%= config.publisher %>",
7 | "scripts": {
8 | "start": "node index.js",
9 | "prestart:prod": "tsc",
10 | "start:prod": "node dist/server.js"
11 | },
12 | "dependencies": {
13 | "@nestjs/common": "^4.3.0",
14 | "@nestjs/core": "^4.3.0",
15 | "@nestjs/microservices": "^4.3.0",
16 | "@nestjs/testing": "^4.3.0",
17 | "@nestjs/websockets": "^4.3.0",
18 | "amqplib": "^0.5.1",
19 | "class-transformer": "^0.1.7",
20 | "class-validator": "^0.7.2",
21 | "redis": "^2.7.1",
22 | "reflect-metadata": "^0.1.10",
23 | "rxjs": "^5.4.3",
24 | "typescript": "^2.4.2"
25 | },
26 | "devDependencies": {
27 | "@types/amqplib": "^0.5.4",
28 | "@types/node": "^7.0.41",
29 | "ts-node": "^3.3.0"
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/generators/app/templates/03-microservices/src/modules/app.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { MathModule } from './math/math.module';
3 |
4 | @Module({
5 | modules: [MathModule],
6 | })
7 | export class ApplicationModule {}
--------------------------------------------------------------------------------
/generators/app/templates/03-microservices/src/modules/common/adapters/rabbitmq-client.ts:
--------------------------------------------------------------------------------
1 | import * as amqp from 'amqplib';
2 | import { ClientProxy } from '@nestjs/microservices';
3 |
4 | export class RabbitMQClient extends ClientProxy {
5 | constructor(
6 | private readonly host: string,
7 | private readonly queue: string) {
8 | super();
9 | }
10 |
11 | protected async sendSingleMessage(messageObj, callback: (err, result, disposed?: boolean) => void) {
12 | const server = await amqp.connect(this.host);
13 | const channel = await server.createChannel();
14 | const { sub, pub } = this.getQueues();
15 |
16 | channel.assertQueue(sub, { durable: false });
17 | channel.assertQueue(pub, { durable: false });
18 |
19 | channel.consume(pub, (message) => this.handleMessage(message, server, callback), { noAck: true });
20 | channel.sendToQueue(sub, Buffer.from(JSON.stringify(messageObj)));
21 | }
22 |
23 | private handleMessage(message, server, callback: (err, result, disposed?: boolean) => void) {
24 | const { content } = message;
25 | const { err, response, disposed } = JSON.parse(content.toString());
26 | if (disposed) {
27 | server.close();
28 | }
29 | callback(err, response, disposed);
30 | }
31 |
32 | private getQueues() {
33 | return { pub: `${this.queue}_pub`, sub: `${this.queue}_sub` };
34 | }
35 | }
--------------------------------------------------------------------------------
/generators/app/templates/03-microservices/src/modules/common/adapters/rabbitmq-server.ts:
--------------------------------------------------------------------------------
1 | import * as amqp from 'amqplib';
2 | import { Server, CustomTransportStrategy } from '@nestjs/microservices';
3 | import { Observable } from 'rxjs/Observable';
4 |
5 | export class RabbitMQServer extends Server implements CustomTransportStrategy {
6 | private server: amqp.Connection = null;
7 | private channel: amqp.Channel = null;
8 |
9 | constructor(
10 | private readonly host: string,
11 | private readonly queue: string) {
12 | super();
13 | }
14 |
15 | public async listen(callback: () => void) {
16 | await this.init();
17 | this.channel.consume(`${this.queue}_sub`, this.handleMessage.bind(this), {
18 | noAck: true,
19 | });
20 | }
21 |
22 | public close() {
23 | this.channel && this.channel.close();
24 | this.server && this.server.close();
25 | }
26 |
27 | private async handleMessage(message) {
28 | const { content } = message;
29 | const messageObj = JSON.parse(content.toString());
30 |
31 | const handlers = this.getHandlers();
32 | const pattern = JSON.stringify(messageObj.pattern);
33 | if (!this.messageHandlers[pattern]) {
34 | return;
35 | }
36 |
37 | const handler = this.messageHandlers[pattern];
38 | const response$ = this.transformToObservable(await handler(messageObj.data)) as Observable;
39 | response$ && this.send(response$, (data) => this.sendMessage(data));
40 | }
41 |
42 | private sendMessage(message) {
43 | const buffer = Buffer.from(JSON.stringify(message));
44 | this.channel.sendToQueue(`${this.queue}_pub`, buffer);
45 | }
46 |
47 | private async init() {
48 | this.server = await amqp.connect(this.host);
49 | this.channel = await this.server.createChannel();
50 | this.channel.assertQueue(`${this.queue}_sub`, { durable: false });
51 | this.channel.assertQueue(`${this.queue}_pub`, { durable: false });
52 | }
53 | }
--------------------------------------------------------------------------------
/generators/app/templates/03-microservices/src/modules/common/decorators/roles.decorator.ts:
--------------------------------------------------------------------------------
1 | import { ReflectMetadata } from '@nestjs/common';
2 |
3 | export const Roles = (...roles: string[]) => ReflectMetadata('roles', roles);
--------------------------------------------------------------------------------
/generators/app/templates/03-microservices/src/modules/common/filters/rpc-exception.filter.ts:
--------------------------------------------------------------------------------
1 | import { Catch, RpcExceptionFilter } from '@nestjs/common';
2 | import { Observable } from 'rxjs/Observable';
3 | import { RpcException } from '@nestjs/microservices';
4 | import 'rxjs/add/observable/throw';
5 |
6 | @Catch(RpcException)
7 | export class ExceptionFilter implements RpcExceptionFilter {
8 | catch(exception: RpcException): Observable {
9 | return Observable.throw(exception.getError());
10 | }
11 | }
--------------------------------------------------------------------------------
/generators/app/templates/03-microservices/src/modules/common/guards/roles.guard.ts:
--------------------------------------------------------------------------------
1 | import { Guard, CanActivate, ExecutionContext } from '@nestjs/common';
2 | import { Observable } from 'rxjs/Observable';
3 | import { Reflector } from '@nestjs/core';
4 |
5 | @Guard()
6 | export class RolesGuard implements CanActivate {
7 | constructor(private readonly reflector: Reflector) {}
8 |
9 | canActivate(data, context: ExecutionContext): boolean {
10 | const { parent, handler } = context;
11 | const roles = this.reflector.get('roles', handler);
12 | if (!roles) {
13 | return true;
14 | }
15 |
16 | const user = data.user;
17 | const hasRole = () => !!user.roles.find((role) => !!roles.find((item) => item === role));
18 | return user && user.roles && hasRole();
19 | }
20 | }
--------------------------------------------------------------------------------
/generators/app/templates/03-microservices/src/modules/common/interceptors/cache.interceptor.ts:
--------------------------------------------------------------------------------
1 | import { Interceptor, NestInterceptor, ExecutionContext } from '@nestjs/common';
2 | import { Observable } from 'rxjs/Observable';
3 | import 'rxjs/add/observable/of';
4 |
5 | @Interceptor()
6 | export class CacheInterceptor implements NestInterceptor {
7 | intercept(dataOrRequest, context: ExecutionContext, stream$: Observable): Observable {
8 | const isCached = true;
9 | if (isCached) {
10 | return Observable.of([]);
11 | }
12 | return stream$;
13 | }
14 | }
--------------------------------------------------------------------------------
/generators/app/templates/03-microservices/src/modules/common/interceptors/logging.interceptor.ts:
--------------------------------------------------------------------------------
1 | import { Interceptor, NestInterceptor, ExecutionContext } from '@nestjs/common';
2 | import { Observable } from 'rxjs/Observable';
3 | import 'rxjs/add/operator/do';
4 |
5 | @Interceptor()
6 | export class LoggingInterceptor implements NestInterceptor {
7 | intercept(dataOrRequest, context: ExecutionContext, stream$: Observable): Observable {
8 | console.log('Before...');
9 | const now = Date.now();
10 |
11 | return stream$.do(
12 | () => console.log(`After... ${Date.now() - now}ms`),
13 | );
14 | }
15 | }
--------------------------------------------------------------------------------
/generators/app/templates/03-microservices/src/modules/common/interceptors/transform.interceptor.ts:
--------------------------------------------------------------------------------
1 | import { Interceptor, NestInterceptor, ExecutionContext } from '@nestjs/common';
2 | import { Observable } from 'rxjs/Observable';
3 | import 'rxjs/add/operator/map';
4 |
5 | @Interceptor()
6 | export class TransformInterceptor implements NestInterceptor {
7 | intercept(dataOrRequest, context: ExecutionContext, stream$: Observable): Observable {
8 | return stream$.map((data) => ({ data }));
9 | }
10 | }
--------------------------------------------------------------------------------
/generators/app/templates/03-microservices/src/modules/common/pipes/validation.pipe.ts:
--------------------------------------------------------------------------------
1 | import { PipeTransform, Pipe, ArgumentMetadata } from '@nestjs/common';
2 | import { validate } from 'class-validator';
3 | import { plainToClass } from 'class-transformer';
4 | import { RpcException } from '@nestjs/microservices';
5 |
6 | @Pipe()
7 | export class ValidationPipe implements PipeTransform {
8 | async transform(value, metadata: ArgumentMetadata) {
9 | const { metatype } = metadata;
10 | if (!metatype || !this.toValidate(metatype)) {
11 | return value;
12 | }
13 | const object = plainToClass(metatype, value);
14 | const errors = await validate(object);
15 | if (errors.length > 0) {
16 | throw new RpcException('Validation failed');
17 | }
18 | return value;
19 | }
20 |
21 | private toValidate(metatype): boolean {
22 | const types = [String, Boolean, Number, Array, Object];
23 | return !types.find((type) => metatype === type);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/generators/app/templates/03-microservices/src/modules/math/math.controller.ts:
--------------------------------------------------------------------------------
1 | import { Controller, Get, UseInterceptors } from '@nestjs/common';
2 | import { ClientProxy, Client, Transport, MessagePattern } from '@nestjs/microservices';
3 | import { Observable } from 'rxjs/Observable';
4 | import { LoggingInterceptor } from '../common/interceptors/logging.interceptor';
5 |
6 | @Controller()
7 | @UseInterceptors(LoggingInterceptor)
8 | export class MathController {
9 | @Client({ transport: Transport.TCP })
10 | client: ClientProxy;
11 |
12 | @Get()
13 | call(): Observable {
14 | const pattern = { cmd: 'sum' };
15 | const data = [1, 2, 3, 4, 5];
16 | return this.client.send(pattern, data);
17 | }
18 |
19 | @MessagePattern({ cmd: 'sum' })
20 | sum(data: number[]): number {
21 | return (data || []).reduce((a, b) => a + b);
22 | }
23 | }
--------------------------------------------------------------------------------
/generators/app/templates/03-microservices/src/modules/math/math.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { MathController } from './math.controller';
3 |
4 | @Module({
5 | controllers: [MathController],
6 | })
7 | export class MathModule {}
--------------------------------------------------------------------------------
/generators/app/templates/03-microservices/src/server.ts:
--------------------------------------------------------------------------------
1 | import { NestFactory } from '@nestjs/core';
2 | import { ApplicationModule } from './modules/app.module';
3 | import { Transport } from '@nestjs/microservices';
4 |
5 | async function bootstrap() {
6 | const app = await NestFactory.create(ApplicationModule);
7 | app.connectMicroservice({
8 | transport: Transport.TCP,
9 | });
10 |
11 | await app.startAllMicroservicesAsync();
12 | await app.listen(3001);
13 | }
14 | bootstrap();
15 |
--------------------------------------------------------------------------------
/generators/app/templates/03-microservices/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "declaration": false,
5 | "noImplicitAny": false,
6 | "removeComments": true,
7 | "noLib": false,
8 | "emitDecoratorMetadata": true,
9 | "experimentalDecorators": true,
10 | "target": "es6",
11 | "sourceMap": true,
12 | "allowJs": true,
13 | "outDir": "./dist"
14 | },
15 | "include": [
16 | "src/**/*"
17 | ],
18 | "exclude": [
19 | "node_modules",
20 | "**/*.spec.ts"
21 | ]
22 | }
--------------------------------------------------------------------------------
/generators/app/templates/03-microservices/tslint.json:
--------------------------------------------------------------------------------
1 | {
2 | "defaultSeverity": "error",
3 | "extends": [
4 | "tslint:recommended"
5 | ],
6 | "jsRules": {
7 | "no-unused-expression": true
8 | },
9 | "rules": {
10 | "eofline": false,
11 | "quotemark": [
12 | true,
13 | "single"
14 | ],
15 | "ordered-imports": [
16 | false
17 | ],
18 | "max-line-length": [
19 | 150
20 | ],
21 | "member-ordering": [
22 | false
23 | ],
24 | "curly": false,
25 | "interface-name": [
26 | false
27 | ],
28 | "array-type": [
29 | false
30 | ],
31 | "member-access": [
32 | false
33 | ],
34 | "no-empty-interface": false,
35 | "no-empty": false,
36 | "arrow-parens": false,
37 | "object-literal-sort-keys": false,
38 | "no-unused-expression": false,
39 | "max-classes-per-file": [
40 | false
41 | ],
42 | "variable-name": [
43 | false
44 | ],
45 | "one-line": [
46 | false
47 | ],
48 | "one-variable-per-declaration": [
49 | false
50 | ]
51 | },
52 | "rulesDirectory": []
53 | }
--------------------------------------------------------------------------------
/generators/app/templates/04-injector/.gitignore:
--------------------------------------------------------------------------------
1 | # dependencies
2 | /node_modules
3 |
4 | # IDE
5 | /.idea
6 | /.awcache
7 | /.vscode
8 |
9 | # misc
10 | npm-debug.log
11 |
12 | # example
13 | /quick-start
14 |
15 | # tests
16 | /test
17 | /coverage
18 | /.nyc_output
19 |
20 | # dist
21 | /dist
--------------------------------------------------------------------------------
/generators/app/templates/04-injector/README.md:
--------------------------------------------------------------------------------
1 | # <%= config.name %>
2 |
3 | <%= config.description %>
--------------------------------------------------------------------------------
/generators/app/templates/04-injector/index.js:
--------------------------------------------------------------------------------
1 | require('ts-node/register');
2 | require('./src/server');
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/generators/app/templates/04-injector/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "<%= config.identifier %>",
3 | "version": "1.0.0",
4 | "description": "<%= config.description %>",
5 | "license": "MIT",
6 | "publisher": "<%= config.publisher %>",
7 | "scripts": {
8 | "start": "node index.js",
9 | "prestart:prod": "tsc",
10 | "start:prod": "node dist/server.js"
11 | },
12 | "dependencies": {
13 | "@nestjs/common": "^4.3.0",
14 | "@nestjs/core": "^4.3.0",
15 | "@nestjs/microservices": "^4.3.0",
16 | "@nestjs/testing": "^4.3.0",
17 | "@nestjs/websockets": "^4.3.0",
18 | "redis": "^2.7.1",
19 | "reflect-metadata": "^0.1.10",
20 | "rxjs": "^5.4.3",
21 | "typescript": "^2.4.2"
22 | },
23 | "devDependencies": {
24 | "@types/node": "^7.0.41",
25 | "ts-node": "^3.3.0"
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/generators/app/templates/04-injector/src/modules/app.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { CoreModule } from './core/core.module';
3 | import { FeatureModule } from './feature/feature.module';
4 |
5 | @Module({
6 | modules: [FeatureModule],
7 | })
8 | export class ApplicationModule {}
--------------------------------------------------------------------------------
/generators/app/templates/04-injector/src/modules/common/common.module.ts:
--------------------------------------------------------------------------------
1 | import { Module, SingleScope } from '@nestjs/common';
2 | import { CommonService } from './common.service';
3 |
4 | @Module({
5 | components: [CommonService],
6 | exports: [CommonService],
7 | })
8 | export class CommonModule {}
--------------------------------------------------------------------------------
/generators/app/templates/04-injector/src/modules/common/common.service.ts:
--------------------------------------------------------------------------------
1 | import { Component } from '@nestjs/common';
2 | import { CoreService } from '../core/core.service';
3 |
4 | @Component()
5 | export class CommonService {
6 | constructor(private readonly coreService: CoreService) {
7 | console.log('CommonService', coreService);
8 | }
9 | }
--------------------------------------------------------------------------------
/generators/app/templates/04-injector/src/modules/core/context.service.ts:
--------------------------------------------------------------------------------
1 | import { Component } from '@nestjs/common';
2 | import { CommonService } from '../common/common.service';
3 | import { CoreService } from './core.service';
4 |
5 | @Component()
6 | export class ContextService {
7 | constructor(private readonly commonService: CoreService) {
8 | console.log('ContextService', commonService);
9 | }
10 | }
--------------------------------------------------------------------------------
/generators/app/templates/04-injector/src/modules/core/core.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { CoreService } from './core.service';
3 | import { CommonModule } from '../common/common.module';
4 | import { ContextService } from './context.service';
5 | import { FeatureModule } from '../feature/feature.module';
6 |
7 | @Module({
8 | modules: [CommonModule],
9 | components: [CoreService, ContextService],
10 | exports: [CommonModule],
11 | })
12 | export class CoreModule {}
--------------------------------------------------------------------------------
/generators/app/templates/04-injector/src/modules/core/core.service.ts:
--------------------------------------------------------------------------------
1 | import { Component } from '@nestjs/common';
2 |
3 | @Component()
4 | export class CoreService {
5 | constructor() {
6 | console.log('CoreService');
7 | }
8 | }
--------------------------------------------------------------------------------
/generators/app/templates/04-injector/src/modules/feature/feature.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { FeatureService } from './feature.service';
3 | import { CoreModule } from '../core/core.module';
4 |
5 | @Module({
6 | modules: [CoreModule],
7 | components: [FeatureService],
8 | })
9 | export class FeatureModule {}
--------------------------------------------------------------------------------
/generators/app/templates/04-injector/src/modules/feature/feature.service.ts:
--------------------------------------------------------------------------------
1 | import { Component } from '@nestjs/common';
2 | import { CommonService } from '../common/common.service';
3 |
4 | @Component()
5 | export class FeatureService {
6 | constructor(
7 | private readonly commonService: CommonService) {
8 | console.log('FeatureService', commonService);
9 | }
10 | }
--------------------------------------------------------------------------------
/generators/app/templates/04-injector/src/server.ts:
--------------------------------------------------------------------------------
1 | import { NestFactory } from '@nestjs/core';
2 | import { ApplicationModule } from './modules/app.module';
3 |
4 | async function bootstrap() {
5 | const app = await NestFactory.create(ApplicationModule);
6 | await app.listen(3001);
7 | }
8 | bootstrap();
9 |
--------------------------------------------------------------------------------
/generators/app/templates/04-injector/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "declaration": false,
5 | "noImplicitAny": false,
6 | "removeComments": true,
7 | "noLib": false,
8 | "emitDecoratorMetadata": true,
9 | "experimentalDecorators": true,
10 | "target": "es6",
11 | "sourceMap": true,
12 | "allowJs": true,
13 | "outDir": "./dist"
14 | },
15 | "include": [
16 | "src/**/*"
17 | ],
18 | "exclude": [
19 | "node_modules",
20 | "**/*.spec.ts"
21 | ]
22 | }
--------------------------------------------------------------------------------
/generators/app/templates/04-injector/tslint.json:
--------------------------------------------------------------------------------
1 | {
2 | "defaultSeverity": "error",
3 | "extends": [
4 | "tslint:recommended"
5 | ],
6 | "jsRules": {
7 | "no-unused-expression": true
8 | },
9 | "rules": {
10 | "eofline": false,
11 | "quotemark": [
12 | true,
13 | "single"
14 | ],
15 | "ordered-imports": [
16 | false
17 | ],
18 | "max-line-length": [
19 | 150
20 | ],
21 | "member-ordering": [
22 | false
23 | ],
24 | "curly": false,
25 | "interface-name": [
26 | false
27 | ],
28 | "array-type": [
29 | false
30 | ],
31 | "member-access": [
32 | false
33 | ],
34 | "no-empty-interface": false,
35 | "no-empty": false,
36 | "arrow-parens": false,
37 | "object-literal-sort-keys": false,
38 | "no-unused-expression": false,
39 | "max-classes-per-file": [
40 | false
41 | ],
42 | "variable-name": [
43 | false
44 | ],
45 | "one-line": [
46 | false
47 | ],
48 | "one-variable-per-declaration": [
49 | false
50 | ]
51 | },
52 | "rulesDirectory": []
53 | }
--------------------------------------------------------------------------------
/generators/app/templates/05-sql-typeorm/.gitignore:
--------------------------------------------------------------------------------
1 | # dependencies
2 | /node_modules
3 |
4 | # IDE
5 | /.idea
6 | /.awcache
7 | /.vscode
8 |
9 | # misc
10 | npm-debug.log
11 |
12 | # example
13 | /quick-start
14 |
15 | # tests
16 | /test
17 | /coverage
18 | /.nyc_output
19 |
20 | # dist
21 | /dist
--------------------------------------------------------------------------------
/generators/app/templates/05-sql-typeorm/README.md:
--------------------------------------------------------------------------------
1 | # <%= config.name %>
2 |
3 | <%= config.description %>
--------------------------------------------------------------------------------
/generators/app/templates/05-sql-typeorm/index.js:
--------------------------------------------------------------------------------
1 | require('ts-node/register');
2 | require('./src/server');
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/generators/app/templates/05-sql-typeorm/ormconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "type": "mysql",
3 | "host": "localhost",
4 | "port": 3306,
5 | "username": "root",
6 | "password": "root",
7 | "database": "test",
8 | "entities": ["src/**/**.entity{.ts,.js}"],
9 | "autoSchemaSync": true
10 | }
--------------------------------------------------------------------------------
/generators/app/templates/05-sql-typeorm/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "<%= config.identifier %>",
3 | "version": "1.0.0",
4 | "description": "<%= config.description %>",
5 | "license": "MIT",
6 | "publisher": "<%= config.publisher %>",
7 | "scripts": {
8 | "start": "node index.js",
9 | "prestart:prod": "tsc",
10 | "start:prod": "node dist/server.js"
11 | },
12 | "dependencies": {
13 | "@nestjs/common": "^4.5.0",
14 | "@nestjs/core": "^4.5.0",
15 | "@nestjs/microservices": "^4.3.0",
16 | "@nestjs/testing": "^4.3.0",
17 | "@nestjs/typeorm": "^1.0.0",
18 | "@nestjs/websockets": "^4.3.0",
19 | "mysql": "^2.14.1",
20 | "redis": "^2.7.1",
21 | "reflect-metadata": "^0.1.10",
22 | "rxjs": "^5.4.3",
23 | "typeorm": "0.1.0-alpha.40",
24 | "typescript": "^2.4.2"
25 | },
26 | "devDependencies": {
27 | "@types/node": "^7.0.41",
28 | "ts-node": "^3.3.0"
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/generators/app/templates/05-sql-typeorm/src/modules/app.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { TypeOrmModule } from '@nestjs/typeorm';
3 |
4 | import { PhotoModule } from './photo/photo.module';
5 | import { Photo } from './photo/photo.entity';
6 |
7 | @Module({
8 | modules: [
9 | TypeOrmModule.forRoot([Photo]),
10 | PhotoModule,
11 | ],
12 | })
13 | export class ApplicationModule {}
14 |
--------------------------------------------------------------------------------
/generators/app/templates/05-sql-typeorm/src/modules/photo/photo.controller.ts:
--------------------------------------------------------------------------------
1 | import { Controller, Get } from '@nestjs/common';
2 | import { PhotoService } from './photo.service';
3 | import { Photo } from './photo.entity';
4 |
5 | @Controller('photo')
6 | export class PhotoController {
7 | constructor(private readonly photoService: PhotoService) {}
8 |
9 | @Get()
10 | findAll(): Promise {
11 | return this.photoService.findAll();
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/generators/app/templates/05-sql-typeorm/src/modules/photo/photo.entity.ts:
--------------------------------------------------------------------------------
1 | import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
2 |
3 | @Entity()
4 | export class Photo {
5 | @PrimaryGeneratedColumn()
6 | id: number;
7 |
8 | @Column({ length: 500 })
9 | name: string;
10 |
11 | @Column('text')
12 | description: string;
13 |
14 | @Column()
15 | filename: string;
16 |
17 | @Column('int')
18 | views: number;
19 |
20 | @Column()
21 | isPublished: boolean;
22 | }
--------------------------------------------------------------------------------
/generators/app/templates/05-sql-typeorm/src/modules/photo/photo.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { PhotoService } from './photo.service';
3 | import { PhotoController } from './photo.controller';
4 |
5 | @Module({
6 | components: [PhotoService],
7 | controllers: [PhotoController],
8 | })
9 | export class PhotoModule {}
10 |
--------------------------------------------------------------------------------
/generators/app/templates/05-sql-typeorm/src/modules/photo/photo.service.ts:
--------------------------------------------------------------------------------
1 | import { Component, Inject } from '@nestjs/common';
2 | import { InjectRepository } from '@nestjs/typeorm';
3 | import { Repository } from 'typeorm';
4 |
5 | import { Photo } from './photo.entity';
6 |
7 | @Component()
8 | export class PhotoService {
9 | constructor(
10 | @InjectRepository(Photo)
11 | private readonly photoRepository: Repository,
12 | ) {}
13 |
14 | async findAll(): Promise {
15 | return await this.photoRepository.find();
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/generators/app/templates/05-sql-typeorm/src/server.ts:
--------------------------------------------------------------------------------
1 | import { NestFactory } from '@nestjs/core';
2 | import { ApplicationModule } from './modules/app.module';
3 |
4 | async function bootstrap() {
5 | const app = await NestFactory.create(ApplicationModule);
6 | await app.listen(3001);
7 | }
8 | bootstrap();
9 |
--------------------------------------------------------------------------------
/generators/app/templates/05-sql-typeorm/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "declaration": false,
5 | "noImplicitAny": false,
6 | "removeComments": true,
7 | "noLib": false,
8 | "emitDecoratorMetadata": true,
9 | "experimentalDecorators": true,
10 | "target": "es6",
11 | "sourceMap": true,
12 | "allowJs": true,
13 | "outDir": "./dist"
14 | },
15 | "include": [
16 | "src/**/*"
17 | ],
18 | "exclude": [
19 | "node_modules",
20 | "**/*.spec.ts"
21 | ]
22 | }
--------------------------------------------------------------------------------
/generators/app/templates/05-sql-typeorm/tslint.json:
--------------------------------------------------------------------------------
1 | {
2 | "defaultSeverity": "error",
3 | "extends": [
4 | "tslint:recommended"
5 | ],
6 | "jsRules": {
7 | "no-unused-expression": true
8 | },
9 | "rules": {
10 | "eofline": false,
11 | "quotemark": [
12 | true,
13 | "single"
14 | ],
15 | "ban-types": false,
16 | "indent": false,
17 | "ordered-imports": [
18 | false
19 | ],
20 | "max-line-length": [
21 | 150
22 | ],
23 | "member-ordering": [
24 | false
25 | ],
26 | "curly": false,
27 | "interface-name": [
28 | false
29 | ],
30 | "array-type": [
31 | false
32 | ],
33 | "member-access": [
34 | false
35 | ],
36 | "no-empty-interface": false,
37 | "no-empty": false,
38 | "arrow-parens": false,
39 | "object-literal-sort-keys": false,
40 | "no-unused-expression": false,
41 | "max-classes-per-file": [
42 | false
43 | ],
44 | "variable-name": [
45 | false
46 | ],
47 | "one-line": [
48 | false
49 | ],
50 | "one-variable-per-declaration": [
51 | false
52 | ]
53 | },
54 | "rulesDirectory": []
55 | }
--------------------------------------------------------------------------------
/generators/app/templates/06-mongoose/.gitignore:
--------------------------------------------------------------------------------
1 | # dependencies
2 | /node_modules
3 |
4 | # IDE
5 | /.idea
6 | /.awcache
7 | /.vscode
8 |
9 | # misc
10 | npm-debug.log
11 |
12 | # example
13 | /quick-start
14 |
15 | # tests
16 | /test
17 | /coverage
18 | /.nyc_output
19 |
20 | # dist
21 | /dist
--------------------------------------------------------------------------------
/generators/app/templates/06-mongoose/README.md:
--------------------------------------------------------------------------------
1 | # <%= config.name %>
2 |
3 | <%= config.description %>
--------------------------------------------------------------------------------
/generators/app/templates/06-mongoose/index.js:
--------------------------------------------------------------------------------
1 | require('ts-node/register');
2 | require('./src/server');
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/generators/app/templates/06-mongoose/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "<%= config.identifier %>",
3 | "version": "1.0.0",
4 | "description": "<%= config.description %>",
5 | "license": "MIT",
6 | "publisher": "<%= config.publisher %>",
7 | "scripts": {
8 | "start": "node index.js",
9 | "prestart:prod": "tsc",
10 | "start:prod": "node dist/server.js"
11 | },
12 | "dependencies": {
13 | "@nestjs/common": "^4.3.0",
14 | "@nestjs/core": "^4.3.0",
15 | "@nestjs/microservices": "^4.3.0",
16 | "@nestjs/testing": "^4.3.0",
17 | "@nestjs/websockets": "^4.3.0",
18 | "mongoose": "^4.11.13",
19 | "redis": "^2.7.1",
20 | "reflect-metadata": "^0.1.10",
21 | "rxjs": "^5.4.3",
22 | "typescript": "^2.4.2"
23 | },
24 | "devDependencies": {
25 | "@types/mongoose": "^4.7.23",
26 | "@types/node": "^7.0.41",
27 | "ts-node": "^3.3.0"
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/generators/app/templates/06-mongoose/src/modules/app.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { CatsModule } from './cats/cats.module';
3 |
4 | @Module({
5 | modules: [CatsModule],
6 | })
7 | export class ApplicationModule {}
--------------------------------------------------------------------------------
/generators/app/templates/06-mongoose/src/modules/cats/cats.controller.ts:
--------------------------------------------------------------------------------
1 | import { Controller, Get, Post, Body, Param } from '@nestjs/common';
2 | import { CreateCatDto } from './dto/create-cat.dto';
3 | import { CatsService } from './cats.service';
4 | import { Cat } from './interfaces/cat.interface';
5 |
6 | @Controller('cats')
7 | export class CatsController {
8 | constructor(private readonly catsService: CatsService) {}
9 |
10 | @Post()
11 | async create(@Body() createCatDto: CreateCatDto) {
12 | this.catsService.create(createCatDto);
13 | }
14 |
15 | @Get()
16 | async findAll(): Promise {
17 | return this.catsService.findAll();
18 | }
19 | }
--------------------------------------------------------------------------------
/generators/app/templates/06-mongoose/src/modules/cats/cats.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { CatsController } from './cats.controller';
3 | import { CatsService } from './cats.service';
4 | import { catsProviders } from './cats.providers';
5 | import { DatabaseModule } from '../database/database.module';
6 |
7 | @Module({
8 | modules: [DatabaseModule],
9 | controllers: [CatsController],
10 | components: [
11 | CatsService,
12 | ...catsProviders,
13 | ],
14 | })
15 | export class CatsModule {}
--------------------------------------------------------------------------------
/generators/app/templates/06-mongoose/src/modules/cats/cats.providers.ts:
--------------------------------------------------------------------------------
1 | import { Connection } from 'mongoose';
2 | import { CatSchema } from './schemas/cat.schema';
3 |
4 | export const catsProviders = [
5 | {
6 | provide: 'CatModelToken',
7 | useFactory: (connection: Connection) => connection.model('Cat', CatSchema),
8 | inject: ['DbConnectionToken'],
9 | },
10 | ];
--------------------------------------------------------------------------------
/generators/app/templates/06-mongoose/src/modules/cats/cats.service.ts:
--------------------------------------------------------------------------------
1 | import { Model } from 'mongoose';
2 | import { Component, Inject } from '@nestjs/common';
3 | import { Cat } from './interfaces/cat.interface';
4 | import { CreateCatDto } from './dto/create-cat.dto';
5 |
6 | @Component()
7 | export class CatsService {
8 | constructor(
9 | @Inject('CatModelToken') private readonly catModel: Model) {}
10 |
11 | async create(createCatDto: CreateCatDto): Promise {
12 | const createdCat = new this.catModel(createCatDto);
13 | return await createdCat.save();
14 | }
15 |
16 | async findAll(): Promise {
17 | return await this.catModel.find().exec();
18 | }
19 | }
--------------------------------------------------------------------------------
/generators/app/templates/06-mongoose/src/modules/cats/dto/create-cat.dto.ts:
--------------------------------------------------------------------------------
1 | export class CreateCatDto {
2 | readonly name: string;
3 | readonly age: number;
4 | readonly breed: string;
5 | }
--------------------------------------------------------------------------------
/generators/app/templates/06-mongoose/src/modules/cats/interfaces/cat.interface.ts:
--------------------------------------------------------------------------------
1 | import { Document } from 'mongoose';
2 |
3 | export interface Cat extends Document {
4 | readonly name: string;
5 | readonly age: number;
6 | readonly breed: string;
7 | }
--------------------------------------------------------------------------------
/generators/app/templates/06-mongoose/src/modules/cats/schemas/cat.schema.ts:
--------------------------------------------------------------------------------
1 | import * as mongoose from 'mongoose';
2 |
3 | export const CatSchema = new mongoose.Schema({
4 | name: String,
5 | age: Number,
6 | breed: String,
7 | });
--------------------------------------------------------------------------------
/generators/app/templates/06-mongoose/src/modules/database/database.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { databaseProviders } from './database.providers';
3 |
4 | @Module({
5 | components: [...databaseProviders],
6 | exports: [...databaseProviders],
7 | })
8 | export class DatabaseModule {}
--------------------------------------------------------------------------------
/generators/app/templates/06-mongoose/src/modules/database/database.providers.ts:
--------------------------------------------------------------------------------
1 | import * as mongoose from 'mongoose';
2 |
3 | export const databaseProviders = [
4 | {
5 | provide: 'DbConnectionToken',
6 | useFactory: async (): Promise => {
7 | (mongoose as any).Promise = global.Promise;
8 | return await mongoose.connect('mongodb://localhost/nest', {
9 | useMongoClient: true,
10 | });
11 | },
12 | },
13 | ];
--------------------------------------------------------------------------------
/generators/app/templates/06-mongoose/src/server.ts:
--------------------------------------------------------------------------------
1 | import { NestFactory } from '@nestjs/core';
2 | import { ApplicationModule } from './modules/app.module';
3 |
4 | async function bootstrap() {
5 | const app = await NestFactory.create(ApplicationModule);
6 | await app.listen(3001);
7 | }
8 | bootstrap();
9 |
--------------------------------------------------------------------------------
/generators/app/templates/06-mongoose/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "declaration": false,
5 | "noImplicitAny": false,
6 | "removeComments": true,
7 | "noLib": false,
8 | "emitDecoratorMetadata": true,
9 | "experimentalDecorators": true,
10 | "target": "es6",
11 | "sourceMap": true,
12 | "allowJs": true,
13 | "outDir": "./dist"
14 | },
15 | "include": [
16 | "src/**/*"
17 | ],
18 | "exclude": [
19 | "node_modules",
20 | "**/*.spec.ts"
21 | ]
22 | }
--------------------------------------------------------------------------------
/generators/app/templates/06-mongoose/tslint.json:
--------------------------------------------------------------------------------
1 | {
2 | "defaultSeverity": "error",
3 | "extends": [
4 | "tslint:recommended"
5 | ],
6 | "jsRules": {
7 | "no-unused-expression": true
8 | },
9 | "rules": {
10 | "eofline": false,
11 | "quotemark": [
12 | true,
13 | "single"
14 | ],
15 | "ordered-imports": [
16 | false
17 | ],
18 | "max-line-length": [
19 | 150
20 | ],
21 | "member-ordering": [
22 | false
23 | ],
24 | "curly": false,
25 | "interface-name": [
26 | false
27 | ],
28 | "array-type": [
29 | false
30 | ],
31 | "member-access": [
32 | false
33 | ],
34 | "no-empty-interface": false,
35 | "no-empty": false,
36 | "arrow-parens": false,
37 | "object-literal-sort-keys": false,
38 | "no-unused-expression": false,
39 | "max-classes-per-file": [
40 | false
41 | ],
42 | "variable-name": [
43 | false
44 | ],
45 | "one-line": [
46 | false
47 | ],
48 | "one-variable-per-declaration": [
49 | false
50 | ]
51 | },
52 | "rulesDirectory": []
53 | }
--------------------------------------------------------------------------------
/generators/app/templates/07-sequelize/.gitignore:
--------------------------------------------------------------------------------
1 | # dependencies
2 | /node_modules
3 |
4 | # IDE
5 | /.idea
6 | /.awcache
7 | /.vscode
8 |
9 | # misc
10 | npm-debug.log
11 |
12 | # example
13 | /quick-start
14 |
15 | # tests
16 | /test
17 | /coverage
18 | /.nyc_output
19 |
20 | # dist
21 | /dist
--------------------------------------------------------------------------------
/generators/app/templates/07-sequelize/README.md:
--------------------------------------------------------------------------------
1 | # <%= config.name %>
2 |
3 | <%= config.description %>
--------------------------------------------------------------------------------
/generators/app/templates/07-sequelize/index.js:
--------------------------------------------------------------------------------
1 | require('ts-node/register');
2 | require('./src/server');
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/generators/app/templates/07-sequelize/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "<%= config.identifier %>",
3 | "version": "1.0.0",
4 | "description": "<%= config.description %>",
5 | "license": "MIT",
6 | "publisher": "<%= config.publisher %>",
7 | "scripts": {
8 | "start": "node index.js",
9 | "prestart:prod": "tsc",
10 | "start:prod": "node dist/server.js"
11 | },
12 | "dependencies": {
13 | "@nestjs/common": "^4.3.0",
14 | "@nestjs/core": "^4.3.0",
15 | "@nestjs/microservices": "^4.3.0",
16 | "@nestjs/testing": "^4.3.0",
17 | "@nestjs/websockets": "^4.3.0",
18 | "mysql2": "^1.4.2",
19 | "redis": "^2.7.1",
20 | "reflect-metadata": "^0.1.10",
21 | "rxjs": "^5.4.3",
22 | "sequelize": "^4.28.0",
23 | "sequelize-typescript": "^0.6.1",
24 | "typescript": "^2.6.2"
25 | },
26 | "devDependencies": {
27 | "@types/node": "^7.0.41",
28 | "@types/sequelize": "^4.0.75",
29 | "ts-node": "^3.3.0"
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/generators/app/templates/07-sequelize/src/modules/app.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { CatsModule } from './cats/cats.module';
3 |
4 | @Module({
5 | modules: [CatsModule],
6 | })
7 | export class ApplicationModule {}
--------------------------------------------------------------------------------
/generators/app/templates/07-sequelize/src/modules/cats/cat.entity.ts:
--------------------------------------------------------------------------------
1 | import { Table, Column, Model } from 'sequelize-typescript';
2 |
3 | @Table
4 | export class Cat extends Model {
5 | @Column
6 | name: string;
7 |
8 | @Column
9 | age: number;
10 |
11 | @Column
12 | breed: string;
13 | }
14 |
--------------------------------------------------------------------------------
/generators/app/templates/07-sequelize/src/modules/cats/cats.controller.ts:
--------------------------------------------------------------------------------
1 | import { Controller, Get, Post, Body, Param } from '@nestjs/common';
2 | import { CreateCatDto } from './dto/create-cat.dto';
3 | import { CatsService } from './cats.service';
4 | import { Cat } from './cat.entity';
5 |
6 | @Controller('cats')
7 | export class CatsController {
8 | constructor(private readonly catsService: CatsService) {}
9 |
10 | @Post()
11 | async create(@Body() createCatDto: CreateCatDto) {
12 | await this.catsService.create(createCatDto);
13 | }
14 |
15 | @Get()
16 | async findAll(): Promise {
17 | return await this.catsService.findAll();
18 | }
19 | }
--------------------------------------------------------------------------------
/generators/app/templates/07-sequelize/src/modules/cats/cats.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { CatsController } from './cats.controller';
3 | import { CatsService } from './cats.service';
4 | import { catsProviders } from './cats.providers';
5 | import { DatabaseModule } from '../database/database.module';
6 |
7 | @Module({
8 | modules: [DatabaseModule],
9 | controllers: [CatsController],
10 | components: [
11 | CatsService,
12 | ...catsProviders,
13 | ],
14 | })
15 | export class CatsModule {}
--------------------------------------------------------------------------------
/generators/app/templates/07-sequelize/src/modules/cats/cats.providers.ts:
--------------------------------------------------------------------------------
1 | import { Cat } from './cat.entity';
2 |
3 | export const catsProviders = [
4 | {
5 | provide: 'CatsRepository',
6 | useValue: Cat,
7 | },
8 | ];
--------------------------------------------------------------------------------
/generators/app/templates/07-sequelize/src/modules/cats/cats.service.ts:
--------------------------------------------------------------------------------
1 |
2 | import { Component, Inject } from '@nestjs/common';
3 | import { CreateCatDto } from './dto/create-cat.dto';
4 | import { Model } from 'sequelize-typescript';
5 | import { Cat } from './cat.entity';
6 |
7 | @Component()
8 | export class CatsService {
9 | constructor(
10 | @Inject('CatsRepository') private readonly catsRepository: typeof Cat) {}
11 |
12 | async create(createCatDto: CreateCatDto): Promise {
13 | const cat = new Cat();
14 | cat.name = createCatDto.name;
15 | cat.breed = createCatDto.breed;
16 | cat.age = createCatDto.age;
17 |
18 | return await cat.save();
19 | }
20 |
21 | async findAll(): Promise {
22 | return await this.catsRepository.findAll();
23 | }
24 | }
--------------------------------------------------------------------------------
/generators/app/templates/07-sequelize/src/modules/cats/dto/create-cat.dto.ts:
--------------------------------------------------------------------------------
1 | export class CreateCatDto {
2 | readonly name: string;
3 | readonly age: number;
4 | readonly breed: string;
5 | }
--------------------------------------------------------------------------------
/generators/app/templates/07-sequelize/src/modules/database/database.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { databaseProviders } from './database.providers';
3 |
4 | @Module({
5 | components: [...databaseProviders],
6 | exports: [...databaseProviders],
7 | })
8 | export class DatabaseModule {}
--------------------------------------------------------------------------------
/generators/app/templates/07-sequelize/src/modules/database/database.providers.ts:
--------------------------------------------------------------------------------
1 | import { Sequelize } from 'sequelize-typescript';
2 | import { Cat } from '../cats/cat.entity';
3 |
4 | export const databaseProviders = [
5 | {
6 | provide: 'SequelizeToken',
7 | useFactory: async () => {
8 | const sequelize = new Sequelize({
9 | dialect: 'mysql',
10 | host: 'localhost',
11 | port: 3306,
12 | username: 'root',
13 | password: '',
14 | database: 'test',
15 | });
16 | sequelize.addModels([Cat]);
17 | await sequelize.sync();
18 | return sequelize;
19 | },
20 | },
21 | ];
--------------------------------------------------------------------------------
/generators/app/templates/07-sequelize/src/server.ts:
--------------------------------------------------------------------------------
1 | import { NestFactory } from '@nestjs/core';
2 | import { ApplicationModule } from './modules/app.module';
3 |
4 | async function bootstrap() {
5 | const app = await NestFactory.create(ApplicationModule);
6 | await app.listen(3001);
7 | }
8 | bootstrap();
9 |
--------------------------------------------------------------------------------
/generators/app/templates/07-sequelize/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "declaration": false,
5 | "noImplicitAny": false,
6 | "removeComments": true,
7 | "noLib": false,
8 | "emitDecoratorMetadata": true,
9 | "experimentalDecorators": true,
10 | "target": "es6",
11 | "sourceMap": true,
12 | "allowJs": true,
13 | "outDir": "./dist"
14 | },
15 | "include": [
16 | "src/**/*"
17 | ],
18 | "exclude": [
19 | "node_modules",
20 | "**/*.spec.ts"
21 | ]
22 | }
--------------------------------------------------------------------------------
/generators/app/templates/07-sequelize/tslint.json:
--------------------------------------------------------------------------------
1 | {
2 | "defaultSeverity": "error",
3 | "extends": [
4 | "tslint:recommended"
5 | ],
6 | "jsRules": {
7 | "no-unused-expression": true
8 | },
9 | "rules": {
10 | "eofline": false,
11 | "quotemark": [
12 | true,
13 | "single"
14 | ],
15 | "ordered-imports": [
16 | false
17 | ],
18 | "max-line-length": [
19 | 150
20 | ],
21 | "member-ordering": [
22 | false
23 | ],
24 | "curly": false,
25 | "interface-name": [
26 | false
27 | ],
28 | "array-type": [
29 | false
30 | ],
31 | "member-access": [
32 | false
33 | ],
34 | "no-empty-interface": false,
35 | "no-empty": false,
36 | "arrow-parens": false,
37 | "object-literal-sort-keys": false,
38 | "no-unused-expression": false,
39 | "max-classes-per-file": [
40 | false
41 | ],
42 | "variable-name": [
43 | false
44 | ],
45 | "one-line": [
46 | false
47 | ],
48 | "one-variable-per-declaration": [
49 | false
50 | ]
51 | },
52 | "rulesDirectory": []
53 | }
--------------------------------------------------------------------------------
/generators/app/templates/08-passport/.gitignore:
--------------------------------------------------------------------------------
1 | # dependencies
2 | /node_modules
3 |
4 | # IDE
5 | /.idea
6 | /.awcache
7 | /.vscode
8 |
9 | # misc
10 | npm-debug.log
11 |
12 | # example
13 | /quick-start
14 |
15 | # tests
16 | /test
17 | /coverage
18 | /.nyc_output
19 |
20 | # dist
21 | /dist
--------------------------------------------------------------------------------
/generators/app/templates/08-passport/README.md:
--------------------------------------------------------------------------------
1 | # <%= config.name %>
2 |
3 | <%= config.description %>
--------------------------------------------------------------------------------
/generators/app/templates/08-passport/index.js:
--------------------------------------------------------------------------------
1 | require('ts-node/register');
2 | require('./src/server');
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/generators/app/templates/08-passport/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "<%= config.identifier %>",
3 | "version": "1.0.0",
4 | "description": "<%= config.description %>",
5 | "license": "MIT",
6 | "publisher": "<%= config.publisher %>",
7 | "scripts": {
8 | "start": "node index.js",
9 | "prestart:prod": "tsc",
10 | "start:prod": "node dist/server.js"
11 | },
12 | "dependencies": {
13 | "@nestjs/common": "^4.3.0",
14 | "@nestjs/core": "^4.3.0",
15 | "@nestjs/microservices": "^4.3.0",
16 | "@nestjs/testing": "^4.3.0",
17 | "@nestjs/websockets": "^4.3.0",
18 | "@types/passport-jwt": "^2.0.24",
19 | "jsonwebtoken": "^8.0.1",
20 | "passport": "^0.4.0",
21 | "passport-jwt": "^3.0.0",
22 | "redis": "^2.7.1",
23 | "reflect-metadata": "^0.1.10",
24 | "rxjs": "^5.4.3",
25 | "typescript": "^2.4.2"
26 | },
27 | "devDependencies": {
28 | "@types/node": "^7.0.41",
29 | "ts-node": "^3.3.0"
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/generators/app/templates/08-passport/src/modules/app.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { AuthModule } from './auth/auth.module';
3 |
4 | @Module({
5 | modules: [AuthModule],
6 | })
7 | export class ApplicationModule {}
--------------------------------------------------------------------------------
/generators/app/templates/08-passport/src/modules/auth/auth.controller.ts:
--------------------------------------------------------------------------------
1 | import { Controller, Post, HttpStatus, HttpCode, Get } from '@nestjs/common';
2 | import { AuthService } from './auth.service';
3 |
4 | @Controller('auth')
5 | export class AuthController {
6 | constructor(private readonly authService: AuthService) {}
7 |
8 | @Post('token')
9 | @HttpCode(HttpStatus.OK)
10 | public async getToken() {
11 | return await this.authService.createToken();
12 | }
13 |
14 | @Get('authorized')
15 | public async authorized() {
16 | console.log('Authorized route...');
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/generators/app/templates/08-passport/src/modules/auth/auth.module.ts:
--------------------------------------------------------------------------------
1 | import * as passport from 'passport';
2 | import {
3 | Module,
4 | NestModule,
5 | MiddlewaresConsumer,
6 | RequestMethod,
7 | } from '@nestjs/common';
8 | import { AuthService } from './auth.service';
9 | import { JwtStrategy } from './passport/jwt.strategy';
10 | import { AuthController } from './auth.controller';
11 |
12 | @Module({
13 | components: [
14 | AuthService,
15 | JwtStrategy,
16 | ],
17 | controllers: [AuthController],
18 | })
19 | export class AuthModule implements NestModule {
20 | public configure(consumer: MiddlewaresConsumer) {
21 | consumer
22 | .apply(passport.authenticate('jwt', { session: false }))
23 | .forRoutes({ path: '/auth/authorized', method: RequestMethod.ALL });
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/generators/app/templates/08-passport/src/modules/auth/auth.service.ts:
--------------------------------------------------------------------------------
1 | import * as jwt from 'jsonwebtoken';
2 | import { Component, Inject } from '@nestjs/common';
3 |
4 | @Component()
5 | export class AuthService {
6 | async createToken() {
7 | const expiresIn = 60 * 60, secretOrKey = 'secret';
8 | const user = { email: 'thisis@example.com' };
9 | const token = jwt.sign(user, secretOrKey, { expiresIn });
10 | return {
11 | expires_in: expiresIn,
12 | access_token: token,
13 | };
14 | }
15 |
16 | async validateUser(signedUser): Promise {
17 | // put some validation logic here
18 | // for example query user by id / email / username
19 | return true;
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/generators/app/templates/08-passport/src/modules/auth/passport/jwt.strategy.ts:
--------------------------------------------------------------------------------
1 | import * as passport from 'passport';
2 | import { ExtractJwt, Strategy } from 'passport-jwt';
3 | import { Component, Inject } from '@nestjs/common';
4 | import { AuthService } from '../auth.service';
5 |
6 | @Component()
7 | export class JwtStrategy extends Strategy {
8 | constructor(private readonly authService: AuthService) {
9 | super(
10 | {
11 | jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
12 | passReqToCallback: true,
13 | secretOrKey: 'secret',
14 | },
15 | async (req, payload, next) => await this.verify(req, payload, next)
16 | );
17 | passport.use(this);
18 | }
19 |
20 | public async verify(req, payload, done) {
21 | const isValid = await this.authService.validateUser(payload);
22 | if (!isValid) {
23 | return done('Unauthorized', false);
24 | }
25 | done(null, payload);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/generators/app/templates/08-passport/src/server.ts:
--------------------------------------------------------------------------------
1 | import { NestFactory } from '@nestjs/core';
2 | import { ApplicationModule } from './modules/app.module';
3 |
4 | async function bootstrap() {
5 | const app = await NestFactory.create(ApplicationModule);
6 | await app.listen(3001);
7 | }
8 | bootstrap();
9 |
--------------------------------------------------------------------------------
/generators/app/templates/08-passport/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "declaration": false,
5 | "noImplicitAny": false,
6 | "removeComments": true,
7 | "noLib": false,
8 | "emitDecoratorMetadata": true,
9 | "experimentalDecorators": true,
10 | "target": "es6",
11 | "sourceMap": true,
12 | "allowJs": true,
13 | "outDir": "./dist"
14 | },
15 | "include": [
16 | "src/**/*"
17 | ],
18 | "exclude": [
19 | "node_modules",
20 | "**/*.spec.ts"
21 | ]
22 | }
--------------------------------------------------------------------------------
/generators/app/templates/08-passport/tslint.json:
--------------------------------------------------------------------------------
1 | {
2 | "defaultSeverity": "error",
3 | "extends": [
4 | "tslint:recommended"
5 | ],
6 | "jsRules": {
7 | "no-unused-expression": true
8 | },
9 | "rules": {
10 | "eofline": false,
11 | "quotemark": [
12 | true,
13 | "single"
14 | ],
15 | "ordered-imports": [
16 | false
17 | ],
18 | "max-line-length": [
19 | 150
20 | ],
21 | "member-ordering": [
22 | false
23 | ],
24 | "curly": false,
25 | "interface-name": [
26 | false
27 | ],
28 | "array-type": [
29 | false
30 | ],
31 | "member-access": [
32 | false
33 | ],
34 | "no-empty-interface": false,
35 | "no-empty": false,
36 | "arrow-parens": false,
37 | "object-literal-sort-keys": false,
38 | "no-unused-expression": false,
39 | "max-classes-per-file": [
40 | false
41 | ],
42 | "variable-name": [
43 | false
44 | ],
45 | "one-line": [
46 | false
47 | ],
48 | "one-variable-per-declaration": [
49 | false
50 | ]
51 | },
52 | "rulesDirectory": []
53 | }
--------------------------------------------------------------------------------
/generators/app/templates/09-babel-example/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": ["env", "stage-0"],
3 | "plugins": ["transform-decorators-legacy"]
4 | }
--------------------------------------------------------------------------------
/generators/app/templates/09-babel-example/.gitignore:
--------------------------------------------------------------------------------
1 | # dependencies
2 | /node_modules
3 |
4 | # IDE
5 | /.idea
6 | /.awcache
7 | /.vscode
8 |
9 | # misc
10 | npm-debug.log
11 |
12 | # example
13 | /quick-start
14 |
15 | # tests
16 | /test
17 | /coverage
18 | /.nyc_output
19 |
20 | # dist
21 | /dist
--------------------------------------------------------------------------------
/generators/app/templates/09-babel-example/README.md:
--------------------------------------------------------------------------------
1 | # <%= config.name %>
2 |
3 | <%= config.description %>
--------------------------------------------------------------------------------
/generators/app/templates/09-babel-example/index.js:
--------------------------------------------------------------------------------
1 | require('babel-core/register');
2 | require('babel-polyfill');
3 | require('./src/server');
4 |
--------------------------------------------------------------------------------
/generators/app/templates/09-babel-example/jsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "target": "ES6",
4 | "experimentalDecorators": true
5 | },
6 | "exclude": [
7 | "node_modules"
8 | ]
9 | }
--------------------------------------------------------------------------------
/generators/app/templates/09-babel-example/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "<%= config.identifier %>",
3 | "version": "1.0.0",
4 | "description": "<%= config.description %>",
5 | "license": "MIT",
6 | "publisher": "<%= config.publisher %>",
7 | "scripts": {
8 | "build": "babel src -d dist",
9 | "prestart": "npm run build",
10 | "start": "node index.js"
11 | },
12 | "dependencies": {
13 | "@nestjs/common": "^4.3.0",
14 | "@nestjs/core": "^4.3.0",
15 | "@nestjs/microservices": "^4.3.0",
16 | "@nestjs/testing": "^4.3.0",
17 | "@nestjs/websockets": "^4.3.0",
18 | "babel-core": "^6.26.0",
19 | "babel-polyfill": "^6.26.0",
20 | "body-parser": "^1.17.2",
21 | "redis": "^2.7.1",
22 | "reflect-metadata": "^0.1.10",
23 | "rxjs": "^5.4.3"
24 | },
25 | "devDependencies": {
26 | "babel-cli": "^6.26.0",
27 | "babel-plugin-transform-decorators-legacy": "^1.3.4",
28 | "babel-preset-env": "^1.6.0",
29 | "babel-preset-stage-0": "^6.24.1"
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/generators/app/templates/09-babel-example/src/modules/app.module.js:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { CatsModule } from './cats/cats.module';
3 | import { CatsController } from './cats/cats.controller';
4 |
5 | @Module({
6 | modules: [CatsModule],
7 | })
8 | export class ApplicationModule {}
--------------------------------------------------------------------------------
/generators/app/templates/09-babel-example/src/modules/cats/cats.controller.js:
--------------------------------------------------------------------------------
1 | import {
2 | Controller,
3 | Get,
4 | Post,
5 | Body,
6 | Bind,
7 | Dependencies,
8 | ReflectMetadata,
9 | Param,
10 | } from '@nestjs/common';
11 | import { CatsService } from './cats.service';
12 |
13 | @Controller('cats')
14 | @Dependencies(CatsService)
15 | export class CatsController {
16 | constructor(catsService) {
17 | this.catsService = catsService;
18 | }
19 |
20 | @Post()
21 | @Bind(Body())
22 | async create(createCatDto) {
23 | this.catsService.create(createCatDto);
24 | }
25 |
26 | @Get()
27 | async findAll() {
28 | return this.catsService.findAll();
29 | }
30 |
31 | @Get(':id')
32 | @Bind(Param('id'))
33 | findOne(id) {
34 | // logic
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/generators/app/templates/09-babel-example/src/modules/cats/cats.module.js:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { CatsController } from './cats.controller';
3 | import { CatsService } from './cats.service';
4 |
5 | @Module({
6 | controllers: [CatsController],
7 | components: [CatsService],
8 | })
9 | export class CatsModule {}
10 |
--------------------------------------------------------------------------------
/generators/app/templates/09-babel-example/src/modules/cats/cats.service.js:
--------------------------------------------------------------------------------
1 | import { Component } from '@nestjs/common';
2 | import { CatsModule } from './cats.module';
3 |
4 | @Component()
5 | export class CatsService {
6 | constructor() {
7 | this.cats = [];
8 | }
9 |
10 | create(cat) {
11 | this.cats.push(cat);
12 | }
13 |
14 | findAll() {
15 | return this.cats;
16 | }
17 | }
--------------------------------------------------------------------------------
/generators/app/templates/09-babel-example/src/server.js:
--------------------------------------------------------------------------------
1 | require('babel-core/register');
2 | require('babel-polyfill');
3 |
4 | import { NestFactory } from '@nestjs/core';
5 | import { ApplicationModule } from './modules/app.module';
6 |
7 | async function bootstrap() {
8 | const app = await NestFactory.create(ApplicationModule);
9 | await app.listen(3000);
10 | }
11 | bootstrap();
12 |
--------------------------------------------------------------------------------
/generators/app/templates/10-mockgoose/README.md:
--------------------------------------------------------------------------------
1 | # <%= config.name %>
2 |
3 | <%= config.description %>
--------------------------------------------------------------------------------
/generators/app/templates/10-mockgoose/e2e/cats/cats.e2e-spec.ts:
--------------------------------------------------------------------------------
1 | import * as express from 'express';
2 | import * as bodyParser from 'body-parser';
3 | import * as request from 'supertest';
4 | import { Test } from '@nestjs/testing';
5 | import { CatsModule } from '../../src/modules/cats/cats.module';
6 |
7 | describe('Cats', () => {
8 | const server = express();
9 | server.use(bodyParser.json());
10 |
11 | beforeAll(async () => {
12 | const module = await Test.createTestingModule({
13 | modules: [CatsModule],
14 | })
15 | .compile();
16 |
17 | const app = module.createNestApplication(server);
18 | await app.init();
19 | });
20 |
21 | it(`/POST insert cat`, () => {
22 | return request(server)
23 | .post('/cats')
24 | .send({
25 | name: 'Tiger',
26 | age: 2,
27 | breed: 'Russian Blue'
28 | })
29 | .expect(201);
30 | });
31 |
32 | it(`/GET cats`, async (done) => {
33 |
34 | const cats = await request(server)
35 | .get('/cats')
36 | .expect(200);
37 |
38 | const [cat] = cats.body;
39 |
40 | expect(cat.name).toBe('Tiger');
41 | expect(cat.age).toBe(2);
42 | expect(cat.breed).toBe('Russian Blue');
43 |
44 | done();
45 | });
46 | });
--------------------------------------------------------------------------------
/generators/app/templates/10-mockgoose/e2e/jest-e2e.json:
--------------------------------------------------------------------------------
1 | {
2 | "moduleFileExtensions": [
3 | "ts",
4 | "tsx",
5 | "js",
6 | "json"
7 | ],
8 | "transform": {
9 | "^.+\\.tsx?$": "/../node_modules/ts-jest/preprocessor.js"
10 | },
11 | "testRegex": "/e2e/.*\\.(e2e-test|e2e-spec).(ts|tsx|js)$",
12 | "collectCoverageFrom" : ["src/**/*.{js,jsx,tsx,ts}", "!**/node_modules/**", "!**/vendor/**"],
13 | "coverageReporters": ["json", "lcov"]
14 | }
--------------------------------------------------------------------------------
/generators/app/templates/10-mockgoose/index.js:
--------------------------------------------------------------------------------
1 | require('ts-node/register');
2 | require('./src/server');
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/generators/app/templates/10-mockgoose/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "<%= config.identifier %>",
3 | "version": "1.0.0",
4 | "description": "<%= config.description %>",
5 | "license": "MIT",
6 | "publisher": "<%= config.publisher %>",
7 | "scripts": {
8 | "start": "node index.js",
9 | "prestart:prod": "tsc",
10 | "start:prod": "node dist/server.js",
11 | "e2e": "jest --config=e2e/jest-e2e.json --forceExit",
12 | "e2e:watch": "jest --watch --config=e2e/jest-e2e.json"
13 | },
14 | "dependencies": {
15 | "@nestjs/common": "^4.3.0",
16 | "@nestjs/core": "^4.3.0",
17 | "@nestjs/microservices": "^4.3.0",
18 | "@nestjs/testing": "^4.3.0",
19 | "@nestjs/websockets": "^4.3.0",
20 | "mongoose": "^4.11.13",
21 | "redis": "^2.7.1",
22 | "reflect-metadata": "^0.1.10",
23 | "rxjs": "^5.4.3",
24 | "typescript": "^2.4.2"
25 | },
26 | "devDependencies": {
27 | "@types/mongoose": "^4.7.23",
28 | "@types/jest": "^20.0.8",
29 | "@types/node": "^7.0.41",
30 | "mockgoose": "^7.3.3",
31 | "jest": "^20.0.4",
32 | "supertest": "^3.0.0",
33 | "ts-jest": "^20.0.14",
34 | "ts-node": "^3.3.0"
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/generators/app/templates/10-mockgoose/src/modules/app.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { CatsModule } from './cats/cats.module';
3 |
4 | @Module({
5 | modules: [CatsModule],
6 | })
7 | export class ApplicationModule {}
--------------------------------------------------------------------------------
/generators/app/templates/10-mockgoose/src/modules/cats/cats.controller.ts:
--------------------------------------------------------------------------------
1 | import { Controller, Get, Post, Body, Param } from '@nestjs/common';
2 | import { CreateCatDto } from './dto/create-cat.dto';
3 | import { CatsService } from './cats.service';
4 | import { Cat } from './interfaces/cat.interface';
5 |
6 | @Controller('cats')
7 | export class CatsController {
8 | constructor(private readonly catsService: CatsService) {}
9 |
10 | @Post()
11 | async create(@Body() createCatDto: CreateCatDto) {
12 | this.catsService.create(createCatDto);
13 | }
14 |
15 | @Get()
16 | async findAll(): Promise {
17 | return this.catsService.findAll();
18 | }
19 | }
--------------------------------------------------------------------------------
/generators/app/templates/10-mockgoose/src/modules/cats/cats.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { CatsController } from './cats.controller';
3 | import { CatsService } from './cats.service';
4 | import { catsProviders } from './cats.providers';
5 | import { DatabaseModule } from '../database/database.module';
6 |
7 | @Module({
8 | modules: [DatabaseModule],
9 | controllers: [CatsController],
10 | components: [
11 | CatsService,
12 | ...catsProviders,
13 | ],
14 | })
15 | export class CatsModule {}
--------------------------------------------------------------------------------
/generators/app/templates/10-mockgoose/src/modules/cats/cats.providers.ts:
--------------------------------------------------------------------------------
1 | import * as mongoose from 'mongoose';
2 | import { CatSchema } from './schemas/cat.schema';
3 |
4 | export const catsProviders = [
5 | {
6 | provide: 'CatModelToken',
7 | useFactory: (mongoose) => mongoose.connection.model('Cat', CatSchema),
8 | inject: ['DbToken'],
9 | },
10 | ];
--------------------------------------------------------------------------------
/generators/app/templates/10-mockgoose/src/modules/cats/cats.service.ts:
--------------------------------------------------------------------------------
1 | import { Model } from 'mongoose';
2 | import { Component, Inject } from '@nestjs/common';
3 | import { Cat } from './interfaces/cat.interface';
4 | import { CreateCatDto } from './dto/create-cat.dto';
5 |
6 | @Component()
7 | export class CatsService {
8 | constructor(
9 | @Inject('CatModelToken') private readonly catModel: Model) {}
10 |
11 | async create(createCatDto: CreateCatDto): Promise {
12 | const createdCat = new this.catModel(createCatDto);
13 | return await createdCat.save();
14 | }
15 |
16 | async findAll(): Promise {
17 | return await this.catModel.find().exec();
18 | }
19 | }
--------------------------------------------------------------------------------
/generators/app/templates/10-mockgoose/src/modules/cats/dto/create-cat.dto.ts:
--------------------------------------------------------------------------------
1 | export class CreateCatDto {
2 | readonly name: string;
3 | readonly age: number;
4 | readonly breed: string;
5 | }
--------------------------------------------------------------------------------
/generators/app/templates/10-mockgoose/src/modules/cats/interfaces/cat.interface.ts:
--------------------------------------------------------------------------------
1 | import { Document } from 'mongoose';
2 |
3 | export interface Cat extends Document {
4 | readonly name: string;
5 | readonly age: number;
6 | readonly breed: string;
7 | }
--------------------------------------------------------------------------------
/generators/app/templates/10-mockgoose/src/modules/cats/schemas/cat.schema.ts:
--------------------------------------------------------------------------------
1 | import * as mongoose from 'mongoose';
2 |
3 | export const CatSchema = new mongoose.Schema({
4 | name: String,
5 | age: Number,
6 | breed: String,
7 | });
--------------------------------------------------------------------------------
/generators/app/templates/10-mockgoose/src/modules/database/database.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { databaseProviders } from './database.providers';
3 |
4 | @Module({
5 | components: [...databaseProviders],
6 | exports: [...databaseProviders],
7 | })
8 | export class DatabaseModule {}
--------------------------------------------------------------------------------
/generators/app/templates/10-mockgoose/src/modules/database/database.providers.ts:
--------------------------------------------------------------------------------
1 | import * as mongoose from 'mongoose';
2 | import { Mockgoose } from 'mockgoose';
3 |
4 | export const databaseProviders = [
5 | {
6 | provide: 'DbToken',
7 | useFactory: async () => {
8 | (mongoose as any).Promise = global.Promise;
9 |
10 | if (process.env.NODE_ENV === 'test') {
11 |
12 | const mockgoose = new Mockgoose(mongoose);
13 | mockgoose.helper.setDbVersion('3.4.3');
14 |
15 | mockgoose.prepareStorage()
16 | .then(async () => {
17 | await mongoose.connect('mongodb://example.com/TestingDB', {
18 | useMongoClient: true,
19 | });
20 | });
21 |
22 | } else {
23 |
24 | await mongoose.connect('mongodb://localhost/nest', {
25 | useMongoClient: true,
26 | });
27 | }
28 |
29 | return mongoose;
30 | },
31 | },
32 | ];
--------------------------------------------------------------------------------
/generators/app/templates/10-mockgoose/src/server.ts:
--------------------------------------------------------------------------------
1 | import { NestFactory } from '@nestjs/core';
2 | import { ApplicationModule } from './modules/app.module';
3 |
4 | async function bootstrap() {
5 | const app = await NestFactory.create(ApplicationModule);
6 | await app.listen(3011);
7 | }
8 | bootstrap();
9 |
--------------------------------------------------------------------------------
/generators/app/templates/10-mockgoose/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "declaration": false,
5 | "noImplicitAny": false,
6 | "removeComments": true,
7 | "noLib": false,
8 | "emitDecoratorMetadata": true,
9 | "experimentalDecorators": true,
10 | "target": "es6",
11 | "sourceMap": true,
12 | "allowJs": true,
13 | "outDir": "./dist"
14 | },
15 | "include": [
16 | "src/**/*"
17 | ],
18 | "exclude": [
19 | "node_modules",
20 | "**/*.spec.ts"
21 | ]
22 | }
--------------------------------------------------------------------------------
/generators/app/templates/10-mockgoose/tslint.json:
--------------------------------------------------------------------------------
1 | {
2 | "defaultSeverity": "error",
3 | "extends": [
4 | "tslint:recommended"
5 | ],
6 | "jsRules": {
7 | "no-unused-expression": true
8 | },
9 | "rules": {
10 | "eofline": false,
11 | "quotemark": [
12 | true,
13 | "single"
14 | ],
15 | "ordered-imports": [
16 | false
17 | ],
18 | "max-line-length": [
19 | 150
20 | ],
21 | "member-ordering": [
22 | false
23 | ],
24 | "curly": false,
25 | "interface-name": [
26 | false
27 | ],
28 | "array-type": [
29 | false
30 | ],
31 | "member-access": [
32 | false
33 | ],
34 | "no-empty-interface": false,
35 | "no-empty": false,
36 | "arrow-parens": false,
37 | "object-literal-sort-keys": false,
38 | "no-unused-expression": false,
39 | "max-classes-per-file": [
40 | false
41 | ],
42 | "variable-name": [
43 | false
44 | ],
45 | "one-line": [
46 | false
47 | ],
48 | "one-variable-per-declaration": [
49 | false
50 | ]
51 | },
52 | "rulesDirectory": []
53 | }
--------------------------------------------------------------------------------
/generators/app/templates/11-swagger/.gitignore:
--------------------------------------------------------------------------------
1 | # dependencies
2 | /node_modules
3 |
4 | # IDE
5 | /.idea
6 | /.awcache
7 | /.vscode
8 |
9 | # misc
10 | npm-debug.log
11 |
12 | # example
13 | /quick-start
14 |
15 | # tests
16 | /test
17 | /coverage
18 | /.nyc_output
19 |
20 | # dist
21 | /dist
--------------------------------------------------------------------------------
/generators/app/templates/11-swagger/README.md:
--------------------------------------------------------------------------------
1 | # <%= config.name %>
2 |
3 | <%= config.description %>
--------------------------------------------------------------------------------
/generators/app/templates/11-swagger/index.js:
--------------------------------------------------------------------------------
1 | require('ts-node/register');
2 | require('./src/server');
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/generators/app/templates/11-swagger/jest.json:
--------------------------------------------------------------------------------
1 | {
2 | "moduleFileExtensions": [
3 | "ts",
4 | "tsx",
5 | "js",
6 | "json"
7 | ],
8 | "transform": {
9 | "^.+\\.tsx?$": "/node_modules/ts-jest/preprocessor.js"
10 | },
11 | "testRegex": "/src/.*\\.(test|spec).(ts|tsx|js)$",
12 | "collectCoverageFrom" : ["src/**/*.{js,jsx,tsx,ts}", "!**/node_modules/**", "!**/vendor/**"],
13 | "coverageReporters": ["json", "lcov"]
14 | }
--------------------------------------------------------------------------------
/generators/app/templates/11-swagger/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "<%= config.identifier %>",
3 | "version": "1.0.0",
4 | "description": "<%= config.description %>",
5 | "license": "MIT",
6 | "publisher": "<%= config.publisher %>",
7 | "scripts": {
8 | "start": "node index.js",
9 | "prestart:prod": "tsc",
10 | "start:prod": "node dist/server.js",
11 | "test": "jest --config=jest.json",
12 | "test:watch": "jest --watch --config=jest.json",
13 | "test:coverage": "jest --config=jest.json --coverage --coverageDirectory=coverage",
14 | "e2e": "jest --config=e2e/jest-e2e.json --forceExit",
15 | "e2e:watch": "jest --watch --config=e2e/jest-e2e.json"
16 | },
17 | "dependencies": {
18 | "@nestjs/common": "^4.4.2",
19 | "@nestjs/core": "^4.4.2",
20 | "@nestjs/microservices": "^4.3.0",
21 | "@nestjs/swagger": "^1.1.2",
22 | "@nestjs/testing": "^4.3.0",
23 | "@nestjs/websockets": "^4.3.0",
24 | "class-transformer": "^0.1.7",
25 | "class-validator": "^0.7.2",
26 | "redis": "^2.7.1",
27 | "reflect-metadata": "^0.1.10",
28 | "rxjs": "^5.4.3",
29 | "typescript": "^2.4.2"
30 | },
31 | "devDependencies": {
32 | "@types/jest": "^20.0.8",
33 | "@types/node": "^7.0.41",
34 | "jest": "^20.0.4",
35 | "supertest": "^3.0.0",
36 | "ts-jest": "^20.0.14",
37 | "ts-node": "^3.3.0"
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/generators/app/templates/11-swagger/src/modules/app.module.ts:
--------------------------------------------------------------------------------
1 | import { Module, NestModule, MiddlewaresConsumer } from '@nestjs/common';
2 | import { CatsModule } from './cats/cats.module';
3 | import { CatsController } from './cats/cats.controller';
4 |
5 | @Module({
6 | modules: [CatsModule],
7 | })
8 | export class ApplicationModule {}
--------------------------------------------------------------------------------
/generators/app/templates/11-swagger/src/modules/cats/cats.controller.ts:
--------------------------------------------------------------------------------
1 | import { Controller, Get, Post, Body, Param } from '@nestjs/common';
2 | import { CreateCatDto } from './dto/create-cat.dto';
3 | import { CatsService } from './cats.service';
4 | import { Cat } from './interfaces/cat.interface';
5 | import { ApiUseTags, ApiBearerAuth, ApiResponse, ApiOperation } from '@nestjs/swagger';
6 |
7 | @ApiBearerAuth()
8 | @ApiUseTags('cats')
9 | @Controller('cats')
10 | export class CatsController {
11 | constructor(private readonly catsService: CatsService) {}
12 |
13 | @Post()
14 | @ApiOperation({ title: 'Create cat' })
15 | @ApiResponse({ status: 201, description: 'The record has been successfully created.'})
16 | @ApiResponse({ status: 403, description: 'Forbidden.'})
17 | async create(@Body() createCatDto: CreateCatDto) {
18 | this.catsService.create(createCatDto);
19 | }
20 |
21 | @Get(':id')
22 | findOne(@Param('id') id: string): Cat {
23 | return this.catsService.findOne(+id);
24 | }
25 | }
--------------------------------------------------------------------------------
/generators/app/templates/11-swagger/src/modules/cats/cats.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { CatsController } from './cats.controller';
3 | import { CatsService } from './cats.service';
4 |
5 | @Module({
6 | controllers: [CatsController],
7 | components: [CatsService],
8 | })
9 | export class CatsModule {}
--------------------------------------------------------------------------------
/generators/app/templates/11-swagger/src/modules/cats/cats.service.ts:
--------------------------------------------------------------------------------
1 | import { Component } from '@nestjs/common';
2 | import { Cat } from './interfaces/cat.interface';
3 | import { CatsModule } from './cats.module';
4 |
5 | @Component()
6 | export class CatsService {
7 | private readonly cats: Cat[] = [];
8 |
9 | create(cat: Cat) {
10 | this.cats.push(cat);
11 | }
12 |
13 | findOne(id: number): Cat {
14 | return this.cats[id];
15 | }
16 | }
--------------------------------------------------------------------------------
/generators/app/templates/11-swagger/src/modules/cats/dto/create-cat.dto.ts:
--------------------------------------------------------------------------------
1 | import { IsString, IsInt } from 'class-validator';
2 | import { ApiModelProperty } from '@nestjs/swagger';
3 |
4 | export class CreateCatDto {
5 | @ApiModelProperty({ type: String })
6 | @IsString()
7 | readonly name;
8 |
9 | @ApiModelProperty({ type: Number })
10 | @IsInt()
11 | readonly age;
12 |
13 | @ApiModelProperty({ type: String })
14 | @IsString()
15 | readonly breed;
16 | }
--------------------------------------------------------------------------------
/generators/app/templates/11-swagger/src/modules/cats/interfaces/cat.interface.ts:
--------------------------------------------------------------------------------
1 | export interface Cat {
2 | readonly name: string;
3 | readonly age: number;
4 | readonly breed: string;
5 | }
--------------------------------------------------------------------------------
/generators/app/templates/11-swagger/src/server.ts:
--------------------------------------------------------------------------------
1 | import { NestFactory } from '@nestjs/core';
2 | import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
3 | import { ApplicationModule } from './modules/app.module';
4 |
5 | async function bootstrap() {
6 | const app = await NestFactory.create(ApplicationModule);
7 |
8 | const options = new DocumentBuilder()
9 | .setTitle('Cats example')
10 | .setDescription('The cats API description')
11 | .setVersion('1.0')
12 | .addTag('cats')
13 | .addBearerAuth()
14 | .build();
15 | const document = SwaggerModule.createDocument(app, options);
16 | SwaggerModule.setup('/api', app, document);
17 |
18 | await app.listen(3001);
19 | }
20 | bootstrap();
21 |
--------------------------------------------------------------------------------
/generators/app/templates/11-swagger/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "declaration": false,
5 | "noImplicitAny": false,
6 | "removeComments": true,
7 | "noLib": false,
8 | "emitDecoratorMetadata": true,
9 | "experimentalDecorators": true,
10 | "target": "es6",
11 | "sourceMap": true,
12 | "allowJs": true,
13 | "outDir": "./dist"
14 | },
15 | "include": [
16 | "src/**/*"
17 | ],
18 | "exclude": [
19 | "node_modules",
20 | "**/*.spec.ts"
21 | ]
22 | }
--------------------------------------------------------------------------------
/generators/app/templates/11-swagger/tslint.json:
--------------------------------------------------------------------------------
1 | {
2 | "defaultSeverity": "error",
3 | "extends": [
4 | "tslint:recommended"
5 | ],
6 | "jsRules": {
7 | "no-unused-expression": true
8 | },
9 | "rules": {
10 | "eofline": false,
11 | "indent": false,
12 | "quotemark": [
13 | true,
14 | "single"
15 | ],
16 | "ordered-imports": [
17 | false
18 | ],
19 | "max-line-length": [
20 | 150
21 | ],
22 | "member-ordering": [
23 | false
24 | ],
25 | "curly": false,
26 | "interface-name": [
27 | false
28 | ],
29 | "array-type": [
30 | false
31 | ],
32 | "member-access": [
33 | false
34 | ],
35 | "no-empty-interface": false,
36 | "no-empty": false,
37 | "arrow-parens": false,
38 | "object-literal-sort-keys": false,
39 | "no-unused-expression": false,
40 | "max-classes-per-file": [
41 | false
42 | ],
43 | "variable-name": [
44 | false
45 | ],
46 | "one-line": [
47 | false
48 | ],
49 | "one-variable-per-declaration": [
50 | false
51 | ]
52 | },
53 | "rulesDirectory": []
54 | }
--------------------------------------------------------------------------------
/generators/app/templates/12-graphql-apollo/.gitignore:
--------------------------------------------------------------------------------
1 | # dependencies
2 | /node_modules
3 |
4 | # IDE
5 | /.idea
6 | /.awcache
7 | /.vscode
8 |
9 | # misc
10 | npm-debug.log
11 |
12 | # example
13 | /quick-start
14 |
15 | # tests
16 | /test
17 | /coverage
18 | /.nyc_output
19 |
20 | # dist
21 | /dist
--------------------------------------------------------------------------------
/generators/app/templates/12-graphql-apollo/README.md:
--------------------------------------------------------------------------------
1 | # <%= config.name %>
2 |
3 | <%= config.description %>
--------------------------------------------------------------------------------
/generators/app/templates/12-graphql-apollo/index.js:
--------------------------------------------------------------------------------
1 | require('ts-node/register');
2 | require('./src/server');
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/generators/app/templates/12-graphql-apollo/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "<%= config.identifier %>",
3 | "version": "1.0.0",
4 | "description": "<%= config.description %>",
5 | "license": "MIT",
6 | "publisher": "<%= config.publisher %>",
7 | "scripts": {
8 | "start": "node index.js",
9 | "prestart:prod": "tsc",
10 | "start:prod": "node dist/server.js"
11 | },
12 | "dependencies": {
13 | "@nestjs/common": "^4.5.0",
14 | "@nestjs/core": "^4.5.0",
15 | "@nestjs/graphql": "^1.0.0",
16 | "apollo-server-express": "^1.2.0",
17 | "graphql": "^0.11.7",
18 | "graphql-tools": "^2.11.0",
19 | "reflect-metadata": "^0.1.10",
20 | "rxjs": "^5.4.3",
21 | "typescript": "^2.4.2"
22 | },
23 | "devDependencies": {
24 | "@types/node": "^7.0.41",
25 | "ts-node": "^3.3.0"
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/generators/app/templates/12-graphql-apollo/src/modules/app.module.ts:
--------------------------------------------------------------------------------
1 | import {
2 | Module,
3 | MiddlewaresConsumer,
4 | NestModule,
5 | RequestMethod,
6 | } from '@nestjs/common';
7 | import { graphqlExpress, graphiqlExpress } from 'apollo-server-express';
8 | import {
9 | makeExecutableSchema,
10 | addMockFunctionsToSchema,
11 | mergeSchemas,
12 | } from 'graphql-tools';
13 | import { GraphQLModule, GraphQLFactory } from '@nestjs/graphql';
14 |
15 | import { CatsModule } from './cats/cats.module';
16 |
17 | @Module({
18 | modules: [CatsModule, GraphQLModule],
19 | })
20 | export class ApplicationModule {
21 | constructor(private readonly graphQLFactory: GraphQLFactory) {}
22 |
23 | configure(consumer: MiddlewaresConsumer) {
24 | const schema = this.createSchema();
25 | consumer
26 | .apply(graphiqlExpress({ endpointURL: '/graphql' }))
27 | .forRoutes({ path: '/graphiql', method: RequestMethod.GET })
28 | .apply(graphqlExpress(req => ({ schema, rootValue: req })))
29 | .forRoutes({ path: '/graphql', method: RequestMethod.ALL });
30 | }
31 |
32 | createSchema() {
33 | const typeDefs = this.graphQLFactory.mergeTypesByPaths('./**/*.graphql');
34 | const schema = this.graphQLFactory.createSchema({ typeDefs });
35 |
36 | const delegates = this.graphQLFactory.createDelegates();
37 | const { humanSchema, linkTypeDefs } = this.createDelegatedSchema();
38 | return mergeSchemas({
39 | schemas: [schema, humanSchema, linkTypeDefs],
40 | resolvers: delegates,
41 | });
42 | }
43 |
44 | createDelegatedSchema() {
45 | const linkTypeDefs = `
46 | extend type Cat {
47 | human: Human
48 | }
49 | `;
50 | const humanSchema = makeExecutableSchema({
51 | typeDefs: `
52 | type Human {
53 | id: ID!
54 | }
55 | type Query {
56 | humanById(id: ID!): Human
57 | }
58 | `,
59 | });
60 | addMockFunctionsToSchema({ schema: humanSchema });
61 | return { humanSchema, linkTypeDefs };
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/generators/app/templates/12-graphql-apollo/src/modules/cats/cats.guard.ts:
--------------------------------------------------------------------------------
1 | import { CanActivate, Guard, ExecutionContext } from '@nestjs/common';
2 |
3 | @Guard()
4 | export class CatsGuard implements CanActivate {
5 | canActivate(request: any, context: ExecutionContext): boolean {
6 | return true;
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/generators/app/templates/12-graphql-apollo/src/modules/cats/cats.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { CatsService } from './cats.service';
3 | import { CatsResolvers } from './cats.resolvers';
4 |
5 | @Module({
6 | components: [CatsService, CatsResolvers],
7 | })
8 | export class CatsModule {}
9 |
--------------------------------------------------------------------------------
/generators/app/templates/12-graphql-apollo/src/modules/cats/cats.resolvers.ts:
--------------------------------------------------------------------------------
1 | import { Component, UseGuards } from '@nestjs/common';
2 | import {
3 | Query,
4 | Mutation,
5 | Resolver,
6 | DelegateProperty,
7 | } from '@nestjs/graphql';
8 |
9 | import { Cat } from './interfaces/cat.interface';
10 | import { CatsService } from './cats.service';
11 | import { CatsGuard } from './cats.guard';
12 | import { MergeInfo } from 'graphql-tools/dist/Interfaces';
13 |
14 | @Resolver('Cat')
15 | export class CatsResolvers {
16 | constructor(private readonly catsService: CatsService) {}
17 |
18 | @Query()
19 | @UseGuards(CatsGuard)
20 | async getCats() {
21 | return await this.catsService.findAll();
22 | }
23 |
24 | @Query('cat')
25 | async findOneById(id: number) {
26 | return await this.catsService.findOneById(id);
27 | }
28 |
29 | @Mutation('createCat')
30 | async create(cat: Cat) {
31 | await this.catsService.create(cat);
32 | }
33 |
34 | @DelegateProperty('human')
35 | findHumansById(cat: Cat) {
36 | return (mergeInfo: MergeInfo) => ({
37 | fragment: `fragment CatFragment on Cat { humanId }`,
38 | resolve(parent, args, context, info) {
39 | const humanId = parent.id;
40 | return mergeInfo.delegate(
41 | 'query',
42 | 'humanById',
43 | {
44 | id: humanId,
45 | },
46 | context,
47 | info,
48 | );
49 | },
50 | });
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/generators/app/templates/12-graphql-apollo/src/modules/cats/cats.service.ts:
--------------------------------------------------------------------------------
1 | import { Component } from '@nestjs/common';
2 | import { Cat } from './interfaces/cat.interface';
3 |
4 | @Component()
5 | export class CatsService {
6 | private readonly cats: Cat[] = [
7 | { id: 1, name: 'Cat', age: 5 },
8 | ];
9 |
10 | create(cat: Cat) {
11 | this.cats.push(cat);
12 | }
13 |
14 | findAll(): Cat[] {
15 | return this.cats;
16 | }
17 |
18 | findOneById(id: number): Cat {
19 | return this.cats.find((cat) => cat.id === id);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/generators/app/templates/12-graphql-apollo/src/modules/cats/cats.types.graphql:
--------------------------------------------------------------------------------
1 | type Query {
2 | getCats: [Cat]
3 | cat(id: ID!): Cat
4 | catByHumanId(id: ID!): Cat
5 | }
6 |
7 | type Mutation {
8 | createCat(name: String): Cat
9 | }
10 |
11 | type Cat {
12 | id: Int
13 | name: String
14 | age: Int
15 | humanId: Int
16 | }
--------------------------------------------------------------------------------
/generators/app/templates/12-graphql-apollo/src/modules/cats/interfaces/cat.interface.ts:
--------------------------------------------------------------------------------
1 | export interface Cat {
2 | readonly id: number;
3 | readonly name: string;
4 | readonly age: number;
5 | }
--------------------------------------------------------------------------------
/generators/app/templates/12-graphql-apollo/src/server.ts:
--------------------------------------------------------------------------------
1 | import { NestFactory } from '@nestjs/core';
2 | import { ApplicationModule } from './modules/app.module';
3 |
4 | async function bootstrap() {
5 | const app = await NestFactory.create(ApplicationModule);
6 | await app.listen(3000);
7 | }
8 | bootstrap();
9 |
--------------------------------------------------------------------------------
/generators/app/templates/12-graphql-apollo/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "declaration": false,
5 | "noImplicitAny": false,
6 | "removeComments": true,
7 | "noLib": false,
8 | "emitDecoratorMetadata": true,
9 | "experimentalDecorators": true,
10 | "target": "es6",
11 | "sourceMap": true,
12 | "allowJs": true,
13 | "outDir": "./dist"
14 | },
15 | "include": [
16 | "src/**/*"
17 | ],
18 | "exclude": [
19 | "node_modules",
20 | "**/*.spec.ts"
21 | ]
22 | }
--------------------------------------------------------------------------------
/generators/app/templates/12-graphql-apollo/tslint.json:
--------------------------------------------------------------------------------
1 | {
2 | "defaultSeverity": "error",
3 | "extends": [
4 | "tslint:recommended"
5 | ],
6 | "jsRules": {
7 | "no-unused-expression": true
8 | },
9 | "rules": {
10 | "eofline": false,
11 | "quotemark": [
12 | true,
13 | "single"
14 | ],
15 | "indent": false,
16 | "ordered-imports": [
17 | false
18 | ],
19 | "max-line-length": [
20 | 150
21 | ],
22 | "member-ordering": [
23 | false
24 | ],
25 | "curly": false,
26 | "interface-name": [
27 | false
28 | ],
29 | "array-type": [
30 | false
31 | ],
32 | "member-access": [
33 | false
34 | ],
35 | "no-empty-interface": false,
36 | "no-empty": false,
37 | "arrow-parens": false,
38 | "object-literal-sort-keys": false,
39 | "no-unused-expression": false,
40 | "max-classes-per-file": [
41 | false
42 | ],
43 | "variable-name": [
44 | false
45 | ],
46 | "one-line": [
47 | false
48 | ],
49 | "one-variable-per-declaration": [
50 | false
51 | ]
52 | },
53 | "rulesDirectory": []
54 | }
--------------------------------------------------------------------------------
/generators/app/templates/13-mongo-typeorm/.gitignore:
--------------------------------------------------------------------------------
1 | # dependencies
2 | /node_modules
3 |
4 | # IDE
5 | /.idea
6 | /.awcache
7 | /.vscode
8 |
9 | # misc
10 | npm-debug.log
11 |
12 | # example
13 | /quick-start
14 |
15 | # tests
16 | /test
17 | /coverage
18 | /.nyc_output
19 |
20 | # dist
21 | /dist
--------------------------------------------------------------------------------
/generators/app/templates/13-mongo-typeorm/README.md:
--------------------------------------------------------------------------------
1 | # <%= config.name %>
2 |
3 | <%= config.description %>
--------------------------------------------------------------------------------
/generators/app/templates/13-mongo-typeorm/index.js:
--------------------------------------------------------------------------------
1 | require('ts-node/register');
2 | require('./src/server');
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/generators/app/templates/13-mongo-typeorm/ormconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "type": "mongodb",
3 | "host": "localhost",
4 | "database": "nest",
5 | "entities": ["src/**/**.entity{.ts,.js}"],
6 | "synchronize": true
7 | }
--------------------------------------------------------------------------------
/generators/app/templates/13-mongo-typeorm/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "<%= config.identifier %>",
3 | "version": "1.0.0",
4 | "description": "<%= config.description %>",
5 | "license": "MIT",
6 | "publisher": "<%= config.publisher %>",
7 | "scripts": {
8 | "start": "node index.js",
9 | "prestart:prod": "tsc",
10 | "start:prod": "node dist/server.js"
11 | },
12 | "dependencies": {
13 | "@nestjs/common": "^4.3.0",
14 | "@nestjs/core": "^4.3.0",
15 | "@nestjs/typeorm": "^1.0.0",
16 | "mongodb": "^2.2.31",
17 | "reflect-metadata": "^0.1.10",
18 | "rxjs": "^5.4.3",
19 | "typeorm": "0.1.0-alpha.40",
20 | "typescript": "^2.4.2"
21 | },
22 | "devDependencies": {
23 | "@types/node": "^7.0.41",
24 | "ts-node": "^3.3.0"
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/generators/app/templates/13-mongo-typeorm/src/modules/app.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { TypeOrmModule } from '@nestjs/typeorm';
3 |
4 | import { PhotoModule } from './photo/photo.module';
5 | import { Photo } from './photo/photo.entity';
6 |
7 | @Module({
8 | modules: [
9 | TypeOrmModule.forRoot([Photo]),
10 | PhotoModule,
11 | ],
12 | })
13 | export class ApplicationModule {}
14 |
--------------------------------------------------------------------------------
/generators/app/templates/13-mongo-typeorm/src/modules/photo/photo.controller.ts:
--------------------------------------------------------------------------------
1 | import { Controller, Get } from '@nestjs/common';
2 | import { PhotoService } from './photo.service';
3 | import { Photo } from './photo.entity';
4 |
5 | @Controller('photo')
6 | export class PhotoController {
7 | constructor(private readonly photoService: PhotoService) {}
8 |
9 | @Get()
10 | findAll(): Promise {
11 | return this.photoService.findAll();
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/generators/app/templates/13-mongo-typeorm/src/modules/photo/photo.entity.ts:
--------------------------------------------------------------------------------
1 | import { Entity, Column, PrimaryGeneratedColumn, ObjectIdColumn, ObjectID } from 'typeorm';
2 |
3 | @Entity()
4 | export class Photo {
5 | @ObjectIdColumn()
6 | id: ObjectID;
7 |
8 | @Column()
9 | name: string;
10 |
11 | @Column()
12 | description: string;
13 |
14 | @Column()
15 | filename: string;
16 |
17 | @Column()
18 | isPublished: boolean;
19 | }
--------------------------------------------------------------------------------
/generators/app/templates/13-mongo-typeorm/src/modules/photo/photo.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { PhotoService } from './photo.service';
3 | import { PhotoController } from './photo.controller';
4 |
5 | @Module({
6 | components: [PhotoService],
7 | controllers: [PhotoController],
8 | })
9 | export class PhotoModule {}
10 |
--------------------------------------------------------------------------------
/generators/app/templates/13-mongo-typeorm/src/modules/photo/photo.service.ts:
--------------------------------------------------------------------------------
1 | import { Component, Inject } from '@nestjs/common';
2 | import { InjectRepository } from '@nestjs/typeorm';
3 | import { Repository } from 'typeorm';
4 |
5 | import { Photo } from './photo.entity';
6 |
7 | @Component()
8 | export class PhotoService {
9 | constructor(
10 | @InjectRepository(Photo)
11 | private readonly photoRepository: Repository,
12 | ) {}
13 |
14 | async findAll(): Promise {
15 | return await this.photoRepository.find();
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/generators/app/templates/13-mongo-typeorm/src/server.ts:
--------------------------------------------------------------------------------
1 | import { NestFactory } from '@nestjs/core';
2 | import { ApplicationModule } from './modules/app.module';
3 |
4 | async function bootstrap() {
5 | const app = await NestFactory.create(ApplicationModule);
6 | await app.listen(3001);
7 | }
8 | bootstrap();
9 |
--------------------------------------------------------------------------------
/generators/app/templates/13-mongo-typeorm/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "declaration": false,
5 | "noImplicitAny": false,
6 | "removeComments": true,
7 | "noLib": false,
8 | "emitDecoratorMetadata": true,
9 | "experimentalDecorators": true,
10 | "target": "es6",
11 | "sourceMap": true,
12 | "allowJs": true,
13 | "outDir": "./dist"
14 | },
15 | "include": [
16 | "src/**/*"
17 | ],
18 | "exclude": [
19 | "node_modules",
20 | "**/*.spec.ts"
21 | ]
22 | }
--------------------------------------------------------------------------------
/generators/app/templates/13-mongo-typeorm/tslint.json:
--------------------------------------------------------------------------------
1 | {
2 | "defaultSeverity": "error",
3 | "extends": [
4 | "tslint:recommended"
5 | ],
6 | "jsRules": {
7 | "no-unused-expression": true
8 | },
9 | "rules": {
10 | "eofline": false,
11 | "quotemark": [
12 | true,
13 | "single"
14 | ],
15 | "ban-types": false,
16 | "indent": false,
17 | "ordered-imports": [
18 | false
19 | ],
20 | "max-line-length": [
21 | 150
22 | ],
23 | "member-ordering": [
24 | false
25 | ],
26 | "curly": false,
27 | "interface-name": [
28 | false
29 | ],
30 | "array-type": [
31 | false
32 | ],
33 | "member-access": [
34 | false
35 | ],
36 | "no-empty-interface": false,
37 | "no-empty": false,
38 | "arrow-parens": false,
39 | "object-literal-sort-keys": false,
40 | "no-unused-expression": false,
41 | "max-classes-per-file": [
42 | false
43 | ],
44 | "variable-name": [
45 | false
46 | ],
47 | "one-line": [
48 | false
49 | ],
50 | "one-variable-per-declaration": [
51 | false
52 | ]
53 | },
54 | "rulesDirectory": []
55 | }
--------------------------------------------------------------------------------
/generators/app/templates/14-mongoose-module/.gitignore:
--------------------------------------------------------------------------------
1 | # dependencies
2 | /node_modules
3 |
4 | # IDE
5 | /.idea
6 | /.awcache
7 | /.vscode
8 |
9 | # misc
10 | npm-debug.log
11 |
12 | # example
13 | /quick-start
14 |
15 | # tests
16 | /test
17 | /coverage
18 | /.nyc_output
19 |
20 | # dist
21 | /dist
--------------------------------------------------------------------------------
/generators/app/templates/14-mongoose-module/README.md:
--------------------------------------------------------------------------------
1 | # <%= config.name %>
2 |
3 | <%= config.description %>
--------------------------------------------------------------------------------
/generators/app/templates/14-mongoose-module/index.js:
--------------------------------------------------------------------------------
1 | require('ts-node/register');
2 | require('./src/server');
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/generators/app/templates/14-mongoose-module/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "<%= config.identifier %>",
3 | "version": "1.0.0",
4 | "description": "<%= config.description %>",
5 | "license": "MIT",
6 | "publisher": "<%= config.publisher %>",
7 | "scripts": {
8 | "start": "node index.js",
9 | "prestart:prod": "tsc",
10 | "start:prod": "node dist/server.js"
11 | },
12 | "dependencies": {
13 | "@nestjs/common": "^4.3.0",
14 | "@nestjs/core": "^4.3.0",
15 | "@nestjs/mongoose": "^1.0.1",
16 | "mongoose": "^4.11.13",
17 | "reflect-metadata": "^0.1.10",
18 | "rxjs": "^5.4.3",
19 | "typescript": "^2.4.2"
20 | },
21 | "devDependencies": {
22 | "@types/mongoose": "^4.7.23",
23 | "@types/node": "^7.0.41",
24 | "ts-node": "^3.3.0"
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/generators/app/templates/14-mongoose-module/src/modules/app.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { MongooseModule } from '@nestjs/mongoose';
3 |
4 | import { CatsModule } from './cats/cats.module';
5 | import { CatSchema } from './cats/schemas/cat.schema';
6 |
7 | @Module({
8 | modules: [
9 | MongooseModule.forRoot('mongodb://localhost/nest', [
10 | { name: 'Cat', schema: CatSchema },
11 | ]),
12 | CatsModule,
13 | ],
14 | })
15 | export class ApplicationModule {}
16 |
--------------------------------------------------------------------------------
/generators/app/templates/14-mongoose-module/src/modules/cats/cats.controller.ts:
--------------------------------------------------------------------------------
1 | import { Controller, Get, Post, Body, Param } from '@nestjs/common';
2 | import { CreateCatDto } from './dto/create-cat.dto';
3 | import { CatsService } from './cats.service';
4 | import { Cat } from './interfaces/cat.interface';
5 |
6 | @Controller('cats')
7 | export class CatsController {
8 | constructor(private readonly catsService: CatsService) {}
9 |
10 | @Post()
11 | async create(@Body() createCatDto: CreateCatDto) {
12 | this.catsService.create(createCatDto);
13 | }
14 |
15 | @Get()
16 | async findAll(): Promise {
17 | return this.catsService.findAll();
18 | }
19 | }
--------------------------------------------------------------------------------
/generators/app/templates/14-mongoose-module/src/modules/cats/cats.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { CatsController } from './cats.controller';
3 | import { CatsService } from './cats.service';
4 |
5 | @Module({
6 | controllers: [CatsController],
7 | components: [CatsService],
8 | })
9 | export class CatsModule {}
10 |
--------------------------------------------------------------------------------
/generators/app/templates/14-mongoose-module/src/modules/cats/cats.service.ts:
--------------------------------------------------------------------------------
1 | import { Model } from 'mongoose';
2 | import { Component, Inject } from '@nestjs/common';
3 | import { InjectModel } from '@nestjs/mongoose';
4 |
5 | import { Cat } from './interfaces/cat.interface';
6 | import { CreateCatDto } from './dto/create-cat.dto';
7 | import { CatSchema } from './schemas/cat.schema';
8 |
9 | @Component()
10 | export class CatsService {
11 | constructor(@InjectModel(CatSchema) private readonly catModel: Model) {}
12 |
13 | async create(createCatDto: CreateCatDto): Promise {
14 | const createdCat = new this.catModel(createCatDto);
15 | return await createdCat.save();
16 | }
17 |
18 | async findAll(): Promise {
19 | return await this.catModel.find().exec();
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/generators/app/templates/14-mongoose-module/src/modules/cats/dto/create-cat.dto.ts:
--------------------------------------------------------------------------------
1 | export class CreateCatDto {
2 | readonly name: string;
3 | readonly age: number;
4 | readonly breed: string;
5 | }
--------------------------------------------------------------------------------
/generators/app/templates/14-mongoose-module/src/modules/cats/interfaces/cat.interface.ts:
--------------------------------------------------------------------------------
1 | import { Document } from 'mongoose';
2 |
3 | export interface Cat extends Document {
4 | readonly name: string;
5 | readonly age: number;
6 | readonly breed: string;
7 | }
--------------------------------------------------------------------------------
/generators/app/templates/14-mongoose-module/src/modules/cats/schemas/cat.schema.ts:
--------------------------------------------------------------------------------
1 | import * as mongoose from 'mongoose';
2 |
3 | export const CatSchema = new mongoose.Schema({
4 | name: String,
5 | age: Number,
6 | breed: String,
7 | });
--------------------------------------------------------------------------------
/generators/app/templates/14-mongoose-module/src/server.ts:
--------------------------------------------------------------------------------
1 | import { NestFactory } from '@nestjs/core';
2 | import { ApplicationModule } from './modules/app.module';
3 |
4 | async function bootstrap() {
5 | const app = await NestFactory.create(ApplicationModule);
6 | await app.listen(3001);
7 | }
8 | bootstrap();
9 |
--------------------------------------------------------------------------------
/generators/app/templates/14-mongoose-module/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "declaration": false,
5 | "noImplicitAny": false,
6 | "removeComments": true,
7 | "noLib": false,
8 | "emitDecoratorMetadata": true,
9 | "experimentalDecorators": true,
10 | "target": "es6",
11 | "sourceMap": true,
12 | "allowJs": true,
13 | "outDir": "./dist"
14 | },
15 | "include": [
16 | "src/**/*"
17 | ],
18 | "exclude": [
19 | "node_modules",
20 | "**/*.spec.ts"
21 | ]
22 | }
--------------------------------------------------------------------------------
/generators/app/templates/14-mongoose-module/tslint.json:
--------------------------------------------------------------------------------
1 | {
2 | "defaultSeverity": "error",
3 | "extends": [
4 | "tslint:recommended"
5 | ],
6 | "jsRules": {
7 | "no-unused-expression": true
8 | },
9 | "rules": {
10 | "eofline": false,
11 | "quotemark": [
12 | true,
13 | "single"
14 | ],
15 | "ban-types": false,
16 | "ordered-imports": [
17 | false
18 | ],
19 | "max-line-length": [
20 | 150
21 | ],
22 | "member-ordering": [
23 | false
24 | ],
25 | "curly": false,
26 | "interface-name": [
27 | false
28 | ],
29 | "array-type": [
30 | false
31 | ],
32 | "member-access": [
33 | false
34 | ],
35 | "no-empty-interface": false,
36 | "no-empty": false,
37 | "arrow-parens": false,
38 | "object-literal-sort-keys": false,
39 | "no-unused-expression": false,
40 | "max-classes-per-file": [
41 | false
42 | ],
43 | "variable-name": [
44 | false
45 | ],
46 | "one-line": [
47 | false
48 | ],
49 | "one-variable-per-declaration": [
50 | false
51 | ]
52 | },
53 | "rulesDirectory": []
54 | }
--------------------------------------------------------------------------------
/generators/app/templates/README.md:
--------------------------------------------------------------------------------
1 | # <%= config.name %>
2 |
3 | <%= config.description %>
--------------------------------------------------------------------------------
/generators/decorator/USAGE:
--------------------------------------------------------------------------------
1 |
2 |
3 | Description:
4 | Creates a minimalistic NESTJS decorator
5 |
6 | Example:
7 | yo nestjs:decorator NAME
8 |
9 | - NAME - OPTIONAL - the name of the module (use kebab-case)
10 |
11 | This will create:
12 | `/src/modules/common/${NAME}.decorator.ts`
13 |
14 |
15 |
--------------------------------------------------------------------------------
/generators/decorator/index.js:
--------------------------------------------------------------------------------
1 |
2 | const path = require('path')
3 | const chalk = require('chalk')
4 | const Generator = require('yeoman-generator');
5 | const yosay = require('yosay')
6 | const kebabToPascal = require('../../utils/case-change').kebabToPascal
7 | const kebabToCamel = require('../../utils/case-change').kebabToCamel
8 | const toLower = require('../../utils/case-change').toLower
9 |
10 | module.exports = class extends Generator {
11 | constructor(args, opt) {
12 | super(args, opt)
13 | this.argument("name", {
14 | required: false
15 | })
16 | this.appConfig = {}
17 | }
18 |
19 | prompting() {
20 | if (!this.options['name']) {
21 | return this.prompt([{
22 | type: 'input',
23 | name: 'name',
24 | message: 'Enter a name',
25 | }]).then(res => {
26 | this.appConfig['name'] = res.name
27 | })
28 | } else {
29 | this.appConfig['name'] = this.options['name']
30 | return Promise.resolve()
31 | }
32 | }
33 |
34 | writing() {
35 | let name = this.appConfig['name']
36 | this.fs.copyTpl(
37 | this.templatePath("index.decorator.ts"),
38 | this.destinationPath(`src/modules/common/${name.toLowerCase()}.decorator.ts`),
39 | { config: this.appConfig, kebabToCamel, kebabToPascal }
40 | );
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/generators/decorator/templates/index.decorator.ts:
--------------------------------------------------------------------------------
1 | import { ReflectMetadata } from '@nestjs/common';
2 |
3 | export const <%= kebabToPascal(config.name) %> = (...<%= kebabToCamel(config.name) %>: string[]) => ReflectMetadata('<%= kebabToCamel(config.name) %>', <%= kebabToCamel(config.name) %>);
--------------------------------------------------------------------------------
/generators/exception/USAGE:
--------------------------------------------------------------------------------
1 |
2 |
3 | Description:
4 | Creates a minimalistic NESTJS exception
5 |
6 | Example:
7 | yo nestjs:exception NAME
8 |
9 | - NAME - OPTIONAL - the name of the module (use kebab-case)
10 |
11 | This will create:
12 | `/src/modules/common/${NAME}.exception.ts`
13 |
14 |
15 |
--------------------------------------------------------------------------------
/generators/exception/index.js:
--------------------------------------------------------------------------------
1 |
2 | const path = require('path')
3 | const chalk = require('chalk')
4 | const Generator = require('yeoman-generator');
5 | const yosay = require('yosay')
6 | const kebabToPascal = require('../../utils/case-change').kebabToPascal
7 | const kebabToCamel = require('../../utils/case-change').kebabToCamel
8 | const toLower = require('../../utils/case-change').toLower
9 | const kebabToConstant = require('../../utils/case-change').kebabToConstant
10 |
11 | module.exports = class extends Generator {
12 | constructor(args, opt) {
13 | super(args, opt)
14 | this.argument("name", {
15 | required: false
16 | })
17 | this.appConfig = {}
18 | }
19 |
20 | prompting() {
21 | if (!this.options['name']) {
22 | return this.prompt([{
23 | type: 'input',
24 | name: 'name',
25 | message: 'Enter a name',
26 | }]).then(res => {
27 | this.appConfig['name'] = res.name
28 | })
29 | } else {
30 | this.appConfig['name'] = this.options['name']
31 | return Promise.resolve()
32 | }
33 | }
34 |
35 | writing() {
36 | let name = this.appConfig['name']
37 | this.fs.copyTpl(
38 | this.templatePath("index.exception.ts"),
39 | this.destinationPath(`src/modules/common/${name.toLowerCase()}.exception.ts`),
40 | { config: this.appConfig, kebabToCamel, kebabToPascal, kebabToConstant }
41 | );
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/generators/exception/templates/index.exception.ts:
--------------------------------------------------------------------------------
1 | import { HttpException } from '@nestjs/common';
2 | import { HttpStatus } from '@nestjs/common';
3 |
4 | export class <%= kebabToPascal(config.name) %>Exception extends HttpException {
5 | constructor() {
6 | super('<%= kebabToPascal(config.name) %>', HttpStatus.<%= kebabToConstant(config.name) %>);
7 | }
8 | }
--------------------------------------------------------------------------------
/generators/filter/USAGE:
--------------------------------------------------------------------------------
1 |
2 |
3 | Description:
4 | Creates a minimalistic NESTJS filter
5 |
6 | Example:
7 | yo nestjs:filter NAME
8 |
9 | - NAME - OPTIONAL - the name of the module (use kebab-case)
10 |
11 | This will create:
12 | `/src/modules/common/${NAME}.filter.ts`
13 |
14 |
15 |
--------------------------------------------------------------------------------
/generators/filter/index.js:
--------------------------------------------------------------------------------
1 |
2 | const path = require('path')
3 | const chalk = require('chalk')
4 | const Generator = require('yeoman-generator');
5 | const yosay = require('yosay')
6 | const kebabToPascal = require('../../utils/case-change').kebabToPascal
7 | const kebabToCamel = require('../../utils/case-change').kebabToCamel
8 | const toLower = require('../../utils/case-change').toLower
9 |
10 | module.exports = class extends Generator {
11 | constructor(args, opt) {
12 | super(args, opt)
13 | this.argument("name", {
14 | required: false
15 | })
16 | this.appConfig = {}
17 | }
18 |
19 | prompting() {
20 | if (!this.options['name']) {
21 | return this.prompt([{
22 | type: 'input',
23 | name: 'name',
24 | message: 'Enter a name',
25 | }]).then(res => {
26 | this.appConfig['name'] = res.name
27 | })
28 | } else {
29 | this.appConfig['name'] = this.options['name']
30 | return Promise.resolve()
31 | }
32 | }
33 |
34 | writing() {
35 | let name = this.appConfig['name']
36 | this.fs.copyTpl(
37 | this.templatePath("index.decorator.ts"),
38 | this.destinationPath(`src/modules/common/${name.toLowerCase()}.decorator.ts`),
39 | { config: this.appConfig, kebabToCamel, kebabToPascal }
40 | );
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/generators/filter/templates/index.filter.ts:
--------------------------------------------------------------------------------
1 | import { ExceptionFilter, Catch } from '@nestjs/common';
2 | import { HttpException } from '@nestjs/common';
3 |
4 | @Catch(HttpException)
5 | export class HttpExceptionFilter implements ExceptionFilter {
6 | catch(exception: HttpException, response) {
7 | const status = exception.getStatus();
8 |
9 | response.status(status).json({
10 | statusCode: status,
11 | message: `It's a message from the exception filter`,
12 | });
13 | }
14 | }
--------------------------------------------------------------------------------
/generators/guard/USAGE:
--------------------------------------------------------------------------------
1 |
2 |
3 | Description:
4 | Creates a minimalistic NESTJS guard
5 |
6 | Example:
7 | yo nestjs:guard NAME
8 |
9 | - NAME - OPTIONAL - the name of the module (use kebab-case)
10 |
11 | This will create:
12 | `/src/modules/common/${NAME}.guard.ts`
13 |
14 |
15 |
--------------------------------------------------------------------------------
/generators/guard/index.js:
--------------------------------------------------------------------------------
1 |
2 | const path = require('path')
3 | const chalk = require('chalk')
4 | const Generator = require('yeoman-generator');
5 | const yosay = require('yosay')
6 | const kebabToPascal = require('../../utils/case-change').kebabToPascal
7 | const kebabToCamel = require('../../utils/case-change').kebabToCamel
8 | const toLower = require('../../utils/case-change').toLower
9 |
10 | module.exports = class extends Generator {
11 | constructor(args, opt) {
12 | super(args, opt)
13 | this.argument("name", {
14 | required: false
15 | })
16 | this.appConfig = {}
17 | }
18 |
19 | prompting() {
20 | if (!this.options['name']) {
21 | return this.prompt([{
22 | type: 'input',
23 | name: 'name',
24 | message: 'Enter a name',
25 | }]).then(res => {
26 | this.appConfig['name'] = res.name
27 | })
28 | } else {
29 | this.appConfig['name'] = this.options['name']
30 | return Promise.resolve()
31 | }
32 | }
33 |
34 | writing() {
35 | let name = this.appConfig['name']
36 | this.fs.copyTpl(
37 | this.templatePath("index.guard.ts"),
38 | this.destinationPath(`src/modules/common/${name.toLowerCase()}.guard.ts`),
39 | { config: this.appConfig, kebabToCamel, kebabToPascal }
40 | );
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/generators/guard/templates/index.guard.ts:
--------------------------------------------------------------------------------
1 | import { Guard, CanActivate, ExecutionContext } from '@nestjs/common';
2 | import { Observable } from 'rxjs/Observable';
3 | import { Reflector } from '@nestjs/core';
4 |
5 | @Guard()
6 | export class <%= kebabToPascal(config.name) %>Guard implements CanActivate {
7 | constructor(private readonly reflector: Reflector) {}
8 |
9 | canActivate(req, context: ExecutionContext): boolean | Promise | Observable {
10 |
11 | }
12 | }
--------------------------------------------------------------------------------
/generators/interceptor/USAGE:
--------------------------------------------------------------------------------
1 |
2 |
3 | Description:
4 | Creates a minimalistic NESTJS interceptor
5 |
6 | Example:
7 | yo nestjs:interceptor NAME
8 |
9 | - NAME - OPTIONAL - the name of the module (use kebab-case)
10 |
11 | This will create:
12 | `/src/modules/common/${NAME}.interceptor.ts`
13 |
14 |
15 |
--------------------------------------------------------------------------------
/generators/interceptor/index.js:
--------------------------------------------------------------------------------
1 |
2 | const path = require('path')
3 | const chalk = require('chalk')
4 | const Generator = require('yeoman-generator');
5 | const yosay = require('yosay')
6 | const kebabToPascal = require('../../utils/case-change').kebabToPascal
7 | const kebabToCamel = require('../../utils/case-change').kebabToCamel
8 | const toLower = require('../../utils/case-change').toLower
9 |
10 | module.exports = class extends Generator {
11 | constructor(args, opt) {
12 | super(args, opt)
13 | this.argument("name", {
14 | required: false
15 | })
16 | this.appConfig = {}
17 | }
18 |
19 | prompting() {
20 | if (!this.options['name']) {
21 | return this.prompt([{
22 | type: 'input',
23 | name: 'name',
24 | message: 'Enter a name',
25 | }]).then(res => {
26 | this.appConfig['name'] = res.name
27 | })
28 | } else {
29 | this.appConfig['name'] = this.options['name']
30 | return Promise.resolve()
31 | }
32 | }
33 |
34 | writing() {
35 | let name = this.appConfig['name']
36 | this.fs.copyTpl(
37 | this.templatePath("index.guard.ts"),
38 | this.destinationPath(`src/modules/common/${name.toLowerCase()}.guard.ts`),
39 | { config: this.appConfig, kebabToCamel, kebabToPascal }
40 | );
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/generators/interceptor/templates/exception.interceptor.ts:
--------------------------------------------------------------------------------
1 | import { Interceptor, NestInterceptor, ExecutionContext, HttpStatus } from '@nestjs/common';
2 | import { HttpException } from '@nestjs/common';
3 | import { Observable } from 'rxjs/Observable';
4 | import 'rxjs/add/operator/catch';
5 | import 'rxjs/add/observable/throw';
6 |
7 | @Interceptor()
8 | export class ExceptionInterceptor implements NestInterceptor {
9 | intercept(dataOrRequest, context: ExecutionContext, stream$: Observable): Observable {
10 | return stream$.catch((err) => Observable.throw(
11 | new HttpException('Exception interceptor message', HttpStatus.BAD_GATEWAY),
12 | ));
13 | }
14 | }
--------------------------------------------------------------------------------
/generators/interceptor/templates/index.interceptor.ts:
--------------------------------------------------------------------------------
1 | import { Interceptor, NestInterceptor, ExecutionContext } from '@nestjs/common';
2 | import { Observable } from 'rxjs/Observable';
3 | import 'rxjs/add/observable/of';
4 |
5 | @Interceptor()
6 | export abstract class <%= kebabToPascal(config.nmae) %>Interceptor implements NestInterceptor {
7 |
8 | intercept(dataOrRequest, context: ExecutionContext, stream$: Observable): Observable {
9 | return stream$;
10 | }
11 | }
--------------------------------------------------------------------------------
/generators/interceptor/templates/logging.interceptor.ts:
--------------------------------------------------------------------------------
1 | import { Interceptor, NestInterceptor, ExecutionContext } from '@nestjs/common';
2 | import { Observable } from 'rxjs/Observable';
3 | import 'rxjs/add/operator/do';
4 |
5 | @Interceptor()
6 | export class LoggingInterceptor implements NestInterceptor {
7 | intercept(dataOrRequest, context: ExecutionContext, stream$: Observable): Observable {
8 | console.log('Before...');
9 | const now = Date.now();
10 |
11 | return stream$.do(
12 | () => console.log(`After... ${Date.now() - now}ms`),
13 | );
14 | }
15 | }
--------------------------------------------------------------------------------
/generators/interceptor/templates/mixin-cache.interceptor.ts:
--------------------------------------------------------------------------------
1 | import { mixin } from '@nestjs/common';
2 | import { CacheInterceptor } from './cache.interceptor';
3 |
4 | export function mixinCacheInterceptor(isCached: () => boolean) {
5 | return mixin(class extends CacheInterceptor {
6 | protected readonly isCached = isCached;
7 | });
8 | }
--------------------------------------------------------------------------------
/generators/interceptor/templates/transform.interceptor.ts:
--------------------------------------------------------------------------------
1 | import { Interceptor, NestInterceptor, ExecutionContext } from '@nestjs/common';
2 | import { Observable } from 'rxjs/Observable';
3 | import 'rxjs/add/operator/map';
4 |
5 | @Interceptor()
6 | export class TransformInterceptor implements NestInterceptor {
7 | intercept(dataOrRequest, context: ExecutionContext, stream$: Observable): Observable {
8 | return stream$.map((data) => ({ data }));
9 | }
10 | }
--------------------------------------------------------------------------------
/generators/middleware/USAGE:
--------------------------------------------------------------------------------
1 |
2 |
3 | Description:
4 | Creates a minimalistic NESTJS middleware
5 |
6 | Example:
7 | yo nestjs:middleware NAME
8 |
9 | - NAME - OPTIONAL - the name of the module (use kebab-case)
10 |
11 | This will create:
12 | `/src/modules/common/${NAME}.middleware.ts`
13 |
14 |
15 |
--------------------------------------------------------------------------------
/generators/middleware/index.js:
--------------------------------------------------------------------------------
1 |
2 | const path = require('path')
3 | const chalk = require('chalk')
4 | const Generator = require('yeoman-generator');
5 | const yosay = require('yosay')
6 | const kebabToPascal = require('../../utils/case-change').kebabToPascal
7 | const kebabToCamel = require('../../utils/case-change').kebabToCamel
8 | const toLower = require('../../utils/case-change').toLower
9 |
10 | module.exports = class extends Generator {
11 | constructor(args, opt) {
12 | super(args, opt)
13 | this.argument("name", {
14 | required: false
15 | })
16 | this.appConfig = {}
17 | }
18 |
19 | prompting() {
20 | if (!this.options['name']) {
21 | return this.prompt([{
22 | type: 'input',
23 | name: 'name',
24 | message: 'Enter a name',
25 | }]).then(res => {
26 | this.appConfig['name'] = res.name
27 | })
28 | } else {
29 | this.appConfig['name'] = this.options['name']
30 | return Promise.resolve()
31 | }
32 | }
33 |
34 | writing() {
35 | let name = this.appConfig['name']
36 | this.fs.copyTpl(
37 | this.templatePath("index.middleware.ts"),
38 | this.destinationPath(`src/modules/common/${name.toLowerCase()}.middleware.ts`),
39 | { config: this.appConfig, kebabToCamel, kebabToPascal }
40 | );
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/generators/middleware/templates/index.middleware.ts:
--------------------------------------------------------------------------------
1 | import { Middleware, NestMiddleware, ExpressMiddleware } from '@nestjs/common';
2 |
3 | @Middleware()
4 | export class <%= kebabToPascal(config.name) %>Middleware implements NestMiddleware {
5 | resolve(name: string): ExpressMiddleware {
6 | return (req, res, next) => {
7 | next();
8 | };
9 | }
10 | }
--------------------------------------------------------------------------------
/generators/module/USAGE:
--------------------------------------------------------------------------------
1 |
2 |
3 | Description:
4 | Creates a minimalistic NESTJS module
5 |
6 | Example:
7 | yo nestjs:module NAME --MODULE-TYPE
8 |
9 | - NAME - REQUIRED - name of module (use kebeb-case)
10 | - MODULE-TYPE - type of module
11 | OPTIONS
12 | - A mongoose module
13 | - A sequelize module
14 | - An SQL typeorm module
15 | - An mongo typeorm module
16 | - An graphQl module
17 |
18 | This will create:
19 | `/src/modules/common/${NAME}.module.ts`
20 |
21 |
22 |
--------------------------------------------------------------------------------
/generators/module/index.js:
--------------------------------------------------------------------------------
1 | const path = require('path')
2 | const os = require('os')
3 | const chalk = require('chalk')
4 | const Generator = require('yeoman-generator');
5 | const yosay = require('yosay')
6 | const walkDir = require('../../utils/walk-dir')
7 | const moduleTypes = require('./module-types')
8 | const kebabToPascal = require('../../utils/case-change').kebabToPascal
9 | const kebabToCamel = require('../../utils/case-change').kebabToCamel
10 | const toLower = require('../../utils/case-change').toLower
11 |
12 | module.exports = class extends Generator {
13 | constructor(args, opt) {
14 | super(args, opt)
15 | this.option('mongoose-module')
16 | this.option('sequelize-module')
17 | this.option('sql-typeorm-module')
18 | this.option('mongo-typeorm-module')
19 | this.option('graphql-module')
20 | this.argument('name', {
21 | required: true,
22 | description: "The name of the module to create",
23 | type: String
24 | })
25 | this.myConfig = {}
26 | }
27 |
28 | prompting() {
29 | // Removes plural 's' from module names
30 | let name = this.options['name']
31 | this.myConfig.name = name.endsWith('s') ? name.substr(0, name.length - 1) : name
32 | // Checks if user added a module option
33 | if (this.options['mongoose-module']) {
34 | this.myConfig.moduleType = "mongoose-module"
35 | return Promise.resolve()
36 | }
37 | if (this.options['sequelize-module']) {
38 | this.myConfig.moduleType = "sequelize-module"
39 | return Promise.resolve()
40 | }
41 | if (this.options['sql-typeorm-module']) {
42 | this.myConfig.moduleType = "sql-typeorm-module"
43 | return Promise.resolve()
44 | }
45 | if (this.options['mongo-typeorm-module']) {
46 | this.myConfig.moduleType = "mongo-typeorm-module"
47 | return Promise.resolve()
48 | }
49 | if (this.options['graphql-module']) {
50 | this.myConfig.moduleType = "graphql-module"
51 | return Promise.resolve()
52 | }
53 | return this.prompt([
54 | {
55 | type: 'list',
56 | name: 'type',
57 | message: 'What type of NESTJS module do you want to create?',
58 | choices: moduleTypes
59 | }
60 | ]).then(res => {
61 | this.myConfig.moduleType = res.type
62 | })
63 | }
64 |
65 | writing() {
66 | let { moduleType, name } = this.myConfig
67 | let templateOptions = { kebabToCamel, kebabToPascal, config: this.myConfig }
68 | switch (moduleType) {
69 | case "mongoose-module": {
70 | this.fs.copyTpl(this.templatePath(moduleType + '/cats/dto/create-cat.dto.ts'),
71 | this.destinationPath(`src/modules/${name}s/dto/create-${name}.dto.ts`), templateOptions)
72 |
73 | this.fs.copyTpl(this.templatePath(moduleType + '/cats/interfaces/cat.interface.ts'),
74 | this.destinationPath(`src/modules/${name}s/interfaces/${name}.interface.ts`), templateOptions)
75 |
76 | this.fs.copyTpl(this.templatePath(moduleType + '/cats/schemas/cat.schema.ts'),
77 | this.destinationPath(`src/modules/${name}s/schemas/${name}.schema.ts`), templateOptions)
78 |
79 | this.fs.copyTpl(this.templatePath(moduleType + '/cats/cats.controller.ts'),
80 | this.destinationPath(`src/modules/${name}s/${name}s.controller.ts`), templateOptions)
81 |
82 | this.fs.copyTpl(this.templatePath(moduleType + '/cats/cats.module.ts'),
83 | this.destinationPath(`src/modules/${name}s/${name}s.module.ts`), templateOptions)
84 |
85 | this.fs.copyTpl(this.templatePath(moduleType + '/cats/cats.service.ts'),
86 | this.destinationPath(`src/modules/${name}s/${name}s.service.ts`), templateOptions)
87 | return
88 | }
89 | case "sequelize-module": {
90 | this.fs.copyTpl(this.templatePath(moduleType + '/cats/dto/create-cat.dto.ts'),
91 | this.destinationPath(`src/modules/${name}s/dto/create-${name}.dto.ts`), templateOptions)
92 |
93 | this.fs.copyTpl(this.templatePath(moduleType + '/cats/cat.entity.ts'),
94 | this.destinationPath(`src/modules/${name}s/${name}.entity.ts`), templateOptions)
95 |
96 | this.fs.copyTpl(this.templatePath(moduleType + '/cats/cats.controller.ts'),
97 | this.destinationPath(`src/modules/${name}s/${name}s.controller.ts`), templateOptions)
98 |
99 | this.fs.copyTpl(this.templatePath(moduleType + '/cats/cats.module.ts'),
100 | this.destinationPath(`src/modules/${name}s/${name}s.module.ts`), templateOptions)
101 |
102 | this.fs.copyTpl(this.templatePath(moduleType + '/cats/cats.providers.ts'),
103 | this.destinationPath(`src/modules/${name}s/${name}s.providers.ts`), templateOptions)
104 |
105 | this.fs.copyTpl(this.templatePath(moduleType + '/cats/cats.service.ts'),
106 | this.destinationPath(`src/modules/${name}s/${name}s.service.ts`), templateOptions)
107 | return
108 | }
109 | case "sql-typeorm-module":
110 | case "mongo-typeorm-module": {
111 | this.fs.copyTpl(this.templatePath(moduleType + '/cats/cat.entity.ts'),
112 | this.destinationPath(`src/modules/${name}s/${name}.entity.ts`), templateOptions)
113 |
114 | this.fs.copyTpl(this.templatePath(moduleType + '/cats/cats.controller.ts'),
115 | this.destinationPath(`src/modules/${name}s/${name}s.controller.ts`), templateOptions)
116 |
117 | this.fs.copyTpl(this.templatePath(moduleType + '/cats/cats.module.ts'),
118 | this.destinationPath(`src/modules/${name}s/${name}s.module.ts`), templateOptions)
119 |
120 | this.fs.copyTpl(this.templatePath(moduleType + '/cats/cats.service.ts'),
121 | this.destinationPath(`src/modules/${name}s/${name}s.service.ts`), templateOptions)
122 | return
123 | }
124 | case "graphql-module": {
125 | this.fs.copyTpl(this.templatePath(moduleType + '/cats/interfaces/cat.interface.ts'),
126 | this.destinationPath(`src/modules/${name}s/interfaces/${name}.interface.ts`), templateOptions)
127 |
128 | this.fs.copyTpl(this.templatePath(moduleType + '/cats/cats.guard.ts'),
129 | this.destinationPath(`src/modules/${name}s/${name}s.guard.ts`), templateOptions)
130 |
131 | this.fs.copyTpl(this.templatePath(moduleType + '/cats/cats.module.ts'),
132 | this.destinationPath(`src/modules/${name}s/${name}s.module.ts`), templateOptions)
133 |
134 | this.fs.copyTpl(this.templatePath(moduleType + '/cats/cats.resolvers.ts'),
135 | this.destinationPath(`src/modules/${name}s/${name}s.resolvers.ts`), templateOptions)
136 |
137 | this.fs.copyTpl(this.templatePath(moduleType + '/cats/cats.service.ts'),
138 | this.destinationPath(`src/modules/${name}s/${name}s.service.ts`), templateOptions)
139 |
140 | this.fs.copyTpl(this.templatePath(moduleType + '/cats/cats.types.graphql'),
141 | this.destinationPath(`src/modules/${name}s/${name}s.types.graphql`), templateOptions)
142 | return
143 | }
144 | }
145 | }
146 | };
--------------------------------------------------------------------------------
/generators/module/module-types.js:
--------------------------------------------------------------------------------
1 | module.exports = [
2 | {
3 | name: 'A mongoose module',
4 | value: 'mongoose-module'
5 | },
6 | {
7 | name: 'A sequelize module',
8 | value: 'sequelize-module'
9 | },
10 | {
11 | name: 'An SQL typeorm module',
12 | value: 'sql-typeorm-module'
13 | },
14 | {
15 | name: 'An mongo typeorm module',
16 | value: 'mongo-typeorm-module'
17 | },
18 | {
19 | name: 'An graphQl module',
20 | value: 'graphql-module'
21 | },
22 | ]
--------------------------------------------------------------------------------
/generators/module/templates/graphql-module/cats/cats.guard.ts:
--------------------------------------------------------------------------------
1 | import { CanActivate, Guard, ExecutionContext } from '@nestjs/common';
2 |
3 | @Guard()
4 | export class <%= kebabToPascal(config.name) %>sGuard implements CanActivate {
5 | canActivate(request: any, context: ExecutionContext): boolean {
6 | return true;
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/generators/module/templates/graphql-module/cats/cats.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { <%= kebabToPascal(config.name) %>sService } from './<%= kebabToCamel(config.name) %>s.service';
3 | import { <%= kebabToPascal(config.name) %>sResolvers } from './<%= kebabToCamel(config.name) %>s.resolvers';
4 |
5 | @Module({
6 | components: [<%= kebabToPascal(config.name) %>sService, <%= kebabToPascal(config.name) %>sResolvers],
7 | })
8 | export class <%= kebabToPascal(config.name) %>sModule {}
9 |
--------------------------------------------------------------------------------
/generators/module/templates/graphql-module/cats/cats.resolvers.ts:
--------------------------------------------------------------------------------
1 | import { Component, UseGuards } from '@nestjs/common';
2 | import {
3 | Query,
4 | Mutation,
5 | Resolver,
6 | DelegateProperty,
7 | } from '@nestjs/graphql';
8 |
9 | import { <%= kebabToPascal(config.name) %> } from './interfaces/<%= kebabToCamel(config.name) %>.interface';
10 | import { <%= kebabToPascal(config.name) %>sService } from './<%= kebabToCamel(config.name) %>s.service';
11 | import { <%= kebabToPascal(config.name) %>sGuard } from './<%= kebabToCamel(config.name) %>s.guard';
12 | import { MergeInfo } from 'graphql-tools/dist/Interfaces';
13 |
14 | @Resolver('<%= kebabToPascal(config.name) %>')
15 | export class <%= kebabToPascal(config.name) %>sResolvers {
16 | constructor(private readonly <%= kebabToCamel(config.name) %>sService: <%= kebabToPascal(config.name) %>sService) {}
17 |
18 | @Query()
19 | @UseGuards(<%= kebabToPascal(config.name) %>sGuard)
20 | async get<%= kebabToPascal(config.name) %>s() {
21 | return await this.<%= kebabToCamel(config.name) %>sService.findAll();
22 | }
23 |
24 | @Query('<%= kebabToCamel(config.name) %>')
25 | async findOneById(id: number) {
26 | return await this.<%= kebabToCamel(config.name) %>sService.findOneById(id);
27 | }
28 |
29 | @Mutation('create<%= kebabToPascal(config.name) %>')
30 | async create(<%= kebabToCamel(config.name) %>: <%= kebabToPascal(config.name) %>) {
31 | await this.<%= kebabToCamel(config.name) %>sService.create(<%= kebabToCamel(config.name) %>);
32 | }
33 |
34 | @DelegateProperty('human')
35 | findHumansById(<%= kebabToCamel(config.name) %>: <%= kebabToPascal(config.name) %>) {
36 | return (mergeInfo: MergeInfo) => ({
37 | fragment: `fragment <%= kebabToPascal(config.name) %>Fragment on <%= kebabToPascal(config.name) %> { humanId }`,
38 | resolve(parent, args, context, info) {
39 | const humanId = parent.id;
40 | return mergeInfo.delegate(
41 | 'query',
42 | 'humanById',
43 | {
44 | id: humanId,
45 | },
46 | context,
47 | info,
48 | );
49 | },
50 | });
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/generators/module/templates/graphql-module/cats/cats.service.ts:
--------------------------------------------------------------------------------
1 | import { Component } from '@nestjs/common';
2 | import { <%= kebabToPascal(config.name) %> } from './interfaces/<%= kebabToCamel(config.name) %>.interface';
3 |
4 | @Component()
5 | export class <%= kebabToPascal(config.name) %>sService {
6 | private readonly <%= kebabToCamel(config.name) %>s: <%= kebabToPascal(config.name) %>[] = [
7 | { id: 1, name: '<%= kebabToPascal(config.name) %>', age: 5 },
8 | ];
9 |
10 | create(<%= kebabToCamel(config.name) %>: <%= kebabToPascal(config.name) %>) {
11 | this.<%= kebabToCamel(config.name) %>s.push(<%= kebabToCamel(config.name) %>);
12 | }
13 |
14 | findAll(): <%= kebabToPascal(config.name) %>[] {
15 | return this.<%= kebabToCamel(config.name) %>s;
16 | }
17 |
18 | findOneById(id: number): <%= kebabToPascal(config.name) %> {
19 | return this.<%= kebabToCamel(config.name) %>s.find((<%= kebabToCamel(config.name) %>) => <%= kebabToCamel(config.name) %>.id === id);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/generators/module/templates/graphql-module/cats/cats.types.graphql:
--------------------------------------------------------------------------------
1 | type Query {
2 | get<%= kebabToPascal(config.name) %>s: [<%= kebabToPascal(config.name) %>]
3 | <%= kebabToCamel(config.name) %>(id: ID!): <%= kebabToPascal(config.name) %>
4 | <%= kebabToCamel(config.name) %>ByHumanId(id: ID!): <%= kebabToPascal(config.name) %>
5 | }
6 |
7 | type Mutation {
8 | create<%= kebabToPascal(config.name) %>(name: String): <%= kebabToPascal(config.name) %>
9 | }
10 |
11 | type <%= kebabToPascal(config.name) %> {
12 | id: Int
13 | name: String
14 | age: Int
15 | humanId: Int
16 | }
--------------------------------------------------------------------------------
/generators/module/templates/graphql-module/cats/interfaces/cat.interface.ts:
--------------------------------------------------------------------------------
1 | export interface <%= kebabToPascal(config.name) %> {
2 | readonly id: number;
3 | readonly name: string;
4 | readonly age: number;
5 | }
--------------------------------------------------------------------------------
/generators/module/templates/mongo-typeorm-module/cats/cat.entity.ts:
--------------------------------------------------------------------------------
1 | import { Entity, Column, PrimaryGeneratedColumn, ObjectIdColumn, ObjectID } from 'typeorm';
2 |
3 | @Entity()
4 | export class <%= kebabToPascal(config.name) %> {
5 | @ObjectIdColumn()
6 | id: ObjectID;
7 |
8 | @Column()
9 | name: string;
10 | }
--------------------------------------------------------------------------------
/generators/module/templates/mongo-typeorm-module/cats/cats.controller.ts:
--------------------------------------------------------------------------------
1 | import { Controller, Get } from '@nestjs/common';
2 | import { <%= kebabToPascal(config.name) %>Service } from './<%= kebabToCamel(config.name) %>.service';
3 | import { <%= kebabToPascal(config.name) %> } from './<%= kebabToCamel(config.name) %>.entity';
4 |
5 | @Controller('<%= kebabToCamel(config.name) %>')
6 | export class <%= kebabToPascal(config.name) %>Controller {
7 | constructor(private readonly <%= kebabToCamel(config.name) %>Service: <%= kebabToPascal(config.name) %>Service) {}
8 |
9 | @Get()
10 | findAll(): Promise<<%= kebabToPascal(config.name) %>[]> {
11 | return this.<%= kebabToCamel(config.name) %>Service.findAll();
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/generators/module/templates/mongo-typeorm-module/cats/cats.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { <%= kebabToPascal(config.name) %>Service } from './<%= kebabToCamel(config.name) %>.service';
3 | import { <%= kebabToPascal(config.name) %>Controller } from './<%= kebabToCamel(config.name) %>.controller';
4 |
5 | @Module({
6 | components: [<%= kebabToPascal(config.name) %>Service],
7 | controllers: [<%= kebabToPascal(config.name) %>Controller],
8 | })
9 | export class <%= kebabToPascal(config.name) %>Module {}
10 |
--------------------------------------------------------------------------------
/generators/module/templates/mongo-typeorm-module/cats/cats.service.ts:
--------------------------------------------------------------------------------
1 | import { Component, Inject } from '@nestjs/common';
2 | import { InjectRepository } from '@nestjs/typeorm';
3 | import { Repository } from 'typeorm';
4 |
5 | import { <%= kebabToPascal(config.name) %> } from './<%= kebabToCamel(config.name) %>.entity';
6 |
7 | @Component()
8 | export class <%= kebabToPascal(config.name) %>Service {
9 | constructor(
10 | @InjectRepository(<%= kebabToPascal(config.name) %>)
11 | private readonly <%= kebabToCamel(config.name) %>Repository: Repository<<%= kebabToPascal(config.name) %>>,
12 | ) {}
13 |
14 | async findAll(): Promise<<%= kebabToPascal(config.name) %>[]> {
15 | return await this.<%= kebabToCamel(config.name) %>Repository.find();
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/generators/module/templates/mongoose-module/cats/cats.controller.ts:
--------------------------------------------------------------------------------
1 | import { Controller, Get, Post, Body, Param } from '@nestjs/common';
2 | import { Create<%= kebabToPascal(config.name) %>Dto } from './dto/create-<%= kebabToCamel(config.name) %>.dto';
3 | import { <%= kebabToPascal(config.name) %>sService } from './<%= kebabToCamel(config.name) %>s.service';
4 | import { <%= kebabToPascal(config.name) %> } from './interfaces/<%= kebabToCamel(config.name) %>.interface';
5 |
6 | @Controller('<%= kebabToCamel(config.name) %>s')
7 | export class <%= kebabToPascal(config.name) %>sController {
8 | constructor(private readonly <%= kebabToCamel(config.name) %>sService: <%= kebabToPascal(config.name) %>sService) {}
9 |
10 | @Post()
11 | async create(@Body() create<%= kebabToPascal(config.name) %>Dto: Create<%= kebabToPascal(config.name) %>Dto) {
12 | this.<%= kebabToCamel(config.name) %>sService.create(create<%= kebabToPascal(config.name) %>Dto);
13 | }
14 |
15 | @Get()
16 | async findAll(): Promise<<%= kebabToPascal(config.name) %>[]> {
17 | return this.<%= kebabToCamel(config.name) %>sService.findAll();
18 | }
19 | }
--------------------------------------------------------------------------------
/generators/module/templates/mongoose-module/cats/cats.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { <%= kebabToPascal(config.name) %>sController } from './<%= kebabToCamel(config.name) %>s.controller';
3 | import { <%= kebabToPascal(config.name) %>sService } from './<%= kebabToCamel(config.name) %>s.service';
4 |
5 | @Module({
6 | controllers: [<%= kebabToPascal(config.name) %>sController],
7 | components: [<%= kebabToPascal(config.name) %>sService],
8 | })
9 | export class <%= kebabToPascal(config.name) %>sModule {}
10 |
--------------------------------------------------------------------------------
/generators/module/templates/mongoose-module/cats/cats.service.ts:
--------------------------------------------------------------------------------
1 | import { Model } from 'mongoose';
2 | import { Component, Inject } from '@nestjs/common';
3 | import { InjectModel } from '@nestjs/mongoose';
4 |
5 | import { <%= kebabToPascal(config.name) %> } from './interfaces/<%= kebabToCamel(config.name) %>.interface';
6 | import { Create<%= kebabToPascal(config.name) %>Dto } from './dto/create-<%= kebabToCamel(config.name) %>.dto';
7 | import { <%= kebabToPascal(config.name) %>Schema } from './schemas/<%= kebabToCamel(config.name) %>.schema';
8 |
9 | @Component()
10 | export class <%= kebabToPascal(config.name) %>sService {
11 | constructor(@InjectModel(<%= kebabToPascal(config.name) %>Schema) private readonly <%= kebabToCamel(config.name) %>Model: Model<<%= kebabToPascal(config.name) %>>) {}
12 |
13 | async create(create<%= kebabToPascal(config.name) %>Dto: Create<%= kebabToPascal(config.name) %>Dto): Promise<<%= kebabToPascal(config.name) %>> {
14 | const created<%= kebabToPascal(config.name) %> = new this.<%= kebabToCamel(config.name) %>Model(create<%= kebabToPascal(config.name) %>Dto);
15 | return await created<%= kebabToPascal(config.name) %>.save();
16 | }
17 |
18 | async findAll(): Promise<<%= kebabToPascal(config.name) %>[]> {
19 | return await this.<%= kebabToCamel(config.name) %>Model.find().exec();
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/generators/module/templates/mongoose-module/cats/dto/create-cat.dto.ts:
--------------------------------------------------------------------------------
1 | export class Create<%= kebabToPascal(config.name) %>Dto {
2 | readonly name: string;
3 | readonly age: number;
4 | readonly breed: string;
5 | }
--------------------------------------------------------------------------------
/generators/module/templates/mongoose-module/cats/interfaces/cat.interface.ts:
--------------------------------------------------------------------------------
1 | import { Document } from 'mongoose';
2 |
3 | export interface <%= kebabToPascal(config.name) %> extends Document {
4 | readonly name: string;
5 | readonly age: number;
6 | readonly breed: string;
7 | }
--------------------------------------------------------------------------------
/generators/module/templates/mongoose-module/cats/schemas/cat.schema.ts:
--------------------------------------------------------------------------------
1 | import * as mongoose from 'mongoose';
2 |
3 | export const <%= kebabToPascal(config.name) %>Schema = new mongoose.Schema({
4 | name: String,
5 | age: Number,
6 | breed: String,
7 | });
--------------------------------------------------------------------------------
/generators/module/templates/sequelize-module/cats/cat.entity.ts:
--------------------------------------------------------------------------------
1 | import { Table, Column, Model } from 'sequelize-typescript';
2 |
3 | @Table
4 | export class <%= kebabToPascal(config.name) %> extends Model<<%= kebabToPascal(config.name) %>> {
5 | @Column
6 | name: string;
7 |
8 | }
9 |
--------------------------------------------------------------------------------
/generators/module/templates/sequelize-module/cats/cats.controller.ts:
--------------------------------------------------------------------------------
1 | import { Controller, Get, Post, Body, Param } from '@nestjs/common';
2 | import { Create<%= kebabToPascal(config.name) %>Dto } from './dto/create-<%= kebabToCamel(config.name) %>.dto';
3 | import { <%= kebabToPascal(config.name) %>sService } from './<%= kebabToCamel(config.name) %>s.service';
4 | import { <%= kebabToPascal(config.name) %> } from './<%= kebabToCamel(config.name) %>.entity';
5 |
6 | @Controller('<%= kebabToCamel(config.name) %>s')
7 | export class <%= kebabToPascal(config.name) %>sController {
8 | constructor(private readonly <%= kebabToCamel(config.name) %>sService: <%= kebabToPascal(config.name) %>sService) {}
9 |
10 | @Post()
11 | async create(@Body() create<%= kebabToPascal(config.name) %>Dto: Create<%= kebabToPascal(config.name) %>Dto) {
12 | await this.<%= kebabToCamel(config.name) %>sService.create(create<%= kebabToPascal(config.name) %>Dto);
13 | }
14 |
15 | @Get()
16 | async findAll(): Promise<<%= kebabToPascal(config.name) %>[]> {
17 | return await this.<%= kebabToCamel(config.name) %>sService.findAll();
18 | }
19 | }
--------------------------------------------------------------------------------
/generators/module/templates/sequelize-module/cats/cats.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { <%= kebabToPascal(config.name) %>sController } from './<%= kebabToCamel(config.name) %>s.controller';
3 | import { <%= kebabToPascal(config.name) %>sService } from './<%= kebabToCamel(config.name) %>s.service';
4 | import { <%= kebabToCamel(config.name) %>sProviders } from './<%= kebabToCamel(config.name) %>s.providers';
5 | import { DatabaseModule } from '../database/database.module';
6 |
7 | @Module({
8 | modules: [DatabaseModule],
9 | controllers: [<%= kebabToPascal(config.name) %>sController],
10 | components: [
11 | <%= kebabToPascal(config.name) %>sService,
12 | ...<%= kebabToCamel(config.name) %>sProviders,
13 | ],
14 | })
15 | export class <%= kebabToPascal(config.name) %>sModule {}
--------------------------------------------------------------------------------
/generators/module/templates/sequelize-module/cats/cats.providers.ts:
--------------------------------------------------------------------------------
1 | import { <%= kebabToPascal(config.name) %> } from './<%= kebabToCamel(config.name) %>.entity';
2 |
3 | export const <%= kebabToCamel(config.name) %>sProviders = [
4 | {
5 | provide: '<%= kebabToPascal(config.name) %>sRepository',
6 | useValue: <%= kebabToPascal(config.name) %>,
7 | },
8 | ];
--------------------------------------------------------------------------------
/generators/module/templates/sequelize-module/cats/cats.service.ts:
--------------------------------------------------------------------------------
1 |
2 | import { Component, Inject } from '@nestjs/common';
3 | import { Create<%= kebabToPascal(config.name) %>Dto } from './dto/create-<%= kebabToCamel(config.name) %>.dto';
4 | import { Model } from 'sequelize-typescript';
5 | import { <%= kebabToPascal(config.name) %> } from './<%= kebabToCamel(config.name) %>.entity';
6 |
7 | @Component()
8 | export class <%= kebabToPascal(config.name) %>sService {
9 | constructor(
10 | @Inject('<%= kebabToPascal(config.name) %>sRepository') private readonly <%= kebabToCamel(config.name) %>sRepository: typeof <%= kebabToPascal(config.name) %>) {}
11 |
12 | async create(create<%= kebabToPascal(config.name) %>Dto: Create<%= kebabToPascal(config.name) %>Dto): Promise<<%= kebabToPascal(config.name) %>> {
13 | const <%= kebabToCamel(config.name) %> = new <%= kebabToPascal(config.name) %>();
14 | <%= kebabToCamel(config.name) %>.name = create<%= kebabToPascal(config.name) %>Dto.name;
15 | <%= kebabToCamel(config.name) %>.breed = create<%= kebabToPascal(config.name) %>Dto.breed;
16 | <%= kebabToCamel(config.name) %>.age = create<%= kebabToPascal(config.name) %>Dto.age;
17 |
18 | return await <%= kebabToCamel(config.name) %>.save();
19 | }
20 |
21 | async findAll(): Promise<<%= kebabToPascal(config.name) %>[]> {
22 | return await this.<%= kebabToCamel(config.name) %>sRepository.findAll<<%= kebabToPascal(config.name) %>>();
23 | }
24 | }
--------------------------------------------------------------------------------
/generators/module/templates/sequelize-module/cats/dto/create-cat.dto.ts:
--------------------------------------------------------------------------------
1 | export class Create<%= kebabToPascal(config.name) %>Dto {
2 | readonly name: string;
3 | readonly age: number;
4 | readonly breed: string;
5 | }
--------------------------------------------------------------------------------
/generators/module/templates/sql-typeorm-module/cats/cat.entity.ts:
--------------------------------------------------------------------------------
1 | import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
2 |
3 | @Entity()
4 | export class <%= kebabToPascal(config.name) %> {
5 | @PrimaryGeneratedColumn()
6 | id: number;
7 |
8 | @Column({ length: 500 })
9 | name: string;
10 |
11 | }
--------------------------------------------------------------------------------
/generators/module/templates/sql-typeorm-module/cats/cats.controller.ts:
--------------------------------------------------------------------------------
1 | import { Controller, Get } from '@nestjs/common';
2 | import { <%= kebabToPascal(config.name) %>Service } from './<%= kebabToCamel(config.name) %>.service';
3 | import { <%= kebabToPascal(config.name) %> } from './<%= kebabToCamel(config.name) %>.entity';
4 |
5 | @Controller('<%= kebabToCamel(config.name) %>')
6 | export class <%= kebabToPascal(config.name) %>Controller {
7 | constructor(private readonly <%= kebabToCamel(config.name) %>Service: <%= kebabToPascal(config.name) %>Service) {}
8 |
9 | @Get()
10 | findAll(): Promise<<%= kebabToPascal(config.name) %>[]> {
11 | return this.<%= kebabToCamel(config.name) %>Service.findAll();
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/generators/module/templates/sql-typeorm-module/cats/cats.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { <%= kebabToPascal(config.name) %>Service } from './<%= kebabToCamel(config.name) %>.service';
3 | import { <%= kebabToPascal(config.name) %>Controller } from './<%= kebabToCamel(config.name) %>.controller';
4 |
5 | @Module({
6 | components: [<%= kebabToPascal(config.name) %>Service],
7 | controllers: [<%= kebabToPascal(config.name) %>Controller],
8 | })
9 | export class <%= kebabToPascal(config.name) %>Module {}
10 |
--------------------------------------------------------------------------------
/generators/module/templates/sql-typeorm-module/cats/cats.service.ts:
--------------------------------------------------------------------------------
1 | import { Component, Inject } from '@nestjs/common';
2 | import { InjectRepository } from '@nestjs/typeorm';
3 | import { Repository } from 'typeorm';
4 |
5 | import { <%= kebabToPascal(config.name) %> } from './<%= kebabToCamel(config.name) %>.entity';
6 |
7 | @Component()
8 | export class <%= kebabToPascal(config.name) %>Service {
9 | constructor(
10 | @InjectRepository(<%= kebabToPascal(config.name) %>)
11 | private readonly <%= kebabToCamel(config.name) %>Repository: Repository<<%= kebabToPascal(config.name) %>>,
12 | ) {}
13 |
14 | async findAll(): Promise<<%= kebabToPascal(config.name) %>[]> {
15 | return await this.<%= kebabToCamel(config.name) %>Repository.find();
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/generators/pipe/USAGE:
--------------------------------------------------------------------------------
1 |
2 |
3 | Description:
4 | Creates a minimalistic NESTJS pipe
5 |
6 | Example:
7 | yo nestjs:pipe NAME
8 |
9 | - NAME - OPTIONAL - the name of the module (use kebab-case)
10 |
11 | This will create:
12 | `/src/modules/common/${NAME}.pipe.ts`
13 |
14 |
15 |
--------------------------------------------------------------------------------
/generators/pipe/index.js:
--------------------------------------------------------------------------------
1 |
2 | const path = require('path')
3 | const chalk = require('chalk')
4 | const Generator = require('yeoman-generator');
5 | const yosay = require('yosay')
6 | const kebabToPascal = require('../../utils/case-change').kebabToPascal
7 | const kebabToCamel = require('../../utils/case-change').kebabToCamel
8 | const toLower = require('../../utils/case-change').toLower
9 |
10 | module.exports = class extends Generator {
11 | constructor(args, opt) {
12 | super(args, opt)
13 | this.argument("name", {
14 | required: false
15 | })
16 | this.appConfig = {}
17 | }
18 |
19 | prompting() {
20 | if (!this.options['name']) {
21 | return this.prompt([{
22 | type: 'input',
23 | name: 'name',
24 | message: 'Enter a name',
25 | }]).then(res => {
26 | this.appConfig['name'] = res.name
27 | })
28 | } else {
29 | this.appConfig['name'] = this.options['name']
30 | return Promise.resolve()
31 | }
32 | }
33 |
34 | writing() {
35 | let name = this.appConfig['name']
36 | this.fs.copyTpl(
37 | this.templatePath("index.decorator.ts"),
38 | this.destinationPath(`src/modules/common/${name.toLowerCase()}.decorator.ts`),
39 | { config: this.appConfig, kebabToCamel, kebabToPascal }
40 | );
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/generators/pipe/templates/index.pipe.ts:
--------------------------------------------------------------------------------
1 | import { PipeTransform, Pipe, ArgumentMetadata, HttpStatus } from '@nestjs/common';
2 |
3 | @Pipe()
4 | export class <%= config.name[0].toUpperCase() + config.name.substring(1) %>tPipe implements PipeTransform {
5 | async transform(value: string, metadata: ArgumentMetadata) {
6 | return value;
7 | }
8 | }
--------------------------------------------------------------------------------
/generators/pipe/templates/validation.pipe.ts:
--------------------------------------------------------------------------------
1 | import { HttpException } from '@nestjs/common';
2 | import { PipeTransform, Pipe, ArgumentMetadata, HttpStatus } from '@nestjs/common';
3 | import { validate } from 'class-validator';
4 | import { plainToClass } from 'class-transformer';
5 |
6 | @Pipe()
7 | export class ValidationPipe implements PipeTransform {
8 | async transform(value, metadata: ArgumentMetadata) {
9 | const { metatype } = metadata;
10 | if (!metatype || !this.toValidate(metatype)) {
11 | return value;
12 | }
13 | const object = plainToClass(metatype, value);
14 | const errors = await validate(object);
15 | if (errors.length > 0) {
16 | throw new HttpException('Validation failed', HttpStatus.BAD_REQUEST);
17 | }
18 | return value;
19 | }
20 |
21 | private toValidate(metatype): boolean {
22 | const types = [String, Boolean, Number, Array, Object];
23 | return !types.find((type) => metatype === type);
24 | }
25 | }
--------------------------------------------------------------------------------
/nestjs.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ashinzekene/generator-nestjs-app/6fa8f70e893b2c7ff2aaa766f81675279110d9ac/nestjs.jpg
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "generator-nestjs-app",
3 | "version": "0.1.2",
4 | "description": "A yeoman generator for nestjs apps",
5 | "repository": {
6 | "type": "git",
7 | "url": "git+https://github.com/ashinzekene/generator-nestjs-app.git"
8 | },
9 | "files": [
10 | "generators",
11 | "scripts",
12 | "utils"
13 | ],
14 | "keywords": [
15 | "yeoman-generator",
16 | "yeoman",
17 | "generator",
18 | "nestjs",
19 | "angular",
20 | "nodejs",
21 | "typescript",
22 | "microservices",
23 | "express",
24 | "typeorm",
25 | "sequelize",
26 | "mongogb",
27 | "mongoose",
28 | "graphql",
29 | "apollo"
30 | ],
31 | "author": "Ashinze Ekene",
32 | "bugs": {
33 | "url": "https://github.com/ashinzekene/generator-nestjs-app/issues"
34 | },
35 | "homepage": "https://github.com/ashinzekene/generator-nestjs-app#readme",
36 | "dependencies": {
37 | "chalk": "^2.3.0",
38 | "yeoman-generator": "^1.0.0",
39 | "yosay": "^2.0.1"
40 | },
41 | "devDependencies": {
42 | "chai": "^4.1.2",
43 | "yeoman-assert": "^3.1.0",
44 | "yeoman-test": "^1.7.0"
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/scripts/rename-template.js:
--------------------------------------------------------------------------------
1 | const fs = require("fs")
2 | const path = require("path")
3 |
4 | fs.readdirSync('./').forEach(mainDir => {
5 | fs.statSync(mainDir).isDirectory() ?
6 | fs.readdirSync(mainDir).forEach(dir => {
7 | fs.statSync(path.join(mainDir, dir)).isDirectory() ?
8 | fs.renameSync(path.join(mainDir, dir), path.join(mainDir, "templates")) :
9 | null
10 | }) :
11 | null
12 | })
--------------------------------------------------------------------------------
/scripts/write-index.js:
--------------------------------------------------------------------------------
1 | const path = require("path")
2 | const fs = require("fs")
3 |
4 | let index = `
5 | const path = require('path')
6 | const chalk = require('chalk')
7 | const Generator = require('yeoman-generator');
8 | const yosay = require('yosay')
9 | const kebabToPascal = require('../../utils/case-change').kebabToPascal
10 | const kebabToCamel = require('../../utils/case-change').kebabToCamel
11 | const toLower = require('../../utils/case-change').toLower
12 |
13 | module.exports = class extends Generator {
14 | constructor(args, opt) {
15 | super(args, opt)
16 | this.argument("name")
17 | this.appConfig = {}
18 | }
19 |
20 | prompting() {
21 | if (!this.options['name']) {
22 | return this.prompt([{
23 | type: 'input',
24 | name: 'name',
25 | message: 'Enter a name',
26 | }]).then(res => {
27 | this.appConfig['name'] = res.name
28 | })
29 | } else {
30 | this.appConfig['name'] = this.options['name']
31 | return Promise.resolve()
32 | }
33 | }
34 |
35 | writing() {
36 | let name = this.appConfig['name']
37 | this.fs.copyTpl(
38 | this.templatePath("index.decorator.ts"),
39 | this.destinationPath(\`src/modules/common/\${name.toLowerCase()}.decorator.ts\`),
40 | { config: this.appConfig, kebabToCamel, kebabToPascal }
41 | );
42 | }
43 | }
44 | `
45 |
46 | function dir(dir) {
47 | fs.readdirSync(dir).forEach(gen => {
48 | if (gen.search("app") === -1 && fs.statSync(dir+gen).isDirectory() ) {
49 | console.log(gen)
50 | fs.writeFileSync(path.resolve(dir, gen, "index.js"), index)
51 | }
52 | })
53 | }
54 |
55 | dir('./generators/')
--------------------------------------------------------------------------------
/scripts/write-readme.js:
--------------------------------------------------------------------------------
1 | const fs = require('fs')
2 | const path = require('path')
3 |
4 | const dir = 'generators/app/templates'
5 | var files = fs.readdirSync(dir)
6 | files.forEach(file => {
7 | fs.copyFileSync('README.md', `${dir}/${file}/README.md`)
8 | console.log(`DONE FOR ${file}`)
9 | })
--------------------------------------------------------------------------------
/scripts/write-usage.js:
--------------------------------------------------------------------------------
1 | const path = require("path")
2 | const fs = require("fs")
3 | let elem = ""
4 | const oldUsage = `
5 | Description:
6 | Create a chrome extension boilerplate generator that creates everything for you. have fun.
7 |
8 | Example:
9 | yeoman init chrome-extension $NAME[OPTIONAL]
10 |
11 | This will create:
12 | Gruntfile.js: Configuration for the task runner.
13 | bower.json: Front-end packages installed by bower.
14 | package.json: Development packages installed by npm.
15 |
16 | app/: Your application files.
17 | test/: Unit tests for your application.
18 | `
19 |
20 | usageFile = `
21 |
22 | Description:
23 | Creaates a minimalistic NESTJS ###elem###
24 |
25 | Example:
26 | yo nestjs:###elem### NAME
27 |
28 | - NAME - OPTIONAL - the name of the module (use kebab-case)
29 |
30 | This will create:
31 | \`/src/modules/common/\${NAME}.###elem###.ts\`
32 |
33 |
34 | `
35 |
36 | function write(dir) {
37 | fs.readdirSync(dir).forEach(cDir => {
38 | elem = cDir
39 | if (fs.statSync(path.resolve(dir, cDir)).isDirectory() && cDir !== 'app') {
40 | // console.log(path.resolve(dir+cDir+'\USAGE'))
41 | fs.writeFileSync(path.resolve(dir,cDir,'USAGE'), usageFile.replace(/###elem###/g, cDir))
42 | }
43 | })
44 | }
45 |
46 | write('./generators')
--------------------------------------------------------------------------------
/test.js:
--------------------------------------------------------------------------------
1 | console.log("Beginning...")
2 |
3 | process.stdin.setEncoding('utf-8')
4 | process.stdin.on('data', data => {
5 | changeName(data.trim())
6 | })
7 |
8 | function changeName(name) {
9 | var res
10 | console.log(name.indexOf('s'), ' ' ,name.endsWith('s'))
11 | if (name.endsWith('s')) {
12 | res = name.substr(0,name.length-1)
13 | } else {
14 | res = name
15 | }
16 | console.log(res+'\n')
17 | }
--------------------------------------------------------------------------------
/utils/case-change.js:
--------------------------------------------------------------------------------
1 | module.exports.kebabToCamel = str => str.split("-").map((a,b) => [...a].map((c,d) => d === 0 && b!==0 ? c.toUpperCase() : c.toLowerCase()).join('')).join('')
2 | module.exports.kebabToPascal = str => str.split("-").map((a) => [...a].map((b, c) => c === 0 ? b.toUpperCase() : b.toLowerCase()).join('')).join('')
3 | module.exports.kebabToConstant = str =>[...str].map(s=> s === " " || s == "-" ? "_" : s.toUpperCase()).join("")
4 | module.exports.toLower = str => [...str].filter((s) => s !== "-" && s !== " ").join('')
--------------------------------------------------------------------------------
/utils/walk-dir.js:
--------------------------------------------------------------------------------
1 | var fs = require("fs")
2 | var path = require("path")
3 |
4 | var fileList = []
5 | /**
6 | *
7 | * @param {string} dir
8 | * @param {bool} inner
9 | * @returns {array of files}
10 | * @description Walks down a file directory returning the path of the children directory,.
11 | * The inner params causes the function to return just the name of the children directory
12 | */
13 | function walkSync(dir, inner) {
14 | let files = fs.readdirSync(dir).forEach(file => {
15 | fs.statSync(path.join(dir, file)).isDirectory() ?
16 | walkSync(path.join(dir, file)) :
17 | fileList.push(path.join(dir, file))
18 | });
19 | if (inner) return fileList.map(pth => pth.replace(/\b\w+\\/, ""))
20 | return fileList
21 | }
22 |
23 | module.exports = walkSync
--------------------------------------------------------------------------------
/yeoman.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ashinzekene/generator-nestjs-app/6fa8f70e893b2c7ff2aaa766f81675279110d9ac/yeoman.png
--------------------------------------------------------------------------------