@pradip1995/segment-primitives 0.4.2 → 0.4.4

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.2",
3
+ "version": "0.4.4",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -16,15 +16,12 @@
16
16
  "./button": "./src/Button.tsx",
17
17
  "./card": "./src/Card.tsx",
18
18
  "./format-price": "./src/format-price.ts",
19
+ "./resolve-media-url": "./src/resolve-media-url.ts",
19
20
  "./section-variants": "./src/section-variants.ts",
20
21
  "./product-variants": "./src/product-variants.ts",
21
22
  "./product-selection-context": "./src/product-selection-context.tsx",
22
23
  "./is-next-redirect": "./src/is-next-redirect.ts"
23
24
  },
24
- "scripts": {
25
- "typecheck": "tsc --noEmit",
26
- "lint": "tsc --noEmit"
27
- },
28
25
  "peerDependencies": {
29
26
  "@medusajs/types": "^2.0.0",
30
27
  "@pradip1995/commerce-core": "^4.0.0",
@@ -39,5 +36,9 @@
39
36
  "@types/react": "^19",
40
37
  "react": "19.0.3",
41
38
  "typescript": "^5.7.2"
39
+ },
40
+ "scripts": {
41
+ "typecheck": "tsc --noEmit",
42
+ "lint": "tsc --noEmit"
42
43
  }
43
- }
44
+ }
@@ -2,16 +2,27 @@
2
2
  * Next.js `redirect()` / `notFound()` throw special control-flow errors.
3
3
  * Catch blocks must rethrow these — never render `err.message` ("NEXT_REDIRECT").
4
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
+
5
12
  export function isNextRedirect(err: unknown): boolean {
6
- if (!err || typeof err !== "object") return false
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
+ }
7
22
 
8
23
  const digest =
9
24
  "digest" in err ? String((err as { digest?: unknown }).digest ?? "") : ""
10
- if (
11
- digest.includes("NEXT_REDIRECT") ||
12
- digest.includes("NEXT_HTTP_ERROR_FALLBACK") ||
13
- digest.includes("NEXT_NOT_FOUND")
14
- ) {
25
+ if (textLooksLikeControlFlow(digest)) {
15
26
  return true
16
27
  }
17
28
 
@@ -22,9 +33,34 @@ export function isNextRedirect(err: unknown): boolean {
22
33
  ? String((err as { message?: unknown }).message ?? "")
23
34
  : ""
24
35
 
25
- return (
26
- message === "NEXT_REDIRECT" ||
27
- message === "NEXT_NOT_FOUND" ||
28
- message.includes("NEXT_REDIRECT")
29
- )
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)
30
66
  }
@@ -0,0 +1,63 @@
1
+ /** Normalize Medusa / S3 / relative media paths and rewrite stale localhost URLs. */
2
+ export function resolveMediaUrl(
3
+ url: string | null | undefined
4
+ ): string | null {
5
+ if (!url || typeof url !== "string") return null
6
+
7
+ const trimmed = url.trim()
8
+ if (!trimmed) return null
9
+
10
+ if (trimmed.startsWith("/")) {
11
+ return trimmed
12
+ }
13
+
14
+ if (trimmed.startsWith("http://") || trimmed.startsWith("https://")) {
15
+ return rewriteStoredBackendUrl(trimmed)
16
+ }
17
+
18
+ const base =
19
+ process.env.FILE_LOCAL_BACKEND_URL ||
20
+ process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL ||
21
+ process.env.MEDUSA_BACKEND_URL ||
22
+ "http://localhost:9000"
23
+
24
+ return `${base.replace(/\/$/, "")}/${trimmed.replace(/^\//, "")}`
25
+ }
26
+
27
+ function rewriteStoredBackendUrl(url: string): string {
28
+ const publicBase =
29
+ process.env.FILE_LOCAL_BACKEND_URL ||
30
+ process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL
31
+
32
+ if (!publicBase) return url
33
+
34
+ let parsedPublic: URL
35
+ try {
36
+ parsedPublic = new URL(publicBase)
37
+ } catch {
38
+ return url
39
+ }
40
+
41
+ let parsedStored: URL
42
+ try {
43
+ parsedStored = new URL(url)
44
+ } catch {
45
+ return url
46
+ }
47
+
48
+ const isLocalStoredHost =
49
+ parsedStored.hostname === "localhost" ||
50
+ parsedStored.hostname === "127.0.0.1" ||
51
+ parsedStored.hostname === "backend"
52
+
53
+ if (!isLocalStoredHost) return url
54
+
55
+ if (
56
+ parsedStored.port === parsedPublic.port &&
57
+ parsedStored.hostname === parsedPublic.hostname
58
+ ) {
59
+ return url
60
+ }
61
+
62
+ return `${parsedPublic.origin}${parsedStored.pathname}${parsedStored.search}${parsedStored.hash}`
63
+ }