You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.6 KiB

11 months ago
  1. const fs = require('fs');
  2. const path = require('path');
  3. const swaggerUi = require('swagger-ui-express');
  4. function faviconUrl() {
  5. return process.env.NODE_ENV === "production" ?
  6. '/public/favicon.png' :
  7. 'http://localhost:3000/public/favicon.png'
  8. }
  9. function useSwagger(app) {
  10. app.use('/api/docs', swaggerUi.serve);
  11. const options = {
  12. customCss: [
  13. fs.readFileSync(path.resolve(__dirname, 'index.css')),
  14. fs.readFileSync(path.resolve(__dirname, 'dark-swagger.css'))
  15. ].join('\n\n\n'),
  16. customSiteTitle: 'AnythingLLM Developer API Documentation',
  17. customfavIcon: faviconUrl(),
  18. }
  19. if (process.env.NODE_ENV === "production") {
  20. const swaggerDocument = require('./openapi.json');
  21. app.get('/api/docs', swaggerUi.setup(
  22. swaggerDocument,
  23. {
  24. ...options,
  25. customJsStr: 'window.SWAGGER_DOCS_ENV = "production";\n\n' + fs.readFileSync(path.resolve(__dirname, 'index.js'), 'utf8'),
  26. },
  27. ));
  28. } else {
  29. // we regenerate the html page only in development mode to ensure it is up-to-date when the code is hot-reloaded.
  30. app.get(
  31. "/api/docs",
  32. async (_, response) => {
  33. // #swagger.ignore = true
  34. const swaggerDocument = require('./openapi.json');
  35. return response.send(
  36. swaggerUi.generateHTML(
  37. swaggerDocument,
  38. {
  39. ...options,
  40. customJsStr: 'window.SWAGGER_DOCS_ENV = "development";\n\n' + fs.readFileSync(path.resolve(__dirname, 'index.js'), 'utf8'),
  41. }
  42. )
  43. );
  44. }
  45. );
  46. }
  47. }
  48. module.exports = { faviconUrl, useSwagger }