feat: switch fetch function
This commit is contained in:
1 parent
bf63026108
commit
2035ee052a
10 files changed
+38
-29
No files matched your search
+15
-3
@@ -14,21 +14,33 @@ export const DEFAULT_INSTANCE_URL = env.PUBLIC_INSTANCE_URL || 'lemmy.ml'
|
||||
export let instance = writable(DEFAULT_INSTANCE_URL)
|
||||
|
||||
async function customFetch(
|
||||
func: (
|
||||
input: RequestInfo | URL,
|
||||
init?: RequestInit | undefined
|
||||
) => Promise<Response>,
|
||||
input: RequestInfo | URL,
|
||||
init?: RequestInit | undefined
|
||||
): Promise<Response> {
|
||||
const res = await fetch(input, init)
|
||||
const res = await func(input, init)
|
||||
if (!res.ok) throw error(res.status, res.statusText)
|
||||
return res
|
||||
}
|
||||
|
||||
export function getClient(instanceURL?: string): LemmyHttp {
|
||||
export function getClient(
|
||||
instanceURL?: string,
|
||||
func?: (
|
||||
input: RequestInfo | URL,
|
||||
init?: RequestInit | undefined
|
||||
) => Promise<Response>
|
||||
): LemmyHttp {
|
||||
if (!instanceURL) {
|
||||
instanceURL = get(instance)
|
||||
}
|
||||
|
||||
return new LemmyHttp(`https://${instanceURL}`, {
|
||||
fetchFunction: customFetch,
|
||||
fetchFunction: func
|
||||
? (input, init) => customFetch(func, input, init)
|
||||
: undefined,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user