refactor: enhance MediaApi typing and improve vite example config handling for d1

Updated `MediaApi` to include improved generic typing for upload methods, ensuring type safety and consistency. Refactored example configuration logic in development environment setup for better modularity and maintainability.
This commit is contained in:
dswbx
2025-10-13 10:41:15 +02:00
parent e6ff5c3f0b
commit fd3dd310a5
3 changed files with 68 additions and 22 deletions

View File

@@ -1,11 +1,14 @@
import type { FileListObject } from "media/storage/Storage"; import type { FileListObject } from "media/storage/Storage";
import { import {
type BaseModuleApiOptions, type BaseModuleApiOptions,
type FetchPromise,
type ResponseObject,
ModuleApi, ModuleApi,
type PrimaryFieldType, type PrimaryFieldType,
type TInput, type TInput,
} from "modules/ModuleApi"; } from "modules/ModuleApi";
import type { ApiFetcher } from "Api"; import type { ApiFetcher } from "Api";
import type { DB, FileUploadedEventData } from "bknd";
export type MediaApiOptions = BaseModuleApiOptions & { export type MediaApiOptions = BaseModuleApiOptions & {
upload_fetcher: ApiFetcher; upload_fetcher: ApiFetcher;
@@ -67,14 +70,14 @@ export class MediaApi extends ModuleApi<MediaApiOptions> {
return new Headers(); return new Headers();
} }
protected uploadFile( protected uploadFile<T extends FileUploadedEventData>(
body: File | Blob | ReadableStream | Buffer<ArrayBufferLike>, body: File | Blob | ReadableStream | Buffer<ArrayBufferLike>,
opts?: { opts?: {
filename?: string; filename?: string;
path?: TInput; path?: TInput;
_init?: Omit<RequestInit, "body">; _init?: Omit<RequestInit, "body">;
}, },
) { ): FetchPromise<ResponseObject<T>> {
const headers = { const headers = {
"Content-Type": "application/octet-stream", "Content-Type": "application/octet-stream",
...(opts?._init?.headers || {}), ...(opts?._init?.headers || {}),
@@ -106,10 +109,10 @@ export class MediaApi extends ModuleApi<MediaApiOptions> {
throw new Error("Invalid filename"); throw new Error("Invalid filename");
} }
return this.post(opts?.path ?? ["upload", name], body, init); return this.post<T>(opts?.path ?? ["upload", name], body, init);
} }
async upload( async upload<T extends FileUploadedEventData>(
item: Request | Response | string | File | Blob | ReadableStream | Buffer<ArrayBufferLike>, item: Request | Response | string | File | Blob | ReadableStream | Buffer<ArrayBufferLike>,
opts: { opts: {
filename?: string; filename?: string;
@@ -124,12 +127,12 @@ export class MediaApi extends ModuleApi<MediaApiOptions> {
if (!res.ok || !res.body) { if (!res.ok || !res.body) {
throw new Error("Failed to fetch file"); throw new Error("Failed to fetch file");
} }
return this.uploadFile(res.body, opts); return this.uploadFile<T>(res.body, opts);
} else if (item instanceof Response) { } else if (item instanceof Response) {
if (!item.body) { if (!item.body) {
throw new Error("Invalid response"); throw new Error("Invalid response");
} }
return this.uploadFile(item.body, { return this.uploadFile<T>(item.body, {
...(opts ?? {}), ...(opts ?? {}),
_init: { _init: {
...(opts._init ?? {}), ...(opts._init ?? {}),
@@ -141,7 +144,7 @@ export class MediaApi extends ModuleApi<MediaApiOptions> {
}); });
} }
return this.uploadFile(item, opts); return this.uploadFile<T>(item, opts);
} }
async uploadToEntity( async uploadToEntity(
@@ -153,7 +156,7 @@ export class MediaApi extends ModuleApi<MediaApiOptions> {
_init?: Omit<RequestInit, "body">; _init?: Omit<RequestInit, "body">;
fetcher?: typeof fetch; fetcher?: typeof fetch;
}, },
) { ): Promise<ResponseObject<FileUploadedEventData & { result: DB["media"] }>> {
return this.upload(item, { return this.upload(item, {
...opts, ...opts,
path: ["entity", entity, id, field], path: ["entity", entity, id, field],

View File

@@ -1,8 +1,14 @@
import type { DB, PrimaryFieldType, EntityData, RepoQueryIn } from "bknd"; import type {
DB,
PrimaryFieldType,
EntityData,
RepoQueryIn,
RepositoryResult,
ResponseObject,
ModuleApi,
} from "bknd";
import { objectTransform, encodeSearch } from "bknd/utils"; import { objectTransform, encodeSearch } from "bknd/utils";
import type { RepositoryResult } from "data/entities";
import type { Insertable, Selectable, Updateable } from "kysely"; import type { Insertable, Selectable, Updateable } from "kysely";
import type { FetchPromise, ModuleApi, ResponseObject } from "modules/ModuleApi";
import useSWR, { type SWRConfiguration, type SWRResponse, mutate } from "swr"; import useSWR, { type SWRConfiguration, type SWRResponse, mutate } from "swr";
import { type Api, useApi } from "ui/client"; import { type Api, useApi } from "ui/client";
@@ -108,7 +114,7 @@ export function makeKey(
); );
} }
interface UseEntityQueryReturn< export interface UseEntityQueryReturn<
Entity extends keyof DB | string, Entity extends keyof DB | string,
Id extends PrimaryFieldType | undefined = undefined, Id extends PrimaryFieldType | undefined = undefined,
Data = Entity extends keyof DB ? Selectable<DB[Entity]> : EntityData, Data = Entity extends keyof DB ? Selectable<DB[Entity]> : EntityData,
@@ -136,11 +142,11 @@ export const useEntityQuery = <
const fetcher = () => read(query ?? {}); const fetcher = () => read(query ?? {});
type T = Awaited<ReturnType<typeof fetcher>>; type T = Awaited<ReturnType<typeof fetcher>>;
const swr = useSWR<T>(options?.enabled === false ? null : key, fetcher as any, { const swr = useSWR(options?.enabled === false ? null : key, fetcher as any, {
revalidateOnFocus: false, revalidateOnFocus: false,
keepPreviousData: true, keepPreviousData: true,
...options, ...options,
}); }) as ReturnType<typeof useSWR<T>>;
const mutateFn = async (id?: PrimaryFieldType) => { const mutateFn = async (id?: PrimaryFieldType) => {
const entityKey = makeKey(api, entity as string, id); const entityKey = makeKey(api, entity as string, id);

View File

@@ -1,4 +1,4 @@
import { readFile } from "node:fs/promises"; import { readFile, writeFile } from "node:fs/promises";
import { serveStatic } from "@hono/node-server/serve-static"; import { serveStatic } from "@hono/node-server/serve-static";
import { showRoutes } from "hono/dev"; import { showRoutes } from "hono/dev";
import { App, registries, type CreateAppConfig } from "./src"; import { App, registries, type CreateAppConfig } from "./src";
@@ -9,6 +9,7 @@ import { $console } from "core/utils/console";
import { createClient } from "@libsql/client"; import { createClient } from "@libsql/client";
import util from "node:util"; import util from "node:util";
import { d1Sqlite } from "adapter/cloudflare/connection/D1Connection"; import { d1Sqlite } from "adapter/cloudflare/connection/D1Connection";
import { slugify } from "./src/core/utils/strings";
util.inspect.defaultOptions.depth = 5; util.inspect.defaultOptions.depth = 5;
registries.media.register("local", StorageLocalAdapter); registries.media.register("local", StorageLocalAdapter);
@@ -21,16 +22,19 @@ $console.debug("Using db type", dbType);
let dbUrl = import.meta.env.VITE_DB_URL ?? ":memory:"; let dbUrl = import.meta.env.VITE_DB_URL ?? ":memory:";
const example = import.meta.env.VITE_EXAMPLE; const example = import.meta.env.VITE_EXAMPLE;
if (example) { async function loadExampleConfig() {
if (example) {
const configPath = `.configs/${example}.json`; const configPath = `.configs/${example}.json`;
$console.debug("Loading config from", configPath); $console.debug("Loading config from", configPath);
const exampleConfig = JSON.parse(await readFile(configPath, "utf-8")); const exampleConfig = JSON.parse(await readFile(configPath, "utf-8"));
config.config = exampleConfig; config.config = exampleConfig;
dbUrl = `file:.configs/${example}.db`; dbUrl = `file:.configs/${example}.db`;
}
} }
switch (dbType) { switch (dbType) {
case "libsql": { case "libsql": {
await loadExampleConfig();
$console.debug("Using libsql connection", dbUrl); $console.debug("Using libsql connection", dbUrl);
const authToken = import.meta.env.VITE_DB_LIBSQL_TOKEN; const authToken = import.meta.env.VITE_DB_LIBSQL_TOKEN;
config.connection = libsql( config.connection = libsql(
@@ -43,15 +47,48 @@ switch (dbType) {
} }
case "d1": { case "d1": {
$console.debug("Using d1 connection"); $console.debug("Using d1 connection");
const wranglerConfig = {
name: "vite-dev",
main: "src/index.ts",
compatibility_date: "2025-08-03",
compatibility_flags: ["nodejs_compat"],
d1_databases: [
{
binding: "DB",
database_name: "vite-dev",
database_id: "00000000-0000-0000-0000-000000000000",
},
],
r2_buckets: [
{
binding: "BUCKET",
bucket_name: "vite-dev",
},
],
};
let configPath = ".configs/vite.wrangler.json";
if (example) {
const name = slugify(example);
configPath = `.configs/${slugify(example)}.wrangler.json`;
const exists = await readFile(configPath, "utf-8");
if (!exists) {
wranglerConfig.name = name;
wranglerConfig.d1_databases[0]!.database_name = name;
wranglerConfig.d1_databases[0]!.database_id = crypto.randomUUID();
wranglerConfig.r2_buckets[0]!.bucket_name = name;
await writeFile(configPath, JSON.stringify(wranglerConfig, null, 2));
}
}
const { getPlatformProxy } = await import("wrangler"); const { getPlatformProxy } = await import("wrangler");
const platformProxy = await getPlatformProxy({ const platformProxy = await getPlatformProxy({
configPath: "./vite.wrangler.json", configPath,
}); });
config.connection = d1Sqlite({ binding: platformProxy.env.DB as any }); config.connection = d1Sqlite({ binding: platformProxy.env.DB as any });
break; break;
} }
default: { default: {
await loadExampleConfig();
$console.debug("Using node-sqlite connection", dbUrl); $console.debug("Using node-sqlite connection", dbUrl);
config.connection = nodeSqlite({ url: dbUrl }); config.connection = nodeSqlite({ url: dbUrl });
break; break;