Skip to content

Commit

Permalink
Update API parsing to parse each ambassador individually
Browse files Browse the repository at this point in the history
  • Loading branch information
MattIPv4 committed Mar 8, 2025
1 parent 9dd1410 commit 5fcd7a1
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/hooks/useAmbassadors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,36 @@ const apiAmbassadorSchema = ambassadorSchema.extend({
}),
});

type Ambassador = z.infer<typeof apiAmbassadorSchema>;

// Use transform here so we parse each ambassador individually
const apiSchema = z.object({
v2: z.record(apiAmbassadorSchema),
v2: z
.record(
// Use nullable here as the fallback for when we fail to parse an ambassador
apiAmbassadorSchema.nullable().catch((ctx) => {
console.error(
"Failed to parse ambassador",
ctx.input,
ctx.error.message,
);
return null;
}),
)
.transform((val) =>
// Filter out any null values that failed to parse
typeSafeObjectFromEntries(
Object.entries(val).filter(
(entry): entry is [string, Ambassador] => !!entry[1],
),
),
)
// Ensure we didn't fail to parse all ambassadors
.refine((val) => Object.keys(val).length > 0, {
message: "No ambassadors found",
}),
});

type Ambassador = z.infer<typeof apiAmbassadorSchema>;

const apiBaseUrl = process.env.REACT_APP_API_BASE_URL?.replace(/\/+$/, "");
if (!apiBaseUrl)
throw new Error("REACT_APP_API_BASE_URL environment variable is not set");
Expand Down

0 comments on commit 5fcd7a1

Please sign in to comment.