@pradip1995/segment-primitives 0.4.3 → 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.3",
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
+ }
@@ -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
+ }