import {z} from 'zod' export type ScreenFilters = {stateType?: Array | undefined, providerIntegrationId?: Array | undefined, providerType?: Array | undefined, providerAccountId?: Array | undefined, stackAssetTypes?: Array | undefined, status?: Array | undefined, iacState?: Array | undefined, vcsRepo?: Array | undefined, backendLocation?: Array | undefined, hasIntegratedProviders?: boolean | undefined, hasNotIntegratedProviders?: boolean | undefined} | {[key: string]: unknown}; export type SortingOptions = {field?: string | undefined, order?: string | undefined} | Array<{field?: string | undefined, sort?: string | undefined}>; export type SearchIacStacksRequest = {page?: number | undefined, pageSize?: number | undefined, pageNumber?: number | undefined, tablePageSize?: number | undefined, screenFilters?: ScreenFilters | undefined, freeTextSearch?: string | undefined, sorting?: SortingOptions | undefined} | {[key: string]: unknown}; export type CreateApiApiIacStacksArgs = {body: SearchIacStacksRequest}; export type CreateApiApiIacStacksOptions = {baseUrl?: string | undefined, JWTAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const stackProvider = z.object({providerType: z.string().optional(), providerAccountId: z.string().optional(), providerIntegrationId: z.string().optional(), isIntegrated: z.boolean().optional()}); export const iacStack = z.object({id: z.string().optional(), name: z.string().optional(), stateType: z.enum(["terraform", "cloudformation", "pulumi", "helm", "k8s"]).optional(), status: z.string().optional(), version: z.string().optional(), backend: z.string().optional(), lastApplied: z.string().optional(), assetCount: z.number().int().optional(), stackProviders: z.array(stackProvider).optional(), metadata: z.object({vcsRepo: z.string().optional(), vcsPath: z.string().optional(), branch: z.string().optional()}).optional()}); export const searchIacStacksResponse = z.object({hits: z.array(iacStack).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 }; /** * Search IaC stacks * * POST /api/iacStacks */ export const createApiApiIacStacks = async (args: CreateApiApiIacStacksArgs, options?: CreateApiApiIacStacksOptions) => { 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${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(searchIacStacksResponse, await res.json()) };