typescript-express-starter 6.1.0 → 7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.kr.md +122 -53
- package/README.md +127 -59
- package/bin/cli.js +4 -3
- package/lib/default/.swcrc +41 -0
- package/lib/default/Dockerfile +1 -1
- package/lib/default/ecosystem.config.js +40 -24
- package/lib/default/nodemon.json +2 -2
- package/lib/default/package.json +15 -8
- package/lib/default/src/app.ts +1 -2
- package/lib/default/src/http/auth.http +12 -15
- package/lib/default/src/http/users.http +12 -16
- package/lib/default/src/index.ts +1 -0
- package/lib/default/src/middlewares/auth.middleware.ts +3 -3
- package/lib/default/src/models/users.model.ts +5 -5
- package/lib/default/src/server.ts +1 -2
- package/lib/default/src/services/auth.service.ts +5 -5
- package/lib/default/src/services/users.service.ts +3 -3
- package/lib/default/src/tests/auth.test.ts +4 -4
- package/lib/default/src/tests/users.test.ts +4 -4
- package/lib/default/src/utils/logger.ts +5 -5
- package/lib/default/tsconfig.json +2 -2
- package/lib/graphql/.dockerignore +18 -0
- package/lib/graphql/.editorconfig +9 -0
- package/lib/graphql/.env +1 -0
- package/lib/graphql/.eslintignore +1 -0
- package/lib/graphql/.eslintrc +18 -0
- package/lib/graphql/.huskyrc +5 -0
- package/lib/graphql/.lintstagedrc.json +5 -0
- package/lib/graphql/.prettierrc +8 -0
- package/lib/graphql/.swcrc +39 -0
- package/lib/graphql/.vscode/launch.json +35 -0
- package/lib/graphql/.vscode/settings.json +6 -0
- package/lib/graphql/Dockerfile +24 -0
- package/lib/graphql/Makefile +6 -0
- package/lib/graphql/docker-compose.yml +50 -0
- package/lib/graphql/ecosystem.config.js +59 -0
- package/lib/graphql/jest.config.js +12 -0
- package/lib/graphql/nginx.conf +40 -0
- package/lib/graphql/nodemon.json +12 -0
- package/lib/graphql/package.json +79 -0
- package/lib/graphql/src/app.ts +105 -0
- package/lib/graphql/src/configs/development.json +19 -0
- package/lib/graphql/src/configs/production.json +19 -0
- package/lib/graphql/src/configs/test.json +19 -0
- package/lib/graphql/src/databases/index.ts +24 -0
- package/lib/graphql/src/dtos/users.dto.ts +14 -0
- package/lib/{typeorm/src/entity → graphql/src/entities}/users.entity.ts +3 -3
- package/lib/graphql/src/exceptions/HttpException.ts +10 -0
- package/lib/graphql/src/http/auth.http +49 -0
- package/lib/graphql/src/http/users.http +78 -0
- package/lib/graphql/src/index.ts +1 -0
- package/lib/graphql/src/interfaces/auth.interface.ts +14 -0
- package/lib/graphql/src/interfaces/db.interface.ts +7 -0
- package/lib/graphql/src/interfaces/users.interface.ts +5 -0
- package/lib/graphql/src/middlewares/auth.middleware.ts +32 -0
- package/lib/graphql/src/middlewares/error.middleware.ts +17 -0
- package/lib/graphql/src/repositories/auth.repository.ts +61 -0
- package/lib/graphql/src/repositories/users.repository.ts +60 -0
- package/lib/graphql/src/resolvers/auth.resolver.ts +32 -0
- package/lib/graphql/src/resolvers/users.resolver.ts +47 -0
- package/lib/graphql/src/server.ts +14 -0
- package/lib/graphql/src/tests/auth.test.ts +52 -0
- package/lib/graphql/src/tests/index.test.ts +18 -0
- package/lib/graphql/src/tests/users.test.ts +71 -0
- package/lib/graphql/src/typedefs/users.type.ts +13 -0
- package/lib/graphql/src/utils/logger.ts +75 -0
- package/lib/graphql/src/utils/util.ts +19 -0
- package/lib/graphql/src/utils/validateEnv.ts +10 -0
- package/lib/graphql/tsconfig.json +39 -0
- package/lib/knex/.swcrc +39 -0
- package/lib/knex/Dockerfile +1 -1
- package/lib/knex/ecosystem.config.js +57 -43
- package/lib/knex/knexfile.ts +6 -6
- package/lib/knex/nodemon.json +2 -2
- package/lib/knex/package.json +14 -8
- package/lib/knex/src/app.ts +2 -3
- package/lib/knex/src/databases/index.ts +6 -6
- package/lib/knex/src/http/auth.http +12 -15
- package/lib/knex/src/http/users.http +12 -16
- package/lib/knex/src/index.ts +1 -0
- package/lib/knex/src/middlewares/auth.middleware.ts +4 -4
- package/lib/knex/src/server.ts +1 -2
- package/lib/knex/src/services/auth.service.ts +5 -5
- package/lib/knex/src/services/users.service.ts +3 -3
- package/lib/knex/src/tests/auth.test.ts +2 -2
- package/lib/knex/src/tests/index.test.ts +1 -1
- package/lib/knex/src/tests/users.test.ts +2 -2
- package/lib/knex/src/utils/logger.ts +5 -5
- package/lib/knex/tsconfig.json +2 -2
- package/lib/mongoose/.swcrc +39 -0
- package/lib/mongoose/Dockerfile +1 -1
- package/lib/mongoose/docker-compose.yml +0 -4
- package/lib/mongoose/ecosystem.config.js +42 -27
- package/lib/mongoose/nodemon.json +2 -2
- package/lib/mongoose/package.json +15 -8
- package/lib/mongoose/src/app.ts +2 -3
- package/lib/mongoose/src/configs/development.json +1 -1
- package/lib/mongoose/src/configs/production.json +1 -1
- package/lib/mongoose/src/configs/test.json +1 -1
- package/lib/mongoose/src/http/auth.http +32 -0
- package/lib/mongoose/src/http/users.http +34 -0
- package/lib/mongoose/src/index.ts +1 -0
- package/lib/mongoose/src/middlewares/auth.middleware.ts +3 -3
- package/lib/mongoose/src/server.ts +1 -2
- package/lib/mongoose/src/services/auth.service.ts +5 -5
- package/lib/mongoose/src/services/users.service.ts +3 -3
- package/lib/mongoose/src/utils/logger.ts +5 -5
- package/lib/mongoose/tsconfig.json +2 -2
- package/lib/prisma/.env +1 -1
- package/lib/prisma/.swcrc +37 -0
- package/lib/prisma/Dockerfile +1 -1
- package/lib/prisma/docker-compose.yml +1 -6
- package/lib/prisma/ecosystem.config.js +40 -24
- package/lib/prisma/nodemon.json +2 -2
- package/lib/prisma/package.json +17 -10
- package/lib/prisma/src/app.ts +2 -3
- package/lib/prisma/src/http/auth.http +12 -15
- package/lib/prisma/src/http/users.http +12 -16
- package/lib/prisma/src/index.ts +1 -0
- package/lib/prisma/src/middlewares/auth.middleware.ts +4 -4
- package/lib/prisma/src/server.ts +1 -2
- package/lib/prisma/src/services/auth.service.ts +5 -5
- package/lib/prisma/src/services/users.service.ts +3 -3
- package/lib/prisma/src/utils/logger.ts +5 -5
- package/lib/prisma/tsconfig.json +2 -2
- package/lib/routing-controllers/.swcrc +37 -0
- package/lib/routing-controllers/Dockerfile +1 -1
- package/lib/routing-controllers/ecosystem.config.js +40 -24
- package/lib/routing-controllers/nodemon.json +2 -2
- package/lib/routing-controllers/package.json +15 -9
- package/lib/routing-controllers/src/app.ts +3 -4
- package/lib/routing-controllers/src/http/auth.http +12 -15
- package/lib/routing-controllers/src/http/users.http +12 -16
- package/lib/routing-controllers/src/index.ts +1 -0
- package/lib/routing-controllers/src/middlewares/auth.middleware.ts +3 -3
- package/lib/routing-controllers/src/middlewares/validation.middleware.ts +10 -2
- package/lib/routing-controllers/src/models/users.model.ts +5 -5
- package/lib/routing-controllers/src/server.ts +1 -3
- package/lib/routing-controllers/src/services/auth.service.ts +5 -5
- package/lib/routing-controllers/src/services/users.service.ts +3 -3
- package/lib/routing-controllers/src/utils/logger.ts +5 -5
- package/lib/routing-controllers/tsconfig.json +2 -2
- package/lib/sequelize/.swcrc +39 -0
- package/lib/sequelize/Dockerfile +1 -1
- package/lib/sequelize/docker-compose.yml +1 -6
- package/lib/sequelize/ecosystem.config.js +42 -27
- package/lib/sequelize/nodemon.json +2 -2
- package/lib/sequelize/package.json +15 -8
- package/lib/sequelize/src/app.ts +1 -2
- package/lib/sequelize/src/configs/development.json +2 -2
- package/lib/sequelize/src/configs/production.json +1 -1
- package/lib/sequelize/src/configs/test.json +1 -1
- package/lib/sequelize/src/http/auth.http +32 -0
- package/lib/sequelize/src/http/users.http +34 -0
- package/lib/sequelize/src/index.ts +1 -0
- package/lib/sequelize/src/middlewares/auth.middleware.ts +4 -4
- package/lib/sequelize/src/server.ts +1 -2
- package/lib/sequelize/src/services/auth.service.ts +5 -5
- package/lib/sequelize/src/services/users.service.ts +3 -3
- package/lib/sequelize/src/utils/logger.ts +5 -5
- package/lib/sequelize/tsconfig.json +2 -2
- package/lib/starter.js +5 -9
- package/lib/typeorm/.swcrc +39 -0
- package/lib/typeorm/Dockerfile +1 -1
- package/lib/typeorm/docker-compose.yml +1 -1
- package/lib/typeorm/ecosystem.config.js +42 -27
- package/lib/typeorm/nodemon.json +2 -2
- package/lib/typeorm/package.json +15 -8
- package/lib/typeorm/src/app.ts +1 -2
- package/lib/typeorm/src/configs/development.json +1 -1
- package/lib/typeorm/src/configs/production.json +1 -1
- package/lib/typeorm/src/configs/test.json +1 -1
- package/lib/typeorm/src/databases/index.ts +5 -5
- package/lib/typeorm/src/entities/users.entity.ts +26 -0
- package/lib/typeorm/src/http/auth.http +12 -15
- package/lib/typeorm/src/http/users.http +12 -16
- package/lib/typeorm/src/index.ts +1 -0
- package/lib/typeorm/src/middlewares/auth.middleware.ts +5 -9
- package/lib/typeorm/src/server.ts +1 -2
- package/lib/typeorm/src/services/auth.service.ts +13 -17
- package/lib/typeorm/src/services/users.service.ts +16 -22
- package/lib/typeorm/src/utils/logger.ts +5 -5
- package/lib/typeorm/tsconfig.json +3 -3
- package/package.json +13 -4
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "0.2.0",
|
|
3
|
+
"configurations": [
|
|
4
|
+
{
|
|
5
|
+
"type": "node-terminal",
|
|
6
|
+
"request": "launch",
|
|
7
|
+
"name": "Dev typescript-express-starter",
|
|
8
|
+
"command": "npm run dev"
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
"type": "node-terminal",
|
|
12
|
+
"request": "launch",
|
|
13
|
+
"name": "Start typescript-express-starter",
|
|
14
|
+
"command": "npm run start"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"type": "node-terminal",
|
|
18
|
+
"request": "launch",
|
|
19
|
+
"name": "Test typescript-express-starter",
|
|
20
|
+
"command": "npm run test"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"type": "node-terminal",
|
|
24
|
+
"request": "launch",
|
|
25
|
+
"name": "Lint typescript-express-starter",
|
|
26
|
+
"command": "npm run lint"
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
"type": "node-terminal",
|
|
30
|
+
"request": "launch",
|
|
31
|
+
"name": "Lint:Fix typescript-express-starter",
|
|
32
|
+
"command": "npm run lint:fix"
|
|
33
|
+
}
|
|
34
|
+
]
|
|
35
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Common build stage
|
|
2
|
+
FROM node:14.14.0-alpine3.12 as common-build-stage
|
|
3
|
+
|
|
4
|
+
COPY . ./app
|
|
5
|
+
|
|
6
|
+
WORKDIR /app
|
|
7
|
+
|
|
8
|
+
RUN npm install
|
|
9
|
+
|
|
10
|
+
EXPOSE 3000
|
|
11
|
+
|
|
12
|
+
# Development build stage
|
|
13
|
+
FROM common-build-stage as development-build-stage
|
|
14
|
+
|
|
15
|
+
ENV NODE_ENV development
|
|
16
|
+
|
|
17
|
+
CMD ["npm", "run", "dev"]
|
|
18
|
+
|
|
19
|
+
# Production build stage
|
|
20
|
+
FROM common-build-stage as production-build-stage
|
|
21
|
+
|
|
22
|
+
ENV NODE_ENV production
|
|
23
|
+
|
|
24
|
+
CMD ["npm", "run", "start"]
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
version: '3.7'
|
|
2
|
+
|
|
3
|
+
services:
|
|
4
|
+
proxy:
|
|
5
|
+
image: nginx:alpine
|
|
6
|
+
container_name: proxy
|
|
7
|
+
ports:
|
|
8
|
+
- '80:80'
|
|
9
|
+
volumes:
|
|
10
|
+
- ./nginx.conf:/etc/nginx/nginx.conf
|
|
11
|
+
restart: 'unless-stopped'
|
|
12
|
+
networks:
|
|
13
|
+
- backend
|
|
14
|
+
server:
|
|
15
|
+
build:
|
|
16
|
+
context: ./
|
|
17
|
+
target: development-build-stage
|
|
18
|
+
dockerfile: Dockerfile
|
|
19
|
+
container_name: server
|
|
20
|
+
ports:
|
|
21
|
+
- '3000:3000'
|
|
22
|
+
volumes:
|
|
23
|
+
- ./:/app
|
|
24
|
+
- /app/node_modules
|
|
25
|
+
restart: 'unless-stopped'
|
|
26
|
+
networks:
|
|
27
|
+
- backend
|
|
28
|
+
links:
|
|
29
|
+
- pg
|
|
30
|
+
depends_on:
|
|
31
|
+
- pg
|
|
32
|
+
pg:
|
|
33
|
+
image: bitnami/postgresql:11-debian-9
|
|
34
|
+
container_name: pg
|
|
35
|
+
environment:
|
|
36
|
+
POSTGRESQL_USERNAME: root
|
|
37
|
+
POSTGRESQL_PASSWORD: password
|
|
38
|
+
POSTGRESQL_DATABASE: test
|
|
39
|
+
ports:
|
|
40
|
+
- '5432:5432'
|
|
41
|
+
networks:
|
|
42
|
+
- backend
|
|
43
|
+
|
|
44
|
+
networks:
|
|
45
|
+
backend:
|
|
46
|
+
driver: bridge
|
|
47
|
+
|
|
48
|
+
volumes:
|
|
49
|
+
data:
|
|
50
|
+
driver: local
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description pm2 configuration file.
|
|
3
|
+
* @example
|
|
4
|
+
* production mode :: pm2 start ecosystem.config.js --only prod
|
|
5
|
+
* development mode :: pm2 start ecosystem.config.js --only dev
|
|
6
|
+
*/
|
|
7
|
+
module.exports = {
|
|
8
|
+
apps: [
|
|
9
|
+
{
|
|
10
|
+
name: 'prod', // pm2 start App name
|
|
11
|
+
script: 'dist/server.js',
|
|
12
|
+
exec_mode: 'cluster', // 'cluster' or 'fork'
|
|
13
|
+
instance_var: 'INSTANCE_ID', // instance variable
|
|
14
|
+
instances: 2, // pm2 instance count
|
|
15
|
+
autorestart: true, // auto restart if process crash
|
|
16
|
+
watch: false, // files change automatic restart
|
|
17
|
+
ignore_watch: ['node_modules', 'logs'], // ignore files change
|
|
18
|
+
max_memory_restart: '1G', // restart if process use more than 1G memory
|
|
19
|
+
merge_logs: true, // if true, stdout and stderr will be merged and sent to pm2 log
|
|
20
|
+
output: './logs/access.log', // pm2 log file
|
|
21
|
+
error: './logs/error.log', // pm2 error log file
|
|
22
|
+
env: {
|
|
23
|
+
// environment variable
|
|
24
|
+
PORT: 3000,
|
|
25
|
+
NODE_ENV: 'production',
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
name: 'dev', // pm2 start App name
|
|
30
|
+
script: 'ts-node', // ts-node
|
|
31
|
+
args: '-r tsconfig-paths/register --transpile-only src/server.ts', // ts-node args
|
|
32
|
+
exec_mode: 'cluster', // 'cluster' or 'fork'
|
|
33
|
+
instance_var: 'INSTANCE_ID', // instance variable
|
|
34
|
+
instances: 2, // pm2 instance count
|
|
35
|
+
autorestart: true, // auto restart if process crash
|
|
36
|
+
watch: false, // files change automatic restart
|
|
37
|
+
ignore_watch: ['node_modules', 'logs'], // ignore files change
|
|
38
|
+
max_memory_restart: '1G', // restart if process use more than 1G memory
|
|
39
|
+
merge_logs: true, // if true, stdout and stderr will be merged and sent to pm2 log
|
|
40
|
+
output: './logs/access.log', // pm2 log file
|
|
41
|
+
error: './logs/error.log', // pm2 error log file
|
|
42
|
+
env: {
|
|
43
|
+
// environment variable
|
|
44
|
+
PORT: 3000,
|
|
45
|
+
NODE_ENV: 'development',
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
deploy: {
|
|
50
|
+
production: {
|
|
51
|
+
user: 'user',
|
|
52
|
+
host: '0.0.0.0',
|
|
53
|
+
ref: 'origin/master',
|
|
54
|
+
repo: 'git@github.com:repo.git',
|
|
55
|
+
path: 'dist/server.js',
|
|
56
|
+
'post-deploy': 'npm install && npm run build && pm2 reload ecosystem.config.js --only prod',
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const { pathsToModuleNameMapper } = require('ts-jest/utils');
|
|
2
|
+
const { compilerOptions } = require('./tsconfig.json');
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
preset: 'ts-jest',
|
|
6
|
+
testEnvironment: 'node',
|
|
7
|
+
roots: ['<rootDir>/src'],
|
|
8
|
+
transform: {
|
|
9
|
+
'^.+\\.tsx?$': 'ts-jest',
|
|
10
|
+
},
|
|
11
|
+
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/src' }),
|
|
12
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
user nginx;
|
|
2
|
+
worker_processes 1;
|
|
3
|
+
|
|
4
|
+
error_log /var/log/nginx/error.log warn;
|
|
5
|
+
pid /var/run/nginx.pid;
|
|
6
|
+
|
|
7
|
+
events {
|
|
8
|
+
worker_connections 1024;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
http {
|
|
12
|
+
include /etc/nginx/mime.types;
|
|
13
|
+
default_type application/octet-stream;
|
|
14
|
+
|
|
15
|
+
upstream api-server {
|
|
16
|
+
server server:3000;
|
|
17
|
+
keepalive 100;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
server {
|
|
21
|
+
listen 80;
|
|
22
|
+
server_name localhost;
|
|
23
|
+
|
|
24
|
+
location / {
|
|
25
|
+
proxy_http_version 1.1;
|
|
26
|
+
proxy_pass http://api-server;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
|
32
|
+
'$status $body_bytes_sent "$http_referer" '
|
|
33
|
+
'"$http_user_agent" "$http_x_forwarded_for"';
|
|
34
|
+
|
|
35
|
+
access_log /var/log/nginx/access.log main;
|
|
36
|
+
|
|
37
|
+
sendfile on;
|
|
38
|
+
keepalive_timeout 65;
|
|
39
|
+
include /etc/nginx/conf.d/*.conf;
|
|
40
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "graphql",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "TypeScript + GraphQL + TypeORM + Express API Server",
|
|
5
|
+
"author": "",
|
|
6
|
+
"license": "ISC",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"start": "npm run build && cross-env NODE_ENV=production node dist/server.js",
|
|
9
|
+
"dev": "cross-env NODE_ENV=development nodemon",
|
|
10
|
+
"build": "swc src -d dist --source-maps --copy-files",
|
|
11
|
+
"build:tsc": "tsc && tsc-alias",
|
|
12
|
+
"test": "jest --forceExit --detectOpenHandles",
|
|
13
|
+
"lint": "eslint --ignore-path .gitignore --ext .ts src/",
|
|
14
|
+
"lint:fix": "npm run lint -- --fix",
|
|
15
|
+
"deploy:prod": "npm run build && pm2 start ecosystem.config.js --only prod",
|
|
16
|
+
"deploy:dev": "pm2 start ecosystem.config.js --only dev"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"apollo-server-core": "^3.5.0",
|
|
20
|
+
"apollo-server-express": "^3.5.0",
|
|
21
|
+
"bcrypt": "^5.0.1",
|
|
22
|
+
"class-validator": "^0.13.1",
|
|
23
|
+
"compression": "^1.7.4",
|
|
24
|
+
"config": "^3.3.6",
|
|
25
|
+
"cookie-parser": "^1.4.5",
|
|
26
|
+
"cors": "^2.8.5",
|
|
27
|
+
"dotenv": "^10.0.0",
|
|
28
|
+
"envalid": "^7.1.0",
|
|
29
|
+
"express": "^4.17.1",
|
|
30
|
+
"graphql": "^15.8.0",
|
|
31
|
+
"helmet": "^4.6.0",
|
|
32
|
+
"hpp": "^0.2.3",
|
|
33
|
+
"jsonwebtoken": "^8.5.1",
|
|
34
|
+
"pg": "^8.7.1",
|
|
35
|
+
"reflect-metadata": "^0.1.13",
|
|
36
|
+
"type-graphql": "^1.1.1",
|
|
37
|
+
"typeorm": "^0.2.34",
|
|
38
|
+
"winston": "^3.3.3",
|
|
39
|
+
"winston-daily-rotate-file": "^4.5.5"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@swc/cli": "^0.1.51",
|
|
43
|
+
"@swc/core": "^1.2.108",
|
|
44
|
+
"@types/bcrypt": "^5.0.0",
|
|
45
|
+
"@types/compression": "^1.7.1",
|
|
46
|
+
"@types/config": "^0.0.39",
|
|
47
|
+
"@types/cookie-parser": "^1.4.2",
|
|
48
|
+
"@types/cors": "^2.8.11",
|
|
49
|
+
"@types/dotenv": "^8.2.0",
|
|
50
|
+
"@types/express": "^4.17.13",
|
|
51
|
+
"@types/helmet": "^4.0.0",
|
|
52
|
+
"@types/hpp": "^0.2.1",
|
|
53
|
+
"@types/jest": "^26.0.24",
|
|
54
|
+
"@types/jsonwebtoken": "^8.5.4",
|
|
55
|
+
"@types/node": "^16.0.1",
|
|
56
|
+
"@types/supertest": "^2.0.11",
|
|
57
|
+
"@types/winston": "^2.4.4",
|
|
58
|
+
"@typescript-eslint/eslint-plugin": "^4.28.2",
|
|
59
|
+
"@typescript-eslint/parser": "^4.28.2",
|
|
60
|
+
"cross-env": "^7.0.3",
|
|
61
|
+
"eslint": "^7.30.0",
|
|
62
|
+
"eslint-config-prettier": "^8.3.0",
|
|
63
|
+
"eslint-plugin-prettier": "^3.4.0",
|
|
64
|
+
"husky": "^7.0.1",
|
|
65
|
+
"jest": "^27.0.6",
|
|
66
|
+
"lint-staged": "^11.0.0",
|
|
67
|
+
"node-config": "^0.0.2",
|
|
68
|
+
"node-gyp": "^8.1.0",
|
|
69
|
+
"nodemon": "^2.0.9",
|
|
70
|
+
"pm2": "^5.1.0",
|
|
71
|
+
"prettier": "^2.3.2",
|
|
72
|
+
"supertest": "^6.1.3",
|
|
73
|
+
"ts-jest": "^27.0.7",
|
|
74
|
+
"ts-node": "^10.4.0",
|
|
75
|
+
"tsc-alias": "^1.4.1",
|
|
76
|
+
"tsconfig-paths": "^3.10.1",
|
|
77
|
+
"typescript": "^4.5.2"
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import '@/index';
|
|
3
|
+
import { ApolloServerPluginLandingPageProductionDefault, ApolloServerPluginLandingPageLocalDefault } from 'apollo-server-core';
|
|
4
|
+
import { ApolloServer } from 'apollo-server-express';
|
|
5
|
+
import compression from 'compression';
|
|
6
|
+
import cookieParser from 'cookie-parser';
|
|
7
|
+
import cors from 'cors';
|
|
8
|
+
import config from 'config';
|
|
9
|
+
import express from 'express';
|
|
10
|
+
import helmet from 'helmet';
|
|
11
|
+
import hpp from 'hpp';
|
|
12
|
+
import { buildSchema } from 'type-graphql';
|
|
13
|
+
import { createConnection } from 'typeorm';
|
|
14
|
+
import { dbConnection } from '@databases';
|
|
15
|
+
import { authMiddleware, authChecker } from '@middlewares/auth.middleware';
|
|
16
|
+
import errorMiddleware from '@middlewares/error.middleware';
|
|
17
|
+
import { logger, responseLogger, errorLogger } from '@utils/logger';
|
|
18
|
+
|
|
19
|
+
class App {
|
|
20
|
+
public app: express.Application;
|
|
21
|
+
public port: string | number;
|
|
22
|
+
public env: string;
|
|
23
|
+
|
|
24
|
+
constructor(resolvers) {
|
|
25
|
+
this.app = express();
|
|
26
|
+
this.port = process.env.PORT || 3000;
|
|
27
|
+
this.env = process.env.NODE_ENV || 'development';
|
|
28
|
+
|
|
29
|
+
this.connectToDatabase();
|
|
30
|
+
this.initializeMiddlewares();
|
|
31
|
+
this.initApolloServer(resolvers);
|
|
32
|
+
this.initializeErrorHandling();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public async listen() {
|
|
36
|
+
this.app.listen(this.port, () => {
|
|
37
|
+
logger.info(`=================================`);
|
|
38
|
+
logger.info(`======= ENV: ${this.env} =======`);
|
|
39
|
+
logger.info(`🚀 App listening on the port ${this.port}`);
|
|
40
|
+
logger.info(`🎮 http://localhost:${this.port}/graphql`);
|
|
41
|
+
logger.info(`=================================`);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public getServer() {
|
|
46
|
+
return this.app;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
private connectToDatabase() {
|
|
50
|
+
createConnection(dbConnection);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private initializeMiddlewares() {
|
|
54
|
+
this.app.use(cors({ origin: config.get('cors.origin'), credentials: config.get('cors.credentials') }));
|
|
55
|
+
this.app.use(hpp());
|
|
56
|
+
this.app.use(helmet());
|
|
57
|
+
this.app.use(compression());
|
|
58
|
+
this.app.use(express.json());
|
|
59
|
+
this.app.use(express.urlencoded({ extended: true }));
|
|
60
|
+
this.app.use(cookieParser());
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private async initApolloServer(resolvers) {
|
|
64
|
+
const schema = await buildSchema({
|
|
65
|
+
resolvers: resolvers,
|
|
66
|
+
authChecker: authChecker,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const apolloServer = new ApolloServer({
|
|
70
|
+
schema: schema,
|
|
71
|
+
plugins: [
|
|
72
|
+
process.env.NODE_ENV === 'production'
|
|
73
|
+
? ApolloServerPluginLandingPageProductionDefault({ footer: false })
|
|
74
|
+
: ApolloServerPluginLandingPageLocalDefault({ footer: false }),
|
|
75
|
+
],
|
|
76
|
+
context: async ({ req }) => {
|
|
77
|
+
try {
|
|
78
|
+
const user = await authMiddleware(req);
|
|
79
|
+
return { user };
|
|
80
|
+
} catch (error) {
|
|
81
|
+
throw new Error(error);
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
formatResponse: (response, request) => {
|
|
85
|
+
responseLogger(request);
|
|
86
|
+
|
|
87
|
+
return response;
|
|
88
|
+
},
|
|
89
|
+
formatError: error => {
|
|
90
|
+
errorLogger(error);
|
|
91
|
+
|
|
92
|
+
return error;
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
await apolloServer.start();
|
|
97
|
+
apolloServer.applyMiddleware({ app: this.app, cors: true, path: '/graphql' });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private initializeErrorHandling() {
|
|
101
|
+
this.app.use(errorMiddleware);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export default App;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"env": "development",
|
|
3
|
+
"dbConfig": {
|
|
4
|
+
"host": "localhost",
|
|
5
|
+
"user": "root",
|
|
6
|
+
"port": 5432,
|
|
7
|
+
"password": "password",
|
|
8
|
+
"database": "test"
|
|
9
|
+
},
|
|
10
|
+
"secretKey": "secretKey",
|
|
11
|
+
"log": {
|
|
12
|
+
"format": "dev",
|
|
13
|
+
"dir": "../logs"
|
|
14
|
+
},
|
|
15
|
+
"cors": {
|
|
16
|
+
"origin": true,
|
|
17
|
+
"credentials": true
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"env": "production",
|
|
3
|
+
"dbConfig": {
|
|
4
|
+
"host": "localhost",
|
|
5
|
+
"user": "root",
|
|
6
|
+
"port": 5432,
|
|
7
|
+
"password": "password",
|
|
8
|
+
"database": "test"
|
|
9
|
+
},
|
|
10
|
+
"secretKey": "secretKey",
|
|
11
|
+
"log": {
|
|
12
|
+
"format": "combined",
|
|
13
|
+
"dir": "../logs"
|
|
14
|
+
},
|
|
15
|
+
"cors": {
|
|
16
|
+
"origin": "your.domain.com",
|
|
17
|
+
"credentials": true
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"env": "test",
|
|
3
|
+
"dbConfig": {
|
|
4
|
+
"host": "localhost",
|
|
5
|
+
"user": "root",
|
|
6
|
+
"port": 5432,
|
|
7
|
+
"password": "password",
|
|
8
|
+
"database": "test"
|
|
9
|
+
},
|
|
10
|
+
"secretKey": "secretKey",
|
|
11
|
+
"log": {
|
|
12
|
+
"format": "dev",
|
|
13
|
+
"dir": "../logs"
|
|
14
|
+
},
|
|
15
|
+
"cors": {
|
|
16
|
+
"origin": true,
|
|
17
|
+
"credentials": true
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import config from 'config';
|
|
2
|
+
import { join } from 'path';
|
|
3
|
+
import { ConnectionOptions } from 'typeorm';
|
|
4
|
+
import { dbConfig } from '@interfaces/db.interface';
|
|
5
|
+
|
|
6
|
+
const { host, port, user, password, database }: dbConfig = config.get('dbConfig');
|
|
7
|
+
export const dbConnection: ConnectionOptions = {
|
|
8
|
+
type: 'postgres',
|
|
9
|
+
host: host,
|
|
10
|
+
port: port,
|
|
11
|
+
username: user,
|
|
12
|
+
password: password,
|
|
13
|
+
database: database,
|
|
14
|
+
synchronize: true,
|
|
15
|
+
logging: false,
|
|
16
|
+
entities: [join(__dirname, '../**/*.entity{.ts,.js}')],
|
|
17
|
+
migrations: [join(__dirname, '../**/*.migration{.ts,.js}')],
|
|
18
|
+
subscribers: [join(__dirname, '../**/*.subscriber{.ts,.js}')],
|
|
19
|
+
cli: {
|
|
20
|
+
entitiesDir: 'src/entities',
|
|
21
|
+
migrationsDir: 'src/migration',
|
|
22
|
+
subscribersDir: 'src/subscriber',
|
|
23
|
+
},
|
|
24
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { IsEmail, IsString } from 'class-validator';
|
|
2
|
+
import { InputType, Field } from 'type-graphql';
|
|
3
|
+
import { User } from '@typedefs/users.type';
|
|
4
|
+
|
|
5
|
+
@InputType()
|
|
6
|
+
export class CreateUserDto implements Partial<User> {
|
|
7
|
+
@Field()
|
|
8
|
+
@IsEmail()
|
|
9
|
+
email: string;
|
|
10
|
+
|
|
11
|
+
@Field()
|
|
12
|
+
@IsString()
|
|
13
|
+
password: string;
|
|
14
|
+
}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { IsNotEmpty } from 'class-validator';
|
|
2
|
-
import { Entity, PrimaryGeneratedColumn, Column, Unique, CreateDateColumn, UpdateDateColumn } from 'typeorm';
|
|
2
|
+
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, Unique, CreateDateColumn, UpdateDateColumn } from 'typeorm';
|
|
3
3
|
import { User } from '@interfaces/users.interface';
|
|
4
4
|
|
|
5
5
|
@Entity()
|
|
6
|
-
|
|
7
|
-
export class UserEntity implements User {
|
|
6
|
+
export class UserEntity extends BaseEntity implements User {
|
|
8
7
|
@PrimaryGeneratedColumn()
|
|
9
8
|
id: number;
|
|
10
9
|
|
|
11
10
|
@Column()
|
|
12
11
|
@IsNotEmpty()
|
|
12
|
+
@Unique()
|
|
13
13
|
email: string;
|
|
14
14
|
|
|
15
15
|
@Column()
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# baseURL
|
|
2
|
+
@baseURL = http://localhost:3000/graphql
|
|
3
|
+
|
|
4
|
+
###
|
|
5
|
+
# User Signup
|
|
6
|
+
POST {{ baseURL }}
|
|
7
|
+
Content-Type: application/json
|
|
8
|
+
X-REQUEST-TYPE: GraphQL
|
|
9
|
+
|
|
10
|
+
mutation {
|
|
11
|
+
signup (userData: {
|
|
12
|
+
email: "example@email.com",
|
|
13
|
+
password: "password"
|
|
14
|
+
}) {
|
|
15
|
+
id,
|
|
16
|
+
email,
|
|
17
|
+
password
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
###
|
|
22
|
+
# User Login
|
|
23
|
+
POST {{ baseURL }}
|
|
24
|
+
Content-Type: application/json
|
|
25
|
+
X-REQUEST-TYPE: GraphQL
|
|
26
|
+
|
|
27
|
+
mutation {
|
|
28
|
+
login (userData: {
|
|
29
|
+
email: "example@email.com",
|
|
30
|
+
password: "password"
|
|
31
|
+
}) {
|
|
32
|
+
email,
|
|
33
|
+
password
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
###
|
|
38
|
+
# User Logout
|
|
39
|
+
POST {{ baseURL }}
|
|
40
|
+
Content-Type: application/json
|
|
41
|
+
X-REQUEST-TYPE: GraphQL
|
|
42
|
+
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwiaWF0IjoxNjM5MDQ3MjM3LCJleHAiOjE2MzkwNTA4Mzd9.HD0AvZl1s-mycKxo0IJ1QT-oSKhjeArXrlNBffZUanY;
|
|
43
|
+
|
|
44
|
+
mutation {
|
|
45
|
+
logout {
|
|
46
|
+
email,
|
|
47
|
+
password
|
|
48
|
+
}
|
|
49
|
+
}
|