@valbuild/next 0.25.0 → 0.27.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/package.json +9 -4
- package/.babelrc.json +0 -10
- package/CHANGELOG.md +0 -0
- package/jest.config.js +0 -4
- package/src/ValProvider.tsx +0 -5
- package/src/client/index.ts +0 -1
- package/src/client/useVal.ts +0 -40
- package/src/index.ts +0 -42
- package/src/initVal.ts +0 -9
- package/src/server/fetchVal.ts +0 -123
- package/src/server/index.ts +0 -1
- package/tsconfig.json +0 -22
package/package.json
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"next",
|
|
9
9
|
"react"
|
|
10
10
|
],
|
|
11
|
-
"version": "0.
|
|
11
|
+
"version": "0.27.0",
|
|
12
12
|
"scripts": {
|
|
13
13
|
"typecheck": "tsc --noEmit",
|
|
14
14
|
"test": "jest"
|
|
@@ -40,9 +40,9 @@
|
|
|
40
40
|
"exports": true
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@valbuild/core": "~0.
|
|
44
|
-
"@valbuild/react": "~0.
|
|
45
|
-
"@valbuild/server": "~0.
|
|
43
|
+
"@valbuild/core": "~0.27.0",
|
|
44
|
+
"@valbuild/react": "~0.27.0",
|
|
45
|
+
"@valbuild/server": "~0.27.0"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
48
|
"next": ">=13.4.0",
|
|
@@ -50,5 +50,10 @@
|
|
|
50
50
|
},
|
|
51
51
|
"externals": [
|
|
52
52
|
"next"
|
|
53
|
+
],
|
|
54
|
+
"files": [
|
|
55
|
+
"dist",
|
|
56
|
+
"client",
|
|
57
|
+
"server"
|
|
53
58
|
]
|
|
54
59
|
}
|
package/.babelrc.json
DELETED
package/CHANGELOG.md
DELETED
|
File without changes
|
package/jest.config.js
DELETED
package/src/ValProvider.tsx
DELETED
package/src/client/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { useVal } from "./useVal";
|
package/src/client/useVal.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
GenericSelector,
|
|
3
|
-
Internal,
|
|
4
|
-
ModuleId,
|
|
5
|
-
SelectorOf,
|
|
6
|
-
SelectorSource,
|
|
7
|
-
} from "@valbuild/core";
|
|
8
|
-
import {
|
|
9
|
-
StegaOfSource,
|
|
10
|
-
getModuleIds,
|
|
11
|
-
stegaEncode,
|
|
12
|
-
} from "@valbuild/react/stega";
|
|
13
|
-
import { useValStore } from "@valbuild/react";
|
|
14
|
-
import { useEffect, useState, useSyncExternalStore } from "react";
|
|
15
|
-
|
|
16
|
-
export function useVal<T extends SelectorSource>(
|
|
17
|
-
selector: T
|
|
18
|
-
): SelectorOf<T> extends GenericSelector<infer S> ? StegaOfSource<S> : never {
|
|
19
|
-
const valStore = useValStore();
|
|
20
|
-
const moduleIds = getModuleIds(selector) as ModuleId[];
|
|
21
|
-
const moduleMap = useSyncExternalStore(
|
|
22
|
-
valStore.subscribe(moduleIds),
|
|
23
|
-
valStore.getSnapshot(moduleIds),
|
|
24
|
-
valStore.getServerSnapshot(moduleIds)
|
|
25
|
-
);
|
|
26
|
-
const [enabled, setEnabled] = useState(false);
|
|
27
|
-
useEffect(() => {
|
|
28
|
-
setEnabled(
|
|
29
|
-
document.cookie.includes(`${Internal.VAL_ENABLE_COOKIE_NAME}=true`)
|
|
30
|
-
);
|
|
31
|
-
}, []);
|
|
32
|
-
return stegaEncode(selector, {
|
|
33
|
-
disabled: !enabled,
|
|
34
|
-
getModule: (moduleId) => {
|
|
35
|
-
if (moduleMap) {
|
|
36
|
-
return moduleMap[moduleId as ModuleId];
|
|
37
|
-
}
|
|
38
|
-
},
|
|
39
|
-
});
|
|
40
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
// Core (excluding initVal)
|
|
2
|
-
export { Schema, type SerializedSchema } from "@valbuild/core";
|
|
3
|
-
export type { SourceObject, SourcePrimitive, Source } from "@valbuild/core";
|
|
4
|
-
export type { ValModule, SerializedModule } from "@valbuild/core";
|
|
5
|
-
export type { FileSource } from "@valbuild/core";
|
|
6
|
-
export type { RichTextSource, RichText } from "@valbuild/core";
|
|
7
|
-
export {
|
|
8
|
-
type Val,
|
|
9
|
-
type SerializedVal,
|
|
10
|
-
type ModuleId,
|
|
11
|
-
type ModulePath,
|
|
12
|
-
type SourcePath,
|
|
13
|
-
type JsonOfSource,
|
|
14
|
-
} from "@valbuild/core";
|
|
15
|
-
export type { Json, JsonPrimitive } from "@valbuild/core";
|
|
16
|
-
export type { ValidationErrors, ValidationError } from "@valbuild/core";
|
|
17
|
-
export type { ValidationFix } from "@valbuild/core";
|
|
18
|
-
export * as expr from "@valbuild/core";
|
|
19
|
-
export { FILE_REF_PROP } from "@valbuild/core";
|
|
20
|
-
export { VAL_EXTENSION, type SourceArray } from "@valbuild/core";
|
|
21
|
-
export { derefPatch } from "@valbuild/core";
|
|
22
|
-
export {
|
|
23
|
-
type SelectorSource,
|
|
24
|
-
type SelectorOf,
|
|
25
|
-
GenericSelector,
|
|
26
|
-
} from "@valbuild/core";
|
|
27
|
-
|
|
28
|
-
// React
|
|
29
|
-
export { ValRichText } from "@valbuild/react";
|
|
30
|
-
|
|
31
|
-
// Stega
|
|
32
|
-
export { type ValEncodedString } from "@valbuild/react/stega";
|
|
33
|
-
|
|
34
|
-
// Next specific
|
|
35
|
-
export { ValProvider } from "./ValProvider";
|
|
36
|
-
export { initVal } from "./initVal";
|
|
37
|
-
|
|
38
|
-
// Auto-tag JSX with Val paths:
|
|
39
|
-
import { autoTagJSX } from "@valbuild/react/stega";
|
|
40
|
-
|
|
41
|
-
// NOTE! Side effects:
|
|
42
|
-
autoTagJSX();
|
package/src/initVal.ts
DELETED
package/src/server/fetchVal.ts
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import { stegaEncode, type StegaOfSource } from "@valbuild/react/stega";
|
|
2
|
-
import {
|
|
3
|
-
SelectorSource,
|
|
4
|
-
SelectorOf,
|
|
5
|
-
GenericSelector,
|
|
6
|
-
ModuleId,
|
|
7
|
-
} from "@valbuild/core";
|
|
8
|
-
import { ValApi } from "@valbuild/core";
|
|
9
|
-
import { result } from "@valbuild/core/fp";
|
|
10
|
-
import { Internal } from "@valbuild/core";
|
|
11
|
-
import { draftMode } from "next/headers";
|
|
12
|
-
|
|
13
|
-
const valApiEndpoints = "/api/val"; // TODO: get from config
|
|
14
|
-
export function fetchVal<T extends SelectorSource>(
|
|
15
|
-
selector: T
|
|
16
|
-
): SelectorOf<T> extends GenericSelector<infer S>
|
|
17
|
-
? Promise<StegaOfSource<S>>
|
|
18
|
-
: never {
|
|
19
|
-
const enabled = safeDraftModeEnabled();
|
|
20
|
-
if (enabled) {
|
|
21
|
-
getHost().then((host) => {
|
|
22
|
-
//
|
|
23
|
-
if (host) {
|
|
24
|
-
// TODO: Use the content.val.build endpoints directly
|
|
25
|
-
const api = new ValApi(`${host}${valApiEndpoints}`);
|
|
26
|
-
// Optimize: only fetch the modules needed, also cache by module id and revalidate when patched
|
|
27
|
-
// const valModuleIds = getModuleIds(selector);
|
|
28
|
-
return api
|
|
29
|
-
.getModules({
|
|
30
|
-
patch: true,
|
|
31
|
-
includeSource: true,
|
|
32
|
-
headers: getValHeaders(),
|
|
33
|
-
})
|
|
34
|
-
.then((res) => {
|
|
35
|
-
if (result.isOk(res)) {
|
|
36
|
-
const { modules } = res.value;
|
|
37
|
-
return stegaEncode(selector, {
|
|
38
|
-
getModule: (moduleId) => {
|
|
39
|
-
const module = modules[moduleId as ModuleId];
|
|
40
|
-
if (module) {
|
|
41
|
-
return module.source;
|
|
42
|
-
}
|
|
43
|
-
},
|
|
44
|
-
});
|
|
45
|
-
} else {
|
|
46
|
-
console.error("Val: could not fetch modules", res.error);
|
|
47
|
-
}
|
|
48
|
-
return stegaEncode(selector, {});
|
|
49
|
-
})
|
|
50
|
-
.catch((err) => {
|
|
51
|
-
console.error("Val: failed while checking modules", err);
|
|
52
|
-
return selector;
|
|
53
|
-
}) as SelectorOf<T> extends GenericSelector<infer S>
|
|
54
|
-
? Promise<StegaOfSource<S>>
|
|
55
|
-
: never;
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
return stegaEncode(selector, {
|
|
60
|
-
disabled: !enabled,
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
async function getHost() {
|
|
65
|
-
// TODO: does NextJs have a way to determine this?
|
|
66
|
-
try {
|
|
67
|
-
const { headers } = await import("next/headers");
|
|
68
|
-
const hs = headers();
|
|
69
|
-
const host = hs.get("host");
|
|
70
|
-
let proto = "https";
|
|
71
|
-
if (hs.get("x-forwarded-proto") === "http") {
|
|
72
|
-
proto = "http";
|
|
73
|
-
} else if (hs.get("referer")?.startsWith("http://")) {
|
|
74
|
-
proto = "http";
|
|
75
|
-
} else if (host?.startsWith("localhost")) {
|
|
76
|
-
proto = "http";
|
|
77
|
-
}
|
|
78
|
-
if (host && proto) {
|
|
79
|
-
return `${proto}://${host}`;
|
|
80
|
-
}
|
|
81
|
-
return null;
|
|
82
|
-
} catch (err) {
|
|
83
|
-
console.error(
|
|
84
|
-
"Val: could not read headers! fetchVal can only be used server-side. Use useVal on clients.",
|
|
85
|
-
err
|
|
86
|
-
);
|
|
87
|
-
return null;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function getValHeaders(): Record<string, string> {
|
|
92
|
-
try {
|
|
93
|
-
// const cs = cookies(); // TODO: simply get all headers?
|
|
94
|
-
const cs = {
|
|
95
|
-
get: (s: string) => ({ value: s }),
|
|
96
|
-
};
|
|
97
|
-
const session = cs.get(Internal.VAL_SESSION_COOKIE);
|
|
98
|
-
if (session) {
|
|
99
|
-
return {
|
|
100
|
-
Cookie: `${Internal.VAL_SESSION_COOKIE}=${session.value}`,
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
return {};
|
|
104
|
-
} catch (err) {
|
|
105
|
-
console.error(
|
|
106
|
-
"Val: could not read cookies! fetchVal can only be used server-side. Use useVal on clients.",
|
|
107
|
-
err
|
|
108
|
-
);
|
|
109
|
-
return {};
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function safeDraftModeEnabled() {
|
|
114
|
-
try {
|
|
115
|
-
return draftMode().isEnabled;
|
|
116
|
-
} catch (err) {
|
|
117
|
-
console.error(
|
|
118
|
-
"Val: could not read draft mode! fetchVal can only be used server-side. Use useVal on clients.",
|
|
119
|
-
err
|
|
120
|
-
);
|
|
121
|
-
return false;
|
|
122
|
-
}
|
|
123
|
-
}
|
package/src/server/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { fetchVal } from "./fetchVal";
|
package/tsconfig.json
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"esModuleInterop": true,
|
|
4
|
-
"strict": true,
|
|
5
|
-
"allowJs": true,
|
|
6
|
-
"isolatedModules": true,
|
|
7
|
-
"jsx": "react-jsx",
|
|
8
|
-
"lib": ["es6", "dom"],
|
|
9
|
-
"module": "CommonJS",
|
|
10
|
-
"noEmit": true,
|
|
11
|
-
"target": "ES5",
|
|
12
|
-
"outDir": "dist",
|
|
13
|
-
"rootDir": "src",
|
|
14
|
-
"skipLibCheck": true
|
|
15
|
-
},
|
|
16
|
-
"include": [
|
|
17
|
-
"src/**/*",
|
|
18
|
-
"../react/stega/autoTagJSX.ts",
|
|
19
|
-
"../react/stega/fetchVal.ts"
|
|
20
|
-
],
|
|
21
|
-
"exclude": ["dist/**"]
|
|
22
|
-
}
|