import {z} from 'zod' export type GoogleTagManagerIntegration = {enabled: boolean, type: 'tracking', config_data: {id: string, l?: (string | null) | undefined, preview?: (string | null) | undefined, cookies_win?: (boolean | null) | undefined, debug?: (boolean | null) | undefined, npa?: (boolean | null) | undefined, data_layer?: (string | null) | undefined, env_name?: (string | null) | undefined, auth_referrer_policy?: (string | null) | undefined}} | null; export type GoogleAnalytics4Integration = {enabled: boolean, type: 'analytics', config_data: {id: string, l?: (string | null) | undefined}} | null; export type EqualWebIntegration = {enabled: boolean, type: 'accessibility', config_data: {site_key: string, widget_config?: Record | undefined}} | null; export type PortalIntegrations = {google_tag_manager?: GoogleTagManagerIntegration | undefined, google_analytics_4?: GoogleAnalytics4Integration | undefined, equal_web?: EqualWebIntegration | undefined}; export type UpdateApiV3PortalsPortalIdIntegrationsArgs = {portalId: string, body: PortalIntegrations}; export type UpdateApiV3PortalsPortalIdIntegrationsOptions = {baseUrl?: string | undefined, personalAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const googleTagManagerIntegration = z.object({enabled: z.boolean(), type: z.literal("tracking"), config_data: z.object({id: z.string().min(5).max(20), l: z.string().max(100).nullable().optional(), preview: z.string().max(100).nullable().optional(), cookies_win: z.boolean().nullable().optional(), debug: z.boolean().nullable().optional(), npa: z.boolean().nullable().optional(), data_layer: z.string().max(100).nullable().optional(), env_name: z.string().max(100).nullable().optional(), auth_referrer_policy: z.string().max(100).nullable().optional()})}).nullable(); export const googleAnalytics4Integration = z.object({enabled: z.boolean(), type: z.literal("analytics"), config_data: z.object({id: z.string().min(5).max(20), l: z.string().max(100).nullable().optional()})}).nullable(); export const equalWebIntegration = z.object({enabled: z.boolean(), type: z.literal("accessibility"), config_data: z.object({site_key: z.string().min(32).max(32), widget_config: z.object({}).optional()})}).nullable(); export const portalIntegrations = z.object({google_tag_manager: googleTagManagerIntegration.optional(), google_analytics_4: googleAnalytics4Integration.optional(), equal_web: equalWebIntegration.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Replace Integration Configurations * * PUT /v3/portals/{portalId}/integrations * * Also accepts auth: systemAccountAccessToken, konnectAccessToken, serviceAccessToken */ export const updateApiV3PortalsPortalIdIntegrations = async (args: UpdateApiV3PortalsPortalIdIntegrationsArgs, options?: UpdateApiV3PortalsPortalIdIntegrationsOptions) => { const {baseUrl = "https://global.api.konghq.com", fetch: fetchFn = fetch, personalAccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (personalAccessToken !== undefined) headers.set('authorization', `Bearer ${personalAccessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/portals/${encodeURIComponent(String(args.portalId))}/integrations${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(portalIntegrations, await res.json()) };