mirror of
https://github.com/movie-web/providers.git
synced 2025-09-13 18:13:25 +00:00
Basically library structure
This commit is contained in:
15
src/fetchers/common.ts
Normal file
15
src/fetchers/common.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { FetcherOptions } from '@/fetchers/types';
|
||||
|
||||
// make url with query params and base url used correctly
|
||||
export function makeFullUrl(url: string, ops?: FetcherOptions): string {
|
||||
// glue baseUrl and rest of url together
|
||||
const fullUrl = ops?.baseUrl ?? '';
|
||||
// TODO make full url
|
||||
|
||||
const parsedUrl = new URL(fullUrl);
|
||||
Object.entries(ops?.query ?? {}).forEach(([k, v]) => {
|
||||
parsedUrl.searchParams.set(k, v);
|
||||
});
|
||||
|
||||
return parsedUrl.toString();
|
||||
}
|
15
src/fetchers/standardFetch.ts
Normal file
15
src/fetchers/standardFetch.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { makeFullUrl } from '@/fetchers/common';
|
||||
import { Fetcher } from '@/fetchers/types';
|
||||
|
||||
export function makeStandardFetcher(f: typeof fetch): Fetcher {
|
||||
const normalFetch: Fetcher = (url, ops) => {
|
||||
const fullUrl = makeFullUrl(url, ops);
|
||||
|
||||
return f(fullUrl, {
|
||||
method: ops.method,
|
||||
body: JSON.stringify(ops.body), // TODO content type headers + proper serialization
|
||||
});
|
||||
};
|
||||
|
||||
return normalFetch;
|
||||
}
|
24
src/fetchers/types.ts
Normal file
24
src/fetchers/types.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
export type FetcherOptions = {
|
||||
baseUrl?: string;
|
||||
headers?: Record<string, string>;
|
||||
query?: Record<string, string>;
|
||||
method?: 'GET' | 'POST';
|
||||
body?: Record<string, any> | string | FormData;
|
||||
};
|
||||
|
||||
export type DefaultedFetcherOptions = {
|
||||
baseUrl?: string;
|
||||
body?: Record<string, any> | string | FormData;
|
||||
headers: Record<string, string>;
|
||||
query: Record<string, string>;
|
||||
method: 'GET' | 'POST';
|
||||
};
|
||||
|
||||
export type Fetcher<T = any> = {
|
||||
(url: string, ops: DefaultedFetcherOptions): T;
|
||||
};
|
||||
|
||||
// this feature has some quality of life features
|
||||
export type UseableFetcher<T = any> = {
|
||||
(url: string, ops?: FetcherOptions): T;
|
||||
};
|
Reference in New Issue
Block a user