@pradip1995/segment-primitives 0.4.4 → 0.4.6

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.4",
3
+ "version": "0.4.6",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -4,10 +4,21 @@ export function resolveMediaUrl(
4
4
  ): string | null {
5
5
  if (!url || typeof url !== "string") return null
6
6
 
7
- const trimmed = url.trim()
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)
8
13
  if (!trimmed) return null
9
14
 
10
15
  if (trimmed.startsWith("/")) {
16
+ const isBackendUpload =
17
+ trimmed.startsWith("/static/") || trimmed.startsWith("/uploads/")
18
+ if (isBackendUpload) {
19
+ const base = mediaBackendBase()
20
+ if (base) return `${base.replace(/\/$/, "")}${trimmed}`
21
+ }
11
22
  return trimmed
12
23
  }
13
24
 
@@ -15,13 +26,47 @@ export function resolveMediaUrl(
15
26
  return rewriteStoredBackendUrl(trimmed)
16
27
  }
17
28
 
18
- const base =
29
+ const base = mediaBackendBase()
30
+ if (!base) return null
31
+
32
+ return `${base.replace(/\/$/, "")}/${trimmed.replace(/^\//, "")}`
33
+ }
34
+
35
+ function mediaBackendBase(): string | null {
36
+ const raw =
19
37
  process.env.FILE_LOCAL_BACKEND_URL ||
20
38
  process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL ||
21
39
  process.env.MEDUSA_BACKEND_URL ||
22
40
  "http://localhost:9000"
23
41
 
24
- return `${base.replace(/\/$/, "")}/${trimmed.replace(/^\//, "")}`
42
+ if (!raw || raw === "undefined") return null
43
+ return raw
44
+ }
45
+
46
+ /** Fix DB values like `undefined/uploadsAnalyze_….webp` from misconfigured file provider. */
47
+ function rewriteBrokenUndefinedUploadUrl(url: string): string | null {
48
+ if (!url.startsWith("undefined/") && !url.startsWith("undefined")) {
49
+ return url
50
+ }
51
+
52
+ const withoutUndefined = url.replace(/^undefined\/?/, "")
53
+ // "uploadsFilename" → "uploads/Filename"; keep "uploads/..." as-is
54
+ const withSlash = withoutUndefined.replace(
55
+ /^uploads(?!\/)(.+)$/i,
56
+ "uploads/$1"
57
+ )
58
+
59
+ const base = mediaBackendBase()
60
+ if (!base) return null
61
+
62
+ // Local file provider serves from /static; S3 keys are usually uploads/...
63
+ const path = withSlash.startsWith("static/")
64
+ ? withSlash
65
+ : withSlash.startsWith("uploads/")
66
+ ? `static/${withSlash}`
67
+ : withSlash
68
+
69
+ return `${base.replace(/\/$/, "")}/${path.replace(/^\//, "")}`
25
70
  }
26
71
 
27
72
  function rewriteStoredBackendUrl(url: string): string {
@@ -61,3 +106,47 @@ function rewriteStoredBackendUrl(url: string): string {
61
106
 
62
107
  return `${parsedPublic.origin}${parsedStored.pathname}${parsedStored.search}${parsedStored.hash}`
63
108
  }
109
+
110
+ type MediaRef = { url?: string | null } | string | null | undefined
111
+
112
+ type CartLineImageSource = {
113
+ thumbnail?: string | null
114
+ product?: {
115
+ thumbnail?: string | null
116
+ images?: Array<{ url?: string | null }> | null
117
+ } | null
118
+ variant?: {
119
+ thumbnail?: string | null
120
+ images?: Array<MediaRef> | null
121
+ product?: {
122
+ thumbnail?: string | null
123
+ images?: Array<{ url?: string | null }> | null
124
+ } | null
125
+ } | null
126
+ }
127
+
128
+ function mediaRefUrl(value: MediaRef): string | null {
129
+ if (!value) return null
130
+ if (typeof value === "string") return value
131
+ return value.url ?? null
132
+ }
133
+
134
+ /** Line-item thumbnail, then variant/product thumbnail, then first gallery image. */
135
+ export function resolveCartLineItemImage(item: CartLineImageSource): string | null {
136
+ const product = item.product || item.variant?.product || null
137
+ const candidates = [
138
+ item.thumbnail,
139
+ item.variant?.thumbnail,
140
+ product?.thumbnail,
141
+ mediaRefUrl(item.variant?.images?.[0]),
142
+ product?.images?.[0]?.url,
143
+ ]
144
+
145
+ for (const candidate of candidates) {
146
+ const resolved = resolveMediaUrl(candidate)
147
+ if (resolved) return resolved
148
+ }
149
+
150
+ return null
151
+ }
152
+