@pradip1995/segment-primitives 0.4.3 → 0.4.5

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.3",
3
+ "version": "0.4.5",
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
+ }
@@ -0,0 +1,102 @@
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
+ let trimmed = url.trim()
8
+ if (!trimmed) return null
9
+
10
+ // Uploads stored while AWS_FILE_URL / backend_url was unset:
11
+ // "undefined/uploadsFoo.webp" or "undefined/uploads/Foo.webp"
12
+ trimmed = rewriteBrokenUndefinedUploadUrl(trimmed)
13
+ if (!trimmed) return null
14
+
15
+ if (trimmed.startsWith("/")) {
16
+ return trimmed
17
+ }
18
+
19
+ if (trimmed.startsWith("http://") || trimmed.startsWith("https://")) {
20
+ return rewriteStoredBackendUrl(trimmed)
21
+ }
22
+
23
+ const base = mediaBackendBase()
24
+ if (!base) return null
25
+
26
+ return `${base.replace(/\/$/, "")}/${trimmed.replace(/^\//, "")}`
27
+ }
28
+
29
+ function mediaBackendBase(): string | null {
30
+ const raw =
31
+ process.env.FILE_LOCAL_BACKEND_URL ||
32
+ process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL ||
33
+ process.env.MEDUSA_BACKEND_URL ||
34
+ "http://localhost:9000"
35
+
36
+ if (!raw || raw === "undefined") return null
37
+ return raw
38
+ }
39
+
40
+ /** Fix DB values like `undefined/uploadsAnalyze_….webp` from misconfigured file provider. */
41
+ function rewriteBrokenUndefinedUploadUrl(url: string): string | null {
42
+ if (!url.startsWith("undefined/") && !url.startsWith("undefined")) {
43
+ return url
44
+ }
45
+
46
+ const withoutUndefined = url.replace(/^undefined\/?/, "")
47
+ // "uploadsFilename" → "uploads/Filename"; keep "uploads/..." as-is
48
+ const withSlash = withoutUndefined.replace(
49
+ /^uploads(?!\/)(.+)$/i,
50
+ "uploads/$1"
51
+ )
52
+
53
+ const base = mediaBackendBase()
54
+ if (!base) return null
55
+
56
+ // Local file provider serves from /static; S3 keys are usually uploads/...
57
+ const path = withSlash.startsWith("static/")
58
+ ? withSlash
59
+ : withSlash.startsWith("uploads/")
60
+ ? `static/${withSlash}`
61
+ : withSlash
62
+
63
+ return `${base.replace(/\/$/, "")}/${path.replace(/^\//, "")}`
64
+ }
65
+
66
+ function rewriteStoredBackendUrl(url: string): string {
67
+ const publicBase =
68
+ process.env.FILE_LOCAL_BACKEND_URL ||
69
+ process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL
70
+
71
+ if (!publicBase) return url
72
+
73
+ let parsedPublic: URL
74
+ try {
75
+ parsedPublic = new URL(publicBase)
76
+ } catch {
77
+ return url
78
+ }
79
+
80
+ let parsedStored: URL
81
+ try {
82
+ parsedStored = new URL(url)
83
+ } catch {
84
+ return url
85
+ }
86
+
87
+ const isLocalStoredHost =
88
+ parsedStored.hostname === "localhost" ||
89
+ parsedStored.hostname === "127.0.0.1" ||
90
+ parsedStored.hostname === "backend"
91
+
92
+ if (!isLocalStoredHost) return url
93
+
94
+ if (
95
+ parsedStored.port === parsedPublic.port &&
96
+ parsedStored.hostname === parsedPublic.hostname
97
+ ) {
98
+ return url
99
+ }
100
+
101
+ return `${parsedPublic.origin}${parsedStored.pathname}${parsedStored.search}${parsedStored.hash}`
102
+ }