mirror of
https://github.com/movie-web/providers.git
synced 2025-09-13 17:43:25 +00:00
add support for full url merging
This commit is contained in:
@@ -1,10 +1,22 @@
|
||||
import { Fetcher, FetcherOptions, UseableFetcher } from '@/fetchers/types';
|
||||
|
||||
export type FullUrlOptions = Pick<FetcherOptions, 'query' | 'baseUrl'>;
|
||||
|
||||
// make url with query params and base url used correctly
|
||||
export function makeFullUrl(url: string, ops?: FetcherOptions): string {
|
||||
export function makeFullUrl(url: string, ops?: FullUrlOptions): string {
|
||||
// glue baseUrl and rest of url together
|
||||
const fullUrl = ops?.baseUrl ?? '';
|
||||
// TODO make full url
|
||||
let leftSide = ops?.baseUrl ?? '';
|
||||
let rightSide = url;
|
||||
|
||||
// left side should always end with slash, if its set
|
||||
if (leftSide.length > 0 && !leftSide.endsWith('/')) leftSide += '/';
|
||||
|
||||
// right side should never start with slash
|
||||
if (rightSide.startsWith('/')) rightSide = rightSide.slice(1);
|
||||
|
||||
const fullUrl = leftSide + rightSide;
|
||||
if (!fullUrl.startsWith('http://') && !fullUrl.startsWith('https://'))
|
||||
throw new Error(`Invald URL -- URL doesn't start with a http scheme: '${fullUrl}'`);
|
||||
|
||||
const parsedUrl = new URL(fullUrl);
|
||||
Object.entries(ops?.query ?? {}).forEach(([k, v]) => {
|
||||
|
Reference in New Issue
Block a user