mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
added useApi and useApiQuery to work seamlessly with SWR (tbc)
This commit is contained in:
@@ -21,7 +21,7 @@ export type ApiResponse<Data = any> = {
|
||||
export type TInput = string | (string | number | PrimaryFieldType)[];
|
||||
|
||||
export abstract class ModuleApi<Options extends BaseModuleApiOptions = BaseModuleApiOptions> {
|
||||
fetcher = fetch;
|
||||
protected fetcher?: typeof fetch;
|
||||
|
||||
constructor(protected readonly _options: Partial<Options> = {}) {}
|
||||
|
||||
@@ -136,17 +136,18 @@ export abstract class ModuleApi<Options extends BaseModuleApiOptions = BaseModul
|
||||
}
|
||||
}
|
||||
|
||||
class FetchPromise<T> implements Promise<T> {
|
||||
export class FetchPromise<T = ApiResponse<any>> implements Promise<T> {
|
||||
// @ts-ignore
|
||||
[Symbol.toStringTag]: "FetchPromise";
|
||||
|
||||
constructor(
|
||||
public request: Request,
|
||||
protected fetcher = fetch
|
||||
protected fetcher?: typeof fetch
|
||||
) {}
|
||||
|
||||
async execute() {
|
||||
const res = await this.fetcher(this.request);
|
||||
const fetcher = this.fetcher ?? fetch;
|
||||
const res = await fetcher(this.request);
|
||||
let resBody: any;
|
||||
let resData: any;
|
||||
|
||||
@@ -195,4 +196,9 @@ class FetchPromise<T> implements Promise<T> {
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
getKey(): string {
|
||||
const url = new URL(this.request.url);
|
||||
return url.pathname + url.search;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user