ChengLei Shao 5df11c58c7
fix: 204 no content response (#378)
* fix: 204 no content response

* Update data-wrapping.ts

* Update plugin.ts

Co-authored-by: chenos <chenlinxh@gmail.com>
2022-05-22 14:48:50 +08:00

41 lines
679 B
TypeScript

import { Context, Next } from '@nocobase/actions';
export function dataWrapping() {
return async function dataWrapping(ctx: Context, next: Next) {
await next();
if (ctx.withoutDataWrapping) {
return;
}
if (!ctx?.action?.params) {
return;
}
if (ctx.body instanceof Buffer) {
return;
}
if (!ctx.body) {
if (ctx.action.actionName == 'get') {
ctx.status = 404;
}
}
const { rows, ...meta } = ctx.body || {};
if (rows) {
ctx.body = {
data: rows,
meta,
};
} else {
ctx.body = {
data: ctx.body,
};
}
};
}
export default dataWrapping;