import {z} from 'zod' export type PersonType = 'internal' | 'external'; export type GetApiV1PeopleListPageArgs = {workspace_id: string, group_id?: (string | null) | undefined, kind?: (PersonType | null) | undefined, search?: (string | null) | undefined, 'X-API-Key'?: (string | null) | undefined}; export type GetApiV1PeopleListPageOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const personListItemOut = z.object({id: z.string(), workspace_id: z.string(), user_id: z.string().nullable().optional(), name: z.string(), email: z.string().nullable().optional(), phone: z.string().nullable().optional(), job_title: z.string().nullable().optional(), department: z.string().nullable().optional(), seniority_level: z.number().int().nullable().optional(), seniority_label: z.string().nullable().optional(), person_type: z.string(), source: z.string(), audience_filter_attributes: z.record(z.string(), z.array(z.string())).optional(), image_url: z.string().nullable().optional(), group_ids: z.array(z.string()).optional(), group_names: z.array(z.string()).optional(), campaign_count: z.number().int().optional(), completed_count: z.number().int().optional(), last_participated_at: z.string().nullable().optional(), channels: z.array(z.string()).optional(), created_at: z.string(), updated_at: z.string()}); export const peopleGroupOut = z.object({id: z.string(), workspace_id: z.string(), name: z.string(), member_count: z.number().int().optional(), created_by: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string()}); export const peopleListPageOut = z.object({people: z.array(personListItemOut), groups: z.array(peopleGroupOut)}); 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 People Page * * GET /v1/people/list-page */ export const getApiV1PeopleListPage = async (args: GetApiV1PeopleListPageArgs, options: GetApiV1PeopleListPageOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.workspace_id != null) query.append("workspace_id", String(args.workspace_id)) if (args.group_id != null) query.append("group_id", String(args.group_id)) if (args.kind != null) query.append("kind", String(args.kind)) if (args.search != null) query.append("search", String(args.search)) if (args['X-API-Key'] != null) headers.set("X-API-Key", String(args['X-API-Key'])) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/people/list-page${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(peopleListPageOut, await res.json()) };