import {z} from 'zod' export type GetApiApiV3CrmIntegrationIdOwnersArgs = {integration_id: string, limit?: number | undefined, after?: (string | null) | undefined, email?: (string | null) | undefined, archived?: boolean | undefined}; export type GetApiApiV3CrmIntegrationIdOwnersOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const teamResponse = z.object({id: z.string(), name: z.string(), primary: z.boolean()}); export const ownerResponse = z.object({id: z.string(), email: z.string().nullable().optional(), firstName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), full_name: z.string(), userId: z.number().int().nullable().optional(), createdAt: z.string().nullable().optional(), updatedAt: z.string().nullable().optional(), archived: z.boolean().optional(), teams: z.array(teamResponse).optional()}); export const ownerListResponse = z.object({results: z.array(ownerResponse), has_more: z.boolean().optional(), nextCursor: z.string().nullable().optional(), total: z.number().int()}); 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 Owners * * GET /api/v3/crm/{integration_id}/owners */ export const getApiApiV3CrmIntegrationIdOwners = async (args: GetApiApiV3CrmIntegrationIdOwnersArgs, options: GetApiApiV3CrmIntegrationIdOwnersOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.after != null) query.append("after", String(args.after)) if (args.email != null) query.append("email", String(args.email)) if (args.archived != null) query.append("archived", String(args.archived)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/crm/${encodeURIComponent(String(args.integration_id))}/owners${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(ownerListResponse, await res.json()) };