import {z} from 'zod' export type GetApiV5PersonsPersonIdRelationshipsArgs = {person_id: string, my_roles?: (Array<'ADVOCATE' | 'FRIEND'>) | undefined, containers?: Array | undefined, programs?: Array | undefined, campaign_ids?: Array | undefined, data_keys?: Array | undefined, data_values?: Array | undefined, exclude_anonymous?: boolean | undefined, include_duplicate_identities?: boolean | undefined, include_self_referrals?: boolean | undefined, offset?: number | undefined, limit?: number | undefined}; export type GetApiV5PersonsPersonIdRelationshipsOptions = {baseUrl?: string | undefined, HEADER?: string | undefined, fetch?: typeof fetch | undefined}; export const zonedDateTime = z.string(); export const personRelationshipDataResponse = z.object({name: z.string().optional(), value: z.object({}).optional()}); export const personRelationshipResponse = z.object({campaign_id: z.string(), cause_event_id: z.string(), container: z.string(), created_date: zonedDateTime, data: z.record(z.string(), personRelationshipDataResponse), id: z.string(), my_role: z.enum(["ADVOCATE", "FRIEND"]), other_person_id: z.string(), program: z.string(), root_event_id: z.string(), updated_date: zonedDateTime}); export const getApiV5PersonsPersonIdRelationshipsResponse = z.array(personRelationshipResponse); 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 person relationships * * GET /v5/persons/{person_id}/relationships * * Also accepts auth: QUERY, COOKIE */ export const getApiV5PersonsPersonIdRelationships = async (args: GetApiV5PersonsPersonIdRelationshipsArgs, options?: GetApiV5PersonsPersonIdRelationshipsOptions) => { const {baseUrl = "https://api.extole.io", fetch: fetchFn = fetch, HEADER} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.my_roles ?? []) query.append("my_roles", String(item)) for (const item of args.containers ?? []) query.append("containers", String(item)) for (const item of args.programs ?? []) query.append("programs", String(item)) for (const item of args.campaign_ids ?? []) query.append("campaign_ids", String(item)) for (const item of args.data_keys ?? []) query.append("data_keys", String(item)) for (const item of args.data_values ?? []) query.append("data_values", String(item)) if (args.exclude_anonymous != null) query.append("exclude_anonymous", String(args.exclude_anonymous)) if (args.include_duplicate_identities != null) query.append("include_duplicate_identities", String(args.include_duplicate_identities)) if (args.include_self_referrals != null) query.append("include_self_referrals", String(args.include_self_referrals)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v5/persons/${encodeURIComponent(String(args.person_id))}/relationships${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(getApiV5PersonsPersonIdRelationshipsResponse, await res.json()) };