-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ebe27fa
commit 1c909c4
Showing
4 changed files
with
46 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,56 @@ | ||
import type { KyInstance, Options } from "ky"; | ||
import ky from "ky"; | ||
type Options = { | ||
headers?: Record<string, string>; | ||
apiKey?: string; | ||
method?: "GET" | "POST"; | ||
onError?: (error: NotWorth) => NotWorth; | ||
responseHandler?: (response: NotWorth) => NotWorth; | ||
[key: string]: NotWorth; | ||
}; | ||
|
||
let kyClientConfig: Options & { apiKey?: string } = {}; | ||
let clientConfig: Options = {}; | ||
|
||
export const defaultRequestHeaders = | ||
typeof window !== "undefined" | ||
? ({} as Record<string, string>) | ||
: { referrer: "https://sk.thorswap.net", referer: "https://sk.thorswap.net" }; | ||
|
||
export function setRequestClientConfig({ apiKey, ...config }: Options & { apiKey?: string }) { | ||
kyClientConfig = { ...config, apiKey }; | ||
export function setRequestClientConfig({ apiKey, ...config }: Options) { | ||
clientConfig = { ...config, apiKey }; | ||
} | ||
|
||
function getKyClient() { | ||
const { apiKey, ...config } = kyClientConfig; | ||
return ky.create({ | ||
...config, | ||
headers: { ...defaultRequestHeaders, ...config.headers, "x-api-key": apiKey }, | ||
}); | ||
} | ||
async function fetchWithConfig(url: string, options: Options = {}) { | ||
const { apiKey, ...config } = clientConfig; | ||
const headers = { ...defaultRequestHeaders, ...config.headers, ...options.headers }; | ||
|
||
if (apiKey) headers["x-api-key"] = apiKey; | ||
|
||
try { | ||
const response = await fetch(url, { ...config, ...options, headers }); | ||
const body = await response.json(); | ||
|
||
const getTypedBaseRequestClient = (ky: KyInstance) => ({ | ||
get: async <T>(url: string | URL | Request, options?: Options) => | ||
(await ky.get(url, options)).json<T>(), | ||
post: async <T>(url: string | URL | Request, options?: Options) => | ||
(await ky.post(url, options)).json<T>(), | ||
}); | ||
if (options.responseHandler) return options.responseHandler(body); | ||
|
||
return body; | ||
} catch (error) { | ||
if (options.onError) return options.onError(error); | ||
|
||
console.error(error); | ||
} | ||
} | ||
|
||
export const RequestClient = { | ||
...getTypedBaseRequestClient(getKyClient()), | ||
get: async <T>(url: string, options?: Options): Promise<T> => | ||
fetchWithConfig(url, { ...options, method: "GET" }), | ||
post: async <T>(url: string, options?: Options): Promise<T> => | ||
fetchWithConfig(url, { ...options, method: "POST" }), | ||
extend: (options: Options) => { | ||
const extendedClient = getKyClient().extend(options); | ||
const extendedConfig = { ...clientConfig, ...options }; | ||
return { | ||
...getTypedBaseRequestClient(extendedClient), | ||
extend: RequestClient.extend, | ||
get: async <T>(url: string, options?: Options): Promise<T> => | ||
fetchWithConfig(url, { ...extendedConfig, ...options, method: "GET" }), | ||
post: async <T>(url: string, options?: Options): Promise<T> => | ||
fetchWithConfig(url, { ...extendedConfig, ...options, method: "POST" }), | ||
extend: (newOptions: Options) => RequestClient.extend({ ...extendedConfig, ...newOptions }), | ||
}; | ||
}, | ||
}; |