import {z} from 'zod' export type PortalMenuItem = {path: string, title: string, visibility: 'public' | 'private', external: boolean}; export type PortalFooterMenuSection = {title: string, items: Array}; export type PortalCustomizationV3 = {theme?: ({name?: string | undefined, mode?: ('light' | 'dark' | 'system') | undefined, colors?: ({primary?: string | undefined}) | undefined}) | undefined, layout?: string | undefined, css?: (string | null) | undefined, menu?: ({main?: Array | undefined, footer_sections?: Array | undefined, footer_bottom?: Array | undefined}) | undefined, spec_renderer?: ({try_it_ui?: boolean | undefined, try_it_insomnia?: boolean | undefined, infinite_scroll?: boolean | undefined, show_schemas?: boolean | undefined, hide_internal?: boolean | undefined, hide_deprecated?: boolean | undefined, allow_custom_server_urls?: boolean | undefined, try_it_ui_audience?: ('all' | 'authenticated' | 'registered') | undefined}) | undefined, robots?: (string | null) | undefined}; export type UpdateApiV3PortalsPortalIdCustomizationArgs = {portalId: string, body: PortalCustomizationV3}; export type UpdateApiV3PortalsPortalIdCustomizationOptions = {baseUrl?: string | undefined, personalAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const portalMenuItem = z.object({path: z.string().max(512), title: z.string(), visibility: z.enum(["public", "private"]), external: z.boolean()}); export const portalFooterMenuSection = z.object({title: z.string().max(512), items: z.array(portalMenuItem)}); export const portalCustomizationV3 = z.object({theme: z.object({name: z.string().optional(), mode: z.enum(["light", "dark", "system"]).optional(), colors: z.object({primary: z.string().optional()}).optional()}).optional(), layout: z.string().optional(), css: z.string().nullable().optional(), menu: z.object({main: z.array(portalMenuItem).optional(), footer_sections: z.array(portalFooterMenuSection).optional(), footer_bottom: z.array(portalMenuItem).optional()}).optional(), spec_renderer: z.object({try_it_ui: z.boolean().optional(), try_it_insomnia: z.boolean().optional(), infinite_scroll: z.boolean().optional(), show_schemas: z.boolean().optional(), hide_internal: z.boolean().optional(), hide_deprecated: z.boolean().optional(), allow_custom_server_urls: z.boolean().optional(), try_it_ui_audience: z.enum(["all", "authenticated", "registered"]).optional()}).optional(), robots: z.string().nullable().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 Customization * * PUT /v3/portals/{portalId}/customization * * Also accepts auth: systemAccountAccessToken, konnectAccessToken, serviceAccessToken */ export const updateApiV3PortalsPortalIdCustomization = async (args: UpdateApiV3PortalsPortalIdCustomizationArgs, options?: UpdateApiV3PortalsPortalIdCustomizationOptions) => { 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))}/customization${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(portalCustomizationV3, await res.json()) };