import {z} from 'zod' export type TextEnableTollFreeNumbersLoaInfoCommon = {projectId: string, displayName?: string | undefined, regionCode: string, numbers: Array, scheduledZipwhipProvisioningTime?: string | undefined, servicePlanId: string, callbackUrl?: string | undefined}; export type AuthorizedPerson = {firstName: string, lastName: string, email: string}; export type Address = {buildingNumber: string, street: string, city: string, state: string, postalCode: string, country: string}; export type DirectLoaInfo = {authorisedPerson: AuthorizedPerson, address: Address, currentVoiceCarrier: string}; export type ResellerLoaInfo = {businessName: string, authorisedPerson: AuthorizedPerson, address: Address, currentVoiceCarrier: string}; export type TextEnableTollFreeNumbersRequest = {allOf?: TextEnableTollFreeNumbersLoaInfoCommon | undefined, directLoaInfo: DirectLoaInfo} | {allOf?: TextEnableTollFreeNumbersLoaInfoCommon | undefined, resellerLoaInfo: ResellerLoaInfo}; export type CreateApiV1ProjectsProjectIdHostingOrdersTextEnableTollFreeNumbersArgs = {projectId: string, body: TextEnableTollFreeNumbersRequest}; export type CreateApiV1ProjectsProjectIdHostingOrdersTextEnableTollFreeNumbersOptions = {baseUrl?: string | undefined, Basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const authorizedPerson = z.object({firstName: z.string(), lastName: z.string(), email: z.string()}); export const address = z.object({buildingNumber: z.string(), street: z.string(), city: z.string(), state: z.string(), postalCode: z.string(), country: z.string()}); export const textEnableInfo = z.object({businessName: z.string().optional(), authorisedPerson: authorizedPerson.optional(), address: address.optional(), serviceProvider: z.string().optional(), currentVoiceCarrier: z.string().optional(), nnid: z.string().optional(), loaType: z.enum(["Direct", "Reseller"]).optional()}); export const hostingOrder = z.object({projectId: z.string().optional(), hostingOrderId: z.string().optional(), displayName: z.string().optional(), regionCode: z.string().optional(), state: z.enum(["HOSTING_ORDER_STATE_UNSPECIFIED", "DRAFT", "SUBMITTED", "WAITING_FOR_LOA_SIGNATURE", "IN_PROGRESS", "COMPLETED", "REJECTED"]).optional(), type: z.enum(["HOSTING_ORDER_TYPE_UNSPECIFIED", "IMPORT", "TYPE_TEXT_ENABLE"]).optional(), createdTime: z.string().optional(), updatedTime: z.string().optional(), scheduledOsrUpdateTime: z.string().optional(), servicePlanId: z.string().optional(), campaignId: z.string().optional(), migrateToSinchTmo: z.boolean().optional(), textEnableInfo: textEnableInfo.optional(), callbackUrl: z.string().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 }; /** * Text enable Toll Free numbers in hosting orders * * POST /v1/projects/{projectId}/hostingOrders:textEnableTollFreeNumbers * * Also accepts auth: OAuth2.0 */ export const createApiV1ProjectsProjectIdHostingOrdersTextEnableTollFreeNumbers = async (args: CreateApiV1ProjectsProjectIdHostingOrdersTextEnableTollFreeNumbersArgs, options?: CreateApiV1ProjectsProjectIdHostingOrdersTextEnableTollFreeNumbersOptions) => { const {baseUrl = "https://imported.numbers.api.sinch.com", fetch: fetchFn = fetch, Basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Basic !== undefined) headers.set('authorization', `Basic ${btoa(`${Basic.username}:${Basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/projects/${encodeURIComponent(String(args.projectId))}/hostingOrders:textEnableTollFreeNumbers${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(hostingOrder, await res.json()) };