import { HttpAdapter } from "../adapter"; import { Axios, AxiosRequestConfig } from "axios"; import { HttpConfig } from "../types"; import { AxiosFlags } from "./types"; import { BaseError, OperationKeys, PrimaryKeyType } from "@decaf-ts/db-decorators"; import { Context, ContextualArgs, PreparedStatement } from "@decaf-ts/core"; import { Model } from "@decaf-ts/decorator-validation"; import { Constructor } from "@decaf-ts/decoration"; /** * @description Axios implementation of the HTTP adapter * @summary Concrete implementation of HttpAdapter using Axios as the HTTP client. * This adapter provides CRUD operations for RESTful APIs using Axios for HTTP requests. * @template Axios - The Axios client type * @template AxiosRequestConfig - The Axios request configuration type * @template AxiosFlags - The flags type extending HttpFlags * @template Context - The context type for this adapter * @param {Axios} native - The Axios instance * @param {HttpConfig} config - Configuration for the HTTP adapter * @param {string} [alias] - Optional alias for the adapter * @class * @example * ```typescript * import axios from 'axios'; * import { AxiosHttpAdapter } from '@decaf-ts/for-http'; * * const config = { protocol: 'https', host: 'api.example.com' }; * const adapter = new AxiosHttpAdapter(axios.create(), config); * * // Use the adapter with a repository * const userRepo = adapter.getRepository(User); * const user = await userRepo.findById('123'); * ``` * @mermaid * sequenceDiagram * participant Client * participant AxiosHttpAdapter * participant Axios * participant API * * Client->>AxiosHttpAdapter: create(table, id, data) * AxiosHttpAdapter->>AxiosHttpAdapter: url(table) * AxiosHttpAdapter->>Axios: post(url, data) * Axios->>API: HTTP POST Request * API-->>Axios: Response * Axios-->>AxiosHttpAdapter: Response Data * AxiosHttpAdapter-->>Client: Created Resource * * Client->>AxiosHttpAdapter: read(table, id) * AxiosHttpAdapter->>AxiosHttpAdapter: url(table, {id}) * AxiosHttpAdapter->>Axios: get(url) * Axios->>API: HTTP GET Request * API-->>Axios: Response * Axios-->>AxiosHttpAdapter: Response Data * AxiosHttpAdapter-->>Client: Resource Data */ export declare class AxiosHttpAdapter extends HttpAdapter, Context> { constructor(config: HttpConfig, alias?: string); protected getClient(): Axios; toRequest(query: PreparedStatement): AxiosRequestConfig; toRequest(ctx: Context): AxiosRequestConfig; toRequest(query: PreparedStatement, ctx: Context): AxiosRequestConfig; /** * @description Sends an HTTP request using Axios * @summary Implementation of the abstract request method from HttpAdapter. * This method uses the Axios instance to send HTTP requests with the provided configuration. * @template V - The response value type * @param {AxiosRequestConfig} details - The Axios request configuration * @return {Promise} A promise that resolves with the response data */ request(details: AxiosRequestConfig, ...args: ContextualArgs>): Promise; parseResponse(clazz: Constructor, method: OperationKeys | string, res: any): any; private normalizeResponseBody; /** * @description Creates a new resource via HTTP POST * @summary Implementation of the abstract create method from HttpAdapter. * This method sends a POST request to the specified endpoint with the model data. * @param {string} tableName - The name of the table or endpoint * @param {string|number} id - The identifier for the resource (not used in URL for POST) * @param {Record} model - The data model to create * @return {Promise>} A promise that resolves with the created resource */ create(tableName: Constructor, id: PrimaryKeyType, model: M, ...args: ContextualArgs>): Promise>; createAll(clazz: Constructor, id: PrimaryKeyType[], model: M[], ...args: ContextualArgs>): Promise[]>; /** * @description Retrieves a resource by ID via HTTP GET * @summary Implementation of the abstract read method from HttpAdapter. * This method sends a GET request to the specified endpoint with the ID as a query parameter. * @param {string} tableName - The name of the table or endpoint * @param {string|number|bigint} id - The identifier for the resource to retrieve * @return {Promise>} A promise that resolves with the retrieved resource */ read(tableName: Constructor | string, id: PrimaryKeyType, ...args: ContextualArgs>): Promise>; readAll(tableName: Constructor | string, ids: PrimaryKeyType[], ...args: ContextualArgs>): Promise[]>; /** * @description Updates an existing resource via HTTP PUT * @summary Implementation of the abstract update method from HttpAdapter. * This method sends a PUT request to the specified endpoint with the updated model data. * @param {string} tableName - The name of the table or endpoint * @param {string|number} id - The identifier for the resource (not used in URL for PUT) * @param {Record} model - The updated data model * @return {Promise>} A promise that resolves with the updated resource */ update(tableName: Constructor, id: PrimaryKeyType, model: Record, ...args: ContextualArgs>): Promise>; updateAll(tableName: Constructor, ids: PrimaryKeyType[], model: M[], ...args: ContextualArgs>): Promise[]>; /** * @description Deletes a resource by ID via HTTP DELETE * @summary Implementation of the abstract delete method from HttpAdapter. * This method sends a DELETE request to the specified endpoint with the ID as a query parameter. * @param {string} tableName - The name of the table or endpoint * @param {string|number|bigint} id - The identifier for the resource to delete * @return {Promise>} A promise that resolves with the deletion result */ delete(tableName: Constructor | string, id: PrimaryKeyType, ...args: ContextualArgs>): Promise>; deleteAll(tableName: Constructor | string, ids: PrimaryKeyType[], ...args: ContextualArgs>): Promise[]>; parseError(err: Error, ...args: any[]): E; }