added useApi and useApiQuery to work seamlessly with SWR (tbc)

This commit is contained in:
dswbx
2024-12-12 10:37:52 +01:00
parent 43ec075a32
commit 9d9aa7b7a5
10 changed files with 72 additions and 24 deletions

View File

@@ -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;
}
}