import {z} from 'zod' export type GetApiKodyRulesInheritedRulesArgs = {teamId: string, repositoryId: string, directoryId: string}; export type GetApiKodyRulesInheritedRulesOptions = {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 kodyRulesInheritedDataDto = z.object({globalRules: z.array(kodyRuleDto), repoRules: z.array(kodyRuleDto), directoryRules: z.array(kodyRuleDto)}); export const kodyRulesInheritedResponseDto = z.object({statusCode: z.number(), type: z.string(), data: kodyRulesInheritedDataDto}); 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 inherited rules * * GET /kody-rules/inherited-rules */ export const getApiKodyRulesInheritedRules = async (args: GetApiKodyRulesInheritedRulesArgs, options?: GetApiKodyRulesInheritedRulesOptions) => { const {baseUrl = "http://localhost:3001", fetch: fetchFn = fetch, jwt} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.teamId != null) query.append("teamId", String(args.teamId)) if (args.repositoryId != null) query.append("repositoryId", String(args.repositoryId)) if (args.directoryId != null) query.append("directoryId", String(args.directoryId)) if (jwt !== undefined) headers.set('authorization', `Bearer ${jwt}`) const url = `${baseUrl.replace(/\/+$/, '')}/kody-rules/inherited-rules${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(kodyRulesInheritedResponseDto, await res.json()) };