import {z} from 'zod' export type RumArchiveSessionsSearchAttributes = {cursor?: string | undefined, from: number, hide_retained?: boolean | undefined, limit?: number | undefined, query: string, to: number}; export type RumArchiveSessionsSearchData = {attributes: RumArchiveSessionsSearchAttributes, id?: string | undefined, type?: string | undefined}; export type RumArchiveSessionsSearchRequest = {data: RumArchiveSessionsSearchData}; export type CreateApiApiV2RumRecoverSessionsArgs = {body: RumArchiveSessionsSearchRequest}; export type CreateApiApiV2RumRecoverSessionsOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const rumRecoverySessionStatus = z.enum(["AVAILABLE", "PENDING", "PROCESSING", "RECOVERED", "FAILED", "RETAINED", "EXPIRED", "CANCELLED"]); export const rumArchiveSessionRecoveryStatus = z.object({recovered_at: z.string().optional(), request_id: z.string().optional(), status: rumRecoverySessionStatus}).nullable(); export const rumArchiveSessionAttributes = z.object({account_id: z.string().nullable(), anonymous_id: z.string().nullable(), application_id: z.string(), country: z.string().nullable(), recovery_status: rumArchiveSessionRecoveryStatus, service: z.string(), session_id: z.string(), timestamp: z.number().int(), user_id: z.string().nullable(), version: z.string().nullable()}); export const rumArchiveSessionType = z.literal("rum_archive_session"); export const rumArchiveSession = z.object({attributes: rumArchiveSessionAttributes, id: z.string(), type: rumArchiveSessionType}); export const rumArchiveSessionsPage = z.object({next_cursor: z.string().optional(), total_count: z.number().int().gte(0)}); export const rumArchiveSessionsMeta = z.object({page: rumArchiveSessionsPage}); export const rumArchiveSessionsResponse = z.object({data: z.array(rumArchiveSession), meta: rumArchiveSessionsMeta}); 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 archived sessions * * POST /api/v2/rum/recover/sessions */ export const createApiApiV2RumRecoverSessions = async (args: CreateApiApiV2RumRecoverSessionsArgs, options?: CreateApiApiV2RumRecoverSessionsOptions) => { const {baseUrl = "https://api.datadoghq.com", fetch: fetchFn = fetch, apiKeyAuth, appKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("DD-API-KEY", apiKeyAuth) if (appKeyAuth !== undefined) headers.set("DD-APPLICATION-KEY", appKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/rum/recover/sessions${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(rumArchiveSessionsResponse, await res.json()) };