import {z} from 'zod' export type CILogContentEncoding = 'identity' | 'gzip'; export type CILogAttributeValue = string | null | number | boolean; export type CILogItem = {ddtags?: string | undefined, job_id: string, line_number?: number | undefined, message: string, pipeline_unique_id: string, provider_name?: string | undefined, section_name?: string | undefined, status?: string | undefined, timestamp?: string | undefined} | {[key: string]: CILogAttributeValue}; export type CILogRequest = Array; export type CreateApiApiV2CilogsArgs = {'Content-Encoding'?: CILogContentEncoding | undefined, body: CILogRequest}; export type CreateApiApiV2CilogsOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV2CilogsResponse = z.object({}); 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 }; /** * Send CI job logs * * POST /api/v2/cilogs */ export const createApiApiV2Cilogs = async (args: CreateApiApiV2CilogsArgs, options?: CreateApiApiV2CilogsOptions) => { const {baseUrl = "https://http-intake.logs.datadoghq.com", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Content-Encoding'] != null) headers.set("Content-Encoding", String(args['Content-Encoding'])) if (apiKeyAuth !== undefined) headers.set("DD-API-KEY", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/cilogs${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(createApiApiV2CilogsResponse, await res.json()) };