@@ -3,7 +3,6 @@ import { getEmbedConfig } from "./embed/embedConfig";
33import { InterceptedApiType , BaseViewConfig , EmbedConfig , InterceptV2Flags , EmbedEvent } from "./types" ;
44import { logger } from "./utils/logger" ;
55
6-
76const defaultUrls : Record < Exclude < InterceptedApiType , InterceptedApiType . ALL > , string [ ] > = {
87 [ InterceptedApiType . METADATA ] : [
98 '/prism/?op=CreateAnswerSession' ,
@@ -43,8 +42,8 @@ const processInterceptUrls = (interceptUrls: (string | InterceptedApiType)[]) =>
4342 } )
4443 return processedUrls . map ( url => formatInterceptUrl ( url ) ) ;
4544}
46- export const getInterceptInitData = ( embedConfig : EmbedConfig , viewConfig : BaseViewConfig ) : InterceptV2Flags => {
47-
45+ export const getInterceptInitData = ( viewConfig : BaseViewConfig ) : InterceptV2Flags => {
46+ const embedConfig = getEmbedConfig ( ) ;
4847 const enableApiIntercept = ( embedConfig . enableApiIntercept || viewConfig . enableApiIntercept ) && ( viewConfig . enableApiIntercept !== false ) ;
4948
5049 if ( ! enableApiIntercept ) return {
@@ -69,16 +68,33 @@ export const getInterceptInitData = (embedConfig: EmbedConfig, viewConfig: BaseV
6968 } ;
7069}
7170
71+ const parseJson = ( jsonString : string ) : [ any , Error | null ] => {
72+ try {
73+ const json = JSON . parse ( jsonString ) ;
74+ return [ json , null ] ;
75+ } catch ( error ) {
76+ return [ null , error ] ;
77+ }
78+ }
79+
7280/**
7381 *
7482 * @param fetchInit
7583 */
7684const parseInterceptData = ( eventDataString : any ) => {
7785
7886 try {
79- const { input, init } = JSON . parse ( eventDataString ) ;
87+ const [ parsedData , error ] = parseJson ( eventDataString ) ;
88+ if ( error ) {
89+ return [ null , error ] ;
90+ }
91+
92+ const { input, init } = parsedData ;
8093
81- init . body = JSON . parse ( init . body ) ;
94+ const [ parsedBody , bodyParseError ] = parseJson ( init . body ) ;
95+ if ( ! bodyParseError ) {
96+ init . body = parsedBody ;
97+ }
8298
8399 const parsedInit = { input, init } ;
84100 return [ parsedInit , null ] ;
@@ -87,7 +103,12 @@ const parseInterceptData = (eventDataString: any) => {
87103 }
88104}
89105
90- export const handleInterceptEvent = async ( params : { eventData : any , executeEvent : ( eventType : EmbedEvent , data : any ) => void , embedConfig : EmbedConfig , viewConfig : BaseViewConfig , getUnsavedAnswerTml : ( props : { sessionId ?: string , vizId ?: string } ) => Promise < { tml : string } > } ) => {
106+ export const handleInterceptEvent = async ( params : {
107+ eventData : any ,
108+ executeEvent : ( eventType : EmbedEvent , data : any ) => void ,
109+ viewConfig : BaseViewConfig ,
110+ getUnsavedAnswerTml : ( props : { sessionId ?: string , vizId ?: string } ) => Promise < { tml : string } >
111+ } ) => {
91112
92113 const { eventData, executeEvent, viewConfig, getUnsavedAnswerTml } = params ;
93114
0 commit comments