From 7fd126d6a3caa6af633035881e03e13e3070286a Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Sun, 27 Aug 2023 15:14:34 +0800 Subject: [PATCH] chore(api-doc): view collection api doc --- .../collections/paths/associations/index.ts | 5 + .../swagger/collections/paths/collection.ts | 13 +- .../server/swagger/collections/paths/index.ts | 10 +- .../collection-manager/src/swagger/index.ts | 273 ++++++++++++++++++ 4 files changed, 296 insertions(+), 5 deletions(-) create mode 100644 packages/plugins/collection-manager/src/swagger/index.ts diff --git a/packages/plugins/api-doc/src/server/swagger/collections/paths/associations/index.ts b/packages/plugins/api-doc/src/server/swagger/collections/paths/associations/index.ts index 39b975c0c..763ed8565 100644 --- a/packages/plugins/api-doc/src/server/swagger/collections/paths/associations/index.ts +++ b/packages/plugins/api-doc/src/server/swagger/collections/paths/associations/index.ts @@ -8,6 +8,7 @@ import { } from '@nocobase/database'; import multipleAssociation from './multiple-association'; import singleAssociation from './single-association'; +import { isViewCollection } from '..'; export default (collection: Collection) => { return associationFields(collection) @@ -27,6 +28,10 @@ export default (collection: Collection) => { }; export function associationFields(collection: Collection): Array { + if (isViewCollection(collection)) { + return []; + } + return Array.from(collection.fields.values()) .filter((field) => field instanceof RelationField) .filter((field) => field.name !== 'createdBy' && field.name !== 'updatedBy'); diff --git a/packages/plugins/api-doc/src/server/swagger/collections/paths/collection.ts b/packages/plugins/api-doc/src/server/swagger/collections/paths/collection.ts index 249794377..487da1c50 100644 --- a/packages/plugins/api-doc/src/server/swagger/collections/paths/collection.ts +++ b/packages/plugins/api-doc/src/server/swagger/collections/paths/collection.ts @@ -1,5 +1,5 @@ import { Collection, RelationField } from '@nocobase/database'; -import { hasSortField } from './index'; +import { hasSortField, readOnlyCollection } from './index'; type TemplateOptions = { collection: Collection; @@ -327,11 +327,16 @@ export default (collection: Collection) => { const apiDoc: any = { [`/${collection.name}:list`]: ListActionTemplate(options), [`/${collection.name}:get`]: GetActionTemplate(options), - [`/${collection.name}:create`]: CreateActionTemplate(options), - [`/${collection.name}:update`]: UpdateActionTemplate(options), - [`/${collection.name}:destroy`]: DestroyActionTemplate(options), }; + if (!readOnlyCollection(collection)) { + Object.assign(apiDoc, { + [`/${collection.name}:create`]: CreateActionTemplate(options), + [`/${collection.name}:update`]: UpdateActionTemplate(options), + [`/${collection.name}:destroy`]: DestroyActionTemplate(options), + }); + } + if (hasSortField(collection)) { apiDoc[`/${collection.name}:move`] = MoveActionTemplate(options); } diff --git a/packages/plugins/api-doc/src/server/swagger/collections/paths/index.ts b/packages/plugins/api-doc/src/server/swagger/collections/paths/index.ts index b3be8cabb..39e66bf7d 100644 --- a/packages/plugins/api-doc/src/server/swagger/collections/paths/index.ts +++ b/packages/plugins/api-doc/src/server/swagger/collections/paths/index.ts @@ -5,7 +5,7 @@ import associations from './associations'; export default (collection: Collection, options) => { const paths = list(collection); - if (options.withAssociation) { + if (options.withAssociation && !isViewCollection(collection)) { Object.assign(paths, associations(collection)); } @@ -21,3 +21,11 @@ export function hasSortField(collection: Collection) { return false; } + +export function readOnlyCollection(collection: Collection) { + return isViewCollection(collection) && collection.options?.writableView == false; +} + +export function isViewCollection(collection: Collection) { + return collection.isView(); +} diff --git a/packages/plugins/collection-manager/src/swagger/index.ts b/packages/plugins/collection-manager/src/swagger/index.ts new file mode 100644 index 000000000..cd9df083a --- /dev/null +++ b/packages/plugins/collection-manager/src/swagger/index.ts @@ -0,0 +1,273 @@ +export default { + openapi: '3.0.2', + info: { + title: 'NocoBase API - Collection manager plugin', + }, + tags: [], + paths: { + '/collections:list': { + get: { + tags: ['collections'], + description: '', + parameters: [], + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + '/collections:get': { + get: { + tags: ['collections'], + description: '', + parameters: [], + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + '/collections:create': { + post: { + tags: ['collections'], + description: '', + parameters: [], + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + '/collections:update': { + post: { + tags: ['collections'], + description: '', + parameters: [], + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + '/collections:destroy': { + post: { + tags: ['collections'], + description: '', + parameters: [], + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + '/collections:move': { + post: { + tags: ['collections'], + description: '', + parameters: [], + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + '/collections:setFields': { + post: { + tags: ['collections'], + description: '', + parameters: [], + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + '/collections/{collectionName}/fields:get': { + get: { + tags: ['collections.fields'], + description: '', + parameters: [], + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + '/collections/{collectionName}/fields:list': { + get: { + tags: ['collections.fields'], + description: '', + parameters: [], + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + '/collections/{collectionName}/fields:create': { + post: { + tags: ['collections.fields'], + description: '', + parameters: [], + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + '/collections/{collectionName}/fields:update': { + post: { + tags: ['collections.fields'], + description: '', + parameters: [], + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + '/collections/{collectionName}/fields:destroy': { + post: { + tags: ['collections.fields'], + description: '', + parameters: [], + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + '/collections/{collectionName}/fields:move': { + post: { + tags: ['collections.fields'], + description: '', + parameters: [], + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + '/collectionCategories:list': { + post: { + tags: ['collectionCategories'], + description: '', + parameters: [], + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + '/collectionCategories:get': { + post: { + tags: ['collectionCategories'], + description: '', + parameters: [], + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + '/collectionCategories:create': { + post: { + tags: ['collectionCategories'], + description: '', + parameters: [], + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + '/collectionCategories:update': { + post: { + tags: ['collectionCategories'], + description: '', + parameters: [], + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + '/collectionCategories:destroy': { + post: { + tags: ['collectionCategories'], + description: '', + parameters: [], + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + '/collectionCategories:move': { + post: { + tags: ['collectionCategories'], + description: '', + parameters: [], + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + '/dbViews:get': { + get: { + tags: ['dbViews'], + description: '', + parameters: [], + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + '/dbViews:list': { + get: { + tags: ['dbViews'], + description: '', + parameters: [], + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + '/dbViews:query': { + get: { + tags: ['dbViews'], + description: '', + parameters: [], + responses: { + '200': { + description: 'OK', + }, + }, + }, + }, + }, +};