icns-frontend/utils/url.ts

26 lines
653 B
TypeScript
Raw Normal View History

2022-12-05 10:50:13 +00:00
export function request<TResponse>(
url: string,
config: RequestInit = {},
): Promise<TResponse> {
return fetch(url, config)
2022-12-14 15:39:51 +00:00
.then((response) => {
if (!response.ok) {
throw new Error(
`This is an HTTP error: The status is ${response.status} ${response.statusText}`,
2022-12-14 15:39:51 +00:00
);
}
return response.json();
})
2022-12-05 10:50:13 +00:00
.then((data) => data as TResponse);
}
export function buildQueryString(query: Record<string, any>): string {
return Object.entries(query)
.map(([key, value]) =>
key && value
? `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
: "",
)
.join("&");
}