tachybase_todo/packages/plugins/@nocobase/plugin-api-doc/src/server/server.ts
chenos 8217ebfb1b
feat: improve plugin manager process (#3386)
* feat: improve plugin manager process

* fix: skip help error

* fix: ipc check

* fix: improve remove

* fix: refresh

* fix: remove dir

* fix: improve code

* fix: update yarn.lock

* fix: e2e error

* fix: migration

* fix: pm create

* Revert "fix: migration"

This reverts commit 8f8fe04436ac96798259fb6debd88fffcb613560.

* fix: remove sample-hello
2024-01-18 00:33:15 +08:00

49 lines
1.5 KiB
TypeScript

import { Context } from '@nocobase/actions';
import { Plugin } from '@nocobase/server';
import { SwaggerManager } from './swagger';
export default class APIDoc extends Plugin {
swagger: SwaggerManager;
constructor(app, options) {
super(app, options);
this.swagger = new SwaggerManager(this);
}
async beforeLoad() {}
async load() {
this.app.resource({
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:*'],
});
}
}