import {z} from 'zod' export type TechniqueParameter = {id?: string | undefined, name?: string | undefined, description?: string | undefined, mayBeEmpty?: boolean | undefined}; export type TechniqueResource = {name?: string | undefined, state?: string | undefined}; export type TechniqueMethodCall = {type?: string | undefined, id?: string | undefined, component?: string | undefined, method?: string | undefined, condition?: string | undefined, disabledReporting?: boolean | undefined, parameters?: (Array<{name?: string | undefined}>) | undefined}; export type TechniqueBlock = {type?: string | undefined, id?: string | undefined, component?: string | undefined, condition?: string | undefined, calls?: (Array) | undefined, reportingLogic?: ({type?: ('worst' | 'focus' | 'worst-case-weighted-one' | 'worst-case-weighted-sum') | undefined, value?: string | undefined}) | undefined}; export type EditorTechnique = {id?: string | undefined, name?: string | undefined, version?: string | undefined, category?: string | undefined, description?: string | undefined, documentation?: string | undefined, tags?: Record | undefined, source?: string | undefined, parameters?: Array | undefined, resources?: Array | undefined, calls?: (Array) | undefined}; export type Techniques = Array; export type CreateApiTechniquesTechniqueIdTechniqueVersionArgs = {techniqueId: string, techniqueVersion: string, body: Techniques}; export type CreateApiTechniquesTechniqueIdTechniqueVersionOptions = {baseUrl?: string | undefined, APITokens?: string | undefined, fetch?: typeof fetch | undefined}; export const techniqueParameter = z.object({id: z.string().optional(), name: z.string().optional(), description: z.string().optional(), mayBeEmpty: z.boolean().optional()}); export const techniqueResource = z.object({name: z.string().optional(), state: z.string().optional()}); export const techniqueMethodCall = z.object({type: z.string().optional(), id: z.string().optional(), component: z.string().optional(), method: z.string().optional(), condition: z.string().optional(), disabledReporting: z.boolean().optional(), parameters: z.array(z.object({name: z.string().optional()})).optional()}); export const techniqueBlock: z.ZodType = z.object({type: z.string().optional(), id: z.string().optional(), component: z.string().optional(), condition: z.string().optional(), calls: z.array(z.union([techniqueMethodCall, z.lazy(() => techniqueBlock)])).optional(), reportingLogic: z.object({type: z.enum(["worst", "focus", "worst-case-weighted-one", "worst-case-weighted-sum"]).optional(), value: z.string().optional()}).optional()}); export const editorTechnique = z.object({id: z.string().optional(), name: z.string().optional(), version: z.string().optional(), category: z.string().optional(), description: z.string().optional(), documentation: z.string().optional(), tags: z.object({}).optional(), source: z.string().optional(), parameters: z.array(techniqueParameter).optional(), resources: z.array(techniqueResource).optional(), calls: z.array(z.union([techniqueMethodCall, techniqueBlock])).optional()}); export const createApiTechniquesTechniqueIdTechniqueVersionResponse = z.object({result: z.enum(["success", "error"]), action: z.literal("listTechniques"), data: z.object({techniques: z.object({technique: editorTechnique})})}); 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 technique * * POST /techniques/{techniqueId}/{techniqueVersion} */ export const createApiTechniquesTechniqueIdTechniqueVersion = async (args: CreateApiTechniquesTechniqueIdTechniqueVersionArgs, options?: CreateApiTechniquesTechniqueIdTechniqueVersionOptions) => { const {baseUrl = "https://rudder.example.com/rudder/api/latest", fetch: fetchFn = fetch, APITokens} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APITokens !== undefined) headers.set("X-API-Token", APITokens) const url = `${baseUrl.replace(/\/+$/, '')}/techniques/${encodeURIComponent(String(args.techniqueId))}/${encodeURIComponent(String(args.techniqueVersion))}${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(createApiTechniquesTechniqueIdTechniqueVersionResponse, await res.json()) };