import type { Header, HttpMethod, LiteralUnionNumber, RetryStrategyOptions } from "../models/core.models.js"; import type { CoreSdkError } from "../models/error.models.js"; import type { JsonValue } from "../models/json.models.js"; type Success = { readonly success: true; readonly response: TData; readonly error?: never; }; type Failure = { readonly success: false; readonly response?: never; readonly error: CoreSdkError; }; export type HttpResult = Success | Failure; /** * Helper status codes for the HTTP service. * It can be any valid number status code as this type only serves as a helper. */ export type HttpServiceStatus = LiteralUnionNumber<200 | 201 | 204 | 500 | 429 | 404 | 403 | 401 | 400>; export type DefaultHttpServiceConfig = { /** * The retry strategy to be used. If not provided, the default retry strategy will be used. */ readonly retryStrategy?: RetryStrategyOptions; /** * The headers appended to all requests. */ readonly requestHeaders?: readonly Header[]; /** * The adapter to be used. If not provided, the default adapter will be used. */ readonly adapter?: HttpAdapter; }; export type HttpResponse = HttpResult<{ readonly data: TResponseData; readonly body: TBodyData; readonly method: HttpMethod; readonly requestHeaders: readonly Header[]; readonly adapterResponse: Omit; }>; export type ExecuteRequestOptions = { readonly url: string; readonly method: HttpMethod; readonly body: TBodyData; readonly requestHeaders?: readonly Header[]; }; export type UploadFileRequestOptions = Omit, "method"> & { readonly method: Extract; }; export type DownloadFileRequestOptions = Pick, "url" | "requestHeaders">; /** * Represents the HTTP service used for making requests to the Kontent.ai API. * * This service includes built-in retry logic, request validation, and automatic header processing. * * By default, it uses the `fetch` API for executing requests. However, you can supply a custom `HttpAdapter` * to integrate your preferred HTTP client. * * For full customization—including retry behavior and other request-handling logic—you may implement the `HttpService` directly. */ export type HttpService = { /** * Executes request with the given method and body. */ requestAsync(opts: ExecuteRequestOptions): Promise>; /** * Downloads a file from the given URL as a blob. */ downloadFileAsync(opts: DownloadFileRequestOptions): Promise>; /** * This method is used to upload a kontent.ai binary file. */ uploadFileAsync(opts: UploadFileRequestOptions): Promise>; }; export type AdapterResponse = { readonly toJsonAsync: () => Promise; readonly toBlobAsync: () => Promise; readonly isValidResponse: boolean; readonly responseHeaders: readonly Header[]; readonly status: TStatusCode; readonly statusText: string; }; export type AdapterRequestOptions = { readonly url: string; readonly method: HttpMethod; readonly body: string | Blob | undefined | null; readonly requestHeaders?: readonly Header[]; }; /** * Defines the adapter responsible solely for executing HTTP requests. * * This implementation should focus exclusively on performing the request itself. * It should not handle concerns such as retry policies, header manipulation, response parsing, or request body transformation. * * To extend functionality, you can implement a custom adapter and pass it to the `getDefaultHttpService` function, * which handles additional concerns like retries and parsing. * * Alternatively, you may implement the entire `HttpService` interface to create a fully customized HTTP service. */ export type HttpAdapter = { readonly requestAsync: (options: AdapterRequestOptions) => Promise; }; export {};