@valbuild/next 0.33.0 → 0.35.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.
Files changed (35) hide show
  1. package/README.md +296 -274
  2. package/client/dist/valbuild-next-client.cjs.dev.js +11 -58
  3. package/client/dist/valbuild-next-client.cjs.prod.js +11 -58
  4. package/client/dist/valbuild-next-client.esm.js +10 -57
  5. package/dist/ValNextProvider-3676dacc.esm.js +25 -0
  6. package/dist/ValNextProvider-ae031b12.cjs.dev.js +29 -0
  7. package/dist/ValNextProvider-fe84f6a9.cjs.js +7 -0
  8. package/dist/ValNextProvider-fe84f6a9.cjs.prod.js +29 -0
  9. package/dist/declarations/src/ValProvider.d.ts +4 -2
  10. package/dist/declarations/src/client/index.d.ts +1 -1
  11. package/dist/declarations/src/client/initValClient.d.ts +8 -0
  12. package/dist/declarations/src/initVal.d.ts +3 -1
  13. package/dist/declarations/src/rsc/index.d.ts +2 -0
  14. package/dist/declarations/src/rsc/initValRsc.d.ts +19 -0
  15. package/dist/declarations/src/server/index.d.ts +1 -1
  16. package/dist/declarations/src/server/initValServer.d.ts +11 -0
  17. package/dist/slicedToArray-1471796d.cjs.dev.js +58 -0
  18. package/dist/slicedToArray-4190fac1.cjs.prod.js +58 -0
  19. package/dist/slicedToArray-62cd636a.esm.js +56 -0
  20. package/dist/valbuild-next.cjs.dev.js +63 -4
  21. package/dist/valbuild-next.cjs.prod.js +63 -4
  22. package/dist/valbuild-next.esm.js +63 -5
  23. package/package.json +15 -6
  24. package/rsc/dist/valbuild-next-rsc.cjs.d.ts +2 -0
  25. package/rsc/dist/valbuild-next-rsc.cjs.d.ts.map +1 -0
  26. package/rsc/dist/valbuild-next-rsc.cjs.dev.js +159 -0
  27. package/rsc/dist/valbuild-next-rsc.cjs.js +7 -0
  28. package/rsc/dist/valbuild-next-rsc.cjs.prod.js +159 -0
  29. package/rsc/dist/valbuild-next-rsc.esm.js +155 -0
  30. package/rsc/package.json +4 -0
  31. package/server/dist/valbuild-next-server.cjs.dev.js +78 -133
  32. package/server/dist/valbuild-next-server.cjs.prod.js +78 -133
  33. package/server/dist/valbuild-next-server.esm.js +78 -115
  34. package/dist/declarations/src/client/useVal.d.ts +0 -3
  35. package/dist/declarations/src/server/fetchVal.d.ts +0 -3
@@ -1,8 +1,7 @@
1
1
  import 'server-only';
2
- import { stegaEncode } from '@valbuild/react/stega';
3
- import { ValApi, Internal } from '@valbuild/core';
4
- import { result } from '@valbuild/core/fp';
5
- import { draftMode } from 'next/headers';
2
+ import { _ as _slicedToArray } from '../../dist/slicedToArray-62cd636a.esm.js';
3
+ import { createValApiRouter, createValServer } from '@valbuild/server';
4
+ import { NextResponse } from 'next/server';
6
5
 
7
6
  function _regeneratorRuntime() {
8
7
  _regeneratorRuntime = function () {
@@ -337,121 +336,85 @@ function _asyncToGenerator(fn) {
337
336
  };
338
337
  }
339
338
 
340
- var valApiEndpoints = "/api/val"; // TODO: get from config
341
- function fetchVal(selector) {
342
- var enabled = safeDraftModeEnabled();
343
- if (enabled) {
344
- getHost().then(function (host) {
345
- //
346
- if (host) {
347
- // TODO: Use the content.val.build endpoints directly
348
- var api = new ValApi("".concat(host).concat(valApiEndpoints));
349
- // Optimize: only fetch the modules needed, also cache by module id and revalidate when patched
350
- // const valModuleIds = getModuleIds(selector);
351
- return api.getModules({
352
- patch: true,
353
- includeSource: true,
354
- headers: getValHeaders()
355
- }).then(function (res) {
356
- if (result.isOk(res)) {
357
- var modules = res.value.modules;
358
- return stegaEncode(selector, {
359
- getModule: function getModule(moduleId) {
360
- var module = modules[moduleId];
361
- if (module) {
362
- return module.source;
363
- }
364
- }
365
- });
366
- } else {
367
- console.error("Val: could not fetch modules", res.error);
339
+ var initValNextAppRouter = function initValNextAppRouter(config, nextConfig) {
340
+ var route = "/api/val"; // TODO: get from config
341
+ return createValApiRouter(route, createValServer(route, config, {
342
+ isEnabled: function isEnabled() {
343
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
344
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
345
+ while (1) switch (_context.prev = _context.next) {
346
+ case 0:
347
+ return _context.abrupt("return", nextConfig.draftMode().isEnabled);
348
+ case 1:
349
+ case "end":
350
+ return _context.stop();
368
351
  }
369
- return stegaEncode(selector, {});
370
- })["catch"](function (err) {
371
- console.error("Val: failed while checking modules", err);
372
- return selector;
373
- });
374
- }
375
- });
376
- }
377
- return stegaEncode(selector, {
378
- disabled: !enabled
379
- });
380
- }
381
- function getHost() {
382
- return _getHost.apply(this, arguments);
383
- }
384
- function _getHost() {
385
- _getHost = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
386
- var _hs$get, _yield$import, headers, hs, host, proto;
387
- return _regeneratorRuntime().wrap(function _callee$(_context) {
388
- while (1) switch (_context.prev = _context.next) {
389
- case 0:
390
- _context.prev = 0;
391
- _context.next = 3;
392
- return import('next/headers');
393
- case 3:
394
- _yield$import = _context.sent;
395
- headers = _yield$import.headers;
396
- hs = headers();
397
- host = hs.get("host");
398
- proto = "https";
399
- if (hs.get("x-forwarded-proto") === "http") {
400
- proto = "http";
401
- } else if ((_hs$get = hs.get("referer")) !== null && _hs$get !== void 0 && _hs$get.startsWith("http://")) {
402
- proto = "http";
403
- } else if (host !== null && host !== void 0 && host.startsWith("localhost")) {
404
- proto = "http";
352
+ }, _callee);
353
+ }))();
354
+ },
355
+ onEnable: function onEnable() {
356
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
357
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
358
+ while (1) switch (_context2.prev = _context2.next) {
359
+ case 0:
360
+ nextConfig.draftMode().enable();
361
+ case 1:
362
+ case "end":
363
+ return _context2.stop();
405
364
  }
406
- if (!(host && proto)) {
407
- _context.next = 11;
408
- break;
365
+ }, _callee2);
366
+ }))();
367
+ },
368
+ onDisable: function onDisable() {
369
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
370
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
371
+ while (1) switch (_context3.prev = _context3.next) {
372
+ case 0:
373
+ nextConfig.draftMode().disable();
374
+ case 1:
375
+ case "end":
376
+ return _context3.stop();
409
377
  }
410
- return _context.abrupt("return", "".concat(proto, "://").concat(host));
411
- case 11:
412
- return _context.abrupt("return", null);
413
- case 14:
414
- _context.prev = 14;
415
- _context.t0 = _context["catch"](0);
416
- console.error("Val: could not read headers! fetchVal can only be used server-side. Use useVal on clients.", _context.t0);
417
- return _context.abrupt("return", null);
418
- case 18:
419
- case "end":
420
- return _context.stop();
421
- }
422
- }, _callee, null, [[0, 14]]);
423
- }));
424
- return _getHost.apply(this, arguments);
425
- }
426
- function getValHeaders() {
427
- try {
428
- // const cs = cookies(); // TODO: simply get all headers?
429
- var cs = {
430
- get: function get(s) {
431
- return {
432
- value: s
433
- };
378
+ }, _callee3);
379
+ }))();
380
+ }
381
+ }), function (valRes) {
382
+ var headers = "headers" in valRes ? new Headers(valRes.headers) : new Headers({});
383
+ if ("cookies" in valRes && valRes.cookies) {
384
+ headers.set("Set-Cookie", "");
385
+ for (var _i = 0, _Object$entries = Object.entries(valRes.cookies); _i < _Object$entries.length; _i++) {
386
+ var _cookie$options, _cookie$options2, _cookie$options3, _cookie$options4, _cookie$options5;
387
+ var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
388
+ cookieName = _Object$entries$_i[0],
389
+ cookie = _Object$entries$_i[1];
390
+ var cookieValue = "".concat(cookieName, "=").concat(encodeURIComponent(cookie.value || "")).concat((_cookie$options = cookie.options) !== null && _cookie$options !== void 0 && _cookie$options.httpOnly ? "; HttpOnly" : "").concat((_cookie$options2 = cookie.options) !== null && _cookie$options2 !== void 0 && _cookie$options2.secure ? "; Secure" : "").concat((_cookie$options3 = cookie.options) !== null && _cookie$options3 !== void 0 && _cookie$options3.sameSite ? "; SameSite=".concat(cookie.options.sameSite) : "").concat((_cookie$options4 = cookie.options) !== null && _cookie$options4 !== void 0 && _cookie$options4.path ? "; Path=".concat(cookie.options.path) : "").concat((_cookie$options5 = cookie.options) !== null && _cookie$options5 !== void 0 && _cookie$options5.expires ? "; Expires=".concat(cookie.options.expires.toISOString()) : "".concat(!cookie.value ? "; Max-Age=0" : ""));
391
+ headers.append("Set-Cookie", cookieValue);
434
392
  }
435
- };
436
- var session = cs.get(Internal.VAL_SESSION_COOKIE);
437
- if (session) {
438
- return {
439
- Cookie: "".concat(Internal.VAL_SESSION_COOKIE, "=").concat(session.value)
440
- };
441
393
  }
442
- return {};
443
- } catch (err) {
444
- console.error("Val: could not read cookies! fetchVal can only be used server-side. Use useVal on clients.", err);
445
- return {};
446
- }
447
- }
448
- function safeDraftModeEnabled() {
449
- try {
450
- return draftMode().isEnabled;
451
- } catch (err) {
452
- console.error("Val: could not read draft mode! fetchVal can only be used server-side. Use useVal on clients.", err);
453
- return false;
454
- }
394
+ if ("json" in valRes) {
395
+ headers.set("Content-Type", "application/json");
396
+ return NextResponse.json(valRes.json, {
397
+ headers: headers,
398
+ status: valRes.status
399
+ });
400
+ } else if (valRes.status === 302) {
401
+ headers.set("Location", valRes.redirectTo);
402
+ return NextResponse.redirect(valRes.redirectTo, {
403
+ status: valRes.status,
404
+ headers: headers
405
+ });
406
+ }
407
+ return new NextResponse("body" in valRes ? valRes.body : null, {
408
+ headers: headers,
409
+ status: valRes.status
410
+ });
411
+ });
412
+ };
413
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
414
+ function initValServer(config, nextConfig) {
415
+ return {
416
+ valNextAppRouter: initValNextAppRouter(config, nextConfig)
417
+ };
455
418
  }
456
419
 
457
- export { fetchVal };
420
+ export { initValServer };
@@ -1,3 +0,0 @@
1
- import { GenericSelector, SelectorOf, SelectorSource } from "@valbuild/core";
2
- import { StegaOfSource } from "@valbuild/react/stega";
3
- export declare function useVal<T extends SelectorSource>(selector: T): SelectorOf<T> extends GenericSelector<infer S> ? StegaOfSource<S> : never;
@@ -1,3 +0,0 @@
1
- import { type StegaOfSource } from "@valbuild/react/stega";
2
- import { SelectorSource, SelectorOf, GenericSelector } from "@valbuild/core";
3
- export declare function fetchVal<T extends SelectorSource>(selector: T): SelectorOf<T> extends GenericSelector<infer S> ? Promise<StegaOfSource<S>> : never;