├── .circleci
└── config.yml
├── .eslintrc.js
├── .github
├── dependabot.yml
└── workflows
│ └── npm-publish.yml
├── .gitignore
├── .prettierrc
├── CHANGELOG.md
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── nest-cli.json
├── package-lock.json
├── package.json
├── src
├── awesome-library-core.module.ts
├── awesome-library.constants.ts
├── awesome-library.interfaces.ts
├── awesome-library.module.ts
├── awesome-library.service.spec.ts
├── awesome-library.service.ts
└── index.ts
├── tsconfig.build.json
└── tsconfig.json
/.circleci/config.yml:
--------------------------------------------------------------------------------
1 | # Use the latest 2.1 version of CircleCI pipeline process engine.
2 | # See: https://circleci.com/docs/2.0/configuration-reference
3 | version: 2.1
4 |
5 | jobs:
6 | test:
7 | parameters:
8 | node-version:
9 | type: string
10 | docker:
11 | - image: circleci/node:<< parameters.node-version >>
12 | resource_class: small
13 | steps:
14 | - checkout
15 | - restore_cache:
16 | # See the configuration reference documentation for more details on using restore_cache and save_cache steps
17 | # https://circleci.com/docs/2.0/configuration-reference/?section=reference#save_cache
18 | keys:
19 | - node-deps-v1-{{ .Branch }}-{{checksum "package-lock.json"}}
20 | - run:
21 | name: install packages
22 | command: npm ci
23 | - save_cache:
24 | key: node-deps-v1-{{ .Branch }}-{{checksum "package-lock.json"}}
25 | paths:
26 | - ~/.npm
27 | - run:
28 | name: Run Lint
29 | command: npm run lint:ci
30 | - run:
31 | name: Run Tests
32 | command: npm run test:cov
33 |
34 | workflows:
35 | test-workflow:
36 | jobs:
37 | - test:
38 | matrix:
39 | parameters:
40 | node-version: ['16.4', '14.17', '12.22']
41 |
--------------------------------------------------------------------------------
/.eslintrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | parser: '@typescript-eslint/parser',
3 | parserOptions: {
4 | project: 'tsconfig.json',
5 | sourceType: 'module',
6 | },
7 | plugins: ['@typescript-eslint/eslint-plugin', 'import'],
8 | extends: [
9 | 'plugin:@typescript-eslint/recommended',
10 | 'plugin:prettier/recommended',
11 | ],
12 | root: true,
13 | env: {
14 | node: true,
15 | jest: true,
16 | },
17 | ignorePatterns: ['.eslintrc.js'],
18 | rules: {
19 | '@typescript-eslint/interface-name-prefix': 'off',
20 | '@typescript-eslint/explicit-function-return-type': 'off',
21 | '@typescript-eslint/explicit-module-boundary-types': 'off',
22 | '@typescript-eslint/no-explicit-any': 'off',
23 | 'import/order': [
24 | 'error',
25 | {
26 | alphabetize: { order: 'asc' },
27 | groups: [
28 | ['builtin', 'external'],
29 | ['internal', 'parent', 'sibling', 'index'],
30 | ],
31 | 'newlines-between': 'never',
32 | },
33 | ],
34 | },
35 | };
36 |
--------------------------------------------------------------------------------
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | version: 2
2 | updates:
3 | - package-ecosystem: npm
4 | directory: '/'
5 | schedule:
6 | interval: daily
7 | open-pull-requests-limit: 10
8 |
--------------------------------------------------------------------------------
/.github/workflows/npm-publish.yml:
--------------------------------------------------------------------------------
1 | on:
2 | push:
3 | branches:
4 | - main
5 |
6 | jobs:
7 | publish:
8 | runs-on: ubuntu-latest
9 | steps:
10 | - uses: actions/checkout@v1
11 | - uses: actions/setup-node@v1
12 | with:
13 | node-version: 12
14 | - run: npm install
15 | - run: npm test
16 | - uses: JS-DevTools/npm-publish@v1
17 | with:
18 | token: ${{ secrets.NPM_TOKEN }}
19 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # compiled output
2 | /dist
3 | /node_modules
4 |
5 | # Logs
6 | logs
7 | *.log
8 | npm-debug.log*
9 | yarn-debug.log*
10 | yarn-error.log*
11 | lerna-debug.log*
12 |
13 | # OS
14 | .DS_Store
15 |
16 | # Tests
17 | /coverage
18 | /.nyc_output
19 |
20 | # IDEs and editors
21 | /.idea
22 | .project
23 | .classpath
24 | .c9/
25 | *.launch
26 | .settings/
27 | *.sublime-workspace
28 |
29 | # IDE - VSCode
30 | .vscode/*
31 | !.vscode/settings.json
32 | !.vscode/tasks.json
33 | !.vscode/launch.json
34 | !.vscode/extensions.json
--------------------------------------------------------------------------------
/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "singleQuote": true,
3 | "trailingComma": "all"
4 | }
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | RELEASE 1.0.0
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contributing
2 |
3 | 1. [Fork it](https://help.github.com/articles/fork-a-repo/)
4 | 2. Install dependencies (`npm install`)
5 | 3. Create your feature branch (`git checkout -b my-new-feature`)
6 | 4. Commit your changes (`git commit -am 'Added some feature'`)
7 | 5. Test your changes (`npm test`)
8 | 6. Push to the branch (`git push origin my-new-feature`)
9 | 7. [Create new Pull Request](https://help.github.com/articles/creating-a-pull-request/)
10 |
11 | ## Testing
12 |
13 | We use [Jest](https://github.com/facebook/jest) to write tests. Run our test suite with this command:
14 |
15 | ```
16 | npm test
17 | ```
18 |
19 | ## Code Style
20 |
21 | We use [Prettier](https://prettier.io/) and tslint to maintain code style and best practices.
22 | Please make sure your PR adheres to the guides by running:
23 |
24 | ```
25 | npm run format
26 | ```
27 |
28 | and
29 | ```
30 | npm run lint
31 | ```
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 Brian Zuker
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, 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,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
9 | NestJS npm Library Template
10 |
11 |
16 |
17 | ### Installation
18 |
19 | 1. Clone the repo
20 | 2. Run npm/yarn install
21 |
22 | ```bash
23 | cd nestjs-library-template
24 | npm install
25 | ```
26 |
27 | # Publishing
28 |
29 | In order to publish to NPM, create a user in npmjs.com. Follow [this guide](https://dev.to/nestjs/publishing-nestjs-packages-with-npm-21fm) for a more detailed explanation.
30 |
31 | This repo includes a Github Action that will publish the package to npm on each push to `main`, where the version in the package.json has changed. In order for this to work you need to add your NPM_TOKEN to the repository secrets
32 | More information [here](https://github.com/marketplace/actions/npm-publish).
33 |
34 | ## Change Log
35 |
36 | See [Changelog](CHANGELOG.md) for more information.
37 |
38 | ## Contributing
39 |
40 | Contributions welcome! See [Contributing](CONTRIBUTING.md).
41 |
42 | ## Author
43 |
44 | Brian Zuker (https://twitter.com/Brian_Zuker))
45 |
46 | ## License
47 |
48 | Licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
49 |
50 | # Shoutout
51 | This repo was adapted from https://github.com/nestjsplus/nestjs-package-starter
--------------------------------------------------------------------------------
/nest-cli.json:
--------------------------------------------------------------------------------
1 | {
2 | "language": "ts",
3 | "collection": "@nestjs/schematics",
4 | "sourceRoot": "src"
5 | }
6 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "nestjs-library-template",
3 | "version": "1.0.1",
4 | "description": "NestJS library template",
5 | "author": "Brian Zuker ",
6 | "license": "MIT",
7 | "readmeFilename": "README.md",
8 | "main": "dist/index.js",
9 | "files": [
10 | "dist/**/*",
11 | "*.md"
12 | ],
13 | "scripts": {
14 | "start:dev": "tsc -w",
15 | "build": "tsc",
16 | "prepare": "npm run build",
17 | "format": "prettier --write \"src/**/*.ts\"",
18 | "lint": "eslint \"{src,apps,libs,test,__tests__}/**/*.ts\" --fix",
19 | "lint:ci": "eslint \"{src,apps,libs,test,__tests__}/**/*.ts\"",
20 | "test": "jest",
21 | "test:watch": "jest --watch",
22 | "test:cov": "jest --coverage",
23 | "test:e2e": "jest --config ./test/jest-e2e.json"
24 | },
25 | "precommit": [
26 | "lint:ci",
27 | "test:cov"
28 | ],
29 | "keywords": [
30 | "nestjs"
31 | ],
32 | "publishConfig": {
33 | "access": "public"
34 | },
35 | "repository": {
36 | "type": "git",
37 | "url": "https://github.com/bzuker/nestjs-library-template"
38 | },
39 | "bugs": "https://github.com/bzuker/nestjs-library-template",
40 | "peerDependencies": {
41 | "@nestjs/common": "^8.0.0",
42 | "rxjs": "^7.x"
43 | },
44 | "dependencies": {
45 | "@golevelup/nestjs-modules": "^0.4.3",
46 | "rxjs": "^7.x"
47 | },
48 | "devDependencies": {
49 | "@nestjs/common": "^8.1.2",
50 | "@nestjs/core": "^8.1.2",
51 | "@nestjs/platform-express": "^8.1.2",
52 | "@nestjs/testing": "^8.1.2",
53 | "@types/express": "^4.17.13",
54 | "@types/jest": "27.0.2",
55 | "@types/node": "16.11.6",
56 | "@types/supertest": "2.0.11",
57 | "@typescript-eslint/eslint-plugin": "^5.3.0",
58 | "@typescript-eslint/parser": "^5.3.0",
59 | "eslint": "^8.2.0",
60 | "eslint-config-prettier": "^8.3.0",
61 | "eslint-plugin-import": "^2.25.2",
62 | "eslint-plugin-prettier": "^4.0.0",
63 | "jest": "27.3.1",
64 | "pre-commit": "^1.2.2",
65 | "prettier": "2.4.1",
66 | "reflect-metadata": "^0.1.13",
67 | "supertest": "6.1.6",
68 | "ts-jest": "27.0.7",
69 | "ts-node": "10.4.0",
70 | "tsc-watch": "4.5.0",
71 | "tsconfig-paths": "3.11.0",
72 | "typescript": "4.4.4"
73 | },
74 | "jest": {
75 | "moduleFileExtensions": [
76 | "js",
77 | "json",
78 | "ts"
79 | ],
80 | "rootDir": "src",
81 | "testRegex": ".spec.ts$",
82 | "transform": {
83 | "^.+\\.(t|j)s$": "ts-jest"
84 | },
85 | "coverageDirectory": "../coverage",
86 | "testEnvironment": "node"
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/src/awesome-library-core.module.ts:
--------------------------------------------------------------------------------
1 | import { createConfigurableDynamicRootModule } from '@golevelup/nestjs-modules';
2 | import { Module } from '@nestjs/common';
3 | import { AWESOME_LIBRARY_CONFIG } from './awesome-library.constants';
4 | import { AwesomeLibraryConfig } from './awesome-library.interfaces';
5 | import { AwesomeLibraryService } from './awesome-library.service';
6 |
7 | @Module({
8 | providers: [AwesomeLibraryService],
9 | })
10 | export class AwesomeLibraryCoreModule extends createConfigurableDynamicRootModule<
11 | AwesomeLibraryCoreModule,
12 | AwesomeLibraryConfig
13 | >(AWESOME_LIBRARY_CONFIG, {}) {}
14 |
--------------------------------------------------------------------------------
/src/awesome-library.constants.ts:
--------------------------------------------------------------------------------
1 | export const AWESOME_LIBRARY_CONFIG = Symbol('AwesomeLibraryConfig');
2 |
--------------------------------------------------------------------------------
/src/awesome-library.interfaces.ts:
--------------------------------------------------------------------------------
1 | export interface AwesomeLibraryConfig {
2 | someConfig?: string;
3 | }
4 |
--------------------------------------------------------------------------------
/src/awesome-library.module.ts:
--------------------------------------------------------------------------------
1 | import { AsyncModuleConfig } from '@golevelup/nestjs-modules';
2 | import { DynamicModule, Module } from '@nestjs/common';
3 | import { AwesomeLibraryCoreModule } from './awesome-library-core.module';
4 | import { AwesomeLibraryConfig } from './awesome-library.interfaces';
5 |
6 | @Module({})
7 | export class AwesomeLibraryModule {
8 | static forRoot(config?: AwesomeLibraryConfig): DynamicModule {
9 | return AwesomeLibraryCoreModule.forRoot(
10 | AwesomeLibraryCoreModule,
11 | config || {},
12 | );
13 | }
14 |
15 | static forRootAsync(
16 | config?: AsyncModuleConfig,
17 | ): DynamicModule {
18 | return AwesomeLibraryCoreModule.forRootAsync(
19 | AwesomeLibraryCoreModule,
20 | config,
21 | );
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/awesome-library.service.spec.ts:
--------------------------------------------------------------------------------
1 | import { Test, TestingModule } from '@nestjs/testing';
2 | import { AWESOME_LIBRARY_CONFIG } from './awesome-library.constants';
3 | import { AwesomeLibraryService } from './awesome-library.service';
4 |
5 | describe('AwesomeLibraryService', () => {
6 | let service: AwesomeLibraryService;
7 |
8 | beforeEach(async () => {
9 | const module: TestingModule = await Test.createTestingModule({
10 | providers: [
11 | AwesomeLibraryService,
12 | {
13 | provide: AWESOME_LIBRARY_CONFIG,
14 | useValue: {
15 | someConfig: 'World',
16 | },
17 | },
18 | ],
19 | }).compile();
20 |
21 | service = module.get(AwesomeLibraryService);
22 | });
23 |
24 | it('should be defined', () => {
25 | expect(service).toBeDefined();
26 | });
27 |
28 | it('should say hello based on the config', () => {
29 | expect(service.getHello()).toBe('Hello World!');
30 | });
31 | });
32 |
--------------------------------------------------------------------------------
/src/awesome-library.service.ts:
--------------------------------------------------------------------------------
1 | import { Inject, Injectable } from '@nestjs/common';
2 | import { AWESOME_LIBRARY_CONFIG } from './awesome-library.constants';
3 | import { AwesomeLibraryConfig } from './awesome-library.interfaces';
4 |
5 | @Injectable()
6 | export class AwesomeLibraryService {
7 | constructor(
8 | @Inject(AWESOME_LIBRARY_CONFIG)
9 | private readonly config: AwesomeLibraryConfig,
10 | ) {}
11 |
12 | getHello(): string {
13 | return `Hello ${this.config.someConfig}!`;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/index.ts:
--------------------------------------------------------------------------------
1 | export { AwesomeLibraryModule } from './awesome-library.module';
2 | export * from './awesome-library.service';
3 | export * from './awesome-library.constants';
4 | export * from './awesome-library.interfaces';
5 |
--------------------------------------------------------------------------------
/tsconfig.build.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "./tsconfig.json",
3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
4 | }
5 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "declaration": true,
5 | "removeComments": true,
6 | "emitDecoratorMetadata": true,
7 | "experimentalDecorators": true,
8 | "target": "es6",
9 | "sourceMap": false,
10 | "outDir": "./dist",
11 | "rootDir": "./src",
12 | "baseUrl": "./",
13 | "noLib": false
14 | },
15 | "include": ["src/**/*.ts"],
16 | "exclude": ["node_modules"]
17 | }
18 |
--------------------------------------------------------------------------------