@jay-framework/wix-utils 0.24.0 → 0.24.1
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/dist/index.d.ts +77 -0
- package/dist/index.js +83 -0
- package/package.json +1 -1
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wix Media URL Utilities
|
|
3
|
+
*
|
|
4
|
+
* Helpers for working with Wix media URLs and the wix:// protocol formats.
|
|
5
|
+
*
|
|
6
|
+
* Supported formats:
|
|
7
|
+
* - wix:image://v1/<id>/<name>#originWidth=<w>&originHeight=<h>
|
|
8
|
+
* - wix:video://v1/<id>/<name>#posterUri=<uri>&posterWidth=<w>&posterHeight=<h>
|
|
9
|
+
* - wix:document://v1/<id>/<name>
|
|
10
|
+
* - wix:audio://v1/<id>/<name>
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Strip /v1/... resize parameters from a Wix static media URL.
|
|
14
|
+
* REST APIs return URLs like https://static.wixstatic.com/media/<id>/v1/fit/w_2316,h_3474,q_90/file.jpg
|
|
15
|
+
* but templates need the bare URL so they can append their own resize params.
|
|
16
|
+
*/
|
|
17
|
+
declare function stripWixMediaResize(url: string): string;
|
|
18
|
+
type WixMediaType = 'image' | 'video' | 'document' | 'audio' | 'unknown';
|
|
19
|
+
/**
|
|
20
|
+
* Parsed Wix media URL with extracted metadata
|
|
21
|
+
*/
|
|
22
|
+
interface ParsedWixMediaUrl {
|
|
23
|
+
type: WixMediaType;
|
|
24
|
+
mediaId: string;
|
|
25
|
+
fileName: string;
|
|
26
|
+
/** Original image dimensions (for images) */
|
|
27
|
+
originWidth?: number;
|
|
28
|
+
originHeight?: number;
|
|
29
|
+
/** Poster image info (for videos) */
|
|
30
|
+
posterUri?: string;
|
|
31
|
+
posterWidth?: number;
|
|
32
|
+
posterHeight?: number;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Parse any wix:// protocol URL and extract metadata
|
|
36
|
+
*/
|
|
37
|
+
declare function parseWixMediaUrl(url: string): ParsedWixMediaUrl | null;
|
|
38
|
+
/**
|
|
39
|
+
* Parse wix:image:// protocol URLs and extract the media ID
|
|
40
|
+
* @deprecated Use parseWixMediaUrl for full metadata extraction
|
|
41
|
+
*/
|
|
42
|
+
declare function parseWixImageUrl(url: string): string | null;
|
|
43
|
+
/**
|
|
44
|
+
* Parse wix:video:// protocol URLs and extract the media ID
|
|
45
|
+
* @deprecated Use parseWixMediaUrl for full metadata extraction
|
|
46
|
+
*/
|
|
47
|
+
declare function parseWixVideoUrl(url: string): string | null;
|
|
48
|
+
/**
|
|
49
|
+
* Format Wix media URL to a usable static URL.
|
|
50
|
+
* Handles all wix:// protocol URLs (image, video, document, audio).
|
|
51
|
+
*
|
|
52
|
+
* @param _id - The media ID (used as fallback if url is not usable)
|
|
53
|
+
* @param url - The original URL (may be http(s):// or wix://)
|
|
54
|
+
* @param resize - Optional resize parameters (for images/video thumbnails)
|
|
55
|
+
* @returns A usable https:// URL
|
|
56
|
+
*/
|
|
57
|
+
declare function formatWixMediaUrl(_id: string, url: string, resize?: {
|
|
58
|
+
w: number;
|
|
59
|
+
h: number;
|
|
60
|
+
}): string;
|
|
61
|
+
/**
|
|
62
|
+
* Get the video poster URL from a wix:video:// URL
|
|
63
|
+
*/
|
|
64
|
+
declare function getVideoPosterUrl(url: string, resize?: {
|
|
65
|
+
w: number;
|
|
66
|
+
h: number;
|
|
67
|
+
}): string;
|
|
68
|
+
/**
|
|
69
|
+
* Get document download URL from a wix:document:// URL
|
|
70
|
+
*/
|
|
71
|
+
declare function getDocumentUrl(url: string): string;
|
|
72
|
+
/**
|
|
73
|
+
* Get audio URL from a wix:audio:// URL
|
|
74
|
+
*/
|
|
75
|
+
declare function getAudioUrl(url: string): string;
|
|
76
|
+
|
|
77
|
+
export { type ParsedWixMediaUrl, type WixMediaType, formatWixMediaUrl, getAudioUrl, getDocumentUrl, getVideoPosterUrl, parseWixImageUrl, parseWixMediaUrl, parseWixVideoUrl, stripWixMediaResize };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
function stripWixMediaResize(url) {
|
|
2
|
+
return url.replace(/\/v1\/(?:fit|fill|crop)\/[^/]+\/file\.\w+$/, "");
|
|
3
|
+
}
|
|
4
|
+
function parseWixMediaUrl(url) {
|
|
5
|
+
if (!url) return null;
|
|
6
|
+
const match = url.match(
|
|
7
|
+
/^wix:(image|video|document|audio):\/\/v1\/([^/]+)\/([^#]+)(?:#(.*))?$/
|
|
8
|
+
);
|
|
9
|
+
if (!match) return null;
|
|
10
|
+
const [, type, mediaId, fileName, hashParams] = match;
|
|
11
|
+
const result = {
|
|
12
|
+
type,
|
|
13
|
+
mediaId,
|
|
14
|
+
fileName: decodeURIComponent(fileName)
|
|
15
|
+
};
|
|
16
|
+
if (hashParams) {
|
|
17
|
+
const params = new URLSearchParams(hashParams);
|
|
18
|
+
const originWidth = params.get("originWidth");
|
|
19
|
+
const originHeight = params.get("originHeight");
|
|
20
|
+
if (originWidth) result.originWidth = parseInt(originWidth, 10);
|
|
21
|
+
if (originHeight) result.originHeight = parseInt(originHeight, 10);
|
|
22
|
+
const posterUri = params.get("posterUri");
|
|
23
|
+
const posterWidth = params.get("posterWidth");
|
|
24
|
+
const posterHeight = params.get("posterHeight");
|
|
25
|
+
if (posterUri) result.posterUri = decodeURIComponent(posterUri);
|
|
26
|
+
if (posterWidth) result.posterWidth = parseInt(posterWidth, 10);
|
|
27
|
+
if (posterHeight) result.posterHeight = parseInt(posterHeight, 10);
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
function parseWixImageUrl(url) {
|
|
32
|
+
const parsed = parseWixMediaUrl(url);
|
|
33
|
+
return (parsed == null ? void 0 : parsed.type) === "image" ? parsed.mediaId : null;
|
|
34
|
+
}
|
|
35
|
+
function parseWixVideoUrl(url) {
|
|
36
|
+
const parsed = parseWixMediaUrl(url);
|
|
37
|
+
return (parsed == null ? void 0 : parsed.type) === "video" ? parsed.mediaId : null;
|
|
38
|
+
}
|
|
39
|
+
function formatWixMediaUrl(_id, url, resize) {
|
|
40
|
+
const resizeFragment = resize ? `/v1/fit/w_${resize.w},h_${resize.h},q_90/file.jpg` : "";
|
|
41
|
+
if (url == null ? void 0 : url.startsWith("wix:")) {
|
|
42
|
+
const parsed = parseWixMediaUrl(url);
|
|
43
|
+
if (parsed) {
|
|
44
|
+
return `https://static.wixstatic.com/media/${parsed.mediaId}${resizeFragment}`;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if ((url == null ? void 0 : url.startsWith("http://")) || (url == null ? void 0 : url.startsWith("https://"))) {
|
|
48
|
+
return stripWixMediaResize(url) + resizeFragment;
|
|
49
|
+
}
|
|
50
|
+
if (_id) {
|
|
51
|
+
return `https://static.wixstatic.com/media/${_id}${resizeFragment}`;
|
|
52
|
+
}
|
|
53
|
+
return "";
|
|
54
|
+
}
|
|
55
|
+
function getVideoPosterUrl(url, resize) {
|
|
56
|
+
const parsed = parseWixMediaUrl(url);
|
|
57
|
+
if (!parsed || parsed.type !== "video") return "";
|
|
58
|
+
if (parsed.posterUri) {
|
|
59
|
+
return formatWixMediaUrl("", parsed.posterUri, resize);
|
|
60
|
+
}
|
|
61
|
+
const resizeFragment = resize ? `/v1/fit/w_${resize.w},h_${resize.h},q_90/file.jpg` : "";
|
|
62
|
+
return `https://static.wixstatic.com/media/${parsed.mediaId}${resizeFragment}`;
|
|
63
|
+
}
|
|
64
|
+
function getDocumentUrl(url) {
|
|
65
|
+
const parsed = parseWixMediaUrl(url);
|
|
66
|
+
if (!parsed || parsed.type !== "document") return "";
|
|
67
|
+
return `https://static.wixstatic.com/ugd/${parsed.mediaId}`;
|
|
68
|
+
}
|
|
69
|
+
function getAudioUrl(url) {
|
|
70
|
+
const parsed = parseWixMediaUrl(url);
|
|
71
|
+
if (!parsed || parsed.type !== "audio") return "";
|
|
72
|
+
return `https://static.wixstatic.com/mp3/${parsed.mediaId}`;
|
|
73
|
+
}
|
|
74
|
+
export {
|
|
75
|
+
formatWixMediaUrl,
|
|
76
|
+
getAudioUrl,
|
|
77
|
+
getDocumentUrl,
|
|
78
|
+
getVideoPosterUrl,
|
|
79
|
+
parseWixImageUrl,
|
|
80
|
+
parseWixMediaUrl,
|
|
81
|
+
parseWixVideoUrl,
|
|
82
|
+
stripWixMediaResize
|
|
83
|
+
};
|