Skip to content

Commit

Permalink
add swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
oroth8 committed Apr 3, 2023
1 parent a139938 commit 8ed19fa
Show file tree
Hide file tree
Showing 8 changed files with 1,061 additions and 15 deletions.
32 changes: 25 additions & 7 deletions be/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import cors from 'cors'
import * as url from 'url'
import cookieParser from 'cookie-parser'
import { ErrorMiddleware } from './src/middlewares/error.middleware.js'
import swaggerJSDoc from 'swagger-jsdoc'
import swaggerUi from 'swagger-ui-express'

loadEnv()

Expand All @@ -28,7 +30,7 @@ export class App {
this.initializeFrontend()
this.initializeMiddlewares()
this.initializeRoutes(routes)
// this.initializeSwagger();
this.initializeSwagger()
this.initializeErrorHandling()
}

Expand All @@ -46,12 +48,12 @@ export class App {
}

initializeFrontend() {
const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
// Use build folder for static files
this.app.use(express.static('build'))
this.app.get('/env', exposeEnvMiddleware(loadPublicEnv))

// TODO ADD ERROR HANDLING FOR UNKNOWN ROUTES
// const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
// this.app.get('*', (req, res) =>
// res.sendFile(path.join(__dirname, '../', '/build/index.html'))
// )
Expand Down Expand Up @@ -99,21 +101,37 @@ export class App {

initializeRoutes(routes) {
routes.forEach((route) => {
this.app.use('/api', route.router)
this.app.use('/api/v1', route.router)
})
}

initializeSwagger() {
const swaggerPath = path.resolve('be', 'src', 'routes', '*')
console.log(swaggerPath)
const options = {
swaggerDefinition: {
definition: {
openapi: '3.0.0',
info: {
title: 'REST API',
title: 'Veritext Express API',
version: '1.0.0',
description: 'Example docs',
description:
'Veritet Express API with autogenerated swagger documentation',
contact: {
name: 'Launchpad Lab',
url: 'https://launchpadlab.com/',
email: '[email protected]',
},
},
servers: [
{
url: 'http://localhost:3000',
basepath: '/api/v1',
},
],
},
apis: ['swagger.yaml'],
apis: [swaggerPath],
}

const specs = swaggerJSDoc(options)
this.app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs))
}
Expand Down
1 change: 1 addition & 0 deletions be/node_modules/.bin/swagger-jsdoc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions be/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"prisma": "^4.12.0",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^4.6.2",
"winston": "^3.8.2",
"winston-daily-rotate-file": "^4.7.1"
},
Expand Down
599 changes: 599 additions & 0 deletions be/src/logs/debug/2023-04-03.log

Large diffs are not rendered by default.

118 changes: 118 additions & 0 deletions be/src/logs/error/2023-04-03.log
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,121 @@ Argument where of type UserWhereUniqueInput needs at least one argument. Availab

Note: Lines with ? are optional.

2023-04-03 09:13:21 error: uncaughtException: swaggerJSDoc is not defined
ReferenceError: swaggerJSDoc is not defined
at App.initializeSwagger (file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/app.js:117:19)
at new App (file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/app.js:31:10)
at file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/server.js:8:13
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
at async loadESM (node:internal/process/esm_loader:91:5)
at async handleMainPromise (node:internal/modules/run_main:65:12)
2023-04-03 09:31:26 error: uncaughtException: Invalid URL
TypeError [ERR_INVALID_URL]: Invalid URL
at new NodeError (node:internal/errors:393:5)
at URL.onParseError (node:internal/url:565:9)
at new URL (node:internal/url:645:5)
at new URL (node:internal/url:642:22)
at App.initializeSwagger (file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/app.js:109:41)
at new App (file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/app.js:33:10)
at file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/server.js:8:13
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
2023-04-03 09:31:31 error: uncaughtException: Invalid URL
TypeError [ERR_INVALID_URL]: Invalid URL
at new NodeError (node:internal/errors:393:5)
at URL.onParseError (node:internal/url:565:9)
at new URL (node:internal/url:645:5)
at new URL (node:internal/url:642:22)
at App.initializeSwagger (file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/app.js:109:41)
at new App (file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/app.js:33:10)
at file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/server.js:8:13
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
2023-04-03 09:31:40 error: uncaughtException: Invalid URL
TypeError [ERR_INVALID_URL]: Invalid URL
at new NodeError (node:internal/errors:393:5)
at URL.onParseError (node:internal/url:565:9)
at new URL (node:internal/url:645:5)
at new URL (node:internal/url:642:22)
at App.initializeSwagger (file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/app.js:109:41)
at new App (file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/app.js:33:10)
at file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/server.js:8:13
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
2023-04-03 09:50:51 error: uncaughtException: EISDIR: illegal operation on a directory, read
Error: EISDIR: illegal operation on a directory, read
at Object.readSync (node:fs:748:3)
at tryReadSync (node:fs:448:20)
at Object.readFileSync (node:fs:494:19)
at extractAnnotations (/Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/node_modules/swagger-jsdoc/src/utils.js:54:26)
at build (/Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/node_modules/swagger-jsdoc/src/specification.js:200:9)
at module.exports (/Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/node_modules/swagger-jsdoc/src/lib.js:32:10)
at App.initializeSwagger (file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/app.js:125:19)
at new App (file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/app.js:33:10)
at file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/server.js:8:13
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
2023-04-03 09:50:56 error: uncaughtException: Cannot read properties of undefined (reading 'schemas')
TypeError: Cannot read properties of undefined (reading 'schemas')
at organize (/Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/node_modules/swagger-jsdoc/src/specification.js:157:32)
at build (/Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/node_modules/swagger-jsdoc/src/specification.js:320:7)
at module.exports (/Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/node_modules/swagger-jsdoc/src/lib.js:32:10)
at App.initializeSwagger (file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/app.js:125:19)
at new App (file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/app.js:33:10)
at file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/server.js:8:13
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
at async loadESM (node:internal/process/esm_loader:91:5)
2023-04-03 09:52:09 error: uncaughtException: Cannot read properties of undefined (reading 'schemas')
TypeError: Cannot read properties of undefined (reading 'schemas')
at organize (/Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/node_modules/swagger-jsdoc/src/specification.js:157:32)
at build (/Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/node_modules/swagger-jsdoc/src/specification.js:320:7)
at module.exports (/Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/node_modules/swagger-jsdoc/src/lib.js:32:10)
at App.initializeSwagger (file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/app.js:125:19)
at new App (file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/app.js:33:10)
at file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/server.js:8:13
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
at async loadESM (node:internal/process/esm_loader:91:5)
2023-04-03 09:52:32 error: uncaughtException: Cannot read properties of undefined (reading 'schemas')
TypeError: Cannot read properties of undefined (reading 'schemas')
at organize (/Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/node_modules/swagger-jsdoc/src/specification.js:157:32)
at build (/Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/node_modules/swagger-jsdoc/src/specification.js:320:7)
at module.exports (/Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/node_modules/swagger-jsdoc/src/lib.js:32:10)
at App.initializeSwagger (file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/app.js:125:19)
at new App (file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/app.js:33:10)
at file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/server.js:8:13
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
at async loadESM (node:internal/process/esm_loader:91:5)
2023-04-03 09:55:17 error: uncaughtException: EISDIR: illegal operation on a directory, read
Error: EISDIR: illegal operation on a directory, read
at Object.readSync (node:fs:748:3)
at tryReadSync (node:fs:448:20)
at Object.readFileSync (node:fs:494:19)
at extractAnnotations (/Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/node_modules/swagger-jsdoc/src/utils.js:54:26)
at build (/Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/node_modules/swagger-jsdoc/src/specification.js:200:9)
at module.exports (/Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/node_modules/swagger-jsdoc/src/lib.js:32:10)
at App.initializeSwagger (file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/app.js:124:19)
at new App (file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/app.js:33:10)
at file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/server.js:8:13
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
2023-04-03 10:10:40 error: uncaughtException: EISDIR: illegal operation on a directory, read
Error: EISDIR: illegal operation on a directory, read
at Object.readSync (node:fs:748:3)
at tryReadSync (node:fs:448:20)
at Object.readFileSync (node:fs:494:19)
at extractAnnotations (/Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/node_modules/swagger-jsdoc/src/utils.js:54:26)
at build (/Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/node_modules/swagger-jsdoc/src/specification.js:200:9)
at module.exports (/Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/node_modules/swagger-jsdoc/src/lib.js:32:10)
at App.initializeSwagger (file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/app.js:134:19)
at new App (file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/app.js:33:10)
at file:///Users/owenroth/Desktop/LPL/projects/veritext-replacement-template/be/server.js:8:13
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
59 changes: 59 additions & 0 deletions be/src/routes/user.routes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,62 @@
/**
* @swagger
* components:
* schemas:
* User:
* type: object
* required:
* - email
* properties:
* id:
* type: string
* description: The auto-generated id of the book
* email:
* type: string
* description: users email
* admin:
* type: boolean
* description: if the user is an admin
* activities:
* type: array
* description: The activities the user has completed
* createdAt:
* type: string
* format: date
* description: The date the user was added
* example:
* id: d5fE_asz
* email: [email protected]
* admin: true
* activities: [1, 2, 3]
* createdAt: 2020-03-10T04:05:06.157Z
*/
/**
* @swagger
* tags:
* name: Users
* description: Users management and retrieval
* /users:
* get:
* summary: Get all users
* tags: [Users]
* requestBody:
* required: false
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/User'
* responses:
* 200:
* description: Users Retrieved.
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/User'
* 500:
* description: Some server error
*
*/

import { Router } from 'express'
import { UsersController } from '../controllers/users.controller.js'

Expand Down
123 changes: 123 additions & 0 deletions be/swagger.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
tags:
- name: users
description: users API

paths:
# [GET] users
/users:
get:
tags:
- users
summary: Find All Users
responses:
200:
description: 'OK'
500:
description: 'Server Error'

# [POST] users
post:
tags:
- users
summary: Add User
parameters:
- name: body
in: body
description: user Data
required: true
schema:
$ref: '#/definitions/users'
responses:
201:
description: 'Created'
400:
description: 'Bad Request'
409:
description: 'Conflict'
500:
description: 'Server Error'

# [GET] users/id
/users/{id}:
get:
tags:
- users
summary: Find User By Id
parameters:
- name: id
in: path
description: User Id
required: true
type: integer
responses:
200:
description: 'OK'
409:
description: 'Conflict'
500:
description: 'Server Error'

# [PUT] users/id
put:
tags:
- users
summary: Update User By Id
parameters:
- name: id
in: path
description: user Id
required: true
type: integer
- name: body
in: body
description: user Data
required: true
schema:
$ref: '#/definitions/users'
responses:
200:
description: 'OK'
400:
description: 'Bad Request'
409:
description: 'Conflict'
500:
description: 'Server Error'

# [DELETE] users/id
delete:
tags:
- users
summary: Delete User By Id
parameters:
- name: id
in: path
description: user Id
required: true
type: integer
responses:
200:
description: 'OK'
409:
description: 'Conflict'
500:
description: 'Server Error'

# definitions
definitions:
users:
type: object
required:
- email
- password
properties:
email:
type: string
description: user Email
password:
type: string
description: user Password

schemes:
- https
- http
Loading

0 comments on commit 8ed19fa

Please sign in to comment.