import {z} from 'zod' export type GetApiApiV4MeStepsArgs = {campaign_id?: (string | null) | undefined, program_label?: (string | null) | undefined, step_name?: (string | null) | undefined, quality?: ('HIGH' | 'LOW' | 'NONE' | null) | undefined, partner_event_id_name?: (string | null) | undefined, partner_event_id_value?: (string | null) | undefined}; export type GetApiApiV4MeStepsOptions = {baseUrl?: string | undefined, HEADER?: string | undefined, fetch?: typeof fetch | undefined}; export const stepResponse = z.object({name: z.string().optional()}); export const getApiApiV4MeStepsResponse = z.array(stepResponse); 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 steps for the authenticated consumer * * GET /api/v4/me/steps * * Also accepts auth: QUERY, COOKIE */ export const getApiApiV4MeSteps = async (args: GetApiApiV4MeStepsArgs, options?: GetApiApiV4MeStepsOptions) => { 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 (args.campaign_id != null) query.append("campaign_id", String(args.campaign_id)) if (args.program_label != null) query.append("program_label", String(args.program_label)) if (args.step_name != null) query.append("step_name", String(args.step_name)) if (args.quality != null) query.append("quality", String(args.quality)) if (args.partner_event_id_name != null) query.append("partner_event_id_name", String(args.partner_event_id_name)) if (args.partner_event_id_value != null) query.append("partner_event_id_value", String(args.partner_event_id_value)) if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/api/v4/me/steps${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(getApiApiV4MeStepsResponse, await res.json()) };