tachybase_todo/packages/plugins/@nocobase/plugin-api-doc/src/server/server.ts
chenos c4aa8b78c2
chore: add tsdoc (#3788)
* chore: tsdoc

* chore: tsdoc

* fix: error

* chore: code format

* chore: code format
2024-03-26 17:08:45 +08:00

51 lines
1.5 KiB
TypeScript

import { Context } from '@nocobase/actions';
import { Plugin } from '@nocobase/server';
import { SwaggerManager } from './swagger';
export class PluginAPIDocServer extends Plugin {
swagger: SwaggerManager;
constructor(app, options) {
super(app, options);
this.swagger = new SwaggerManager(this);
}
async beforeLoad() {}
async load() {
this.app.resourcer.define({
name: 'swagger',
type: 'single',
actions: {
getUrls: async (ctx: Context, next) => {
// ctx.withoutDataWrapping = true;
ctx.body = await this.swagger.getUrls();
await next();
},
get: async (ctx: Context, next) => {
ctx.withoutDataWrapping = true;
const { ns } = ctx.action.params;
if (!ns) {
ctx.body = await this.swagger.getSwagger();
return;
}
const [type, index] = ns.split('/');
if (type === 'core') {
ctx.body = await this.swagger.getCoreSwagger();
} else if (type === 'plugins') {
ctx.body = await this.swagger.getPluginsSwagger(index);
} else if (type === 'collections') {
ctx.body = await this.swagger.getCollectionsSwagger(index);
}
await next();
},
},
only: ['get', 'getUrls'],
});
this.app.acl.allow('swagger', ['get', 'getUrls'], 'loggedIn');
this.app.acl.registerSnippet({
name: ['pm', this.name, 'documentation'].join('.'),
actions: ['swagger:*'],
});
}
}
export default PluginAPIDocServer;