Skip to content

Commit

Permalink
chore: replace ky
Browse files Browse the repository at this point in the history
  • Loading branch information
chillios-dev committed Jul 18, 2024
1 parent 6b3d279 commit ec84a1f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 36 deletions.
Binary file modified bun.lockb
Binary file not shown.
20 changes: 7 additions & 13 deletions packages/swapkit/api/src/thorswapApi/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,13 @@ import {
const baseUrlV1 = "https://api.thorswap.finance";

export const APIV1RequestClient = RequestClient.extend({
hooks: {
afterResponse: [
async (_request, _options, response) => {
const body = await response.json();

try {
const errorBody = ApiV1ErrorSchema.parse(body);
return new Response(JSON.stringify(errorBody), { status: 200 });
} catch (_error) {
return body;
}
},
],
responseHandler: (response) => {
try {
const errorBody = ApiV1ErrorSchema.parse(response);
return errorBody;
} catch (_error) {
return response;
}
},
});

Expand Down
1 change: 0 additions & 1 deletion packages/swapkit/helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"description": "SwapKit - Helpers",
"dependencies": {
"@swapkit/tokens": "workspace:*",
"ky": "1.4.0",
"picocolors": "1.0.1",
"zod": "3.23.8"
},
Expand Down
61 changes: 39 additions & 22 deletions packages/swapkit/helpers/src/modules/requestClient.ts
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 }),
};
},
};

0 comments on commit ec84a1f

Please sign in to comment.