add fetcher option to api to allow local calls

This commit is contained in:
dswbx
2025-01-25 19:29:36 +01:00
parent ba1eb5fe08
commit 2d6d83ccb2
3 changed files with 18 additions and 18 deletions

View File

@@ -27,10 +27,7 @@ describe("ModuleApi", () => {
it("fetches endpoint", async () => { it("fetches endpoint", async () => {
const app = new Hono().get("/endpoint", (c) => c.json({ foo: "bar" })); const app = new Hono().get("/endpoint", (c) => c.json({ foo: "bar" }));
const api = new Api({ host }); const api = new Api({ host }, app.request as typeof fetch);
// @ts-expect-error it's protected
api.fetcher = app.request as typeof fetch;
const res = await api.get("/endpoint"); const res = await api.get("/endpoint");
expect(res.res.ok).toEqual(true); expect(res.res.ok).toEqual(true);
@@ -41,10 +38,7 @@ describe("ModuleApi", () => {
it("has accessible request", async () => { it("has accessible request", async () => {
const app = new Hono().get("/endpoint", (c) => c.json({ foo: "bar" })); const app = new Hono().get("/endpoint", (c) => c.json({ foo: "bar" }));
const api = new Api({ host }); const api = new Api({ host }, app.request as typeof fetch);
// @ts-expect-error it's protected
api.fetcher = app.request as typeof fetch;
const promise = api.get("/endpoint"); const promise = api.get("/endpoint");
expect(promise.request).toBeDefined(); expect(promise.request).toBeDefined();

View File

@@ -23,6 +23,7 @@ export type ApiOptions = {
headers?: Headers; headers?: Headers;
key?: string; key?: string;
localStorage?: boolean; localStorage?: boolean;
fetcher?: typeof fetch;
}; };
export type AuthState = { export type AuthState = {
@@ -163,14 +164,18 @@ export class Api {
headers: this.options.headers, headers: this.options.headers,
token_transport: this.token_transport token_transport: this.token_transport
}; };
const fetcher = this.options.fetcher;
this.system = new SystemApi(baseParams); this.system = new SystemApi(baseParams, fetcher);
this.data = new DataApi(baseParams); this.data = new DataApi(baseParams, fetcher);
this.auth = new AuthApi({ this.auth = new AuthApi(
...baseParams, {
onTokenUpdate: (token) => this.updateToken(token, true) ...baseParams,
}); onTokenUpdate: (token) => this.updateToken(token, true)
this.media = new MediaApi(baseParams); },
fetcher
);
this.media = new MediaApi(baseParams, fetcher);
} }
} }

View File

@@ -23,9 +23,10 @@ export type ApiResponse<Data = any> = {
export type TInput = string | (string | number | PrimaryFieldType)[]; export type TInput = string | (string | number | PrimaryFieldType)[];
export abstract class ModuleApi<Options extends BaseModuleApiOptions = BaseModuleApiOptions> { export abstract class ModuleApi<Options extends BaseModuleApiOptions = BaseModuleApiOptions> {
protected fetcher?: typeof fetch; constructor(
protected readonly _options: Partial<Options> = {},
constructor(protected readonly _options: Partial<Options> = {}) {} protected fetcher?: typeof fetch
) {}
protected getDefaultOptions(): Partial<Options> { protected getDefaultOptions(): Partial<Options> {
return {}; return {};