@pradip1995/segment-primitives 0.4.7 → 0.4.8
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 +43 -0
package/package.json
CHANGED
package/src/resolve-media-url.ts
CHANGED
|
@@ -100,3 +100,46 @@ function rewriteStoredBackendUrl(url: string): string {
|
|
|
100
100
|
|
|
101
101
|
return `${parsedPublic.origin}${parsedStored.pathname}${parsedStored.search}${parsedStored.hash}`
|
|
102
102
|
}
|
|
103
|
+
|
|
104
|
+
type MediaRef = { url?: string | null } | string | null | undefined
|
|
105
|
+
|
|
106
|
+
type CartLineImageSource = {
|
|
107
|
+
thumbnail?: string | null
|
|
108
|
+
product?: {
|
|
109
|
+
thumbnail?: string | null
|
|
110
|
+
images?: Array<{ url?: string | null }> | null
|
|
111
|
+
} | null
|
|
112
|
+
variant?: {
|
|
113
|
+
thumbnail?: string | null
|
|
114
|
+
images?: Array<MediaRef> | null
|
|
115
|
+
product?: {
|
|
116
|
+
thumbnail?: string | null
|
|
117
|
+
images?: Array<{ url?: string | null }> | null
|
|
118
|
+
} | null
|
|
119
|
+
} | null
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function mediaRefUrl(value: MediaRef): string | null {
|
|
123
|
+
if (!value) return null
|
|
124
|
+
if (typeof value === "string") return value
|
|
125
|
+
return value.url ?? null
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Line-item thumbnail, then variant/product thumbnail, then first gallery image. */
|
|
129
|
+
export function resolveCartLineItemImage(item: CartLineImageSource): string | null {
|
|
130
|
+
const product = item.product || item.variant?.product || null
|
|
131
|
+
const candidates = [
|
|
132
|
+
item.thumbnail,
|
|
133
|
+
item.variant?.thumbnail,
|
|
134
|
+
product?.thumbnail,
|
|
135
|
+
mediaRefUrl(item.variant?.images?.[0]),
|
|
136
|
+
product?.images?.[0]?.url,
|
|
137
|
+
]
|
|
138
|
+
|
|
139
|
+
for (const candidate of candidates) {
|
|
140
|
+
const resolved = resolveMediaUrl(candidate)
|
|
141
|
+
if (resolved) return resolved
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return null
|
|
145
|
+
}
|