Skip to content

Commit

Permalink
Actions: fix missing orThrow type when input is omitted (#11658)
Browse files Browse the repository at this point in the history
* fix: orThrow missing when input is omitted

* chore: changeset
  • Loading branch information
bholmesdev authored Aug 8, 2024
1 parent a851021 commit 13b912a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-seas-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes `orThrow()` type when calling an Action without an `input` validator.
2 changes: 1 addition & 1 deletion packages/astro/src/actions/runtime/virtual/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type ActionClient<
input: TAccept extends 'form' ? FormData : z.input<TInputSchema>,
) => Promise<Awaited<TOutput>>;
}
: (input?: any) => Promise<SafeResult<never, Awaited<TOutput>>> & {
: ((input?: any) => Promise<SafeResult<never, Awaited<TOutput>>>) & {
orThrow: (input?: any) => Promise<Awaited<TOutput>>;
};

Expand Down
19 changes: 15 additions & 4 deletions packages/astro/test/types/action-return-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,29 @@ import { z } from '../../zod.mjs';

describe('ActionReturnType', () => {
it('Infers action return type', async () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const action = defineAction({
const _action = defineAction({
input: z.object({
name: z.string(),
}),
handler: async ({ name }) => {
return { name };
},
});
expectTypeOf<ActionReturnType<typeof action>>().toEqualTypeOf<
expectTypeOf<ActionReturnType<typeof _action>>().toEqualTypeOf<
SafeResult<any, { name: string }>
>();
expectTypeOf<ActionReturnType<typeof action.orThrow>>().toEqualTypeOf<{ name: string }>();
expectTypeOf<ActionReturnType<typeof _action.orThrow>>().toEqualTypeOf<{ name: string }>();
});

it('Infers action return type when input is omitted', async () => {
const _action = defineAction({
handler: async () => {
return { name: 'Ben' };
},
});
expectTypeOf<ActionReturnType<typeof _action>>().toEqualTypeOf<
SafeResult<any, { name: string }>
>();
expectTypeOf<ActionReturnType<typeof _action.orThrow>>().toEqualTypeOf<{ name: string }>();
});
});

0 comments on commit 13b912a

Please sign in to comment.