import {z} from 'zod' export type AudiencesPatchRequest = {name?: string | undefined, filters?: ({filters: Array<{field: string, operator: '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: unknown, type?: string | undefined}>, logicalOperator: 'and' | 'or'}) | undefined}; export type PatchApiV1AudiencesAudienceIdArgs = {audienceId: string, body: AudiencesPatchRequest}; export type PatchApiV1AudiencesAudienceIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const audience = 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()}); 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 an audience * * PATCH /v1/audiences/{audienceId} * * Also accepts auth: apiKeyAuth */ export const patchApiV1AudiencesAudienceId = async (args: PatchApiV1AudiencesAudienceIdArgs, options?: PatchApiV1AudiencesAudienceIdOptions) => { const {baseUrl = "https://brew.new/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/audiences/${encodeURIComponent(String(args.audienceId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(audience, await res.json()) };