import {z} from 'zod' export type GetApiDirectivesTreeArgs = {includeSystem?: boolean | undefined, body: {includeSystem?: boolean | undefined}}; export type GetApiDirectivesTreeOptions = {baseUrl?: string | undefined, APITokens?: string | undefined, fetch?: typeof fetch | undefined}; export const directive = z.object({id: z.string().optional(), displayName: z.string().optional(), shortDescription: z.string().optional(), longDescription: z.string().optional(), techniqueName: z.string().optional(), techniqueVersion: z.string().optional(), priority: z.number().int().gte(0).lte(10).optional(), enabled: z.boolean().optional(), system: z.boolean().optional(), policyMode: z.enum(["enforce", "audit"]).optional(), tags: z.array(z.record(z.string(), z.unknown())).optional(), parameters: z.object({}).optional()}); export const directiveTree = z.object({name: z.string().optional(), description: z.string().optional(), subCategories: z.array(z.object({name: z.string().optional(), description: z.string().optional(), subCategories: z.array(z.unknown()).optional(), techniques: z.array(z.unknown()).optional()})).optional(), techniques: z.array(z.object({name: z.string().optional(), directives: z.array(directive).optional()})).optional()}); export const getApiDirectivesTreeResponse = z.object({result: z.enum(["success", "error"]), action: z.literal("directivesTree"), data: z.object({directives: z.array(directiveTree)})}); 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 directive tree * * GET /directives/tree */ export const getApiDirectivesTree = async (args: GetApiDirectivesTreeArgs, options?: GetApiDirectivesTreeOptions) => { 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 (args.includeSystem != null) query.append("includeSystem", String(args.includeSystem)) if (APITokens !== undefined) headers.set("X-API-Token", APITokens) const url = `${baseUrl.replace(/\/+$/, '')}/directives/tree${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", 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(getApiDirectivesTreeResponse, await res.json()) };