import {z} from 'zod' export type GetApiUserContactsArgs = {skip?: number | undefined, top?: number | undefined}; export type GetApiUserContactsOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiUserContactsResponse = z.object({count: z.number().int().gte(0).optional(), items: z.array(z.object({id: z.string().optional(), name: z.string().min(1).max(64).optional(), email: z.string().max(128).optional(), company: z.string().max(128).optional(), title: z.string().max(128).optional(), business: z.string().max(128).optional(), business2: z.string().max(128).optional(), mobile_phone: z.string().max(128).optional(), mobile_phone2: z.string().max(128).optional(), home_phone: z.string().max(128).optional(), home_phone2: z.string().max(128).optional(), other: z.string().max(128).optional(), business_fax: z.string().max(128).optional(), home_fax: z.string().max(128).optional(), address: z.string().max(128).optional(), notes: z.string().max(1024).optional(), sync_type: z.literal("Microsoft 365").optional(), favorite: z.boolean().optional()})).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 }; /** * List personal contact * * GET /user/contacts */ export const getApiUserContacts = async (args: GetApiUserContactsArgs, options?: GetApiUserContactsOptions) => { const {baseUrl = "https://HOSTNAME:8887/api", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.skip != null) query.append("skip", String(args.skip)) if (args.top != null) query.append("top", String(args.top)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/user/contacts${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(getApiUserContactsResponse, await res.json()) };