import {z} from 'zod' export type KodyRuleSeverity = 'low' | 'medium' | 'high' | 'critical'; export type KodyRulesOrigin = 'user' | 'library' | 'generated'; export type KodyRulesStatus = 'active' | 'rejected' | 'pending' | 'deleted'; export type AddLibraryKodyRulesDto = {severity: KodyRuleSeverity, origin?: KodyRulesOrigin | undefined, status?: KodyRulesStatus | undefined}; export type CreateApiKodyRulesAddLibraryKodyRulesArgs = {body: AddLibraryKodyRulesDto}; export type CreateApiKodyRulesAddLibraryKodyRulesOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const kodyRulesExampleDto = z.object({snippet: z.string(), isCorrect: z.boolean()}); export const kodyRuleInheritanceDto = z.object({inheritable: z.boolean(), exclude: z.array(z.string()), include: z.array(z.string())}); export const kodyRuleExternalReferenceDto = z.object({filePath: z.string(), description: z.string().nullable(), repositoryName: z.string().nullable()}); export const kodyRuleSyncErrorDto = z.object({type: z.string(), message: z.string().nullable(), details: z.record(z.string(), z.unknown())}); export const kodyRuleDto = z.object({uuid: z.string(), title: z.string(), rule: z.string(), path: z.object({}).nullable(), sourcePath: z.object({}).nullable(), sourceAnchor: z.object({}).nullable(), severity: z.string(), status: z.string(), repositoryId: z.object({}).nullable(), directoryId: z.object({}).nullable(), examples: z.array(kodyRulesExampleDto).nullable(), origin: z.string(), scope: z.string(), inheritance: kodyRuleInheritanceDto, createdAt: z.string(), updatedAt: z.string(), referenceProcessingStatus: z.object({}).optional(), externalReferences: z.array(kodyRuleExternalReferenceDto), syncErrors: z.array(kodyRuleSyncErrorDto)}); export const kodyRulesArrayResponseDto = z.object({statusCode: z.number(), type: z.string(), data: z.array(kodyRuleDto)}); 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 }; /** * Add library rules * * POST /kody-rules/add-library-kody-rules */ export const createApiKodyRulesAddLibraryKodyRules = async (args: CreateApiKodyRulesAddLibraryKodyRulesArgs, options?: CreateApiKodyRulesAddLibraryKodyRulesOptions) => { const {baseUrl = "http://localhost:3001", fetch: fetchFn = fetch, jwt} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (jwt !== undefined) headers.set('authorization', `Bearer ${jwt}`) const url = `${baseUrl.replace(/\/+$/, '')}/kody-rules/add-library-kody-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(kodyRulesArrayResponseDto, await res.json()) };