import {z} from 'zod' export type PersonShareableContentRequest = {description?: (string | null) | undefined, image_url?: (string | null) | undefined, partner_content_id?: (string | null) | undefined, title?: (string | null) | undefined, url?: (string | null) | undefined}; export type PersonShareableUpdateRequest = {content?: PersonShareableContentRequest | undefined, data?: {[key: string]: string} | undefined, key?: (string | null) | undefined, label?: (string | null) | undefined, program_url?: string | undefined}; export type UpdateApiV5PersonsPersonIdShareablesCodeArgs = {person_id: string, code: string, body: PersonShareableUpdateRequest}; export type UpdateApiV5PersonsPersonIdShareablesCodeOptions = {baseUrl?: string | undefined, HEADER?: string | undefined, fetch?: typeof fetch | undefined}; export const personShareableContentResponse = z.object({description: z.string().optional(), image_url: z.string().optional(), partner_content_id: z.string().optional(), title: z.string().optional(), url: z.string().optional()}); export const zonedDateTime = z.string(); export const personShareableDataResponse = z.object({name: z.string().optional(), value: z.object({}).optional()}); export const personShareableResponse = z.object({code: z.string(), content: personShareableContentResponse, created_date: zonedDateTime, data: z.record(z.string(), personShareableDataResponse), key: z.string(), label: z.string(), link: z.string(), updated_date: zonedDateTime}); 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 a person shareable * * PUT /v5/persons/{person_id}/shareables/{code} * * Also accepts auth: QUERY, COOKIE */ export const updateApiV5PersonsPersonIdShareablesCode = async (args: UpdateApiV5PersonsPersonIdShareablesCodeArgs, options?: UpdateApiV5PersonsPersonIdShareablesCodeOptions) => { const {baseUrl = "https://api.extole.io", fetch: fetchFn = fetch, HEADER} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v5/persons/${encodeURIComponent(String(args.person_id))}/shareables/${encodeURIComponent(String(args.code))}${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(personShareableResponse, await res.json()) };