@noy-db/in-nextjs 0.2.0-pre.8 → 0.3.0-pre.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -26,7 +26,7 @@ See the [main repository](https://github.com/vLannaAi/noy-db#readme) for setup,
26
26
 
27
27
  - Source — [`packages/in-nextjs`](https://github.com/vLannaAi/noy-db/tree/main/packages/in-nextjs)
28
28
  - Issues — [github.com/vLannaAi/noy-db/issues](https://github.com/vLannaAi/noy-db/issues)
29
- - Spec — [`SPEC.md`](https://github.com/vLannaAi/noy-db/blob/main/SPEC.md)
29
+ - Spec — [`SPEC.md`](https://github.com/vLannaAi/noy-db-docs/blob/main/SPEC.md)
30
30
 
31
31
  ## License
32
32
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noy-db/in-nextjs",
3
- "version": "0.2.0-pre.8",
3
+ "version": "0.3.0-pre.1",
4
4
  "description": "Next.js App Router helpers for noy-db — server components, route handlers, client hooks, and cookie-based session. Thin bridge that composes @noy-db/in-react with Next's cookies()/headers() APIs.",
5
5
  "license": "MIT",
6
6
  "author": "vLannaAi <vicio@lanna.ai>",
@@ -17,27 +17,14 @@
17
17
  "sideEffects": false,
18
18
  "exports": {
19
19
  ".": {
20
- "import": {
21
- "types": "./dist/index.d.ts",
22
- "default": "./dist/index.js"
23
- },
24
- "require": {
25
- "types": "./dist/index.d.cts",
26
- "default": "./dist/index.cjs"
27
- }
20
+ "types": "./dist/index.d.ts",
21
+ "default": "./dist/index.js"
28
22
  },
29
23
  "./client": {
30
- "import": {
31
- "types": "./dist/client.d.ts",
32
- "default": "./dist/client.js"
33
- },
34
- "require": {
35
- "types": "./dist/client.d.cts",
36
- "default": "./dist/client.cjs"
37
- }
24
+ "types": "./dist/client.d.ts",
25
+ "default": "./dist/client.js"
38
26
  }
39
27
  },
40
- "main": "./dist/index.cjs",
41
28
  "module": "./dist/index.js",
42
29
  "types": "./dist/index.d.ts",
43
30
  "files": [
@@ -46,13 +33,13 @@
46
33
  "LICENSE"
47
34
  ],
48
35
  "engines": {
49
- "node": ">=18.0.0"
36
+ "node": ">=22.0.0"
50
37
  },
51
38
  "peerDependencies": {
52
39
  "next": "^14.0.0 || ^15.0.0",
53
40
  "react": "^18.0.0 || ^19.0.0",
54
- "@noy-db/hub": "0.2.0-pre.8",
55
- "@noy-db/in-react": "0.2.0-pre.8"
41
+ "@noy-db/hub": "0.3.0-pre.1",
42
+ "@noy-db/in-react": "0.3.0-pre.1"
56
43
  },
57
44
  "peerDependenciesMeta": {
58
45
  "next": {
@@ -66,8 +53,8 @@
66
53
  }
67
54
  },
68
55
  "devDependencies": {
69
- "@noy-db/hub": "0.2.0-pre.8",
70
- "@noy-db/in-react": "0.2.0-pre.8"
56
+ "@noy-db/hub": "0.3.0-pre.1",
57
+ "@noy-db/in-react": "0.3.0-pre.1"
71
58
  },
72
59
  "keywords": [
73
60
  "noy-db",
package/dist/client.cjs DELETED
@@ -1,25 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __copyProps = (to, from, except, desc) => {
7
- if (from && typeof from === "object" || typeof from === "function") {
8
- for (let key of __getOwnPropNames(from))
9
- if (!__hasOwnProp.call(to, key) && key !== except)
10
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
- }
12
- return to;
13
- };
14
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
15
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
16
-
17
- // src/client.ts
18
- var client_exports = {};
19
- module.exports = __toCommonJS(client_exports);
20
- __reExport(client_exports, require("@noy-db/in-react"), module.exports);
21
- // Annotate the CommonJS export names for ESM import in node:
22
- 0 && (module.exports = {
23
- ...require("@noy-db/in-react")
24
- });
25
- //# sourceMappingURL=client.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/client.ts"],"sourcesContent":["/**\n * **@noy-db/in-nextjs/client** — client-component hooks.\n *\n * Re-export of `@noy-db/in-react` so `'use client'` files don't\n * accidentally pull in `next/headers` (which only works in server\n * contexts and breaks the client bundle).\n *\n * Use inside any Next.js client component:\n *\n * ```tsx\n * 'use client'\n * import { useNoydb, useVault, useCollection } from '@noy-db/in-nextjs/client'\n * ```\n *\n * @packageDocumentation\n */\n\nexport * from '@noy-db/in-react'\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA;AAAA;AAiBA,2BAAc,6BAjBd;","names":[]}
package/dist/client.d.cts DELETED
@@ -1 +0,0 @@
1
- export * from '@noy-db/in-react';
package/dist/index.cjs DELETED
@@ -1,161 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/index.ts
31
- var src_exports = {};
32
- __export(src_exports, {
33
- clearSession: () => clearSession,
34
- configureNoydb: () => configureNoydb,
35
- cookieSession: () => cookieSession,
36
- getNoydb: () => getNoydb,
37
- getVault: () => getVault,
38
- resetNoydbConfig: () => resetNoydbConfig,
39
- withNoydb: () => withNoydb,
40
- withVault: () => withVault,
41
- writeSession: () => writeSession
42
- });
43
- module.exports = __toCommonJS(src_exports);
44
- async function defaultCookies() {
45
- const mod = await import("next/headers").catch(() => null);
46
- if (!mod) {
47
- throw new Error(
48
- "[@noy-db/in-nextjs] `next/headers` is unavailable. Supply a custom `cookies` resolver in cookieSession({ cookies: () => \u2026 }) when running outside a Next.js server context."
49
- );
50
- }
51
- return mod.cookies();
52
- }
53
- function cookieSession(options = {}) {
54
- const cookieName = options.cookieName ?? "noydb_session";
55
- const userCookieName = options.userCookieName ?? "noydb_user";
56
- const getJar = options.cookies ?? defaultCookies;
57
- const maxAgeSeconds = options.maxAgeSeconds ?? 3600;
58
- return {
59
- async read() {
60
- const jar = await Promise.resolve(getJar());
61
- const sessionCookie = jar.get(cookieName);
62
- const userCookie = jar.get(userCookieName);
63
- if (!sessionCookie || !userCookie) return null;
64
- return { userId: userCookie.value, sessionToken: sessionCookie.value };
65
- },
66
- async write(value) {
67
- const jar = await Promise.resolve(getJar());
68
- const opts = {
69
- httpOnly: true,
70
- secure: true,
71
- sameSite: "lax",
72
- path: "/",
73
- maxAge: value.maxAgeSeconds ?? maxAgeSeconds
74
- };
75
- jar.set(cookieName, value.sessionToken, opts);
76
- jar.set(userCookieName, value.userId, opts);
77
- },
78
- async clear() {
79
- const jar = await Promise.resolve(getJar());
80
- jar.delete(cookieName);
81
- jar.delete(userCookieName);
82
- }
83
- };
84
- }
85
- var configured = null;
86
- function configureNoydb(options) {
87
- configured = {
88
- factory: options.factory,
89
- session: options.session ?? cookieSession()
90
- };
91
- }
92
- function requireConfig() {
93
- if (!configured) {
94
- throw new Error(
95
- "[@noy-db/in-nextjs] configureNoydb({ factory, session }) must be called before getNoydb()."
96
- );
97
- }
98
- return configured;
99
- }
100
- async function getNoydb() {
101
- const { factory, session } = requireConfig();
102
- const current = await session.read();
103
- return factory(current);
104
- }
105
- async function getVault(db, name) {
106
- return db.openVault(name);
107
- }
108
- function withVault(vaultName, handler) {
109
- return async (request) => {
110
- const db = await getNoydb();
111
- try {
112
- const vault = await db.openVault(vaultName);
113
- return await handler(vault, request);
114
- } finally {
115
- await safeClose(db);
116
- }
117
- };
118
- }
119
- function withNoydb(handler) {
120
- return async (request) => {
121
- const db = await getNoydb();
122
- try {
123
- return await handler(db, request);
124
- } finally {
125
- await safeClose(db);
126
- }
127
- };
128
- }
129
- async function writeSession(value) {
130
- const { session } = requireConfig();
131
- await session.write(value);
132
- }
133
- async function clearSession() {
134
- const { session } = requireConfig();
135
- await session.clear();
136
- }
137
- function resetNoydbConfig() {
138
- configured = null;
139
- }
140
- async function safeClose(db) {
141
- try {
142
- const result = db.close();
143
- if (result && typeof result.then === "function") {
144
- await result;
145
- }
146
- } catch {
147
- }
148
- }
149
- // Annotate the CommonJS export names for ESM import in node:
150
- 0 && (module.exports = {
151
- clearSession,
152
- configureNoydb,
153
- cookieSession,
154
- getNoydb,
155
- getVault,
156
- resetNoydbConfig,
157
- withNoydb,
158
- withVault,
159
- writeSession
160
- });
161
- //# sourceMappingURL=index.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * **@noy-db/in-nextjs** — Next.js App Router helpers for noy-db.\n *\n * Two surfaces:\n *\n * - **Default export** (this file): **server-only** helpers that\n * use Next's `cookies()` and `headers()` APIs. Import from\n * `@noy-db/in-nextjs` inside server components, route handlers,\n * and server actions.\n *\n * - **`@noy-db/in-nextjs/client`**: re-exports of `@noy-db/in-react`\n * hooks for client components. Import from\n * `@noy-db/in-nextjs/client` inside any `'use client'` file.\n *\n * Keeping them in separate entry points means Next's bundler prunes\n * the server helpers out of the client bundle automatically — no\n * manual `'use server'` / `'use client'` annotations needed.\n *\n * ## Cookie-based session\n *\n * The default session store reads an opaque session token from a\n * cookie (`noydb_session` by default) and passes it to the hub. The\n * token IS NOT the vault passphrase — it's a reference token that\n * the hub resolves against its own session table. Tokens rotate on\n * every unlock and invalidate on logout.\n *\n * ```ts\n * // app/invoices/page.tsx (server component)\n * import { getNoydb, getVault } from '@noy-db/in-nextjs'\n *\n * export default async function InvoicesPage() {\n * const db = await getNoydb()\n * const vault = await getVault(db, 'acme')\n * const invoices = await vault.collection('invoices').list()\n * return <InvoicesList items={invoices} />\n * }\n * ```\n *\n * ```ts\n * // app/api/invoices/route.ts (route handler)\n * import { withVault } from '@noy-db/in-nextjs'\n * import { NextResponse } from 'next/server'\n *\n * export const GET = withVault('acme', async (vault) => {\n * const invoices = await vault.collection('invoices').list()\n * return NextResponse.json(invoices)\n * })\n * ```\n *\n * @packageDocumentation\n */\n\nimport type { Noydb, Vault } from '@noy-db/hub'\n\n// ─── Session-store abstraction ─────────────────────────────────────────\n\n/**\n * Pluggable session resolver. Default implementation reads from Next's\n * `cookies()` API; consumers can swap in a header-based or memory-based\n * resolver for testing or custom auth.\n */\nexport interface SessionStore {\n /** Returns the user id + session token for the current request, or null when absent. */\n read(): Promise<{ userId: string; sessionToken: string } | null>\n /** Write a new session token (called on unlock or rotation). */\n write(value: { userId: string; sessionToken: string; maxAgeSeconds?: number }): Promise<void>\n /** Clear the session (called on logout). */\n clear(): Promise<void>\n}\n\n/**\n * Duck-typed `cookies()` return — we don't import from `next/headers`\n * directly so tests don't need Next's runtime.\n */\nexport interface NextCookieJar {\n get(name: string): { name: string; value: string } | undefined\n set(name: string, value: string, options?: { httpOnly?: boolean; secure?: boolean; sameSite?: 'lax' | 'strict' | 'none'; maxAge?: number; path?: string }): void\n delete(name: string): void\n}\n\nexport interface CookieSessionOptions {\n /** How to obtain Next's cookie jar. Default dynamically imports `next/headers`. */\n readonly cookies?: () => Promise<NextCookieJar> | NextCookieJar\n /** Cookie name for the session token. Default `'noydb_session'`. */\n readonly cookieName?: string\n /** Cookie name for the user id. Default `'noydb_user'`. */\n readonly userCookieName?: string\n /** Cookie maxAge in seconds. Default 1 hour. */\n readonly maxAgeSeconds?: number\n}\n\nasync function defaultCookies(): Promise<NextCookieJar> {\n // Dynamic import keeps `next/headers` out of non-Next environments.\n const mod = (await import('next/headers').catch(() => null)) as\n | { cookies: () => Promise<NextCookieJar> | NextCookieJar }\n | null\n if (!mod) {\n throw new Error(\n \"[@noy-db/in-nextjs] `next/headers` is unavailable. Supply a custom `cookies` resolver \" +\n \"in cookieSession({ cookies: () => … }) when running outside a Next.js server context.\",\n )\n }\n return mod.cookies()\n}\n\n/** Build a cookie-backed session store for Next.js server contexts. */\nexport function cookieSession(options: CookieSessionOptions = {}): SessionStore {\n const cookieName = options.cookieName ?? 'noydb_session'\n const userCookieName = options.userCookieName ?? 'noydb_user'\n const getJar = options.cookies ?? defaultCookies\n const maxAgeSeconds = options.maxAgeSeconds ?? 3600\n\n return {\n async read() {\n const jar = await Promise.resolve(getJar())\n const sessionCookie = jar.get(cookieName)\n const userCookie = jar.get(userCookieName)\n if (!sessionCookie || !userCookie) return null\n return { userId: userCookie.value, sessionToken: sessionCookie.value }\n },\n async write(value) {\n const jar = await Promise.resolve(getJar())\n const opts = {\n httpOnly: true,\n secure: true,\n sameSite: 'lax' as const,\n path: '/',\n maxAge: value.maxAgeSeconds ?? maxAgeSeconds,\n }\n jar.set(cookieName, value.sessionToken, opts)\n jar.set(userCookieName, value.userId, opts)\n },\n async clear() {\n const jar = await Promise.resolve(getJar())\n jar.delete(cookieName)\n jar.delete(userCookieName)\n },\n }\n}\n\n// ─── Server helpers ────────────────────────────────────────────────────\n\n/**\n * The Noydb factory contract — consumers call `setNoydbFactory()` at\n * app init with whatever store + auth wiring they prefer. The factory\n * receives the current session and returns an opened Noydb instance.\n */\nexport type NoydbFactory = (session: { userId: string; sessionToken: string } | null) => Promise<Noydb>\n\nlet configured: { factory: NoydbFactory; session: SessionStore } | null = null\n\n/**\n * Configure the Next.js integration. Call once at app init (e.g. in\n * `app/layout.tsx` or a `lib/noydb.ts` module).\n */\nexport function configureNoydb(options: { factory: NoydbFactory; session?: SessionStore }): void {\n configured = {\n factory: options.factory,\n session: options.session ?? cookieSession(),\n }\n}\n\nfunction requireConfig(): NonNullable<typeof configured> {\n if (!configured) {\n throw new Error(\n \"[@noy-db/in-nextjs] configureNoydb({ factory, session }) must be called before getNoydb().\",\n )\n }\n return configured\n}\n\n/**\n * Server-only helper. Reads the current session from cookies, invokes\n * the configured factory, and returns an open `Noydb` instance.\n */\nexport async function getNoydb(): Promise<Noydb> {\n const { factory, session } = requireConfig()\n const current = await session.read()\n return factory(current)\n}\n\n/** Convenience: open a vault by name, returning the `Vault` directly. */\nexport async function getVault(db: Noydb, name: string): Promise<Vault> {\n return db.openVault(name)\n}\n\n/**\n * Route-handler wrapper. Opens the vault once per request and passes\n * it to your handler. The Noydb instance is closed on exit so the\n * session keys don't outlive the response.\n */\nexport function withVault<T extends Request, R>(\n vaultName: string,\n handler: (vault: Vault, request: T) => Promise<R>,\n): (request: T) => Promise<R> {\n return async (request: T) => {\n const db = await getNoydb()\n try {\n const vault = await db.openVault(vaultName)\n return await handler(vault, request)\n } finally {\n await safeClose(db)\n }\n }\n}\n\n/**\n * Route-handler wrapper that does NOT open a vault — use when the\n * handler needs direct `Noydb` access (e.g. managing multiple vaults\n * per request).\n */\nexport function withNoydb<T extends Request, R>(\n handler: (db: Noydb, request: T) => Promise<R>,\n): (request: T) => Promise<R> {\n return async (request: T) => {\n const db = await getNoydb()\n try {\n return await handler(db, request)\n } finally {\n await safeClose(db)\n }\n }\n}\n\n/**\n * Server action helper — write a new session on login. Called from a\n * server action that validated the user's passphrase through\n * `@noy-db/on-*` and received a session token back.\n */\nexport async function writeSession(value: { userId: string; sessionToken: string; maxAgeSeconds?: number }): Promise<void> {\n const { session } = requireConfig()\n await session.write(value)\n}\n\n/** Server action helper — clear the session on logout. */\nexport async function clearSession(): Promise<void> {\n const { session } = requireConfig()\n await session.clear()\n}\n\n/** Introspection — useful for tests. Resets the configured factory. */\nexport function resetNoydbConfig(): void {\n configured = null\n}\n\n/**\n * Defensively close a Noydb instance, tolerating sync + async close\n * signatures without tripping `await-thenable` lint.\n */\nasync function safeClose(db: Noydb): Promise<void> {\n try {\n const result: unknown = db.close()\n if (result && typeof (result as { then?: unknown }).then === 'function') {\n await (result as Promise<unknown>)\n }\n } catch {\n // best-effort — never block the response on teardown\n }\n}\n"],"mappings":"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","names":[]}
package/dist/index.d.cts DELETED
@@ -1,156 +0,0 @@
1
- import { Noydb, Vault } from '@noy-db/hub';
2
-
3
- /**
4
- * **@noy-db/in-nextjs** — Next.js App Router helpers for noy-db.
5
- *
6
- * Two surfaces:
7
- *
8
- * - **Default export** (this file): **server-only** helpers that
9
- * use Next's `cookies()` and `headers()` APIs. Import from
10
- * `@noy-db/in-nextjs` inside server components, route handlers,
11
- * and server actions.
12
- *
13
- * - **`@noy-db/in-nextjs/client`**: re-exports of `@noy-db/in-react`
14
- * hooks for client components. Import from
15
- * `@noy-db/in-nextjs/client` inside any `'use client'` file.
16
- *
17
- * Keeping them in separate entry points means Next's bundler prunes
18
- * the server helpers out of the client bundle automatically — no
19
- * manual `'use server'` / `'use client'` annotations needed.
20
- *
21
- * ## Cookie-based session
22
- *
23
- * The default session store reads an opaque session token from a
24
- * cookie (`noydb_session` by default) and passes it to the hub. The
25
- * token IS NOT the vault passphrase — it's a reference token that
26
- * the hub resolves against its own session table. Tokens rotate on
27
- * every unlock and invalidate on logout.
28
- *
29
- * ```ts
30
- * // app/invoices/page.tsx (server component)
31
- * import { getNoydb, getVault } from '@noy-db/in-nextjs'
32
- *
33
- * export default async function InvoicesPage() {
34
- * const db = await getNoydb()
35
- * const vault = await getVault(db, 'acme')
36
- * const invoices = await vault.collection('invoices').list()
37
- * return <InvoicesList items={invoices} />
38
- * }
39
- * ```
40
- *
41
- * ```ts
42
- * // app/api/invoices/route.ts (route handler)
43
- * import { withVault } from '@noy-db/in-nextjs'
44
- * import { NextResponse } from 'next/server'
45
- *
46
- * export const GET = withVault('acme', async (vault) => {
47
- * const invoices = await vault.collection('invoices').list()
48
- * return NextResponse.json(invoices)
49
- * })
50
- * ```
51
- *
52
- * @packageDocumentation
53
- */
54
-
55
- /**
56
- * Pluggable session resolver. Default implementation reads from Next's
57
- * `cookies()` API; consumers can swap in a header-based or memory-based
58
- * resolver for testing or custom auth.
59
- */
60
- interface SessionStore {
61
- /** Returns the user id + session token for the current request, or null when absent. */
62
- read(): Promise<{
63
- userId: string;
64
- sessionToken: string;
65
- } | null>;
66
- /** Write a new session token (called on unlock or rotation). */
67
- write(value: {
68
- userId: string;
69
- sessionToken: string;
70
- maxAgeSeconds?: number;
71
- }): Promise<void>;
72
- /** Clear the session (called on logout). */
73
- clear(): Promise<void>;
74
- }
75
- /**
76
- * Duck-typed `cookies()` return — we don't import from `next/headers`
77
- * directly so tests don't need Next's runtime.
78
- */
79
- interface NextCookieJar {
80
- get(name: string): {
81
- name: string;
82
- value: string;
83
- } | undefined;
84
- set(name: string, value: string, options?: {
85
- httpOnly?: boolean;
86
- secure?: boolean;
87
- sameSite?: 'lax' | 'strict' | 'none';
88
- maxAge?: number;
89
- path?: string;
90
- }): void;
91
- delete(name: string): void;
92
- }
93
- interface CookieSessionOptions {
94
- /** How to obtain Next's cookie jar. Default dynamically imports `next/headers`. */
95
- readonly cookies?: () => Promise<NextCookieJar> | NextCookieJar;
96
- /** Cookie name for the session token. Default `'noydb_session'`. */
97
- readonly cookieName?: string;
98
- /** Cookie name for the user id. Default `'noydb_user'`. */
99
- readonly userCookieName?: string;
100
- /** Cookie maxAge in seconds. Default 1 hour. */
101
- readonly maxAgeSeconds?: number;
102
- }
103
- /** Build a cookie-backed session store for Next.js server contexts. */
104
- declare function cookieSession(options?: CookieSessionOptions): SessionStore;
105
- /**
106
- * The Noydb factory contract — consumers call `setNoydbFactory()` at
107
- * app init with whatever store + auth wiring they prefer. The factory
108
- * receives the current session and returns an opened Noydb instance.
109
- */
110
- type NoydbFactory = (session: {
111
- userId: string;
112
- sessionToken: string;
113
- } | null) => Promise<Noydb>;
114
- /**
115
- * Configure the Next.js integration. Call once at app init (e.g. in
116
- * `app/layout.tsx` or a `lib/noydb.ts` module).
117
- */
118
- declare function configureNoydb(options: {
119
- factory: NoydbFactory;
120
- session?: SessionStore;
121
- }): void;
122
- /**
123
- * Server-only helper. Reads the current session from cookies, invokes
124
- * the configured factory, and returns an open `Noydb` instance.
125
- */
126
- declare function getNoydb(): Promise<Noydb>;
127
- /** Convenience: open a vault by name, returning the `Vault` directly. */
128
- declare function getVault(db: Noydb, name: string): Promise<Vault>;
129
- /**
130
- * Route-handler wrapper. Opens the vault once per request and passes
131
- * it to your handler. The Noydb instance is closed on exit so the
132
- * session keys don't outlive the response.
133
- */
134
- declare function withVault<T extends Request, R>(vaultName: string, handler: (vault: Vault, request: T) => Promise<R>): (request: T) => Promise<R>;
135
- /**
136
- * Route-handler wrapper that does NOT open a vault — use when the
137
- * handler needs direct `Noydb` access (e.g. managing multiple vaults
138
- * per request).
139
- */
140
- declare function withNoydb<T extends Request, R>(handler: (db: Noydb, request: T) => Promise<R>): (request: T) => Promise<R>;
141
- /**
142
- * Server action helper — write a new session on login. Called from a
143
- * server action that validated the user's passphrase through
144
- * `@noy-db/on-*` and received a session token back.
145
- */
146
- declare function writeSession(value: {
147
- userId: string;
148
- sessionToken: string;
149
- maxAgeSeconds?: number;
150
- }): Promise<void>;
151
- /** Server action helper — clear the session on logout. */
152
- declare function clearSession(): Promise<void>;
153
- /** Introspection — useful for tests. Resets the configured factory. */
154
- declare function resetNoydbConfig(): void;
155
-
156
- export { type CookieSessionOptions, type NextCookieJar, type NoydbFactory, type SessionStore, clearSession, configureNoydb, cookieSession, getNoydb, getVault, resetNoydbConfig, withNoydb, withVault, writeSession };