@graphql-hive/laboratory
Version:
[Hive](https://the-guild.dev/graphql/hive) is a fully open-source schema registry, analytics, metrics and gateway for [GraphQL federation](https://the-guild.dev/graphql/hive/federation) and other GraphQL APIs.
108 lines (107 loc) • 5.03 kB
TypeScript
import { LaboratoryEnv, LaboratoryEnvActions, LaboratoryEnvState } from './env';
import { LaboratoryPlugin } from './plugins';
import { Dispatch, SetStateAction } from 'react';
export interface LaboratoryPreflightLog {
level: 'log' | 'warn' | 'error' | 'info' | 'system';
message: unknown[];
createdAt: string;
/** Where in the script this came from, when the browser reports a usable stack. */
line?: number;
column?: number;
}
/**
* Maps a stack frame back onto the user's script. The script runs inside a generated
* `AsyncFunction`, so it starts three lines in (`async function anonymous(…`, `) {`,
* `with(lab){`). A `console.x` frame points at the member name rather than the statement,
* which is what `columnOffset` is for.
*/
export declare const readLineAndColumn: (stack?: string, columnOffset?: number) => {
line?: undefined;
column?: undefined;
} | {
line: number;
column: number;
};
export interface LaboratoryPreflightResult {
status: 'success' | 'error';
error?: string;
logs: LaboratoryPreflightLog[];
env: LaboratoryEnv;
headers: Record<string, string>;
pluginsState: Record<string, any>;
}
export interface LaboratoryPreflight {
enabled: boolean;
script: string;
lastTestResult?: LaboratoryPreflightResult | null;
}
export interface LaboratoryPreflightState {
preflight: LaboratoryPreflight | null;
/** Logs from every run, oldest first, not just the ones started from the Test button. */
preflightLogs: LaboratoryPreflightLog[];
isPreflightRunning: boolean;
}
/** The script-supplied parts of a `lab.prompt()` call. */
export interface LaboratoryPreflightPromptField {
title?: string;
defaultValue?: string;
description?: string;
placeholder?: string;
}
/** A single `lab.prompt()` call waiting on the user. */
export interface LaboratoryPreflightPromptRequest extends LaboratoryPreflightPromptField {
onSubmit?: (value: string | null) => void;
}
export type LaboratoryPreflightPrompt = (title: string, defaultValue: string, options?: {
placeholder?: string;
description?: string;
}) => Promise<string | null>;
/**
* How long a script may run before the worker is killed.
* The clock only covers execution: it is paused while a
* prompt is waiting on the user.
*/
export declare const PREFLIGHT_TIMEOUT = 30000;
/**
* Environment values are interpolated into headers as text and persisted by the host, so a
* script can only store JSON scalars. Defined here and injected into the worker by source, so
* the rule is stated once.
*/
export declare const isValidEnvValue: (value: unknown) => boolean;
export interface LaboratoryPreflightRunOptions {
/** Aborting terminates the worker and settles the run as an error. */
signal?: AbortSignal;
/** Called as each log arrives, so a long-running script reports before it finishes. */
onLog?: (log: LaboratoryPreflightLog) => void;
}
export interface LaboratoryPreflightActions {
setPreflight: (preflight: LaboratoryPreflight) => void;
runPreflight: (plugins?: LaboratoryPlugin[], pluginsState?: Record<string, any>) => Promise<LaboratoryPreflightResult | null>;
/** Runs the script on demand, ignoring `preflight.enabled`, and records the result. */
testPreflight: (plugins?: LaboratoryPlugin[], pluginsState?: Record<string, any>) => Promise<LaboratoryPreflightResult | null>;
/** Stops every in-flight run and releases any prompt waiting on the user. */
abortPreflight: () => void;
clearPreflightLogs: () => void;
setLastTestResult: (result: LaboratoryPreflightResult | null) => void;
}
/**
* Owns the one prompt dialog every preflight run shares. Runs overlap in practice (an
* operation run while schema polling has a prompt open), and only one request can be on
* screen, so a displaced request is answered with null rather than left waiting.
*/
export declare const usePreflightPrompt: () => {
isPreflightPromptModalOpen: boolean;
setIsPreflightPromptModalOpen: Dispatch<SetStateAction<boolean>>;
preflightPromptModalProps: LaboratoryPreflightPromptRequest;
openPreflightPromptModal: (request: LaboratoryPreflightPromptRequest) => void;
closePreflightPromptModal: () => void;
};
export declare const usePreflight: (props: {
defaultPreflight?: LaboratoryPreflight | null;
onPreflightChange?: (preflight: LaboratoryPreflight | null) => void;
envApi: LaboratoryEnvState & LaboratoryEnvActions;
openPreflightPromptModal?: (request: LaboratoryPreflightPromptRequest) => void;
/** Releases a prompt still waiting on the user, so aborting doesn't strand the dialog. */
closePreflightPromptModal?: () => void;
}) => LaboratoryPreflightState & LaboratoryPreflightActions;
export declare function runIsolatedLabScript(script: string, env: LaboratoryEnv, prompt?: LaboratoryPreflightPrompt, plugins?: LaboratoryPlugin[], pluginsState?: Record<string, any>, options?: LaboratoryPreflightRunOptions): Promise<LaboratoryPreflightResult>;