@nuecms/sdk-builder
Version:
An advanced, modular SDK Builder for creating universal API clients in browser and Node.js.
130 lines (129 loc) • 4.98 kB
TypeScript
import { CacheProvider } from '../cache/cacheProvider';
import { ResponseTransformer } from '../transformers/responseTransformer';
export interface FetchContext<Params = Record<string, any>> {
body: Params;
headers: Record<string, string>;
path: string;
method: string;
endpointName: string;
url: string;
params: Record<string, any>;
extParams?: Record<string, any>;
config: Record<string, any>;
}
export interface SdkBuilderConfig {
retryStatus?: (status: number) => boolean;
validateStatus?: (status: number) => boolean;
retryDelay?: number;
maxRetries?: number;
method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
baseUrl: string;
defaultHeaders?: Record<string, string>;
timeout?: number;
type?: 'json' | 'text' | 'blob';
responseFormat?: 'json' | 'text' | 'blob' | 'buffer';
cacheProvider?: CacheProvider;
customResponseTransformer?: ResponseTransformer<FetchContext>;
placeholders?: Record<string, string>;
config?: Record<string, any>;
authCheckStatus?: (status: number, response?: object, fetchContext?: FetchContext) => boolean;
}
interface ExecuteApiCallOptions<Params> {
method: string;
body?: Params;
headers?: Record<string, string>;
params?: Record<string, any>;
extParams?: Record<string, any>;
endpointName: string;
dataType?: string;
contentType?: string;
stringifyBody?: (body: Record<string, any>) => string;
retryDelay?: number;
maxRetries?: number;
endPoint?: string;
}
type Params = Record<string, any>;
type ArgumentParams = Array<any>;
type Response = any;
type EndpointPureFunction<ArgumentParams extends any[] = any[], Response = any> = (config: Record<string, any>, ...params: ArgumentParams) => Promise<Response>;
type EndpointAFunction<Params = any, Response = any> = (params?: Params) => Promise<Response>;
type EndpointBFunction<Params = any, Response = any> = (params: Params, extParams?: any) => Promise<Response>;
export declare const defaultConfig: {
defaultHeaders: {
'Content-Type': string;
};
timeout: number;
type: string;
responseFormat: "json";
maxRetries: number;
retryDelay: number;
method: string;
config: {};
placeholders: {};
authCheckStatus: (status: number) => status is 401;
validateStatus: (status: number) => boolean;
retryStatus: (status: number) => boolean;
};
export declare const updateDefaultConfig: (config: SdkBuilderConfig) => void;
export declare class RequestError extends Error {
}
export declare class SdkBuilder {
private baseUrl;
private defaultHeaders;
private timeout;
private responseFormat;
private customResponseTransformer?;
private endpoints;
private placeholders;
private config;
private type;
private maxRetries;
private retryDelay;
private method;
cacheProvider: CacheProvider;
[x: string]: any;
authCheckStatus: (status: number, response?: object, fetchContext?: FetchContext) => boolean;
constructor(config: SdkBuilderConfig);
/**
* Registers a new endpoint.
*/
r<K extends string, P extends string>(name: K, path: P, method?: string): asserts this is this & Record<K, EndpointBFunction<Params, Response>>;
/**
* Registers a new endpoint with options.
*/
z<K extends string, P extends Record<string, any>>(name: K, opt: P): asserts this is this & Record<K, EndpointBFunction<Params, Response>>;
/**
* Registers a custom function.
*/
rx<K extends string, P extends EndpointPureFunction<ArgumentParams, Response>>(name: K, path: P): asserts this is this & Record<K, EndpointAFunction<ArgumentParams, Response>>;
/**
* Enhances the current configuration with new values.
*/
enhanceConfig(config: Record<string, any>): Promise<void>;
requestInterceptor(req: any): Promise<any>;
private handleAuthError;
private executeApiCall;
private callApi;
/**
* Call the API without an endpoint name.
*/
callApiWithoutEndpoint<Params extends Record<string, any>, Response>(path: string, options: Omit<ExecuteApiCallOptions<Params>, 'endpointName'>): Promise<Response | undefined>;
/**
* Public method to make a POST request.
*/
post<Params extends Record<string, any>, Response>(path: string, options: Omit<ExecuteApiCallOptions<Params>, 'method' | 'endpointName'> & {
body: Params;
}): Promise<Response | undefined>;
/**
* Public method to make a GET request.
*/
get<Params extends Record<string, any>, Response>(path: string, options: Omit<ExecuteApiCallOptions<Params>, 'method' | 'endpointName'> & {
params: Record<string, any>;
}): Promise<Response | undefined>;
private resolveHeaders;
private getResponseFormat;
private resolvePath;
private delay;
}
export declare const sdkBuilder: (config: SdkBuilderConfig) => SdkBuilder & Record<string, any>;
export {};