ChengLei Shao 79f14d9024
chore: main datasource api (#3880)
* chore: main datasource api

* chore: test

* chore: console.log

* chore: middleware order

* chore: test

* chore: middleware order

* chore: test

* chore: middleware options
2024-03-31 16:22:45 +08:00

64 lines
1.2 KiB
TypeScript

import { Context, Next } from '@nocobase/actions';
import stream from 'stream';
export function dataWrapping() {
return async function dataWrapping(ctx: Context, next: Next) {
await next();
if (ctx.withoutDataWrapping) {
return;
}
if (ctx.body instanceof stream.Readable) {
return;
}
if (ctx.body instanceof Buffer) {
return;
}
if (!ctx.body) {
if (ctx.action?.actionName == 'get') {
ctx.status = 200;
}
}
if (Array.isArray(ctx.body)) {
ctx.body = {
data: ctx.body,
};
} else {
if (ctx.body) {
const { rows, ...meta } = ctx.body;
if (rows) {
ctx.body = {
data: rows,
meta,
};
} else {
ctx.body = {
data: ctx.body,
};
if (ctx.bodyMeta) {
ctx.body.meta = ctx.bodyMeta;
}
}
} else if (ctx.action) {
ctx.body = {
data: ctx.body,
};
}
}
if (ctx.body && ctx.state.messages?.length) {
ctx.body.messages = ctx.state.messages;
}
ctx.dataWrapped = true;
};
}
export default dataWrapping;