import {z} from 'zod' export type SortingOptions = {field?: string | undefined, order?: string | undefined} | Array<{field?: string | undefined, sort?: string | undefined}>; export type QueryModulesRequest = {page?: number | undefined, pageSize?: number | undefined, pageNumber?: number | undefined, tablePageSize?: number | undefined, screenFilters?: ({repoFullName?: Array | undefined, moduleOriginType?: Array | undefined, misconfigurations?: Array | undefined} | {[key: string]: unknown}) | undefined, freeTextSearch?: string | undefined, size?: number | undefined, sort?: Array | undefined, sortArr?: (Array<{field?: string | undefined, sort?: string | undefined}>) | undefined} | {[key: string]: unknown}; export type CreateApiApiIacStacksModulesArgs = {body: QueryModulesRequest}; export type CreateApiApiIacStacksModulesOptions = {baseUrl?: string | undefined, JWTAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const module = z.object({id: z.string().optional(), moduleFolderPath: z.string().optional(), repoFullName: z.string().optional(), moduleVcsPath: z.string().optional(), vcsType: z.enum(["github", "gitlab", "bitbucket", "azure-devops"]).optional(), updateAt: z.string().optional(), lastContributor: z.string().optional(), firstContributor: z.string().optional(), usageCount: z.number().int().optional(), version: z.string().optional(), moduleOrigin: z.string().optional(), moduleOriginType: z.enum(["local", "registry", "git"]).optional()}); export const queryModulesResponse = z.object({modules: z.array(module).optional(), total: z.number().int().optional()}); 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 }; /** * Query modules * * POST /api/iacStacks/modules */ export const createApiApiIacStacksModules = async (args: CreateApiApiIacStacksModulesArgs, options?: CreateApiApiIacStacksModulesOptions) => { 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(/\/+$/, '')}/api/iacStacks/modules${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(queryModulesResponse, await res.json()) };