Compare commits

..

26 Commits

Author SHA1 Message Date
dswbx dedfb77884 removed durable mode as it requires an import from "cloudflare:" that often fails in non-cf environments 2025-08-12 14:16:22 +02:00
dswbx b5b4c5eef0 Merge pull request #231 from DimplesY/main
fix: add missing @clack/prompts dependency
2025-08-08 13:37:53 +02:00
Dimples_YJ 7af8e09468 fix: add missing @clack/prompts dependency 2025-08-08 13:51:12 +08:00
dswbx fd966b6aef Merge pull request #230 from stormbyte/main
Update Dockerfile to node 24
2025-08-06 20:01:54 +02:00
stormbyte 6bed2e4fca Update to node 24 2025-08-06 16:06:03 +02:00
dswbx dfb1457dfa Merge pull request #229 from DimplesY/main
docs: fix <Admin /> config props
2025-08-06 10:00:33 +02:00
Jie 4f945842a9 docs: update astro.mdx 2025-08-06 15:52:05 +08:00
Jie 44fdfcc8d9 docs: Correct the incorrect value of the config attribute in the admin component 2025-08-06 15:19:28 +08:00
dswbx 143d6a55ff updated lock to 0.16.1 2025-08-06 08:31:19 +02:00
dswbx 8fa905a8f1 docs: adding poster to assets (#228)
* docs: adding poster to assets

* readme: fix poster image
2025-08-06 08:29:46 +02:00
dswbx ad0d2e6ff8 add jsonv-ts as dependency for type inference, fix media api types (#227)
* add jsonv-ts as dependency for type inference, fix media api types

* add jsonv-ts as dependency for type inference, fix media api types

* add jsonv-ts as dependency for type inference, fix media api types
2025-08-06 08:21:17 +02:00
dswbx aa0e6f90d9 examples: fixing imports due to 0.16 (#226) 2025-08-06 08:19:29 +02:00
dswbx bd48bb7a18 v0.16 2025-08-01 16:03:23 +02:00
dswbx a298b65abf Release 0.16 (#196)
* initial refactor

* fixes

* test secrets extraction

* updated lock

* fix secret schema

* updated schemas, fixed tests, skipping flow tests for now

* added validator for rjsf, hook form via standard schema

* removed @sinclair/typebox

* remove unneeded vite dep

* fix jsonv literal on Field.tsx

* fix schema import path

* fix schema modals

* fix schema modals

* fix json field form, replaced auth form

* initial waku

* finalize waku example

* fix jsonv-ts version

* fix schema updates with falsy values

* fix media api to respect options' init, improve types

* checking media controller test

* checking media controller test

* checking media controller test

* clean up mediacontroller test

* added cookie option `partitioned`, as well as cors `origin` to be array, option to enable `credentials` (#214)

* added cookie option `partitioned`, as well as cors `origin` to be array, option to enable `credentials`

* fix server test

* fix data api (updated jsonv-ts)

* enhance cloudflare image optimization plugin with new options and explain endpoint (#215)

* feat: add ability to serve static by using dynamic imports (#197)

* feat: add ability to serve static by using dynamic imports

* serveStaticViaImport: make manifest optional

* serveStaticViaImport: add error log

* refactor/imports (#217)

* refactored core and core/utils imports

* refactored core and core/utils imports

* refactored media imports

* refactored auth imports

* refactored data imports

* updated package json exports, fixed mm config

* fix tests

* feat/deno (#219)

* update bun version

* fix module manager's em reference

* add basic deno example

* finalize

* docs: fumadocs migration (#185)

* feat(docs): initialize documentation structure with Fumadocs

* feat(docs): remove home route and move /docs route to /route

* feat(docs): add redirect to /start page

* feat(docs): migrate Getting Started chapters

* feat(docs): migrate Usage and Extending chapters

* feat(callout): add CalloutCaution, CalloutDanger, CalloutInfo, and CalloutPositive

* feat(layout): add Discord and GitHub links to documentation layout

* feat(docs): add integration chapters draft

* feat(docs): add modules chapters draft

* refactor(mdx-components): remove unused Icon import

* refactor(StackBlitz): enhance type safety by using unknown instead of any

* refactor(layout): update navigation mode to 'top' in layout configuration

* feat(docs): add @iconify/react package

* docs(mdx-components): add Icon component to MDX components list

* feat(docs): update Next.js integration guide

* feat(docs): update React Router integration guide

* feat(docs): update Astro integration guide

* feat(docs): update Vite integration guide

* fix(docs): update package manager initialization commands

* feat(docs): migrate Modules chapters

* chore(docs): update package.json with new devDependencies

* feat(docs): migrate Integration Runtimes chapters

* feat(docs): update Database usage chapter

* feat(docs): restructure documentation paths

* chore(docs): clean up unused imports and files in documentation

* style(layout): revert navigation mode to previous state

* fix(docs): routing for documentation structure

* feat(openapi): add API documentation generation from OpenAPI schema

* feat(docs): add icons to documentation pages

* chore(dependencies): remove unused content-collections packages

* fix(types): fix type error for attachFile in source.ts

* feat(redirects): update root redirect destination to '/start'

* feat(search): add static search functionality

* chore(dependencies): update fumadocs-core and fumadocs-ui to latest versions

* feat(search): add Powered by Orama link

* feat(generate-openapi): add error handling for missing OpenAPI schema

* feat(scripts): add OpenAPI generation to build process

* feat(config): enable dynamic redirects and rewrites in development mode

* feat(layout): add GitHub token support for improved API rate limits

* feat(redirects): add 301 redirects for cloudflare pages

* feat(docs): add Vercel redirects configuration

* feat(config): enable standalone output for development environment

* chore(layout): adjust layout settings

* refactor(package): clean up ajv dependency versions

* feat(docs): add twoslash support

* refactor(layout): update DocsLayout import and navigation configuration

* chore(layout): clean up layout.tsx by commenting out GithubInfo

* fix(Search): add locale to search initialization

* chore(package): update fumadocs and orama to latest versions

* docs: add menu items descriptions

* feat(layout): add GitHub URL to the layout component

* feat(docs): add AutoTypeTable component to MDX components

* feat(app): implement AutoTypeTable rendering for AppEvents type

* docs(layout): switch callouts back to default components

* fix(config): use __filename and __dirname for module paths

* docs: add note about node.js 22 requirement

* feat(styles): add custom color variables for light and dark themes

* docs: add S3 setup instructions for media module

* docs: fix typos and indentation in media module docs

* docs: add local media adapter example for Node.js

* docs(media): add S3/R2 URL format examples and fix typo

* docs: add cross-links to initial config and seeding sections

* indent numbered lists content, clarified media serve locations

* fix mediacontroller tests

* feat(layout): add AnimatedGridPattern component for dynamic background

* style(layout): configure fancy ToC style ('clerk')

* fix(AnimatedGridPattern): correct strokeDasharray type

* docs: actualize docs

* feat: add favicon

* style(cloudflare): format code examples

* feat(layout): add Github and Discord footer icons

* feat(footer): add SVG social media icons for GitHub and Discord

* docs: adjusted auto type table, added llm functions

* added static deployment to cloudflare workers

* docs: change cf redirects to proxy *.mdx instead of redirecting

---------

Co-authored-by: dswbx <dennis.senn@gmx.ch>
Co-authored-by: cameronapak <cameronandrewpak@gmail.com>

* build: improve build script

* add missing exports, fix EntityTypescript imports

* media: Dropzone: add programmatic upload, additional events, loading state

* schema object: disable extended defaults to allow empty config values

* Feat/new docs deploy (#224)

* test

* try fixing pm

* try fixing pm

* fix docs on imports, export events correctly

---------

Co-authored-by: Tim Seriakov <59409712+timseriakov@users.noreply.github.com>
Co-authored-by: cameronapak <cameronandrewpak@gmail.com>
2025-08-01 15:55:59 +02:00
dswbx daaaae82b6 Merge pull request #203 from bknd-io/fix/auth-redirect-and-adapter-config
fix auth redirect on error and adapter app fn
2025-07-19 09:15:23 +02:00
dswbx 79ace4f1e5 Merge pull request #206 from cameronapak/main
docs: add documentation on S3 integration
2025-07-19 09:13:20 +02:00
dswbx f19977ac28 fix mediacontroller tests 2025-07-19 09:12:28 +02:00
dswbx a3782728f9 indent numbered lists content, clarified media serve locations 2025-07-19 08:39:19 +02:00
cameronapak 8b832f02ac docs: add cross-links to initial config and seeding sections 2025-07-16 23:03:38 -05:00
cameronapak cec32ae881 docs(media): add S3/R2 URL format examples and fix typo 2025-07-16 22:57:15 -05:00
cameronapak 0fffa26d55 docs: add local media adapter example for Node.js 2025-07-16 22:47:09 -05:00
cameronapak 2b6f520f46 docs: fix typos and indentation in media module docs 2025-07-16 22:41:55 -05:00
cameronapak 7379bf2b1b docs: add S3 setup instructions for media module 2025-07-16 22:38:36 -05:00
dswbx db3c2cea10 docs: add note about node.js 22 requirement 2025-07-11 12:49:35 +02:00
dswbx 2b32a836cd auth error should not redirect to parameter, adapter app function should fire correctly 2025-07-09 08:00:52 +02:00
dswbx 957355c862 bump v0.15 2025-07-05 11:08:26 +02:00
449 changed files with 20967 additions and 12532 deletions
+1 -1
View File
@@ -20,7 +20,7 @@ jobs:
- name: Setup Bun
uses: oven-sh/setup-bun@v1
with:
bun-version: "1.2.14"
bun-version: "1.2.19"
- name: Install dependencies
working-directory: ./app
+3 -1
View File
@@ -31,4 +31,6 @@ packages/media/.env
.git_old
docker/tmp
.debug
.history
.history
.aider*
.vercel
+1 -1
View File
@@ -1,6 +1,6 @@
[![npm version](https://img.shields.io/npm/v/bknd.svg)](https://npmjs.org/package/bknd)
![bknd](https://raw.githubusercontent.com/bknd-io/bknd/refs/heads/main/docs/_assets/poster.png)
![bknd](https://raw.githubusercontent.com/bknd-io/bknd/refs/heads/main/docs/public/assets/poster.png)
<p align="center" width="100%">
<a href="https://stackblitz.com/github/bknd-io/bknd-examples?hideExplorer=1&embed=1&view=preview&startScript=example-admin-rich&initialPath=%2Fdata%2Fschema" target="_blank">
+2 -1
View File
@@ -1 +1,2 @@
tmp/*
tmp/*
!tmp/.gitkeep
+6 -6
View File
@@ -9,16 +9,16 @@ beforeAll(disableConsoleLog);
afterAll(enableConsoleLog);
describe("adapter", () => {
it("makes config", () => {
expect(omitKeys(adapter.makeConfig({}), ["connection"])).toEqual({});
expect(omitKeys(adapter.makeConfig({}, { env: { TEST: "test" } }), ["connection"])).toEqual(
{},
);
it("makes config", async () => {
expect(omitKeys(await adapter.makeConfig({}), ["connection"])).toEqual({});
expect(
omitKeys(await adapter.makeConfig({}, { env: { TEST: "test" } }), ["connection"]),
).toEqual({});
// merges everything returned from `app` with the config
expect(
omitKeys(
adapter.makeConfig(
await adapter.makeConfig(
{ app: (a) => ({ initialConfig: { server: { cors: { origin: a.env.TEST } } } }) },
{ env: { TEST: "test" } },
),
+3 -3
View File
@@ -1,12 +1,12 @@
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
import { Guard } from "../../src/auth";
import { parse } from "../../src/core/utils";
import { Guard } from "../../src/auth/authorize/Guard";
import { DataApi } from "../../src/data/api/DataApi";
import { DataController } from "../../src/data/api/DataController";
import { dataConfigSchema } from "../../src/data/data-schema";
import * as proto from "../../src/data/prototype";
import { schemaToEm } from "../helper";
import { disableConsoleLog, enableConsoleLog } from "core/utils/test";
import { parse } from "core/utils/schema";
beforeAll(disableConsoleLog);
afterAll(enableConsoleLog);
@@ -202,7 +202,7 @@ describe("DataApi", () => {
{
// create many
const res = await api.createMany("posts", payload);
expect(res.data.length).toEqual(4);
expect(res.data?.length).toEqual(4);
expect(res.ok).toBeTrue();
}
+37
View File
@@ -0,0 +1,37 @@
import { AppServer, serverConfigSchema } from "modules/server/AppServer";
import { describe, test, expect } from "bun:test";
describe("AppServer", () => {
test("config", () => {
{
const server = new AppServer();
expect(server).toBeDefined();
expect(server.config).toEqual({
cors: {
allow_credentials: true,
origin: "*",
allow_methods: ["GET", "POST", "PATCH", "PUT", "DELETE"],
allow_headers: ["Content-Type", "Content-Length", "Authorization", "Accept"],
},
});
}
{
const server = new AppServer({
cors: {
origin: "https",
allow_methods: ["GET", "POST"],
},
});
expect(server).toBeDefined();
expect(server.config).toEqual({
cors: {
allow_credentials: true,
origin: "https",
allow_methods: ["GET", "POST"],
allow_headers: ["Content-Type", "Content-Length", "Authorization", "Accept"],
},
});
}
});
});
+1 -44
View File
@@ -1,46 +1,3 @@
import { describe, expect, test } from "bun:test";
import { Authenticator, type User, type UserPool } from "../../src/auth";
import { cookieConfig } from "../../src/auth/authenticate/Authenticator";
import { PasswordStrategy } from "../../src/auth/authenticate/strategies/PasswordStrategy";
import * as hash from "../../src/auth/utils/hash";
import { Default, parse } from "../../src/core/utils";
/*class MemoryUserPool implements UserPool {
constructor(private users: User[] = []) {}
async findBy(prop: "id" | "email" | "username", value: string | number) {
return this.users.find((user) => user[prop] === value);
}
async create(user: Pick<User, "email" | "password">) {
const id = this.users.length + 1;
const newUser = { ...user, id, username: user.email };
this.users.push(newUser);
return newUser;
}
}*/
describe("Authenticator", async () => {
test("cookie options", async () => {
console.log("parsed", parse(cookieConfig, undefined));
console.log(Default(cookieConfig, {}));
});
/*const userpool = new MemoryUserPool([
{ id: 1, email: "d", username: "test", password: await hash.sha256("test") },
]);
test("sha256 login", async () => {
const auth = new Authenticator(userpool, {
password: new PasswordStrategy({
hashing: "sha256",
}),
});
const { token } = await auth.login("password", { email: "d", password: "test" });
expect(token).toBeDefined();
const { iat, ...decoded } = decodeJwt<any>(token);
expect(decoded).toEqual({ id: 1, email: "d", username: "test" });
expect(await auth.verify(token)).toBe(true);
});*/
});
describe("Authenticator", async () => {});
@@ -1,5 +1,5 @@
import { describe, expect, test } from "bun:test";
import { Guard } from "../../../src/auth";
import { Guard } from "../../../src/auth/authorize/Guard";
describe("authorize", () => {
test("basic", async () => {
+11 -9
View File
@@ -1,6 +1,6 @@
import { describe, expect, test } from "bun:test";
import { type TObject, type TString, Type } from "@sinclair/typebox";
import { Registry } from "core";
import { Registry } from "core/registry/Registry";
import { s } from "core/utils/schema";
type Constructor<T> = new (...args: any[]) => T;
@@ -11,7 +11,7 @@ class What {
return null;
}
getType() {
return Type.Object({ type: Type.String() });
return s.object({ type: s.string() });
}
}
class What2 extends What {}
@@ -19,7 +19,7 @@ class NotAllowed {}
type Test1 = {
cls: new (...args: any[]) => What;
schema: TObject<{ type: TString }>;
schema: s.ObjectSchema<{ type: s.StringSchema }>;
enabled: boolean;
};
@@ -28,7 +28,7 @@ describe("Registry", () => {
const registry = new Registry<Test1>().set({
first: {
cls: What,
schema: Type.Object({ type: Type.String(), what: Type.String() }),
schema: s.object({ type: s.string(), what: s.string() }),
enabled: true,
},
} satisfies Record<string, Test1>);
@@ -37,7 +37,7 @@ describe("Registry", () => {
expect(item).toBeDefined();
expect(item?.cls).toBe(What);
const second = Type.Object({ type: Type.String(), what: Type.String() });
const second = s.object({ type: s.string(), what: s.string() });
registry.add("second", {
cls: What2,
schema: second,
@@ -46,7 +46,7 @@ describe("Registry", () => {
// @ts-ignore
expect(registry.get("second").schema).toEqual(second);
const third = Type.Object({ type: Type.String({ default: "1" }), what22: Type.String() });
const third = s.object({ type: s.string({ default: "1" }), what22: s.string() });
registry.add("third", {
// @ts-expect-error
cls: NotAllowed,
@@ -56,7 +56,7 @@ describe("Registry", () => {
// @ts-ignore
expect(registry.get("third").schema).toEqual(third);
const fourth = Type.Object({ type: Type.Number(), what22: Type.String() });
const fourth = s.object({ type: s.number(), what22: s.string() });
registry.add("fourth", {
cls: What,
// @ts-expect-error
@@ -81,6 +81,8 @@ describe("Registry", () => {
registry.register("what2", What2);
expect(registry.get("what2")).toBeDefined();
expect(registry.get("what2").cls).toBe(What2);
expect(registry.get("what2").schema).toEqual(What2.prototype.getType());
expect(JSON.stringify(registry.get("what2").schema)).toEqual(
JSON.stringify(What2.prototype.getType()),
);
});
});
+46 -32
View File
@@ -1,11 +1,11 @@
import { describe, expect, test } from "bun:test";
import { SchemaObject } from "../../../src/core";
import { Type } from "@sinclair/typebox";
import { s } from "core/utils/schema";
import { SchemaObject } from "core/object/SchemaObject";
describe("SchemaObject", async () => {
test("basic", async () => {
const m = new SchemaObject(
Type.Object({ a: Type.String({ default: "b" }) }),
s.strictObject({ a: s.string({ default: "b" }) }),
{ a: "test" },
{
forceParse: true,
@@ -23,19 +23,19 @@ describe("SchemaObject", async () => {
test("patch", async () => {
const m = new SchemaObject(
Type.Object({
s: Type.Object(
s.strictObject({
s: s.strictObject(
{
a: Type.String({ default: "b" }),
b: Type.Object(
a: s.string({ default: "b" }),
b: s.strictObject(
{
c: Type.String({ default: "d" }),
e: Type.String({ default: "f" }),
c: s.string({ default: "d" }),
e: s.string({ default: "f" }),
},
{ default: {} },
),
},
{ default: {}, additionalProperties: false },
{ default: {} },
),
}),
);
@@ -44,7 +44,7 @@ describe("SchemaObject", async () => {
await m.patch("s.a", "c");
// non-existing path on no additional properties
expect(() => m.patch("s.s.s", "c")).toThrow();
expect(m.patch("s.s.s", "c")).rejects.toThrow();
// wrong type
expect(() => m.patch("s.a", 1)).toThrow();
@@ -58,8 +58,8 @@ describe("SchemaObject", async () => {
test("patch array", async () => {
const m = new SchemaObject(
Type.Object({
methods: Type.Array(Type.String(), { default: ["GET", "PATCH"] }),
s.strictObject({
methods: s.array(s.string(), { default: ["GET", "PATCH"] }),
}),
);
expect(m.get()).toEqual({ methods: ["GET", "PATCH"] });
@@ -75,13 +75,13 @@ describe("SchemaObject", async () => {
test("remove", async () => {
const m = new SchemaObject(
Type.Object({
s: Type.Object(
s.object({
s: s.object(
{
a: Type.String({ default: "b" }),
b: Type.Object(
a: s.string({ default: "b" }),
b: s.object(
{
c: Type.String({ default: "d" }),
c: s.string({ default: "d" }),
},
{ default: {} },
),
@@ -107,8 +107,8 @@ describe("SchemaObject", async () => {
test("set", async () => {
const m = new SchemaObject(
Type.Object({
methods: Type.Array(Type.String(), { default: ["GET", "PATCH"] }),
s.strictObject({
methods: s.array(s.string(), { default: ["GET", "PATCH"] }),
}),
);
expect(m.get()).toEqual({ methods: ["GET", "PATCH"] });
@@ -124,8 +124,8 @@ describe("SchemaObject", async () => {
let called = false;
let result: any;
const m = new SchemaObject(
Type.Object({
methods: Type.Array(Type.String(), { default: ["GET", "PATCH"] }),
s.strictObject({
methods: s.array(s.string(), { default: ["GET", "PATCH"] }),
}),
undefined,
{
@@ -145,8 +145,8 @@ describe("SchemaObject", async () => {
test("listener: onBeforeUpdate", async () => {
let called = false;
const m = new SchemaObject(
Type.Object({
methods: Type.Array(Type.String(), { default: ["GET", "PATCH"] }),
s.strictObject({
methods: s.array(s.string(), { default: ["GET", "PATCH"] }),
}),
undefined,
{
@@ -167,7 +167,7 @@ describe("SchemaObject", async () => {
});
test("throwIfRestricted", async () => {
const m = new SchemaObject(Type.Object({}), undefined, {
const m = new SchemaObject(s.strictObject({}), undefined, {
restrictPaths: ["a.b"],
});
@@ -179,13 +179,13 @@ describe("SchemaObject", async () => {
test("restriction bypass", async () => {
const m = new SchemaObject(
Type.Object({
s: Type.Object(
s.strictObject({
s: s.strictObject(
{
a: Type.String({ default: "b" }),
b: Type.Object(
a: s.string({ default: "b" }),
b: s.strictObject(
{
c: Type.String({ default: "d" }),
c: s.string({ default: "d" }),
},
{ default: {} },
),
@@ -205,7 +205,21 @@ describe("SchemaObject", async () => {
expect(m.get()).toEqual({ s: { a: "b", b: { c: "e" } } });
});
const dataEntitiesSchema = Type.Object(
const dataEntitiesSchema = s.strictObject({
entities: s.record(
s.object({
fields: s.record(
s.object({
type: s.string(),
config: s.object({}).optional(),
}),
),
config: s.record(s.string()).optional(),
}),
),
});
/* const dataEntitiesSchema = Type.Object(
{
entities: Type.Object(
{},
@@ -230,7 +244,7 @@ describe("SchemaObject", async () => {
{
additionalProperties: false,
},
);
); */
test("patch safe object, overwrite", async () => {
const data = {
entities: {
+6 -9
View File
@@ -1,19 +1,16 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { Guard } from "../../src/auth";
import { parse } from "../../src/core/utils";
import {
Entity,
type EntityData,
EntityManager,
ManyToOneRelation,
TextField,
} from "../../src/data";
import { Guard } from "../../src/auth/authorize/Guard";
import { parse } from "core/utils/schema";
import { DataController } from "../../src/data/api/DataController";
import { dataConfigSchema } from "../../src/data/data-schema";
import { disableConsoleLog, enableConsoleLog, getDummyConnection } from "../helper";
import type { RepositoryResultJSON } from "data/entities/query/RepositoryResult";
import type { MutatorResultJSON } from "data/entities/mutation/MutatorResult";
import { Entity, EntityManager, type EntityData } from "data/entities";
import { TextField } from "data/fields";
import { ManyToOneRelation } from "data/relations";
const { dummyConnection, afterAllCleanup } = getDummyConnection();
beforeAll(() => disableConsoleLog(["log", "warn"]));
+2 -8
View File
@@ -1,12 +1,6 @@
import { afterAll, describe, expect, test } from "bun:test";
import {
Entity,
EntityManager,
NumberField,
PrimaryField,
Repository,
TextField,
} from "../../src/data";
import { Entity, EntityManager } from "data/entities";
import { TextField, PrimaryField, NumberField } from "data/fields";
import { getDummyConnection } from "./helper";
const { dummyConnection, afterAllCleanup } = getDummyConnection();
+1 -1
View File
@@ -2,7 +2,7 @@ import { unlink } from "node:fs/promises";
import type { SqliteDatabase } from "kysely";
// @ts-ignore
import Database from "libsql";
import { SqliteLocalConnection } from "../../src/data";
import { SqliteLocalConnection } from "data/connection/sqlite/SqliteLocalConnection";
export function getDummyDatabase(memory: boolean = true): {
dummyDb: SqliteDatabase;
+5 -8
View File
@@ -1,13 +1,10 @@
// eslint-disable-next-line import/no-unresolved
import { afterAll, describe, expect, test } from "bun:test";
import {
Entity,
EntityManager,
ManyToOneRelation,
NumberField,
SchemaManager,
TextField,
} from "../../src/data";
import { Entity } from "data/entities";
import { EntityManager } from "data/entities/EntityManager";
import { ManyToOneRelation } from "data/relations";
import { NumberField, TextField } from "data/fields";
import { SchemaManager } from "data/schema/SchemaManager";
import { getDummyConnection } from "./helper";
const { dummyConnection, afterAllCleanup } = getDummyConnection();
+3 -2
View File
@@ -1,7 +1,8 @@
// eslint-disable-next-line import/no-unresolved
import { afterAll, describe, expect, test } from "bun:test";
import { Entity, EntityManager, Mutator, NumberField, TextField } from "../../src/data";
import { TransformPersistFailedException } from "../../src/data/errors";
import { Entity, EntityManager } from "data/entities";
import { NumberField, TextField } from "data/fields";
import { TransformPersistFailedException } from "data/errors";
import { getDummyConnection } from "./helper";
const { dummyConnection, afterAllCleanup } = getDummyConnection();
+4 -2
View File
@@ -1,6 +1,8 @@
import { afterAll, expect as bunExpect, describe, test } from "bun:test";
import { stripMark } from "../../src/core/utils";
import { Entity, EntityManager, PolymorphicRelation, TextField } from "../../src/data";
import { stripMark } from "core/utils/schema";
import { Entity, EntityManager } from "data/entities";
import { TextField } from "data/fields";
import { PolymorphicRelation } from "data/relations";
import { getDummyConnection } from "./helper";
const { dummyConnection, afterAllCleanup } = getDummyConnection();
+14 -12
View File
@@ -2,19 +2,20 @@ import { describe, expect, test } from "bun:test";
import {
BooleanField,
DateField,
Entity,
EntityIndex,
EntityManager,
EnumField,
JsonField,
NumberField,
TextField,
EntityIndex,
} from "data/fields";
import { Entity, EntityManager } from "data/entities";
import {
ManyToManyRelation,
ManyToOneRelation,
NumberField,
OneToOneRelation,
PolymorphicRelation,
TextField,
} from "../../src/data";
import { DummyConnection } from "../../src/data/connection/DummyConnection";
} from "data/relations";
import { DummyConnection } from "data/connection/DummyConnection";
import {
FieldPrototype,
type FieldSchema,
@@ -32,8 +33,8 @@ import {
number,
relation,
text,
} from "../../src/data/prototype";
import { MediaField } from "../../src/media/MediaField";
} from "data/prototype";
import { MediaField } from "media/MediaField";
describe("prototype", () => {
test("...", () => {
@@ -101,7 +102,8 @@ describe("prototype", () => {
type Posts = Schema<typeof posts2>;
expect(posts1.toJSON()).toEqual(posts2.toJSON());
// @todo: check
//expect(posts1.toJSON()).toEqual(posts2.toJSON());
});
test("test example", async () => {
@@ -295,9 +297,9 @@ describe("prototype", () => {
new Entity("posts", [new TextField("name"), new TextField("slug", { required: true })]),
new Entity("comments", [new TextField("some")]),
new Entity("users", [new TextField("email")]),
];
] as const;
const _em2 = new EntityManager(
es,
[...es],
new DummyConnection(),
[new ManyToOneRelation(es[0], es[1]), new ManyToOneRelation(es[0], es[2])],
[
+11 -10
View File
@@ -1,13 +1,14 @@
// eslint-disable-next-line import/no-unresolved
import { afterAll, describe, expect, test } from "bun:test";
import { Entity, EntityManager, TextField } from "../../src/data";
import { Entity, EntityManager } from "data/entities";
import { TextField } from "data/fields";
import {
ManyToManyRelation,
ManyToOneRelation,
OneToOneRelation,
PolymorphicRelation,
RelationField,
} from "../../src/data/relations";
} from "data/relations";
import { getDummyConnection } from "./helper";
const { dummyConnection, afterAllCleanup } = getDummyConnection();
@@ -77,7 +78,7 @@ describe("Relations", async () => {
const em = new EntityManager(entities, dummyConnection, relations);
// verify naming
const rel = em.relations.all[0];
const rel = em.relations.all[0]!;
expect(rel.source.entity.name).toBe(posts.name);
expect(rel.source.reference).toBe(posts.name);
expect(rel.target.entity.name).toBe(users.name);
@@ -89,11 +90,11 @@ describe("Relations", async () => {
// verify low level relation
expect(em.relationsOf(users.name).length).toBe(1);
expect(em.relationsOf(users.name).length).toBe(1);
expect(em.relationsOf(users.name)[0].source.entity).toBe(posts);
expect(em.relationsOf(users.name)[0]!.source.entity).toBe(posts);
expect(posts.field("author_id")).toBeInstanceOf(RelationField);
expect(em.relationsOf(users.name).length).toBe(1);
expect(em.relationsOf(users.name).length).toBe(1);
expect(em.relationsOf(users.name)[0].source.entity).toBe(posts);
expect(em.relationsOf(users.name)[0]!.source.entity).toBe(posts);
// verify high level relation (from users)
const userPostsRel = em.relationOf(users.name, "posts");
@@ -191,7 +192,7 @@ describe("Relations", async () => {
const em = new EntityManager(entities, dummyConnection, relations);
// verify naming
const rel = em.relations.all[0];
const rel = em.relations.all[0]!;
expect(rel.source.entity.name).toBe(users.name);
expect(rel.source.reference).toBe(users.name);
expect(rel.target.entity.name).toBe(settings.name);
@@ -202,8 +203,8 @@ describe("Relations", async () => {
expect(em.relationsOf(users.name).length).toBe(1);
expect(em.relationsOf(users.name).length).toBe(1);
expect(em.relationsOf(users.name)[0].source.entity).toBe(users);
expect(em.relationsOf(users.name)[0].target.entity).toBe(settings);
expect(em.relationsOf(users.name)[0]!.source.entity).toBe(users);
expect(em.relationsOf(users.name)[0]!.target.entity).toBe(settings);
// verify high level relation (from users)
const userSettingRel = em.relationOf(users.name, settings.name);
@@ -323,7 +324,7 @@ describe("Relations", async () => {
);
// mutation info
expect(relations[0].helper(posts.name)!.getMutationInfo()).toEqual({
expect(relations[0]!.helper(posts.name)!.getMutationInfo()).toEqual({
reference: "categories",
local_field: undefined,
$set: false,
@@ -334,7 +335,7 @@ describe("Relations", async () => {
cardinality: undefined,
relation_type: "m:n",
});
expect(relations[0].helper(categories.name)!.getMutationInfo()).toEqual({
expect(relations[0]!.helper(categories.name)!.getMutationInfo()).toEqual({
reference: "posts",
local_field: undefined,
$set: false,
+2 -2
View File
@@ -1,6 +1,6 @@
import { describe, expect, test } from "bun:test";
import { Entity, NumberField, TextField } from "data";
import * as p from "data/prototype";
import { Entity } from "data/entities";
import { NumberField, TextField } from "data/fields";
describe("[data] Entity", async () => {
const entity = new Entity("test", [
@@ -1,12 +1,8 @@
import { afterAll, describe, expect, test } from "bun:test";
import {
Entity,
EntityManager,
ManyToManyRelation,
ManyToOneRelation,
SchemaManager,
} from "../../../src/data";
import { UnableToConnectException } from "../../../src/data/errors";
import { Entity, EntityManager } from "data/entities";
import { ManyToManyRelation, ManyToOneRelation } from "data/relations";
import { SchemaManager } from "data/schema/SchemaManager";
import { UnableToConnectException } from "data/errors";
import { getDummyConnection } from "../helper";
const { dummyConnection, afterAllCleanup } = getDummyConnection();
+4 -2
View File
@@ -1,6 +1,8 @@
import { afterAll, describe, expect, test } from "bun:test";
import { Entity, EntityManager, ManyToOneRelation, TextField } from "../../../src/data";
import { JoinBuilder } from "../../../src/data/entities/query/JoinBuilder";
import { Entity, EntityManager } from "data/entities";
import { ManyToOneRelation } from "data/relations";
import { TextField } from "data/fields";
import { JoinBuilder } from "data/entities/query/JoinBuilder";
import { getDummyConnection } from "../helper";
const { dummyConnection, afterAllCleanup } = getDummyConnection();
+6 -8
View File
@@ -1,18 +1,16 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import type { EventManager } from "../../../src/core/events";
import { Entity, EntityManager } from "data/entities";
import {
Entity,
EntityManager,
ManyToOneRelation,
MutatorEvents,
NumberField,
OneToOneRelation,
type RelationField,
RelationField,
RelationMutator,
TextField,
} from "../../../src/data";
import * as proto from "../../../src/data/prototype";
} from "data/relations";
import { NumberField, TextField } from "data/fields";
import * as proto from "data/prototype";
import { getDummyConnection, disableConsoleLog, enableConsoleLog } from "../../helper";
import { MutatorEvents } from "data/events";
const { dummyConnection, afterAllCleanup } = getDummyConnection();
afterAll(afterAllCleanup);
+5 -12
View File
@@ -1,17 +1,10 @@
import { afterAll, describe, expect, test } from "bun:test";
import type { Kysely, Transaction } from "kysely";
import { Perf } from "core/utils";
import {
Entity,
EntityManager,
LibsqlConnection,
ManyToOneRelation,
RepositoryEvents,
TextField,
entity as $entity,
text as $text,
em as $em,
} from "data";
import { TextField } from "data/fields";
import { em as $em, entity as $entity, text as $text } from "data/prototype";
import { Entity, EntityManager } from "data/entities";
import { ManyToOneRelation } from "data/relations";
import { RepositoryEvents } from "data/events";
import { getDummyConnection } from "../helper";
type E = Kysely<any> | Transaction<any>;
@@ -1,7 +1,9 @@
// eslint-disable-next-line import/no-unresolved
import { afterAll, describe, expect, test } from "bun:test";
import { randomString } from "../../../src/core/utils";
import { Entity, EntityIndex, EntityManager, SchemaManager, TextField } from "../../../src/data";
import { randomString } from "core/utils";
import { Entity, EntityManager } from "data/entities";
import { TextField, EntityIndex } from "data/fields";
import { SchemaManager } from "data/schema/SchemaManager";
import { getDummyConnection } from "../helper";
const { dummyConnection, afterAllCleanup } = getDummyConnection();
+1 -1
View File
@@ -1,6 +1,6 @@
import { describe, test, expect } from "bun:test";
import { getDummyConnection } from "../helper";
import { type WhereQuery, WhereBuilder } from "data";
import { WhereBuilder, type WhereQuery } from "data/entities/query/WhereBuilder";
function qb() {
const c = getDummyConnection();
+5 -10
View File
@@ -1,14 +1,9 @@
import { describe, expect, test } from "bun:test";
import {
Entity,
EntityManager,
ManyToManyRelation,
ManyToOneRelation,
PolymorphicRelation,
TextField,
WithBuilder,
} from "../../../src/data";
import * as proto from "../../../src/data/prototype";
import { Entity, EntityManager } from "data/entities";
import { ManyToManyRelation, ManyToOneRelation, PolymorphicRelation } from "data/relations";
import { TextField } from "data/fields";
import * as proto from "data/prototype";
import { WithBuilder } from "data/entities/query/WithBuilder";
import { schemaToEm } from "../../helper";
import { getDummyConnection } from "../helper";
@@ -1,5 +1,5 @@
import { afterAll, describe, expect, test } from "bun:test";
import { EntityManager } from "../../../../src/data";
import { EntityManager } from "data/entities/EntityManager";
import { getDummyConnection } from "../../helper";
const { dummyConnection, afterAllCleanup } = getDummyConnection();
@@ -1,9 +1,10 @@
import { bunTestRunner } from "adapter/bun/test";
import { describe, expect, test } from "bun:test";
import { BooleanField } from "../../../../src/data";
import { BooleanField } from "data/fields";
import { fieldTestSuite, transformPersist } from "data/fields/field-test-suite";
describe("[data] BooleanField", async () => {
fieldTestSuite({ expect, test }, BooleanField, { defaultValue: true, schemaType: "boolean" });
fieldTestSuite(bunTestRunner, BooleanField, { defaultValue: true, schemaType: "boolean" });
test("transformRetrieve", async () => {
const field = new BooleanField("test");
@@ -1,9 +1,15 @@
import { describe, expect, test } from "bun:test";
import { DateField } from "../../../../src/data";
import { describe, test } from "bun:test";
import { DateField } from "data/fields";
import { fieldTestSuite } from "data/fields/field-test-suite";
import { bunTestRunner } from "adapter/bun/test";
describe("[data] DateField", async () => {
fieldTestSuite({ expect, test }, DateField, { defaultValue: new Date(), schemaType: "date" });
fieldTestSuite(
bunTestRunner,
DateField,
{ defaultValue: new Date(), schemaType: "date" },
{ type: "date" },
);
// @todo: add datefield tests
test("week", async () => {
@@ -1,5 +1,6 @@
import { bunTestRunner } from "adapter/bun/test";
import { describe, expect, test } from "bun:test";
import { EnumField } from "../../../../src/data";
import { EnumField } from "data/fields";
import { fieldTestSuite, transformPersist } from "data/fields/field-test-suite";
function options(strings: string[]) {
@@ -8,7 +9,7 @@ function options(strings: string[]) {
describe("[data] EnumField", async () => {
fieldTestSuite(
{ expect, test },
bunTestRunner,
// @ts-ignore
EnumField,
{ defaultValue: "a", schemaType: "text" },
+4 -3
View File
@@ -1,7 +1,8 @@
import { describe, expect, test } from "bun:test";
import { Default, stripMark } from "../../../../src/core/utils";
import { baseFieldConfigSchema, Field } from "../../../../src/data/fields/Field";
import { fieldTestSuite } from "data/fields/field-test-suite";
import { bunTestRunner } from "adapter/bun/test";
import { stripMark } from "core/utils/schema";
describe("[data] Field", async () => {
class FieldSpec extends Field {
@@ -19,10 +20,10 @@ describe("[data] Field", async () => {
});
});
fieldTestSuite({ expect, test }, FieldSpec, { defaultValue: "test", schemaType: "text" });
fieldTestSuite(bunTestRunner, FieldSpec, { defaultValue: "test", schemaType: "text" });
test("default config", async () => {
const config = Default(baseFieldConfigSchema, {});
const config = baseFieldConfigSchema.template({});
expect(stripMark(new FieldSpec("test").config)).toEqual(config as any);
});
@@ -1,10 +1,11 @@
import { describe, expect, test } from "bun:test";
import { Type } from "@sinclair/typebox";
import { Entity, EntityIndex, Field } from "../../../../src/data";
import { Entity } from "data/entities";
import { Field, EntityIndex } from "data/fields";
import { s } from "core/utils/schema";
class TestField extends Field {
protected getSchema(): any {
return Type.Any();
return s.any();
}
override schema() {
@@ -1,10 +1,11 @@
import { bunTestRunner } from "adapter/bun/test";
import { describe, expect, test } from "bun:test";
import { JsonField } from "../../../../src/data";
import { JsonField } from "data/fields";
import { fieldTestSuite, transformPersist } from "data/fields/field-test-suite";
describe("[data] JsonField", async () => {
const field = new JsonField("test");
fieldTestSuite({ expect, test }, JsonField, {
fieldTestSuite(bunTestRunner, JsonField, {
defaultValue: { a: 1 },
sampleValues: ["string", { test: 1 }, 1],
schemaType: "text",
@@ -1,5 +1,5 @@
import { describe, expect, test } from "bun:test";
import { JsonSchemaField } from "../../../../src/data";
import { JsonSchemaField } from "data/fields";
import { fieldTestSuite } from "data/fields/field-test-suite";
describe("[data] JsonSchemaField", async () => {
@@ -1,5 +1,6 @@
import { bunTestRunner } from "adapter/bun/test";
import { describe, expect, test } from "bun:test";
import { NumberField } from "../../../../src/data";
import { NumberField } from "data/fields";
import { fieldTestSuite, transformPersist } from "data/fields/field-test-suite";
describe("[data] NumberField", async () => {
@@ -15,5 +16,5 @@ describe("[data] NumberField", async () => {
expect(transformPersist(field2, 10000)).resolves.toBe(10000);
});
fieldTestSuite({ expect, test }, NumberField, { defaultValue: 12, schemaType: "integer" });
fieldTestSuite(bunTestRunner, NumberField, { defaultValue: 12, schemaType: "integer" });
});
@@ -1,5 +1,5 @@
import { describe, expect, test } from "bun:test";
import { PrimaryField } from "../../../../src/data";
import { PrimaryField } from "data/fields";
describe("[data] PrimaryField", async () => {
const field = new PrimaryField("primary");
@@ -1,6 +1,7 @@
import { describe, expect, test } from "bun:test";
import { TextField } from "../../../../src/data";
import { TextField } from "data/fields";
import { fieldTestSuite, transformPersist } from "data/fields/field-test-suite";
import { bunTestRunner } from "adapter/bun/test";
describe("[data] TextField", async () => {
test("transformPersist (config)", async () => {
@@ -11,5 +12,5 @@ describe("[data] TextField", async () => {
expect(transformPersist(field, "abc")).resolves.toBe("abc");
});
fieldTestSuite({ expect, test }, TextField, { defaultValue: "abc", schemaType: "text" });
fieldTestSuite(bunTestRunner, TextField, { defaultValue: "abc", schemaType: "text" });
});
@@ -1,11 +1,11 @@
import { describe, expect, it, test } from "bun:test";
import { Entity, type EntityManager } from "../../../../src/data";
import { Entity, type EntityManager } from "data/entities";
import {
type BaseRelationConfig,
EntityRelation,
EntityRelationAnchor,
RelationTypes,
} from "../../../../src/data/relations";
} from "data/relations";
class TestEntityRelation extends EntityRelation {
constructor(config?: BaseRelationConfig) {
@@ -24,11 +24,11 @@ class TestEntityRelation extends EntityRelation {
return this;
}
buildWith(a: any, b: any, c: any): any {
buildWith(): any {
return;
}
buildJoin(a: any, b: any): any {
buildJoin(): any {
return;
}
}
+1 -1
View File
@@ -41,7 +41,7 @@ beforeAll(() =>
);
afterAll(unmockFetch);
describe("FetchTask", async () => {
describe.skip("FetchTask", async () => {
test("Simple fetch", async () => {
const task = new FetchTask("Fetch Something", {
url: "https://jsonplaceholder.typicode.com/todos/1",
+5 -7
View File
@@ -1,6 +1,6 @@
import { describe, expect, test } from "bun:test";
import { Flow, LogTask, SubFlowTask, RenderTask, Task } from "../../src/flows";
import { Type } from "@sinclair/typebox";
import { s } from "core/utils/schema";
export class StringifyTask<Output extends string> extends Task<
typeof StringifyTask.schema,
@@ -8,18 +8,16 @@ export class StringifyTask<Output extends string> extends Task<
> {
type = "stringify";
static override schema = Type.Optional(
Type.Object({
input: Type.Optional(Type.String()),
}),
);
static override schema = s.object({
input: s.string().optional(),
});
async execute() {
return JSON.stringify(this.params.input) as Output;
}
}
describe("SubFlowTask", async () => {
describe.skip("SubFlowTask", async () => {
test("Simple Subflow", async () => {
const subTask = new RenderTask("render", {
render: "subflow",
+5 -5
View File
@@ -1,12 +1,12 @@
import { describe, expect, test } from "bun:test";
import { Type } from "@sinclair/typebox";
import { Task } from "../../src/flows";
import { dynamic } from "../../src/flows/tasks/Task";
import { s } from "core/utils/schema";
describe("Task", async () => {
describe.skip("Task", async () => {
test("resolveParams: template with parse", async () => {
const result = await Task.resolveParams(
Type.Object({ test: dynamic(Type.Number()) }),
s.object({ test: dynamic(s.number()) }),
{
test: "{{ some.path }}",
},
@@ -22,7 +22,7 @@ describe("Task", async () => {
test("resolveParams: with string", async () => {
const result = await Task.resolveParams(
Type.Object({ test: Type.String() }),
s.object({ test: s.string() }),
{
test: "{{ some.path }}",
},
@@ -38,7 +38,7 @@ describe("Task", async () => {
test("resolveParams: with object", async () => {
const result = await Task.resolveParams(
Type.Object({ test: dynamic(Type.Object({ key: Type.String(), value: Type.String() })) }),
s.object({ test: dynamic(s.object({ key: s.string(), value: s.string() })) }),
{
test: { key: "path", value: "{{ some.path }}" },
},
+6 -7
View File
@@ -1,8 +1,7 @@
import { describe, expect, test } from "bun:test";
import { Hono } from "hono";
import { Event, EventManager } from "../../src/core/events";
import { parse } from "../../src/core/utils";
import { type Static, type StaticDecode, Type } from "@sinclair/typebox";
import { s, parse } from "core/utils/schema";
import { EventTrigger, Flow, HttpTrigger, type InputsMap, Task } from "../../src/flows";
import { dynamic } from "../../src/flows/tasks/Task";
@@ -15,15 +14,15 @@ class Passthrough extends Task {
}
}
type OutputIn = Static<typeof OutputParamTask.schema>;
type OutputOut = StaticDecode<typeof OutputParamTask.schema>;
type OutputIn = s.Static<typeof OutputParamTask.schema>;
type OutputOut = s.StaticCoerced<typeof OutputParamTask.schema>;
class OutputParamTask extends Task<typeof OutputParamTask.schema> {
type = "output-param";
static override schema = Type.Object({
static override schema = s.strictObject({
number: dynamic(
Type.Number({
s.number({
title: "Output number",
}),
Number.parseInt,
@@ -44,7 +43,7 @@ class PassthroughFlowInput extends Task {
}
}
describe("Flow task inputs", async () => {
describe.skip("Flow task inputs", async () => {
test("types", async () => {
const schema = OutputParamTask.schema;
+1 -1
View File
@@ -30,7 +30,7 @@ class ExecTask extends Task {
}
}
describe("Flow trigger", async () => {
describe.skip("Flow trigger", async () => {
test("manual trigger", async () => {
let called = false;
+6 -6
View File
@@ -2,7 +2,7 @@
import { describe, expect, test } from "bun:test";
import { isEqual } from "lodash-es";
import { _jsonp, withDisabledConsole } from "../../src/core/utils";
import { type Static, Type } from "@sinclair/typebox";
import { s } from "core/utils/schema";
import { Condition, ExecutionEvent, FetchTask, Flow, LogTask, Task } from "../../src/flows";
/*beforeAll(disableConsoleLog);
@@ -11,19 +11,19 @@ afterAll(enableConsoleLog);*/
class ExecTask extends Task<typeof ExecTask.schema> {
type = "exec";
static override schema = Type.Object({
delay: Type.Number({ default: 10 }),
static override schema = s.object({
delay: s.number({ default: 10 }),
});
constructor(
name: string,
params: Static<typeof ExecTask.schema>,
params: s.Static<typeof ExecTask.schema>,
private func: () => Promise<any>,
) {
super(name, params);
}
override clone(name: string, params: Static<typeof ExecTask.schema>) {
override clone(name: string, params: s.Static<typeof ExecTask.schema>) {
return new ExecTask(name, params, this.func);
}
@@ -78,7 +78,7 @@ function getObjectDiff(obj1, obj2) {
return diff;
}
describe("Flow tests", async () => {
describe.skip("Flow tests", async () => {
test("Simple single task", async () => {
const simple = getTask(0);
+2 -1
View File
@@ -2,11 +2,12 @@ import { unlink } from "node:fs/promises";
import type { SelectQueryBuilder, SqliteDatabase } from "kysely";
import Database from "libsql";
import { format as sqlFormat } from "sql-formatter";
import { type Connection, EntityManager, SqliteLocalConnection } from "../src/data";
import type { em as protoEm } from "../src/data/prototype";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { slugify } from "core/utils/strings";
import { type Connection, SqliteLocalConnection } from "data/connection";
import { EntityManager } from "data/entities/EntityManager";
export function getDummyDatabase(memory: boolean = true): {
dummyDb: SqliteDatabase;
@@ -13,9 +13,8 @@ describe("integration config", () => {
// create entity
await api.system.addConfig("data", "entities.posts", {
name: "posts",
config: { sort_field: "id", sort_dir: "asc" },
fields: { id: { type: "primary", name: "id" }, asdf: { type: "text" } },
fields: { id: { type: "primary" }, asdf: { type: "text" } },
type: "regular",
});
@@ -46,7 +46,6 @@ afterAll(enableConsoleLog);
describe("MediaController", () => {
test("accepts direct", async () => {
const app = await makeApp();
console.log("app", app);
const file = Bun.file(path);
const name = makeName("png");
@@ -55,7 +54,6 @@ describe("MediaController", () => {
body: file,
});
const result = (await res.json()) as any;
console.log(result);
expect(result.name).toBe(name);
const destFile = Bun.file(assetsTmpPath + "/" + name);
+1 -1
View File
@@ -1,7 +1,7 @@
import { describe, expect, test } from "bun:test";
import { type FileBody, Storage } from "../../src/media/storage/Storage";
import * as StorageEvents from "../../src/media/storage/events";
import { StorageAdapter } from "media";
import { StorageAdapter } from "media/storage/StorageAdapter";
class TestAdapter extends StorageAdapter {
files: Record<string, FileBody> = {};
+8 -3
View File
@@ -1,13 +1,18 @@
import { afterAll, beforeAll, beforeEach, describe, expect, spyOn, test } from "bun:test";
import { createApp } from "core/test/utils";
import { AuthController } from "../../src/auth/api/AuthController";
import { em, entity, make, text } from "../../src/data";
import { AppAuth, type ModuleBuildContext } from "../../src/modules";
import { em, entity, make, text } from "data/prototype";
import { AppAuth, type ModuleBuildContext } from "modules";
import { disableConsoleLog, enableConsoleLog } from "../helper";
// @ts-ignore
import { makeCtx, moduleTestSuite } from "./module-test-suite";
describe("AppAuth", () => {
test.only("...", () => {
const auth = new AppAuth({});
console.log(auth.toJSON());
console.log(auth.config);
});
moduleTestSuite(AppAuth);
let ctx: ModuleBuildContext;
+1 -1
View File
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, test } from "bun:test";
import { parse } from "../../src/core/utils";
import { parse } from "core/utils/schema";
import { fieldsSchema } from "../../src/data/data-schema";
import { AppData, type ModuleBuildContext } from "../../src/modules";
import { makeCtx, moduleTestSuite } from "./module-test-suite";
+8 -3
View File
@@ -1,12 +1,17 @@
import { describe, expect, test } from "bun:test";
import { registries } from "../../src";
import { createApp } from "core/test/utils";
import { em, entity, text } from "../../src/data";
import { em, entity, text } from "data/prototype";
import { registries } from "modules/registries";
import { StorageLocalAdapter } from "adapter/node/storage/StorageLocalAdapter";
import { AppMedia } from "../../src/modules";
import { AppMedia } from "../../src/media/AppMedia";
import { moduleTestSuite } from "./module-test-suite";
describe("AppMedia", () => {
test.only("...", () => {
const media = new AppMedia();
console.log(media.toJSON());
});
moduleTestSuite(AppMedia);
test("should allow additional fields", async () => {
+8 -10
View File
@@ -1,13 +1,13 @@
import { describe, expect, test } from "bun:test";
import { stripMark } from "../../src/core/utils";
import { type TSchema, Type } from "@sinclair/typebox";
import { EntityManager, em, entity, index, text } from "../../src/data";
import { s, stripMark } from "core/utils/schema";
import { em, entity, index, text } from "data/prototype";
import { EntityManager } from "data/entities/EntityManager";
import { DummyConnection } from "../../src/data/connection/DummyConnection";
import { Module } from "../../src/modules/Module";
import { ModuleHelper } from "modules/ModuleHelper";
function createModule<Schema extends TSchema>(schema: Schema) {
class TestModule extends Module<typeof schema> {
function createModule<Schema extends s.Schema>(schema: Schema) {
return class TestModule extends Module<Schema> {
getSchema() {
return schema;
}
@@ -17,9 +17,7 @@ function createModule<Schema extends TSchema>(schema: Schema) {
override useForceParse() {
return true;
}
}
return TestModule;
};
}
describe("Module", async () => {
@@ -27,7 +25,7 @@ describe("Module", async () => {
test("listener", async () => {
let result: any;
const module = createModule(Type.Object({ a: Type.String() }));
const module = createModule(s.object({ a: s.string() }));
const m = new module({ a: "test" });
await m.schema().set({ a: "test2" });
@@ -43,7 +41,7 @@ describe("Module", async () => {
describe("db schema", () => {
class M extends Module {
override getSchema() {
return Type.Object({});
return s.object({});
}
prt = {
+17 -13
View File
@@ -1,13 +1,13 @@
import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";
import { disableConsoleLog, enableConsoleLog, stripMark } from "core/utils";
import { Type } from "@sinclair/typebox";
import { Connection, entity, text } from "data";
import { disableConsoleLog, enableConsoleLog } from "core/utils";
import { Module } from "modules/Module";
import { type ConfigTable, getDefaultConfig, ModuleManager } from "modules/ModuleManager";
import { CURRENT_VERSION, TABLE_NAME } from "modules/migrations";
import { getDummyConnection } from "../helper";
import { diff } from "core/object/diff";
import type { Static } from "@sinclair/typebox";
import { s, stripMark } from "core/utils/schema";
import { Connection } from "data/connection/Connection";
import { entity, text } from "data/prototype";
describe("ModuleManager", async () => {
test("s1: no config, no build", async () => {
@@ -92,7 +92,11 @@ describe("ModuleManager", async () => {
await mm2.build();
expect(stripMark(json)).toEqual(stripMark(mm2.configs()));
/* console.log({
json,
configs: mm2.configs(),
}); */
//expect(stripMark(json)).toEqual(stripMark(mm2.configs()));
expect(mm2.configs().data.entities?.test).toBeDefined();
expect(mm2.configs().data.entities?.test?.fields?.content).toBeDefined();
expect(mm2.get("data").toJSON().entities?.test?.fields?.content).toBeDefined();
@@ -257,10 +261,10 @@ describe("ModuleManager", async () => {
// @todo: add tests for migrations (check "backup" and new version)
describe("revert", async () => {
const failingModuleSchema = Type.Object({
value: Type.Optional(Type.Number()),
const failingModuleSchema = s.partialObject({
value: s.number(),
});
class FailingModule extends Module<typeof failingModuleSchema> {
class FailingModule extends Module<s.Static<typeof failingModuleSchema>> {
getSchema() {
return failingModuleSchema;
}
@@ -431,11 +435,11 @@ describe("ModuleManager", async () => {
});
describe("validate & revert", () => {
const schema = Type.Object({
value: Type.Array(Type.Number(), { default: [] }),
const schema = s.object({
value: s.array(s.number()),
});
type SampleSchema = Static<typeof schema>;
class Sample extends Module<typeof schema> {
type SampleSchema = s.Static<typeof schema>;
class Sample extends Module<SampleSchema> {
getSchema() {
return schema;
}
+7 -7
View File
@@ -1,12 +1,11 @@
import { beforeEach, describe, expect, it } from "bun:test";
import { Hono } from "hono";
import { Guard } from "../../src/auth";
import { DebugLogger } from "../../src/core";
import { EventManager } from "../../src/core/events";
import { Default, stripMark } from "../../src/core/utils";
import { EntityManager } from "../../src/data";
import { Module, type ModuleBuildContext } from "../../src/modules/Module";
import { Guard } from "auth/authorize/Guard";
import { DebugLogger } from "core/utils/DebugLogger";
import { EventManager } from "core/events";
import { EntityManager } from "data/entities/EntityManager";
import { Module, type ModuleBuildContext } from "modules/Module";
import { getDummyConnection } from "../helper";
import { ModuleHelper } from "modules/ModuleHelper";
@@ -45,7 +44,8 @@ export function moduleTestSuite(module: { new (): Module }) {
it("uses the default config", async () => {
const m = new module();
await m.setContext(ctx).build();
expect(stripMark(m.toJSON())).toEqual(Default(m.getSchema(), {}));
expect(m.toJSON()).toEqual(m.getSchema().template({}, { withOptional: true }));
//expect(stripMark(m.toJSON())).toEqual(Default(m.getSchema(), {}));
});
});
}
+77 -83
View File
@@ -1,6 +1,7 @@
import { $ } from "bun";
import * as tsup from "tsup";
import pkg from "./package.json" with { type: "json" };
import c from "picocolors";
const args = process.argv.slice(2);
const watch = args.includes("--watch");
@@ -9,6 +10,14 @@ const types = args.includes("--types");
const sourcemap = args.includes("--sourcemap");
const clean = args.includes("--clean");
// silence tsup
const oldConsole = {
log: console.log,
warn: console.warn,
};
console.log = () => {};
console.warn = () => {};
const define = {
__isDev: "0",
__version: JSON.stringify(pkg.version),
@@ -27,11 +36,11 @@ function buildTypes() {
Bun.spawn(["bun", "build:types"], {
stdout: "inherit",
onExit: () => {
console.info("Types built");
oldConsole.log(c.cyan("[Types]"), c.green("built"));
Bun.spawn(["bun", "tsc-alias"], {
stdout: "inherit",
onExit: () => {
console.info("Types aliased");
oldConsole.log(c.cyan("[Types]"), c.green("aliased"));
types_running = false;
},
});
@@ -39,6 +48,10 @@ function buildTypes() {
});
}
if (types && !watch) {
buildTypes();
}
let watcher_timeout: any;
function delayTypes() {
if (!watch || !types) return;
@@ -48,17 +61,6 @@ function delayTypes() {
watcher_timeout = setTimeout(buildTypes, 1000);
}
if (types && !watch) {
buildTypes();
}
function banner(title: string) {
console.info("");
console.info("=".repeat(40));
console.info(title.toUpperCase());
console.info("-".repeat(40));
}
// collection of always-external packages
const external = [
"bun:test",
@@ -73,20 +75,12 @@ const external = [
* Building backend and general API
*/
async function buildApi() {
banner("Building API");
await tsup.build({
minify,
sourcemap,
watch,
define,
entry: [
"src/index.ts",
"src/core/index.ts",
"src/core/utils/index.ts",
"src/data/index.ts",
"src/media/index.ts",
"src/plugins/index.ts",
],
entry: ["src/index.ts", "src/core/utils/index.ts", "src/plugins/index.ts"],
outDir: "dist",
external: [...external],
metafile: true,
@@ -99,6 +93,7 @@ async function buildApi() {
},
onSuccess: async () => {
delayTypes();
oldConsole.log(c.cyan("[API]"), c.green("built"));
},
});
}
@@ -142,7 +137,6 @@ async function buildUi() {
},
} satisfies tsup.Options;
banner("Building UI");
await tsup.build({
...base,
entry: ["src/ui/index.ts", "src/ui/main.css", "src/ui/styles.css"],
@@ -150,10 +144,10 @@ async function buildUi() {
onSuccess: async () => {
await rewriteClient("./dist/ui/index.js");
delayTypes();
oldConsole.log(c.cyan("[UI]"), c.green("built"));
},
});
banner("Building Client");
await tsup.build({
...base,
entry: ["src/ui/client/index.ts"],
@@ -161,6 +155,7 @@ async function buildUi() {
onSuccess: async () => {
await rewriteClient("./dist/ui/client/index.js");
delayTypes();
oldConsole.log(c.cyan("[UI]"), "Client", c.green("built"));
},
});
}
@@ -171,7 +166,6 @@ async function buildUi() {
* - ui/client is external, and after built replaced with "bknd/client"
*/
async function buildUiElements() {
banner("Building UI Elements");
await tsup.build({
minify,
sourcemap,
@@ -205,6 +199,7 @@ async function buildUiElements() {
onSuccess: async () => {
await rewriteClient("./dist/ui/elements/index.js");
delayTypes();
oldConsole.log(c.cyan("[UI]"), "Elements", c.green("built"));
},
});
}
@@ -225,6 +220,7 @@ function baseConfig(adapter: string, overrides: Partial<tsup.Options> = {}): tsu
splitting: false,
onSuccess: async () => {
delayTypes();
oldConsole.log(c.cyan("[Adapter]"), adapter || "base", c.green("built"));
},
...overrides,
define: {
@@ -233,7 +229,7 @@ function baseConfig(adapter: string, overrides: Partial<tsup.Options> = {}): tsu
},
external: [
/^cloudflare*/,
/^@?(hono).*?/,
/^@?hono.*?/,
/^(bknd|react|next|node).*?/,
/.*\.(html)$/,
...external,
@@ -243,65 +239,63 @@ function baseConfig(adapter: string, overrides: Partial<tsup.Options> = {}): tsu
}
async function buildAdapters() {
banner("Building Adapters");
// base adapter handles
await tsup.build({
...baseConfig(""),
entry: ["src/adapter/index.ts"],
outDir: "dist/adapter",
});
await Promise.all([
// base adapter handles
tsup.build({
...baseConfig(""),
entry: ["src/adapter/index.ts"],
outDir: "dist/adapter",
}),
// specific adatpers
await tsup.build(baseConfig("react-router"));
await tsup.build(
baseConfig("bun", {
// specific adatpers
tsup.build(baseConfig("react-router")),
tsup.build(
baseConfig("bun", {
external: [/^bun\:.*/],
}),
),
tsup.build(baseConfig("astro")),
tsup.build(baseConfig("aws")),
tsup.build(baseConfig("cloudflare")),
tsup.build({
...baseConfig("vite"),
platform: "node",
}),
tsup.build({
...baseConfig("nextjs"),
platform: "node",
}),
tsup.build({
...baseConfig("node"),
platform: "node",
}),
tsup.build({
...baseConfig("sqlite/edge"),
entry: ["src/adapter/sqlite/edge.ts"],
outDir: "dist/adapter/sqlite",
metafile: false,
}),
tsup.build({
...baseConfig("sqlite/node"),
entry: ["src/adapter/sqlite/node.ts"],
outDir: "dist/adapter/sqlite",
platform: "node",
metafile: false,
}),
tsup.build({
...baseConfig("sqlite/bun"),
entry: ["src/adapter/sqlite/bun.ts"],
outDir: "dist/adapter/sqlite",
metafile: false,
external: [/^bun\:.*/],
}),
);
await tsup.build(baseConfig("astro"));
await tsup.build(baseConfig("aws"));
await tsup.build(baseConfig("cloudflare"));
await tsup.build({
...baseConfig("vite"),
platform: "node",
});
await tsup.build({
...baseConfig("nextjs"),
platform: "node",
});
await tsup.build({
...baseConfig("node"),
platform: "node",
});
await tsup.build({
...baseConfig("sqlite/edge"),
entry: ["src/adapter/sqlite/edge.ts"],
outDir: "dist/adapter/sqlite",
metafile: false,
});
await tsup.build({
...baseConfig("sqlite/node"),
entry: ["src/adapter/sqlite/node.ts"],
outDir: "dist/adapter/sqlite",
platform: "node",
metafile: false,
});
await tsup.build({
...baseConfig("sqlite/bun"),
entry: ["src/adapter/sqlite/bun.ts"],
outDir: "dist/adapter/sqlite",
metafile: false,
external: [/^bun\:.*/],
});
]);
}
await buildApi();
await buildUi();
await buildUiElements();
await buildAdapters();
await Promise.all([buildApi(), buildUi(), buildUiElements(), buildAdapters()]);
+10 -26
View File
@@ -3,7 +3,7 @@
"type": "module",
"sideEffects": false,
"bin": "./dist/cli/index.js",
"version": "0.15.0-rc.10",
"version": "0.16.1",
"description": "Lightweight Firebase/Supabase alternative built to run anywhere — incl. Next.js, React Router, Astro, Cloudflare, Bun, Node, AWS Lambda & more.",
"homepage": "https://bknd.io",
"repository": {
@@ -13,6 +13,7 @@
"bugs": {
"url": "https://github.com/bknd-io/bknd/issues"
},
"packageManager": "bun@1.2.19",
"engines": {
"node": ">=22"
},
@@ -53,7 +54,6 @@
"@hono/swagger-ui": "^0.5.1",
"@mantine/core": "^7.17.1",
"@mantine/hooks": "^7.17.1",
"@sinclair/typebox": "0.34.30",
"@tanstack/react-form": "^1.0.5",
"@uiw/react-codemirror": "^4.23.10",
"@xyflow/react": "^12.4.4",
@@ -61,12 +61,11 @@
"bcryptjs": "^3.0.2",
"dayjs": "^1.11.13",
"fast-xml-parser": "^5.0.8",
"hono": "^4.7.11",
"json-schema-form-react": "^0.0.2",
"hono": "4.8.3",
"json-schema-library": "10.0.0-rc7",
"json-schema-to-ts": "^3.1.1",
"jsonv-ts": "^0.1.0",
"kysely": "^0.27.6",
"jsonv-ts": "0.3.2",
"kysely": "0.27.6",
"lodash-es": "^4.17.21",
"oauth4webapi": "^2.11.1",
"object-path-immutable": "^4.1.2",
@@ -76,10 +75,10 @@
"devDependencies": {
"@aws-sdk/client-s3": "^3.758.0",
"@bluwy/giget-core": "^0.1.2",
"@clack/prompts": "^0.11.0",
"@cloudflare/vitest-pool-workers": "^0.8.38",
"@cloudflare/workers-types": "^4.20250606.0",
"@dagrejs/dagre": "^1.1.4",
"@hono/typebox-validator": "^0.3.3",
"@hono/vite-dev-server": "^0.19.1",
"@hookform/resolvers": "^4.1.3",
"@libsql/client": "^0.15.9",
@@ -87,6 +86,7 @@
"@mantine/notifications": "^7.17.1",
"@playwright/test": "^1.51.1",
"@rjsf/core": "5.22.2",
"@standard-schema/spec": "^1.0.0",
"@tabler/icons-react": "3.18.0",
"@tailwindcss/postcss": "^4.0.12",
"@tailwindcss/vite": "^4.0.12",
@@ -126,6 +126,7 @@
"tsx": "^4.19.3",
"uuid": "^11.1.0",
"vite": "^6.3.5",
"vite-plugin-circular-dependency": "^0.5.0",
"vite-tsconfig-paths": "^5.1.4",
"vitest": "^3.0.9",
"wouter": "^3.6.0"
@@ -161,16 +162,6 @@
"import": "./dist/ui/client/index.js",
"require": "./dist/ui/client/index.js"
},
"./data": {
"types": "./dist/types/data/index.d.ts",
"import": "./dist/data/index.js",
"require": "./dist/data/index.js"
},
"./core": {
"types": "./dist/types/core/index.d.ts",
"import": "./dist/core/index.js",
"require": "./dist/core/index.js"
},
"./utils": {
"types": "./dist/types/core/utils/index.d.ts",
"import": "./dist/core/utils/index.js",
@@ -181,11 +172,6 @@
"import": "./dist/cli/index.js",
"require": "./dist/cli/index.js"
},
"./media": {
"types": "./dist/types/media/index.d.ts",
"import": "./dist/media/index.js",
"require": "./dist/media/index.js"
},
"./plugins": {
"types": "./dist/types/plugins/index.d.ts",
"import": "./dist/plugins/index.js",
@@ -251,15 +237,13 @@
},
"./dist/main.css": "./dist/ui/main.css",
"./dist/styles.css": "./dist/ui/styles.css",
"./dist/manifest.json": "./dist/static/.vite/manifest.json"
"./dist/manifest.json": "./dist/static/.vite/manifest.json",
"./static/*": "./dist/static/*"
},
"typesVersions": {
"*": {
"data": ["./dist/types/data/index.d.ts"],
"core": ["./dist/types/core/index.d.ts"],
"utils": ["./dist/types/core/utils/index.d.ts"],
"cli": ["./dist/types/cli/index.d.ts"],
"media": ["./dist/types/media/index.d.ts"],
"plugins": ["./dist/types/plugins/index.d.ts"],
"adapter": ["./dist/types/adapter/index.d.ts"],
"adapter/cloudflare": ["./dist/types/adapter/cloudflare/index.d.ts"],
+1 -1
View File
@@ -1,4 +1,4 @@
import type { SafeUser } from "auth";
import type { SafeUser } from "bknd";
import { AuthApi, type AuthApiOptions } from "auth/api/AuthApi";
import { DataApi, type DataApiOptions } from "data/api/DataApi";
import { decode } from "hono/jwt";
+6
View File
@@ -40,6 +40,9 @@ export class AppConfigUpdatedEvent extends AppEvent<{
}> {
static override slug = "app-config-updated";
}
/**
* @type {Event<{ app: App }>}
*/
export class AppBuiltEvent extends AppEvent {
static override slug = "app-built";
}
@@ -71,6 +74,9 @@ export type AppOptions = {
};
};
export type CreateAppConfig = {
/**
* bla
*/
connection?: Connection | { url: string };
initialConfig?: InitialModuleConfigs;
options?: AppOptions;
+4 -3
View File
@@ -3,10 +3,9 @@
import path from "node:path";
import { type RuntimeBkndConfig, createRuntimeApp, type RuntimeOptions } from "bknd/adapter";
import { registerLocalMediaAdapter } from ".";
import { config } from "bknd/core";
import { config, type App } from "bknd";
import type { ServeOptions } from "bun";
import { serveStatic } from "hono/bun";
import type { App } from "App";
type BunEnv = Bun.Env;
export type BunBkndConfig<Env = BunEnv> = RuntimeBkndConfig<Env> & Omit<ServeOptions, "fetch">;
@@ -21,8 +20,8 @@ export async function createApp<Env = BunEnv>(
return await createRuntimeApp(
{
...config,
serveStatic: serveStatic({ root }),
...config,
},
args ?? (process.env as Env),
opts,
@@ -53,6 +52,7 @@ export function serve<Env = BunEnv>(
onBuilt,
buildConfig,
adminOptions,
serveStatic,
...serveOptions
}: BunBkndConfig<Env> = {},
args: Env = {} as Env,
@@ -70,6 +70,7 @@ export function serve<Env = BunEnv>(
buildConfig,
adminOptions,
distPath,
serveStatic,
},
args,
opts,
@@ -1,5 +1,5 @@
import { Database } from "bun:sqlite";
import { genericSqlite, type GenericSqliteConnection } from "bknd/data";
import { genericSqlite, type GenericSqliteConnection } from "bknd";
export type BunSqliteConnection = GenericSqliteConnection<Database>;
export type BunSqliteConnectionConfig = {
+4 -1
View File
@@ -12,7 +12,10 @@ export function getBindings<T extends GetBindingType>(env: any, type: T): Bindin
const bindings: BindingMap<T>[] = [];
for (const key in env) {
try {
if (env[key] && (env[key] as any).constructor.name === type) {
if (
env[key] &&
((env[key] as any).constructor.name === type || String(env[key]) === `[object ${type}]`)
) {
bindings.push({
key,
value: env[key] as BindingTypeMap[T],
@@ -18,7 +18,7 @@ describe("cf adapter", () => {
});
it("makes config", async () => {
const staticConfig = makeConfig(
const staticConfig = await makeConfig(
{
connection: { url: DB_URL },
initialConfig: { data: { basepath: DB_URL } },
@@ -28,7 +28,7 @@ describe("cf adapter", () => {
expect(staticConfig.initialConfig).toEqual({ data: { basepath: DB_URL } });
expect(staticConfig.connection).toBeDefined();
const dynamicConfig = makeConfig(
const dynamicConfig = await makeConfig(
{
app: (env) => ({
initialConfig: { data: { basepath: env.DB_URL } },
@@ -5,7 +5,6 @@ import { Hono } from "hono";
import { serveStatic } from "hono/cloudflare-workers";
import { getFresh } from "./modes/fresh";
import { getCached } from "./modes/cached";
import { getDurable } from "./modes/durable";
import type { App } from "bknd";
import { $console } from "core/utils";
@@ -17,10 +16,9 @@ declare global {
export type CloudflareEnv = Cloudflare.Env;
export type CloudflareBkndConfig<Env = CloudflareEnv> = RuntimeBkndConfig<Env> & {
mode?: "warm" | "fresh" | "cache" | "durable";
mode?: "warm" | "fresh" | "cache";
bindings?: (args: Env) => {
kv?: KVNamespace;
dobj?: DurableObjectNamespace;
db?: D1Database;
};
d1?: {
@@ -93,8 +91,6 @@ export function serve<Env extends CloudflareEnv = CloudflareEnv>(
case "cache":
app = await getCached(config, context);
break;
case "durable":
return await getDurable(config, context);
default:
throw new Error(`Unknown mode ${mode}`);
}
+6 -6
View File
@@ -1,16 +1,16 @@
/// <reference types="@cloudflare/workers-types" />
import { Connection } from "bknd";
import { sqlite } from "bknd/adapter/sqlite";
import { makeConfig as makeAdapterConfig } from "bknd/adapter";
import { registerMedia } from "./storage/StorageR2Adapter";
import { getBinding } from "./bindings";
import { d1Sqlite } from "./connection/D1Connection";
import { Connection } from "bknd/data";
import type { CloudflareBkndConfig, CloudflareEnv } from ".";
import { App } from "bknd";
import { makeConfig as makeAdapterConfig } from "bknd/adapter";
import type { Context, ExecutionContext } from "hono";
import { $console } from "core/utils";
import { setCookie } from "hono/cookie";
import { sqlite } from "bknd/adapter/sqlite";
export const constants = {
exec_async_event_id: "cf_register_waituntil",
@@ -89,7 +89,7 @@ export function d1SessionHelper(config: CloudflareBkndConfig<any>) {
}
let media_registered: boolean = false;
export function makeConfig<Env extends CloudflareEnv = CloudflareEnv>(
export async function makeConfig<Env extends CloudflareEnv = CloudflareEnv>(
config: CloudflareBkndConfig<Env>,
args?: CfMakeConfigArgs<Env>,
) {
@@ -102,7 +102,7 @@ export function makeConfig<Env extends CloudflareEnv = CloudflareEnv>(
media_registered = true;
}
const appConfig = makeAdapterConfig(config, args?.env);
const appConfig = await makeAdapterConfig(config, args?.env);
// if connection instance is given, don't do anything
// other than checking if D1 session is defined
@@ -115,7 +115,7 @@ export function makeConfig<Env extends CloudflareEnv = CloudflareEnv>(
}
// if connection is given, try to open with unified sqlite adapter
} else if (appConfig.connection) {
appConfig.connection = sqlite(appConfig.connection);
appConfig.connection = sqlite(appConfig.connection) as any;
// if connection is not given, but env is set
// try to make D1 from bindings
@@ -1,6 +1,6 @@
/// <reference types="@cloudflare/workers-types" />
import { genericSqlite, type GenericSqliteConnection } from "bknd/data";
import { genericSqlite, type GenericSqliteConnection } from "bknd";
import type { QueryResult } from "kysely";
export type D1SqliteConnection = GenericSqliteConnection<D1Database>;
@@ -1,6 +1,6 @@
/// <reference types="@cloudflare/workers-types" />
import { genericSqlite, type GenericSqliteConnection } from "bknd/data";
import { genericSqlite, type GenericSqliteConnection } from "bknd";
import type { QueryResult } from "kysely";
export type D1SqliteConnection = GenericSqliteConnection<D1Database>;
+1 -2
View File
@@ -3,7 +3,6 @@ import { d1Sqlite, type D1ConnectionConfig } from "./connection/D1Connection";
export * from "./cloudflare-workers.adapter";
export { makeApp, getFresh } from "./modes/fresh";
export { getCached } from "./modes/cached";
export { DurableBkndApp, getDurable } from "./modes/durable";
export { d1Sqlite, type D1ConnectionConfig };
export {
getBinding,
@@ -13,7 +12,7 @@ export {
type BindingMap,
} from "./bindings";
export { constants } from "./config";
export { StorageR2Adapter } from "./storage/StorageR2Adapter";
export { StorageR2Adapter, registerMedia } from "./storage/StorageR2Adapter";
export { registries } from "bknd";
// for compatibility with old code
-134
View File
@@ -1,134 +0,0 @@
import { DurableObject } from "cloudflare:workers";
import type { App, CreateAppConfig } from "bknd";
import { createRuntimeApp, makeConfig } from "bknd/adapter";
import type { CloudflareBkndConfig, Context, CloudflareEnv } from "../index";
import { constants, registerAsyncsExecutionContext } from "../config";
import { $console } from "core/utils";
export async function getDurable<Env extends CloudflareEnv = CloudflareEnv>(
config: CloudflareBkndConfig<Env>,
ctx: Context<Env>,
) {
const { dobj } = config.bindings?.(ctx.env)!;
if (!dobj) throw new Error("durable object is not defined in cloudflare.bindings");
const key = config.key ?? "app";
if ([config.onBuilt, config.beforeBuild].some((x) => x)) {
$console.warn("onBuilt and beforeBuild are not supported with DurableObject mode");
}
const start = performance.now();
const id = dobj.idFromName(key);
const stub = dobj.get(id) as unknown as DurableBkndApp;
const create_config = makeConfig(config, ctx.env);
const res = await stub.fire(ctx.request, {
config: create_config,
keepAliveSeconds: config.keepAliveSeconds,
});
const headers = new Headers(res.headers);
headers.set("X-TTDO", String(performance.now() - start));
return new Response(res.body, {
status: res.status,
statusText: res.statusText,
headers,
});
}
export class DurableBkndApp extends DurableObject {
protected id = Math.random().toString(36).slice(2);
protected app?: App;
protected interval?: any;
async fire(
request: Request,
options: {
config: CreateAppConfig;
html?: string;
keepAliveSeconds?: number;
setAdminHtml?: boolean;
},
) {
let buildtime = 0;
if (!this.app) {
const start = performance.now();
const config = options.config;
// change protocol to websocket if libsql
if (
config?.connection &&
"type" in config.connection &&
config.connection.type === "libsql"
) {
//config.connection.config.protocol = "wss";
}
this.app = await createRuntimeApp({
...config,
onBuilt: async (app) => {
registerAsyncsExecutionContext(app, this.ctx);
app.modules.server.get(constants.do_endpoint, async (c) => {
// @ts-ignore
const context: any = c.req.raw.cf ? c.req.raw.cf : c.env.cf;
return c.json({
id: this.id,
keepAliveSeconds: options?.keepAliveSeconds ?? 0,
colo: context.colo,
});
});
await this.onBuilt(app);
},
adminOptions: { html: options.html },
beforeBuild: async (app) => {
await this.beforeBuild(app);
},
});
buildtime = performance.now() - start;
}
if (options?.keepAliveSeconds) {
this.keepAlive(options.keepAliveSeconds);
}
const res = await this.app!.fetch(request);
const headers = new Headers(res.headers);
headers.set("X-BuildTime", buildtime.toString());
headers.set("X-DO-ID", this.id);
return new Response(res.body, {
status: res.status,
statusText: res.statusText,
headers,
});
}
async onBuilt(app: App) {}
async beforeBuild(app: App) {}
protected keepAlive(seconds: number) {
if (this.interval) {
clearInterval(this.interval);
}
let i = 0;
this.interval = setInterval(() => {
i += 1;
if (i === seconds) {
console.log("cleared");
clearInterval(this.interval);
// ping every 30 seconds
} else if (i % 30 === 0) {
console.log("ping");
this.app?.modules.ctx().connection.ping();
}
}, 1000);
}
}
+1 -1
View File
@@ -7,7 +7,7 @@ export async function makeApp<Env extends CloudflareEnv = CloudflareEnv>(
args?: CfMakeConfigArgs<Env>,
opts?: RuntimeOptions,
) {
return await createRuntimeApp<Env>(makeConfig(config, args), args?.env, opts);
return await createRuntimeApp<Env>(await makeConfig(config, args), args?.env, opts);
}
export async function getFresh<Env extends CloudflareEnv = CloudflareEnv>(
@@ -1,22 +1,21 @@
import { registries } from "bknd";
import { isDebug } from "bknd/core";
// @ts-ignore
import { StringEnum } from "bknd/utils";
import { guessMimeType as guess, StorageAdapter, type FileBody } from "bknd/media";
import { registries as $registries, isDebug, guessMimeType } from "bknd";
import { getBindings } from "../bindings";
import * as tb from "@sinclair/typebox";
const { Type } = tb;
import { s } from "bknd/utils";
import { StorageAdapter, type FileBody } from "bknd";
export function makeSchema(bindings: string[] = []) {
return Type.Object(
return s.object(
{
binding: bindings.length > 0 ? StringEnum(bindings) : Type.Optional(Type.String()),
binding: bindings.length > 0 ? s.string({ enum: bindings }) : s.string().optional(),
},
{ title: "R2", description: "Cloudflare R2 storage" },
);
}
export function registerMedia(env: Record<string, any>) {
export function registerMedia(
env: Record<string, any>,
registries: typeof $registries = $registries,
) {
const r2_bindings = getBindings(env, "R2Bucket");
registries.media.register(
@@ -93,7 +92,7 @@ export class StorageR2Adapter extends StorageAdapter {
const responseHeaders = new Headers({
"Accept-Ranges": "bytes",
"Content-Type": guess(key),
"Content-Type": guessMimeType(key),
});
const range = headers.has("range");
@@ -145,7 +144,7 @@ export class StorageR2Adapter extends StorageAdapter {
if (!metadata || Object.keys(metadata).length === 0) {
// guessing is especially required for dev environment (miniflare)
metadata = {
contentType: guess(object.key),
contentType: guessMimeType(object.key),
};
}
@@ -162,7 +161,7 @@ export class StorageR2Adapter extends StorageAdapter {
}
return {
type: String(head.httpMetadata?.contentType ?? guess(key)),
type: String(head.httpMetadata?.contentType ?? guessMimeType(key)),
size: head.size,
};
}
+73 -17
View File
@@ -1,16 +1,21 @@
import { App, type CreateAppConfig } from "bknd";
import { config as $config } from "bknd/core";
import {
config as $config,
App,
type CreateAppConfig,
Connection,
guessMimeType,
type MaybePromise,
registries as $registries,
} from "bknd";
import { $console } from "bknd/utils";
import type { MiddlewareHandler } from "hono";
import type { Context, MiddlewareHandler, Next } from "hono";
import type { AdminControllerOptions } from "modules/server/AdminController";
import { Connection } from "bknd/data";
export { Connection } from "bknd/data";
import type { Manifest } from "vite";
export type BkndConfig<Args = any> = CreateAppConfig & {
app?: CreateAppConfig | ((args: Args) => CreateAppConfig);
app?: CreateAppConfig | ((args: Args) => MaybePromise<CreateAppConfig>);
onBuilt?: (app: App) => Promise<void>;
beforeBuild?: (app: App) => Promise<void>;
beforeBuild?: (app: App, registries?: typeof $registries) => Promise<void>;
buildConfig?: Parameters<App["build"]>[0];
};
@@ -33,10 +38,10 @@ export type DefaultArgs = {
[key: string]: any;
};
export function makeConfig<Args = DefaultArgs>(
export async function makeConfig<Args = DefaultArgs>(
config: BkndConfig<Args>,
args?: Args,
): CreateAppConfig {
): Promise<CreateAppConfig> {
let additionalConfig: CreateAppConfig = {};
const { app, ...rest } = config;
if (app) {
@@ -44,7 +49,7 @@ export function makeConfig<Args = DefaultArgs>(
if (!args) {
throw new Error("args is required when config.app is a function");
}
additionalConfig = app(args);
additionalConfig = await app(args);
} else {
additionalConfig = app;
}
@@ -63,16 +68,16 @@ export async function createAdapterApp<Config extends BkndConfig = BkndConfig, A
const id = opts?.id ?? "app";
let app = apps.get(id);
if (!app || opts?.force) {
const appConfig = makeConfig(config, args);
const appConfig = await makeConfig(config, args);
if (!appConfig.connection || !Connection.isConnection(appConfig.connection)) {
let connection: Connection | undefined;
if (Connection.isConnection(config.connection)) {
connection = config.connection;
} else {
const sqlite = (await import("bknd/adapter/sqlite")).sqlite;
const conf = config.connection ?? { url: ":memory:" };
connection = sqlite(conf);
$console.info(`Using ${connection.name} connection`, conf.url);
const conf = appConfig.connection ?? { url: ":memory:" };
connection = sqlite(conf) as any;
$console.info(`Using ${connection!.name} connection`, conf.url);
}
appConfig.connection = connection;
}
@@ -101,7 +106,7 @@ export async function createFrameworkApp<Args = DefaultArgs>(
);
}
await config.beforeBuild?.(app);
await config.beforeBuild?.(app, $registries);
await app.build(config.buildConfig);
}
@@ -134,9 +139,60 @@ export async function createRuntimeApp<Args = DefaultArgs>(
"sync",
);
await config.beforeBuild?.(app);
await config.beforeBuild?.(app, $registries);
await app.build(config.buildConfig);
}
return app;
}
/**
* Creates a middleware handler to serve static assets via dynamic imports.
* This is useful for environments where filesystem access is limited but bundled assets can be imported.
*
* @param manifest - Vite manifest object containing asset information
* @returns Hono middleware handler for serving static assets
*
* @example
* ```typescript
* import { serveStaticViaImport } from "bknd/adapter";
*
* serve({
* serveStatic: serveStaticViaImport(),
* });
* ```
*/
export function serveStaticViaImport(opts?: { manifest?: Manifest }) {
let files: string[] | undefined;
// @ts-ignore
return async (c: Context, next: Next) => {
if (!files) {
const manifest =
opts?.manifest || ((await import("bknd/dist/manifest.json")).default as Manifest);
files = Object.values(manifest).flatMap((asset) => [asset.file, ...(asset.css || [])]);
}
const path = c.req.path.substring(1);
if (files.includes(path)) {
try {
const content = await import(/* @vite-ignore */ `bknd/static/${path}?raw`, {
assert: { type: "text" },
}).then((m) => m.default);
if (content) {
return c.body(content, {
headers: {
"Content-Type": guessMimeType(path),
"Cache-Control": "public, max-age=31536000, immutable",
},
});
}
} catch (e) {
console.error("Error serving static file:", e);
return c.text("File not found", 404);
}
}
await next();
};
}
@@ -1,4 +1,4 @@
import { genericSqlite } from "bknd/data";
import { genericSqlite } from "bknd";
import { DatabaseSync } from "node:sqlite";
export type NodeSqliteConnectionConfig = {
+3 -4
View File
@@ -3,9 +3,8 @@ import { serve as honoServe } from "@hono/node-server";
import { serveStatic } from "@hono/node-server/serve-static";
import { registerLocalMediaAdapter } from "adapter/node/storage";
import { type RuntimeBkndConfig, createRuntimeApp, type RuntimeOptions } from "bknd/adapter";
import { config as $config } from "bknd/core";
import { $console } from "core/utils";
import type { App } from "App";
import { config as $config, type App } from "bknd";
import { $console } from "bknd/utils";
type NodeEnv = NodeJS.ProcessEnv;
export type NodeBkndConfig<Env = NodeEnv> = RuntimeBkndConfig<Env> & {
@@ -32,8 +31,8 @@ export async function createApp<Env = NodeEnv>(
registerLocalMediaAdapter();
return await createRuntimeApp(
{
...config,
serveStatic: serveStatic({ root }),
...config,
},
// @ts-ignore
args ?? { env: process.env },
@@ -1,17 +1,15 @@
import { readFile, readdir, stat, unlink, writeFile } from "node:fs/promises";
import { type Static, isFile, parse } from "bknd/utils";
import type { FileBody, FileListObject, FileMeta, FileUploadPayload } from "bknd/media";
import { StorageAdapter, guessMimeType as guess } from "bknd/media";
import * as tb from "@sinclair/typebox";
const { Type } = tb;
import type { FileBody, FileListObject, FileMeta, FileUploadPayload } from "bknd";
import { StorageAdapter, guessMimeType } from "bknd";
import { parse, s, isFile } from "bknd/utils";
export const localAdapterConfig = Type.Object(
export const localAdapterConfig = s.object(
{
path: Type.String({ default: "./" }),
path: s.string({ default: "./" }),
},
{ title: "Local", description: "Local file system storage", additionalProperties: false },
);
export type LocalAdapterConfig = Static<typeof localAdapterConfig>;
export type LocalAdapterConfig = s.Static<typeof localAdapterConfig>;
export class StorageLocalAdapter extends StorageAdapter {
private config: LocalAdapterConfig;
@@ -62,8 +60,7 @@ export class StorageLocalAdapter extends StorageAdapter {
}
const filePath = `${this.config.path}/${key}`;
const is_file = isFile(body);
await writeFile(filePath, is_file ? body.stream() : body);
await writeFile(filePath, isFile(body) ? body.stream() : body);
return await this.computeEtag(body);
}
@@ -86,7 +83,7 @@ export class StorageLocalAdapter extends StorageAdapter {
async getObject(key: string, headers: Headers): Promise<Response> {
try {
const content = await readFile(`${this.config.path}/${key}`);
const mimeType = guess(key);
const mimeType = guessMimeType(key);
return new Response(content, {
status: 200,
@@ -108,7 +105,7 @@ export class StorageLocalAdapter extends StorageAdapter {
async getObjectMeta(key: string): Promise<FileMeta> {
const stats = await stat(`${this.config.path}/${key}`);
return {
type: guess(key) || "application/octet-stream",
type: guessMimeType(key) || "application/octet-stream",
size: stats.size,
};
}
+1 -1
View File
@@ -1,4 +1,4 @@
import type { Connection } from "bknd/data";
import type { Connection } from "bknd";
import { bunSqlite } from "../bun/connection/BunSqliteConnection";
export function sqlite(config?: { url: string }): Connection {
+1 -1
View File
@@ -1,4 +1,4 @@
import { type Connection, libsql } from "bknd/data";
import { type Connection, libsql } from "bknd";
export function sqlite(config: { url: string }): Connection {
return libsql(config);
+1 -1
View File
@@ -1,4 +1,4 @@
import type { Connection } from "bknd/data";
import type { Connection } from "bknd";
import { nodeSqlite } from "../node/connection/NodeSqliteConnection";
export function sqlite(config?: { url: string }): Connection {
+11 -8
View File
@@ -1,8 +1,9 @@
import { Authenticator, AuthPermissions, Role, type Strategy } from "auth";
import type { PasswordStrategy } from "auth/authenticate/strategies";
import type { DB } from "core";
import type { DB } from "bknd";
import * as AuthPermissions from "auth/auth-permissions";
import type { AuthStrategy } from "auth/authenticate/strategies/Strategy";
import type { PasswordStrategy } from "auth/authenticate/strategies/PasswordStrategy";
import { $console, secureRandomString, transformObject } from "core/utils";
import type { Entity, EntityManager } from "data";
import type { Entity, EntityManager } from "data/entities";
import { em, entity, enumm, type FieldSchema } from "data/prototype";
import { Module } from "modules/Module";
import { AuthController } from "./api/AuthController";
@@ -10,9 +11,11 @@ import { type AppAuthSchema, authConfigSchema, STRATEGIES } from "./auth-schema"
import { AppUserPool } from "auth/AppUserPool";
import type { AppEntity } from "core/config";
import { usersFields } from "./auth-entities";
import { Authenticator } from "./authenticate/Authenticator";
import { Role } from "./authorize/Role";
export type UserFieldSchema = FieldSchema<typeof AppAuth.usersFields>;
declare module "core" {
declare module "bknd" {
interface Users extends AppEntity, UserFieldSchema {}
interface DB {
users: Users;
@@ -21,7 +24,7 @@ declare module "core" {
export type CreateUserPayload = { email: string; password: string; [key: string]: any };
export class AppAuth extends Module<typeof authConfigSchema> {
export class AppAuth extends Module<AppAuthSchema> {
private _authenticator?: Authenticator;
cache: Record<string, any> = {};
_controller!: AuthController;
@@ -88,7 +91,7 @@ export class AppAuth extends Module<typeof authConfigSchema> {
this.ctx.guard.registerPermissions(AuthPermissions);
}
isStrategyEnabled(strategy: Strategy | string) {
isStrategyEnabled(strategy: AuthStrategy | string) {
const name = typeof strategy === "string" ? strategy : strategy.getName();
// for now, password is always active
if (name === "password") return true;
@@ -187,6 +190,6 @@ export class AppAuth extends Module<typeof authConfigSchema> {
enabled: this.isStrategyEnabled(strategy),
...strategy.toJSON(secrets),
})),
};
} as AppAuthSchema;
}
}
+2 -2
View File
@@ -1,6 +1,6 @@
import type { AuthActionResponse } from "auth/api/AuthController";
import type { AppAuthSchema } from "auth/auth-schema";
import type { AuthResponse, SafeUser, Strategy } from "auth/authenticate/Authenticator";
import type { AuthResponse, SafeUser, AuthStrategy } from "bknd";
import { type BaseModuleApiOptions, ModuleApi } from "modules/ModuleApi";
export type AuthApiOptions = BaseModuleApiOptions & {
@@ -39,7 +39,7 @@ export class AuthApi extends ModuleApi<AuthApiOptions> {
}
async actionSchema(strategy: string, action: string) {
return this.get<Strategy>([strategy, "actions", action, "schema.json"]);
return this.get<AuthStrategy>([strategy, "actions", action, "schema.json"]);
}
async action(strategy: string, action: string, input: any) {
+9 -7
View File
@@ -1,9 +1,11 @@
import { type AppAuth, AuthPermissions, type SafeUser, type Strategy } from "auth";
import { TypeInvalidError, parse, transformObject } from "core/utils";
import { DataPermissions } from "data";
import type { SafeUser } from "bknd";
import type { AuthStrategy } from "auth/authenticate/strategies/Strategy";
import type { AppAuth } from "auth/AppAuth";
import * as AuthPermissions from "auth/auth-permissions";
import * as DataPermissions from "data/permissions";
import type { Hono } from "hono";
import { Controller, type ServerEnv } from "modules/Controller";
import { describeRoute, jsc, s } from "core/object/schema";
import { describeRoute, jsc, s, parse, InvalidSchemaError, transformObject } from "bknd/utils";
export type AuthActionResponse = {
success: boolean;
@@ -30,7 +32,7 @@ export class AuthController extends Controller {
return this.em.repo(entity_name as "users");
}
private registerStrategyActions(strategy: Strategy, mainHono: Hono<ServerEnv>) {
private registerStrategyActions(strategy: AuthStrategy, mainHono: Hono<ServerEnv>) {
if (!this.auth.isStrategyEnabled(strategy)) {
return;
}
@@ -58,7 +60,7 @@ export class AuthController extends Controller {
try {
const body = await this.auth.authenticator.getBody(c);
const valid = parse(create.schema, body, {
skipMark: true,
//skipMark: true,
});
const processed = (await create.preprocess?.(valid)) ?? valid;
@@ -78,7 +80,7 @@ export class AuthController extends Controller {
data: created as unknown as SafeUser,
} as AuthActionResponse);
} catch (e) {
if (e instanceof TypeInvalidError) {
if (e instanceof InvalidSchemaError) {
return c.json(
{
success: false,
+1 -1
View File
@@ -1,4 +1,4 @@
import { Permission } from "core";
import { Permission } from "core/security/Permission";
export const createUser = new Permission("auth.user.create");
//export const updateUser = new Permission("auth.user.update");
+37 -45
View File
@@ -1,8 +1,6 @@
import { cookieConfig, jwtConfig } from "auth/authenticate/Authenticator";
import { CustomOAuthStrategy, OAuthStrategy, PasswordStrategy } from "auth/authenticate/strategies";
import { type Static, StringRecord, objectTransform } from "core/utils";
import * as tbbox from "@sinclair/typebox";
const { Type } = tbbox;
import { objectTransform, s } from "bknd/utils";
export const Strategies = {
password: {
@@ -21,64 +19,58 @@ export const Strategies = {
export const STRATEGIES = Strategies;
const strategiesSchemaObject = objectTransform(STRATEGIES, (strategy, name) => {
return Type.Object(
return s.strictObject(
{
enabled: Type.Optional(Type.Boolean({ default: true })),
type: Type.Const(name, { default: name, readOnly: true }),
enabled: s.boolean({ default: true }).optional(),
type: s.literal(name),
config: strategy.schema,
},
{
title: name,
additionalProperties: false,
},
);
});
const strategiesSchema = Type.Union(Object.values(strategiesSchemaObject));
export type AppAuthStrategies = Static<typeof strategiesSchema>;
export type AppAuthOAuthStrategy = Static<typeof STRATEGIES.oauth.schema>;
export type AppAuthCustomOAuthStrategy = Static<typeof STRATEGIES.custom_oauth.schema>;
const guardConfigSchema = Type.Object({
enabled: Type.Optional(Type.Boolean({ default: false })),
const strategiesSchema = s.anyOf(Object.values(strategiesSchemaObject));
export type AppAuthStrategies = s.Static<typeof strategiesSchema>;
export type AppAuthOAuthStrategy = s.Static<typeof STRATEGIES.oauth.schema>;
export type AppAuthCustomOAuthStrategy = s.Static<typeof STRATEGIES.custom_oauth.schema>;
const guardConfigSchema = s.object({
enabled: s.boolean({ default: false }).optional(),
});
export const guardRoleSchema = s.strictObject({
permissions: s.array(s.string()).optional(),
is_default: s.boolean().optional(),
implicit_allow: s.boolean().optional(),
});
export const guardRoleSchema = Type.Object(
{
permissions: Type.Optional(Type.Array(Type.String())),
is_default: Type.Optional(Type.Boolean()),
implicit_allow: Type.Optional(Type.Boolean()),
},
{ additionalProperties: false },
);
export const authConfigSchema = Type.Object(
export const authConfigSchema = s.strictObject(
{
enabled: Type.Boolean({ default: false }),
basepath: Type.String({ default: "/api/auth" }),
entity_name: Type.String({ default: "users" }),
allow_register: Type.Optional(Type.Boolean({ default: true })),
enabled: s.boolean({ default: false }),
basepath: s.string({ default: "/api/auth" }),
entity_name: s.string({ default: "users" }),
allow_register: s.boolean({ default: true }).optional(),
jwt: jwtConfig,
cookie: cookieConfig,
strategies: Type.Optional(
StringRecord(strategiesSchema, {
title: "Strategies",
default: {
password: {
type: "password",
enabled: true,
config: {
hashing: "sha256",
},
strategies: s.record(strategiesSchema, {
title: "Strategies",
default: {
password: {
type: "password",
enabled: true,
config: {
hashing: "sha256",
},
},
}),
),
guard: Type.Optional(guardConfigSchema),
roles: Type.Optional(StringRecord(guardRoleSchema, { default: {} })),
},
{
title: "Authentication",
additionalProperties: false,
},
}),
guard: guardConfigSchema.optional(),
roles: s.record(guardRoleSchema, { default: {} }).optional(),
},
{ title: "Authentication" },
);
export type AppAuthSchema = Static<typeof authConfigSchema>;
export type AppAuthJWTConfig = s.Static<typeof jwtConfig>;
export type AppAuthSchema = s.Static<typeof authConfigSchema>;
+52 -62
View File
@@ -1,46 +1,27 @@
import { type DB, Exception } from "core";
import type { DB } from "bknd";
import { Exception } from "core/errors";
import { addFlashMessage } from "core/server/flash";
import {
$console,
type Static,
StringEnum,
type TObject,
parse,
runtimeSupports,
truncate,
} from "core/utils";
import type { Context, Hono } from "hono";
import type { Context } from "hono";
import { deleteCookie, getSignedCookie, setSignedCookie } from "hono/cookie";
import { sign, verify } from "hono/jwt";
import type { CookieOptions } from "hono/utils/cookie";
import { type CookieOptions, serializeSigned } from "hono/utils/cookie";
import type { ServerEnv } from "modules/Controller";
import { pick } from "lodash-es";
import * as tbbox from "@sinclair/typebox";
import { InvalidConditionsException } from "auth/errors";
const { Type } = tbbox;
import { s, parse, secret, runtimeSupports, truncate, $console } from "bknd/utils";
import type { AuthStrategy } from "./strategies/Strategy";
type Input = any; // workaround
export type JWTPayload = Parameters<typeof sign>[0];
export const strategyActions = ["create", "change"] as const;
export type StrategyActionName = (typeof strategyActions)[number];
export type StrategyAction<S extends TObject = TObject> = {
export type StrategyAction<S extends s.ObjectSchema = s.ObjectSchema> = {
schema: S;
preprocess: (input: Static<S>) => Promise<Omit<DB["users"], "id" | "strategy">>;
preprocess: (input: s.Static<S>) => Promise<Omit<DB["users"], "id" | "strategy">>;
};
export type StrategyActions = Partial<Record<StrategyActionName, StrategyAction>>;
// @todo: add schema to interface to ensure proper inference
// @todo: add tests (e.g. invalid strategy_value)
export interface Strategy {
getController: (auth: Authenticator) => Hono<any>;
getType: () => string;
getMode: () => "form" | "external";
getName: () => string;
toJSON: (secrets?: boolean) => any;
getActions?: () => StrategyActions;
}
export type User = DB["users"];
export type ProfileExchange = {
@@ -60,43 +41,45 @@ export interface UserPool {
}
const defaultCookieExpires = 60 * 60 * 24 * 7; // 1 week in seconds
export const cookieConfig = Type.Partial(
Type.Object({
path: Type.String({ default: "/" }),
sameSite: StringEnum(["strict", "lax", "none"], { default: "lax" }),
secure: Type.Boolean({ default: true }),
httpOnly: Type.Boolean({ default: true }),
expires: Type.Number({ default: defaultCookieExpires }), // seconds
renew: Type.Boolean({ default: true }),
pathSuccess: Type.String({ default: "/" }),
pathLoggedOut: Type.String({ default: "/" }),
}),
{ default: {}, additionalProperties: false },
);
export const cookieConfig = s
.object({
path: s.string({ default: "/" }),
sameSite: s.string({ enum: ["strict", "lax", "none"], default: "lax" }),
secure: s.boolean({ default: true }),
httpOnly: s.boolean({ default: true }),
expires: s.number({ default: defaultCookieExpires }), // seconds
partitioned: s.boolean({ default: false }),
renew: s.boolean({ default: true }),
pathSuccess: s.string({ default: "/" }),
pathLoggedOut: s.string({ default: "/" }),
})
.partial()
.strict();
// @todo: maybe add a config to not allow cookie/api tokens to be used interchangably?
// see auth.integration test for further details
export const jwtConfig = Type.Object(
{
// @todo: autogenerate a secret if not present. But it must be persisted from AppAuth
secret: Type.String({ default: "" }),
alg: Type.Optional(StringEnum(["HS256", "HS384", "HS512"], { default: "HS256" })),
expires: Type.Optional(Type.Number()), // seconds
issuer: Type.Optional(Type.String()),
fields: Type.Array(Type.String(), { default: ["id", "email", "role"] }),
},
{
default: {},
additionalProperties: false,
},
);
export const authenticatorConfig = Type.Object({
export const jwtConfig = s
.object(
{
// @todo: autogenerate a secret if not present. But it must be persisted from AppAuth
secret: secret({ default: "" }),
alg: s.string({ enum: ["HS256", "HS384", "HS512"], default: "HS256" }).optional(),
expires: s.number().optional(), // seconds
issuer: s.string().optional(),
fields: s.array(s.string(), { default: ["id", "email", "role"] }),
},
{
default: {},
},
)
.strict();
export const authenticatorConfig = s.object({
jwt: jwtConfig,
cookie: cookieConfig,
});
type AuthConfig = Static<typeof authenticatorConfig>;
type AuthConfig = s.Static<typeof authenticatorConfig>;
export type AuthAction = "login" | "register";
export type AuthResolveOptions = {
identifier?: "email" | string;
@@ -105,7 +88,7 @@ export type AuthResolveOptions = {
};
export type AuthUserResolver = (
action: AuthAction,
strategy: Strategy,
strategy: AuthStrategy,
profile: ProfileExchange,
opts?: AuthResolveOptions,
) => Promise<ProfileExchange | undefined>;
@@ -115,7 +98,9 @@ type AuthClaims = SafeUser & {
exp?: number;
};
export class Authenticator<Strategies extends Record<string, Strategy> = Record<string, Strategy>> {
export class Authenticator<
Strategies extends Record<string, AuthStrategy> = Record<string, AuthStrategy>,
> {
private readonly config: AuthConfig;
constructor(
@@ -128,7 +113,7 @@ export class Authenticator<Strategies extends Record<string, Strategy> = Record<
async resolveLogin(
c: Context,
strategy: Strategy,
strategy: AuthStrategy,
profile: Partial<SafeUser>,
verify: (user: User) => Promise<void>,
opts?: AuthResolveOptions,
@@ -166,7 +151,7 @@ export class Authenticator<Strategies extends Record<string, Strategy> = Record<
async resolveRegister(
c: Context,
strategy: Strategy,
strategy: AuthStrategy,
profile: CreateUser,
verify: (user: User) => Promise<void>,
opts?: AuthResolveOptions,
@@ -225,7 +210,7 @@ export class Authenticator<Strategies extends Record<string, Strategy> = Record<
await addFlashMessage(c, String(error), "error");
const referer = this.getSafeUrl(c, opts?.redirect ?? c.req.header("Referer") ?? "/");
const referer = this.getSafeUrl(c, c.req.header("Referer") ?? "/");
return c.redirect(referer);
}
@@ -235,7 +220,7 @@ export class Authenticator<Strategies extends Record<string, Strategy> = Record<
strategy<
StrategyName extends keyof Strategies,
Strat extends Strategy = Strategies[StrategyName],
Strat extends AuthStrategy = Strategies[StrategyName],
>(strategy: StrategyName): Strat {
try {
return this.strategies[strategy] as unknown as Strat;
@@ -342,6 +327,11 @@ export class Authenticator<Strategies extends Record<string, Strategy> = Record<
await setSignedCookie(c, "auth", token, secret, this.cookieOptions);
}
async unsafeGetAuthCookie(token: string): Promise<string | undefined> {
// this works for as long as cookieOptions.prefix is not set
return serializeSigned("auth", token, this.config.jwt.secret, this.cookieOptions);
}
private deleteAuthCookie(c: Context) {
$console.debug("deleting auth cookie");
deleteCookie(c, "auth", this.cookieOptions);
@@ -1,21 +1,22 @@
import { type Authenticator, InvalidCredentialsException, type User } from "auth";
import { tbValidator as tb } from "core";
import { $console, hash, parse, type Static, StrictObject, StringEnum } from "core/utils";
import type { User } from "bknd";
import type { Authenticator } from "auth/authenticate/Authenticator";
import { InvalidCredentialsException } from "auth/errors";
import { hash, $console } from "core/utils";
import { Hono } from "hono";
import { compare as bcryptCompare, genSalt as bcryptGenSalt, hash as bcryptHash } from "bcryptjs";
import * as tbbox from "@sinclair/typebox";
import { Strategy } from "./Strategy";
import { AuthStrategy } from "./Strategy";
import { s, parse, jsc } from "bknd/utils";
const { Type } = tbbox;
const schema = s
.object({
hashing: s.string({ enum: ["plain", "sha256", "bcrypt"], default: "sha256" }),
rounds: s.number({ minimum: 1, maximum: 10 }).optional(),
})
.strict();
const schema = StrictObject({
hashing: StringEnum(["plain", "sha256", "bcrypt"], { default: "sha256" }),
rounds: Type.Optional(Type.Number({ minimum: 1, maximum: 10 })),
});
export type PasswordStrategyOptions = s.Static<typeof schema>;
export type PasswordStrategyOptions = Static<typeof schema>;
export class PasswordStrategy extends Strategy<typeof schema> {
export class PasswordStrategy extends AuthStrategy<typeof schema> {
constructor(config: Partial<PasswordStrategyOptions> = {}) {
super(config as any, "password", "password", "form");
@@ -32,11 +33,11 @@ export class PasswordStrategy extends Strategy<typeof schema> {
}
private getPayloadSchema() {
return Type.Object({
email: Type.String({
pattern: "^[\\w-\\.\\+_]+@([\\w-]+\\.)+[\\w-]{2,4}$",
return s.object({
email: s.string({
format: "email",
}),
password: Type.String({
password: s.string({
minLength: 8, // @todo: this should be configurable
}),
});
@@ -79,12 +80,12 @@ export class PasswordStrategy extends Strategy<typeof schema> {
getController(authenticator: Authenticator): Hono<any> {
const hono = new Hono();
const redirectQuerySchema = Type.Object({
redirect: Type.Optional(Type.String()),
const redirectQuerySchema = s.object({
redirect: s.string().optional(),
});
const payloadSchema = this.getPayloadSchema();
hono.post("/login", tb("query", redirectQuerySchema), async (c) => {
hono.post("/login", jsc("query", redirectQuerySchema), async (c) => {
try {
const body = parse(payloadSchema, await authenticator.getBody(c), {
onError: (errors) => {
@@ -102,7 +103,7 @@ export class PasswordStrategy extends Strategy<typeof schema> {
}
});
hono.post("/register", tb("query", redirectQuerySchema), async (c) => {
hono.post("/register", jsc("query", redirectQuerySchema), async (c) => {
try {
const { redirect } = c.req.valid("query");
const { password, email, ...body } = parse(
@@ -5,31 +5,31 @@ import type {
StrategyActions,
} from "../Authenticator";
import type { Hono } from "hono";
import type { Static, TSchema } from "@sinclair/typebox";
import { parse, type TObject } from "core/utils";
import { type s, parse } from "bknd/utils";
export type StrategyMode = "form" | "external";
export abstract class Strategy<Schema extends TSchema = TSchema> {
export abstract class AuthStrategy<Schema extends s.Schema = s.Schema> {
protected actions: StrategyActions = {};
constructor(
protected config: Static<Schema>,
protected config: s.Static<Schema>,
public type: string,
public name: string,
public mode: StrategyMode,
) {
// don't worry about typing, it'll throw if invalid
this.config = parse(this.getSchema(), (config ?? {}) as any) as Static<Schema>;
this.config = parse(this.getSchema(), (config ?? {}) as any) as s.Static<Schema>;
}
protected registerAction<S extends TObject = TObject>(
protected registerAction<S extends s.ObjectSchema = s.ObjectSchema>(
name: StrategyActionName,
schema: S,
preprocess: StrategyAction<S>["preprocess"],
): void {
this.actions[name] = {
schema,
// @ts-expect-error - @todo: fix this
preprocess,
} as const;
}
@@ -50,7 +50,7 @@ export abstract class Strategy<Schema extends TSchema = TSchema> {
return this.name;
}
toJSON(secrets?: boolean): { type: string; config: Static<Schema> | {} | undefined } {
toJSON(secrets?: boolean): { type: string; config: s.Static<Schema> | {} | undefined } {
return {
type: this.getType(),
config: secrets ? this.config : undefined,
@@ -1,38 +1,36 @@
import { type Static, StrictObject, StringEnum } from "core/utils";
import * as tbbox from "@sinclair/typebox";
import type * as oauth from "oauth4webapi";
import { OAuthStrategy } from "./OAuthStrategy";
const { Type } = tbbox;
import { s } from "bknd/utils";
type SupportedTypes = "oauth2" | "oidc";
type RequireKeys<T extends object, K extends keyof T> = Required<Pick<T, K>> & Omit<T, K>;
const UrlString = Type.String({ pattern: "^(https?|wss?)://[^\\s/$.?#].[^\\s]*$" });
const oauthSchemaCustom = StrictObject(
const UrlString = s.string({ pattern: "^(https?|wss?)://[^\\s/$.?#].[^\\s]*$" });
const oauthSchemaCustom = s.strictObject(
{
type: StringEnum(["oidc", "oauth2"] as const, { default: "oidc" }),
name: Type.String(),
client: StrictObject({
client_id: Type.String(),
client_secret: Type.String(),
token_endpoint_auth_method: StringEnum(["client_secret_basic"]),
type: s.string({ enum: ["oidc", "oauth2"] as const, default: "oidc" }),
name: s.string(),
client: s.object({
client_id: s.string(),
client_secret: s.string(),
token_endpoint_auth_method: s.string({ enum: ["client_secret_basic"] }),
}),
as: StrictObject({
issuer: Type.String(),
code_challenge_methods_supported: Type.Optional(StringEnum(["S256"])),
scopes_supported: Type.Optional(Type.Array(Type.String())),
scope_separator: Type.Optional(Type.String({ default: " " })),
authorization_endpoint: Type.Optional(UrlString),
token_endpoint: Type.Optional(UrlString),
userinfo_endpoint: Type.Optional(UrlString),
as: s.strictObject({
issuer: s.string(),
code_challenge_methods_supported: s.string({ enum: ["S256"] }).optional(),
scopes_supported: s.array(s.string()).optional(),
scope_separator: s.string({ default: " " }).optional(),
authorization_endpoint: UrlString.optional(),
token_endpoint: UrlString.optional(),
userinfo_endpoint: UrlString.optional(),
}),
// @todo: profile mapping
},
{ title: "Custom OAuth" },
);
type OAuthConfigCustom = Static<typeof oauthSchemaCustom>;
type OAuthConfigCustom = s.Static<typeof oauthSchemaCustom>;
export type UserProfile = {
sub: string;
@@ -1,31 +1,32 @@
import type { AuthAction, Authenticator } from "auth";
import { Exception, isDebug } from "core";
import { type Static, StringEnum, filterKeys, StrictObject } from "core/utils";
import type { Authenticator, AuthAction } from "auth/authenticate/Authenticator";
import { type Context, Hono } from "hono";
import { getSignedCookie, setSignedCookie } from "hono/cookie";
import * as oauth from "oauth4webapi";
import * as issuers from "./issuers";
import * as tbbox from "@sinclair/typebox";
import { Strategy } from "auth/authenticate/strategies/Strategy";
const { Type } = tbbox;
import { s, filterKeys } from "bknd/utils";
import { Exception } from "core/errors";
import { isDebug } from "core/env";
import { AuthStrategy } from "../Strategy";
type ConfiguredIssuers = keyof typeof issuers;
type SupportedTypes = "oauth2" | "oidc";
type RequireKeys<T extends object, K extends keyof T> = Required<Pick<T, K>> & Omit<T, K>;
const schemaProvided = Type.Object(
const schemaProvided = s.object(
{
name: StringEnum(Object.keys(issuers) as ConfiguredIssuers[]),
type: StringEnum(["oidc", "oauth2"] as const, { default: "oauth2" }),
client: StrictObject({
client_id: Type.String(),
client_secret: Type.String(),
}),
name: s.string({ enum: Object.keys(issuers) as ConfiguredIssuers[] }),
type: s.string({ enum: ["oidc", "oauth2"] as const, default: "oauth2" }),
client: s
.object({
client_id: s.string(),
client_secret: s.string(),
})
.strict(),
},
{ title: "OAuth" },
);
type ProvidedOAuthConfig = Static<typeof schemaProvided>;
type ProvidedOAuthConfig = s.Static<typeof schemaProvided>;
export type CustomOAuthConfig = {
type: SupportedTypes;
@@ -69,7 +70,7 @@ export class OAuthCallbackException extends Exception {
}
}
export class OAuthStrategy extends Strategy<typeof schemaProvided> {
export class OAuthStrategy extends AuthStrategy<typeof schemaProvided> {
constructor(config: ProvidedOAuthConfig) {
super(config, "oauth", config.name, "external");
}
+2 -1
View File
@@ -1,5 +1,6 @@
import { Exception, Permission } from "core";
import { Exception } from "core/errors";
import { $console, objectTransform } from "core/utils";
import { Permission } from "core/security/Permission";
import type { Context } from "hono";
import type { ServerEnv } from "modules/Controller";
import { Role } from "./Role";
+1 -1
View File
@@ -1,4 +1,4 @@
import { Permission } from "core";
import { Permission } from "core/security/Permission";
export class RolePermission {
constructor(
+3 -2
View File
@@ -1,5 +1,6 @@
import { Exception, isDebug } from "core";
import { HttpStatus } from "core/utils";
import { Exception } from "core/errors";
import { isDebug } from "core/env";
import { HttpStatus } from "bknd/utils";
export class AuthException extends Exception {
getSafeErrorAndCode() {
-22
View File
@@ -1,22 +0,0 @@
export { UserExistsException, UserNotFoundException, InvalidCredentialsException } from "./errors";
export {
type ProfileExchange,
type Strategy,
type User,
type SafeUser,
type CreateUser,
type AuthResponse,
type UserPool,
type AuthAction,
type AuthUserResolver,
Authenticator,
authenticatorConfig,
jwtConfig,
} from "./authenticate/Authenticator";
export { AppAuth, type UserFieldSchema } from "./AppAuth";
export { Guard, type GuardUserContext, type GuardConfig } from "./authorize/Guard";
export { Role } from "./authorize/Role";
export * as AuthPermissions from "./auth-permissions";
+2 -2
View File
@@ -1,5 +1,5 @@
import type { Permission } from "core";
import { $console, patternMatch } from "core/utils";
import type { Permission } from "core/security/Permission";
import { $console, patternMatch } from "bknd/utils";
import type { Context } from "hono";
import { createMiddleware } from "hono/factory";
import type { ServerEnv } from "modules/Controller";
+1 -1
View File
@@ -5,7 +5,7 @@ import type { CliCommand } from "cli/types";
import { typewriter, wait } from "cli/utils/cli";
import { execAsync, getVersion } from "cli/utils/sys";
import { Option } from "commander";
import { env } from "core";
import { env } from "bknd";
import color from "picocolors";
import { overridePackageJson, updateBkndPackages } from "./npm";
import { type Template, templates, type TemplateSetupCtx } from "./templates";
+4 -5
View File
@@ -1,9 +1,8 @@
import type { Config } from "@libsql/client/node";
import type { App, CreateAppConfig } from "App";
import { StorageLocalAdapter } from "adapter/node/storage";
import type { CliBkndConfig, CliCommand } from "cli/types";
import { Option } from "commander";
import { config } from "core";
import { config, type App, type CreateAppConfig } from "bknd";
import dotenv from "dotenv";
import { registries } from "modules/registries";
import c from "picocolors";
@@ -16,8 +15,8 @@ import {
serveStatic,
startServer,
} from "./platform";
import { createRuntimeApp, makeConfig } from "adapter";
import { colorizeConsole, isBun } from "core/utils";
import { createRuntimeApp, makeConfig } from "bknd/adapter";
import { colorizeConsole, isBun } from "bknd/utils";
const env_files = [".env", ".dev.vars"];
dotenv.config({
@@ -78,7 +77,7 @@ async function makeApp(config: MakeAppConfig) {
}
export async function makeConfigApp(_config: CliBkndConfig, platform?: Platform) {
const config = makeConfig(_config, process.env);
const config = await makeConfig(_config, process.env);
return makeApp({
...config,
server: { platform },
+2 -2
View File
@@ -1,7 +1,7 @@
import { PostHog } from "posthog-js-lite";
import { getVersion } from "cli/utils/sys";
import { env, isDebug } from "core";
import { $console } from "core/utils";
import { env, isDebug } from "bknd";
import { $console } from "bknd/utils";
type Properties = { [p: string]: any };
-20
View File
@@ -6,23 +6,3 @@ export interface IEmailDriver<Data = unknown, Options = object> {
options?: Options,
): Promise<Data>;
}
import type { BkndConfig } from "bknd";
import { resendEmail, memoryCache } from "bknd/core";
export default {
onBuilt: async (app) => {
app.server.get("/send-email", async (c) => {
if (await app.drivers?.email?.send("test@test.com", "Test", "Test")) {
return c.text("success");
}
return c.text("failed");
});
},
options: {
drivers: {
email: resendEmail({ apiKey: "..." }),
cache: memoryCache(),
},
},
} as const satisfies BkndConfig;
-48
View File
@@ -1,48 +0,0 @@
import type { Hono, MiddlewareHandler } from "hono";
export { tbValidator } from "./server/lib/tbValidator";
export { Exception, BkndError } from "./errors";
export { isDebug, env } from "./env";
export { type PrimaryFieldType, config, type DB, type AppEntity } from "./config";
export { AwsClient } from "./clients/aws/AwsClient";
export {
SimpleRenderer,
type TemplateObject,
type TemplateTypes,
type SimpleRendererOptions,
} from "./template/SimpleRenderer";
export { SchemaObject } from "./object/SchemaObject";
export { DebugLogger } from "./utils/DebugLogger";
export { Permission } from "./security/Permission";
export {
exp,
makeValidator,
type FilterQuery,
type Primitive,
isPrimitive,
type TExpression,
type BooleanLike,
isBooleanLike,
} from "./object/query/query";
export { Registry, type Constructor } from "./registry/Registry";
export { getFlashMessage } from "./server/flash";
export {
s,
parse,
jsc,
describeRoute,
schemaToSpec,
openAPISpecs,
type ParseOptions,
InvalidSchemaError,
} from "./object/schema";
export * from "./drivers";
export * from "./events";
// compatibility
export type Middleware = MiddlewareHandler<any, any, any>;
export interface ClassController {
getController: () => Hono<any, any, any>;
getMiddleware?: MiddlewareHandler<any, any, any>;
}
+43 -40
View File
@@ -1,62 +1,61 @@
import { get, has, omit, set } from "lodash-es";
import {
Default,
type Static,
type TObject,
getFullPathKeys,
mergeObjectWith,
parse,
stripMark,
} from "../utils";
import { type s, parse, stripMark, getFullPathKeys, mergeObjectWith, deepFreeze } from "bknd/utils";
export type SchemaObjectOptions<Schema extends TObject> = {
onUpdate?: (config: Static<Schema>) => void | Promise<void>;
export type SchemaObjectOptions<Schema extends s.Schema> = {
onUpdate?: (config: s.Static<Schema>) => void | Promise<void>;
onBeforeUpdate?: (
from: Static<Schema>,
to: Static<Schema>,
) => Static<Schema> | Promise<Static<Schema>>;
from: s.Static<Schema>,
to: s.Static<Schema>,
) => s.Static<Schema> | Promise<s.Static<Schema>>;
restrictPaths?: string[];
overwritePaths?: (RegExp | string)[];
forceParse?: boolean;
};
export class SchemaObject<Schema extends TObject> {
private readonly _default: Partial<Static<Schema>>;
private _value: Static<Schema>;
private _config: Static<Schema>;
type TSchema = s.ObjectSchema<any>;
export class SchemaObject<Schema extends TSchema = TSchema> {
private readonly _default: Partial<s.Static<Schema>>;
private _value: s.Static<Schema>;
private _config: s.Static<Schema>;
private _restriction_bypass: boolean = false;
constructor(
private _schema: Schema,
initial?: Partial<Static<Schema>>,
initial?: Partial<s.Static<Schema>>,
private options?: SchemaObjectOptions<Schema>,
) {
this._default = Default(_schema, {} as any) as any;
this._value = initial
? parse(_schema, structuredClone(initial as any), {
forceParse: this.isForceParse(),
skipMark: this.isForceParse(),
})
: this._default;
this._config = Object.freeze(this._value);
this._default = deepFreeze(_schema.template({}, { withOptional: true }) as any);
this._value = deepFreeze(
parse(_schema, structuredClone(initial ?? {}), {
withDefaults: true,
//withExtendedDefaults: true,
forceParse: this.isForceParse(),
skipMark: this.isForceParse(),
}),
);
this._config = deepFreeze(this._value);
}
protected isForceParse(): boolean {
return this.options?.forceParse ?? true;
}
default(): Static<Schema> {
default() {
return this._default;
}
private async onBeforeUpdate(from: Static<Schema>, to: Static<Schema>): Promise<Static<Schema>> {
private async onBeforeUpdate(
from: s.Static<Schema>,
to: s.Static<Schema>,
): Promise<s.Static<Schema>> {
if (this.options?.onBeforeUpdate) {
return this.options.onBeforeUpdate(from, to);
}
return to;
}
get(options?: { stripMark?: boolean }): Static<Schema> {
get(options?: { stripMark?: boolean }): s.Static<Schema> {
if (options?.stripMark) {
return stripMark(this._config);
}
@@ -68,8 +67,9 @@ export class SchemaObject<Schema extends TObject> {
return structuredClone(this._config);
}
async set(config: Static<Schema>, noEmit?: boolean): Promise<Static<Schema>> {
async set(config: s.Static<Schema>, noEmit?: boolean): Promise<s.Static<Schema>> {
const valid = parse(this._schema, structuredClone(config) as any, {
coerce: false,
forceParse: true,
skipMark: this.isForceParse(),
});
@@ -77,8 +77,8 @@ export class SchemaObject<Schema extends TObject> {
// regardless of "noEmit" this should always be triggered
const updatedConfig = await this.onBeforeUpdate(this._config, valid);
this._value = updatedConfig;
this._config = Object.freeze(updatedConfig);
this._value = deepFreeze(updatedConfig);
this._config = deepFreeze(updatedConfig);
if (noEmit !== true) {
await this.options?.onUpdate?.(this._config);
@@ -118,9 +118,9 @@ export class SchemaObject<Schema extends TObject> {
return;
}
async patch(path: string, value: any): Promise<[Partial<Static<Schema>>, Static<Schema>]> {
async patch(path: string, value: any): Promise<[Partial<s.Static<Schema>>, s.Static<Schema>]> {
const current = this.clone();
const partial = path.length > 0 ? (set({}, path, value) as Partial<Static<Schema>>) : value;
const partial = path.length > 0 ? (set({}, path, value) as Partial<s.Static<Schema>>) : value;
this.throwIfRestricted(partial);
@@ -168,9 +168,12 @@ export class SchemaObject<Schema extends TObject> {
return [partial, newConfig];
}
async overwrite(path: string, value: any): Promise<[Partial<Static<Schema>>, Static<Schema>]> {
async overwrite(
path: string,
value: any,
): Promise<[Partial<s.Static<Schema>>, s.Static<Schema>]> {
const current = this.clone();
const partial = path.length > 0 ? (set({}, path, value) as Partial<Static<Schema>>) : value;
const partial = path.length > 0 ? (set({}, path, value) as Partial<s.Static<Schema>>) : value;
this.throwIfRestricted(partial);
@@ -194,7 +197,7 @@ export class SchemaObject<Schema extends TObject> {
return has(this._config, path);
}
async remove(path: string): Promise<[Partial<Static<Schema>>, Static<Schema>]> {
async remove(path: string): Promise<[Partial<s.Static<Schema>>, s.Static<Schema>]> {
this.throwIfRestricted(path);
if (!this.has(path)) {
@@ -202,9 +205,9 @@ export class SchemaObject<Schema extends TObject> {
}
const current = this.clone();
const removed = get(current, path) as Partial<Static<Schema>>;
const removed = get(current, path) as Partial<s.Static<Schema>>;
const config = omit(current, path);
const newConfig = await this.set(config);
const newConfig = await this.set(config as any);
return [removed, newConfig];
}
}

Some files were not shown because too many files have changed in this diff Show More