import {z} from 'zod' export type QuickLinkPagePatchPayload = {kb_id: string, title?: string | undefined, url?: string | undefined, open_in_new_tab?: boolean | undefined, pull_meta_data?: boolean | undefined, order?: number | undefined, id?: string | undefined}; export type MetaLinkPagePatchPayload = {kb_id: string, title?: string | undefined, url?: string | undefined, open_in_new_tab?: boolean | undefined, pull_meta_data?: boolean | undefined, order?: number | undefined, description?: string | undefined, image_url?: string | undefined, id?: string | undefined}; export type LinkGroupSlotItemPatchPayload = {id?: string | undefined, name?: string | undefined, url?: string | undefined, order?: number | undefined}; export type LinkGroupPagePatchPayload = {kb_id: string, title?: string | undefined, open_all_in_new_tab?: boolean | undefined, items?: Array | undefined, order?: number | undefined, id?: string | undefined}; export type VideoPagePatchPayload = {kb_id: string, title?: string | undefined, url?: string | undefined, description?: string | undefined, show_thumbnail?: boolean | undefined, thumbnail_url?: string | undefined, order?: number | undefined, id?: string | undefined}; export type SliderSlotItemPatchPayload = {id?: string | undefined, name?: string | undefined, url?: string | undefined, thumbnail_url?: string | undefined, description?: string | undefined, order?: number | undefined}; export type SliderPagePatchPayload = {kb_id: string, title?: string | undefined, order?: number | undefined, items?: Array | undefined, id?: string | undefined}; export type SlotsPayload = {quick_links?: Array | undefined, meta_links?: Array | undefined, link_groups?: Array | undefined, videos?: Array | undefined, sliders?: Array | undefined}; export type TabsPagePayload = {home_tab_enabled: boolean, avatar_enabled: boolean, slots: SlotsPayload}; export type CreateApiTabsPageArgs = {kb_id: string, body: TabsPagePayload}; export type CreateApiTabsPageOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const quickLinkResponse = z.object({id: z.string(), user_id: z.string(), kb_id: z.string(), title: z.string(), url: z.string(), open_in_new_tab: z.boolean(), pull_meta_data: z.boolean(), order: z.number().int(), disclaimer: z.string(), type: z.string()}); export const metaLinkResponse = z.object({id: z.string(), user_id: z.string(), kb_id: z.string(), title: z.string(), url: z.string(), open_in_new_tab: z.boolean(), pull_meta_data: z.boolean(), order: z.number().int(), disclaimer: z.string(), type: z.string(), description: z.string(), image_url: z.string()}); export const linkGroupSlotItemResponse = z.object({id: z.string(), name: z.string(), url: z.string(), order: z.number().int()}); export const linkGroupSlotResponse = z.object({id: z.string(), user_id: z.string(), kb_id: z.string(), title: z.string(), open_all_in_new_tab: z.boolean(), items: z.array(linkGroupSlotItemResponse), order: z.number().int(), disclaimer: z.string(), type: z.string()}); export const videoSlotResponse = z.object({id: z.string(), user_id: z.string(), kb_id: z.string(), title: z.string(), url: z.string(), description: z.string(), show_thumbnail: z.boolean(), thumbnail_url: z.string().optional(), order: z.number().int(), disclaimer: z.string(), type: z.string()}); export const sliderSlotItemResponse = z.object({id: z.string(), name: z.string(), url: z.string(), thumbnail_url: z.string(), description: z.string(), order: z.number().int()}); export const sliderSlotResponse = z.object({id: z.string(), user_id: z.string(), kb_id: z.string(), title: z.string(), items: z.array(sliderSlotItemResponse), order: z.number().int(), disclaimer: z.string(), type: z.string()}); export const slotsResponse = z.object({quick_links: z.array(quickLinkResponse).optional(), meta_links: z.array(metaLinkResponse).optional(), link_groups: z.array(linkGroupSlotResponse).optional(), videos: z.array(videoSlotResponse).optional(), sliders: z.array(sliderSlotResponse).optional()}); export const tabsResponse = z.object({home_tab_enabled: z.boolean(), avatar_enabled: z.boolean(), slots: slotsResponse}); 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 }; /** * Post Tabs Slots * * POST /tabs/page * * Also accepts auth: OAuth2PasswordBearer */ export const createApiTabsPage = async (args: CreateApiTabsPageArgs, options: CreateApiTabsPageOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.kb_id != null) query.append("kb_id", String(args.kb_id)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/tabs/page${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(tabsResponse, await res.json()) };