@usegraft/sdk-sveltekit 0.2.0
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 +21 -0
- package/README.md +104 -0
- package/dist/index.d.ts +60 -0
- package/dist/index.js +40 -0
- package/package.json +54 -0
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)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { AnyCollection, ReadOptions, Document, ListOptions, SearchOptions, SearchHit, GraftClient, ClientOptions } from '@usegraft/sdk-core';
|
|
2
|
+
export { AnyCollection, ChangeSet, ClientOptions, Document, GraftClient, ListOptions, ReadOptions, SearchHit, SearchOptions, TAG_NAMESPACE, collectionTag, createClient, documentTag, tagsFor, tagsForChanges, toDocument } from '@usegraft/sdk-core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* SvelteKit adapter over @usegraft/sdk-core — the same `getContent` /
|
|
6
|
+
* `listContent` / `searchContent` surface as @usegraft/sdk-next, so a schema
|
|
7
|
+
* types every read identically across frameworks.
|
|
8
|
+
*
|
|
9
|
+
* No request-level memo (sdk-next's React.cache has no SvelteKit
|
|
10
|
+
* equivalent): reads go straight to Postgres from server `load` functions
|
|
11
|
+
* and `+server.ts` endpoints. Server-only by nature (it holds a database
|
|
12
|
+
* handle) — construct it in `$lib/server/` so SvelteKit's server-only
|
|
13
|
+
* enforcement guards it, and never import it into client code.
|
|
14
|
+
*
|
|
15
|
+
* Caching: SvelteKit has no tag-based data cache, so the Phase 4 tag
|
|
16
|
+
* contract maps onto HTTP — stamp `tagsFor(...)` into a CDN surrogate-key
|
|
17
|
+
* header (`Cache-Tag` / `Surrogate-Key`) via `setHeaders` in `load`, and
|
|
18
|
+
* purge `tagsForChanges(branch, changeSet)` from your compile webhook.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
interface Graft<TCollections extends Record<string, AnyCollection>> {
|
|
22
|
+
/** Typed getDocument. */
|
|
23
|
+
getContent<K extends keyof TCollections & string>(collection: K, slug: string, options?: ReadOptions): Promise<Document<TCollections[K]> | null>;
|
|
24
|
+
/** Typed listDocuments. */
|
|
25
|
+
listContent<K extends keyof TCollections & string>(collection: K, options?: ListOptions): Promise<Document<TCollections[K]>[]>;
|
|
26
|
+
/** Typed searchDocuments (full-text, best-ranked first). */
|
|
27
|
+
searchContent<K extends keyof TCollections & string>(collection: K, query: string, options?: SearchOptions): Promise<SearchHit<TCollections[K]>[]>;
|
|
28
|
+
/** The underlying sdk-core client, for anything the helpers don't cover. */
|
|
29
|
+
client: GraftClient<TCollections>;
|
|
30
|
+
}
|
|
31
|
+
declare function createGraft<TCollections extends Record<string, AnyCollection>>(options: ClientOptions<TCollections>): Graft<TCollections>;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Mounting the Graft runtime in SvelteKit — `+server.ts` endpoints are
|
|
35
|
+
* already Web-standard, so the "adapter" is one property access: SvelteKit
|
|
36
|
+
* hands a RequestEvent, the handlers want its `request`.
|
|
37
|
+
*
|
|
38
|
+
* ```ts
|
|
39
|
+
* // src/routes/api/fn/[name]/+server.ts
|
|
40
|
+
* import { createFunctionsHandler } from "@usegraft/core";
|
|
41
|
+
* import { graftRoute } from "@usegraft/sdk-sveltekit";
|
|
42
|
+
* const handler = createFunctionsHandler({ … });
|
|
43
|
+
* export const POST = graftRoute(handler);
|
|
44
|
+
* export const GET = graftRoute(handler); // 405s with Allow + fix
|
|
45
|
+
*
|
|
46
|
+
* // src/routes/api/mcp/+server.ts
|
|
47
|
+
* import { createGraftMcpHandler } from "@usegraft/mcp";
|
|
48
|
+
* export const POST = graftRoute(createGraftMcpHandler({ … }));
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* Typed structurally (`{ request: Request }`) so this package needs no
|
|
52
|
+
* @sveltejs/kit dependency — every RequestEvent satisfies it.
|
|
53
|
+
*/
|
|
54
|
+
type FetchHandler = (request: Request) => Promise<Response>;
|
|
55
|
+
/** A SvelteKit RequestHandler (or anything event-shaped) over a Graft handler. */
|
|
56
|
+
declare function graftRoute(handler: FetchHandler): (event: {
|
|
57
|
+
request: Request;
|
|
58
|
+
}) => Promise<Response>;
|
|
59
|
+
|
|
60
|
+
export { type FetchHandler, type Graft, createGraft, graftRoute };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// src/graft.ts
|
|
2
|
+
import {
|
|
3
|
+
createClient
|
|
4
|
+
} from "@usegraft/sdk-core";
|
|
5
|
+
function createGraft(options) {
|
|
6
|
+
const client = createClient(options);
|
|
7
|
+
return {
|
|
8
|
+
client,
|
|
9
|
+
getContent: (collection, slug, opts) => client.getDocument(collection, slug, opts),
|
|
10
|
+
listContent: (collection, opts) => client.listDocuments(collection, opts),
|
|
11
|
+
searchContent: (collection, query, opts) => client.searchDocuments(collection, query, opts)
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// src/routes.ts
|
|
16
|
+
function graftRoute(handler) {
|
|
17
|
+
return (event) => handler(event.request);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
import {
|
|
22
|
+
createClient as createClient2,
|
|
23
|
+
toDocument,
|
|
24
|
+
collectionTag,
|
|
25
|
+
documentTag,
|
|
26
|
+
tagsFor,
|
|
27
|
+
tagsForChanges,
|
|
28
|
+
TAG_NAMESPACE
|
|
29
|
+
} from "@usegraft/sdk-core";
|
|
30
|
+
export {
|
|
31
|
+
TAG_NAMESPACE,
|
|
32
|
+
collectionTag,
|
|
33
|
+
createClient2 as createClient,
|
|
34
|
+
createGraft,
|
|
35
|
+
documentTag,
|
|
36
|
+
graftRoute,
|
|
37
|
+
tagsFor,
|
|
38
|
+
tagsForChanges,
|
|
39
|
+
toDocument
|
|
40
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@usegraft/sdk-sveltekit",
|
|
3
|
+
"version": "0.2.0",
|
|
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/sdk-core": "0.2.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@usegraft/core": "0.2.0"
|
|
44
|
+
},
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=22.16"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "tsup src/index.ts --format esm --dts --clean",
|
|
50
|
+
"dev": "tsup src/index.ts --format esm --watch",
|
|
51
|
+
"typecheck": "tsc --noEmit",
|
|
52
|
+
"test": "vitest run --passWithNoTests"
|
|
53
|
+
}
|
|
54
|
+
}
|