mirror of
https://github.com/luxfi/explore.git
synced 2026-09-24 00:28:18 +00:00
25 lines
913 B
TypeScript
25 lines
913 B
TypeScript
import { compile } from 'path-to-regexp';
|
|
|
|
import appConfig from 'configs/app/config';
|
|
|
|
import { RESOURCES } from './resources';
|
|
import type { ApiResource, ResourceName } from './resources';
|
|
|
|
export default function buildUrlNode(
|
|
_resource: ApiResource | ResourceName,
|
|
pathParams?: Record<string, string | undefined>,
|
|
queryParams?: Record<string, string | number | undefined>,
|
|
) {
|
|
const resource: ApiResource = typeof _resource === 'string' ? RESOURCES[_resource] : _resource;
|
|
const baseUrl = resource.endpoint || appConfig.api.endpoint;
|
|
const basePath = resource.basePath !== undefined ? resource.basePath : appConfig.api.basePath;
|
|
const path = basePath + resource.path;
|
|
const url = new URL(compile(path)(pathParams), baseUrl);
|
|
|
|
queryParams && Object.entries(queryParams).forEach(([ key, value ]) => {
|
|
value && url.searchParams.append(key, String(value));
|
|
});
|
|
|
|
return url.toString();
|
|
}
|