import {z} from 'zod' export type Item = {key?: string | undefined, value?: string | undefined}; export type Breadcrumb = {dateTime?: string | undefined, severity?: string | undefined, action?: string | undefined, message?: string | undefined}; export type CreateMessage = {application?: string | undefined, detail?: string | undefined, hostname?: string | undefined, title?: string | undefined, titleTemplate?: string | undefined, source?: string | undefined, statusCode?: number | undefined, dateTime?: string | undefined, type?: string | undefined, user?: string | undefined, severity?: string | undefined, url?: string | undefined, method?: string | undefined, version?: string | undefined, correlationId?: string | undefined, code?: string | undefined, category?: string | undefined, cookies?: Array | undefined, form?: Array | undefined, queryString?: Array | undefined, serverVariables?: Array | undefined, data?: Array | undefined, breadcrumbs?: Array | undefined}; export type CreateApiV3MessagesLogIdBulkArgs = {logId: string, body: Array}; export type CreateApiV3MessagesLogIdBulkOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createBulkMessageResult = z.object({statusCode: z.number().int().optional(), location: z.string().optional()}); export const createApiV3MessagesLogIdBulkResponse = z.array(createBulkMessageResult); 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 one or more new messages. * * POST /v3/messages/{logId}/_bulk */ export const createApiV3MessagesLogIdBulk = async (args: CreateApiV3MessagesLogIdBulkArgs, options: CreateApiV3MessagesLogIdBulkOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) query.set("api_key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v3/messages/${encodeURIComponent(String(args.logId))}/_bulk${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(createApiV3MessagesLogIdBulkResponse, await res.json()) };