simple-queries-react
Version:
Simple Queries React: Streamlined requests, powerful results. Transform your API calls into something simple, fast, and scalable with just a few lines of code.
182 lines (167 loc) • 7.27 kB
TypeScript
type PathMap = Record<string, string | number | null | undefined>;
type AnyObject = Record<string, any>;
type FetchHeaders = Record<string, string>;
type ApiRequest<T = any> = {
url: string;
endpoint: string;
pathRest: AnyObject;
params: AnyObject;
body: AnyObject;
headers: FetchHeaders | undefined;
errorFn: (data: any) => void;
files: File | File[];
apiName: string;
methods: MethodsRequest;
fileName: [string, string];
onSuccess: (data?: T) => void;
bodyURLSearchParams: AnyObject;
};
type MethodsRequest = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
type HookRequest<T> = Pick<ApiRequest<T>, "endpoint" | "headers" | "errorFn" | "url" | "apiName" | "onSuccess" | "bodyURLSearchParams">;
type RequestGet = Pick<ApiRequest, "params" | "pathRest">;
type UseRequestHook<T = any> = Partial<Pick<HookRequest<T>, "endpoint" | "headers" | "errorFn" | "url" | "apiName" | "onSuccess" | "bodyURLSearchParams">>;
type Config = {
baseUrl: string;
bearerToken?: string | null;
headers?: FetchHeaders | undefined;
};
type SimpleQueriesConfig = Config & {
APIs?: (Config & {
name: string;
enableDefaultToken?: boolean;
})[];
};
type FileExtensions = "pdf" | "xml" | "doc" | "docx" | "xls" | "xlsx" | "ppt" | "pptx" | "zip" | "rar" | "jpg" | "jpeg" | "png" | "gif" | "mp3" | "mp4" | "avi";
type FetchDownload = Partial<Omit<ApiRequest, "body" | "files"> & {
download: boolean;
}>;
type UseDownloadHook = UseRequestHook & {
download?: boolean;
defaultName?: string;
extension?: Extension;
};
type Extension = FileExtensions | {
customExtension: string;
};
declare const initSimpleQueries: (config: Partial<SimpleQueriesConfig>) => void;
declare const setBearerToken: (token: string | null | undefined, apiName?: string | undefined) => void;
declare const cleanBearerToken: (apiName?: string | undefined) => void;
declare const setHeaders: (headers: FetchHeaders | undefined, apiName?: string | undefined) => void;
declare const deleteData: ({ url, endpoint, errorFn, pathRest, params, body, headers, apiName, onSuccess, }?: Partial<ApiRequest>) => Promise<any>;
declare const getData: ({ url, endpoint, errorFn, pathRest, params, body, headers, apiName, onSuccess, }?: Partial<ApiRequest>) => Promise<any>;
declare const patchData: ({ url, endpoint, errorFn, pathRest, params, body, headers, apiName, onSuccess, }?: Partial<ApiRequest>) => Promise<any>;
declare const postData: ({ url, endpoint, errorFn, pathRest, params, body, headers, apiName, onSuccess, bodyURLSearchParams }?: Partial<ApiRequest>) => Promise<any>;
declare const putData: ({ url, endpoint, errorFn, pathRest, params, body, headers, apiName, onSuccess, bodyURLSearchParams }?: Partial<ApiRequest>) => Promise<any>;
declare const downloadData: ({ url, endpoint, errorFn, pathRest, params, headers, apiName, fileName, download, methods, onSuccess, }?: Partial<ApiRequest<any> & {
download?: boolean | undefined;
}>) => Promise<string | undefined>;
declare function useDelete<T, B = AnyObject, P = AnyObject>(props?: UseRequestHook<T> | string | undefined | AnyObject): {
getResponse: () => T | undefined;
send: (data?: string | number | AnyObject | B | Partial<Partial<{
body: B;
pathRest: AnyObject;
params: P;
}>> | undefined) => void;
clearResponse: () => void;
isLoading: () => boolean;
cancelLoading: () => void;
getErrors: () => any;
clearErrors: () => void;
setHeaders: (headers: FetchHeaders) => void;
success: boolean;
error: boolean;
msgErrors: any;
};
declare function useDownload<P = AnyObject>(props?: UseDownloadHook | string | undefined | AnyObject): {
getResponse: () => string | undefined;
send: (data?: Partial<Partial<{
pathRest: AnyObject;
params: P;
}> & {
download?: boolean | undefined;
fileName?: string | undefined;
extension: Extension;
}> | undefined) => void;
clearResponse: () => void;
isLoading: () => boolean;
cancelLoading: () => void;
getErrors: () => any;
clearErrors: () => void;
setHeaders: (headers: FetchHeaders) => void;
success: boolean;
error: boolean;
msgErrors: any;
};
declare function useGet<T, P = AnyObject, B = AnyObject>(props?: UseRequestHook<T> | string | undefined | AnyObject): {
getResponse: () => T | undefined;
send: (data?: string | number | AnyObject | P | Partial<Partial<{
pathRest: AnyObject;
params: P;
body: B;
}>> | undefined) => void;
clearResponse: () => void;
isLoading: () => boolean;
cancelLoading: () => void;
getErrors: () => any;
clearErrors: () => void;
setHeaders: (headers: FetchHeaders) => void;
success: boolean;
error: boolean;
msgErrors: any;
};
declare function usePatch<T, B = AnyObject, P = AnyObject>(props?: UseRequestHook<T> | string | undefined | AnyObject): {
getResponse: () => T | undefined;
send: (data?: string | number | AnyObject | B | Partial<Partial<{
body: B;
pathRest: AnyObject;
params: P;
}>> | undefined) => void;
clearResponse: () => void;
isLoading: () => boolean;
cancelLoading: () => void;
getErrors: () => any;
clearErrors: () => void;
setHeaders: (headers: FetchHeaders) => void;
msgErrors: any;
success: boolean;
error: boolean;
};
declare function usePost<T, B = AnyObject, P = AnyObject>(props?: UseRequestHook<T> | string | undefined | AnyObject): {
getResponse: () => T | undefined;
send: (data?: string | number | AnyObject | B | (Partial<Partial<{
bodyURLSearchParams: AnyObject;
body: B;
pathRest: AnyObject;
params: P;
}>> & {
bodyURLSearchParams: AnyObject;
}) | undefined) => void;
clearResponse: () => void;
isLoading: () => boolean;
cancelLoading: () => void;
getErrors: () => any;
clearErrors: () => void;
setHeaders: (headers: FetchHeaders) => void;
success: boolean;
error: boolean;
msgErrors: any;
};
declare function usePut<T, B = AnyObject, P = AnyObject>(props?: UseRequestHook<T> | string | undefined | AnyObject): {
getResponse: () => T | undefined;
send: (data?: string | number | AnyObject | B | Partial<Partial<{
body: B;
pathRest: AnyObject;
params: P;
bodyURLSearchParams: AnyObject;
}>> | undefined) => void;
clearResponse: () => void;
isLoading: () => boolean;
cancelLoading: () => void;
getErrors: () => any;
clearErrors: () => void;
setHeaders: (headers: FetchHeaders) => void;
success: boolean;
error: boolean;
msgErrors: any;
};
export { AnyObject, ApiRequest, Config, Extension, FetchDownload, FetchHeaders, FileExtensions, HookRequest, MethodsRequest, PathMap, RequestGet, SimpleQueriesConfig, UseDownloadHook, UseRequestHook, cleanBearerToken, deleteData, downloadData, getData, initSimpleQueries, patchData, postData, putData, setBearerToken, setHeaders, useDelete, useDownload, useGet, usePatch, usePost, usePut };