diff --git a/packages/actions/src/__tests__/middleware.test.ts b/packages/actions/src/__tests__/middleware.test.ts index f83c32a95..b61998220 100644 --- a/packages/actions/src/__tests__/middleware.test.ts +++ b/packages/actions/src/__tests__/middleware.test.ts @@ -1,6 +1,6 @@ import actions from '..'; import { Context } from '../actions'; -import jsonReponse from '../middlewares/json-reponse'; +import { dataWrapping } from '../middlewares'; import { initDatabase, agent, resourcer } from './index'; describe('list', () => { @@ -10,7 +10,7 @@ describe('list', () => { resourcer.define({ name: 'articles', middlewares: [ - jsonReponse, + dataWrapping, ], actions: actions.common, }); diff --git a/packages/actions/src/middleware.ts b/packages/actions/src/middleware.ts deleted file mode 100644 index f9c17e479..000000000 --- a/packages/actions/src/middleware.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { Context, Next } from './actions'; -import { Action } from '@nocobase/resourcer'; - -export async function middleware(ctx: Context, next: Next) { - await next(); - if (ctx.action instanceof Action) { - const { rows, ...meta } = ctx.body as any; - if (rows) { - ctx.body = { - data: rows, - meta, - }; - } else { - ctx.body = { - data: ctx.body, - }; - } - } -} - -export default middleware; diff --git a/packages/actions/src/middlewares/data-wrapping.ts b/packages/actions/src/middlewares/data-wrapping.ts new file mode 100644 index 000000000..3a64432dd --- /dev/null +++ b/packages/actions/src/middlewares/data-wrapping.ts @@ -0,0 +1,31 @@ +import { Context, Next } from '../actions'; +import { Action } from '@nocobase/resourcer'; + +export async function dataWrapping(ctx: Context, next: Next) { + await next(); + if (!(ctx.action instanceof Action)) { + return; + } + if (ctx.withoutDataWrapping) { + return; + } + if (ctx.body instanceof Buffer) { + return; + } + if (!ctx.body) { + ctx.body = {}; + } + const { rows, ...meta } = ctx.body; + if (rows) { + ctx.body = { + data: rows, + meta, + }; + } else { + ctx.body = { + data: ctx.body, + }; + } +} + +export default dataWrapping; diff --git a/packages/actions/src/middlewares/index.ts b/packages/actions/src/middlewares/index.ts index 782137c25..6455ad0be 100644 --- a/packages/actions/src/middlewares/index.ts +++ b/packages/actions/src/middlewares/index.ts @@ -1,2 +1,2 @@ export * from './associated'; -export * from './json-reponse'; \ No newline at end of file +export * from './data-wrapping'; diff --git a/packages/actions/src/middlewares/json-reponse.ts b/packages/actions/src/middlewares/json-reponse.ts deleted file mode 100644 index f22885d2f..000000000 --- a/packages/actions/src/middlewares/json-reponse.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { Context, Next } from '../actions'; -import { Action } from '@nocobase/resourcer'; - -export async function jsonResponse(ctx: Context, next: Next) { - await next(); - if (ctx.action instanceof Action) { - const { rows, ...meta } = ctx.body as any; - if (rows) { - ctx.body = { - data: rows, - meta, - }; - } else { - ctx.body = { - data: ctx.body, - }; - } - } -} - -export default jsonResponse; diff --git a/packages/api/src/app.ts b/packages/api/src/app.ts index c3fc688b9..747595b10 100644 --- a/packages/api/src/app.ts +++ b/packages/api/src/app.ts @@ -1,8 +1,8 @@ import path from 'path'; import dotenv from 'dotenv'; -import Api from '@nocobase/server'; -import actions from '@nocobase/actions'; -import { middlewares } from '@nocobase/actions'; +import Api from '@nocobase/server/src'; +import actions from '@nocobase/actions/src'; +import { middlewares } from '@nocobase/actions/src'; // @ts-ignore const sync = global.sync || { @@ -45,9 +45,6 @@ const api = Api.create({ }, }); -api.resourcer.use(middlewares.associated); -api.resourcer.registerActionHandlers({ ...actions.common, ...actions.associate }); - const plugins = [ '@nocobase/plugin-collections', '@nocobase/plugin-action-logs', diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index 930e6e021..0f88c23bd 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -1,5 +1,4 @@ -import Resourcer, { Action } from '@nocobase/resourcer'; -import actions from '@nocobase/actions'; +import { actions, middlewares } from '@nocobase/actions'; import Application from './application'; import bodyParser from 'koa-bodyparser'; import cors from '@koa/cors'; @@ -22,8 +21,7 @@ export default { app.use(bodyParser()); app.use(cors()); - // 这段代码处理的不完整 - app.resourcer.registerActionHandlers(actions.common); + app.resourcer.registerActionHandlers({ ...actions.common, ...actions.associate }); app.use(async (ctx, next) => { ctx.db = app.database; @@ -31,25 +29,8 @@ export default { await next(); }); - app.use(async (ctx, next) => { - await next(); - if (ctx.action instanceof Action) { - if (!ctx.body) { - ctx.body = {}; - } - const { rows, ...meta } = ctx.body||{}; - if (rows) { - ctx.body = { - data: rows, - meta, - }; - } else { - ctx.body = { - data: ctx.body, - }; - } - } - }); + app.resourcer.use(middlewares.associated); + app.use(middlewares.dataWrapping); app.use(middleware({ database: app.database,