import {z} from 'zod' export type GetApiV1SegmentsArgs = {pageToken?: string | undefined, pageSize?: unknown | undefined, nameContains?: string | undefined, teamId?: string | undefined, retailerTeamIds?: Array | undefined}; export type GetApiV1SegmentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const v1Segment = z.object({id: z.string().optional(), segmentId: z.string().optional(), sourceId: z.string().optional(), name: z.string().optional(), description: z.string().optional(), category: z.array(z.string()).optional(), retailerTeamId: z.string().optional()}); export const v1ListSegmentsResponse = z.object({segments: z.array(v1Segment).optional(), nextPageToken: z.string().optional(), totalCount: 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 }; /** * Lists all segments (based on params provided) * * GET /v1/segments */ export const getApiV1Segments = async (args: GetApiV1SegmentsArgs, options?: GetApiV1SegmentsOptions) => { const {baseUrl = "https://auth..citrusad.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageToken != null) query.append("pageToken", String(args.pageToken)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.nameContains != null) query.append("nameContains", String(args.nameContains)) if (args.teamId != null) query.append("teamId", String(args.teamId)) for (const item of args.retailerTeamIds ?? []) query.append("retailerTeamIds", String(item)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/segments${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(v1ListSegmentsResponse, await res.json()) };