|
| 1 | +import path from 'path'; |
| 2 | +import pluralize from 'pluralize'; |
| 3 | +import m2s from 'mongoose-to-swagger'; |
| 4 | +import kebabCase from 'lodash/kebabCase'; |
| 5 | +import swaggerUi from 'swagger-ui-express'; |
| 6 | +import { REQUEST_TYPES } from 'api/customApisMapper'; |
| 7 | +import Pack from '../../package.json'; |
| 8 | +import { getModelFiles } from '.'; |
| 9 | +import customSwaggerDoc from '../../swagger.json'; |
| 10 | + |
| 11 | +export const REQUEST_METHODS = { |
| 12 | + [REQUEST_TYPES.create]: 'post', |
| 13 | + [REQUEST_TYPES.update]: 'patch', |
| 14 | + [REQUEST_TYPES.fetchOne]: 'get', |
| 15 | + [REQUEST_TYPES.fetchAll]: 'get', |
| 16 | + [REQUEST_TYPES.remove]: 'delete' |
| 17 | +}; |
| 18 | + |
| 19 | +export const DEFAULT_DEFINITIONS = { |
| 20 | + deleteResponse: { |
| 21 | + type: 'object', |
| 22 | + properties: { |
| 23 | + deletedCount: { |
| 24 | + type: 'integer', |
| 25 | + format: 'int64', |
| 26 | + example: 1 |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | +}; |
| 31 | + |
| 32 | +/** |
| 33 | + * @typedef CustomSwagger |
| 34 | + * @type {object} |
| 35 | + * @property {Array|object} tags |
| 36 | + * @property {object} paths |
| 37 | + * @property {object} definitions |
| 38 | + */ |
| 39 | + |
| 40 | +/** |
| 41 | + * |
| 42 | + * @param {any} app |
| 43 | + * @param {CustomSwagger} customSwagger |
| 44 | + */ |
| 45 | +export const registerSwagger = app => { |
| 46 | + const SWAGGER_DOCS_PATH = '/api-docs/swagger.json'; |
| 47 | + const options = { |
| 48 | + swaggerOptions: { |
| 49 | + url: SWAGGER_DOCS_PATH |
| 50 | + } |
| 51 | + }; |
| 52 | + const swaggerDocument = generateSwaggerDoc(); |
| 53 | + appendToSwaggerDoc(swaggerDocument, customSwaggerDoc); |
| 54 | + app.get(SWAGGER_DOCS_PATH, (_, res) => res.json(swaggerDocument)); |
| 55 | + app.use( |
| 56 | + '/api-docs', |
| 57 | + swaggerUi.serveFiles(null, options), |
| 58 | + swaggerUi.setup(null, options) |
| 59 | + ); |
| 60 | +}; |
| 61 | + |
| 62 | +export const generateSwaggerDoc = () => { |
| 63 | + const swaggerDocument = { |
| 64 | + swagger: '2.0', |
| 65 | + info: { |
| 66 | + title: 'Parcel Node Mongo Express Documentation', |
| 67 | + version: Pack.version |
| 68 | + }, |
| 69 | + tags: [], |
| 70 | + paths: {}, |
| 71 | + definitions: {} |
| 72 | + }; |
| 73 | + const modelsFolderPath = path.join(__dirname, '../../models/'); |
| 74 | + const fileArray = getModelFiles(modelsFolderPath); |
| 75 | + fileArray.forEach(f => { |
| 76 | + const { model } = require(modelsFolderPath + f); |
| 77 | + const name = f.split('.')[0]; |
| 78 | + |
| 79 | + const { swaggerPaths, swaggerDefs } = swagGeneratorFactory(name, model); |
| 80 | + appendToSwaggerDoc(swaggerDocument, { |
| 81 | + paths: swaggerPaths, |
| 82 | + definitions: swaggerDefs, |
| 83 | + tags: { |
| 84 | + name, |
| 85 | + description: `${name} related endpoints` |
| 86 | + } |
| 87 | + }); |
| 88 | + }); |
| 89 | + return swaggerDocument; |
| 90 | +}; |
| 91 | + |
| 92 | +/** |
| 93 | + * |
| 94 | + * @param {any} swaggerDocument |
| 95 | + * @param {CustomSwagger} swaggerData |
| 96 | + */ |
| 97 | +export const appendToSwaggerDoc = (swaggerDocument, swaggerData) => { |
| 98 | + const { paths, definitions, tags } = swaggerData; |
| 99 | + if (Array.isArray(tags)) { |
| 100 | + swaggerDocument.tags.push(...tags); |
| 101 | + } else { |
| 102 | + swaggerDocument.tags.push(tags); |
| 103 | + } |
| 104 | + swaggerDocument.paths = { |
| 105 | + ...swaggerDocument.paths, |
| 106 | + ...paths |
| 107 | + }; |
| 108 | + swaggerDocument.definitions = { |
| 109 | + ...swaggerDocument.definitions, |
| 110 | + ...definitions |
| 111 | + }; |
| 112 | +}; |
| 113 | + |
| 114 | +export const swagGeneratorFactory = (name, model) => { |
| 115 | + const swaggerPaths = {}; |
| 116 | + const swaggerDefs = { |
| 117 | + ...DEFAULT_DEFINITIONS |
| 118 | + }; |
| 119 | + appendSwagDefs(name, model, swaggerDefs); |
| 120 | + Object.values(REQUEST_TYPES).forEach(type => |
| 121 | + appendSwagPaths(type, name, swaggerPaths) |
| 122 | + ); |
| 123 | + return { swaggerPaths, swaggerDefs }; |
| 124 | +}; |
| 125 | + |
| 126 | +export const appendSwagPaths = (type, name, swaggerPaths) => { |
| 127 | + if (type === REQUEST_TYPES.create && name === 'orders') { |
| 128 | + return; |
| 129 | + } |
| 130 | + const routeName = `/${kebabCase(name)}`; |
| 131 | + const method = REQUEST_METHODS[type]; |
| 132 | + const lowerType = type.toLowerCase(); |
| 133 | + const isPluralEnity = type === REQUEST_TYPES.fetchAll; |
| 134 | + const hasPathParam = ![ |
| 135 | + REQUEST_TYPES.create, |
| 136 | + REQUEST_TYPES.fetchAll |
| 137 | + ].includes(type); |
| 138 | + const entityName = isPluralEnity ? name : pluralize.singular(name); |
| 139 | + const summary = `${lowerType} ${entityName}`; |
| 140 | + const parameters = hasPathParam |
| 141 | + ? [ |
| 142 | + { |
| 143 | + name: '_id', |
| 144 | + in: 'path', |
| 145 | + description: `ID of ${pluralize.singular( |
| 146 | + name |
| 147 | + )} to ${lowerType}`, |
| 148 | + required: true, |
| 149 | + type: 'string' |
| 150 | + } |
| 151 | + ] |
| 152 | + : {}; |
| 153 | + const responses = { |
| 154 | + 200: { |
| 155 | + type: 'object', |
| 156 | + description: `${lowerType} ${entityName} is success`, |
| 157 | + schema: { |
| 158 | + type: 'object', |
| 159 | + properties: { |
| 160 | + data: isPluralEnity |
| 161 | + ? { |
| 162 | + type: 'array', |
| 163 | + items: { $ref: `#/definitions/${name}` } |
| 164 | + } |
| 165 | + : type === REQUEST_TYPES.remove |
| 166 | + ? { $ref: '#/definitions/deleteResponse' } |
| 167 | + : { $ref: `#/definitions/${name}` } |
| 168 | + } |
| 169 | + } |
| 170 | + }, |
| 171 | + 400: { |
| 172 | + type: 'object', |
| 173 | + description: `${lowerType} ${entityName} is failed`, |
| 174 | + schema: { |
| 175 | + type: 'object', |
| 176 | + required: ['error'], |
| 177 | + properties: { |
| 178 | + error: { |
| 179 | + type: 'string', |
| 180 | + example: `unable to ${lowerType} ${entityName}` |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + }; |
| 186 | + const pathKey = !hasPathParam ? routeName : `${routeName}/{_id}`; |
| 187 | + swaggerPaths[pathKey] = { |
| 188 | + ...(swaggerPaths[pathKey] || {}), |
| 189 | + [method]: { |
| 190 | + tags: [name], |
| 191 | + summary, |
| 192 | + produces: ['application/json'], |
| 193 | + parameters, |
| 194 | + responses |
| 195 | + } |
| 196 | + }; |
| 197 | +}; |
| 198 | + |
| 199 | +export const appendSwagDefs = (name, model, swaggerDefs) => { |
| 200 | + const modelSchema = m2s(model); |
| 201 | + // modify model schema properties here |
| 202 | + if (modelSchema.properties.purchasedProducts) { |
| 203 | + modelSchema.properties.purchasedProducts = { |
| 204 | + $ref: '#/definitions/products' |
| 205 | + }; |
| 206 | + } |
| 207 | + swaggerDefs[name] = { |
| 208 | + type: 'object', |
| 209 | + ...modelSchema, |
| 210 | + title: undefined |
| 211 | + }; |
| 212 | +}; |
0 commit comments