feat(api): setup nestjs

This commit is contained in:
Julian Tölle 2020-01-25 22:19:14 +01:00
commit db62d5d908
25 changed files with 10115 additions and 0 deletions

37
.gitignore vendored Normal file
View file

@ -0,0 +1,37 @@
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
# Credentials
.env

23
Dockerfile Normal file
View file

@ -0,0 +1,23 @@
FROM node:12-alpine as common
LABEL org.label-schema.schema-version="1.0" \
org.label-schema.name="listory" \
stage="common"
WORKDIR /app
COPY *.json /app/
FROM common as builder
LABEL stage="builder"
RUN npm ci
COPY src/ /app/src/
RUN npm run build
FROM common as app
LABEL stage="app"
RUN npm ci --only=production
COPY --from=builder /app/dist/ /app/dist/
CMD ["dist/main"]

75
README.md Normal file
View file

@ -0,0 +1,75 @@
<p align="center">
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo_text.svg" width="320" alt="Nest Logo" /></a>
</p>
[travis-image]: https://api.travis-ci.org/nestjs/nest.svg?branch=master
[travis-url]: https://travis-ci.org/nestjs/nest
[linux-image]: https://img.shields.io/travis/nestjs/nest/master.svg?label=linux
[linux-url]: https://travis-ci.org/nestjs/nest
<p align="center">A progressive <a href="http://nodejs.org" target="blank">Node.js</a> framework for building efficient and scalable server-side applications, heavily inspired by <a href="https://angular.io" target="blank">Angular</a>.</p>
<p align="center">
<a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
<a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
<a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/dm/@nestjs/core.svg" alt="NPM Downloads" /></a>
<a href="https://travis-ci.org/nestjs/nest"><img src="https://api.travis-ci.org/nestjs/nest.svg?branch=master" alt="Travis" /></a>
<a href="https://travis-ci.org/nestjs/nest"><img src="https://img.shields.io/travis/nestjs/nest/master.svg?label=linux" alt="Linux" /></a>
<a href="https://coveralls.io/github/nestjs/nest?branch=master"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#5" alt="Coverage" /></a>
<a href="https://gitter.im/nestjs/nestjs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=body_badge"><img src="https://badges.gitter.im/nestjs/nestjs.svg" alt="Gitter" /></a>
<a href="https://opencollective.com/nest#backer"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
<a href="https://opencollective.com/nest#sponsor"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
<a href="https://paypal.me/kamilmysliwiec"><img src="https://img.shields.io/badge/Donate-PayPal-dc3d53.svg"/></a>
<a href="https://twitter.com/nestframework"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow"></a>
</p>
<!--[![Backers on Open Collective](https://opencollective.com/nest/backers/badge.svg)](https://opencollective.com/nest#backer)
[![Sponsors on Open Collective](https://opencollective.com/nest/sponsors/badge.svg)](https://opencollective.com/nest#sponsor)-->
## Description
[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
## Installation
```bash
$ npm install
```
## Running the app
```bash
# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod
```
## Test
```bash
# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:cov
```
## Support
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
## Stay in touch
- Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
- Website - [https://nestjs.com](https://nestjs.com/)
- Twitter - [@nestframework](https://twitter.com/nestframework)
## License
Nest is [MIT licensed](LICENSE).

31
docker-compose.yml Normal file
View file

@ -0,0 +1,31 @@
version: "3.4"
services:
db:
image: postgres:12.1
environment:
POSTGRES_PASSWORD: listory
POSTGRES_USER: listory
POSTGRES_DB: listory
networks:
- listory
service:
build:
context: .
target: builder
command: ["npm", "run", "start:dev"]
environment:
DB_USERNAME: listory
DB_PASSWORD: listory
DB_HOST: db
env_file: .env
volumes:
- ./src:/app/src
ports:
- 3000:3000
networks:
- listory
networks:
listory: {}

4
nest-cli.json Normal file
View file

@ -0,0 +1,4 @@
{
"collection": "@nestjs/schematics",
"sourceRoot": "src"
}

9622
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

75
package.json Normal file
View file

@ -0,0 +1,75 @@
{
"name": "listory",
"version": "0.0.1",
"description": "Track your Spotify Listen History",
"author": "Julian Tölle <julian.toelle97@gmail.com>",
"license": "MIT",
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"apps/**/*.ts\" \"libs/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "tslint -p tsconfig.json -c tslint.json",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./apps/listory/test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^6.7.2",
"@nestjs/config": "^0.1.0",
"@nestjs/core": "^6.7.2",
"@nestjs/passport": "^6.1.1",
"@nestjs/platform-express": "^6.7.2",
"@nestjs/typeorm": "^6.2.0",
"jwks-rsa": "^1.6.2",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0",
"pg": "^7.17.1",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.0",
"rxjs": "^6.5.3",
"typeorm": "^0.2.22"
},
"devDependencies": {
"@nestjs/cli": "^6.9.0",
"@nestjs/schematics": "^6.7.0",
"@nestjs/testing": "^6.7.1",
"@types/express": "^4.17.1",
"@types/jest": "^24.0.18",
"@types/node": "^12.7.5",
"@types/supertest": "^2.0.8",
"jest": "^24.9.0",
"prettier": "^1.18.2",
"supertest": "^4.0.2",
"ts-jest": "^24.1.0",
"ts-loader": "^6.1.1",
"ts-node": "^8.4.1",
"tsconfig-paths": "^3.9.0",
"tslint": "^5.20.0",
"tslint-config-prettier": "^1.18.0",
"tslint-plugin-prettier": "^2.1.0",
"typescript": "^3.6.3"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": ".",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"coverageDirectory": "./coverage",
"testEnvironment": "node",
"roots": [
"<rootDir>/apps/"
]
}
}

15
src/app.module.ts Normal file
View file

@ -0,0 +1,15 @@
import { Module } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";
import { AuthenticationModule } from "./authentication/authentication.module";
import { DatabaseModule } from "./database/database.module";
import { ConnectionsModule } from "./connections/connections.module";
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
DatabaseModule,
AuthenticationModule,
ConnectionsModule
]
})
export class AppModule {}

View file

@ -0,0 +1,10 @@
import { Module } from "@nestjs/common";
import { JwtStrategy } from "./jwt.strategy";
import { PassportModule } from "@nestjs/passport";
@Module({
imports: [PassportModule.register({ defaultStrategy: "jwt" })],
providers: [JwtStrategy],
exports: [PassportModule]
})
export class AuthenticationModule {}

View file

@ -0,0 +1,30 @@
import { Injectable } from "@nestjs/common";
import { PassportStrategy } from "@nestjs/passport";
import { passportJwtSecret } from "jwks-rsa";
import { ExtractJwt, Strategy } from "passport-jwt";
import { ConfigService } from "@nestjs/config";
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(private readonly config: ConfigService) {
super({
secretOrKeyProvider: passportJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: `${config.get<string>(
"JWT_AUTH0_DOMAIN"
)}.well-known/jwks.json`
}),
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
audience: config.get<string>("JWT_AUTH0_AUDIENCE"),
issuer: config.get<string>("JWT_AUTH0_DOMAIN"),
algorithms: ["RS256"]
});
}
validate(payload: any) {
return payload;
}
}

View file

@ -0,0 +1,3 @@
export enum ConnectionType {
SPOTIFY = "spotify"
}

View file

@ -0,0 +1,14 @@
import { Entity, Column, PrimaryGeneratedColumn } from "typeorm";
import { ConnectionType } from "./connection-type.enum";
@Entity()
export class Connection {
@PrimaryGeneratedColumn("uuid")
id: string;
@Column()
userID: string;
@Column()
type: ConnectionType;
}

View file

@ -0,0 +1,18 @@
import { Test, TestingModule } from "@nestjs/testing";
import { ConnectionsController } from "./connections.controller";
describe("Connections Controller", () => {
let controller: ConnectionsController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [ConnectionsController]
}).compile();
controller = module.get<ConnectionsController>(ConnectionsController);
});
it("should be defined", () => {
expect(controller).toBeDefined();
});
});

View file

@ -0,0 +1,4 @@
import { Controller } from "@nestjs/common";
@Controller("connections")
export class ConnectionsController {}

View file

@ -0,0 +1,12 @@
import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { ConnectionsController } from "./connections.controller";
import { ConnectionsRepository } from "./connections.repository";
import { ConnectionsService } from "./connections.service";
@Module({
imports: [TypeOrmModule.forFeature([ConnectionsRepository])],
controllers: [ConnectionsController],
providers: [ConnectionsService]
})
export class ConnectionsModule {}

View file

@ -0,0 +1,5 @@
import { EntityRepository, Repository } from "typeorm";
import { Connection } from "./connection.entity";
@EntityRepository(Connection)
export class ConnectionsRepository extends Repository<Connection> {}

View file

@ -0,0 +1,18 @@
import { Test, TestingModule } from "@nestjs/testing";
import { ConnectionsService } from "./connections.service";
describe("ConnectionsService", () => {
let service: ConnectionsService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ConnectionsService]
}).compile();
service = module.get<ConnectionsService>(ConnectionsService);
});
it("should be defined", () => {
expect(service).toBeDefined();
});
});

View file

@ -0,0 +1,7 @@
import { Injectable } from "@nestjs/common";
import { ConnectionsRepository } from "./connections.repository";
@Injectable()
export class ConnectionsService {
constructor(private readonly connectionRepository: ConnectionsRepository) {}
}

View file

@ -0,0 +1,25 @@
import { ConfigModule, ConfigService } from "@nestjs/config";
import { TypeOrmModule } from "@nestjs/typeorm";
import { join } from "path";
// tslint:disable-next-line variable-name
export const DatabaseModule = TypeOrmModule.forRootAsync({
useFactory: (config: ConfigService) => ({
type: "postgres",
// Connection details
host: config.get<string>("DB_HOST"),
username: config.get<string>("DB_USERNAME"),
password: config.get<string>("DB_PASSWORD"),
database: config.get<string>("DB_DATABASE"),
// Entities
entities: [join(__dirname, "..", "**/*.entity.{ts,js}")],
// Migrations
migrationsRun: true,
migrations: [join(__dirname, "migrations", "*.{ts,js}")],
synchronize: false
}),
inject: [ConfigService]
});

8
src/main.ts Normal file
View file

@ -0,0 +1,8 @@
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();

24
test/app.e2e-spec.ts Normal file
View file

@ -0,0 +1,24 @@
import { Test, TestingModule } from "@nestjs/testing";
import { INestApplication } from "@nestjs/common";
import * as request from "supertest";
import { AppModule } from "../src/app.module";
describe("AppController (e2e)", () => {
let app: INestApplication;
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule]
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
});
it("/ (GET)", () => {
return request(app.getHttpServer())
.get("/")
.expect(200)
.expect("Hello World!");
});
});

9
test/jest-e2e.json Normal file
View file

@ -0,0 +1,9 @@
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
}
}

4
tsconfig.build.json Normal file
View file

@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}

19
tsconfig.json Normal file
View file

@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"paths": {}
},
"exclude": [
"node_modules",
"dist"
]
}

23
tslint.json Normal file
View file

@ -0,0 +1,23 @@
{
"defaultSeverity": "error",
"extends": [
"tslint:recommended",
"tslint-config-prettier",
"tslint-plugin-prettier"
],
"jsRules": {
"no-unused-expression": true
},
"rules": {
"member-access": [false],
"ordered-imports": [false],
"max-line-length": [true, 150],
"member-ordering": [false],
"interface-name": [false],
"arrow-parens": false,
"object-literal-sort-keys": false,
"trailing-comma": false,
"prettier": "true"
},
"rulesDirectory": []
}