mirror of
https://github.com/withastro/astro.git
synced 2025-01-06 22:10:10 -05:00
refactor: enhance -> acceptFormData
This commit is contained in:
parent
8ffa7a33df
commit
9a31940a43
2 changed files with 10 additions and 11 deletions
|
@ -8,34 +8,33 @@ type MaybePromise<T> = T | Promise<T>;
|
||||||
export function defineAction<TOutput, TInputSchema extends z.ZodType>({
|
export function defineAction<TOutput, TInputSchema extends z.ZodType>({
|
||||||
input: inputSchema,
|
input: inputSchema,
|
||||||
handler,
|
handler,
|
||||||
enhance,
|
acceptFormData,
|
||||||
}: {
|
}: {
|
||||||
input?: TInputSchema;
|
input?: TInputSchema;
|
||||||
handler: (input: z.infer<TInputSchema>, context: APIContext) => MaybePromise<TOutput>;
|
handler: (input: z.infer<TInputSchema>, context: APIContext) => MaybePromise<TOutput>;
|
||||||
enhance?: boolean;
|
acceptFormData?: boolean;
|
||||||
}): (input: z.input<TInputSchema>) => Promise<Awaited<TOutput>> {
|
}): (input: z.input<TInputSchema>) => Promise<Awaited<TOutput>> {
|
||||||
return async (unparsedInput): Promise<Awaited<TOutput>> => {
|
return async (unparsedInput): Promise<Awaited<TOutput>> => {
|
||||||
const context = ApiContextStorage.getStore()!;
|
const context = ApiContextStorage.getStore()!;
|
||||||
const ContentType = context.request.headers.get('content-type');
|
if (!acceptFormData && unparsedInput instanceof FormData) {
|
||||||
if (!enhance && (ContentType !== 'application/json' || unparsedInput instanceof FormData)) {
|
|
||||||
// TODO: prettify dev server error
|
|
||||||
throw new ActionError({
|
throw new ActionError({
|
||||||
status: 'INTERNAL_SERVER_ERROR',
|
status: 'INTERNAL_SERVER_ERROR',
|
||||||
message:
|
message:
|
||||||
'Called an action with a non-JSON body. To enhance an action to accept form data, add `enhance: true` to your `defineAction()` config.',
|
'Called an action with a non-JSON body. To acceptFormData an action to accept form data, add `acceptFormData: true` to your `defineAction()` config.',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!inputSchema) return await handler(unparsedInput, context);
|
if (!inputSchema) return await handler(unparsedInput, context);
|
||||||
|
|
||||||
if (enhance && unparsedInput instanceof FormData) {
|
if (acceptFormData && unparsedInput instanceof FormData) {
|
||||||
if (!(inputSchema instanceof z.ZodObject)) {
|
if (!(inputSchema instanceof z.ZodObject)) {
|
||||||
throw new ActionError({
|
throw new ActionError({
|
||||||
status: 'INTERNAL_SERVER_ERROR',
|
status: 'INTERNAL_SERVER_ERROR',
|
||||||
message: '`input` must use a Zod object schema (z.object) when `enhance` is enabled.',
|
message:
|
||||||
|
'`input` must use a Zod object schema (z.object) when `acceptFormData` is enabled.',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
unparsedInput = enhanceFormData(unparsedInput, inputSchema);
|
unparsedInput = upgradeFormData(unparsedInput, inputSchema);
|
||||||
}
|
}
|
||||||
|
|
||||||
const parsed = inputSchema.safeParse(unparsedInput);
|
const parsed = inputSchema.safeParse(unparsedInput);
|
||||||
|
@ -46,7 +45,7 @@ export function defineAction<TOutput, TInputSchema extends z.ZodType>({
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function enhanceFormData<T extends z.AnyZodObject>(
|
function upgradeFormData<T extends z.AnyZodObject>(
|
||||||
formData: FormData,
|
formData: FormData,
|
||||||
schema: T
|
schema: T
|
||||||
): Record<string, unknown> {
|
): Record<string, unknown> {
|
||||||
|
|
|
@ -23,7 +23,7 @@ export default {
|
||||||
}),
|
}),
|
||||||
|
|
||||||
comment: defineAction({
|
comment: defineAction({
|
||||||
enhance: true,
|
acceptFormData: true,
|
||||||
input: z.object({
|
input: z.object({
|
||||||
postId: z.string(),
|
postId: z.string(),
|
||||||
author: z.string(),
|
author: z.string(),
|
||||||
|
|
Loading…
Reference in a new issue