diff --git a/app/build.ts b/app/build.ts index ab66688..faa81d0 100644 --- a/app/build.ts +++ b/app/build.ts @@ -179,3 +179,8 @@ await tsup.build({ platform: "node", format: ["esm", "cjs"] }); + +await tsup.build({ + ...baseConfig("astro"), + format: ["esm", "cjs"] +}); diff --git a/app/package.json b/app/package.json index 2a1ae80..e024279 100644 --- a/app/package.json +++ b/app/package.json @@ -160,6 +160,11 @@ "import": "./dist/adapter/node/index.js", "require": "./dist/adapter/node/index.cjs" }, + "./adapter/astro": { + "types": "./dist/adapter/astro/index.d.ts", + "import": "./dist/adapter/astro/index.js", + "require": "./dist/adapter/astro/index.cjs" + }, "./dist/styles.css": "./dist/ui/main.css", "./dist/manifest.json": "./dist/static/manifest.json" }, diff --git a/app/src/adapter/astro/astro.adapter.ts b/app/src/adapter/astro/astro.adapter.ts new file mode 100644 index 0000000..6076750 --- /dev/null +++ b/app/src/adapter/astro/astro.adapter.ts @@ -0,0 +1,21 @@ +import { Api, type ApiOptions } from "bknd"; + +type TAstro = { + request: { + url: string; + headers: Headers; + }; +}; + +export type Options = { + mode?: "static" | "dynamic"; +} & Omit & { + host?: string; + }; + +export function getApi(Astro: TAstro, options: Options = { mode: "static" }) { + return new Api({ + host: new URL(Astro.request.url).origin, + headers: options.mode === "dynamic" ? Astro.request.headers : undefined + }); +} diff --git a/app/src/adapter/astro/index.ts b/app/src/adapter/astro/index.ts new file mode 100644 index 0000000..d5010a5 --- /dev/null +++ b/app/src/adapter/astro/index.ts @@ -0,0 +1 @@ +export * from "./astro.adapter"; diff --git a/app/src/ui/modules/data/components/EntityForm.tsx b/app/src/ui/modules/data/components/EntityForm.tsx index 4219547..40147b0 100644 --- a/app/src/ui/modules/data/components/EntityForm.tsx +++ b/app/src/ui/modules/data/components/EntityForm.tsx @@ -135,10 +135,10 @@ type FormInputElement = HTMLInputElement | HTMLTextAreaElement; function EntityFormField({ fieldApi, field, action, data, ...props }: EntityFormFieldProps) { const handleUpdate = useEvent((e: React.ChangeEvent | any) => { if (typeof e === "object" && "target" in e) { - console.log("handleUpdate", e.target.value); + //console.log("handleUpdate", e.target.value); fieldApi.handleChange(e.target.value); } else { - console.log("handleUpdate-", e); + //console.log("handleUpdate-", e); fieldApi.handleChange(e); } }); diff --git a/bun.lockb b/bun.lockb index 50b05d6..5ea65b5 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/examples/astro/src/components/Card.astro b/examples/astro/src/components/Card.astro new file mode 100644 index 0000000..b29aa31 --- /dev/null +++ b/examples/astro/src/components/Card.astro @@ -0,0 +1,73 @@ +--- +interface Props { + title: string; + body: string; + done?: boolean; +} + +const { done, title, body } = Astro.props; +--- + + + diff --git a/examples/astro/src/layouts/Layout.astro b/examples/astro/src/layouts/Layout.astro new file mode 100644 index 0000000..32f2cc1 --- /dev/null +++ b/examples/astro/src/layouts/Layout.astro @@ -0,0 +1,137 @@ +--- +import Card from "../components/Card.astro"; +interface Props { + title: string; +} + +const { title } = Astro.props; +const path = new URL(Astro.request.url).pathname; +const items = [ + { href: "/", text: "Home (static)" }, + { href: "/ssr", text: "SSR (with auth)" }, + { href: "/admin", text: "Admin" } +]; +--- + + + + + + + + + + {title} + + + +
+
+

bknd + Astro

+ +
+ + +
+ + + diff --git a/examples/astro/src/pages/admin/[...admin].astro b/examples/astro/src/pages/admin/[...admin].astro index 50d5d2e..0f5d0bf 100644 --- a/examples/astro/src/pages/admin/[...admin].astro +++ b/examples/astro/src/pages/admin/[...admin].astro @@ -1,21 +1,20 @@ --- -import { Api } from "bknd"; import { Admin } from "bknd/ui"; import "bknd/dist/styles.css"; -export const prerender = false; -const api = new Api({ - host: new URL(Astro.request.url).origin, - headers: Astro.request.headers -}); +import { getApi } from "bknd/adapter/astro"; + +const api = getApi(Astro, { mode: "dynamic" }); const user = api.getUser(); + +export const prerender = false; --- diff --git a/examples/astro/src/pages/index.astro b/examples/astro/src/pages/index.astro index 2693802..4c2f31b 100644 --- a/examples/astro/src/pages/index.astro +++ b/examples/astro/src/pages/index.astro @@ -1,16 +1,29 @@ --- -import { Api } from "bknd"; -const api = new Api({ - host: new URL(Astro.request.url).origin -}); +import { getApi } from "bknd/adapter/astro"; +import Card from "../components/Card.astro"; +import Layout from "../layouts/Layout.astro"; +const api = getApi(Astro); const { data } = await api.data.readMany("todos"); --- + +

Static Rendering

+ +
-

Home (static)

-

Data

-
{JSON.stringify(data, null, 2)}
- -Admin - -SSR (with auth) - -Home (static) \ No newline at end of file + diff --git a/examples/astro/src/pages/ssr.astro b/examples/astro/src/pages/ssr.astro index a45dafe..eb3a8aa 100644 --- a/examples/astro/src/pages/ssr.astro +++ b/examples/astro/src/pages/ssr.astro @@ -1,23 +1,35 @@ --- -import { Api } from "bknd"; -const api = new Api({ - host: new URL(Astro.request.url).origin, - headers: Astro.request.headers -}); +import { getApi } from "bknd/adapter/astro"; +import Card from "../components/Card.astro"; +import Layout from "../layouts/Layout.astro"; +const api = getApi(Astro, { mode: "dynamic" }); const { data } = await api.data.readMany("todos"); const user = api.getUser(); -console.log("user", user); export const prerender = false; --- -

SSR

-

Data

-
{JSON.stringify(data, null, 2)}
+ +

Server Side Rendering

+ +
+ {user ?

Logged in as {user?.email}. Logout

:

Not authenticated. Sign in

} +
+
-

User

-
{JSON.stringify(user, null, 2)}
- -Admin - -SSR (with auth) - -Home (static) \ No newline at end of file +