Skip to content

Commit

Permalink
feat(documentation): expose full_documentation.json under /documentat…
Browse files Browse the repository at this point in the history
…ion/openapi.json route (strapi/documentation#2143)
  • Loading branch information
faessler committed Jul 24, 2024
1 parent 2700c7c commit 0b222a4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/plugins/documentation/server/controllers/documentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,36 @@ module.exports = {
}
},

async openapiJson(ctx) {
try {
const { major, minor, patch } = ctx.params;
const version =
major && minor && patch
? `${major}.${minor}.${patch}`
: strapi.plugin('documentation').service('documentation').getDocumentationVersion();

const openAPISpecsPath = path.join(
strapi.dirs.app.extensions,
'documentation',
'documentation',
version,
'full_documentation.json'
);

try {
const documentation = fs.readFileSync(openAPISpecsPath, 'utf8');
const response = JSON.parse(documentation);

ctx.send(response);
} catch (e) {
strapi.log.error(e);
ctx.badRequest(null, e.message);
}
} catch (e) {
strapi.log.error(e);
}
},

async loginView(ctx, next) {
// lazy require cheerio
const cheerio = require('cheerio');
Expand Down
18 changes: 18 additions & 0 deletions packages/plugins/documentation/server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ module.exports = [
middlewares: [restrictAccess],
},
},
{
method: 'GET',
path: '/openapi.json',
handler: 'documentation.openapiJson',
config: {
auth: false,
middlewares: [restrictAccess],
},
},
{
method: 'GET',
path: '/v:major(\\d+).:minor(\\d+).:patch(\\d+)/openapi.json',
handler: 'documentation.openapiJson',
config: {
auth: false,
middlewares: [restrictAccess],
},
},
{
method: 'GET',
path: '/login',
Expand Down
3 changes: 3 additions & 0 deletions packages/plugins/documentation/tests/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const handlers = [
})
);
}),
rest.post('*/openapi.json', (req, res, ctx) => {
return res(ctx.status(200));
}),
rest.post('*/regenerateDoc', (req, res, ctx) => {
return res(ctx.status(200));
}),
Expand Down

0 comments on commit 0b222a4

Please sign in to comment.