You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm a big fan of wretch but recently I wanted to add Schema validation to my API responses.
If I'm not mistaken wretch doesn't support it out-of-the-box (comparing to up-fetch).
I've created a tiny addon that is using validbot. Let me know what do you think.
Addon
import{typeBaseIssue,typeBaseSchema,typeInferOutput,safeParse}from'valibot';importtype{WretchAddon,WretchResponseChain}from'wretch/types';exportinterfaceParseAddonType{parse: <T,CextendsParseAddonType,R,SextendsBaseSchema<unknown,unknown,BaseIssue<unknown>>>(this: C&WretchResponseChain<T,C,R>,schema: S,)=>Promise<Awaited<InferOutput<S>>>;}/** * Parse the json response with the given schema. * The `parse` resolver will implicitly call the `json` resolver before the validation. */constParseAddon: WretchAddon<unknown,ParseAddonType>={resolver: {parse(schema){returnthis.json((data)=>{// safe parsing here but you can use simple `parse` as well to throw ValiError const{ success, output, issues }=safeParse(schema,data);if(success){returnoutput;}// ... throw your error});},},};exportdefaultParseAddon;
I'm a big fan of
wretch
but recently I wanted to add Schema validation to my API responses.If I'm not mistaken
wretch
doesn't support it out-of-the-box (comparing to up-fetch).I've created a tiny addon that is using validbot. Let me know what do you think.
Addon
Usage
The text was updated successfully, but these errors were encountered: