import {z} from 'zod' export type GetApiV5PersonsPersonIdJourneysJourneyIdArgs = {person_id: string, journey_id: string}; export type GetApiV5PersonsPersonIdJourneysJourneyIdOptions = {baseUrl?: string | undefined, HEADER?: string | undefined, fetch?: typeof fetch | undefined}; export const zonedDateTime = z.string(); export const personJourneyDataResponse = z.object({id: z.string().optional(), name: z.string().optional(), scope: z.enum(["CLIENT", "PRIVATE", "PUBLIC"]).optional(), value: z.object({}).optional()}); export const journeyKey = z.object({name: z.string().optional(), value: z.string().optional()}); export const personJourneyResponse = z.object({campaign_id: z.string(), container: z.string(), created_date: zonedDateTime, data: z.record(z.string(), personJourneyDataResponse), id: z.string(), key: journeyKey, name: z.string(), program: 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 }; /** * Get a person journey * * GET /v5/persons/{person_id}/journeys/{journey_id} * * Also accepts auth: QUERY, COOKIE */ export const getApiV5PersonsPersonIdJourneysJourneyId = async (args: GetApiV5PersonsPersonIdJourneysJourneyIdArgs, options?: GetApiV5PersonsPersonIdJourneysJourneyIdOptions) => { const {baseUrl = "https://api.extole.io", fetch: fetchFn = fetch, HEADER} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v5/persons/${encodeURIComponent(String(args.person_id))}/journeys/${encodeURIComponent(String(args.journey_id))}${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(personJourneyResponse, await res.json()) };