Skip to content

Commit 0bf9aa8

Browse files
committed
🏭 addon.resolver can now be a function
should solve #212
1 parent c65dd5c commit 0bf9aa8

File tree

3 files changed

+4
-5
lines changed

3 files changed

+4
-5
lines changed

src/resolver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export const resolver = <T, Chain, R>(wretch: T & Wretch<T, Chain, R>) => {
9595
})
9696
}
9797
// Enforces the proper promise type when a body parsing method is called.
98-
type BodyParser = <Type>(funName: string | null) => <Result = void>(cb?: (type: Type) => Result) => Promise<Awaited<Result>>
98+
type BodyParser = <Type>(funName: "json" | "blob" | "formData" | "arrayBuffer" | "text" | null) => <Result = void>(cb?: (type: Type) => Result) => Promise<Awaited<Result>>
9999
const bodyParser: BodyParser = funName => cb => funName ?
100100
// If a callback is provided, then callback with the body result otherwise return the parsed body itself.
101101
catchersWrapper(throwingPromise.then(_ => _ && _[funName]()).then(_ => cb ? cb(_) : _)) :
@@ -127,7 +127,7 @@ export const resolver = <T, Chain, R>(wretch: T & Wretch<T, Chain, R>) => {
127127

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

133133
return resolvers.reduce((chain, r) => r(chain, wretch), enhancedResponseChain)

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,6 @@ export type FetchLike = (url: string, opts: WretchOptions) => Promise<WretchResp
786786
export type WretchAddon<W extends unknown, R extends unknown = unknown> = {
787787
beforeRequest?<T, C, R>(wretch: T & Wretch<T, C, R>, options: WretchOptions, state: Record<any, any>): T & Wretch<T, C, R>,
788788
wretch?: W,
789-
resolver?: R
789+
resolver?: R | (<T, C>(_: C & WretchResponseChain<T, C, R>) => R)
790790
}
791791

tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"isolatedModules": true
1818
},
1919
"include": [
20-
"src/**/*",
21-
"test.ts"
20+
"src/**/*"
2221
]
2322
}

0 commit comments

Comments
 (0)