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 { 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,
});

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 './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 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',

View File

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