@akala/core
Version:
134 lines (133 loc) • 4.95 kB
TypeScript
import type { Formatter } from './formatters/common.js';
import type { MiddlewareAsync } from './middlewares/shared.js';
import type { TypedSerializableObject } from './helpers.js';
/**
* Configuration options for HTTP requests.
*/
export interface HttpOptions<T> {
method?: string;
url: string | URL;
queryString?: string | URLSearchParams;
body?: BodyInit | TypedSerializableObject<T>;
headers?: {
[key: string]: string | number | Date;
};
contentType?: 'json' | 'form' | 'form-urlencoded';
type?: 'json' | 'xml' | 'text' | 'raw';
}
/**
* Interface defining methods for making HTTP requests.
*/
export interface Http<TResponse = Response> {
/**
* Sends a GET request to the specified URL.
* @param url The URL to request.
* @param params Query parameters to append.
* @returns A promise resolving to the HTTP response.
*/
get(url: string | URL, params?: string | URLSearchParams): PromiseLike<TResponse>;
/**
* Sends a POST request with a body.
* @param url The target URL.
* @param body The request body.
* @returns A promise resolving to form data from the response.
*/
post(url: string | URL, body?: unknown): PromiseLike<FormData>;
/**
* Sends a JSON POST request.
* @param url The target URL.
* @param body The JSON body content.
* @returns A promise resolving to the parsed JSON response.
*/
postJSON<T = string>(url: string | URL, body?: unknown): PromiseLike<T>;
/**
* Sends a GET request expecting JSON response.
* @param url The target URL.
* @param params Query parameters to include.
* @returns A promise resolving to the parsed JSON data.
*/
getJSON<T>(url: string | URL, params?: string | URLSearchParams): PromiseLike<T>;
/**
* Sends a SOAP-based POST request.
* @param namespace SOAP namespace.
* @param action SOAP action name.
* @param url The target URL.
* @param params SOAP parameters.
* @returns A promise resolving to the SOAP response.
*/
invokeSOAP(namespace: string, action: string, url: string | URL, params?: {
[key: string]: string | number | boolean;
}): PromiseLike<TResponse>;
/**
* Sends a custom HTTP request using options.
* @param options Configuration for the request.
* @returns A promise resolving to the HTTP response.
*/
call<T>(options: HttpOptions<T>): PromiseLike<TResponse>;
}
export type CallInterceptor = MiddlewareAsync<[RequestInit, Response]>;
export declare class FetchHttp implements Http<Response> {
private interceptor;
constructor(interceptor: CallInterceptor);
/**
* Sends a GET request to the specified URL.
* @param url The URL to request.
* @param params Query parameters to append.
* @returns A promise resolving to the HTTP response.
*/
get(url: string, params?: URLSearchParams): Promise<Response>;
/**
* Sends a POST request with a body.
* @param url The target URL.
* @param body The request body.
* @returns A promise resolving to form data from the response.
*/
post(url: string, body?: BodyInit): PromiseLike<FormData>;
/**
* Sends a JSON POST request.
* @param url The target URL.
* @param body The JSON body content.
* @returns A promise resolving to the parsed JSON response.
*/
postJSON<T = string>(url: string, body?: BodyInit): PromiseLike<T>;
/**
* Sends a GET request expecting JSON response.
* @param url The target URL.
* @param params Query parameters to include.
* @returns A promise resolving to the parsed JSON data.
*/
getJSON<T>(url: string, params?: string | URLSearchParams): PromiseLike<T>;
/**
* Sends a SOAP-based POST request.
* @param namespace SOAP namespace.
* @param action SOAP action name.
* @param url The target URL.
* @param params SOAP parameters.
* @returns A promise resolving to the SOAP response.
*/
invokeSOAP(namespace: string, action: string, url: string, params?: {
[key: string]: string | number | boolean;
}): Promise<Response>;
/**
* Sends a custom HTTP request using options.
* @param options Configuration for the request.
* @returns A promise resolving to the HTTP response.
*/
call<T>(options: HttpOptions<T>): Promise<Response>;
private fetch;
/**
* Serializes an object into URL-encoded format.
*/
static serialize(obj: any, prefix?: string): string | FormData;
}
type SettingsType = {
method?: keyof Http;
} & Omit<HttpOptions<undefined>, 'url'>;
export declare class HttpCallFormatter implements Formatter<PromiseLike<Response>> {
private readonly settings;
private previousValue;
private previousCall;
constructor(settings: SettingsType);
format(scope: unknown): PromiseLike<Response>;
}
export {};