import {z} from 'zod' export type ProgrammingLanguage = 'JSTS' | 'PYTHON' | 'JAVA' | 'CSHARP' | 'DART' | 'RUBY'; export type GetApiKodyRulesFindLibraryKodyRulesArgs = {page?: number | undefined, limit?: number | undefined, skip?: number | undefined, title?: string | undefined, severity?: string | undefined, tags?: Array | undefined, plug_and_play?: boolean | undefined, language?: ProgrammingLanguage | undefined, buckets?: Array | undefined}; export type GetApiKodyRulesFindLibraryKodyRulesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const kodyRulesExampleDto = z.object({snippet: z.string(), isCorrect: z.boolean()}); export const kodyRulesLibraryRuleDto = z.object({title: z.string(), rule: z.string(), why_is_this_important: z.string(), severity: z.string(), bad_example: z.string(), good_example: z.string(), examples: z.array(kodyRulesExampleDto), language: z.string(), uuid: z.string(), buckets: z.array(z.string()), scope: z.string(), plug_and_play: z.boolean(), positiveCount: z.number(), negativeCount: z.number(), userFeedback: z.record(z.string(), z.unknown()).nullable()}); export const kodyRulesPaginationDto = z.object({currentPage: z.number(), totalPages: z.number(), totalItems: z.number(), itemsPerPage: z.number(), hasNextPage: z.boolean(), hasPreviousPage: z.boolean()}); export const kodyRulesLibraryDataDto = z.object({data: z.array(kodyRulesLibraryRuleDto), pagination: kodyRulesPaginationDto}); export const kodyRulesLibraryResponseDto = z.object({statusCode: z.number(), type: z.string(), data: kodyRulesLibraryDataDto}); 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 }; /** * List library rules * * GET /kody-rules/find-library-kody-rules */ export const getApiKodyRulesFindLibraryKodyRules = async (args: GetApiKodyRulesFindLibraryKodyRulesArgs, options?: GetApiKodyRulesFindLibraryKodyRulesOptions) => { const {baseUrl = "http://localhost:3001", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.skip != null) query.append("skip", String(args.skip)) if (args.title != null) query.append("title", String(args.title)) if (args.severity != null) query.append("severity", String(args.severity)) for (const item of args.tags ?? []) query.append("tags", String(item)) if (args.plug_and_play != null) query.append("plug_and_play", String(args.plug_and_play)) if (args.language != null) query.append("language", String(args.language)) for (const item of args.buckets ?? []) query.append("buckets", String(item)) const url = `${baseUrl.replace(/\/+$/, '')}/kody-rules/find-library-kody-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(kodyRulesLibraryResponseDto, await res.json()) };