import {z} from 'zod' export type GetApiApiV4NumbersNumberIdArgs = {number_id: number}; export type GetApiApiV4NumbersNumberIdOptions = {baseUrl: string, StaticKey?: string | undefined, fetch?: typeof fetch | undefined}; export const callerIDGroupShort = z.object({id: z.number().int().optional(), name: z.string().optional()}); export const flowShort = z.object({id: z.string().optional(), name: z.string().optional()}); export const number = z.object({id: z.number().int().optional(), phone_number: z.string().optional(), label: z.string().nullable().optional(), type: z.enum(["mobile", "national", "toll_free", "shared_cost", "geographic"]).optional(), country: z.string().nullable().optional(), city: z.string().nullable().optional(), channels: z.number().int().nullable().optional(), provider: z.enum(["Voiso", "3rd party"]).nullable().optional(), pricing: z.object({activation_fee: z.string().nullable().optional(), monthly_fee: z.string().nullable().optional(), partial_month_fee: z.string().nullable().optional(), price_per_min: z.string().nullable().optional()}).optional(), caller_id_groups: z.array(callerIDGroupShort).optional(), flow: flowShort.nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const getApiApiV4NumbersNumberIdResponse = z.object({number: number.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 phone number * * GET /api/v4/numbers/{number_id} */ export const getApiApiV4NumbersNumberId = async (args: GetApiApiV4NumbersNumberIdArgs, options: GetApiApiV4NumbersNumberIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, StaticKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (StaticKey !== undefined) headers.set('authorization', `Bearer ${StaticKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v4/numbers/${encodeURIComponent(String(args.number_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(getApiApiV4NumbersNumberIdResponse, await res.json()) };