@usegraft/sdk-astro 0.2.0 → 1.0.0-beta.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 +57 -8
- package/dist/index.d.ts +11 -2
- package/dist/index.js +2 -1
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -12,33 +12,82 @@ npm i @usegraft/sdk-astro
|
|
|
12
12
|
|
|
13
13
|
## Read content
|
|
14
14
|
|
|
15
|
+
Build the handle once in a server-only module, then import it from frontmatter, endpoints, and middleware. Never from a client island: it holds a database handle.
|
|
16
|
+
|
|
15
17
|
```ts
|
|
18
|
+
// src/lib/graft.ts
|
|
19
|
+
import { createDb } from "@usegraft/db";
|
|
16
20
|
import { createGraft } from "@usegraft/sdk-astro";
|
|
17
|
-
import { collections } from "
|
|
21
|
+
import { collections } from "../../graft.config";
|
|
18
22
|
|
|
19
|
-
const graft = createGraft({ db, collections });
|
|
20
|
-
|
|
23
|
+
export const graft = createGraft({ db: createDb(process.env.DATABASE_URL!).db, collections });
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```astro
|
|
27
|
+
---
|
|
28
|
+
// src/pages/[slug].astro
|
|
29
|
+
import { graft } from "../lib/graft";
|
|
30
|
+
|
|
31
|
+
const page = await graft.getContent("pages", Astro.params.slug!);
|
|
32
|
+
---
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Return types come from your `defineCollection` schemas, so a renamed field is a build error rather than a runtime `undefined`.
|
|
36
|
+
|
|
37
|
+
There is no request-level memo here, because `React.cache` has no Astro equivalent. Reads go straight to the index, which is the right default: prerendered pages read at build time, and an SSR page makes a handful of reads.
|
|
38
|
+
|
|
39
|
+
## Read with no database
|
|
40
|
+
|
|
41
|
+
Pass `index` instead of `db` and the same surface reads the SQLite artifact `graft compile` writes.
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { openStaticIndex } from "@usegraft/db";
|
|
45
|
+
|
|
46
|
+
export const graft = createGraft({
|
|
47
|
+
index: await openStaticIndex(".graft/index.db"),
|
|
48
|
+
collections,
|
|
49
|
+
});
|
|
21
50
|
```
|
|
22
51
|
|
|
23
52
|
## Mount the runtime
|
|
24
53
|
|
|
25
|
-
`graftRoute` binds Graft
|
|
54
|
+
`graftRoute` binds a Graft handler to an Astro 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: Astro hands over an `APIContext`, and the handlers want its `request`.
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
// src/pages/api/fn/[name].ts
|
|
58
|
+
import { createFunctionsHandler } from "@usegraft/core";
|
|
59
|
+
import { graftRoute } from "@usegraft/sdk-astro";
|
|
60
|
+
|
|
61
|
+
const handler = createFunctionsHandler({ db, collections, functions });
|
|
62
|
+
|
|
63
|
+
export const POST = graftRoute(handler);
|
|
64
|
+
export const GET = graftRoute(handler); // 405s with Allow and a fix
|
|
65
|
+
```
|
|
26
66
|
|
|
27
67
|
```ts
|
|
28
|
-
// src/pages/api/
|
|
68
|
+
// src/pages/api/mcp.ts
|
|
69
|
+
import { createGraftMcpHandler } from "@usegraft/mcp";
|
|
29
70
|
import { graftRoute } from "@usegraft/sdk-astro";
|
|
30
71
|
|
|
31
|
-
export const
|
|
72
|
+
export const POST = graftRoute(createGraftMcpHandler({ db, collections, functions, actor }));
|
|
32
73
|
```
|
|
33
74
|
|
|
75
|
+
The parameter is typed structurally as `{ request: Request }`, so this package needs no `astro` dependency and every `APIContext` satisfies it.
|
|
76
|
+
|
|
34
77
|
## MDX
|
|
35
78
|
|
|
36
79
|
Bodies come back as authored source. Render them with Astro's own MDX pipeline rather than a runtime evaluator, which is why this package ships no `MdxBody` equivalent.
|
|
37
80
|
|
|
38
81
|
## Cache invalidation
|
|
39
82
|
|
|
40
|
-
|
|
83
|
+
Astro 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 on SSR responses, and purge `tagsForChanges(branch, changeSet)` from your compile webhook.
|
|
84
|
+
|
|
85
|
+
```astro
|
|
86
|
+
---
|
|
87
|
+
Astro.response.headers.set("Cache-Tag", tagsFor(branch, "pages", slug).join(","));
|
|
88
|
+
---
|
|
89
|
+
```
|
|
41
90
|
|
|
42
91
|
---
|
|
43
92
|
|
|
44
|
-
MIT. [Repository](https://github.com/AndersonDesign1/graft) · [Changelog](https://github.com/AndersonDesign1/graft/blob/
|
|
93
|
+
MIT. [Repository](https://github.com/AndersonDesign1/graft) · [Changelog](https://github.com/AndersonDesign1/graft/blob/main/packages/sdk-astro/CHANGELOG.md)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { DbClientOptions } from '@usegraft/sdk-core/db';
|
|
1
2
|
import { AnyCollection, ReadOptions, Document, ListOptions, SearchOptions, SearchHit, GraftClient, ClientOptions } from '@usegraft/sdk-core';
|
|
2
3
|
export { AnyCollection, ChangeSet, ClientOptions, Document, GraftClient, ListOptions, ReadOptions, SearchHit, SearchOptions, TAG_NAMESPACE, collectionTag, createClient, documentTag, tagsFor, tagsForChanges, toDocument } from '@usegraft/sdk-core';
|
|
3
4
|
|
|
@@ -29,7 +30,15 @@ interface Graft<TCollections extends Record<string, AnyCollection>> {
|
|
|
29
30
|
/** The underlying sdk-core client, for anything the helpers don't cover. */
|
|
30
31
|
client: GraftClient<TCollections>;
|
|
31
32
|
}
|
|
32
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Either an index this adapter reads through, or a Postgres handle it builds
|
|
35
|
+
* the reader for. Two shapes rather than one optional field so that neither can
|
|
36
|
+
* be omitted: `createClient` no longer accepts `db`, because taking it there
|
|
37
|
+
* meant every consumer of `@usegraft/sdk-core` — the browser client included —
|
|
38
|
+
* carried a Postgres driver in its dependency graph.
|
|
39
|
+
*/
|
|
40
|
+
type GraftOptions<TCollections extends Record<string, AnyCollection>> = ClientOptions<TCollections> | DbClientOptions<TCollections>;
|
|
41
|
+
declare function createGraft<TCollections extends Record<string, AnyCollection>>(options: GraftOptions<TCollections>): Graft<TCollections>;
|
|
33
42
|
|
|
34
43
|
/**
|
|
35
44
|
* Mounting the Graft runtime in Astro — endpoints are already Web-standard,
|
|
@@ -58,4 +67,4 @@ declare function graftRoute(handler: FetchHandler): (context: {
|
|
|
58
67
|
request: Request;
|
|
59
68
|
}) => Promise<Response>;
|
|
60
69
|
|
|
61
|
-
export { type FetchHandler, type Graft, createGraft, graftRoute };
|
|
70
|
+
export { type FetchHandler, type Graft, type GraftOptions, createGraft, graftRoute };
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
// src/graft.ts
|
|
2
|
+
import { createDbClient } from "@usegraft/sdk-core/db";
|
|
2
3
|
import {
|
|
3
4
|
createClient
|
|
4
5
|
} from "@usegraft/sdk-core";
|
|
5
6
|
function createGraft(options) {
|
|
6
|
-
const client = createClient(options);
|
|
7
|
+
const client = "db" in options ? createDbClient(options) : createClient(options);
|
|
7
8
|
return {
|
|
8
9
|
client,
|
|
9
10
|
getContent: (collection, slug, opts) => client.getDocument(collection, slug, opts),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@usegraft/sdk-astro",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0-beta.1",
|
|
4
4
|
"description": "Astro adapter: typed content reads and endpoint mounts over Graft's stateless handlers.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agent",
|
|
@@ -38,10 +38,11 @@
|
|
|
38
38
|
"access": "public"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@usegraft/
|
|
41
|
+
"@usegraft/db": "1.0.0-beta.1",
|
|
42
|
+
"@usegraft/sdk-core": "1.0.0-beta.1"
|
|
42
43
|
},
|
|
43
44
|
"devDependencies": {
|
|
44
|
-
"@usegraft/core": "0.
|
|
45
|
+
"@usegraft/core": "1.0.0-beta.1"
|
|
45
46
|
},
|
|
46
47
|
"engines": {
|
|
47
48
|
"node": ">=22.16"
|