import {z} from 'zod' export type CreateApiRelationshipsArgs = {body: {frn: string, integrationId: string, level?: number | undefined}}; export type CreateApiRelationshipsOptions = {baseUrl?: string | undefined, JWTAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const resourceConnection = z.object({id: z.string(), frn: z.string(), assetId: z.string().optional(), resourceId: z.string().optional(), integrationId: z.string(), accountId: z.string(), assetType: z.string(), name: z.string().optional(), region: z.string().optional(), provider: z.string().optional(), providerId: z.string().optional(), providerType: z.enum(["cloud", "saas", "kubernetes"]).optional(), providerSubType: z.string().optional(), service: z.string().optional(), connectionType: z.string().optional(), hierarchy: z.array(z.string()), type: z.enum(["ASSET", "TRACE"]), hasOwnerTargets: z.boolean().optional(), hasOwnerSources: z.boolean().optional(), ownerTargetsList: z.array(z.string()).optional(), ownerSourcesList: z.array(z.string()).optional(), hasControllerTargets: z.boolean().optional(), hasControllerSources: z.boolean().optional(), state: z.enum(["unmanaged", "managed", "modified", "ghost", "iacIgnored", "child"]).optional(), prevState: z.string().optional(), iacType: z.string().optional(), isExcluded: z.boolean().optional(), exclusions: z.array(z.string()).optional(), firstSeen: z.string().optional(), updateAt: z.string().optional(), inventoryUpdateTime: z.string().optional(), lastResourceStateChange: z.string().optional(), tags: z.string().optional(), tagsList: z.array(z.string()).optional(), taggingStatus: z.enum(["NOT_SUPPORTED", "NOT_TAGGED", "TAGGED"]).optional(), consoleURL: z.string().optional(), deleteCommand: z.string().optional(), stackId: z.string().optional(), stackName: z.string().optional(), stackIds: z.array(z.string()).optional(), stacks: z.array(z.object({stackId: z.string().optional(), stackType: z.string().optional(), stackName: z.string().optional()})).optional(), metaHash: z.string().optional(), prevSignature: z.string().optional(), schemaVersion: z.number().int().optional(), revisionsNumber: z.number().int().optional(), hasRevisions: z.boolean().optional(), isChild: z.boolean().optional(), isLocked: z.boolean().optional(), isEventDriven: z.boolean().optional(), isOutOfSync: z.boolean().optional(), runId: z.string().optional(), accountName: z.string().optional(), arn: z.string().optional(), nonBijectionFRN: z.string().optional()}); export const createApiRelationshipsResponse = z.array(resourceConnection); 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 resource relationships * * POST /relationships */ export const createApiRelationships = async (args: CreateApiRelationshipsArgs, options?: CreateApiRelationshipsOptions) => { 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(/\/+$/, '')}/relationships${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(createApiRelationshipsResponse, await res.json()) };