@valbuild/next 0.34.0 → 0.36.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 +1 -1
  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 +163 -0
  27. package/rsc/dist/valbuild-next-rsc.cjs.js +7 -0
  28. package/rsc/dist/valbuild-next-rsc.cjs.prod.js +160 -0
  29. package/rsc/dist/valbuild-next-rsc.esm.js +159 -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
@@ -0,0 +1,159 @@
1
+ import 'server-only';
2
+ import { getModuleIds, stegaEncode } from '@valbuild/react/stega';
3
+ import { ValApi, Internal } from '@valbuild/core';
4
+ import { result } from '@valbuild/core/fp';
5
+
6
+ function optimizeTreePath(ids) {
7
+ return findUpperDirectory(ids);
8
+ }
9
+ function findUpperDirectory(paths) {
10
+ if (paths.length === 0) {
11
+ return null;
12
+ }
13
+ var pathSegments = paths[0].split("/");
14
+ for (var i = 1; i < paths.length; i++) {
15
+ var currentPathSegments = paths[i].split("/");
16
+ // Find the minimum length of the two paths
17
+ var minLength = Math.min(pathSegments.length, currentPathSegments.length);
18
+
19
+ // Iterate through the segments and find the common prefix
20
+ var commonPrefix = "";
21
+ for (var j = 0; j < minLength; j++) {
22
+ if (pathSegments[j] === currentPathSegments[j]) {
23
+ commonPrefix += pathSegments[j] + "/";
24
+ } else {
25
+ break;
26
+ }
27
+ }
28
+ pathSegments = commonPrefix.split("/").slice(0, -1);
29
+
30
+ // If there is no common prefix, return /
31
+ if (pathSegments.length <= 1) {
32
+ return "/";
33
+ }
34
+ }
35
+ var upperDirectory = pathSegments.join("/");
36
+ return upperDirectory;
37
+ }
38
+
39
+ var initFetchValStega = function initFetchValStega(config, valApiEndpoints, isEnabled, getHeaders, getCookies) {
40
+ return function (selector) {
41
+ var enabled = false;
42
+ try {
43
+ enabled = isEnabled();
44
+ } catch (err) {
45
+ console.error("Val: could not check if Val is enabled! This might be due to an error to check draftMode. fetchVal can only be used server-side. Use useVal on clients.", err);
46
+ }
47
+ if (enabled) {
48
+ var headers;
49
+ try {
50
+ headers = getHeaders();
51
+ if (!(headers instanceof Headers)) {
52
+ throw new Error("Expected an instance of Headers. Check Val rsc config.");
53
+ }
54
+ } catch (err) {
55
+ console.error("Val: could not read headers! fetchVal can only be used server-side. Use useVal on clients.", err);
56
+ headers = null;
57
+ }
58
+ var cookies;
59
+ try {
60
+ cookies = getCookies();
61
+ } catch (err) {
62
+ console.error("Val: could not read cookies! fetchVal can only be used server-side. Use useVal on clients.", err);
63
+ cookies = null;
64
+ }
65
+ var host = headers && getHost(headers);
66
+ if (host && cookies && isProxyMode(config)) {
67
+ var _optimizeTreePath;
68
+ var api = new ValApi("".concat(host).concat(valApiEndpoints));
69
+ var valModuleIds = getModuleIds(selector);
70
+ return api.getTree({
71
+ // TODO: get tree should probably have a list of ids instead
72
+ treePath: (_optimizeTreePath = optimizeTreePath(valModuleIds)) !== null && _optimizeTreePath !== void 0 ? _optimizeTreePath : undefined,
73
+ patch: true,
74
+ includeSource: true,
75
+ headers: getValAuthHeaders(cookies)
76
+ }).then(function (res) {
77
+ if (result.isOk(res)) {
78
+ var modules = res.value.modules;
79
+ return stegaEncode(selector, {
80
+ disabled: !enabled,
81
+ getModule: function getModule(moduleId) {
82
+ var module = modules[moduleId];
83
+ if (module) {
84
+ return module.source;
85
+ }
86
+ }
87
+ });
88
+ } else {
89
+ console.error("Val: could not fetch modules", res.error);
90
+ throw Error(JSON.stringify(res.error, null, 2));
91
+ }
92
+ })["catch"](function (err) {
93
+ console.error("Val: failed while fetching modules", err);
94
+ if (process.env.NODE_ENV === "development") {
95
+ throw Error('You are running in "proxy" mode in development and Val could not fetch remote / proxy data. This is likely due to a misconfiguration. Check the console for more details.');
96
+ }
97
+ return stegaEncode(selector, {});
98
+ });
99
+ }
100
+ }
101
+ return stegaEncode(selector, {
102
+ disabled: !enabled
103
+ });
104
+ };
105
+ };
106
+ function getHost(headers) {
107
+ var _headers$get;
108
+ // TODO: does NextJs have a way to determine this?
109
+ var host = headers.get("host");
110
+ var proto = "https";
111
+ if (headers.get("x-forwarded-proto") === "http") {
112
+ proto = "http";
113
+ } else if ((_headers$get = headers.get("referer")) !== null && _headers$get !== void 0 && _headers$get.startsWith("http://")) {
114
+ proto = "http";
115
+ } else if (host !== null && host !== void 0 && host.startsWith("localhost")) {
116
+ proto = "http";
117
+ }
118
+ if (host && proto) {
119
+ return "".concat(proto, "://").concat(host);
120
+ }
121
+ return null;
122
+ }
123
+ function isProxyMode(opts) {
124
+ var maybeApiKey = opts.apiKey || process.env.VAL_API_KEY;
125
+ var maybeValSecret = opts.valSecret || process.env.VAL_SECRET;
126
+ var isProxyMode = opts.mode === "proxy" || opts.mode === undefined && (maybeApiKey || maybeValSecret);
127
+ return !!isProxyMode;
128
+ }
129
+ function getValAuthHeaders(cookies) {
130
+ try {
131
+ var session = cookies.get(Internal.VAL_SESSION_COOKIE);
132
+ if (session) {
133
+ return {
134
+ Cookie: "".concat(Internal.VAL_SESSION_COOKIE, "=").concat(encodeURIComponent(session.value))
135
+ };
136
+ }
137
+ return {};
138
+ } catch (err) {
139
+ console.error("Val: could not read cookies! fetchVal can only be used server-side. Use useVal on clients.", err);
140
+ return {};
141
+ }
142
+ }
143
+ var valApiEndpoints = "/api/val";
144
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
145
+ function initValRsc(config, rscNextConfig) {
146
+ return {
147
+ fetchValStega: initFetchValStega(config, valApiEndpoints,
148
+ // TODO: get from config
149
+ function () {
150
+ return rscNextConfig.draftMode().isEnabled;
151
+ }, function () {
152
+ return rscNextConfig.headers();
153
+ }, function () {
154
+ return rscNextConfig.cookies();
155
+ })
156
+ };
157
+ }
158
+
159
+ export { initValRsc };
@@ -0,0 +1,4 @@
1
+ {
2
+ "main": "dist/valbuild-next-rsc.cjs.js",
3
+ "module": "dist/valbuild-next-rsc.esm.js"
4
+ }
@@ -3,28 +3,9 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  require('server-only');
6
- var stega = require('@valbuild/react/stega');
7
- var core = require('@valbuild/core');
8
- var fp = require('@valbuild/core/fp');
9
- var headers = require('next/headers');
10
-
11
- function _interopNamespace(e) {
12
- if (e && e.__esModule) return e;
13
- var n = Object.create(null);
14
- if (e) {
15
- Object.keys(e).forEach(function (k) {
16
- if (k !== 'default') {
17
- var d = Object.getOwnPropertyDescriptor(e, k);
18
- Object.defineProperty(n, k, d.get ? d : {
19
- enumerable: true,
20
- get: function () { return e[k]; }
21
- });
22
- }
23
- });
24
- }
25
- n["default"] = e;
26
- return Object.freeze(n);
27
- }
6
+ var slicedToArray = require('../../dist/slicedToArray-1471796d.cjs.dev.js');
7
+ var server = require('@valbuild/server');
8
+ var server$1 = require('next/server');
28
9
 
29
10
  function _regeneratorRuntime() {
30
11
  _regeneratorRuntime = function () {
@@ -359,121 +340,85 @@ function _asyncToGenerator(fn) {
359
340
  };
360
341
  }
361
342
 
362
- var valApiEndpoints = "/api/val"; // TODO: get from config
363
- function fetchVal(selector) {
364
- var enabled = safeDraftModeEnabled();
365
- if (enabled) {
366
- getHost().then(function (host) {
367
- //
368
- if (host) {
369
- // TODO: Use the content.val.build endpoints directly
370
- var api = new core.ValApi("".concat(host).concat(valApiEndpoints));
371
- // Optimize: only fetch the modules needed, also cache by module id and revalidate when patched
372
- // const valModuleIds = getModuleIds(selector);
373
- return api.getModules({
374
- patch: true,
375
- includeSource: true,
376
- headers: getValHeaders()
377
- }).then(function (res) {
378
- if (fp.result.isOk(res)) {
379
- var modules = res.value.modules;
380
- return stega.stegaEncode(selector, {
381
- getModule: function getModule(moduleId) {
382
- var module = modules[moduleId];
383
- if (module) {
384
- return module.source;
385
- }
386
- }
387
- });
388
- } else {
389
- console.error("Val: could not fetch modules", res.error);
343
+ var initValNextAppRouter = function initValNextAppRouter(config, nextConfig) {
344
+ var route = "/api/val"; // TODO: get from config
345
+ return server.createValApiRouter(route, server.createValServer(route, config, {
346
+ isEnabled: function isEnabled() {
347
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
348
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
349
+ while (1) switch (_context.prev = _context.next) {
350
+ case 0:
351
+ return _context.abrupt("return", nextConfig.draftMode().isEnabled);
352
+ case 1:
353
+ case "end":
354
+ return _context.stop();
390
355
  }
391
- return stega.stegaEncode(selector, {});
392
- })["catch"](function (err) {
393
- console.error("Val: failed while checking modules", err);
394
- return selector;
395
- });
396
- }
397
- });
398
- }
399
- return stega.stegaEncode(selector, {
400
- disabled: !enabled
401
- });
402
- }
403
- function getHost() {
404
- return _getHost.apply(this, arguments);
405
- }
406
- function _getHost() {
407
- _getHost = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
408
- var _hs$get, _yield$import, headers, hs, host, proto;
409
- return _regeneratorRuntime().wrap(function _callee$(_context) {
410
- while (1) switch (_context.prev = _context.next) {
411
- case 0:
412
- _context.prev = 0;
413
- _context.next = 3;
414
- return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('next/headers')); });
415
- case 3:
416
- _yield$import = _context.sent;
417
- headers = _yield$import.headers;
418
- hs = headers();
419
- host = hs.get("host");
420
- proto = "https";
421
- if (hs.get("x-forwarded-proto") === "http") {
422
- proto = "http";
423
- } else if ((_hs$get = hs.get("referer")) !== null && _hs$get !== void 0 && _hs$get.startsWith("http://")) {
424
- proto = "http";
425
- } else if (host !== null && host !== void 0 && host.startsWith("localhost")) {
426
- proto = "http";
356
+ }, _callee);
357
+ }))();
358
+ },
359
+ onEnable: function onEnable() {
360
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
361
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
362
+ while (1) switch (_context2.prev = _context2.next) {
363
+ case 0:
364
+ nextConfig.draftMode().enable();
365
+ case 1:
366
+ case "end":
367
+ return _context2.stop();
427
368
  }
428
- if (!(host && proto)) {
429
- _context.next = 11;
430
- break;
369
+ }, _callee2);
370
+ }))();
371
+ },
372
+ onDisable: function onDisable() {
373
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
374
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
375
+ while (1) switch (_context3.prev = _context3.next) {
376
+ case 0:
377
+ nextConfig.draftMode().disable();
378
+ case 1:
379
+ case "end":
380
+ return _context3.stop();
431
381
  }
432
- return _context.abrupt("return", "".concat(proto, "://").concat(host));
433
- case 11:
434
- return _context.abrupt("return", null);
435
- case 14:
436
- _context.prev = 14;
437
- _context.t0 = _context["catch"](0);
438
- console.error("Val: could not read headers! fetchVal can only be used server-side. Use useVal on clients.", _context.t0);
439
- return _context.abrupt("return", null);
440
- case 18:
441
- case "end":
442
- return _context.stop();
443
- }
444
- }, _callee, null, [[0, 14]]);
445
- }));
446
- return _getHost.apply(this, arguments);
447
- }
448
- function getValHeaders() {
449
- try {
450
- // const cs = cookies(); // TODO: simply get all headers?
451
- var cs = {
452
- get: function get(s) {
453
- return {
454
- value: s
455
- };
382
+ }, _callee3);
383
+ }))();
384
+ }
385
+ }), function (valRes) {
386
+ var headers = "headers" in valRes ? new Headers(valRes.headers) : new Headers({});
387
+ if ("cookies" in valRes && valRes.cookies) {
388
+ headers.set("Set-Cookie", "");
389
+ for (var _i = 0, _Object$entries = Object.entries(valRes.cookies); _i < _Object$entries.length; _i++) {
390
+ var _cookie$options, _cookie$options2, _cookie$options3, _cookie$options4, _cookie$options5;
391
+ var _Object$entries$_i = slicedToArray._slicedToArray(_Object$entries[_i], 2),
392
+ cookieName = _Object$entries$_i[0],
393
+ cookie = _Object$entries$_i[1];
394
+ 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" : ""));
395
+ headers.append("Set-Cookie", cookieValue);
456
396
  }
457
- };
458
- var session = cs.get(core.Internal.VAL_SESSION_COOKIE);
459
- if (session) {
460
- return {
461
- Cookie: "".concat(core.Internal.VAL_SESSION_COOKIE, "=").concat(session.value)
462
- };
463
397
  }
464
- return {};
465
- } catch (err) {
466
- console.error("Val: could not read cookies! fetchVal can only be used server-side. Use useVal on clients.", err);
467
- return {};
468
- }
469
- }
470
- function safeDraftModeEnabled() {
471
- try {
472
- return headers.draftMode().isEnabled;
473
- } catch (err) {
474
- console.error("Val: could not read draft mode! fetchVal can only be used server-side. Use useVal on clients.", err);
475
- return false;
476
- }
398
+ if ("json" in valRes) {
399
+ headers.set("Content-Type", "application/json");
400
+ return server$1.NextResponse.json(valRes.json, {
401
+ headers: headers,
402
+ status: valRes.status
403
+ });
404
+ } else if (valRes.status === 302) {
405
+ headers.set("Location", valRes.redirectTo);
406
+ return server$1.NextResponse.redirect(valRes.redirectTo, {
407
+ status: valRes.status,
408
+ headers: headers
409
+ });
410
+ }
411
+ return new server$1.NextResponse("body" in valRes ? valRes.body : null, {
412
+ headers: headers,
413
+ status: valRes.status
414
+ });
415
+ });
416
+ };
417
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
418
+ function initValServer(config, nextConfig) {
419
+ return {
420
+ valNextAppRouter: initValNextAppRouter(config, nextConfig)
421
+ };
477
422
  }
478
423
 
479
- exports.fetchVal = fetchVal;
424
+ exports.initValServer = initValServer;
@@ -3,28 +3,9 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  require('server-only');
6
- var stega = require('@valbuild/react/stega');
7
- var core = require('@valbuild/core');
8
- var fp = require('@valbuild/core/fp');
9
- var headers = require('next/headers');
10
-
11
- function _interopNamespace(e) {
12
- if (e && e.__esModule) return e;
13
- var n = Object.create(null);
14
- if (e) {
15
- Object.keys(e).forEach(function (k) {
16
- if (k !== 'default') {
17
- var d = Object.getOwnPropertyDescriptor(e, k);
18
- Object.defineProperty(n, k, d.get ? d : {
19
- enumerable: true,
20
- get: function () { return e[k]; }
21
- });
22
- }
23
- });
24
- }
25
- n["default"] = e;
26
- return Object.freeze(n);
27
- }
6
+ var slicedToArray = require('../../dist/slicedToArray-4190fac1.cjs.prod.js');
7
+ var server = require('@valbuild/server');
8
+ var server$1 = require('next/server');
28
9
 
29
10
  function _regeneratorRuntime() {
30
11
  _regeneratorRuntime = function () {
@@ -359,121 +340,85 @@ function _asyncToGenerator(fn) {
359
340
  };
360
341
  }
361
342
 
362
- var valApiEndpoints = "/api/val"; // TODO: get from config
363
- function fetchVal(selector) {
364
- var enabled = safeDraftModeEnabled();
365
- if (enabled) {
366
- getHost().then(function (host) {
367
- //
368
- if (host) {
369
- // TODO: Use the content.val.build endpoints directly
370
- var api = new core.ValApi("".concat(host).concat(valApiEndpoints));
371
- // Optimize: only fetch the modules needed, also cache by module id and revalidate when patched
372
- // const valModuleIds = getModuleIds(selector);
373
- return api.getModules({
374
- patch: true,
375
- includeSource: true,
376
- headers: getValHeaders()
377
- }).then(function (res) {
378
- if (fp.result.isOk(res)) {
379
- var modules = res.value.modules;
380
- return stega.stegaEncode(selector, {
381
- getModule: function getModule(moduleId) {
382
- var module = modules[moduleId];
383
- if (module) {
384
- return module.source;
385
- }
386
- }
387
- });
388
- } else {
389
- console.error("Val: could not fetch modules", res.error);
343
+ var initValNextAppRouter = function initValNextAppRouter(config, nextConfig) {
344
+ var route = "/api/val"; // TODO: get from config
345
+ return server.createValApiRouter(route, server.createValServer(route, config, {
346
+ isEnabled: function isEnabled() {
347
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
348
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
349
+ while (1) switch (_context.prev = _context.next) {
350
+ case 0:
351
+ return _context.abrupt("return", nextConfig.draftMode().isEnabled);
352
+ case 1:
353
+ case "end":
354
+ return _context.stop();
390
355
  }
391
- return stega.stegaEncode(selector, {});
392
- })["catch"](function (err) {
393
- console.error("Val: failed while checking modules", err);
394
- return selector;
395
- });
396
- }
397
- });
398
- }
399
- return stega.stegaEncode(selector, {
400
- disabled: !enabled
401
- });
402
- }
403
- function getHost() {
404
- return _getHost.apply(this, arguments);
405
- }
406
- function _getHost() {
407
- _getHost = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
408
- var _hs$get, _yield$import, headers, hs, host, proto;
409
- return _regeneratorRuntime().wrap(function _callee$(_context) {
410
- while (1) switch (_context.prev = _context.next) {
411
- case 0:
412
- _context.prev = 0;
413
- _context.next = 3;
414
- return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('next/headers')); });
415
- case 3:
416
- _yield$import = _context.sent;
417
- headers = _yield$import.headers;
418
- hs = headers();
419
- host = hs.get("host");
420
- proto = "https";
421
- if (hs.get("x-forwarded-proto") === "http") {
422
- proto = "http";
423
- } else if ((_hs$get = hs.get("referer")) !== null && _hs$get !== void 0 && _hs$get.startsWith("http://")) {
424
- proto = "http";
425
- } else if (host !== null && host !== void 0 && host.startsWith("localhost")) {
426
- proto = "http";
356
+ }, _callee);
357
+ }))();
358
+ },
359
+ onEnable: function onEnable() {
360
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
361
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
362
+ while (1) switch (_context2.prev = _context2.next) {
363
+ case 0:
364
+ nextConfig.draftMode().enable();
365
+ case 1:
366
+ case "end":
367
+ return _context2.stop();
427
368
  }
428
- if (!(host && proto)) {
429
- _context.next = 11;
430
- break;
369
+ }, _callee2);
370
+ }))();
371
+ },
372
+ onDisable: function onDisable() {
373
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
374
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
375
+ while (1) switch (_context3.prev = _context3.next) {
376
+ case 0:
377
+ nextConfig.draftMode().disable();
378
+ case 1:
379
+ case "end":
380
+ return _context3.stop();
431
381
  }
432
- return _context.abrupt("return", "".concat(proto, "://").concat(host));
433
- case 11:
434
- return _context.abrupt("return", null);
435
- case 14:
436
- _context.prev = 14;
437
- _context.t0 = _context["catch"](0);
438
- console.error("Val: could not read headers! fetchVal can only be used server-side. Use useVal on clients.", _context.t0);
439
- return _context.abrupt("return", null);
440
- case 18:
441
- case "end":
442
- return _context.stop();
443
- }
444
- }, _callee, null, [[0, 14]]);
445
- }));
446
- return _getHost.apply(this, arguments);
447
- }
448
- function getValHeaders() {
449
- try {
450
- // const cs = cookies(); // TODO: simply get all headers?
451
- var cs = {
452
- get: function get(s) {
453
- return {
454
- value: s
455
- };
382
+ }, _callee3);
383
+ }))();
384
+ }
385
+ }), function (valRes) {
386
+ var headers = "headers" in valRes ? new Headers(valRes.headers) : new Headers({});
387
+ if ("cookies" in valRes && valRes.cookies) {
388
+ headers.set("Set-Cookie", "");
389
+ for (var _i = 0, _Object$entries = Object.entries(valRes.cookies); _i < _Object$entries.length; _i++) {
390
+ var _cookie$options, _cookie$options2, _cookie$options3, _cookie$options4, _cookie$options5;
391
+ var _Object$entries$_i = slicedToArray._slicedToArray(_Object$entries[_i], 2),
392
+ cookieName = _Object$entries$_i[0],
393
+ cookie = _Object$entries$_i[1];
394
+ 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" : ""));
395
+ headers.append("Set-Cookie", cookieValue);
456
396
  }
457
- };
458
- var session = cs.get(core.Internal.VAL_SESSION_COOKIE);
459
- if (session) {
460
- return {
461
- Cookie: "".concat(core.Internal.VAL_SESSION_COOKIE, "=").concat(session.value)
462
- };
463
397
  }
464
- return {};
465
- } catch (err) {
466
- console.error("Val: could not read cookies! fetchVal can only be used server-side. Use useVal on clients.", err);
467
- return {};
468
- }
469
- }
470
- function safeDraftModeEnabled() {
471
- try {
472
- return headers.draftMode().isEnabled;
473
- } catch (err) {
474
- console.error("Val: could not read draft mode! fetchVal can only be used server-side. Use useVal on clients.", err);
475
- return false;
476
- }
398
+ if ("json" in valRes) {
399
+ headers.set("Content-Type", "application/json");
400
+ return server$1.NextResponse.json(valRes.json, {
401
+ headers: headers,
402
+ status: valRes.status
403
+ });
404
+ } else if (valRes.status === 302) {
405
+ headers.set("Location", valRes.redirectTo);
406
+ return server$1.NextResponse.redirect(valRes.redirectTo, {
407
+ status: valRes.status,
408
+ headers: headers
409
+ });
410
+ }
411
+ return new server$1.NextResponse("body" in valRes ? valRes.body : null, {
412
+ headers: headers,
413
+ status: valRes.status
414
+ });
415
+ });
416
+ };
417
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
418
+ function initValServer(config, nextConfig) {
419
+ return {
420
+ valNextAppRouter: initValNextAppRouter(config, nextConfig)
421
+ };
477
422
  }
478
423
 
479
- exports.fetchVal = fetchVal;
424
+ exports.initValServer = initValServer;