@usegraft/sdk-sveltekit 0.0.0-canary-20260901211203

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Anderson Joseph
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # @usegraft/sdk-sveltekit
2
+
3
+ > SvelteKit adapter: typed content reads and endpoint mounts over the same handlers every other surface serves.
4
+
5
+ Part of [Graft](https://github.com/AndersonDesign1/graft), a CMS built so an AI agent is the primary operator.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm i @usegraft/sdk-sveltekit
11
+ ```
12
+
13
+ ## Read content
14
+
15
+ Build the handle in `$lib/server/`, so SvelteKit's server-only enforcement guards it. This client holds a database handle and must never reach the browser.
16
+
17
+ ```ts
18
+ // src/lib/server/graft.ts
19
+ import { DATABASE_URL } from "$env/static/private";
20
+ import { createDb } from "@usegraft/db";
21
+ import { createGraft } from "@usegraft/sdk-sveltekit";
22
+ import { collections } from "../../../graft.config";
23
+
24
+ export const graft = createGraft({ db: createDb(DATABASE_URL).db, collections });
25
+ ```
26
+
27
+ ```ts
28
+ // src/routes/[slug]/+page.server.ts
29
+ import { error } from "@sveltejs/kit";
30
+ import { graft } from "$lib/server/graft";
31
+ import type { PageServerLoad } from "./$types";
32
+
33
+ export const load: PageServerLoad = async ({ params }) => {
34
+ const page = await graft.getContent("pages", params.slug);
35
+ if (!page) error(404);
36
+ return { page };
37
+ };
38
+ ```
39
+
40
+ Return types come from your `defineCollection` schemas, so a renamed field is a build error rather than a runtime `undefined`.
41
+
42
+ There is no request-level memo here, because `React.cache` has no SvelteKit equivalent. Reads go straight to the index from server `load` functions and `+server.ts` endpoints.
43
+
44
+ ## Read with no database
45
+
46
+ Pass `index` instead of `db` and the same surface reads the SQLite artifact `graft compile` writes.
47
+
48
+ ```ts
49
+ import { openStaticIndex } from "@usegraft/db";
50
+
51
+ export const graft = createGraft({
52
+ index: await openStaticIndex(".graft/index.db"),
53
+ collections,
54
+ });
55
+ ```
56
+
57
+ ## Mount the runtime
58
+
59
+ `graftRoute` binds a Graft handler to a `+server.ts` endpoint, so typed functions and MCP are served from your own app rather than a separate process. It takes the handler, not a config object: SvelteKit hands over a `RequestEvent`, and the handlers want its `request`.
60
+
61
+ ```ts
62
+ // src/routes/api/fn/[name]/+server.ts
63
+ import { createFunctionsHandler } from "@usegraft/core";
64
+ import { graftRoute } from "@usegraft/sdk-sveltekit";
65
+
66
+ const handler = createFunctionsHandler({ db, collections, functions });
67
+
68
+ export const POST = graftRoute(handler);
69
+ export const GET = graftRoute(handler); // 405s with Allow and a fix
70
+ ```
71
+
72
+ ```ts
73
+ // src/routes/api/mcp/+server.ts
74
+ import { createGraftMcpHandler } from "@usegraft/mcp";
75
+ import { graftRoute } from "@usegraft/sdk-sveltekit";
76
+
77
+ export const POST = graftRoute(createGraftMcpHandler({ db, collections, functions, actor }));
78
+ ```
79
+
80
+ The parameter is typed structurally as `{ request: Request }`, so this package needs no `@sveltejs/kit` dependency and every `RequestEvent` satisfies it.
81
+
82
+ ## MDX
83
+
84
+ Bodies come back as authored source. Render them with mdsvex or your own pipeline rather than a runtime evaluator, which is why this package ships no `MdxBody` equivalent.
85
+
86
+ ## Cache invalidation
87
+
88
+ SvelteKit has no tag-based data cache, so the [`@usegraft/sdk-core`](https://www.npmjs.com/package/@usegraft/sdk-core) tag contract maps onto HTTP. Stamp `tagsFor(...)` into a CDN surrogate-key header with `setHeaders` in `load`, and purge `tagsForChanges(branch, changeSet)` from your compile webhook.
89
+
90
+ ```ts
91
+ export const load: PageServerLoad = async ({ params, setHeaders }) => {
92
+ const page = await graft.getContent("pages", params.slug);
93
+ setHeaders({ "Cache-Tag": tagsFor("main", "pages", params.slug).join(",") });
94
+ return { page };
95
+ };
96
+ ```
97
+
98
+ ## Bundling
99
+
100
+ `node:sqlite` backs the static index and must stay external to the client bundle. Keeping the handle in `$lib/server/` is what guarantees that.
101
+
102
+ ---
103
+
104
+ MIT. [Repository](https://github.com/AndersonDesign1/graft) · [Changelog](https://github.com/AndersonDesign1/graft/blob/main/packages/sdk-sveltekit/CHANGELOG.md)
@@ -0,0 +1,69 @@
1
+ import { DbClientOptions } from '@usegraft/sdk-core/db';
2
+ import { AnyCollection, ReadOptions, Document, ListOptions, SearchOptions, SearchHit, GraftClient, ClientOptions } from '@usegraft/sdk-core';
3
+ export { AnyCollection, ChangeSet, ClientOptions, Document, GraftClient, ListOptions, ReadOptions, SearchHit, SearchOptions, TAG_NAMESPACE, collectionTag, createClient, documentTag, tagsFor, tagsForChanges, toDocument } from '@usegraft/sdk-core';
4
+
5
+ /**
6
+ * SvelteKit adapter over @usegraft/sdk-core — the same `getContent` /
7
+ * `listContent` / `searchContent` surface as @usegraft/sdk-next, so a schema
8
+ * types every read identically across frameworks.
9
+ *
10
+ * No request-level memo (sdk-next's React.cache has no SvelteKit
11
+ * equivalent): reads go straight to Postgres from server `load` functions
12
+ * and `+server.ts` endpoints. Server-only by nature (it holds a database
13
+ * handle) — construct it in `$lib/server/` so SvelteKit's server-only
14
+ * enforcement guards it, and never import it into client code.
15
+ *
16
+ * Caching: SvelteKit has no tag-based data cache, so the Phase 4 tag
17
+ * contract maps onto HTTP — stamp `tagsFor(...)` into a CDN surrogate-key
18
+ * header (`Cache-Tag` / `Surrogate-Key`) via `setHeaders` in `load`, and
19
+ * purge `tagsForChanges(branch, changeSet)` from your compile webhook.
20
+ */
21
+
22
+ interface Graft<TCollections extends Record<string, AnyCollection>> {
23
+ /** Typed getDocument. */
24
+ getContent<K extends keyof TCollections & string>(collection: K, slug: string, options?: ReadOptions): Promise<Document<TCollections[K]> | null>;
25
+ /** Typed listDocuments. */
26
+ listContent<K extends keyof TCollections & string>(collection: K, options?: ListOptions): Promise<Document<TCollections[K]>[]>;
27
+ /** Typed searchDocuments (full-text, best-ranked first). */
28
+ searchContent<K extends keyof TCollections & string>(collection: K, query: string, options?: SearchOptions): Promise<SearchHit<TCollections[K]>[]>;
29
+ /** The underlying sdk-core client, for anything the helpers don't cover. */
30
+ client: GraftClient<TCollections>;
31
+ }
32
+ /**
33
+ * Either an index this adapter reads through, or a Postgres handle it builds
34
+ * the reader for. Two shapes rather than one optional field so that neither can
35
+ * be omitted: `createClient` no longer accepts `db`, because taking it there
36
+ * meant every consumer of `@usegraft/sdk-core` — the browser client included —
37
+ * carried a Postgres driver in its dependency graph.
38
+ */
39
+ type GraftOptions<TCollections extends Record<string, AnyCollection>> = ClientOptions<TCollections> | DbClientOptions<TCollections>;
40
+ declare function createGraft<TCollections extends Record<string, AnyCollection>>(options: GraftOptions<TCollections>): Graft<TCollections>;
41
+
42
+ /**
43
+ * Mounting the Graft runtime in SvelteKit — `+server.ts` endpoints are
44
+ * already Web-standard, so the "adapter" is one property access: SvelteKit
45
+ * hands a RequestEvent, the handlers want its `request`.
46
+ *
47
+ * ```ts
48
+ * // src/routes/api/fn/[name]/+server.ts
49
+ * import { createFunctionsHandler } from "@usegraft/core";
50
+ * import { graftRoute } from "@usegraft/sdk-sveltekit";
51
+ * const handler = createFunctionsHandler({ … });
52
+ * export const POST = graftRoute(handler);
53
+ * export const GET = graftRoute(handler); // 405s with Allow + fix
54
+ *
55
+ * // src/routes/api/mcp/+server.ts
56
+ * import { createGraftMcpHandler } from "@usegraft/mcp";
57
+ * export const POST = graftRoute(createGraftMcpHandler({ … }));
58
+ * ```
59
+ *
60
+ * Typed structurally (`{ request: Request }`) so this package needs no
61
+ * @sveltejs/kit dependency — every RequestEvent satisfies it.
62
+ */
63
+ type FetchHandler = (request: Request) => Promise<Response>;
64
+ /** A SvelteKit RequestHandler (or anything event-shaped) over a Graft handler. */
65
+ declare function graftRoute(handler: FetchHandler): (event: {
66
+ request: Request;
67
+ }) => Promise<Response>;
68
+
69
+ export { type FetchHandler, type Graft, type GraftOptions, createGraft, graftRoute };
package/dist/index.js ADDED
@@ -0,0 +1,41 @@
1
+ // src/graft.ts
2
+ import { createDbClient } from "@usegraft/sdk-core/db";
3
+ import {
4
+ createClient
5
+ } from "@usegraft/sdk-core";
6
+ function createGraft(options) {
7
+ const client = "db" in options ? createDbClient(options) : createClient(options);
8
+ return {
9
+ client,
10
+ getContent: (collection, slug, opts) => client.getDocument(collection, slug, opts),
11
+ listContent: (collection, opts) => client.listDocuments(collection, opts),
12
+ searchContent: (collection, query, opts) => client.searchDocuments(collection, query, opts)
13
+ };
14
+ }
15
+
16
+ // src/routes.ts
17
+ function graftRoute(handler) {
18
+ return (event) => handler(event.request);
19
+ }
20
+
21
+ // src/index.ts
22
+ import {
23
+ createClient as createClient2,
24
+ toDocument,
25
+ collectionTag,
26
+ documentTag,
27
+ tagsFor,
28
+ tagsForChanges,
29
+ TAG_NAMESPACE
30
+ } from "@usegraft/sdk-core";
31
+ export {
32
+ TAG_NAMESPACE,
33
+ collectionTag,
34
+ createClient2 as createClient,
35
+ createGraft,
36
+ documentTag,
37
+ graftRoute,
38
+ tagsFor,
39
+ tagsForChanges,
40
+ toDocument
41
+ };
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@usegraft/sdk-sveltekit",
3
+ "version": "0.0.0-canary-20260901211203",
4
+ "description": "SvelteKit adapter: typed content reads and endpoint mounts over Graft's stateless handlers.",
5
+ "keywords": [
6
+ "agent",
7
+ "ai",
8
+ "cms",
9
+ "graft",
10
+ "headless-cms",
11
+ "mcp",
12
+ "svelte",
13
+ "sveltekit",
14
+ "typescript"
15
+ ],
16
+ "homepage": "https://github.com/AndersonDesign1/graft#readme",
17
+ "license": "MIT",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/AndersonDesign1/graft.git",
21
+ "directory": "packages/sdk-sveltekit"
22
+ },
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "type": "module",
27
+ "main": "./dist/index.js",
28
+ "module": "./dist/index.js",
29
+ "types": "./dist/index.d.ts",
30
+ "exports": {
31
+ ".": {
32
+ "types": "./dist/index.d.ts",
33
+ "import": "./dist/index.js"
34
+ }
35
+ },
36
+ "publishConfig": {
37
+ "access": "public"
38
+ },
39
+ "dependencies": {
40
+ "@usegraft/db": "0.0.0-canary-20260901211203",
41
+ "@usegraft/sdk-core": "0.0.0-canary-20260901211203"
42
+ },
43
+ "devDependencies": {
44
+ "@usegraft/core": "0.0.0-canary-20260901211203"
45
+ },
46
+ "engines": {
47
+ "node": ">=22.16"
48
+ },
49
+ "scripts": {
50
+ "build": "tsup src/index.ts --format esm --dts --clean",
51
+ "dev": "tsup src/index.ts --format esm --watch",
52
+ "typecheck": "tsc --noEmit",
53
+ "test": "vitest run --passWithNoTests"
54
+ }
55
+ }