@pradip1995/segment-primitives 0.4.5 → 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 +1 -1
- package/src/resolve-media-url.ts +50 -0
package/package.json
CHANGED
package/src/resolve-media-url.ts
CHANGED
|
@@ -13,6 +13,12 @@ export function resolveMediaUrl(
|
|
|
13
13
|
if (!trimmed) return null
|
|
14
14
|
|
|
15
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
|
+
}
|
|
16
22
|
return trimmed
|
|
17
23
|
}
|
|
18
24
|
|
|
@@ -100,3 +106,47 @@ function rewriteStoredBackendUrl(url: string): string {
|
|
|
100
106
|
|
|
101
107
|
return `${parsedPublic.origin}${parsedStored.pathname}${parsedStored.search}${parsedStored.hash}`
|
|
102
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
|
+
|