import {z} from 'zod' export type CostCriteria = {thresholdAmount: number, thresholdPercentage?: number | undefined} | {thresholdAmount?: number | undefined, thresholdPercentage: number}; export type Policies = {include?: Array | undefined, exclude?: Array | undefined}; export type PolicyCriteria = {severity?: string | undefined, policies?: Policies | undefined}; export type ResourceActionEnum = 'no-op' | 'create' | 'read' | 'update' | 'delete'; export type ResourceCriteria = {actions: Array, regions: {include: Array, exclude?: Array | undefined}, assetTypes: {include: Array, exclude?: Array | undefined}, specificResources?: Array | undefined}; export type AnyTagsCriteria = {tagEnforcementMode: 'anyTags'}; export type RequiredTagsCriteria = {tagEnforcementMode: 'requiredTags', requiredTags: Array}; export type RequiredValuesCriteria = {tagEnforcementMode: 'requiredValues', requiredValues: {[key: string]: Array}}; export type TagCriteria = AnyTagsCriteria | RequiredTagsCriteria | RequiredValuesCriteria; export type Criteria = {cost?: CostCriteria | undefined, policy?: PolicyCriteria | undefined, resource?: ResourceCriteria | undefined, tag?: TagCriteria | undefined}; export type UpdateGuardrailRequest = {id?: string | undefined, accountId?: string | undefined, createdBy?: string | undefined, name: string, type: 'policy' | 'cost' | 'resource' | 'tag', scope: {workspaces: {include: Array, exclude?: Array | undefined}, repositories: {include: Array, exclude?: Array | undefined}, branches: {include: Array, exclude?: Array | undefined}, labels: {include: Array, exclude?: Array | undefined}}, criteria: Criteria, isEnabled: boolean, createdAt?: string | undefined, updatedAt?: string | undefined, notificationId?: string | undefined, severity?: (0 | 1 | 2 | 3 | 4) | undefined, enforceOnNoOp?: boolean | undefined}; export type PatchApiGuardrailsRuleIdArgs = {body: UpdateGuardrailRequest}; export type PatchApiGuardrailsRuleIdOptions = {baseUrl?: string | undefined, JWTAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const updateGuardrailResponse = z.object({id: z.string(), name: z.string().min(1).max(100), enabled: z.boolean(), 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 a Guardrail Rule * * PATCH /guardrails/{ruleId} */ export const patchApiGuardrailsRuleId = async (args: PatchApiGuardrailsRuleIdArgs, options?: PatchApiGuardrailsRuleIdOptions) => { const {baseUrl = "https://api.firefly.ai/v2", fetch: fetchFn = fetch, JWTAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (JWTAuth !== undefined) headers.set('authorization', `Bearer ${JWTAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/guardrails/${encodeURIComponent(String(args.ruleId))}${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(updateGuardrailResponse, await res.json()) };