import {z} from 'zod' export type RuleDirection = 'inbound' | 'outbound' | 'both'; export type PhoneRuleAction = 'allow' | 'block'; export type PhoneRuleMatchType = 'exact_number'; export type PhoneContactRuleCreate = {direction?: RuleDirection | undefined, action: PhoneRuleAction, match_type?: PhoneRuleMatchType | undefined, match_target: string}; export type CreateApiApiV1PhoneNumbersPhoneNumberIdContactRulesArgs = {phone_number_id: string, body: PhoneContactRuleCreate}; export type CreateApiApiV1PhoneNumbersPhoneNumberIdContactRulesOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const ruleDirection = z.enum(["inbound", "outbound", "both"]); export const phoneRuleAction = z.enum(["allow", "block"]); export const phoneRuleMatchType = z.literal("exact_number"); export const resourceStatus = z.enum(["active", "paused", "deleted"]); export const phoneContactRuleResponse = z.object({id: z.string(), phone_number_id: z.string(), direction: ruleDirection.optional(), action: phoneRuleAction, match_type: phoneRuleMatchType, match_target: z.string(), status: resourceStatus, created_at: z.string(), updated_at: z.string()}); 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 }; /** * Create Contact Rule * * POST /api/v1/phone/numbers/{phone_number_id}/contact-rules * * Also accepts auth: BearerAuth */ export const createApiApiV1PhoneNumbersPhoneNumberIdContactRules = async (args: CreateApiApiV1PhoneNumbersPhoneNumberIdContactRulesArgs, options: CreateApiApiV1PhoneNumbersPhoneNumberIdContactRulesOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/phone/numbers/${encodeURIComponent(String(args.phone_number_id))}/contact-rules${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(phoneContactRuleResponse, await res.json()) };