import {z} from 'zod' export type CreateApiPhonesGetArgs = {body: {device_id: string}}; export type CreateApiPhonesGetOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const phone = z.object({created_at: z.string(), custom_metadata: z.record(z.string(), z.union([z.string(), z.boolean()])), device_id: z.string(), device_type: z.enum(["ios_phone", "android_phone"]), display_name: z.string(), errors: z.array(z.object({error_code: z.string(), message: z.string()})), nickname: z.string().optional(), properties: z.object({assa_abloy_credential_service_metadata: z.object({endpoints: z.array(z.object({endpoint_id: z.string().optional(), is_active: z.boolean().optional()})).optional(), has_active_endpoint: z.boolean().optional()}).optional(), salto_space_credential_service_metadata: z.object({has_active_phone: z.boolean().optional()}).optional()}), warnings: z.array(z.object({message: z.string(), warning_code: z.string()})), workspace_id: z.string()}); export const createApiPhonesGetResponse = z.object({phone: phone}); 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 a Phone * * POST /phones/get * * Also accepts auth: pat_with_workspace, console_session_with_workspace */ export const createApiPhonesGet = async (args: CreateApiPhonesGetArgs, options?: CreateApiPhonesGetOptions) => { const {baseUrl = "https://connect.getseam.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/phones/get${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(createApiPhonesGetResponse, await res.json()) };