import {z} from 'zod' export type GetApiCasesCaseTokenArgs = {case_token: string}; export type GetApiCasesCaseTokenOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const linkReference = z.object({href: z.string().optional()}); export const embedded = z.object({case_token: z.string().optional(), entity_token: z.string().optional(), status: z.string().optional(), closed: z.boolean().optional(), sandbox: z.boolean().optional(), unread: z.boolean().optional(), created_at: z.number().optional(), updated_at: z.number().optional(), closed_at: z.number().optional(), assigned_agent_email: z.string().optional(), assigned_agent_name: z.string().optional(), program_name: z.string().optional(), alerts: z.array(z.string()).optional(), customer_tag_type_name: z.string().optional(), customer_tag_type_description: z.string().optional(), is_escalated: z.boolean().optional(), is_manual: z.boolean().optional(), created_by_agent_email: z.string().optional(), created_by_agent_name: z.string().optional(), _embedded: z.object({entity: z.object({name: z.string().optional(), entity_token: z.string().optional(), external_entity_id: z.string().optional(), type: z.string().optional(), _links: z.object({self: linkReference.optional()}).optional()}).optional()}).optional(), _links: z.object({self: linkReference.optional(), evidences: linkReference.optional(), works: linkReference.optional()}).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 }; /** * Get a case by a uniquely defined case token * * GET /cases/{case_token} * * Also accepts auth: basic */ export const getApiCasesCaseToken = async (args: GetApiCasesCaseTokenArgs, options?: GetApiCasesCaseTokenOptions) => { const {baseUrl = "https://sandbox.alloy.co/v1", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/cases/${encodeURIComponent(String(args.case_token))}${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(embedded, await res.json()) };