/** * An app's own requests: any API, from the app's code. * * Plant data comes through `useLatest` and the widgets, but an app is more than plant data — a * maintenance system, an ERP, a weather service, the app's own backend. `request` is `fetch` with * the two things every such call wants: the body as JSON in and out, and an error when the server * says no. `useRequest` is the same as a hook, with loading and error states, so a screen that reads * an API is three lines rather than thirty. Plain `fetch` is allowed too; these are conveniences. * * **The login token goes only where it belongs.** `auth: 'iosense'` adds the viewer's IOSense token * — needed for IOSense's own APIs — and is refused for any other host, so an app cannot be talked * into sending the token somewhere else. Everything else is the browser's ordinary rules: a * third-party API answers a page only if its CORS allows this origin (the Studio's while previewing, * the app's own once deployed). */ export interface RequestOptions extends Omit { /** Sent as JSON, with the content type set. A string or FormData goes as it is. */ body?: unknown; /** `'iosense'`: add the viewer's IOSense token. Refused for a host that is not IOSense's. */ auth?: 'iosense'; } export declare class RequestError extends Error { status: number; /** What the server sent back, parsed as JSON when it was JSON. */ body: unknown; constructor(message: string, status: number, body: unknown); } /** Where the viewer's token may go: IOSense's API host, and IOSense's own domains. */ export declare function isIOSenseHost(url: string, apiBaseUrl: string): boolean; export declare function requestWith(token: string | null, apiBaseUrl: string, url: string, options?: RequestOptions): Promise; /** One request. Resolves with the parsed body; throws `RequestError` on a non-2xx answer. */ export declare function request(url: string, options?: RequestOptions): Promise; export declare function publishSession(session: { token: string | null; apiBaseUrl: string; }): void; export interface UseRequestResult { data: T | undefined; loading: boolean; error: Error | null; /** Ask again now. */ refresh: () => void; } export interface UseRequestOptions extends RequestOptions { /** Ask again every so often. Default: once. */ refreshMs?: number; /** False: do not ask yet (the URL is not ready, a form is not filled in). */ enabled?: boolean; } /** * A request as a hook: asked when the component mounts, again when the URL changes. * * Options are compared as JSON, so `useRequest(url, { body: { mill } })` asks again when `mill` * changes and not on every render. */ export declare function useRequest(url: string | null | undefined, options?: UseRequestOptions): UseRequestResult;