@pradip1995/segment-primitives 0.4.1 → 0.4.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pradip1995/segment-primitives",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -18,26 +18,26 @@
18
18
  "./format-price": "./src/format-price.ts",
19
19
  "./section-variants": "./src/section-variants.ts",
20
20
  "./product-variants": "./src/product-variants.ts",
21
- "./product-selection-context": "./src/product-selection-context.tsx"
21
+ "./product-selection-context": "./src/product-selection-context.tsx",
22
+ "./is-next-redirect": "./src/is-next-redirect.ts"
23
+ },
24
+ "scripts": {
25
+ "typecheck": "tsc --noEmit",
26
+ "lint": "tsc --noEmit"
22
27
  },
23
28
  "peerDependencies": {
24
29
  "@medusajs/types": "^2.0.0",
25
30
  "@pradip1995/commerce-core": "^4.0.0",
26
31
  "@pradip1995/plugin-sdk": "^0.2.0",
32
+ "next": ">=15",
27
33
  "react": ">=19",
28
- "react-dom": ">=19",
29
- "next": ">=15"
34
+ "react-dom": ">=19"
30
35
  },
31
- "dependencies": {},
32
36
  "devDependencies": {
33
37
  "@medusajs/types": "^2.0.0",
34
38
  "@pradip1995/plugin-sdk": "^0.2.0",
35
39
  "@types/react": "^19",
36
40
  "react": "19.0.3",
37
41
  "typescript": "^5.7.2"
38
- },
39
- "scripts": {
40
- "typecheck": "tsc --noEmit",
41
- "lint": "tsc --noEmit"
42
42
  }
43
- }
43
+ }
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Next.js `redirect()` / `notFound()` throw special control-flow errors.
3
+ * Catch blocks must rethrow these — never render `err.message` ("NEXT_REDIRECT").
4
+ */
5
+
6
+ const CONTROL_FLOW_RE = /NEXT_REDIRECT|NEXT_REDIRECTION|NEXT_NOT_FOUND|NEXT_HTTP_ERROR_FALLBACK/i
7
+
8
+ function textLooksLikeControlFlow(value: string): boolean {
9
+ return CONTROL_FLOW_RE.test(value)
10
+ }
11
+
12
+ export function isNextRedirect(err: unknown): boolean {
13
+ if (err == null) return false
14
+
15
+ if (typeof err === "string") {
16
+ return textLooksLikeControlFlow(err)
17
+ }
18
+
19
+ if (typeof err !== "object") {
20
+ return textLooksLikeControlFlow(String(err))
21
+ }
22
+
23
+ const digest =
24
+ "digest" in err ? String((err as { digest?: unknown }).digest ?? "") : ""
25
+ if (textLooksLikeControlFlow(digest)) {
26
+ return true
27
+ }
28
+
29
+ const message =
30
+ err instanceof Error
31
+ ? err.message
32
+ : "message" in err
33
+ ? String((err as { message?: unknown }).message ?? "")
34
+ : ""
35
+
36
+ if (textLooksLikeControlFlow(message)) {
37
+ return true
38
+ }
39
+
40
+ // Some runtimes stringify the redirect error oddly.
41
+ try {
42
+ return textLooksLikeControlFlow(String(err))
43
+ } catch {
44
+ return false
45
+ }
46
+ }
47
+
48
+ /** Never return a Next.js control-flow token as a user-facing error. */
49
+ export function safeErrorMessage(err: unknown, fallback: string): string {
50
+ if (isNextRedirect(err)) return fallback
51
+ if (err instanceof Error) {
52
+ const msg = err.message?.trim()
53
+ if (!msg || textLooksLikeControlFlow(msg)) return fallback
54
+ return msg
55
+ }
56
+ if (typeof err === "string") {
57
+ const msg = err.trim()
58
+ if (!msg || textLooksLikeControlFlow(msg)) return fallback
59
+ return msg
60
+ }
61
+ return fallback
62
+ }
63
+
64
+ export function isNextRedirectMessage(value: unknown): boolean {
65
+ return typeof value === "string" && textLooksLikeControlFlow(value)
66
+ }