Spaces:
Running
Running
File size: 6,503 Bytes
4446d89 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
import type { JsonValue } from "type-fest";
export type QueryParams = Record<string, string | number | boolean | undefined | string[]>;
/**
* Create a query string ("?param=value") from an object {[key]: value}.
* Undefined valued are ignored, and an empty string is returned if all values are undefined.
*/
export function queryString(params: QueryParams): string {
const searchParams = new URLSearchParams();
for (const [key, value] of Object.entries(params)) {
if (value !== undefined) {
if (Array.isArray(value)) {
for (const val of value) {
searchParams.append(key, String(val));
}
} else {
searchParams.set(key, String(value));
}
}
}
const searchParamsStr = searchParams.toString();
return searchParamsStr ? `?${searchParamsStr}` : "";
}
interface HttpResponseBase<T> {
/** set to true if the call was aborted by the User */
aborted?: boolean;
/** set to true if the call resulted in an error */
isError: boolean;
/** the parsed server response, whether the call ended up in an error or not */
payload: T;
/** a clone of the raw Response object returned by fetch, in case it is needed for some edge cases */
rawResponse: Response | undefined;
/** the request status code */
statusCode: number;
/** Parsed links in Link header */
links?: Record<string, string>;
}
interface HttpResponseError<T> extends HttpResponseBase<T> {
aborted: boolean;
error: string;
isError: true;
}
interface HttpResponseSuccess<T> extends HttpResponseBase<T> {
isError: false;
payload: T;
}
export type HttpResponse<SuccessType, ErrorType = unknown> =
| HttpResponseSuccess<SuccessType>
| HttpResponseError<ErrorType>;
export type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS";
type ResponseType = "blob" | "json" | "text";
interface SendOptions<D> {
/** the data sent to the server */
data?: D;
/** the request headers */
headers?: Record<string, string>;
/**
* determines how the server response will be parsed (as JSON, text, or a blob)
* @default "json"
*/
responseType?: ResponseType;
/**
* The AbortSignal interface represents a signal object that allows you to communicate with a
* DOM request (such as a fetch request) and abort it if required via an AbortController object.
* read more at: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
*/
signal?: AbortSignal;
credentials?: RequestCredentials;
}
async function getResponseContent(res: Response, type?: ResponseType): Promise<Blob | JsonValue | undefined> {
try {
if (res.headers.get("content-type")?.includes("json")) {
return (await res.json()) as JsonValue;
}
if (type === "blob") {
return await res.blob();
}
return await res.text();
} catch (err) {
return undefined;
}
}
/**
* Handle fetch calls, parse the server response, capture every possible error,
* and returns standardized {@link HttpResponse} objects in every scenario
*/
export async function httpSend<T, D = Record<string, unknown> | File>(
method: HttpMethod,
path: string,
sendOptions: SendOptions<D> = {}
): Promise<HttpResponse<T>> {
try {
const headers = {
...sendOptions.headers,
...(sendOptions.responseType === "json"
? { Accept: "application/json" }
: sendOptions.responseType === "text"
? { Accept: "text/plain" }
: {}),
};
const res = await fetch(path, {
body:
sendOptions.data instanceof File
? sendOptions.data
: sendOptions.data
? JSON.stringify(sendOptions.data)
: undefined,
headers,
method,
...(sendOptions.signal ? { signal: sendOptions.signal } : {}),
...(sendOptions.credentials ? { credentials: sendOptions.credentials } : {}),
});
const rawResponse = res.clone();
if (!res.ok) {
let error = `${res.status} ${res.statusText}`;
const payload = await getResponseContent(res);
// In case we get a detailed JSON error message from the backend - which we should in any of the following cases:
// - When hitting /api/... endpoints
// - When using header X-Requested-With: XMLHttpRequest
// - When using header Content-Type: application/json
if (typeof payload === "object" && payload) {
if ("message" in payload && typeof payload.message === "string") {
error = payload.message;
} else if ("error" in payload && typeof payload.error === "string") {
error = payload.error;
}
}
return {
aborted: false,
error,
isError: true,
payload,
rawResponse,
statusCode: res.status,
};
}
const payload = await getResponseContent(res, sendOptions.responseType);
const links = res.headers.get("Link") ? parseLinkHeader(res.headers.get("Link")!) : undefined;
return payload !== undefined
? {
isError: false,
payload: payload as T,
rawResponse,
statusCode: res.status,
links,
}
: {
aborted: false,
error: sendOptions.responseType === "json" ? "Error parsing JSON" : "Error parsing server response",
isError: true,
payload,
rawResponse,
statusCode: res.status,
links,
};
} catch (e) {
return {
aborted: e instanceof DOMException && e.name === "AbortError",
error: (e instanceof TypeError || e instanceof DOMException) && e.message ? e.message : "Failed to fetch",
isError: true,
payload: undefined,
rawResponse: undefined,
statusCode: 0,
};
}
}
type GetOptions = Omit<SendOptions<unknown>, "data">;
/**
* Helper function to easily and safely make GET calls
*/
export function httpGet<T>(path: string, opts: GetOptions = {}): Promise<HttpResponse<T>> {
return httpSend<T>("GET", path, { ...opts });
}
export function parseLinkHeader(header: string): Record<string, string> {
const regex = /<(https?:[/][/][^>]+)>;\s+rel="([^"]+)"/g;
return Object.fromEntries([...header.matchAll(regex)].map(([_, url, rel]) => [rel, url]));
}
/// A not-that-great throttling function
export function throttle<T extends unknown[]>(callback: (...rest: T) => unknown, limit: number): (...rest: T) => void {
let last: number;
/// setTimeout can return different types on browser or node
let deferTimer: ReturnType<typeof setTimeout>;
return function (...rest) {
const now = Date.now();
if (last && now < last + limit) {
clearTimeout(deferTimer);
deferTimer = setTimeout(function () {
last = now;
callback(...rest);
}, limit);
} else {
last = now;
callback(...rest);
}
};
}
|