Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Actions accept type completions #11436

Merged
merged 4 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/swift-cows-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes `astro:actions` autocompletion for the `defineAction` `accept` property
6 changes: 3 additions & 3 deletions packages/astro/src/actions/runtime/virtual/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export { z } from 'zod';
export const getApiContext = _getApiContext;

export type Accept = 'form' | 'json';
export type InputSchema<T extends Accept> = T extends 'form'
export type InputSchema<T extends Accept | undefined> = T extends 'form'
? z.AnyZodObject | z.ZodType<FormData>
: z.ZodType;

Expand All @@ -21,7 +21,7 @@ type Handler<TInputSchema, TOutput> = TInputSchema extends z.ZodType

export type ActionClient<
TOutput,
TAccept extends Accept,
TAccept extends Accept | undefined,
TInputSchema extends InputSchema<TAccept> | undefined,
> = TInputSchema extends z.ZodType
? ((
Expand All @@ -44,7 +44,7 @@ export type ActionClient<

export function defineAction<
TOutput,
TAccept extends Accept = 'json',
TAccept extends Accept | undefined = undefined,
TInputSchema extends InputSchema<Accept> | undefined = TAccept extends 'form'
? // If `input` is omitted, default to `FormData` for forms and `any` for JSON.
z.ZodType<FormData>
Expand Down
45 changes: 45 additions & 0 deletions packages/astro/test/types/define-action-accept.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe, it } from 'node:test';
import { expectTypeOf } from 'expect-type';
import { defineAction } from '../../dist/actions/runtime/virtual/server.js';
import { z } from '../../zod.mjs';

describe('defineAction accept', () => {
it('accepts type `any` when input is omitted with accept json', async () => {
const action = defineAction({
handler: () => {},
});
expectTypeOf(action).parameter(0).toBeAny();
expectTypeOf(action).parameter(0).not.toEqualTypeOf<FormData>();

const jsonAction = defineAction({
accept: 'json',
handler: () => {},
});
expectTypeOf(jsonAction).parameter(0).toBeAny();
expectTypeOf(jsonAction).parameter(0).not.toEqualTypeOf<FormData>();
});
it('accepts type `FormData` when input is omitted with accept form', async () => {
const action = defineAction({
accept: 'form',
handler: () => {},
});
expectTypeOf(action).parameter(0).toEqualTypeOf<FormData>();
});

it('accept type safe values for input with accept json', async () => {
const action = defineAction({
input: z.object({ name: z.string() }),
handler: () => {},
});
expectTypeOf(action).parameter(0).toEqualTypeOf<{ name: string }>();
});

it('accepts type `FormData` for all inputs with accept form', async () => {
const action = defineAction({
accept: 'form',
input: z.object({ name: z.string() }),
handler: () => {},
});
expectTypeOf(action).parameter(0).toEqualTypeOf<FormData>();
});
});
Loading