import { GraphQLParams, YogaInitialContext, YogaLogger } from 'graphql-yoga'; import type { GraphQLError, ExecutionResult } from 'graphql'; export type { GraphQLError }; export type GraphQLData = ExecutionResult['data']; export type GraphQLResult = { data?: GraphQLData; errors?: GraphQLError[]; extensions?: Record; }; /** * State API interface for managing key-value pairs. */ export interface StateApi { /** * Get a value by key. * @param key Key to retrieve. */ get(key: string): Promise; /** * Put a key-value pair with optional TTL. * @param key Key to store. * @param value Value to store. * @param config Optional configuration object that may contain a TTL value in seconds. */ put(key: string, value: string, config?: { ttl?: number; }): Promise; /** * Delete a key-value pair. * @param key */ delete(key: string): Promise; } export interface UserContext extends YogaInitialContext { operationName?: string; document?: unknown; headers?: Record; secrets?: Record; state?: StateApi; } export type SourceHookConfig = Record; export interface HookConfig { blocking: boolean; composer?: string; module?: Module; fn?: string; } export interface MemoizedFns { beforeAll?: HookFunction; beforeSource?: Record; afterSource?: Record; afterAll?: HookFunction; } export interface Module { [key: string]: object | HookFunction | undefined; default?: Module; } /** * Configuration required when building/memoizing the handler wrapping the black box hook function. */ export interface HookBuildConfig { baseDir: string; logger: YogaLogger; memoizedFns: MemoizedFns; } /** * Configuration required when executing the hook handler. */ export interface HookExecConfig { payload: HookFunctionPayload; } /** * Configuration required when executing the source hook handler. */ export interface SourceHookExecConfig extends HookExecConfig { sourceName: string; hookType: string; payload: SourceHookFunctionPayload; } export interface HookFunctionPayloadContext { request: Request; params: GraphQLParams; body?: unknown; headers?: Record; secrets?: Record; state?: StateApi; logger?: YogaLogger; } export type HookFunctionPayload = { context: HookFunctionPayloadContext; document?: unknown; }; export type SourceHookFunctionPayload = HookFunctionPayload & { sourceName?: string; }; export type BeforeAllHookFunctionPayload = HookFunctionPayload & {}; export type BeforeSourceHookFunctionPayload = SourceHookFunctionPayload & { request: RequestInit; }; export type AfterSourceHookFunctionPayload = SourceHookFunctionPayload & { response?: Response; }; export type AfterAllHookFunctionPayload = HookFunctionPayload & { result?: GraphQLResult; }; export type HookFunction = (payload: HookFunctionPayload | SourceHookFunctionPayload) => Promise | HookResponse; export interface HookResponse { status: HookStatus; message: string; } export interface BeforeAllHookResponse extends HookResponse { data?: { headers?: { [headerName: string]: string; }; }; } export interface BeforeSourceHookResponse extends HookResponse { data?: { request?: RequestInit | { body?: string | ReadableStream; headers?: Record; method?: string; url?: string; }; }; } export interface AfterSourceHookResponse extends HookResponse { data?: { response?: Response | { body?: string | ReadableStream; headers?: Record; status?: number; statusText?: string; }; }; } export interface AfterAllHookResponse extends HookResponse { data?: { result?: GraphQLResult; }; } export declare enum HookStatus { SUCCESS = "SUCCESS", ERROR = "ERROR" } /** * Updates the context with new headers. */ export type UpdateContextFn = (data: { headers?: Record; }) => void; /** * Sets GraphQL result and stops further execution. */ export type SetResultAndStopExecutionFn = (result: ExecutionResult) => void; export { PLUGIN_HOOKS_ERROR_CODES, type PluginHooksErrorCode } from './errors';