UNPKG

fetch-buddy

Version:

A TypeScript API client that creates fetch requests from a staticly typed structured object

50 lines (49 loc) 1.79 kB
/** * Provided a structured request object, this utility will format the structured * request object into a string that will be used as the URL for the request */ export function formatStructuredApiRequest(args) { // Parse the QueryParams const urlSearchParams = Object.entries(args.queryParams ?? {}).reduce((accum, [argKey, argValue]) => { if (typeof argValue === "string") { accum.set(argKey, argValue); } if (typeof argValue === "number") { accum.set(argKey, argValue.toString()); } if (typeof argValue === "boolean") { accum.set(argKey, argValue ? "true" : "false"); } // Ignore null or undefined values return accum; }, new URLSearchParams()); // Parse the segments const urlSegments = (args.segments ?? []).filter(Boolean).join("/"); // URL assembly const requestQueryParams = urlSearchParams.size === 0 ? "" : `?${urlSearchParams.toString()}`; const requestRoot = args.root; const requestSegments = urlSegments.length === 0 ? "" : `/${urlSegments}`; const requestUrl = `${requestRoot}${requestSegments}${requestQueryParams}`; return requestUrl; } export class RequestError extends Error { _code; _data; constructor({ message, code, data, }) { super(message); this._code = code; this.name = "RequestError"; this._data = data; // Maintains proper stack trace for where the error was thrown (only available on V8 engines) if (Error.captureStackTrace) { Error.captureStackTrace(this, RequestError); } } payload() { return { detail: this.message, error_code: this._code, data: this._data, }; } }