zod-server-actions
Version:
Simple utility library to create server actions in Next.js
25 lines (24 loc) • 842 B
JavaScript
import { makeResponseError, makeResponseSuccess } from "./response";
export class Query {
constructor() { }
static create(props) {
return async function () {
try {
const data = await props.cb(props.config.context);
if (props.validator.outputSchema) {
const outputData = props.validator.parseOutput(data);
return makeResponseSuccess(outputData);
}
return makeResponseSuccess(data);
}
catch (error) {
if (!props.config)
return makeResponseError(error);
if (props.config.onError) {
await props.config.onError(error);
}
return makeResponseError(error);
}
};
}
}