@valbuild/next 0.15.0 → 0.17.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/client/dist/valbuild-next-client.cjs.d.ts +2 -0
- package/client/dist/valbuild-next-client.cjs.d.ts.map +1 -0
- package/client/dist/valbuild-next-client.cjs.dev.js +86 -0
- package/client/dist/valbuild-next-client.cjs.js +7 -0
- package/client/dist/valbuild-next-client.cjs.prod.js +86 -0
- package/client/dist/valbuild-next-client.esm.js +82 -0
- package/client/package.json +4 -0
- package/dist/ValProvider-53a38560.cjs.js +7 -0
- package/dist/ValProvider-53a38560.cjs.prod.js +11 -0
- package/dist/ValProvider-5c9531c7.cjs.dev.js +11 -0
- package/dist/ValProvider-7bf49cb7.esm.js +7 -0
- package/dist/declarations/src/ValProvider.d.ts +2 -0
- package/dist/declarations/src/client/index.d.ts +1 -0
- package/dist/declarations/src/client/useVal.d.ts +3 -0
- package/dist/declarations/src/index.d.ts +16 -4
- package/dist/declarations/src/initVal.d.ts +2 -0
- package/dist/declarations/src/server/fetchVal.d.ts +3 -0
- package/dist/declarations/src/server/index.d.ts +1 -0
- package/dist/valbuild-next.cjs.dev.js +58 -17
- package/dist/valbuild-next.cjs.prod.js +58 -17
- package/dist/valbuild-next.esm.js +20 -3
- package/package.json +16 -6
- package/server/dist/valbuild-next-server.cjs.d.ts +2 -0
- package/server/dist/valbuild-next-server.cjs.d.ts.map +1 -0
- package/server/dist/valbuild-next-server.cjs.dev.js +478 -0
- package/server/dist/valbuild-next-server.cjs.js +7 -0
- package/server/dist/valbuild-next-server.cjs.prod.js +478 -0
- package/server/dist/valbuild-next-server.esm.js +456 -0
- package/server/package.json +4 -0
- package/src/ValProvider.tsx +5 -0
- package/src/client/index.ts +1 -0
- package/src/client/useVal.ts +36 -0
- package/src/index.ts +40 -4
- package/src/initVal.ts +9 -0
- package/src/server/fetchVal.ts +123 -0
- package/src/server/index.ts +1 -0
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
import { transform } from '@valbuild/react/stega';
|
|
2
|
+
import { ValApi, Internal } from '@valbuild/core';
|
|
3
|
+
import { result } from '@valbuild/core/fp';
|
|
4
|
+
import { draftMode } from 'next/headers';
|
|
5
|
+
|
|
6
|
+
function _regeneratorRuntime() {
|
|
7
|
+
_regeneratorRuntime = function () {
|
|
8
|
+
return exports;
|
|
9
|
+
};
|
|
10
|
+
var exports = {},
|
|
11
|
+
Op = Object.prototype,
|
|
12
|
+
hasOwn = Op.hasOwnProperty,
|
|
13
|
+
defineProperty = Object.defineProperty || function (obj, key, desc) {
|
|
14
|
+
obj[key] = desc.value;
|
|
15
|
+
},
|
|
16
|
+
$Symbol = "function" == typeof Symbol ? Symbol : {},
|
|
17
|
+
iteratorSymbol = $Symbol.iterator || "@@iterator",
|
|
18
|
+
asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator",
|
|
19
|
+
toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
|
|
20
|
+
function define(obj, key, value) {
|
|
21
|
+
return Object.defineProperty(obj, key, {
|
|
22
|
+
value: value,
|
|
23
|
+
enumerable: !0,
|
|
24
|
+
configurable: !0,
|
|
25
|
+
writable: !0
|
|
26
|
+
}), obj[key];
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
define({}, "");
|
|
30
|
+
} catch (err) {
|
|
31
|
+
define = function (obj, key, value) {
|
|
32
|
+
return obj[key] = value;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function wrap(innerFn, outerFn, self, tryLocsList) {
|
|
36
|
+
var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator,
|
|
37
|
+
generator = Object.create(protoGenerator.prototype),
|
|
38
|
+
context = new Context(tryLocsList || []);
|
|
39
|
+
return defineProperty(generator, "_invoke", {
|
|
40
|
+
value: makeInvokeMethod(innerFn, self, context)
|
|
41
|
+
}), generator;
|
|
42
|
+
}
|
|
43
|
+
function tryCatch(fn, obj, arg) {
|
|
44
|
+
try {
|
|
45
|
+
return {
|
|
46
|
+
type: "normal",
|
|
47
|
+
arg: fn.call(obj, arg)
|
|
48
|
+
};
|
|
49
|
+
} catch (err) {
|
|
50
|
+
return {
|
|
51
|
+
type: "throw",
|
|
52
|
+
arg: err
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.wrap = wrap;
|
|
57
|
+
var ContinueSentinel = {};
|
|
58
|
+
function Generator() {}
|
|
59
|
+
function GeneratorFunction() {}
|
|
60
|
+
function GeneratorFunctionPrototype() {}
|
|
61
|
+
var IteratorPrototype = {};
|
|
62
|
+
define(IteratorPrototype, iteratorSymbol, function () {
|
|
63
|
+
return this;
|
|
64
|
+
});
|
|
65
|
+
var getProto = Object.getPrototypeOf,
|
|
66
|
+
NativeIteratorPrototype = getProto && getProto(getProto(values([])));
|
|
67
|
+
NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype);
|
|
68
|
+
var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype);
|
|
69
|
+
function defineIteratorMethods(prototype) {
|
|
70
|
+
["next", "throw", "return"].forEach(function (method) {
|
|
71
|
+
define(prototype, method, function (arg) {
|
|
72
|
+
return this._invoke(method, arg);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
function AsyncIterator(generator, PromiseImpl) {
|
|
77
|
+
function invoke(method, arg, resolve, reject) {
|
|
78
|
+
var record = tryCatch(generator[method], generator, arg);
|
|
79
|
+
if ("throw" !== record.type) {
|
|
80
|
+
var result = record.arg,
|
|
81
|
+
value = result.value;
|
|
82
|
+
return value && "object" == typeof value && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) {
|
|
83
|
+
invoke("next", value, resolve, reject);
|
|
84
|
+
}, function (err) {
|
|
85
|
+
invoke("throw", err, resolve, reject);
|
|
86
|
+
}) : PromiseImpl.resolve(value).then(function (unwrapped) {
|
|
87
|
+
result.value = unwrapped, resolve(result);
|
|
88
|
+
}, function (error) {
|
|
89
|
+
return invoke("throw", error, resolve, reject);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
reject(record.arg);
|
|
93
|
+
}
|
|
94
|
+
var previousPromise;
|
|
95
|
+
defineProperty(this, "_invoke", {
|
|
96
|
+
value: function (method, arg) {
|
|
97
|
+
function callInvokeWithMethodAndArg() {
|
|
98
|
+
return new PromiseImpl(function (resolve, reject) {
|
|
99
|
+
invoke(method, arg, resolve, reject);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
function makeInvokeMethod(innerFn, self, context) {
|
|
107
|
+
var state = "suspendedStart";
|
|
108
|
+
return function (method, arg) {
|
|
109
|
+
if ("executing" === state) throw new Error("Generator is already running");
|
|
110
|
+
if ("completed" === state) {
|
|
111
|
+
if ("throw" === method) throw arg;
|
|
112
|
+
return doneResult();
|
|
113
|
+
}
|
|
114
|
+
for (context.method = method, context.arg = arg;;) {
|
|
115
|
+
var delegate = context.delegate;
|
|
116
|
+
if (delegate) {
|
|
117
|
+
var delegateResult = maybeInvokeDelegate(delegate, context);
|
|
118
|
+
if (delegateResult) {
|
|
119
|
+
if (delegateResult === ContinueSentinel) continue;
|
|
120
|
+
return delegateResult;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) {
|
|
124
|
+
if ("suspendedStart" === state) throw state = "completed", context.arg;
|
|
125
|
+
context.dispatchException(context.arg);
|
|
126
|
+
} else "return" === context.method && context.abrupt("return", context.arg);
|
|
127
|
+
state = "executing";
|
|
128
|
+
var record = tryCatch(innerFn, self, context);
|
|
129
|
+
if ("normal" === record.type) {
|
|
130
|
+
if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue;
|
|
131
|
+
return {
|
|
132
|
+
value: record.arg,
|
|
133
|
+
done: context.done
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
"throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg);
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function maybeInvokeDelegate(delegate, context) {
|
|
141
|
+
var methodName = context.method,
|
|
142
|
+
method = delegate.iterator[methodName];
|
|
143
|
+
if (undefined === method) return context.delegate = null, "throw" === methodName && delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method) || "return" !== methodName && (context.method = "throw", context.arg = new TypeError("The iterator does not provide a '" + methodName + "' method")), ContinueSentinel;
|
|
144
|
+
var record = tryCatch(method, delegate.iterator, context.arg);
|
|
145
|
+
if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel;
|
|
146
|
+
var info = record.arg;
|
|
147
|
+
return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel);
|
|
148
|
+
}
|
|
149
|
+
function pushTryEntry(locs) {
|
|
150
|
+
var entry = {
|
|
151
|
+
tryLoc: locs[0]
|
|
152
|
+
};
|
|
153
|
+
1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry);
|
|
154
|
+
}
|
|
155
|
+
function resetTryEntry(entry) {
|
|
156
|
+
var record = entry.completion || {};
|
|
157
|
+
record.type = "normal", delete record.arg, entry.completion = record;
|
|
158
|
+
}
|
|
159
|
+
function Context(tryLocsList) {
|
|
160
|
+
this.tryEntries = [{
|
|
161
|
+
tryLoc: "root"
|
|
162
|
+
}], tryLocsList.forEach(pushTryEntry, this), this.reset(!0);
|
|
163
|
+
}
|
|
164
|
+
function values(iterable) {
|
|
165
|
+
if (iterable) {
|
|
166
|
+
var iteratorMethod = iterable[iteratorSymbol];
|
|
167
|
+
if (iteratorMethod) return iteratorMethod.call(iterable);
|
|
168
|
+
if ("function" == typeof iterable.next) return iterable;
|
|
169
|
+
if (!isNaN(iterable.length)) {
|
|
170
|
+
var i = -1,
|
|
171
|
+
next = function next() {
|
|
172
|
+
for (; ++i < iterable.length;) if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next;
|
|
173
|
+
return next.value = undefined, next.done = !0, next;
|
|
174
|
+
};
|
|
175
|
+
return next.next = next;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return {
|
|
179
|
+
next: doneResult
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
function doneResult() {
|
|
183
|
+
return {
|
|
184
|
+
value: undefined,
|
|
185
|
+
done: !0
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
return GeneratorFunction.prototype = GeneratorFunctionPrototype, defineProperty(Gp, "constructor", {
|
|
189
|
+
value: GeneratorFunctionPrototype,
|
|
190
|
+
configurable: !0
|
|
191
|
+
}), defineProperty(GeneratorFunctionPrototype, "constructor", {
|
|
192
|
+
value: GeneratorFunction,
|
|
193
|
+
configurable: !0
|
|
194
|
+
}), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) {
|
|
195
|
+
var ctor = "function" == typeof genFun && genFun.constructor;
|
|
196
|
+
return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name));
|
|
197
|
+
}, exports.mark = function (genFun) {
|
|
198
|
+
return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun;
|
|
199
|
+
}, exports.awrap = function (arg) {
|
|
200
|
+
return {
|
|
201
|
+
__await: arg
|
|
202
|
+
};
|
|
203
|
+
}, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () {
|
|
204
|
+
return this;
|
|
205
|
+
}), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) {
|
|
206
|
+
void 0 === PromiseImpl && (PromiseImpl = Promise);
|
|
207
|
+
var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl);
|
|
208
|
+
return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) {
|
|
209
|
+
return result.done ? result.value : iter.next();
|
|
210
|
+
});
|
|
211
|
+
}, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () {
|
|
212
|
+
return this;
|
|
213
|
+
}), define(Gp, "toString", function () {
|
|
214
|
+
return "[object Generator]";
|
|
215
|
+
}), exports.keys = function (val) {
|
|
216
|
+
var object = Object(val),
|
|
217
|
+
keys = [];
|
|
218
|
+
for (var key in object) keys.push(key);
|
|
219
|
+
return keys.reverse(), function next() {
|
|
220
|
+
for (; keys.length;) {
|
|
221
|
+
var key = keys.pop();
|
|
222
|
+
if (key in object) return next.value = key, next.done = !1, next;
|
|
223
|
+
}
|
|
224
|
+
return next.done = !0, next;
|
|
225
|
+
};
|
|
226
|
+
}, exports.values = values, Context.prototype = {
|
|
227
|
+
constructor: Context,
|
|
228
|
+
reset: function (skipTempReset) {
|
|
229
|
+
if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined);
|
|
230
|
+
},
|
|
231
|
+
stop: function () {
|
|
232
|
+
this.done = !0;
|
|
233
|
+
var rootRecord = this.tryEntries[0].completion;
|
|
234
|
+
if ("throw" === rootRecord.type) throw rootRecord.arg;
|
|
235
|
+
return this.rval;
|
|
236
|
+
},
|
|
237
|
+
dispatchException: function (exception) {
|
|
238
|
+
if (this.done) throw exception;
|
|
239
|
+
var context = this;
|
|
240
|
+
function handle(loc, caught) {
|
|
241
|
+
return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught;
|
|
242
|
+
}
|
|
243
|
+
for (var i = this.tryEntries.length - 1; i >= 0; --i) {
|
|
244
|
+
var entry = this.tryEntries[i],
|
|
245
|
+
record = entry.completion;
|
|
246
|
+
if ("root" === entry.tryLoc) return handle("end");
|
|
247
|
+
if (entry.tryLoc <= this.prev) {
|
|
248
|
+
var hasCatch = hasOwn.call(entry, "catchLoc"),
|
|
249
|
+
hasFinally = hasOwn.call(entry, "finallyLoc");
|
|
250
|
+
if (hasCatch && hasFinally) {
|
|
251
|
+
if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
|
|
252
|
+
if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
|
|
253
|
+
} else if (hasCatch) {
|
|
254
|
+
if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
|
|
255
|
+
} else {
|
|
256
|
+
if (!hasFinally) throw new Error("try statement without catch or finally");
|
|
257
|
+
if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
},
|
|
262
|
+
abrupt: function (type, arg) {
|
|
263
|
+
for (var i = this.tryEntries.length - 1; i >= 0; --i) {
|
|
264
|
+
var entry = this.tryEntries[i];
|
|
265
|
+
if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) {
|
|
266
|
+
var finallyEntry = entry;
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null);
|
|
271
|
+
var record = finallyEntry ? finallyEntry.completion : {};
|
|
272
|
+
return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record);
|
|
273
|
+
},
|
|
274
|
+
complete: function (record, afterLoc) {
|
|
275
|
+
if ("throw" === record.type) throw record.arg;
|
|
276
|
+
return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel;
|
|
277
|
+
},
|
|
278
|
+
finish: function (finallyLoc) {
|
|
279
|
+
for (var i = this.tryEntries.length - 1; i >= 0; --i) {
|
|
280
|
+
var entry = this.tryEntries[i];
|
|
281
|
+
if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel;
|
|
282
|
+
}
|
|
283
|
+
},
|
|
284
|
+
catch: function (tryLoc) {
|
|
285
|
+
for (var i = this.tryEntries.length - 1; i >= 0; --i) {
|
|
286
|
+
var entry = this.tryEntries[i];
|
|
287
|
+
if (entry.tryLoc === tryLoc) {
|
|
288
|
+
var record = entry.completion;
|
|
289
|
+
if ("throw" === record.type) {
|
|
290
|
+
var thrown = record.arg;
|
|
291
|
+
resetTryEntry(entry);
|
|
292
|
+
}
|
|
293
|
+
return thrown;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
throw new Error("illegal catch attempt");
|
|
297
|
+
},
|
|
298
|
+
delegateYield: function (iterable, resultName, nextLoc) {
|
|
299
|
+
return this.delegate = {
|
|
300
|
+
iterator: values(iterable),
|
|
301
|
+
resultName: resultName,
|
|
302
|
+
nextLoc: nextLoc
|
|
303
|
+
}, "next" === this.method && (this.arg = undefined), ContinueSentinel;
|
|
304
|
+
}
|
|
305
|
+
}, exports;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
309
|
+
try {
|
|
310
|
+
var info = gen[key](arg);
|
|
311
|
+
var value = info.value;
|
|
312
|
+
} catch (error) {
|
|
313
|
+
reject(error);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
if (info.done) {
|
|
317
|
+
resolve(value);
|
|
318
|
+
} else {
|
|
319
|
+
Promise.resolve(value).then(_next, _throw);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
function _asyncToGenerator(fn) {
|
|
323
|
+
return function () {
|
|
324
|
+
var self = this,
|
|
325
|
+
args = arguments;
|
|
326
|
+
return new Promise(function (resolve, reject) {
|
|
327
|
+
var gen = fn.apply(self, args);
|
|
328
|
+
function _next(value) {
|
|
329
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
330
|
+
}
|
|
331
|
+
function _throw(err) {
|
|
332
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
333
|
+
}
|
|
334
|
+
_next(undefined);
|
|
335
|
+
});
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
var valApiEndpoints = "/api/val"; // TODO: get from config
|
|
340
|
+
function fetchVal(selector) {
|
|
341
|
+
var enabled = safeDraftModeEnabled();
|
|
342
|
+
if (enabled) {
|
|
343
|
+
getHost().then(function (host) {
|
|
344
|
+
//
|
|
345
|
+
if (host) {
|
|
346
|
+
// TODO: Use the content.val.build endpoints directly
|
|
347
|
+
var api = new ValApi("".concat(host).concat(valApiEndpoints));
|
|
348
|
+
// Optimize: only fetch the modules needed, also cache by module id and revalidate when patched
|
|
349
|
+
// const valModuleIds = getModuleIds(selector);
|
|
350
|
+
return api.getModules({
|
|
351
|
+
patch: true,
|
|
352
|
+
includeSource: true,
|
|
353
|
+
headers: getValHeaders()
|
|
354
|
+
}).then(function (res) {
|
|
355
|
+
if (result.isOk(res)) {
|
|
356
|
+
var modules = res.value.modules;
|
|
357
|
+
return transform(selector, {
|
|
358
|
+
getModule: function getModule(moduleId) {
|
|
359
|
+
var module = modules[moduleId];
|
|
360
|
+
if (module) {
|
|
361
|
+
return module.source;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
} else {
|
|
366
|
+
console.error("Val: could not fetch modules", res.error);
|
|
367
|
+
}
|
|
368
|
+
return transform(selector, {});
|
|
369
|
+
})["catch"](function (err) {
|
|
370
|
+
console.error("Val: failed while checking modules", err);
|
|
371
|
+
return selector;
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
return transform(selector, {
|
|
377
|
+
disabled: !enabled
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
function getHost() {
|
|
381
|
+
return _getHost.apply(this, arguments);
|
|
382
|
+
}
|
|
383
|
+
function _getHost() {
|
|
384
|
+
_getHost = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
385
|
+
var _hs$get, _yield$import, headers, hs, host, proto;
|
|
386
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
387
|
+
while (1) switch (_context.prev = _context.next) {
|
|
388
|
+
case 0:
|
|
389
|
+
_context.prev = 0;
|
|
390
|
+
_context.next = 3;
|
|
391
|
+
return import('next/headers');
|
|
392
|
+
case 3:
|
|
393
|
+
_yield$import = _context.sent;
|
|
394
|
+
headers = _yield$import.headers;
|
|
395
|
+
hs = headers();
|
|
396
|
+
host = hs.get("host");
|
|
397
|
+
proto = "https";
|
|
398
|
+
if (hs.get("x-forwarded-proto") === "http") {
|
|
399
|
+
proto = "http";
|
|
400
|
+
} else if ((_hs$get = hs.get("referer")) !== null && _hs$get !== void 0 && _hs$get.startsWith("http://")) {
|
|
401
|
+
proto = "http";
|
|
402
|
+
} else if (host !== null && host !== void 0 && host.startsWith("localhost")) {
|
|
403
|
+
proto = "http";
|
|
404
|
+
}
|
|
405
|
+
if (!(host && proto)) {
|
|
406
|
+
_context.next = 11;
|
|
407
|
+
break;
|
|
408
|
+
}
|
|
409
|
+
return _context.abrupt("return", "".concat(proto, "://").concat(host));
|
|
410
|
+
case 11:
|
|
411
|
+
return _context.abrupt("return", null);
|
|
412
|
+
case 14:
|
|
413
|
+
_context.prev = 14;
|
|
414
|
+
_context.t0 = _context["catch"](0);
|
|
415
|
+
console.error("Val: could not read headers! fetchVal can only be used server-side. Use useVal on clients.", _context.t0);
|
|
416
|
+
return _context.abrupt("return", null);
|
|
417
|
+
case 18:
|
|
418
|
+
case "end":
|
|
419
|
+
return _context.stop();
|
|
420
|
+
}
|
|
421
|
+
}, _callee, null, [[0, 14]]);
|
|
422
|
+
}));
|
|
423
|
+
return _getHost.apply(this, arguments);
|
|
424
|
+
}
|
|
425
|
+
function getValHeaders() {
|
|
426
|
+
try {
|
|
427
|
+
// const cs = cookies(); // TODO: simply get all headers?
|
|
428
|
+
var cs = {
|
|
429
|
+
get: function get(s) {
|
|
430
|
+
return {
|
|
431
|
+
value: s
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
var session = cs.get(Internal.VAL_SESSION_COOKIE);
|
|
436
|
+
if (session) {
|
|
437
|
+
return {
|
|
438
|
+
Cookie: "".concat(Internal.VAL_SESSION_COOKIE, "=").concat(session.value)
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
return {};
|
|
442
|
+
} catch (err) {
|
|
443
|
+
console.error("Val: could not read cookies! fetchVal can only be used server-side. Use useVal on clients.", err);
|
|
444
|
+
return {};
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
function safeDraftModeEnabled() {
|
|
448
|
+
try {
|
|
449
|
+
return draftMode().isEnabled;
|
|
450
|
+
} catch (err) {
|
|
451
|
+
console.error("Val: could read draft mode! fetchVal can only be used server-side. Use useVal on clients.", err);
|
|
452
|
+
return false;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
export { fetchVal };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useVal } from "./useVal";
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {
|
|
2
|
+
GenericSelector,
|
|
3
|
+
Internal,
|
|
4
|
+
ModuleId,
|
|
5
|
+
SelectorOf,
|
|
6
|
+
SelectorSource,
|
|
7
|
+
} from "@valbuild/core";
|
|
8
|
+
import { StegaOfSource, getModuleIds, transform } from "@valbuild/react/stega";
|
|
9
|
+
import { useValStore } from "@valbuild/react";
|
|
10
|
+
import { useEffect, useState, useSyncExternalStore } from "react";
|
|
11
|
+
|
|
12
|
+
export function useVal<T extends SelectorSource>(
|
|
13
|
+
selector: T
|
|
14
|
+
): SelectorOf<T> extends GenericSelector<infer S> ? StegaOfSource<S> : never {
|
|
15
|
+
const valStore = useValStore();
|
|
16
|
+
const moduleIds = getModuleIds(selector) as ModuleId[];
|
|
17
|
+
const moduleMap = useSyncExternalStore(
|
|
18
|
+
valStore.subscribe(moduleIds),
|
|
19
|
+
valStore.getSnapshot(moduleIds),
|
|
20
|
+
valStore.getServerSnapshot(moduleIds)
|
|
21
|
+
);
|
|
22
|
+
const [enabled, setEnabled] = useState(false);
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
setEnabled(
|
|
25
|
+
document.cookie.includes(`${Internal.VAL_ENABLE_COOKIE_NAME}=true`)
|
|
26
|
+
);
|
|
27
|
+
}, []);
|
|
28
|
+
return transform(selector, {
|
|
29
|
+
disabled: !enabled,
|
|
30
|
+
getModule: (moduleId) => {
|
|
31
|
+
if (moduleMap) {
|
|
32
|
+
return moduleMap[moduleId as ModuleId];
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,11 +1,47 @@
|
|
|
1
|
-
|
|
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 {
|
|
7
|
+
RichTextSource,
|
|
8
|
+
RichText,
|
|
9
|
+
TextNode,
|
|
10
|
+
ParagraphNode,
|
|
11
|
+
HeadingNode,
|
|
12
|
+
ListItemNode,
|
|
13
|
+
ListNode,
|
|
14
|
+
} from "@valbuild/core";
|
|
15
|
+
export {
|
|
16
|
+
type Val,
|
|
17
|
+
type SerializedVal,
|
|
18
|
+
type ModuleId,
|
|
19
|
+
type ModulePath,
|
|
20
|
+
type SourcePath,
|
|
21
|
+
type JsonOfSource,
|
|
22
|
+
} from "@valbuild/core";
|
|
23
|
+
export type { Json, JsonPrimitive } from "@valbuild/core";
|
|
24
|
+
export type { ValidationErrors, ValidationError } from "@valbuild/core";
|
|
25
|
+
export type { ValidationFix } from "@valbuild/core";
|
|
26
|
+
export * as expr from "@valbuild/core";
|
|
27
|
+
export { FILE_REF_PROP } from "@valbuild/core";
|
|
28
|
+
export { VAL_EXTENSION, type SourceArray } from "@valbuild/core";
|
|
29
|
+
export { derefPatch } from "@valbuild/core";
|
|
30
|
+
export {
|
|
31
|
+
type SelectorSource,
|
|
32
|
+
type SelectorOf,
|
|
33
|
+
GenericSelector,
|
|
34
|
+
} from "@valbuild/core";
|
|
2
35
|
|
|
3
|
-
|
|
36
|
+
// React
|
|
4
37
|
export { ValRichText } from "@valbuild/react";
|
|
5
38
|
|
|
6
|
-
|
|
39
|
+
// Stega
|
|
7
40
|
export { type ValEncodedString } from "@valbuild/react/stega";
|
|
8
|
-
|
|
41
|
+
|
|
42
|
+
// Next specific
|
|
43
|
+
export { ValProvider } from "./ValProvider";
|
|
44
|
+
export { initVal } from "./initVal";
|
|
9
45
|
|
|
10
46
|
// Auto-tag JSX with Val paths:
|
|
11
47
|
import { autoTagJSX } from "@valbuild/react/stega";
|
package/src/initVal.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { transform, 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 transform(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 transform(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 transform(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 read draft mode! fetchVal can only be used server-side. Use useVal on clients.",
|
|
119
|
+
err
|
|
120
|
+
);
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
}
|