@nimpl/getters 2.0.0-canary.0 → 2.1.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/README.md CHANGED
@@ -4,9 +4,9 @@
4
4
 
5
5
  Implementation of server getters in React Server Components without switching to SSR in Next.js
6
6
 
7
- Before using the library, read the [Possible Issues](https://nimpl.tech/getters/possible-issues)
7
+ Before using the library, read the [Possible Issues](https://nimpl.tech/docs/server-getters#possible-issues)
8
8
 
9
- Visit https://nimpl.tech/getters to view the full documentation.
9
+ Visit https://nimpl.tech/docs/server-getters to view the full documentation.
10
10
 
11
11
  Use `@nimpl/getters 2.x` for Next.js v15. For earlier versions of Next.js use `@nimpl/getters 1.x`.
12
12
 
@@ -26,12 +26,14 @@ yarn add @nimpl/getters
26
26
 
27
27
  ## Current Getters
28
28
 
29
- - [get-pathname](https://nimpl.tech/getters/current-getters/get-pathname)
30
- - [get-page-config](https://nimpl.tech/getters/current-getters/get-page-config)
31
- - [get-params](https://nimpl.tech/getters/current-getters/get-params)
32
- - [get-search-params](https://nimpl.tech/getters/current-getters/get-search-params) (_deprecated_)
29
+ - [get-pathname](https://nimpl.tech/docs/server-getters#get-pathname)
30
+ - [get-app-pathname](https://nimpl.tech/docs/server-getters#get-app-pathname)
31
+ - [get-params](https://nimpl.tech/docs/server-getters#get-params)
32
+ - [get-app-params](https://nimpl.tech/docs/server-getters#get-app-params)
33
+ - [get-page-config](https://nimpl.tech/docs/server-getters#get-page-config)
34
+ - [get-search-params](https://nimpl.tech/docs/server-getters#get-search-params) (_deprecated_)
33
35
 
34
- > _`create-server-context` and `get-server-context` were moved to a separate package - [@nimpl/context](https://nimpl.tech/context)_
36
+ > _`create-server-context` and `get-server-context` were moved to a separate package - [@nimpl/context](https://nimpl.tech/docs/context)_
35
37
 
36
38
  ## Stability
37
39
 
@@ -41,6 +43,6 @@ In this way, you can be sure not only of the stability of the code, but also tha
41
43
 
42
44
  ## Additional
43
45
 
44
- Please consider giving a star if you like it, it will help promote the implementation in the eyes of the Next.js team.
46
+ Please consider giving a star if you like it, it will help promote the implementation in the eyes of the Next.js team. This also motivates the author to continue working on this and other solutions ❤️
45
47
 
46
- Create tasks for identified issues, desired getters, or various improvements.
48
+ Create issues for identified problems, desired getters, or various improvements.
@@ -0,0 +1,16 @@
1
+ type GetParamsOptions = {
2
+ /**
3
+ * In case of error or segments difference
4
+ * getter will return null
5
+ * (_f.e. `/_not-found` could be at `/it/removed-page` and it would not have any params_)
6
+ */
7
+ ignoreDifferenceError?: true;
8
+ /** Custom pathname (f.e. `["/example/custom"]`). Usable for rewritten pages in SSR or custom functions */
9
+ pathname?: string;
10
+ /** Custom pagePaths list (f.e. `["/example/[slug]/page"]`). Usable for rewritten pages in SSR or custom functions */
11
+ pagePaths?: string[];
12
+ };
13
+ export declare const getAppParams: (options?: GetParamsOptions) => {
14
+ [key: string]: string | string[];
15
+ };
16
+ export {};
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getAppParams = void 0;
4
+ const work_async_storage_external_1 = require("next/dist/server/app-render/work-async-storage.external");
5
+ const work_unit_async_storage_external_1 = require("next/dist/server/app-render/work-unit-async-storage.external");
6
+ const server_getter_in_client_component_error_1 = require("./server-getter-in-client-component-error");
7
+ const utils_1 = require("./utils");
8
+ const get_app_pathname_1 = require("./get-app-pathname");
9
+ const getAppParams = (options = {}) => {
10
+ (0, server_getter_in_client_component_error_1.serverGetterInClientComponentError)("getAppParams");
11
+ const store = work_async_storage_external_1.workAsyncStorage.getStore();
12
+ if (!store)
13
+ return {};
14
+ const { ignoreDifferenceError, pagePaths, pathname } = options;
15
+ const pageConfigurationStore = work_async_storage_external_1.workAsyncStorage.getStore();
16
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
+ const pageStore = work_unit_async_storage_external_1.workUnitAsyncStorage.getStore();
18
+ if (!pageConfigurationStore || !pageStore?.url?.pathname)
19
+ return {};
20
+ const { route } = pageConfigurationStore;
21
+ const pagePath = route;
22
+ const urlPathname = (0, get_app_pathname_1.getAppPathname)();
23
+ const targetUrlPathname = pathname || urlPathname;
24
+ let isInvalid = false;
25
+ try {
26
+ if (pagePaths) {
27
+ for (const userPagePath of pagePaths) {
28
+ const params = (0, utils_1.parseParams)(targetUrlPathname, userPagePath);
29
+ if (params === utils_1.INVALID_PARSE) {
30
+ isInvalid ||= true;
31
+ continue;
32
+ }
33
+ return params || {};
34
+ }
35
+ }
36
+ else {
37
+ const params = (0, utils_1.parseParams)(targetUrlPathname, pagePath);
38
+ if (params === utils_1.INVALID_PARSE) {
39
+ isInvalid ||= true;
40
+ }
41
+ else {
42
+ return params || {};
43
+ }
44
+ }
45
+ }
46
+ catch {
47
+ isInvalid = true;
48
+ }
49
+ if (isInvalid && !ignoreDifferenceError) {
50
+ const createIssueUrl = new URL("https://github.com/vordgi/nimpl-getters/issues/new");
51
+ createIssueUrl.searchParams.set("title", "Error parsing segments in get-params");
52
+ createIssueUrl.searchParams.set("body", `urlPathname: \`${urlPathname}\`;\n\npagePath(s): \`${pagePaths || pagePath}\`;`);
53
+ createIssueUrl.searchParams.append("labels", "bug");
54
+ throw new Error(`Something went wrong. Please create an issue on Github: ${createIssueUrl}`);
55
+ }
56
+ return {};
57
+ };
58
+ exports.getAppParams = getAppParams;
@@ -0,0 +1 @@
1
+ export declare function getAppPathname(): any;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getAppPathname = void 0;
4
+ const work_unit_async_storage_external_1 = require("next/dist/server/app-render/work-unit-async-storage.external");
5
+ const server_getter_in_client_component_error_1 = require("./server-getter-in-client-component-error");
6
+ function getAppPathname() {
7
+ (0, server_getter_in_client_component_error_1.serverGetterInClientComponentError)("getAppPathname");
8
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
+ const pageStore = work_unit_async_storage_external_1.workUnitAsyncStorage.getStore();
10
+ const pageTags = pageStore.implicitTags.filter((tag) => tag.startsWith("_N_T_/"));
11
+ const pathnameTag = pageTags[pageTags.length - 1];
12
+ return pathnameTag.substring(5);
13
+ }
14
+ exports.getAppPathname = getAppPathname;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nimpl/getters",
3
- "version": "2.0.0-canary.0",
3
+ "version": "2.1.0",
4
4
  "description": "Implementation of server getters in React Server Components without switching to SSR in next.js",
5
5
  "files": [
6
6
  "**/*.js",
@@ -18,6 +18,7 @@
18
18
  "getters",
19
19
  "implementation",
20
20
  "get-params",
21
+ "app router",
21
22
  "rsc"
22
23
  ],
23
24
  "repository": {
@@ -43,4 +44,4 @@
43
44
  "react-dom": ">= 18.2.0",
44
45
  "next": ">= 14.0.0"
45
46
  }
46
- }
47
+ }