import {z} from 'zod' export type GetApiHdDesksArgs = {host: string}; export type GetApiHdDesksOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const helpDeskDesk = z.union([z.object({kind: z.literal("desk"), desk_id: z.string(), handle: z.string(), canonical_host: z.string(), organization_name: z.string().nullable(), branding: z.object({color: z.string().nullable()}), auth_mode: z.enum(["identity", "email_pin", "identity_unavailable"]), identity: z.object({issuer_account_id: z.string(), client_id: z.string()}).nullable(), customer_can_mark_resolved: z.boolean(), knowledge_base_enabled: z.boolean(), portal_client: z.literal("help-desk-portal")}), z.object({kind: z.literal("moved"), moved_to: z.string()})]); export const responseMeta = z.object({api_version: z.string().optional(), request_id: z.string().optional(), response_ms: z.number().int().optional(), timestamp: z.number().int().optional(), pools: z.object({}).optional(), billing: z.object({past_due: z.boolean().optional()}).optional(), quota: z.object({limit: z.number().int().optional(), used: z.number().int().optional(), remaining: z.number().int().optional(), resets_at: z.number().int().optional()}).optional()}); export const getApiHdDesksResponse = z.object({data: helpDeskDesk, meta: responseMeta.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 }; /** * Look up a help desk by its host * * GET /hd/desks */ export const getApiHdDesks = async (args: GetApiHdDesksArgs, options?: GetApiHdDesksOptions) => { const {baseUrl = "https://api.sparkvault.com/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.host != null) query.append("host", String(args.host)) const url = `${baseUrl.replace(/\/+$/, '')}/hd/desks${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiHdDesksResponse, await res.json()) };