const komoFormFieldPrefix = 'komo_f:'; const komoExtensionDataPrefix = 'komo_e:'; // These must match parameters defined on GetOAuthAuthorizeUrlCommand, and are case sensitive const authorizeEndpointParams = { returnTo: 'ReturnTo', passthrough: 'Passthrough', }; /** * Given a raw URL, form prefill values, and extension data values, return the URL with the prefill values and extension data values added as query parameters. * @param rawUrl * @param formPrefillValues - The form prefill values to add to the URL. * @param extensionDataValues - The extension data values to add to the URL. * @returns The URL with the prefill values and extension data values added as query parameters. */ export const tryResolveEmbeddedContentUrl = ( rawUrl?: string, formPrefillValues?: Record, extensionDataValues?: Record ) => { if (!rawUrl || (!formPrefillValues && !extensionDataValues)) return rawUrl; try { const url = new URL(rawUrl); if (formPrefillValues) { Object.keys(formPrefillValues).forEach((key) => { if (!formPrefillValues[key]) return; url.searchParams.set(komoFormFieldPrefix + key, formPrefillValues[key]); }); } if (extensionDataValues) { Object.keys(extensionDataValues).forEach((key) => { if (!extensionDataValues[key]) return; url.searchParams.set( komoExtensionDataPrefix + key, JSON.stringify(extensionDataValues[key]) ); }); } return url.toString(); } catch { return rawUrl; } }; /** * Resolve the full iframe URL, taking into account the embedded content URL and the auth URL. * @param rawUrl - The raw URL to resolve. * @param embedAuthUrl - The embed auth URL to resolve. * @param authPassthroughParams - The auth passthrough params to add to the URL. * @param formPrefillValues - The form prefill values to add to the URL. * @param extensionDataValues - The extension data values to add to the URL. * @returns The full iframe URL. */ export const tryResolveFullIframeUrl = ( rawUrl?: string, embedAuthUrl?: string, authPassthroughParams?: URLSearchParams, formPrefillValues?: Record, extensionDataValues?: Record ) => { const embeddedContentUrl = tryResolveEmbeddedContentUrl( rawUrl, formPrefillValues, extensionDataValues ); if (!embeddedContentUrl) { console.warn('No embedded content URL provided'); return undefined; } // If there is an auth URL provided, we need to first load the Auth URL, providing the content URL as an URL encoded "returnTo" parameter. if (embedAuthUrl) { try { const authUrlWithReturnTo = new URL(embedAuthUrl); authUrlWithReturnTo.searchParams.set( authorizeEndpointParams.returnTo, embeddedContentUrl ); // We also need to URL encode passthrough params and provide them to "passthrough" if (authPassthroughParams) { authUrlWithReturnTo.searchParams.set( authorizeEndpointParams.passthrough, authPassthroughParams.toString() ); } return authUrlWithReturnTo.toString(); } catch (error) { console.error('Error resolving auth URL', error); return undefined; } } // otherwise just load the embedded content url return embeddedContentUrl; };