typescript-express-starter 9.2.0 → 10.0.1
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/CONTRIBUTORS.md +69 -0
- package/README.kr.md +122 -167
- package/README.md +118 -163
- package/bin/cli.js +1 -1
- package/lib/default/.swcrc +0 -3
- package/lib/default/Dockerfile.dev +18 -0
- package/lib/default/Dockerfile.prod +18 -0
- package/lib/default/Makefile +29 -16
- package/lib/default/docker-compose.yml +8 -8
- package/lib/default/package.json +3 -1
- package/lib/default/src/app.ts +4 -5
- package/lib/default/src/controllers/auth.controller.ts +9 -11
- package/lib/default/src/controllers/users.controller.ts +11 -13
- package/lib/default/src/dtos/users.dto.ts +12 -1
- package/lib/default/src/interfaces/users.interface.ts +2 -2
- package/lib/default/src/middlewares/auth.middleware.ts +16 -10
- package/lib/default/src/middlewares/error.middleware.ts +2 -4
- package/lib/default/src/middlewares/validation.middleware.ts +22 -20
- package/lib/default/src/models/users.model.ts +6 -8
- package/lib/default/src/routes/auth.route.ts +8 -11
- package/lib/default/src/routes/users.route.ts +10 -12
- package/lib/default/src/server.ts +6 -7
- package/lib/default/src/services/auth.service.ts +22 -33
- package/lib/default/src/services/users.service.ts +16 -25
- package/lib/{graphql/src/tests → default/src/test}/auth.test.ts +21 -21
- package/lib/default/src/test/users.test.ts +62 -0
- package/lib/default/src/utils/validateEnv.ts +1 -3
- package/lib/default/tsconfig.json +1 -1
- package/lib/graphql/.env.development.local +6 -6
- package/lib/graphql/.env.production.local +6 -6
- package/lib/graphql/.env.test.local +6 -6
- package/lib/graphql/.swcrc +1 -1
- package/lib/graphql/Dockerfile.dev +19 -0
- package/lib/graphql/Dockerfile.prod +19 -0
- package/lib/graphql/Makefile +32 -15
- package/lib/graphql/docker-compose.yml +23 -15
- package/lib/graphql/package.json +2 -1
- package/lib/graphql/src/app.ts +16 -16
- package/lib/graphql/src/config/index.ts +2 -2
- package/lib/graphql/src/database/index.ts +26 -0
- package/lib/graphql/src/dtos/users.dto.ts +14 -1
- package/lib/graphql/src/interfaces/users.interface.ts +2 -2
- package/lib/graphql/src/middlewares/auth.middleware.ts +16 -6
- package/lib/graphql/src/middlewares/error.middleware.ts +2 -4
- package/lib/graphql/src/repositories/auth.repository.ts +16 -24
- package/lib/graphql/src/repositories/users.repository.ts +5 -14
- package/lib/graphql/src/resolvers/auth.resolver.ts +2 -2
- package/lib/graphql/src/resolvers/users.resolver.ts +4 -4
- package/lib/graphql/src/server.ts +6 -7
- package/lib/graphql/src/test/auth.test.ts +10 -0
- package/lib/graphql/src/test/users.test.ts +11 -0
- package/lib/graphql/src/typedefs/users.type.ts +2 -2
- package/lib/graphql/src/utils/logger.ts +2 -0
- package/lib/graphql/src/utils/validateEnv.ts +1 -3
- package/lib/graphql/tsconfig.json +2 -2
- package/lib/knex/.env.development.local +2 -2
- package/lib/knex/.env.production.local +3 -3
- package/lib/knex/.env.test.local +3 -3
- package/lib/knex/.swcrc +1 -1
- package/lib/knex/Dockerfile.dev +19 -0
- package/lib/knex/Dockerfile.prod +19 -0
- package/lib/knex/Makefile +32 -15
- package/lib/knex/docker-compose.yml +19 -14
- package/lib/knex/knexfile.ts +7 -9
- package/lib/knex/package.json +2 -0
- package/lib/knex/src/app.ts +7 -9
- package/lib/knex/src/config/index.ts +2 -1
- package/lib/knex/src/controllers/auth.controller.ts +9 -11
- package/lib/knex/src/controllers/users.controller.ts +10 -12
- package/lib/knex/src/database/index.ts +24 -0
- package/lib/knex/src/database/migrations/20210713110926_initial.ts +17 -0
- package/lib/knex/src/dtos/users.dto.ts +12 -1
- package/lib/knex/src/interfaces/users.interface.ts +2 -2
- package/lib/knex/src/middlewares/auth.middleware.ts +17 -10
- package/lib/knex/src/middlewares/error.middleware.ts +2 -4
- package/lib/knex/src/middlewares/validation.middleware.ts +22 -20
- package/lib/knex/src/models/users.model.ts +2 -2
- package/lib/knex/src/routes/auth.route.ts +8 -10
- package/lib/knex/src/routes/users.route.ts +10 -12
- package/lib/knex/src/server.ts +6 -7
- package/lib/knex/src/services/auth.service.ts +24 -33
- package/lib/knex/src/services/users.service.ts +14 -19
- package/lib/knex/src/{tests → test}/auth.test.ts +2 -2
- package/lib/knex/src/{tests → test}/users.test.ts +2 -2
- package/lib/knex/src/utils/validateEnv.ts +1 -3
- package/lib/knex/tsconfig.json +2 -2
- package/lib/mikro-orm/.env.production.local +1 -1
- package/lib/mikro-orm/.env.test.local +1 -1
- package/lib/mikro-orm/.swcrc +1 -1
- package/lib/mikro-orm/Dockerfile.dev +19 -0
- package/lib/mikro-orm/Dockerfile.prod +19 -0
- package/lib/mikro-orm/Makefile +32 -15
- package/lib/mikro-orm/docker-compose.yml +20 -11
- package/lib/mikro-orm/package.json +2 -0
- package/lib/mikro-orm/src/app.ts +12 -13
- package/lib/mikro-orm/src/config/index.ts +2 -1
- package/lib/mikro-orm/src/controllers/auth.controller.ts +9 -11
- package/lib/mikro-orm/src/controllers/users.controller.ts +11 -13
- package/lib/mikro-orm/src/dtos/users.dto.ts +12 -1
- package/lib/mikro-orm/src/interfaces/users.interface.ts +1 -1
- package/lib/mikro-orm/src/middlewares/auth.middleware.ts +17 -10
- package/lib/mikro-orm/src/middlewares/error.middleware.ts +2 -4
- package/lib/mikro-orm/src/middlewares/validation.middleware.ts +22 -20
- package/lib/mikro-orm/src/routes/auth.route.ts +8 -10
- package/lib/mikro-orm/src/routes/users.route.ts +9 -11
- package/lib/mikro-orm/src/server.ts +6 -7
- package/lib/mikro-orm/src/services/auth.service.ts +19 -29
- package/lib/mikro-orm/src/services/users.service.ts +8 -16
- package/lib/mikro-orm/src/{tests → test}/auth.test.ts +3 -4
- package/lib/mikro-orm/src/{tests → test}/users.test.ts +4 -5
- package/lib/mikro-orm/src/utils/validateEnv.ts +1 -3
- package/lib/mikro-orm/tsconfig.json +2 -2
- package/lib/mongoose/.env.production.local +1 -1
- package/lib/mongoose/.env.test.local +1 -1
- package/lib/mongoose/.swcrc +1 -1
- package/lib/mongoose/Dockerfile.dev +19 -0
- package/lib/mongoose/Dockerfile.prod +19 -0
- package/lib/mongoose/Makefile +32 -15
- package/lib/mongoose/docker-compose.yml +20 -11
- package/lib/mongoose/package.json +2 -0
- package/lib/mongoose/src/app.ts +7 -13
- package/lib/mongoose/src/config/index.ts +2 -1
- package/lib/mongoose/src/controllers/auth.controller.ts +9 -11
- package/lib/mongoose/src/controllers/users.controller.ts +11 -13
- package/lib/mongoose/src/database/index.ts +18 -0
- package/lib/mongoose/src/dtos/users.dto.ts +12 -1
- package/lib/mongoose/src/interfaces/users.interface.ts +1 -1
- package/lib/mongoose/src/middlewares/auth.middleware.ts +16 -9
- package/lib/mongoose/src/middlewares/error.middleware.ts +2 -4
- package/lib/mongoose/src/middlewares/validation.middleware.ts +22 -20
- package/lib/mongoose/src/models/users.model.ts +2 -4
- package/lib/mongoose/src/routes/auth.route.ts +8 -10
- package/lib/mongoose/src/routes/users.route.ts +9 -11
- package/lib/mongoose/src/server.ts +6 -7
- package/lib/mongoose/src/services/auth.service.ts +22 -33
- package/lib/mongoose/src/services/users.service.ts +14 -24
- package/lib/{typegoose/src/tests → mongoose/src/test}/users.test.ts +7 -7
- package/lib/mongoose/src/utils/validateEnv.ts +1 -3
- package/lib/mongoose/tsconfig.json +2 -2
- package/lib/node-postgres/.dockerignore +18 -0
- package/lib/node-postgres/.editorconfig +9 -0
- package/lib/node-postgres/.env.development.local +13 -0
- package/lib/node-postgres/.env.production.local +13 -0
- package/lib/node-postgres/.env.test.local +13 -0
- package/lib/node-postgres/.eslintignore +1 -0
- package/lib/node-postgres/.eslintrc +18 -0
- package/lib/node-postgres/.huskyrc +5 -0
- package/lib/node-postgres/.lintstagedrc.json +5 -0
- package/lib/node-postgres/.prettierrc +8 -0
- package/lib/node-postgres/.swcrc +39 -0
- package/lib/node-postgres/.vscode/launch.json +35 -0
- package/lib/node-postgres/.vscode/settings.json +6 -0
- package/lib/node-postgres/Dockerfile.dev +17 -0
- package/lib/node-postgres/Dockerfile.prod +17 -0
- package/lib/node-postgres/Makefile +46 -0
- package/lib/node-postgres/docker-compose.yml +61 -0
- package/lib/node-postgres/ecosystem.config.js +57 -0
- package/lib/node-postgres/jest.config.js +12 -0
- package/lib/node-postgres/nginx.conf +40 -0
- package/lib/node-postgres/nodemon.json +12 -0
- package/lib/node-postgres/package.json +78 -0
- package/lib/node-postgres/src/app.ts +87 -0
- package/lib/node-postgres/src/config/index.ts +6 -0
- package/lib/node-postgres/src/controllers/auth.controller.ts +44 -0
- package/lib/node-postgres/src/controllers/users.controller.ts +63 -0
- package/lib/node-postgres/src/database/index.ts +6 -0
- package/lib/node-postgres/src/database/init.sql +110 -0
- package/lib/node-postgres/src/dtos/users.dto.ts +20 -0
- package/lib/node-postgres/src/exceptions/httpException.ts +10 -0
- package/lib/node-postgres/src/http/auth.http +27 -0
- package/lib/node-postgres/src/http/users.http +34 -0
- package/lib/node-postgres/src/interfaces/auth.interface.ts +15 -0
- package/lib/node-postgres/src/interfaces/routes.interface.ts +6 -0
- package/lib/node-postgres/src/interfaces/users.interface.ts +5 -0
- package/lib/node-postgres/src/middlewares/auth.middleware.ts +38 -0
- package/lib/node-postgres/src/middlewares/error.middleware.ts +15 -0
- package/lib/node-postgres/src/middlewares/validation.middleware.ts +27 -0
- package/lib/node-postgres/src/models/users.model.ts +9 -0
- package/lib/node-postgres/src/routes/auth.route.ts +21 -0
- package/lib/node-postgres/src/routes/users.route.ts +23 -0
- package/lib/node-postgres/src/server.ts +10 -0
- package/lib/node-postgres/src/services/auth.service.ts +52 -0
- package/lib/node-postgres/src/services/users.service.ts +51 -0
- package/lib/{default/src/tests → node-postgres/src/test}/auth.test.ts +2 -2
- package/lib/{default/src/tests → node-postgres/src/test}/users.test.ts +5 -5
- package/lib/node-postgres/src/utils/logger.ts +65 -0
- package/lib/node-postgres/src/utils/validateEnv.ts +8 -0
- package/lib/node-postgres/swagger.yaml +123 -0
- package/lib/node-postgres/tsconfig.json +40 -0
- package/lib/prisma/.env.production.local +1 -1
- package/lib/prisma/.env.test.local +1 -1
- package/lib/prisma/Dockerfile.dev +19 -0
- package/lib/prisma/Dockerfile.prod +19 -0
- package/lib/prisma/Makefile +32 -15
- package/lib/prisma/docker-compose.yml +15 -14
- package/lib/prisma/package.json +2 -0
- package/lib/prisma/src/app.ts +4 -5
- package/lib/prisma/src/controllers/auth.controller.ts +10 -12
- package/lib/prisma/src/controllers/users.controller.ts +12 -14
- package/lib/prisma/src/dtos/users.dto.ts +12 -1
- package/lib/prisma/src/interfaces/auth.interface.ts +1 -1
- package/lib/prisma/src/interfaces/users.interface.ts +5 -0
- package/lib/prisma/src/middlewares/auth.middleware.ts +16 -11
- package/lib/prisma/src/middlewares/error.middleware.ts +2 -4
- package/lib/prisma/src/middlewares/validation.middleware.ts +22 -20
- package/lib/prisma/src/routes/auth.route.ts +8 -10
- package/lib/prisma/src/routes/users.route.ts +9 -11
- package/lib/prisma/src/server.ts +6 -7
- package/lib/prisma/src/services/auth.service.ts +6 -12
- package/lib/prisma/src/services/users.service.ts +15 -23
- package/lib/prisma/src/utils/validateEnv.ts +1 -3
- package/lib/prisma/tsconfig.json +1 -1
- package/lib/routing-controllers/Dockerfile.dev +19 -0
- package/lib/routing-controllers/Dockerfile.prod +19 -0
- package/lib/routing-controllers/Makefile +29 -16
- package/lib/routing-controllers/docker-compose.yml +9 -9
- package/lib/routing-controllers/package.json +1 -0
- package/lib/routing-controllers/src/app.ts +4 -6
- package/lib/routing-controllers/src/controllers/auth.controller.ts +13 -12
- package/lib/routing-controllers/src/controllers/users.controller.ts +15 -13
- package/lib/routing-controllers/src/dtos/users.dto.ts +12 -1
- package/lib/routing-controllers/src/interfaces/users.interface.ts +1 -1
- package/lib/routing-controllers/src/middlewares/auth.middleware.ts +16 -10
- package/lib/routing-controllers/src/middlewares/error.middleware.ts +2 -5
- package/lib/routing-controllers/src/middlewares/validation.middleware.ts +23 -27
- package/lib/routing-controllers/src/models/users.model.ts +1 -3
- package/lib/routing-controllers/src/server.ts +5 -6
- package/lib/routing-controllers/src/services/auth.service.ts +21 -31
- package/lib/routing-controllers/src/services/users.service.ts +13 -21
- package/lib/routing-controllers/src/{tests → test}/auth.test.ts +1 -1
- package/lib/routing-controllers/src/{tests → test}/users.test.ts +10 -10
- package/lib/routing-controllers/src/utils/validateEnv.ts +1 -3
- package/lib/routing-controllers/tsconfig.json +1 -1
- package/lib/sequelize/.env.development.local +2 -2
- package/lib/sequelize/.env.production.local +3 -3
- package/lib/sequelize/.env.test.local +3 -3
- package/lib/sequelize/.sequelizerc +8 -0
- package/lib/sequelize/.swcrc +1 -1
- package/lib/sequelize/Dockerfile.dev +23 -0
- package/lib/sequelize/Dockerfile.prod +23 -0
- package/lib/sequelize/Makefile +32 -15
- package/lib/sequelize/docker-compose.yml +19 -14
- package/lib/sequelize/package.json +5 -1
- package/lib/sequelize/src/app.ts +7 -8
- package/lib/sequelize/src/config/index.ts +2 -1
- package/lib/sequelize/src/config/sequelize-cli.js +15 -0
- package/lib/sequelize/src/controllers/auth.controller.ts +7 -8
- package/lib/sequelize/src/controllers/users.controller.ts +9 -10
- package/lib/sequelize/src/{databases → database}/index.ts +1 -3
- package/lib/sequelize/src/dtos/users.dto.ts +12 -1
- package/lib/sequelize/src/interfaces/users.interface.ts +1 -1
- package/lib/sequelize/src/middlewares/auth.middleware.ts +16 -10
- package/lib/sequelize/src/middlewares/error.middleware.ts +2 -4
- package/lib/sequelize/src/middlewares/validation.middleware.ts +22 -20
- package/lib/sequelize/src/routes/auth.route.ts +8 -11
- package/lib/sequelize/src/routes/users.route.ts +9 -11
- package/lib/sequelize/src/server.ts +6 -7
- package/lib/sequelize/src/services/auth.service.ts +20 -31
- package/lib/sequelize/src/services/users.service.ts +14 -25
- package/lib/sequelize/src/{tests → test}/auth.test.ts +2 -2
- package/lib/sequelize/src/{tests → test}/users.test.ts +2 -2
- package/lib/sequelize/src/utils/validateEnv.ts +1 -3
- package/lib/sequelize/tsconfig.json +2 -2
- package/lib/starter.js +17 -26
- package/lib/typegoose/.env.production.local +1 -1
- package/lib/typegoose/.env.test.local +1 -1
- package/lib/typegoose/.swcrc +1 -1
- package/lib/typegoose/Dockerfile.dev +19 -0
- package/lib/typegoose/Dockerfile.prod +19 -0
- package/lib/typegoose/Makefile +32 -15
- package/lib/typegoose/docker-compose.yml +20 -11
- package/lib/typegoose/package.json +2 -0
- package/lib/typegoose/src/app.ts +7 -10
- package/lib/typegoose/src/config/index.ts +2 -1
- package/lib/typegoose/src/controllers/auth.controller.ts +9 -11
- package/lib/typegoose/src/controllers/users.controller.ts +11 -13
- package/lib/typegoose/src/database/index.ts +18 -0
- package/lib/typegoose/src/dtos/users.dto.ts +12 -1
- package/lib/typegoose/src/interfaces/users.interface.ts +1 -1
- package/lib/typegoose/src/middlewares/auth.middleware.ts +16 -10
- package/lib/typegoose/src/middlewares/error.middleware.ts +2 -4
- package/lib/typegoose/src/middlewares/validation.middleware.ts +22 -20
- package/lib/typegoose/src/models/users.model.ts +1 -3
- package/lib/typegoose/src/routes/auth.route.ts +8 -11
- package/lib/typegoose/src/routes/users.route.ts +9 -11
- package/lib/typegoose/src/server.ts +5 -6
- package/lib/typegoose/src/services/auth.service.ts +23 -32
- package/lib/typegoose/src/services/users.service.ts +12 -21
- package/lib/typegoose/src/{tests → test}/auth.test.ts +2 -2
- package/lib/{mongoose/src/tests → typegoose/src/test}/users.test.ts +7 -7
- package/lib/typegoose/src/utils/validateEnv.ts +1 -3
- package/lib/typegoose/tsconfig.json +2 -2
- package/lib/typeorm/.env.development.local +2 -2
- package/lib/typeorm/.env.production.local +3 -3
- package/lib/typeorm/.env.test.local +3 -3
- package/lib/typeorm/.swcrc +1 -1
- package/lib/typeorm/Dockerfile.dev +19 -0
- package/lib/typeorm/Dockerfile.prod +19 -0
- package/lib/typeorm/Makefile +32 -15
- package/lib/typeorm/docker-compose.yml +21 -14
- package/lib/typeorm/package.json +1 -0
- package/lib/typeorm/src/app.ts +7 -10
- package/lib/typeorm/src/config/index.ts +2 -1
- package/lib/typeorm/src/controllers/auth.controller.ts +9 -11
- package/lib/typeorm/src/controllers/users.controller.ts +11 -13
- package/lib/typeorm/src/database/index.ts +26 -0
- package/lib/typeorm/src/dtos/users.dto.ts +12 -1
- package/lib/typeorm/src/interfaces/users.interface.ts +1 -1
- package/lib/typeorm/src/middlewares/auth.middleware.ts +14 -7
- package/lib/typeorm/src/middlewares/error.middleware.ts +2 -4
- package/lib/typeorm/src/middlewares/validation.middleware.ts +22 -20
- package/lib/typeorm/src/routes/auth.route.ts +8 -11
- package/lib/typeorm/src/routes/users.route.ts +9 -11
- package/lib/typeorm/src/server.ts +6 -7
- package/lib/typeorm/src/services/auth.service.ts +20 -28
- package/lib/typeorm/src/services/users.service.ts +6 -16
- package/lib/typeorm/src/{tests → test}/auth.test.ts +6 -6
- package/lib/typeorm/src/{tests → test}/users.test.ts +6 -6
- package/lib/typeorm/src/utils/validateEnv.ts +1 -3
- package/lib/typeorm/tsconfig.json +2 -2
- package/package.json +2 -1
- package/lib/default/Dockerfile +0 -24
- package/lib/default/src/controllers/index.controller.ts +0 -13
- package/lib/default/src/routes/index.route.ts +0 -19
- package/lib/default/src/utils/util.ts +0 -19
- package/lib/graphql/Dockerfile +0 -24
- package/lib/graphql/src/databases/index.ts +0 -22
- package/lib/graphql/src/tests/index.test.ts +0 -18
- package/lib/graphql/src/tests/users.test.ts +0 -71
- package/lib/graphql/src/utils/util.ts +0 -19
- package/lib/knex/Dockerfile +0 -24
- package/lib/knex/src/controllers/index.controller.ts +0 -13
- package/lib/knex/src/databases/index.ts +0 -21
- package/lib/knex/src/databases/migrations/20210713110926_initial.ts +0 -13
- package/lib/knex/src/routes/index.route.ts +0 -19
- package/lib/knex/src/tests/index.test.ts +0 -17
- package/lib/knex/src/utils/util.ts +0 -19
- package/lib/mikro-orm/Dockerfile +0 -24
- package/lib/mikro-orm/src/controllers/index.controller.ts +0 -13
- package/lib/mikro-orm/src/routes/index.route.ts +0 -19
- package/lib/mikro-orm/src/tests/index.test.ts +0 -18
- package/lib/mikro-orm/src/utils/util.ts +0 -19
- package/lib/mongoose/Dockerfile +0 -24
- package/lib/mongoose/src/controllers/index.controller.ts +0 -13
- package/lib/mongoose/src/databases/index.ts +0 -9
- package/lib/mongoose/src/routes/index.route.ts +0 -19
- package/lib/mongoose/src/utils/util.ts +0 -19
- package/lib/prisma/Dockerfile +0 -24
- package/lib/prisma/src/controllers/index.controller.ts +0 -13
- package/lib/prisma/src/routes/index.route.ts +0 -19
- package/lib/prisma/src/tests/index.test.ts +0 -18
- package/lib/prisma/src/utils/util.ts +0 -19
- package/lib/routing-controllers/Dockerfile +0 -24
- package/lib/routing-controllers/src/controllers/index.controller.ts +0 -9
- package/lib/routing-controllers/src/tests/index.test.ts +0 -17
- package/lib/routing-controllers/src/utils/util.ts +0 -19
- package/lib/sequelize/Dockerfile +0 -28
- package/lib/sequelize/src/controllers/index.controller.ts +0 -13
- package/lib/sequelize/src/routes/index.route.ts +0 -19
- package/lib/sequelize/src/tests/index.test.ts +0 -18
- package/lib/sequelize/src/utils/util.ts +0 -19
- package/lib/typegoose/Dockerfile +0 -24
- package/lib/typegoose/src/controllers/index.controller.ts +0 -13
- package/lib/typegoose/src/databases/index.ts +0 -3
- package/lib/typegoose/src/routes/index.route.ts +0 -19
- package/lib/typegoose/src/tests/index.test.ts +0 -18
- package/lib/typegoose/src/utils/util.ts +0 -19
- package/lib/typeorm/Dockerfile +0 -24
- package/lib/typeorm/src/controllers/index.controller.ts +0 -13
- package/lib/typeorm/src/databases/index.ts +0 -22
- package/lib/typeorm/src/routes/index.route.ts +0 -19
- package/lib/typeorm/src/tests/index.test.ts +0 -18
- package/lib/typeorm/src/utils/util.ts +0 -19
- package/lib/knex/src/{databases → database}/migrations/.gitkeep +0 -0
- package/lib/knex/src/{databases → database}/seeds/.gitkeep +0 -0
- package/lib/mikro-orm/src/{databases → database}/index.ts +0 -0
- package/lib/mongoose/src/{tests → test}/auth.test.ts +0 -0
- package/lib/{default/src/tests → mongoose/src/test}/index.test.ts +0 -0
- package/lib/prisma/src/{tests → test}/auth.test.ts +1 -1
- package/lib/{mongoose/src/tests → prisma/src/test}/index.test.ts +0 -0
- package/lib/prisma/src/{tests → test}/users.test.ts +1 -1
- /package/lib/sequelize/src/{migrations → database/migrations}/.gitkeep +0 -0
- /package/lib/sequelize/src/{seeders → database/seeders}/.gitkeep +0 -0
- /package/lib/typeorm/src/{migration → database/migrations}/.gitkeep +0 -0
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "typescript-express-starter",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "TypeScript + PostgreSQL + 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
|
+
"bcrypt": "^5.0.1",
|
|
20
|
+
"class-transformer": "^0.5.1",
|
|
21
|
+
"class-validator": "^0.13.2",
|
|
22
|
+
"compression": "^1.7.4",
|
|
23
|
+
"cookie-parser": "^1.4.6",
|
|
24
|
+
"cors": "^2.8.5",
|
|
25
|
+
"dotenv": "^16.0.1",
|
|
26
|
+
"envalid": "^7.3.1",
|
|
27
|
+
"express": "^4.18.1",
|
|
28
|
+
"helmet": "^5.1.1",
|
|
29
|
+
"hpp": "^0.2.3",
|
|
30
|
+
"jsonwebtoken": "^8.5.1",
|
|
31
|
+
"morgan": "^1.10.0",
|
|
32
|
+
"pg": "^8.8.0",
|
|
33
|
+
"swagger-jsdoc": "^6.2.1",
|
|
34
|
+
"swagger-ui-express": "^4.5.0",
|
|
35
|
+
"reflect-metadata": "^0.1.3",
|
|
36
|
+
"typedi": "^0.10.0",
|
|
37
|
+
"winston": "^3.8.1",
|
|
38
|
+
"winston-daily-rotate-file": "^4.7.1"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@swc/cli": "^0.1.57",
|
|
42
|
+
"@swc/core": "^1.2.220",
|
|
43
|
+
"@types/bcrypt": "^5.0.0",
|
|
44
|
+
"@types/compression": "^1.7.2",
|
|
45
|
+
"@types/cookie-parser": "^1.4.3",
|
|
46
|
+
"@types/cors": "^2.8.12",
|
|
47
|
+
"@types/express": "^4.17.13",
|
|
48
|
+
"@types/hpp": "^0.2.2",
|
|
49
|
+
"@types/jest": "^28.1.6",
|
|
50
|
+
"@types/jsonwebtoken": "^8.5.8",
|
|
51
|
+
"@types/morgan": "^1.9.3",
|
|
52
|
+
"@types/node": "^17.0.45",
|
|
53
|
+
"@types/pg": "^8.6.5",
|
|
54
|
+
"@types/supertest": "^2.0.12",
|
|
55
|
+
"@types/swagger-jsdoc": "^6.0.1",
|
|
56
|
+
"@types/swagger-ui-express": "^4.1.3",
|
|
57
|
+
"@typescript-eslint/eslint-plugin": "^5.29.0",
|
|
58
|
+
"@typescript-eslint/parser": "^5.29.0",
|
|
59
|
+
"cross-env": "^7.0.3",
|
|
60
|
+
"eslint": "^8.20.0",
|
|
61
|
+
"eslint-config-prettier": "^8.5.0",
|
|
62
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
63
|
+
"husky": "^8.0.1",
|
|
64
|
+
"jest": "^28.1.1",
|
|
65
|
+
"lint-staged": "^13.0.3",
|
|
66
|
+
"node-config": "^0.0.2",
|
|
67
|
+
"node-gyp": "^9.1.0",
|
|
68
|
+
"nodemon": "^2.0.19",
|
|
69
|
+
"pm2": "^5.2.0",
|
|
70
|
+
"prettier": "^2.7.1",
|
|
71
|
+
"supertest": "^6.2.4",
|
|
72
|
+
"ts-jest": "^28.0.7",
|
|
73
|
+
"ts-node": "^10.9.1",
|
|
74
|
+
"tsc-alias": "^1.7.0",
|
|
75
|
+
"tsconfig-paths": "^4.0.0",
|
|
76
|
+
"typescript": "^4.7.4"
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import compression from 'compression';
|
|
3
|
+
import cookieParser from 'cookie-parser';
|
|
4
|
+
import cors from 'cors';
|
|
5
|
+
import express from 'express';
|
|
6
|
+
import helmet from 'helmet';
|
|
7
|
+
import hpp from 'hpp';
|
|
8
|
+
import morgan from 'morgan';
|
|
9
|
+
import swaggerJSDoc from 'swagger-jsdoc';
|
|
10
|
+
import swaggerUi from 'swagger-ui-express';
|
|
11
|
+
import { NODE_ENV, PORT, LOG_FORMAT, ORIGIN, CREDENTIALS } from '@config';
|
|
12
|
+
import { client } from '@database';
|
|
13
|
+
import { Routes } from '@interfaces/routes.interface';
|
|
14
|
+
import { ErrorMiddleware } from '@middlewares/error.middleware';
|
|
15
|
+
import { logger, stream } from '@utils/logger';
|
|
16
|
+
|
|
17
|
+
export class App {
|
|
18
|
+
public app: express.Application;
|
|
19
|
+
public env: string;
|
|
20
|
+
public port: string | number;
|
|
21
|
+
|
|
22
|
+
constructor(routes: Routes[]) {
|
|
23
|
+
this.app = express();
|
|
24
|
+
this.env = NODE_ENV || 'development';
|
|
25
|
+
this.port = PORT || 3000;
|
|
26
|
+
|
|
27
|
+
this.connectToDatabase();
|
|
28
|
+
this.initializeMiddlewares();
|
|
29
|
+
this.initializeRoutes(routes);
|
|
30
|
+
this.initializeSwagger();
|
|
31
|
+
this.initializeErrorHandling();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public listen() {
|
|
35
|
+
this.app.listen(this.port, () => {
|
|
36
|
+
logger.info(`=================================`);
|
|
37
|
+
logger.info(`======= ENV: ${this.env} =======`);
|
|
38
|
+
logger.info(`🚀 App listening on the port ${this.port}`);
|
|
39
|
+
logger.info(`=================================`);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public getServer() {
|
|
44
|
+
return this.app;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private async connectToDatabase() {
|
|
48
|
+
await client.connect();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private initializeMiddlewares() {
|
|
52
|
+
this.app.use(morgan(LOG_FORMAT, { stream }));
|
|
53
|
+
this.app.use(cors({ origin: ORIGIN, credentials: CREDENTIALS }));
|
|
54
|
+
this.app.use(hpp());
|
|
55
|
+
this.app.use(helmet());
|
|
56
|
+
this.app.use(compression());
|
|
57
|
+
this.app.use(express.json());
|
|
58
|
+
this.app.use(express.urlencoded({ extended: true }));
|
|
59
|
+
this.app.use(cookieParser());
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private initializeRoutes(routes: Routes[]) {
|
|
63
|
+
routes.forEach(route => {
|
|
64
|
+
this.app.use('/', route.router);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private initializeSwagger() {
|
|
69
|
+
const options = {
|
|
70
|
+
swaggerDefinition: {
|
|
71
|
+
info: {
|
|
72
|
+
title: 'REST API',
|
|
73
|
+
version: '1.0.0',
|
|
74
|
+
description: 'Example docs',
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
apis: ['swagger.yaml'],
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const specs = swaggerJSDoc(options);
|
|
81
|
+
this.app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private initializeErrorHandling() {
|
|
85
|
+
this.app.use(ErrorMiddleware);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { config } from 'dotenv';
|
|
2
|
+
config({ path: `.env.${process.env.NODE_ENV || 'development'}.local` });
|
|
3
|
+
|
|
4
|
+
export const CREDENTIALS = process.env.CREDENTIALS === 'true';
|
|
5
|
+
export const { NODE_ENV, PORT, SECRET_KEY, LOG_FORMAT, LOG_DIR, ORIGIN } = process.env;
|
|
6
|
+
export const { DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_DATABASE } = process.env;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { NextFunction, Request, Response } from 'express';
|
|
2
|
+
import { Container } from 'typedi';
|
|
3
|
+
import { RequestWithUser } from '@interfaces/auth.interface';
|
|
4
|
+
import { User } from '@interfaces/users.interface';
|
|
5
|
+
import { AuthService } from '@services/auth.service';
|
|
6
|
+
|
|
7
|
+
export class AuthController {
|
|
8
|
+
public auth = Container.get(AuthService);
|
|
9
|
+
|
|
10
|
+
public signUp = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
|
11
|
+
try {
|
|
12
|
+
const userData: User = req.body;
|
|
13
|
+
const signUpUserData: User = await this.auth.signup(userData);
|
|
14
|
+
|
|
15
|
+
res.status(201).json({ data: signUpUserData, message: 'signup' });
|
|
16
|
+
} catch (error) {
|
|
17
|
+
next(error);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
public logIn = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
|
22
|
+
try {
|
|
23
|
+
const userData: User = req.body;
|
|
24
|
+
const { cookie, findUser } = await this.auth.login(userData);
|
|
25
|
+
|
|
26
|
+
res.setHeader('Set-Cookie', [cookie]);
|
|
27
|
+
res.status(200).json({ data: findUser, message: 'login' });
|
|
28
|
+
} catch (error) {
|
|
29
|
+
next(error);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
public logOut = async (req: RequestWithUser, res: Response, next: NextFunction): Promise<void> => {
|
|
34
|
+
try {
|
|
35
|
+
const userData: User = req.user;
|
|
36
|
+
const logOutUserData: User = await this.auth.logout(userData);
|
|
37
|
+
|
|
38
|
+
res.setHeader('Set-Cookie', ['Authorization=; Max-age=0']);
|
|
39
|
+
res.status(200).json({ data: logOutUserData, message: 'logout' });
|
|
40
|
+
} catch (error) {
|
|
41
|
+
next(error);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { NextFunction, Request, Response } from 'express';
|
|
2
|
+
import { Container } from 'typedi';
|
|
3
|
+
import { User } from '@interfaces/users.interface';
|
|
4
|
+
import { UserService } from '@services/users.service';
|
|
5
|
+
|
|
6
|
+
export class UserController {
|
|
7
|
+
public user = Container.get(UserService);
|
|
8
|
+
|
|
9
|
+
public getUsers = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
|
10
|
+
try {
|
|
11
|
+
const findAllUsersData: User[] = await this.user.findAllUser();
|
|
12
|
+
|
|
13
|
+
res.status(200).json({ data: findAllUsersData, message: 'findAll' });
|
|
14
|
+
} catch (error) {
|
|
15
|
+
next(error);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
public getUserById = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
|
20
|
+
try {
|
|
21
|
+
const userId = Number(req.params.id);
|
|
22
|
+
const findOneUserData: User = await this.user.findUserById(userId);
|
|
23
|
+
|
|
24
|
+
res.status(200).json({ data: findOneUserData, message: 'findOne' });
|
|
25
|
+
} catch (error) {
|
|
26
|
+
next(error);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
public createUser = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
|
31
|
+
try {
|
|
32
|
+
const userData: User = req.body;
|
|
33
|
+
const createUserData: User = await this.user.createUser(userData);
|
|
34
|
+
|
|
35
|
+
res.status(201).json({ data: createUserData, message: 'created' });
|
|
36
|
+
} catch (error) {
|
|
37
|
+
next(error);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
public updateUser = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
|
42
|
+
try {
|
|
43
|
+
const userId = Number(req.params.id);
|
|
44
|
+
const userData: User = req.body;
|
|
45
|
+
const updateUserData: User[] = await this.user.updateUser(userId, userData);
|
|
46
|
+
|
|
47
|
+
res.status(200).json({ data: updateUserData, message: 'updated' });
|
|
48
|
+
} catch (error) {
|
|
49
|
+
next(error);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
public deleteUser = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
|
54
|
+
try {
|
|
55
|
+
const userId = Number(req.params.id);
|
|
56
|
+
const deleteUserData: User[] = await this.user.deleteUser(userId);
|
|
57
|
+
|
|
58
|
+
res.status(200).json({ data: deleteUserData, message: 'deleted' });
|
|
59
|
+
} catch (error) {
|
|
60
|
+
next(error);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
-- TABLE 존재할 경우 삭제
|
|
2
|
+
DROP TABLE IF EXISTS teachers cascade;
|
|
3
|
+
DROP TABLE IF EXISTS classes cascade;
|
|
4
|
+
DROP TABLE IF EXISTS students cascade;
|
|
5
|
+
DROP TABLE IF EXISTS classes_category cascade;
|
|
6
|
+
DROP TABLE IF EXISTS enrolment cascade;
|
|
7
|
+
|
|
8
|
+
-- ============
|
|
9
|
+
-- 강사 테이블
|
|
10
|
+
-- ============
|
|
11
|
+
-- 강사 테이블 생성
|
|
12
|
+
CREATE TABLE teachers(
|
|
13
|
+
"teacherId" SERIAL PRIMARY KEY,
|
|
14
|
+
-- 강사 ID
|
|
15
|
+
"teacherName" VARCHAR(32) NOT NULL,
|
|
16
|
+
-- 강사명
|
|
17
|
+
"teacherAbout" VARCHAR(48) -- 강사 정보
|
|
18
|
+
);
|
|
19
|
+
-- 강사 데이터 생성
|
|
20
|
+
INSERT INTO teachers(
|
|
21
|
+
"teacherId",
|
|
22
|
+
"teacherName",
|
|
23
|
+
"teacherAbout"
|
|
24
|
+
)
|
|
25
|
+
VALUES (1, '조현영', '웹 개발 강사'),
|
|
26
|
+
(2, '개복치개발자', '안드로이드 코틀린 강사'),
|
|
27
|
+
(3, '이고잉', '생활코딩 운영진 겸 강사'),
|
|
28
|
+
(4, '김태원', '파이썬 알고리즘 강사'),
|
|
29
|
+
(5, '윤재성', 'Kotiln 기반 안드로이드 강사'),
|
|
30
|
+
(6, '조훈', '쿠버네티스 강사'),
|
|
31
|
+
(7, 'Rookiss', '얼리언 엔진 전문 강사'),
|
|
32
|
+
(8, '유용한IT학습', 'SQLD 자격증 취득 강사'),
|
|
33
|
+
(9, '김태민', '쿠버네티스 강사'),
|
|
34
|
+
(10, '큰돌', '알고리즘 강사');
|
|
35
|
+
SELECT SETVAL(
|
|
36
|
+
'"teachers_teacherId_seq"',
|
|
37
|
+
(
|
|
38
|
+
SELECT max("teacherId")
|
|
39
|
+
FROM teachers
|
|
40
|
+
)
|
|
41
|
+
);
|
|
42
|
+
-- ===================
|
|
43
|
+
-- 강의 카테고리 테이블
|
|
44
|
+
-- ===================
|
|
45
|
+
-- 강의 카테고리 테이블 생성
|
|
46
|
+
CREATE TABLE classes_category(
|
|
47
|
+
"categoryId" SERIAL PRIMARY KEY,
|
|
48
|
+
-- 카테고리 ID
|
|
49
|
+
"categoryName" VARCHAR(32) UNIQUE NOT NULL -- 카테고리 명
|
|
50
|
+
);
|
|
51
|
+
-- 강의 카테고리 데이터 생성
|
|
52
|
+
INSERT INTO classes_category("categoryId", "categoryName")
|
|
53
|
+
VALUES (1, '웹'),
|
|
54
|
+
(2, '앱'),
|
|
55
|
+
(3, '게임'),
|
|
56
|
+
(4, '알고리즘'),
|
|
57
|
+
(5, '인프라'),
|
|
58
|
+
(6, '데이터베이스');
|
|
59
|
+
SELECT SETVAL(
|
|
60
|
+
'"classes_category_categoryId_seq"',
|
|
61
|
+
(
|
|
62
|
+
SELECT max("categoryId")
|
|
63
|
+
FROM classes_category
|
|
64
|
+
)
|
|
65
|
+
);
|
|
66
|
+
-- ============
|
|
67
|
+
-- 강의 테이블
|
|
68
|
+
-- ============
|
|
69
|
+
-- 강의 테이블 생성
|
|
70
|
+
CREATE TABLE classes(
|
|
71
|
+
"classId" SERIAL PRIMARY KEY,
|
|
72
|
+
-- 강의 ID
|
|
73
|
+
"className" VARCHAR(32) UNIQUE NOT NULL,
|
|
74
|
+
-- 강의명
|
|
75
|
+
"classPrice" INTEGER NOT NULL DEFAULT 0,
|
|
76
|
+
-- 가격
|
|
77
|
+
"introduce" TEXT,
|
|
78
|
+
-- 강의 소개
|
|
79
|
+
"active" BOOLEAN NOT NULL DEFAULT false,
|
|
80
|
+
-- 강의 활성화 (true: 공개, false, 비공개)
|
|
81
|
+
"createdAt" TIMESTAMP WITHOUT TIME ZONE DEFAULT(NOW() AT TIME ZONE 'utc'),
|
|
82
|
+
-- 강의 생성 일자
|
|
83
|
+
"updatedAt" TIMESTAMP WITHOUT TIME ZONE,
|
|
84
|
+
-- 강의 수정 일자
|
|
85
|
+
"teacherId" INTEGER REFERENCES teachers("teacherId") ON DELETE CASCADE,
|
|
86
|
+
-- 강사 ID
|
|
87
|
+
"categoryId" INTEGER REFERENCES classes_category("categoryId") ON DELETE CASCADE -- 강사 ID
|
|
88
|
+
);
|
|
89
|
+
-- ==============
|
|
90
|
+
-- 수강생 테이블
|
|
91
|
+
-- ==============
|
|
92
|
+
-- 수강생 테이블 생성
|
|
93
|
+
CREATE TABLE students(
|
|
94
|
+
"studentId" SERIAL PRIMARY KEY,
|
|
95
|
+
-- 수강생 ID
|
|
96
|
+
"email" VARCHAR(48) UNIQUE NOT NULL,
|
|
97
|
+
-- 수강생 이메일
|
|
98
|
+
"nickname" VARCHAR(32) NOT NULL -- 수강생 닉네임
|
|
99
|
+
);
|
|
100
|
+
-- ===============
|
|
101
|
+
-- 수강신청 테이블
|
|
102
|
+
-- ===============
|
|
103
|
+
-- 수강신청 테이블 생성
|
|
104
|
+
CREATE TABLE enrolment(
|
|
105
|
+
"classId" INTEGER REFERENCES classes("classId") ON DELETE CASCADE,
|
|
106
|
+
-- 강의 ID
|
|
107
|
+
"studentId" INTEGER REFERENCES students("studentId") ON DELETE CASCADE,
|
|
108
|
+
-- 수강생 ID
|
|
109
|
+
"applicationAt" TIMESTAMP WITHOUT TIME ZONE DEFAULT(NOW() AT TIME ZONE 'utc') -- 신청 일자
|
|
110
|
+
);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { IsEmail, IsString, IsNotEmpty, MinLength, MaxLength } from 'class-validator';
|
|
2
|
+
|
|
3
|
+
export class CreateUserDto {
|
|
4
|
+
@IsEmail()
|
|
5
|
+
public email: string;
|
|
6
|
+
|
|
7
|
+
@IsString()
|
|
8
|
+
@IsNotEmpty()
|
|
9
|
+
@MinLength(9)
|
|
10
|
+
@MaxLength(32)
|
|
11
|
+
public password: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class UpdateUserDto {
|
|
15
|
+
@IsString()
|
|
16
|
+
@IsNotEmpty()
|
|
17
|
+
@MinLength(9)
|
|
18
|
+
@MaxLength(32)
|
|
19
|
+
public password: string;
|
|
20
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# baseURL
|
|
2
|
+
@baseURL = http://localhost:3000
|
|
3
|
+
|
|
4
|
+
###
|
|
5
|
+
# User Signup
|
|
6
|
+
POST {{ baseURL }}/signup
|
|
7
|
+
Content-Type: application/json
|
|
8
|
+
|
|
9
|
+
{
|
|
10
|
+
"email": "example@email.com",
|
|
11
|
+
"password": "password"
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
###
|
|
15
|
+
# User Login
|
|
16
|
+
POST {{ baseURL }}/login
|
|
17
|
+
Content-Type: application/json
|
|
18
|
+
|
|
19
|
+
{
|
|
20
|
+
"email": "example@email.com",
|
|
21
|
+
"password": "password"
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
###
|
|
25
|
+
# User Logout
|
|
26
|
+
POST {{ baseURL }}/logout
|
|
27
|
+
Content-Type: application/json
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# baseURL
|
|
2
|
+
@baseURL = http://localhost:3000
|
|
3
|
+
|
|
4
|
+
###
|
|
5
|
+
# Find All Users
|
|
6
|
+
GET {{ baseURL }}/users
|
|
7
|
+
|
|
8
|
+
###
|
|
9
|
+
# Find User By Id
|
|
10
|
+
GET {{ baseURL }}/users/1
|
|
11
|
+
|
|
12
|
+
###
|
|
13
|
+
# Create User
|
|
14
|
+
POST {{ baseURL }}/users
|
|
15
|
+
Content-Type: application/json
|
|
16
|
+
|
|
17
|
+
{
|
|
18
|
+
"email": "example@email.com",
|
|
19
|
+
"password": "password"
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
###
|
|
23
|
+
# Modify User By Id
|
|
24
|
+
PUT {{ baseURL }}/users/1
|
|
25
|
+
Content-Type: application/json
|
|
26
|
+
|
|
27
|
+
{
|
|
28
|
+
"email": "example@email.com",
|
|
29
|
+
"password": "password"
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
###
|
|
33
|
+
# Delete User By Id
|
|
34
|
+
DELETE {{ baseURL }}/users/1
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Request } from 'express';
|
|
2
|
+
import { User } from '@interfaces/users.interface';
|
|
3
|
+
|
|
4
|
+
export interface DataStoredInToken {
|
|
5
|
+
id: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface TokenData {
|
|
9
|
+
token: string;
|
|
10
|
+
expiresIn: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface RequestWithUser extends Request {
|
|
14
|
+
user: User;
|
|
15
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { NextFunction, Response } from 'express';
|
|
2
|
+
import { verify } from 'jsonwebtoken';
|
|
3
|
+
import { SECRET_KEY } from '@config';
|
|
4
|
+
import { HttpException } from '@exceptions/httpException';
|
|
5
|
+
import { DataStoredInToken, RequestWithUser } from '@interfaces/auth.interface';
|
|
6
|
+
import { UserModel } from '@models/users.model';
|
|
7
|
+
|
|
8
|
+
const getAuthorization = req => {
|
|
9
|
+
const coockie = req.cookies['Authorization'];
|
|
10
|
+
if (coockie) return coockie;
|
|
11
|
+
|
|
12
|
+
const header = req.header('Authorization');
|
|
13
|
+
if (header) return header.split('Bearer ')[1];
|
|
14
|
+
|
|
15
|
+
return null;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const AuthMiddleware = async (req: RequestWithUser, res: Response, next: NextFunction) => {
|
|
19
|
+
try {
|
|
20
|
+
const Authorization = getAuthorization(req);
|
|
21
|
+
|
|
22
|
+
if (Authorization) {
|
|
23
|
+
const { id } = (await verify(Authorization, SECRET_KEY)) as DataStoredInToken;
|
|
24
|
+
const findUser = UserModel.find(user => user.id === id);
|
|
25
|
+
|
|
26
|
+
if (findUser) {
|
|
27
|
+
req.user = findUser;
|
|
28
|
+
next();
|
|
29
|
+
} else {
|
|
30
|
+
next(new HttpException(401, 'Wrong authentication token'));
|
|
31
|
+
}
|
|
32
|
+
} else {
|
|
33
|
+
next(new HttpException(404, 'Authentication token missing'));
|
|
34
|
+
}
|
|
35
|
+
} catch (error) {
|
|
36
|
+
next(new HttpException(401, 'Wrong authentication token'));
|
|
37
|
+
}
|
|
38
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { NextFunction, Request, Response } from 'express';
|
|
2
|
+
import { HttpException } from '@exceptions/httpException';
|
|
3
|
+
import { logger } from '@utils/logger';
|
|
4
|
+
|
|
5
|
+
export const ErrorMiddleware = (error: HttpException, req: Request, res: Response, next: NextFunction) => {
|
|
6
|
+
try {
|
|
7
|
+
const status: number = error.status || 500;
|
|
8
|
+
const message: string = error.message || 'Something went wrong';
|
|
9
|
+
|
|
10
|
+
logger.error(`[${req.method}] ${req.path} >> StatusCode:: ${status}, Message:: ${message}`);
|
|
11
|
+
res.status(status).json({ message });
|
|
12
|
+
} catch (error) {
|
|
13
|
+
next(error);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { plainToInstance } from 'class-transformer';
|
|
2
|
+
import { validateOrReject, ValidationError } from 'class-validator';
|
|
3
|
+
import { NextFunction, Request, Response } from 'express';
|
|
4
|
+
import { HttpException } from '@exceptions/httpException';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @name ValidationMiddleware
|
|
8
|
+
* @description Allows use of decorator and non-decorator based validation
|
|
9
|
+
* @param type dto
|
|
10
|
+
* @param skipMissingProperties When skipping missing properties
|
|
11
|
+
* @param whitelist Even if your object is an instance of a validation class it can contain additional properties that are not defined
|
|
12
|
+
* @param forbidNonWhitelisted If you would rather to have an error thrown when any non-whitelisted properties are present
|
|
13
|
+
*/
|
|
14
|
+
export const ValidationMiddleware = (type: any, skipMissingProperties = false, whitelist = false, forbidNonWhitelisted = false) => {
|
|
15
|
+
return (req: Request, res: Response, next: NextFunction) => {
|
|
16
|
+
const dto = plainToInstance(type, req.body);
|
|
17
|
+
validateOrReject(dto, { skipMissingProperties, whitelist, forbidNonWhitelisted })
|
|
18
|
+
.then(() => {
|
|
19
|
+
req.body = dto;
|
|
20
|
+
next();
|
|
21
|
+
})
|
|
22
|
+
.catch((errors: ValidationError[]) => {
|
|
23
|
+
const message = errors.map((error: ValidationError) => Object.values(error.constraints)).join(', ');
|
|
24
|
+
next(new HttpException(400, message));
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { User } from '@interfaces/users.interface';
|
|
2
|
+
|
|
3
|
+
// password: password
|
|
4
|
+
export const UserModel: User[] = [
|
|
5
|
+
{ id: 1, email: 'example1@email.com', password: '$2b$10$TBEfaCe1oo.2jfkBDWcj/usBj4oECsW2wOoDXpCa2IH9xqCpEK/hC' },
|
|
6
|
+
{ id: 2, email: 'example2@email.com', password: '$2b$10$TBEfaCe1oo.2jfkBDWcj/usBj4oECsW2wOoDXpCa2IH9xqCpEK/hC' },
|
|
7
|
+
{ id: 3, email: 'example3@email.com', password: '$2b$10$TBEfaCe1oo.2jfkBDWcj/usBj4oECsW2wOoDXpCa2IH9xqCpEK/hC' },
|
|
8
|
+
{ id: 4, email: 'example4@email.com', password: '$2b$10$TBEfaCe1oo.2jfkBDWcj/usBj4oECsW2wOoDXpCa2IH9xqCpEK/hC' },
|
|
9
|
+
];
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import { AuthController } from '@controllers/auth.controller';
|
|
3
|
+
import { CreateUserDto } from '@dtos/users.dto';
|
|
4
|
+
import { Routes } from '@interfaces/routes.interface';
|
|
5
|
+
import { AuthMiddleware } from '@middlewares/auth.middleware';
|
|
6
|
+
import { ValidationMiddleware } from '@middlewares/validation.middleware';
|
|
7
|
+
|
|
8
|
+
export class AuthRoute implements Routes {
|
|
9
|
+
public router = Router();
|
|
10
|
+
public auth = new AuthController();
|
|
11
|
+
|
|
12
|
+
constructor() {
|
|
13
|
+
this.initializeRoutes();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
private initializeRoutes() {
|
|
17
|
+
this.router.post('/signup', ValidationMiddleware(CreateUserDto, 'body'), this.auth.signUp);
|
|
18
|
+
this.router.post('/login', ValidationMiddleware(CreateUserDto, 'body'), this.auth.logIn);
|
|
19
|
+
this.router.post('/logout', AuthMiddleware, this.auth.logOut);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import { UserController } from '@controllers/users.controller';
|
|
3
|
+
import { CreateUserDto } from '@dtos/users.dto';
|
|
4
|
+
import { Routes } from '@interfaces/routes.interface';
|
|
5
|
+
import { ValidationMiddleware } from '@middlewares/validation.middleware';
|
|
6
|
+
|
|
7
|
+
export class UserRoute implements Routes {
|
|
8
|
+
public path = '/users';
|
|
9
|
+
public router = Router();
|
|
10
|
+
public user = new UserController();
|
|
11
|
+
|
|
12
|
+
constructor() {
|
|
13
|
+
this.initializeRoutes();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
private initializeRoutes() {
|
|
17
|
+
this.router.get(`${this.path}`, this.user.getUsers);
|
|
18
|
+
this.router.get(`${this.path}/:id(\\d+)`, this.user.getUserById);
|
|
19
|
+
this.router.post(`${this.path}`, ValidationMiddleware(CreateUserDto, 'body'), this.user.createUser);
|
|
20
|
+
this.router.put(`${this.path}/:id(\\d+)`, ValidationMiddleware(CreateUserDto, 'body', true), this.user.updateUser);
|
|
21
|
+
this.router.delete(`${this.path}/:id(\\d+)`, this.user.deleteUser);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { App } from '@/app';
|
|
2
|
+
import { AuthRoute } from '@routes/auth.route';
|
|
3
|
+
import { UserRoute } from '@routes/users.route';
|
|
4
|
+
import { ValidateEnv } from '@utils/validateEnv';
|
|
5
|
+
|
|
6
|
+
ValidateEnv();
|
|
7
|
+
|
|
8
|
+
const app = new App([new AuthRoute(), new UserRoute()]);
|
|
9
|
+
|
|
10
|
+
app.listen();
|