Merge pull request #63 from bknd-io/fix/api-and-adapter

fix/api-and-adapter
This commit is contained in:
dswbx
2025-01-28 08:16:56 +01:00
committed by GitHub
11 changed files with 29 additions and 25 deletions

View File

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

View File

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

View File

@@ -1,6 +1,7 @@
import type { CreateUserPayload } from "auth/AppAuth";
import { Event } from "core/events";
import { Connection, type LibSqlCredentials, LibsqlConnection } from "data";
import type { Hono } from "hono";
import {
type InitialModuleConfigs,
ModuleManager,
@@ -132,7 +133,7 @@ export class App {
return this.modules.ctx().em;
}
get fetch(): any {
get fetch(): Hono["fetch"] {
return this.server.fetch;
}
@@ -155,6 +156,10 @@ export class App {
return this.modules.version();
}
isBuilt(): boolean {
return this.modules.isBuilt();
}
registerAdminController(config?: AdminControllerOptions) {
// register admin
this.adminController = new AdminController(this, config);

View File

@@ -1,2 +1 @@
export * from "./astro.adapter";
export { registerLocalMediaAdapter } from "bknd/adapter/node";

View File

@@ -1,2 +1 @@
export * from "./nextjs.adapter";
export { registerLocalMediaAdapter } from "bknd/adapter/node";

View File

@@ -1,3 +1,2 @@
export * from "./remix.adapter";
export * from "./AdminPage";
export { registerLocalMediaAdapter } from "bknd/adapter/node";

View File

@@ -1,2 +1 @@
export * from "./vite.adapter";
export { registerLocalMediaAdapter } from "bknd/adapter/node";

View File

@@ -259,7 +259,7 @@ export class Authenticator<Strategies extends Record<string, Strategy> = Record<
}
async requestCookieRefresh(c: Context) {
if (this.config.cookie.renew) {
if (this.config.cookie.renew && this.isUserLoggedIn()) {
const token = await this.getAuthCookie(c);
if (token) {
await this.setAuthCookie(c, token);

View File

@@ -12,7 +12,6 @@ export {
export * as middlewares from "modules/middlewares";
export { registries } from "modules/registries";
export type * from "./adapter";
export { Api, type ApiOptions } from "./Api";
export type { MediaFieldSchema } from "media/AppMedia";

View File

@@ -23,9 +23,10 @@ export type ApiResponse<Data = any> = {
export type TInput = string | (string | number | PrimaryFieldType)[];
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> {
return {};

View File

@@ -171,6 +171,10 @@ export class ModuleManager {
}
}
isBuilt(): boolean {
return this._built;
}
/**
* This is set through module's setListener
* It's called everytime a module's config is updated in SchemaObject