import {z} from 'zod' export type SiteAddress = {id?: (string | null) | undefined}; export type EmergencyNotification = {type?: ('EMAIL' | null) | undefined, values?: (Array | null) | undefined}; export type CallerIdSettings = {mainNumber?: (string | null) | undefined, setMainNumberAsCallerId?: (boolean | null) | undefined, shareMainNumberAsCallerId?: (boolean | null) | undefined, displayName?: (string | null) | undefined}; export type Site = {id?: (string | null) | undefined, name: string, pbxName: string, locale: 'da-DK' | 'de-DE' | 'en-AU' | 'en-GB' | 'en-US' | 'es-ES' | 'fi-FI' | 'fr-BE' | 'fr-CA' | 'fr-CH' | 'fr-FR' | 'it-IT' | 'ja-JP' | 'nl-BE' | 'nl-NL' | 'no-NO' | 'pl-PL' | 'pt-BR' | 'pt-PT' | 'sv-SE' | 'zh-CN', timezone: string, address: SiteAddress, siteCode?: (string | null) | undefined, extensionLength?: (number | null) | undefined, emergencyNotifications?: (Array | null) | undefined, callerIdSettings?: (CallerIdSettings | null) | undefined, defaultDialPlanCallingCountry?: (string | null) | undefined, defaultDialPlanRuleset?: ('INTERNATIONAL' | 'DOMESTIC' | 'EMERGENCYONLY' | null) | undefined, defaultVoicemailZeroOutDestinationExtensionId?: (string | null) | undefined, defaultVoicemailZeroOutDestinationExtensionNumber?: (string | null) | undefined}; export type CreateApiSitesArgs = {'X-Request-Id'?: string | undefined, body: Site}; export type CreateApiSitesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const error = z.object({field: z.string().nullable().optional(), code: z.enum(["UNKNOWN", "VALIDATION_ERROR", "FORBIDDEN", "CONFLICTING_QUERY_PARAMETER", "SERVICE_UNAVAILABLE", "NOT_FOUND", "BAD_REQUEST", "REQUEST_TIMEOUT", "TOO_MANY_REQUESTS", "CONFLICT"]).nullable().optional(), message: z.string().nullable().optional()}); export const errorResponse = z.object({status: z.number().int().nullable().optional(), instance: z.string().nullable().optional(), time: z.string().nullable().optional(), title: z.string().nullable().optional(), detail: z.string().nullable().optional(), errors: z.array(error).nullable().optional()}); export const link = z.object({href: z.string().nullable().optional()}); export const operationLinks = z.object({self: link.optional(), resource: link.optional()}); export const operation = z.object({operationId: z.string().nullable().optional(), status: z.enum(["PENDING", "IN_PROGRESS", "COMPLETED", "FAILED", "UNKNOWN"]).nullable().optional(), customerId: z.string().nullable().optional(), resourceType: z.enum(["USER", "RING_GROUP", "SITE"]).nullable().optional(), resourceId: z.string().nullable().optional(), operationType: z.enum(["CREATE", "UPDATE", "DELETE"]).nullable().optional(), createdTime: z.string().nullable().optional(), completedTime: z.string().nullable().optional(), error: errorResponse.optional(), _links: operationLinks.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 }; /** * Create a new site * * POST /sites */ export const createApiSites = async (args: CreateApiSitesArgs, options?: CreateApiSitesOptions) => { const {baseUrl = "https://api.8x8.com/admin-provisioning", 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 (args['X-Request-Id'] != null) headers.set("X-Request-Id", String(args['X-Request-Id'])) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/sites${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(operation, await res.json()) };