@nimpl/getters 1.3.1 → 1.3.2-canary.1
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/create-server-context.d.ts +1 -1
- package/create-server-context.js +2 -1
- package/get-page-config.js +6 -6
- package/get-params.js +17 -10
- package/get-pathname.js +3 -5
- package/get-search-params.d.ts +1 -1
- package/get-search-params.js +6 -6
- package/get-server-context.d.ts +1 -1
- package/get-server-context.js +1 -1
- package/package.json +1 -1
- package/server-getter-in-client-component-error.js +1 -1
- package/utils.js +13 -13
package/create-server-context.js
CHANGED
|
@@ -6,12 +6,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const async_hooks_1 = require("async_hooks");
|
|
7
7
|
const server_getter_in_client_component_error_1 = require("./server-getter-in-client-component-error");
|
|
8
8
|
const react_1 = __importDefault(require("react"));
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
10
|
const Enter = ({ storage, value }) => {
|
|
10
11
|
storage.enterWith({ value });
|
|
11
12
|
return react_1.default.createElement(react_1.default.Fragment, null);
|
|
12
13
|
};
|
|
13
14
|
function createServerContext(defaultValue) {
|
|
14
|
-
(0, server_getter_in_client_component_error_1.serverGetterInClientComponentError)(
|
|
15
|
+
(0, server_getter_in_client_component_error_1.serverGetterInClientComponentError)("createServerContext");
|
|
15
16
|
const storage = new async_hooks_1.AsyncLocalStorage();
|
|
16
17
|
return {
|
|
17
18
|
Provider: async ({ children, value }) => {
|
package/get-page-config.js
CHANGED
|
@@ -4,21 +4,21 @@ exports.getPageConfig = void 0;
|
|
|
4
4
|
const static_generation_async_storage_external_1 = require("next/dist/client/components/static-generation-async-storage.external");
|
|
5
5
|
const server_getter_in_client_component_error_1 = require("./server-getter-in-client-component-error");
|
|
6
6
|
const getPageConfig = () => {
|
|
7
|
-
(0, server_getter_in_client_component_error_1.serverGetterInClientComponentError)(
|
|
7
|
+
(0, server_getter_in_client_component_error_1.serverGetterInClientComponentError)("getPageConfig");
|
|
8
8
|
const store = static_generation_async_storage_external_1.staticGenerationAsyncStorage.getStore();
|
|
9
9
|
if (!store)
|
|
10
10
|
return {};
|
|
11
|
-
const basePath = process.env.__NEXT_ROUTER_BASEPATH ||
|
|
11
|
+
const basePath = process.env.__NEXT_ROUTER_BASEPATH || "";
|
|
12
12
|
const { pagePath, forceDynamic, forceStatic, dynamicShouldError, revalidate } = store || {};
|
|
13
|
-
let dynamic =
|
|
13
|
+
let dynamic = "auto";
|
|
14
14
|
if (forceDynamic) {
|
|
15
|
-
dynamic =
|
|
15
|
+
dynamic = "force-dynamic";
|
|
16
16
|
}
|
|
17
17
|
else if (forceStatic) {
|
|
18
|
-
dynamic =
|
|
18
|
+
dynamic = "force-static";
|
|
19
19
|
}
|
|
20
20
|
else if (dynamicShouldError) {
|
|
21
|
-
dynamic =
|
|
21
|
+
dynamic = "error";
|
|
22
22
|
}
|
|
23
23
|
return { pagePath, dynamic, revalidate, basePath };
|
|
24
24
|
};
|
package/get-params.js
CHANGED
|
@@ -5,26 +5,33 @@ const static_generation_async_storage_external_1 = require("next/dist/client/com
|
|
|
5
5
|
const utils_1 = require("./utils");
|
|
6
6
|
const server_getter_in_client_component_error_1 = require("./server-getter-in-client-component-error");
|
|
7
7
|
const getParams = () => {
|
|
8
|
-
(0, server_getter_in_client_component_error_1.serverGetterInClientComponentError)(
|
|
8
|
+
(0, server_getter_in_client_component_error_1.serverGetterInClientComponentError)("getParams");
|
|
9
9
|
const store = static_generation_async_storage_external_1.staticGenerationAsyncStorage.getStore();
|
|
10
10
|
if (!store)
|
|
11
11
|
return {};
|
|
12
|
-
const { urlPathname, pagePath =
|
|
12
|
+
const { urlPathname, pagePath = "/" } = store;
|
|
13
13
|
const cleanUrlPathname = (0, utils_1.normalizePathname)(urlPathname);
|
|
14
14
|
const cleanPagePath = (0, utils_1.normalizePagePath)(pagePath);
|
|
15
|
-
const pagePathParts = cleanPagePath
|
|
15
|
+
const pagePathParts = cleanPagePath
|
|
16
|
+
.split("/")
|
|
17
|
+
.slice(1)
|
|
18
|
+
.filter((part) => !part.match(/^(\([^)]+\)|\@.+)$/));
|
|
16
19
|
const pagePathInterceptedParts = (0, utils_1.normalizeInterceptingRoutes)(pagePathParts);
|
|
17
|
-
const pathnameParts = cleanUrlPathname.split(
|
|
18
|
-
const isRootPage = cleanUrlPathname ===
|
|
20
|
+
const pathnameParts = cleanUrlPathname.split("/").slice(1);
|
|
21
|
+
const isRootPage = cleanUrlPathname === "" && cleanPagePath === "";
|
|
19
22
|
const isNotFoundPage = pagePath.match(/\/_not-found\/?$/);
|
|
20
23
|
const isValidCatchALl = cleanPagePath.match(/\/\[\.\.\.[^\]]+\]/) && pathnameParts.length >= pagePathInterceptedParts.length;
|
|
21
24
|
const isValidOptionalCatchALl = cleanPagePath.match(/\/\[\[\.\.\.[^\]]+\]\]/) && pathnameParts.length >= pagePathInterceptedParts.length - 1;
|
|
22
|
-
const isCorrectMatched = isRootPage ||
|
|
25
|
+
const isCorrectMatched = isRootPage ||
|
|
26
|
+
isNotFoundPage ||
|
|
27
|
+
pagePathInterceptedParts.length === pathnameParts.length ||
|
|
28
|
+
isValidCatchALl ||
|
|
29
|
+
isValidOptionalCatchALl;
|
|
23
30
|
if (!isCorrectMatched) {
|
|
24
|
-
const createIssueUrl = new URL(
|
|
25
|
-
createIssueUrl.searchParams.set(
|
|
26
|
-
createIssueUrl.searchParams.set(
|
|
27
|
-
createIssueUrl.searchParams.append(
|
|
31
|
+
const createIssueUrl = new URL("https://github.com/vordgi/nimpl-getters/issues/new");
|
|
32
|
+
createIssueUrl.searchParams.set("title", "Error parsing segments in get-params");
|
|
33
|
+
createIssueUrl.searchParams.set("body", `urlPathname: \`${urlPathname}\`;\n\npagePath: \`${pagePath}\`;`);
|
|
34
|
+
createIssueUrl.searchParams.append("labels", "bug");
|
|
28
35
|
throw new Error(`Something went wrong. Please create an issue on Github: ${createIssueUrl}`);
|
|
29
36
|
}
|
|
30
37
|
const query = (0, utils_1.parseSegments)(pagePathInterceptedParts, pathnameParts);
|
package/get-pathname.js
CHANGED
|
@@ -6,15 +6,13 @@ const has_base_path_1 = require("next/dist/client/has-base-path");
|
|
|
6
6
|
const remove_base_path_1 = require("next/dist/client/remove-base-path");
|
|
7
7
|
const server_getter_in_client_component_error_1 = require("./server-getter-in-client-component-error");
|
|
8
8
|
function getPathname() {
|
|
9
|
-
(0, server_getter_in_client_component_error_1.serverGetterInClientComponentError)(
|
|
9
|
+
(0, server_getter_in_client_component_error_1.serverGetterInClientComponentError)("getPathname");
|
|
10
10
|
const store = static_generation_async_storage_external_1.staticGenerationAsyncStorage.getStore();
|
|
11
11
|
if (!store)
|
|
12
12
|
return null;
|
|
13
13
|
const { urlPathname } = store;
|
|
14
|
-
const url = new URL(urlPathname,
|
|
15
|
-
const pathname = (0, has_base_path_1.hasBasePath)(url.pathname)
|
|
16
|
-
? (0, remove_base_path_1.removeBasePath)(url.pathname)
|
|
17
|
-
: url.pathname;
|
|
14
|
+
const url = new URL(urlPathname, "http://n");
|
|
15
|
+
const pathname = (0, has_base_path_1.hasBasePath)(url.pathname) ? (0, remove_base_path_1.removeBasePath)(url.pathname) : url.pathname;
|
|
18
16
|
return pathname;
|
|
19
17
|
}
|
|
20
18
|
exports.getPathname = getPathname;
|
package/get-search-params.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ReadonlyURLSearchParams } from
|
|
1
|
+
import { ReadonlyURLSearchParams } from "next/navigation";
|
|
2
2
|
/** @deprecated getSearchParams is deprecated. [Read more](https://nimpl.tech/getters/current-getters/get-search-params) */
|
|
3
3
|
export declare function getSearchParams(opts?: {
|
|
4
4
|
ignoreDynamicOptionErrors?: boolean;
|
package/get-search-params.js
CHANGED
|
@@ -7,24 +7,24 @@ const navigation_1 = require("next/navigation");
|
|
|
7
7
|
const internal_utils_1 = require("next/dist/server/internal-utils");
|
|
8
8
|
/** @deprecated getSearchParams is deprecated. [Read more](https://nimpl.tech/getters/current-getters/get-search-params) */
|
|
9
9
|
function getSearchParams(opts) {
|
|
10
|
-
(0, server_getter_in_client_component_error_1.serverGetterInClientComponentError)(
|
|
11
|
-
console.error(
|
|
10
|
+
(0, server_getter_in_client_component_error_1.serverGetterInClientComponentError)("getSearchParams");
|
|
11
|
+
console.error("getSearchParams is deprecated. Read more - https://nimpl.tech/getters/current-getters/get-search-params");
|
|
12
12
|
const store = static_generation_async_storage_external_1.staticGenerationAsyncStorage.getStore();
|
|
13
13
|
if (!store)
|
|
14
14
|
return new URLSearchParams();
|
|
15
15
|
const { urlPathname, forceStatic, forceDynamic, dynamicShouldError } = store;
|
|
16
16
|
if (!opts?.ignoreDynamicOptionErrors) {
|
|
17
17
|
if (forceStatic) {
|
|
18
|
-
throw new Error(
|
|
18
|
+
throw new Error("Сannot get client search parameters with dynamic=force-static setting");
|
|
19
19
|
}
|
|
20
20
|
else if (dynamicShouldError) {
|
|
21
|
-
throw new Error(
|
|
21
|
+
throw new Error("Сannot get client search parameters with dynamic=error setting");
|
|
22
22
|
}
|
|
23
23
|
else if (!forceDynamic) {
|
|
24
|
-
console.warn(
|
|
24
|
+
console.warn("Do not use getSearchParams with unselected dynamic setting, use force-dynamic instead");
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
|
-
const url = new URL(urlPathname,
|
|
27
|
+
const url = new URL(urlPathname, "http://n");
|
|
28
28
|
const strippedUrl = (0, internal_utils_1.stripInternalSearchParams)(url, true);
|
|
29
29
|
const readonlySearchParams = new navigation_1.ReadonlyURLSearchParams(strippedUrl.searchParams);
|
|
30
30
|
return readonlySearchParams;
|
package/get-server-context.d.ts
CHANGED
package/get-server-context.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const server_getter_in_client_component_error_1 = require("./server-getter-in-client-component-error");
|
|
4
4
|
function getServerContext({ _storage, _defaultValue }) {
|
|
5
|
-
(0, server_getter_in_client_component_error_1.serverGetterInClientComponentError)(
|
|
5
|
+
(0, server_getter_in_client_component_error_1.serverGetterInClientComponentError)("getServerContext");
|
|
6
6
|
const store = _storage.getStore();
|
|
7
7
|
if (!store)
|
|
8
8
|
return _defaultValue;
|
package/package.json
CHANGED
|
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.serverGetterInClientComponentError = void 0;
|
|
7
7
|
const react_1 = __importDefault(require("react"));
|
|
8
8
|
function serverGetterInClientComponentError(getterName) {
|
|
9
|
-
if (process.env.NODE_ENV !==
|
|
9
|
+
if (process.env.NODE_ENV !== "production") {
|
|
10
10
|
// If useState is defined we're in a client component
|
|
11
11
|
if (Boolean(react_1.default.useState)) {
|
|
12
12
|
throw new Error(`${getterName} only works in Server Components`);
|
package/utils.js
CHANGED
|
@@ -2,16 +2,16 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.normalizeInterceptingRoutes = exports.parseSegments = exports.normalizePagePath = exports.normalizePathname = void 0;
|
|
4
4
|
const normalizePathname = (pathname) => {
|
|
5
|
-
const cleanPathname = pathname && new URL(pathname,
|
|
6
|
-
const pathnameWithoutTrailingSlash = cleanPathname?.replace(/^(\/.*)\/$/,
|
|
7
|
-
const pathnameWithoutFileType = pathnameWithoutTrailingSlash?.replace(/\/_not-found$/,
|
|
8
|
-
return pathnameWithoutFileType ||
|
|
5
|
+
const cleanPathname = pathname && new URL(pathname, "http://n").pathname;
|
|
6
|
+
const pathnameWithoutTrailingSlash = cleanPathname?.replace(/^(\/.*)\/$/, "$1");
|
|
7
|
+
const pathnameWithoutFileType = pathnameWithoutTrailingSlash?.replace(/\/_not-found$/, "");
|
|
8
|
+
return pathnameWithoutFileType || "/";
|
|
9
9
|
};
|
|
10
10
|
exports.normalizePathname = normalizePathname;
|
|
11
11
|
const normalizePagePath = (pagePath) => {
|
|
12
|
-
const cleanPagePath = pagePath && new URL(pagePath,
|
|
13
|
-
const pagePathWithoutFileType = cleanPagePath?.replace(/(\/page|\/_not-found)$/,
|
|
14
|
-
return pagePathWithoutFileType ||
|
|
12
|
+
const cleanPagePath = pagePath && new URL(pagePath, "http://n").pathname;
|
|
13
|
+
const pagePathWithoutFileType = cleanPagePath?.replace(/(\/page|\/_not-found)$/, "");
|
|
14
|
+
return pagePathWithoutFileType || "/";
|
|
15
15
|
};
|
|
16
16
|
exports.normalizePagePath = normalizePagePath;
|
|
17
17
|
const parseSegments = (pagePathParts, pathnameParts) => {
|
|
@@ -50,17 +50,17 @@ const normalizeInterceptingRoutes = (pageParts) => {
|
|
|
50
50
|
skip -= 1;
|
|
51
51
|
continue;
|
|
52
52
|
}
|
|
53
|
-
if (pagepart.startsWith(
|
|
54
|
-
normilizedParts.push(pagepart.replace(/^\(\.\.\.\)/,
|
|
53
|
+
if (pagepart.startsWith("(...)")) {
|
|
54
|
+
normilizedParts.push(pagepart.replace(/^\(\.\.\.\)/, ""));
|
|
55
55
|
break;
|
|
56
56
|
}
|
|
57
|
-
else if (pagepart.startsWith(
|
|
58
|
-
normilizedParts.push(pagepart.replace(/^\(\.\)/,
|
|
57
|
+
else if (pagepart.startsWith("(.)")) {
|
|
58
|
+
normilizedParts.push(pagepart.replace(/^\(\.\)/, ""));
|
|
59
59
|
}
|
|
60
|
-
else if (pagepart.startsWith(
|
|
60
|
+
else if (pagepart.startsWith("(..)")) {
|
|
61
61
|
const skipLeafs = pagepart.match(/\(\.\.\)/g);
|
|
62
62
|
skip += skipLeafs?.length || 0;
|
|
63
|
-
normilizedParts.push(pagepart.replace(/^(\(\.\.\))+/,
|
|
63
|
+
normilizedParts.push(pagepart.replace(/^(\(\.\.\))+/, ""));
|
|
64
64
|
}
|
|
65
65
|
else {
|
|
66
66
|
normilizedParts.push(pagepart);
|