@iamnnort/request
Version:
Request handler for Node.js - Fast - Interactive - Simple
170 lines (158 loc) • 5.72 kB
text/typescript
import * as _iamnnort_config_http from '@iamnnort/config/http';
import { HttpStatuses, HttpMethods } from '@iamnnort/config/http';
export { HttpMethods, HttpStatuses } from '@iamnnort/config/http';
import { AxiosRequestConfig, AxiosResponse, AxiosError, ParamsSerializerOptions } from 'axios';
type LoggerConfig = {
name: string;
level: LoggerLevels;
};
declare enum LoggerLevels {
FATAL = "fatal",
ERROR = "error",
WARN = "warn",
INFO = "info",
DEBUG = "debug",
TRACE = "trace"
}
declare class Logger {
private logger;
private config;
constructor(config?: Partial<LoggerConfig>);
trace(message: string | object): void;
debug(message: string | object): void;
info(message: string | object): void;
warn(message: string | object): void;
error(message: string | object): void;
fatal(message: string | object): void;
logRequest(request: AxiosRequestConfig): void;
logResponse(response: AxiosResponse, duration: number): void;
logError(request: AxiosRequestConfig, error: AxiosError, duration: number): void;
makeResponse<T>(response: AxiosResponse): {
success: boolean;
status: HttpStatuses;
method: _iamnnort_config_http.HttpMethods;
data: T;
};
makeErrorResponse<T>(error: AxiosError): {
success: boolean;
status: HttpStatuses;
method: _iamnnort_config_http.HttpMethods;
data: T;
};
}
type PaginationDto = {
pagination?: boolean | null;
page?: number | null;
pageSize?: number | null;
bulkSize?: number | null;
};
type Pagination = {
total: number;
currentPage: number;
lastPage: number;
from: number;
to: number;
pageSize: number;
};
type PaginationResponse<T = unknown> = {
data: T[];
pagination: Pagination;
};
type SerializerConfig = {
arrayFormat: SerializerArrayFormats;
};
declare enum SerializerArrayFormats {
INDICES = "indices",
BRACKETS = "brackets",
REPEAT = "repeat",
COMMA = "comma"
}
declare class Serializer {
private config;
constructor(config?: Partial<SerializerConfig>);
getConfig(): ParamsSerializerOptions;
}
type SignerConfig = {
secretKey: string;
header?: string;
};
declare class Signer {
private config;
constructor(config?: Partial<SignerConfig>);
private sign;
getConfig(body: string): {
[x: string]: string;
};
}
type RequestParams = Pick<AxiosRequestConfig, 'params' | 'data'>;
type RequestConfigParams = Pick<AxiosRequestConfig, 'params' | 'data'>;
type RequestConfig = Omit<AxiosRequestConfig, 'baseURL' | 'url'> & {
baseUrl?: string;
baseUrlName?: string;
baseUrlMap?: Record<string, string>;
url?: number | string;
urlParts?: (number | string)[];
bearerToken?: string;
apiKey?: string;
urlencoded?: boolean;
multipart?: boolean;
xml?: boolean;
};
type BaseRequestConfig = Pick<AxiosRequestConfig, 'auth' | 'headers' | 'timeout' | 'responseType'> & {
baseUrl?: string;
baseUrlName?: string;
baseUrlMap?: Record<string, string>;
url?: number | string;
urlParts?: (number | string)[];
bearerToken?: string;
apiKey?: string;
serializer?: Partial<SerializerConfig>;
logger?: Partial<LoggerConfig>;
signer?: Partial<SignerConfig>;
};
type ResponseConfig = {
raw?: boolean;
bulkCallback?: (page: number) => Promise<void>;
};
type Response<T = unknown> = {
success: boolean;
data?: T;
errorCode?: string;
};
type RawResponse<T = unknown> = {
success: boolean;
status: HttpStatuses;
method: HttpMethods;
data: T;
};
declare class RequestDataSource<Entity extends Record<string, any> = any, SearchParams extends RequestConfigParams = any, SearchResponse extends Record<string, any> = any, CreateParams extends RequestConfigParams = any, UpdateParams extends RequestConfigParams = any> {
baseRequestConfig: BaseRequestConfig;
logger: Logger;
constructor(baseRequestConfig: BaseRequestConfig);
common<T>(requestConfig: RequestConfig, responseConfig?: ResponseConfig): Promise<T>;
common<T>(requestConfig: RequestConfig, responseConfig: ResponseConfig & {
raw: true;
}): Promise<RawResponse<T>>;
bulkCommon<T>(requestConfig: RequestConfig, responseConfig?: ResponseConfig): AsyncGenerator<T[]>;
bulkCommon<T>(requestConfig: RequestConfig, responseConfig: ResponseConfig & {
raw: true;
}): AsyncGenerator<PaginationResponse<T>>;
search(config?: SearchParams): Promise<SearchResponse>;
bulkSearch(config?: SearchParams): AsyncGenerator<Entity[], any, any>;
searchOne(config?: SearchParams): Promise<Entity>;
get(id: number | string, config?: SearchParams): Promise<Entity>;
create(config: CreateParams): Promise<Entity>;
bulkCreate(config: Omit<CreateParams, 'data'> & {
data: CreateParams['data'][];
}): Promise<Entity[]>;
update(id: number | string, config: UpdateParams): Promise<Entity>;
bulkUpdate(config: Omit<UpdateParams, 'data'> & {
data: UpdateParams['data'][];
}): Promise<Entity[]>;
remove(id: number | string, config?: SearchParams): Promise<void>;
bulkRemove(config?: SearchParams): Promise<void[]>;
}
declare class RequestHelper {
static sleep(seconds: number): Promise<unknown>;
}
export { type BaseRequestConfig, Logger, type LoggerConfig, LoggerLevels, type Pagination, type PaginationDto, type PaginationResponse, type RawResponse, type RequestConfig, type RequestConfigParams, RequestDataSource, RequestHelper, type RequestParams, type Response, type ResponseConfig, Serializer, SerializerArrayFormats, type SerializerConfig, Signer, type SignerConfig };