refactor: data wrapping middleware

This commit is contained in:
chenos 2021-04-01 23:51:00 +08:00
parent d7dbad1ba8
commit b1c1a5cee8
7 changed files with 41 additions and 74 deletions

View File

@ -1,6 +1,6 @@
import actions from '..'; import actions from '..';
import { Context } from '../actions'; import { Context } from '../actions';
import jsonReponse from '../middlewares/json-reponse'; import { dataWrapping } from '../middlewares';
import { initDatabase, agent, resourcer } from './index'; import { initDatabase, agent, resourcer } from './index';
describe('list', () => { describe('list', () => {
@ -10,7 +10,7 @@ describe('list', () => {
resourcer.define({ resourcer.define({
name: 'articles', name: 'articles',
middlewares: [ middlewares: [
jsonReponse, dataWrapping,
], ],
actions: actions.common, actions: actions.common,
}); });

View File

@ -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;

View File

@ -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;

View File

@ -1,2 +1,2 @@
export * from './associated'; export * from './associated';
export * from './json-reponse'; export * from './data-wrapping';

View File

@ -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;

View File

@ -1,8 +1,8 @@
import path from 'path'; import path from 'path';
import dotenv from 'dotenv'; import dotenv from 'dotenv';
import Api from '@nocobase/server'; import Api from '@nocobase/server/src';
import actions from '@nocobase/actions'; import actions from '@nocobase/actions/src';
import { middlewares } from '@nocobase/actions'; import { middlewares } from '@nocobase/actions/src';
// @ts-ignore // @ts-ignore
const sync = global.sync || { 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 = [ const plugins = [
'@nocobase/plugin-collections', '@nocobase/plugin-collections',
'@nocobase/plugin-action-logs', '@nocobase/plugin-action-logs',

View File

@ -1,5 +1,4 @@
import Resourcer, { Action } from '@nocobase/resourcer'; import { actions, middlewares } from '@nocobase/actions';
import actions from '@nocobase/actions';
import Application from './application'; import Application from './application';
import bodyParser from 'koa-bodyparser'; import bodyParser from 'koa-bodyparser';
import cors from '@koa/cors'; import cors from '@koa/cors';
@ -22,8 +21,7 @@ export default {
app.use(bodyParser()); app.use(bodyParser());
app.use(cors()); app.use(cors());
// 这段代码处理的不完整 app.resourcer.registerActionHandlers({ ...actions.common, ...actions.associate });
app.resourcer.registerActionHandlers(actions.common);
app.use(async (ctx, next) => { app.use(async (ctx, next) => {
ctx.db = app.database; ctx.db = app.database;
@ -31,25 +29,8 @@ export default {
await next(); await next();
}); });
app.use(async (ctx, next) => { app.resourcer.use(middlewares.associated);
await next(); app.use(middlewares.dataWrapping);
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.use(middleware({ app.use(middleware({
database: app.database, database: app.database,