Skip to content

Commit

Permalink
🏭 addon.resolver can now be a function
Browse files Browse the repository at this point in the history
should solve #212
  • Loading branch information
elbywan committed Dec 30, 2023
1 parent c65dd5c commit 0bf9aa8
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const resolver = <T, Chain, R>(wretch: T & Wretch<T, Chain, R>) => {
})
}
// Enforces the proper promise type when a body parsing method is called.
type BodyParser = <Type>(funName: string | null) => <Result = void>(cb?: (type: Type) => Result) => Promise<Awaited<Result>>
type BodyParser = <Type>(funName: "json" | "blob" | "formData" | "arrayBuffer" | "text" | null) => <Result = void>(cb?: (type: Type) => Result) => Promise<Awaited<Result>>
const bodyParser: BodyParser = funName => cb => funName ?
// If a callback is provided, then callback with the body result otherwise return the parsed body itself.
catchersWrapper(throwingPromise.then(_ => _ && _[funName]()).then(_ => cb ? cb(_) : _)) :
Expand Down Expand Up @@ -127,7 +127,7 @@ export const resolver = <T, Chain, R>(wretch: T & Wretch<T, Chain, R>) => {

const enhancedResponseChain: R extends undefined ? Chain & WretchResponseChain<T, Chain, undefined> : R = addons.reduce((chain, addon) => ({
...chain,
...(addon.resolver as any)
...(typeof addon.resolver === "function" ? (addon.resolver as (_: WretchResponseChain<T, Chain, R>) => any)(chain) : addon.resolver)
}), responseChain)

return resolvers.reduce((chain, r) => r(chain, wretch), enhancedResponseChain)
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,6 @@ export type FetchLike = (url: string, opts: WretchOptions) => Promise<WretchResp
export type WretchAddon<W extends unknown, R extends unknown = unknown> = {
beforeRequest?<T, C, R>(wretch: T & Wretch<T, C, R>, options: WretchOptions, state: Record<any, any>): T & Wretch<T, C, R>,
wretch?: W,
resolver?: R
resolver?: R | (<T, C>(_: C & WretchResponseChain<T, C, R>) => R)
}

3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"isolatedModules": true
},
"include": [
"src/**/*",
"test.ts"
"src/**/*"
]
}

0 comments on commit 0bf9aa8

Please sign in to comment.