import {z} from 'zod' export type InfracostIntegration = {attributes: {'api-key': string | null, 'err-message'?: (string | null) | undefined, 'is-shared'?: boolean | undefined, name: string, status?: ('active' | 'disabled' | 'failed') | undefined}, id?: string | undefined, links?: ({self?: string | undefined}) | undefined, relationships?: ({environments?: ({data: Array<{id: string, type: 'environments' | ''}> | null}) | undefined}) | undefined, type: 'infracost-integration'}; export type InfracostIntegrationDocument = {data?: InfracostIntegration | undefined, included?: Array> | undefined, meta?: Record | undefined}; export type CreateApiIntegrationsInfracostArgs = {include?: Array<'environments'> | undefined, body: InfracostIntegrationDocument}; export type CreateApiIntegrationsInfracostOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const infracostIntegration = z.object({attributes: z.object({"api-key": z.string().min(1).max(255).nullable(), "err-message": z.string().nullable().optional(), "is-shared": z.boolean().optional(), name: z.string().max(128), status: z.enum(["active", "disabled", "failed"]).optional()}), id: z.string().optional(), links: z.object({self: z.string().optional()}).optional(), relationships: z.object({environments: z.object({data: z.array(z.object({id: z.string(), type: z.enum(["environments", ""])})).nullable()}).optional()}).optional(), type: z.literal("infracost-integration")}); export const infracostIntegrationDocument = z.object({data: infracostIntegration.optional(), included: z.array(z.object({})).optional(), meta: z.object({}).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 }; /** * Create Infracost integration * * POST /integrations/infracost */ export const createApiIntegrationsInfracost = async (args: CreateApiIntegrationsInfracostArgs, options?: CreateApiIntegrationsInfracostOptions) => { const {baseUrl = "https://example.scalr.io/api/iacp/v3", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.api+json") headers.set('accept', "application/vnd.api+json") if (args.include != null) query.append("include", args.include.join(',')) if (APIKey !== undefined) headers.set('authorization', `Bearer ${APIKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/integrations/infracost${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(infracostIntegrationDocument, await res.json()) };