import {z} from 'zod' export type DlqSortEnum = 'message_count' | 'name' | 'owner'; export type GetApiDashboardDlqArgs = {include_empty?: boolean | undefined, search?: (string | null) | undefined, page?: number | undefined, page_size?: number | undefined, sort?: DlqSortEnum | undefined, sort_dir?: ('asc' | 'desc') | undefined, include_last_message?: boolean | undefined}; export type GetApiDashboardDlqOptions = {baseUrl?: string | undefined, FronteggHTTPAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const dlqOwnerTypeEnum = z.enum(["source", "destination", "transform", "unknown"]); export const dlqTopicRes = z.object({topic_id: z.string(), owner_type: dlqOwnerTypeEnum, owner_id: z.string().nullable().optional(), owner_name: z.string().nullable().optional(), owner_connector: z.string().nullable().optional(), message_count: z.number().int().optional(), partition_count: z.number().int().optional(), last_message_at: z.number().int().nullable().optional()}).and(z.record(z.string(), z.unknown())); export const dashboardDlqRes = z.object({page: z.number().int().optional(), page_size: z.number().int().optional(), total: z.number().int().nullable().optional(), has_next: z.boolean().nullable().optional(), result: z.array(dlqTopicRes).optional(), total_with_messages: z.number().int().optional()}).and(z.record(z.string(), z.unknown())); 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 }; /** * List DLQ topics and their current size * * GET /dashboard/dlq */ export const getApiDashboardDlq = async (args: GetApiDashboardDlqArgs, options?: GetApiDashboardDlqOptions) => { const {baseUrl = "https://api.streamkap.com", fetch: fetchFn = fetch, FronteggHTTPAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.include_empty != null) query.append("include_empty", String(args.include_empty)) if (args.search != null) query.append("search", String(args.search)) if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.sort_dir != null) query.append("sort_dir", String(args.sort_dir)) if (args.include_last_message != null) query.append("include_last_message", String(args.include_last_message)) if (FronteggHTTPAuthentication !== undefined) headers.set('authorization', `Bearer ${FronteggHTTPAuthentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/dashboard/dlq${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(dashboardDlqRes, await res.json()) };