import {z} from 'zod' export type GetApiInboundRulesArgs = {filter?: string | undefined, search?: string | undefined, orderby?: string | undefined, skip?: number | undefined, top?: number | undefined}; export type GetApiInboundRulesOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiInboundRulesResponse = z.object({count: z.number().int().gte(0).optional(), items: z.array(z.object({id: z.string().optional(), name: z.string().min(1).max(64).optional(), enabled: z.boolean().optional(), cid_number_mask: z.string().optional(), did_numbers: z.string().min(1).optional(), office_hours_forward_rule: z.object({action: z.enum(["FORWARD_TO_NUMBER", "FORWARD_TO_VOICEMAIL", "HANGUP"]).optional(), number: z.string().optional()}).optional(), non_office_hours_forward_rule: z.object({action: z.enum(["FORWARD_TO_NUMBER", "FORWARD_TO_VOICEMAIL", "HANGUP"]).optional(), number: z.string().optional()}).optional(), holiday_forward_rule: z.object({action: z.enum(["FORWARD_TO_NUMBER", "FORWARD_TO_VOICEMAIL", "HANGUP"]).optional(), number: z.string().optional()}).optional(), office_hours: z.object({mode: z.enum(["GLOBAL", "CUSTOM"]).optional(), monday: z.object({enabled: z.boolean().optional(), ranges: z.array(z.object({from: z.string().optional(), to: z.string().optional()})).optional()}).optional(), tuesday: z.object({enabled: z.boolean().optional(), ranges: z.array(z.object({from: z.string().optional(), to: z.string().optional()})).optional()}).optional(), wednesday: z.object({enabled: z.boolean().optional(), ranges: z.array(z.object({from: z.string().optional(), to: z.string().optional()})).optional()}).optional(), thursday: z.object({enabled: z.boolean().optional(), ranges: z.array(z.object({from: z.string().optional(), to: z.string().optional()})).optional()}).optional(), friday: z.object({enabled: z.boolean().optional(), ranges: z.array(z.object({from: z.string().optional(), to: z.string().optional()})).optional()}).optional(), saturday: z.object({enabled: z.boolean().optional(), ranges: z.array(z.object({from: z.string().optional(), to: z.string().optional()})).optional()}).optional(), sunday: z.object({enabled: z.boolean().optional(), ranges: z.array(z.object({from: z.string().optional(), to: z.string().optional()})).optional()}).optional()}).optional(), holidays: z.array(z.object({id: z.string().optional(), name: z.string().min(1).max(64).optional(), every_year: z.boolean().optional(), consecutive: z.boolean().optional(), year_start: z.number().int().optional(), year_end: z.number().int().optional(), month_start: z.number().int().optional(), month_end: z.number().int().optional(), day_start: z.number().int().optional(), day_end: z.number().int().optional(), hour_start: z.number().int().optional(), hour_end: z.number().int().optional(), minute_start: z.number().int().optional(), minute_end: z.number().int().optional()})).optional(), provider_id: z.string().optional(), enable_advanced_routing: z.boolean().optional(), advanced_routing: z.string().optional(), play_recording_disclaimer: z.boolean().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 }; /** * List inbound rules * * GET /inbound_rules */ export const getApiInboundRules = async (args: GetApiInboundRulesArgs, options?: GetApiInboundRulesOptions) => { 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 (args.filter != null) query.append("filter", String(args.filter)) if (args.search != null) query.append("search", String(args.search)) if (args.orderby != null) query.append("orderby", String(args.orderby)) if (args.skip != null) query.append("skip", String(args.skip)) if (args.top != null) query.append("top", String(args.top)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/inbound_rules${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(getApiInboundRulesResponse, await res.json()) };