mirror of
https://github.com/luxfi/explore.git
synced 2026-09-24 00:28:18 +00:00
37 lines
1.2 KiB
TypeScript
37 lines
1.2 KiB
TypeScript
import React from 'react';
|
|
|
|
import appConfig from 'configs/app/config';
|
|
import type { Params as FetchParams } from 'lib/hooks/useFetch';
|
|
import useFetch from 'lib/hooks/useFetch';
|
|
|
|
import buildUrl from './buildUrl';
|
|
import { RESOURCES } from './resources';
|
|
import type { ApiResource, ResourceName, ResourcePathParams } from './resources';
|
|
|
|
export interface Params<R extends ResourceName> {
|
|
pathParams?: ResourcePathParams<R>;
|
|
queryParams?: Record<string, string | Array<string> | number | undefined>;
|
|
fetchParams?: Pick<FetchParams, 'body' | 'method' | 'signal'>;
|
|
}
|
|
|
|
export default function useApiFetch() {
|
|
const fetch = useFetch();
|
|
|
|
return React.useCallback(<R extends ResourceName, SuccessType = unknown, ErrorType = unknown>(
|
|
resourceName: R,
|
|
{ pathParams, queryParams, fetchParams }: Params<R> = {},
|
|
) => {
|
|
const resource: ApiResource = RESOURCES[resourceName];
|
|
const url = buildUrl(resourceName, pathParams, queryParams);
|
|
return fetch<SuccessType, ErrorType>(url, {
|
|
credentials: 'include',
|
|
...(resource.endpoint && appConfig.host === 'localhost' ? {
|
|
headers: {
|
|
'x-endpoint': resource.endpoint,
|
|
},
|
|
} : {}),
|
|
...fetchParams,
|
|
});
|
|
}, [ fetch ]);
|
|
}
|