import { removeUndef } from "@adpt/utils" ; import * as ld from "lodash" ; import { PluginObservations } from "../deploy" ; import { Observations } from "../observers" ; import { prepareAllObservationsForJson , PreparedObservations , reconstituteAllObservations } from "../observers/serialize" ; export interface FullObservations { plugin ? : PluginObservations ; observer ? : Observations ; } export interface PreparedFullObservations { plugin : PluginObservations ; observer : PreparedObservations ; } function isObject ( x ) { return ld . isObject ( x ) ; } export function hasPreparedFullObservationsShape ( candidate ) { if ( ! isObject ( candidate ) ) return false ; const plugin = ( candidate as any ) . plugin ; const observer = ( candidate as any ) . observer ; if ( plugin && ! isObject ( plugin ) ) return false ; if ( observer ) return hasPreparedFullObservationsShape ( observer ) ; return true ; } export function parseFullObservationsJson ( json ) { const candidate = JSON . parse ( json ) ; if ( ! hasPreparedFullObservationsShape ( candidate ) ) { if ( ! isObject ( candidate ) ) throw new Error ( "Full observations is not an object" ) ; if ( ( candidate as any ) . observer ) { reconstituteAllObservations ( ( candidate as any ) . observer ) ; } throw new Error ( "Illegal shape for full observations" ) ; } const observer = candidate . observer ? reconstituteAllObservations ( candidate . observer ) : undefined ; const plugin = candidate . plugin ; return removeUndef ( { plugin , observer } ) ; } export function stringifyFullObservations ( observations ) { const observer = observations . observer ; const prepped = { plugin : observations . plugin , observer : observer ? prepareAllObservationsForJson ( observer ) : observer } ; return JSON . stringify ( removeUndef ( prepped ) ) ; }