import type { HTTPResponse as ConfidentialHTTPResponse } from '../../../../generated/capabilities/networking/confidentialhttp/v1alpha/client_pb'; import type { Request, RequestJson, Response } from '../../../../generated/capabilities/networking/http/v1alpha/client_pb'; import type { ReportResponse } from '../../../../generated/sdk/v1alpha/sdk_pb'; import type { NodeRuntime } from '../../..'; import type { Report } from '../../../report'; /** * HTTP Response Helper Functions * * These utility functions provide a convenient way to work with HTTP Response objects, * mimicking the native Response API methods like .json(), .text(), and .arrayBuffer(). * * @example * ```typescript * import { text, json, ok, getHeader } from './http-helpers' * * // Direct usage with Response object * const response = sendRequester.sendRequest({ url: 'https://api.example.com/data' }).result() * * // Check if response is successful * if (!ok(response)) { * throw new Error(`Request failed with status: ${response.statusCode}`) * } * * // Get response as text (automatically trimmed) * const responseText = text(response) * * // Parse JSON response * const data = json(response) * * // Get specific header * const contentType = getHeader(response, 'content-type') * ``` * * @example * ```typescript * // Function overload usage * const responseFn = sendRequester.sendRequest({ url: 'https://api.example.com/data' }) * * // Check if response is successful * if (!ok(() => ({ result: responseFn.result() })).result()) { * const response = responseFn.result() * throw new Error(`Request failed with status: ${response.statusCode}`) * } * * // Get response as text (automatically trimmed) * const responseText = text(() => ({ result: responseFn.result() })).result() * ``` */ /** * Returns the response body as a UTF-8 string, automatically trimmed * @param response - The Response object * @returns The body as a trimmed string */ export declare function text(response: Response | ConfidentialHTTPResponse): string; /** * Returns the response body as a UTF-8 string, automatically trimmed * @param responseFn - Function that returns an object with result function that returns Response * @returns Object with result function that returns the body as a trimmed string */ export declare function text(responseFn: () => { result: Response | ConfidentialHTTPResponse; }): { result: () => string; }; /** * Parses the response body as JSON * @param response - The Response object * @returns The parsed JSON * @throws Error if the body is not valid JSON */ export declare function json(response: Response | ConfidentialHTTPResponse): unknown; /** * Parses the response body as JSON * @param responseFn - Function that returns an object with result function that returns Response * @returns Object with result function that returns the parsed JSON * @throws Error if the body is not valid JSON */ export declare function json(responseFn: () => { result: Response | ConfidentialHTTPResponse; }): { result: () => unknown; }; /** * Gets a specific header value * @param response - The Response object * @param name - The header name (case-insensitive) * @returns The header value or undefined if not found */ export declare function getHeader(response: Response, name: string): string | undefined; /** * Gets a specific header value * @param responseFn - Function that returns an object with result function that returns Response * @param name - The header name (case-insensitive) * @returns Object with result function that returns the header value or undefined if not found */ export declare function getHeader(responseFn: () => { result: Response; }, name: string): { result: () => string | undefined; }; /** * Checks if the response status indicates success (200-299) * @param response - The Response object * @returns True if the status code is in the 200-299 range */ export declare function ok(response: Response | ConfidentialHTTPResponse): boolean; /** * Checks if the response status indicates success (200-299) * @param responseFn - Function that returns an object with result function that returns Response * @returns Object with result function that returns true if the status code is in the 200-299 range */ export declare function ok(responseFn: () => { result: Response | ConfidentialHTTPResponse; }): { result: () => boolean; }; declare module '../../../../generated-sdk/capabilities/networking/http/v1alpha/client_sdk_gen' { interface ClientCapability { /** * SendReport functions the same as SendRequest, but takes a Report and a function * to convert the inner ReportResponse to a Request. * Note that caching is limited as reports may contain different sets of signatures * on different nodes, leading to a cache miss. * * @param runtime - The runtime instance * @param report - The Report to process * @param fn - Function to convert ReportResponse to Request * @returns Response result function */ sendReport(runtime: NodeRuntime, report: Report, fn: (reportResponse: ReportResponse) => Request | RequestJson): { result: () => Response; }; } interface SendRequester { /** * SendReport functions the same as SendRequest, but takes a Report and a function * to convert the inner ReportResponse to a Request. * Note that caching is limited as reports may contain different sets of signatures * on different nodes, leading to a cache miss. * * @param report - The Report to process * @param fn - Function to convert ReportResponse to Request * @returns Response result function */ sendReport(report: Report, fn: (reportResponse: ReportResponse) => Request | RequestJson): { result: () => Response; }; } }