import {z} from 'zod' export type EndUserUpdate = {display_name?: (string | null) | undefined, primary_email?: (string | null) | undefined, primary_phone?: (string | null) | undefined, metadata?: ({[key: string]: unknown} | null) | undefined, org_external_end_user_id?: (string | null) | undefined}; export type UpdateApiV1IdentityEndUsersEndUserIdArgs = {end_user_id: string, allow_merge?: boolean | undefined, body: EndUserUpdate}; export type UpdateApiV1IdentityEndUsersEndUserIdOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const endUserResponse = z.object({uid: z.string(), org_id: z.string(), status: z.string(), display_name: z.string().nullable().optional(), primary_email: z.string().nullable().optional(), primary_phone: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).optional(), org_external_end_user_id: z.string().nullable().optional(), merged_into: z.string().nullable().optional(), dropped_external_ids: z.array(z.string()).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 }; /** * Update an end user (merge-on-conflict requires ?allow_merge=true) * * PUT /v1/identity/end-users/{end_user_id} */ export const updateApiV1IdentityEndUsersEndUserId = async (args: UpdateApiV1IdentityEndUsersEndUserIdArgs, options: UpdateApiV1IdentityEndUsersEndUserIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.allow_merge != null) query.append("allow_merge", String(args.allow_merge)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/identity/end-users/${encodeURIComponent(String(args.end_user_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(endUserResponse, await res.json()) };