UNPKG

@soleil-se/app-util

Version:

Utility functions for WebApps, RESTApps and Widgets in Sitevision.

57 lines (56 loc) 2.05 kB
/** * Fetches JSON data from a specified URI. * URI:s starting with `/rest-api`, `/appresource`, `/edit-app-config` or a protocol, for example * `https://` will be left as is. Other URI:s willbe converted to match a route in the current app * with `getRouteUri`. * * @template T * @param {string} uri - The URI to fetch JSON data from. * @param {Options} [options] - The options for the fetch request with some extensions. * @returns {Promise<T>} A promise that resolves to the JSON response. * @throws {FetchError} If the response is not successful or cannot be parsed as JSON. */ export default function fetchJson<T>(uri: string, options?: Options): Promise<T>; /** * @typedef {Object} ExtensionOptions * @property {{ [key: string]: unknown }} [params] - The parameters to be included in the request * URL. * @property {number} [retries=0] - The number of retries to attempt in case of a timeout error. * * @typedef {RequestInit & ExtensionOptions} Options */ /** * Custom error class for fetch-related errors with additional HTTP context. */ export class FetchError extends Error { /** * @param {string} message - The error message. * @param {Object} [options] - Error options. * @param {number} [options.status] - HTTP status code. * @param {boolean} [options.aborted=false] - Whether the request was aborted. * @param {Object.<string, unknown>} [options.additionalProps] - Additional properties. */ constructor(message: string, { status, aborted, ...additionalProps }?: { status?: number; aborted?: boolean; additionalProps?: { [x: string]: unknown; }; }); status: number; aborted: boolean; } export type ExtensionOptions = { /** * - The parameters to be included in the request * URL. */ params?: { [key: string]: unknown; }; /** * - The number of retries to attempt in case of a timeout error. */ retries?: number; }; export type Options = RequestInit & ExtensionOptions;