import {z} from 'zod' export type GetApiV1AudiencesArgs = {audienceId?: string | undefined, include?: 'count' | undefined, limit?: number | undefined, cursor?: string | undefined}; export type GetApiV1AudiencesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const audiencesListResponse = z.object({data: z.array(z.object({audienceId: z.string().min(1).max(64), audienceName: z.string().min(1).max(200), filters: z.object({filters: z.array(z.object({field: z.string().min(1), operator: z.enum(["equals", "not_equals", "contains", "not_contains", "contains_any", "not_contains_any", "starts_with", "ends_with", "gt", "gte", "lt", "lte", "between", "is_true", "is_false", "in", "not_in", "is_empty", "not_exists", "is_not_empty", "exists", "is_set", "before", "after", "on_date"]), value: z.unknown(), type: z.string().optional()})), logicalOperator: z.enum(["and", "or"])}), count: z.number().int().gte(0), createdAt: z.string(), updatedAt: z.string()})), pagination: z.object({limit: z.number().int().gte(1).lte(100), cursor: z.string().nullable(), hasMore: z.boolean()}).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 }; /** * Get audiences * * GET /v1/audiences * * Also accepts auth: apiKeyAuth */ export const getApiV1Audiences = async (args: GetApiV1AudiencesArgs, options?: GetApiV1AudiencesOptions) => { const {baseUrl = "https://brew.new/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.audienceId != null) query.append("audienceId", String(args.audienceId)) if (args.include != null) query.append("include", String(args.include)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/audiences${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(audiencesListResponse, await res.json()) };