Skip to content

Commit

Permalink
Fix mutation parameters when using Zod
Browse files Browse the repository at this point in the history
  • Loading branch information
dguenther committed Apr 24, 2024
1 parent b4a6847 commit 6b22f6d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 22 deletions.
4 changes: 2 additions & 2 deletions packages/data-facade/src/facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ function handlerQueryBuilder<TResolver extends ResolverFunc>(

function buildUseMutation() {
return <TResolver extends ResolverFunc>(resolver: TResolver) => ({
useMutation: (opts?: UseMutationOptions<Awaited<ReturnType<TResolver>>, Error, unknown, unknown>) => {
useMutation: (opts?: UseMutationOptions<Awaited<ReturnType<TResolver>>, Error, Parameters<TResolver>[0], unknown>) => {
return useMutation<
Awaited<ReturnType<TResolver>>,
Error,
unknown,
Parameters<TResolver>[0],
unknown
>({
...opts,
Expand Down
4 changes: 3 additions & 1 deletion packages/data-facade/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export type HandlerQueryBuilderReturn<TResolver extends ResolverFunc> = (
type UseMutationType<
TResolver extends ResolverFunc,
TReturn = Awaited<ReturnType<TResolver>>,
> = (opts?: UseMutationOptions<TReturn, Error, unknown, unknown>) => UseMutationResult<TReturn>;
> = Parameters<TResolver>["length"] extends 0
? (opts?: UseMutationOptions<TReturn, Error, null | undefined, unknown>) => UseMutationResult<TReturn>
: (opts?: UseMutationOptions<TReturn, Error, Parameters<TResolver>[0], unknown>) => UseMutationResult<TReturn>

export type HandlerMutationBuilderReturn<TResolver extends ResolverFunc> =
() => {
Expand Down
13 changes: 3 additions & 10 deletions packages/mobile-app/data/facades/accounts/demoHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { f } from "data-facade";
import { z } from "zod";
import { AccountsMethods } from "./types";
import { AccountFormat } from "@ironfish/sdk";
import { AccountFormat, LanguageKey } from "@ironfish/sdk";

let ACCOUNTS = [
{ id: 0, name: "alice", viewOnlyAccount: "alice" },
Expand Down Expand Up @@ -31,16 +31,9 @@ export const accountsHandlers = f.facade<AccountsMethods>({
ACCOUNTS.push(account);
return account;
}),
// The enums used when exporting are tricky to use with Zod
exportAccount: f.handler
.input(
z.object({
name: z.string(),
format: z.nativeEnum(AccountFormat),
language: z.string().optional(),
viewOnly: z.boolean().optional(),
}),
)
.mutation(async ({ name }) => {
.mutation(async ({ name }: { name: string, format: AccountFormat, language?: LanguageKey, viewOnly?: boolean }) => {
const account = ACCOUNTS.find((a) => a.name === name)
if (account === undefined) {
throw new Error(`No account found with name ${name}`);
Expand Down
11 changes: 2 additions & 9 deletions packages/mobile-app/data/facades/accounts/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,9 @@ export const accountsHandlers = f.facade<AccountsMethods>({
const account = await wallet.createAccount(name);
return account;
}),
// The enums used when exporting are tricky to use with Zod
exportAccount: f.handler
.input(
z.object({
name: z.string(),
format: z.nativeEnum(AccountFormat),
language: z.string().optional(),
viewOnly: z.boolean().optional(),
}),
)
.mutation(async ({ name, format, language, viewOnly }) => {
.mutation(async ({ name, format, language, viewOnly }: { name: string, format: AccountFormat, language?: LanguageKey, viewOnly?: boolean }) => {
if (language && !Object.hasOwn(LanguageUtils.LANGUAGES, language)) {
throw new Error(`Language ${language} is not supported`);
}
Expand Down

0 comments on commit 6b22f6d

Please sign in to comment.