/**
 *
 * common
 *
 */ import type { DocumentNode , GraphQLError } from 'graphql' ; category export const TOKEN_HEADER_KEY = 'x-graphql-event-stream-token' as const ; category export const TOKEN_QUERY_KEY = 'token' as const ; category export interface RequestParams { operationName ? : string | undefined ; query : DocumentNode | string ; variables ? : Record < string , unknown > | undefined ; extensions ? : Record < string , unknown > | undefined ; } category export interface StreamMessage < ForID extends boolean = false , E extends StreamEvent = StreamEvent > { event : E ; data : ForID extends true ? StreamDataForID < E > : StreamData < E > ; } category export type StreamEvent = 'next' | 'complete' ; category export function validateStreamEvent ( e ) { e = e as StreamEvent ; if ( e !== 'next' && e !== 'complete' ) throw new Error ( `Invalid stream event "${ e }"` ) ; return e ; } category export interface ExecutionResult < Data = Record < string , unknown > , Extensions = Record < string , unknown > > { errors ? : ReadonlyArray < GraphQLError > ; data ? : Data | null ; hasNext ? : boolean ; extensions ? : Extensions ; } category export interface ExecutionPatchResult < Data = unknown , Extensions = Record < string , unknown > > { errors ? : ReadonlyArray < GraphQLError > ; data ? : Data | null ; path ? : ReadonlyArray < string | number > ; label ? : string ; hasNext : boolean ; extensions ? : Extensions ; } category export type StreamData < E extends StreamEvent = StreamEvent > = E extends 'next' ? ExecutionResult | ExecutionPatchResult : E extends 'complete' ? null : never ; category export type StreamDataForID < E extends StreamEvent = StreamEvent > = E extends 'next' ? { id : string ; payload : ExecutionResult | ExecutionPatchResult ; } : E extends 'complete' ? { id : string ; } : never ; category export function parseStreamData ( e , data ) { if ( data ) { try { data = JSON . parse ( data ) ; } catch { throw new Error ( 'Invalid stream data' ) ; } } if ( e === 'next' && ! data ) throw new Error ( 'Stream data must be an object for "next" events' ) ; return ( data || null ) as StreamData ; } category export interface Sink < T = unknown > { /** Next value arriving. */ next ( value ) : void ; /** An error that has occured. This function "closes" the sink. */ error ( error ) : void ; /** The sink has completed. This function "closes" the sink. */ complete ( ) : void ; }