import {z} from 'zod' export type RestPoolRuleCondition = {type: 'invocation_type' | 'project_name' | 'workflow_name' | 'workflow_type', values: Array<'on_demand' | 'scheduled'>} | {operator: 'starts_with', type: 'invocation_type' | 'project_name' | 'workflow_name' | 'workflow_type', values: Array} | {operator: 'starts_with', type: 'invocation_type' | 'project_name' | 'workflow_name' | 'workflow_type', values: Array} | {type: 'invocation_type' | 'project_name' | 'workflow_name' | 'workflow_type', values: Array<'ab_test' | 'audience' | 'customer_journey' | 'engage' | 'predictive_scoring' | 'realtime' | 'segment_insights' | 'syndication'>}; export type RestPoolRuleCreateRequest = {conditions: Array, poolId: string, priority: number}; export type CreateApiApiPoolRulesArgs = {body: RestPoolRuleCreateRequest}; export type CreateApiApiPoolRulesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const restPoolRuleCondition = z.discriminatedUnion("type", [z.object({type: z.enum(["invocation_type", "project_name", "workflow_name", "workflow_type"]), values: z.array(z.enum(["on_demand", "scheduled"]))}), z.object({operator: z.literal("starts_with"), type: z.enum(["invocation_type", "project_name", "workflow_name", "workflow_type"]), values: z.array(z.string())}), z.object({operator: z.literal("starts_with"), type: z.enum(["invocation_type", "project_name", "workflow_name", "workflow_type"]), values: z.array(z.string())}), z.object({type: z.enum(["invocation_type", "project_name", "workflow_name", "workflow_type"]), values: z.array(z.enum(["ab_test", "audience", "customer_journey", "engage", "predictive_scoring", "realtime", "segment_insights", "syndication"]))})]); export const restPoolRule = z.object({conditions: z.array(restPoolRuleCondition), id: z.string(), poolId: z.string(), priority: z.number().int().gte(1).lte(10000)}); 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 }; /** * Create a pool rule * * POST /api/pool_rules */ export const createApiApiPoolRules = async (args: CreateApiApiPoolRulesArgs, options?: CreateApiApiPoolRulesOptions) => { const {baseUrl = "https://api-workflow.treasuredata.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set('authorization', `TD1 ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/pool_rules${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(restPoolRule, await res.json()) };