import {z} from 'zod' export type GetApiRingGroupsIdArgs = {id: string}; export type GetApiRingGroupsIdOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiRingGroupsIdResponse = z.object({id: z.string().optional(), name: z.string().min(1).max(64).optional(), extension_number: z.string().min(3).max(64).optional(), ring_time: z.number().int().gte(0).optional(), ring_strategy: z.enum(["RING_SIMULTANEOUSLY", "PRIORITIZED_HUNT", "CYCLIC_HUNT", "LEAST_WORKED_HUNT", "PAGING_INTERCOM", "SKILL_BASED_ROUTING_PRIORITIZED_HUNT", "SKILL_BASED_ROUTING_CYCLIC_HUNT", "SKILL_BASED_ROUTING_LEAST_WORKED_HUNT"]).optional(), skip_busy_member: z.boolean().optional(), enable_paid: z.boolean().optional(), enable_prid: z.boolean().optional(), extension_number_as_to_header: z.boolean().optional(), no_answer_forward_rule: z.object({action: z.enum(["FORWARD_TO_NUMBER", "FORWARD_TO_VOICEMAIL", "REPEAT", "HANGUP"]).optional(), number: z.string().optional()}).optional(), outbound_caller_ids: z.array(z.object({provider_id: z.string().optional(), caller_id: z.string().min(1).max(64).optional(), description: z.string().max(1024).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 }; /** * Retrieve a ring group * * GET /ring_groups/{id} */ export const getApiRingGroupsId = async (args: GetApiRingGroupsIdArgs, options?: GetApiRingGroupsIdOptions) => { const {baseUrl = "https://HOSTNAME:8887/api", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/ring_groups/${encodeURIComponent(String(args.id))}${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(getApiRingGroupsIdResponse, await res.json()) };