Skip to content

Commit

Permalink
Build swagger.json as part of webpack.make.js when running npm run bu…
Browse files Browse the repository at this point in the history
…ild. Currently using hardcoded path that works only on my computer (WIP) (#535)
  • Loading branch information
tschaffter committed Sep 24, 2019
1 parent 34a388d commit a534dc0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 67 deletions.
8 changes: 8 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"serve-favicon": "^2.5.0",
"slugify": "^1.3.5",
"swagger-jsdoc": "^3.4.0",
"swagger-jsdoc-webpack-plugin": "^1.0.0",
"swagger-ui-express": "^4.1.1",
"ws": "^7.1.2"
},
Expand Down
66 changes: 2 additions & 64 deletions server/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@

import errors from './components/errors';
import path from 'path';
import config from './config/environment';

const swaggerUi = require('swagger-ui-express');
var swaggerJSDoc = require('swagger-jsdoc');
// const swaggerDocument = require('./swagger/swagger.json');
// const YAML = require('yamljs');
// const swaggerDocument = YAML.load(path.join(__dirname, './swagger/swagger.yaml'));
import swaggerUi from 'swagger-ui-express';

export default app => {
// Insert routes below
Expand All @@ -32,71 +26,15 @@ export default app => {
app.use('/api/provenance', require('./api/provenance'));
app.use('/auth', require('./auth').default);

console.log('domain', config.domain);

// swagger definition
// maps to https://swagger.io/specification/#oasObject
var swaggerDefinition = {
openapi: '3.0.2',
info: {
title: 'PHC Collaboration Portal API',
version: '1.0.0', // TODO Get version from config
description: `Personalized Health Care (PHC) Collaboration Portal
developed by Sage Bionetworks and Roche/Genentech`,
contact: {
name: 'API Support',
url: 'https://www.synapse.org',
email: '[email protected]'
},
license: {
name: 'CC BY-NC 3.0',
url: 'https://creativecommons.org/licenses/by-nc/3.0/'
}
},
servers: [{
url: `${config.domain}/api`,
description: 'This app'
}],
components: {
securitySchemes: {
BearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT'
}
},
responses: {
UnauthorizedError: {
description: 'Access token is missing or invalid'
}
}
}
};

const swaggerOptions = {
customSiteTitle: 'PHC Collaboration Portal API',
customCss: '.topbar { display: none }',
// docExpansion: 'none'
};

// options for the swagger docs
var swaggerJSDocOptions = {
swaggerDefinition: swaggerDefinition,
apis: [
'./**/api/**/index.js',
'./**/auth/**/*.js',
'./shared/interfaces/**/*.ts'
// './**/swagger/**/*.ts'
]
};

// initialize swagger-jsdoc
const swaggerSpec = swaggerJSDoc(swaggerJSDocOptions);
const swaggerSpec = require('/Users/tschaffter/dev/PHCCollaborationPortal/dist/client/swagger.json');
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec, swaggerOptions));

// Swagger
// app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));

// All undefined asset or api routes should return a 404
app.route('/:url(api|auth|components|app|bower_components|assets|api-docs)/*')
.get(errors[404]);
Expand Down
51 changes: 48 additions & 3 deletions webpack.make.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const path = require('path');
const TerserPlugin = require('terser-webpack-plugin');
const webpack = require('webpack');
var GitRevisionPlugin = require('git-revision-webpack-plugin');
const GitRevisionPlugin = require('git-revision-webpack-plugin');
const MomentLocalesPlugin = require('moment-locales-webpack-plugin');
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const SwaggerJSDocWebpackPlugin = require('swagger-jsdoc-webpack-plugin');
const fs = require('fs');

module.exports = function makeWebpackConfig(options) {
Expand Down Expand Up @@ -315,10 +316,54 @@ module.exports = function makeWebpackConfig(options) {
// ("en" is built into Moment and can’t be removed)
new MomentLocalesPlugin({
// localesToKeep: ['fr'],
})
}),
// new BundleAnalyzerPlugin({
// generateStatsFile: true,
// }),

// https://github.com/patsimm/swagger-jsdoc-webpack-plugin
// This writes a swagger.json file to ./dist/client/swagger.json.
new SwaggerJSDocWebpackPlugin({
swaggerDefinition: {
openapi: '3.0.2',
info: {
title: 'PHC Collaboration Portal API',
version: '1.0.0', // TODO Get version from config
description: `Personalized Health Care (PHC) Collaboration Portal
developed by Sage Bionetworks and Roche/Genentech`,
contact: {
name: 'API Support',
url: 'https://www.synapse.org',
email: '[email protected]',
},
license: {
name: 'CC BY-NC 3.0',
url: 'https://creativecommons.org/licenses/by-nc/3.0/',
},
},
servers: [
{
url: 'plop/api', // `${config.domain}/api`,
description: 'This app',
},
],
components: {
securitySchemes: {
BearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
},
responses: {
UnauthorizedError: {
description: 'Access token is missing or invalid',
},
},
},
},
apis: ['./**/api/**/index.js', './**/auth/**/*.js', './shared/interfaces/**/*.ts'],
})
);
}

Expand Down

0 comments on commit a534dc0

Please sign in to comment.