@usegraft/sdk-next 0.1.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/dist/index.d.ts +55 -0
- package/dist/index.js +85 -0
- package/dist/next-config.d.ts +19 -0
- package/dist/next-config.js +15 -0
- package/package.json +57 -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/dist/index.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { AnyCollection, ReadOptions, Document, ListOptions, SearchOptions, SearchHit, GraftClient, ClientOptions, ChangeSet } 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
|
+
import { MDXComponents } from 'mdx/types';
|
|
4
|
+
import { ReactElement } from 'react';
|
|
5
|
+
|
|
6
|
+
interface Graft<TCollections extends Record<string, AnyCollection>> {
|
|
7
|
+
/** Request-deduped getDocument. */
|
|
8
|
+
getContent<K extends keyof TCollections & string>(collection: K, slug: string, options?: ReadOptions): Promise<Document<TCollections[K]> | null>;
|
|
9
|
+
/** Request-deduped listDocuments. */
|
|
10
|
+
listContent<K extends keyof TCollections & string>(collection: K, options?: ListOptions): Promise<Document<TCollections[K]>[]>;
|
|
11
|
+
/** Request-deduped searchDocuments (full-text, best-ranked first). */
|
|
12
|
+
searchContent<K extends keyof TCollections & string>(collection: K, query: string, options?: SearchOptions): Promise<SearchHit<TCollections[K]>[]>;
|
|
13
|
+
/** The underlying sdk-core client, for anything the helpers don't cover. */
|
|
14
|
+
client: GraftClient<TCollections>;
|
|
15
|
+
}
|
|
16
|
+
declare function createGraft<TCollections extends Record<string, AnyCollection>>(options: ClientOptions<TCollections>): Graft<TCollections>;
|
|
17
|
+
/** How long a background-revalidated tag may keep serving stale before it hard-expires. */
|
|
18
|
+
type RevalidateProfile = string | {
|
|
19
|
+
expire?: number;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Background-invalidate the Data Cache for everything a compile changed, on
|
|
23
|
+
* `branch`. Call it from a **route handler** (a compile webhook): the next
|
|
24
|
+
* request triggers a background refresh (stale-while-revalidate). Refreshes
|
|
25
|
+
* only the changed pages — per-doc + per-collection tags — and returns them.
|
|
26
|
+
*
|
|
27
|
+
* `profile` is Next 16's required cache-life argument to `revalidateTag`
|
|
28
|
+
* (a built-in name like `"max"`/`"hours"` or `{ expire }`); defaults to `"max"`.
|
|
29
|
+
* A no-op unless the reads were cached with `'use cache'` + `cacheTag`, but
|
|
30
|
+
* always safe to call.
|
|
31
|
+
*/
|
|
32
|
+
declare function revalidateContent(branch: string, changes: ChangeSet, profile?: RevalidateProfile): string[];
|
|
33
|
+
/**
|
|
34
|
+
* Immediately invalidate the Data Cache for a compile's changes, with
|
|
35
|
+
* read-your-own-writes semantics. Call it from a **Server Action** (e.g. an
|
|
36
|
+
* in-app "publish" that compiles then updates): the same request sees fresh
|
|
37
|
+
* content. Returns the tags it hit. Like `revalidateContent`, a no-op unless
|
|
38
|
+
* the reads were cached with `'use cache'` + `cacheTag`.
|
|
39
|
+
*/
|
|
40
|
+
declare function updateContent(branch: string, changes: ChangeSet): string[];
|
|
41
|
+
|
|
42
|
+
type MdxComponents = MDXComponents;
|
|
43
|
+
interface MdxBodyProps {
|
|
44
|
+
/** Raw MDX body (the string stored on the document / content_index row). */
|
|
45
|
+
source: string;
|
|
46
|
+
/** Component map — typically the generated mdx-components export. */
|
|
47
|
+
components?: MdxComponents;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Compile + run an MDX source string and render it with the given components.
|
|
51
|
+
* Empty source renders nothing (valid for body-less docs).
|
|
52
|
+
*/
|
|
53
|
+
declare function MdxBody({ source, components }: MdxBodyProps): Promise<ReactElement | null>;
|
|
54
|
+
|
|
55
|
+
export { type Graft, MdxBody, type MdxBodyProps, type MdxComponents, type RevalidateProfile, createGraft, revalidateContent, updateContent };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// src/graft.ts
|
|
2
|
+
import { revalidateTag, updateTag } from "next/cache";
|
|
3
|
+
import { cache } from "react";
|
|
4
|
+
import {
|
|
5
|
+
createClient,
|
|
6
|
+
tagsForChanges
|
|
7
|
+
} from "@usegraft/sdk-core";
|
|
8
|
+
function createGraft(options) {
|
|
9
|
+
const client = createClient(options);
|
|
10
|
+
const cachedGet = cache(
|
|
11
|
+
(collection, slug, branch) => client.getDocument(
|
|
12
|
+
collection,
|
|
13
|
+
slug,
|
|
14
|
+
branch === void 0 ? void 0 : { branch }
|
|
15
|
+
)
|
|
16
|
+
);
|
|
17
|
+
const cachedList = cache(
|
|
18
|
+
(collection, branch, limit, offset) => client.listDocuments(collection, { branch, limit, offset })
|
|
19
|
+
);
|
|
20
|
+
const cachedSearch = cache(
|
|
21
|
+
(collection, query, branch, limit) => client.searchDocuments(collection, query, { branch, limit })
|
|
22
|
+
);
|
|
23
|
+
return {
|
|
24
|
+
client,
|
|
25
|
+
getContent: (collection, slug, opts) => cachedGet(collection, slug, opts?.branch),
|
|
26
|
+
listContent: (collection, opts) => cachedList(collection, opts?.branch, opts?.limit, opts?.offset),
|
|
27
|
+
searchContent: (collection, query, opts) => cachedSearch(collection, query, opts?.branch, opts?.limit)
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function revalidateContent(branch, changes, profile = "max") {
|
|
31
|
+
const tags = tagsForChanges(branch, changes);
|
|
32
|
+
for (const tag of tags) revalidateTag(tag, profile);
|
|
33
|
+
return tags;
|
|
34
|
+
}
|
|
35
|
+
function updateContent(branch, changes) {
|
|
36
|
+
const tags = tagsForChanges(branch, changes);
|
|
37
|
+
for (const tag of tags) updateTag(tag);
|
|
38
|
+
return tags;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/mdx.tsx
|
|
42
|
+
import { compile, run } from "@mdx-js/mdx";
|
|
43
|
+
import * as runtime from "react/jsx-runtime";
|
|
44
|
+
import remarkGfm from "remark-gfm";
|
|
45
|
+
import { jsx } from "react/jsx-runtime";
|
|
46
|
+
async function MdxBody({ source, components }) {
|
|
47
|
+
const trimmed = source.trim();
|
|
48
|
+
if (trimmed.length === 0) return null;
|
|
49
|
+
const compiled = String(
|
|
50
|
+
await compile(trimmed, {
|
|
51
|
+
outputFormat: "function-body",
|
|
52
|
+
remarkPlugins: [remarkGfm],
|
|
53
|
+
development: process.env.NODE_ENV !== "production"
|
|
54
|
+
})
|
|
55
|
+
);
|
|
56
|
+
const { default: Content } = await run(compiled, {
|
|
57
|
+
...runtime,
|
|
58
|
+
baseUrl: import.meta.url
|
|
59
|
+
});
|
|
60
|
+
return /* @__PURE__ */ jsx(Content, { components: components ?? {} });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// src/index.ts
|
|
64
|
+
import {
|
|
65
|
+
createClient as createClient2,
|
|
66
|
+
toDocument,
|
|
67
|
+
collectionTag,
|
|
68
|
+
documentTag,
|
|
69
|
+
tagsFor,
|
|
70
|
+
tagsForChanges as tagsForChanges2,
|
|
71
|
+
TAG_NAMESPACE
|
|
72
|
+
} from "@usegraft/sdk-core";
|
|
73
|
+
export {
|
|
74
|
+
MdxBody,
|
|
75
|
+
TAG_NAMESPACE,
|
|
76
|
+
collectionTag,
|
|
77
|
+
createClient2 as createClient,
|
|
78
|
+
createGraft,
|
|
79
|
+
documentTag,
|
|
80
|
+
revalidateContent,
|
|
81
|
+
tagsFor,
|
|
82
|
+
tagsForChanges2 as tagsForChanges,
|
|
83
|
+
toDocument,
|
|
84
|
+
updateContent
|
|
85
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { NextConfig } from 'next';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* withGraft — wrap a Next config with everything a Graft app needs from the
|
|
5
|
+
* bundler, so consuming apps can't forget it.
|
|
6
|
+
*
|
|
7
|
+
* Today that is one thing: `@usegraft/registry` reads its bundled primitives from
|
|
8
|
+
* disk at runtime (registryRoot() → the package's registry/ dir in
|
|
9
|
+
* node_modules), so it must stay server-external — bundling it breaks
|
|
10
|
+
* list_registry / describe_item at runtime with no build-time error. Future
|
|
11
|
+
* Graft-wide Next requirements land here instead of in every app's config.
|
|
12
|
+
*
|
|
13
|
+
* Shipped as its own `@usegraft/sdk-next/config` entry: next.config.ts is loaded
|
|
14
|
+
* with require semantics and must not drag the React/MDX runtime surface in.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
declare function withGraft(config?: NextConfig): NextConfig;
|
|
18
|
+
|
|
19
|
+
export { withGraft };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// src/next-config.ts
|
|
2
|
+
var GRAFT_SERVER_EXTERNALS = ["@usegraft/registry"];
|
|
3
|
+
function withGraft(config = {}) {
|
|
4
|
+
const existing = config.serverExternalPackages ?? [];
|
|
5
|
+
return {
|
|
6
|
+
...config,
|
|
7
|
+
serverExternalPackages: [
|
|
8
|
+
...existing,
|
|
9
|
+
...GRAFT_SERVER_EXTERNALS.filter((pkg) => !existing.includes(pkg))
|
|
10
|
+
]
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export {
|
|
14
|
+
withGraft
|
|
15
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@usegraft/sdk-next",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/AndersonDesign1/graft.git",
|
|
8
|
+
"directory": "packages/sdk-next"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"module": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./config": {
|
|
23
|
+
"types": "./dist/next-config.d.ts",
|
|
24
|
+
"import": "./dist/next-config.js",
|
|
25
|
+
"default": "./dist/next-config.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@mdx-js/mdx": "^3.1.1",
|
|
33
|
+
"remark-gfm": "^4.0.1",
|
|
34
|
+
"@usegraft/sdk-core": "0.1.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/mdx": "^2.0.14",
|
|
38
|
+
"@types/react": "^19.0.0",
|
|
39
|
+
"next": "^16.0.0",
|
|
40
|
+
"react": "^19.0.0",
|
|
41
|
+
"@usegraft/core": "0.1.0"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"next": ">=15.0.0",
|
|
45
|
+
"react": "^19.0.0"
|
|
46
|
+
},
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=22.16"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsup src/index.ts src/next-config.ts --format esm --dts --clean --external react --external react/jsx-runtime --external next",
|
|
52
|
+
"dev": "tsup src/index.ts src/next-config.ts --format esm --watch --external react --external react/jsx-runtime --external next",
|
|
53
|
+
"typecheck": "tsc --noEmit",
|
|
54
|
+
"test": "vitest run --passWithNoTests",
|
|
55
|
+
"lint": "oxlint ."
|
|
56
|
+
}
|
|
57
|
+
}
|