@pairbo/ui-kit 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/dist/ui-kit.js
CHANGED
|
@@ -1862,10 +1862,7 @@
|
|
|
1862
1862
|
class=${fe({loaded:this.loaded,hidden:!this.loaded})}
|
|
1863
1863
|
@error=${()=>this.handleError()}
|
|
1864
1864
|
/>
|
|
1865
|
-
`}};ae.styles=[bt,op];We([C()],ae.prototype,"skeletonHeight",2);We([C()],ae.prototype,"skeletonWidth",2);We([C()],ae.prototype,"src",2);We([C()],ae.prototype,"alt",2);We([C()],ae.prototype,"placeholder",2);We([C({type:Boolean})],ae.prototype,"skeleton",2);We([I()],ae.prototype,"loaded",2);We([B("img")],ae.prototype,"imgEl",2);We([G("src")],ae.prototype,"handleSrcChange",1);ae=We([mt("pbo-image")],ae);var cp=Object.defineProperty,hp=Object.getOwnPropertyDescriptor,Ft=(n,t,e,i)=>{for(var s=i>1?void 0:i?hp(t,e):t,r=n.length-1,o;r>=0;r--)(o=n[r])&&(s=(i?o(t,e,s):o(s))||s);return i&&s&&cp(t,e,s),s};let ct=class extends vt{constructor(){super(...arguments),this.card=null,this.focusIndex=0,this.livePreviewProps={font:"Monsieur La Doulaise",text:"",color:"rgb(0,0,0)",alignment:"left"},this.handwrittenPreviewProps={isLoading:!1,processedFileUrl:null},this.type="type",this.allowPinchToToggle=!0,this.distance=0,this.mainImageTemplates=[],this.closeZoomedPreview=()=>{this.zoomedPreview&&(this.zoomedPreview.open=!1),this.emit("pbo_close_live_preview",{detail:{editor:this.type}})},this.openZoomedPreview=()=>{this.zoomedPreview&&(this.zoomedPreview.open=!0),this.emit("pbo_open_live_preview",{detail:{editor:this.type}})},this.handleOpenZoomedClick=t=>{if(!bi()){t.preventDefault(),t.stopPropagation();return}this.openZoomedPreview()},this.handleTouchMove=t=>{if(this.allowPinchToToggle===!1)return;t.preventDefault();const e=t.touches[0],i=t.touches[1];if(!(e&&!i)&&e&&i){const s={x:e.clientX,y:e.clientY},r={x:i.clientX,y:i.clientY},o=dc(s,r);this.distance||(this.distance=o),o-this.distance>10&&this.openZoomedPreview()}},this.handleTouchEnd=()=>{this.allowPinchToToggle!==!1&&(this.distance=null)}}firstUpdated(){this.addEventListener("handwritten-image-dimensions-changed",t=>{var r,o;const{detail:e}=t,i=(r=this.shadowRoot)==null?void 0:r.querySelector("#handwritten-main-canvas"),s=(o=this.shadowRoot)==null?void 0:o.querySelector("#handwritten-zoomed-preview-canvas");i&&s&&(i.imageWidth=e.width,i.imageHeight=e.height,i.imageTop=e.top,i.imageLeft=e.left,s.imageWidth=e.width,s.imageHeight=e.height,s.imageTop=e.top,s.imageLeft=e.left)}),this.updateComplete,this.thumbnailElements[this.focusIndex].classList.add("active"),this.mainSlideElements[this.focusIndex].classList.add("active")}handleCardChange(t,e){var i,s,r,o;e&&(this.mainImageTemplates=[F`<pbo-image
|
|
1866
|
-
src="${e.medias.cover.url||"https://picsum.photos/id/1/300/400"}"
|
|
1867
|
-
alt=""
|
|
1868
|
-
/>`,F`<pbo-type-canvas
|
|
1865
|
+
`}};ae.styles=[bt,op];We([C()],ae.prototype,"skeletonHeight",2);We([C()],ae.prototype,"skeletonWidth",2);We([C()],ae.prototype,"src",2);We([C()],ae.prototype,"alt",2);We([C()],ae.prototype,"placeholder",2);We([C({type:Boolean})],ae.prototype,"skeleton",2);We([I()],ae.prototype,"loaded",2);We([B("img")],ae.prototype,"imgEl",2);We([G("src")],ae.prototype,"handleSrcChange",1);ae=We([mt("pbo-image")],ae);var cp=Object.defineProperty,hp=Object.getOwnPropertyDescriptor,Ft=(n,t,e,i)=>{for(var s=i>1?void 0:i?hp(t,e):t,r=n.length-1,o;r>=0;r--)(o=n[r])&&(s=(i?o(t,e,s):o(s))||s);return i&&s&&cp(t,e,s),s};let ct=class extends vt{constructor(){super(...arguments),this.card=null,this.focusIndex=0,this.livePreviewProps={font:"Monsieur La Doulaise",text:"",color:"rgb(0,0,0)",alignment:"left"},this.handwrittenPreviewProps={isLoading:!1,processedFileUrl:null},this.type="type",this.allowPinchToToggle=!0,this.distance=0,this.mainImageTemplates=[],this.closeZoomedPreview=()=>{this.zoomedPreview&&(this.zoomedPreview.open=!1),this.emit("pbo_close_live_preview",{detail:{editor:this.type}})},this.openZoomedPreview=()=>{this.zoomedPreview&&(this.zoomedPreview.open=!0),this.emit("pbo_open_live_preview",{detail:{editor:this.type}})},this.handleOpenZoomedClick=t=>{if(!bi()){t.preventDefault(),t.stopPropagation();return}this.openZoomedPreview()},this.handleTouchMove=t=>{if(this.allowPinchToToggle===!1)return;t.preventDefault();const e=t.touches[0],i=t.touches[1];if(!(e&&!i)&&e&&i){const s={x:e.clientX,y:e.clientY},r={x:i.clientX,y:i.clientY},o=dc(s,r);this.distance||(this.distance=o),o-this.distance>10&&this.openZoomedPreview()}},this.handleTouchEnd=()=>{this.allowPinchToToggle!==!1&&(this.distance=null)}}firstUpdated(){this.addEventListener("handwritten-image-dimensions-changed",t=>{var r,o;const{detail:e}=t,i=(r=this.shadowRoot)==null?void 0:r.querySelector("#handwritten-main-canvas"),s=(o=this.shadowRoot)==null?void 0:o.querySelector("#handwritten-zoomed-preview-canvas");i&&s&&(i.imageWidth=e.width,i.imageHeight=e.height,i.imageTop=e.top,i.imageLeft=e.left,s.imageWidth=e.width,s.imageHeight=e.height,s.imageTop=e.top,s.imageLeft=e.left)}),this.updateComplete,this.thumbnailElements[this.focusIndex].classList.add("active"),this.mainSlideElements[this.focusIndex].classList.add("active")}handleCardChange(t,e){var i,s,r,o;e&&(this.mainImageTemplates=[F`<pbo-image src="${e.medias.cover.url}" alt="" />`,F`<pbo-type-canvas
|
|
1869
1866
|
background-url=${e.medias.insert.url}
|
|
1870
1867
|
message=${(i=this.livePreviewProps)==null?void 0:i.text}
|
|
1871
1868
|
alignment=${((s=this.livePreviewProps)==null?void 0:s.alignment)||"left"}
|
|
@@ -1873,10 +1870,7 @@
|
|
|
1873
1870
|
font=${(o=this.livePreviewProps)==null?void 0:o.font}
|
|
1874
1871
|
display-type=${e.hasEditorial?"default":"fullSize"}
|
|
1875
1872
|
@click="${this.handleOpenZoomedClick}"
|
|
1876
|
-
></pbo-type-canvas>`,F`<pbo-image
|
|
1877
|
-
src="${e.medias.rendered_2.url||"https://picsum.photos/id/10/300/400"}"
|
|
1878
|
-
alt=""
|
|
1879
|
-
/>`])}handleFocusIndexChange(t,e){var i,s,r,o;this.thumbnailElements.length!==0&&t!==e&&((i=this.thumbnailElements[t])==null||i.classList.remove("active"),(s=this.thumbnailElements[e])==null||s.classList.add("active"),(r=this.mainSlideElements[t])==null||r.classList.remove("active"),(o=this.mainSlideElements[e])==null||o.classList.add("active"),this.requestUpdate())}handleAllowPinchToToggle(){this.allowPinchToToggle=this.type!=="handwritten"||this.focusIndex!==1||!0}focusOn(t){if(t<0||t>=this.thumbnailElements.length){console.warn("Invalid index:",t);return}this.focusIndex=t}get mainSlides(){return this.card?[F`<pbo-image src="${this.card.medias.cover.url}" alt="" />`,F`<pbo-type-canvas
|
|
1873
|
+
></pbo-type-canvas>`,F`<pbo-image src="${e.medias.rendered_2.url}" alt="" />`])}handleFocusIndexChange(t,e){var i,s,r,o;this.thumbnailElements.length!==0&&t!==e&&((i=this.thumbnailElements[t])==null||i.classList.remove("active"),(s=this.thumbnailElements[e])==null||s.classList.add("active"),(r=this.mainSlideElements[t])==null||r.classList.remove("active"),(o=this.mainSlideElements[e])==null||o.classList.add("active"),this.requestUpdate())}handleAllowPinchToToggle(){this.allowPinchToToggle=this.type!=="handwritten"||this.focusIndex!==1||!0}focusOn(t){if(t<0||t>=this.thumbnailElements.length){console.warn("Invalid index:",t);return}this.focusIndex=t}get mainSlides(){return this.card?[F`<pbo-image src="${this.card.medias.cover.url}" alt="" />`,F`<pbo-type-canvas
|
|
1880
1874
|
background-url=${this.card.medias.insert.url}
|
|
1881
1875
|
message=${this.livePreviewProps.text}
|
|
1882
1876
|
alignment=${this.livePreviewProps.alignment}
|
|
@@ -1911,12 +1905,7 @@
|
|
|
1911
1905
|
aria-pressed="${this.focusIndex===0}"
|
|
1912
1906
|
@click="${()=>this.focusOn(0)}"
|
|
1913
1907
|
>
|
|
1914
|
-
<pbo-image
|
|
1915
|
-
src="${((t=this.card)==null?void 0:t.medias.cover.url)||"https://picsum.photos/id/1/300/400"}"
|
|
1916
|
-
alt=""
|
|
1917
|
-
aria-hidden="true"
|
|
1918
|
-
>
|
|
1919
|
-
</pbo-image>
|
|
1908
|
+
<pbo-image src="${(t=this.card)==null?void 0:t.medias.cover.url}" alt="" aria-hidden="true"> </pbo-image>
|
|
1920
1909
|
</button>
|
|
1921
1910
|
<button
|
|
1922
1911
|
class="thumbnail"
|
|
@@ -1926,7 +1915,7 @@
|
|
|
1926
1915
|
@click="${()=>this.focusOn(1)}"
|
|
1927
1916
|
>
|
|
1928
1917
|
<pbo-image
|
|
1929
|
-
src="${(
|
|
1918
|
+
src="${(e=this.card)==null?void 0:e.medias.rendered_1.url}"
|
|
1930
1919
|
alt=""
|
|
1931
1920
|
aria-hidden="true"
|
|
1932
1921
|
></pbo-image>
|
|
@@ -1939,7 +1928,7 @@
|
|
|
1939
1928
|
@click="${()=>this.focusOn(2)}"
|
|
1940
1929
|
>
|
|
1941
1930
|
<pbo-image
|
|
1942
|
-
src="${(
|
|
1931
|
+
src="${(i=this.card)==null?void 0:i.medias.rendered_2.url}"
|
|
1943
1932
|
alt=""
|
|
1944
1933
|
aria-hidden="true"
|
|
1945
1934
|
></pbo-image>
|
|
@@ -1952,10 +1941,7 @@
|
|
|
1952
1941
|
@touchend="${this.handleTouchEnd}"
|
|
1953
1942
|
>
|
|
1954
1943
|
<div class="main-slide" @click="${this.handleOpenZoomedClick}">
|
|
1955
|
-
<pbo-image
|
|
1956
|
-
src="${((s=this.card)==null?void 0:s.medias.cover.url)||"https://picsum.photos/id/1/300/400"}"
|
|
1957
|
-
alt=""
|
|
1958
|
-
></pbo-image>
|
|
1944
|
+
<pbo-image src="${(s=this.card)==null?void 0:s.medias.cover.url}" alt=""></pbo-image>
|
|
1959
1945
|
${bi()?F`<button
|
|
1960
1946
|
class="zoomed-preview-open-button"
|
|
1961
1947
|
type="button"
|
|
@@ -1991,10 +1977,7 @@
|
|
|
1991
1977
|
</button>`:Z}
|
|
1992
1978
|
</div>
|
|
1993
1979
|
<div class="main-slide" @click=${this.handleOpenZoomedClick}>
|
|
1994
|
-
<pbo-image
|
|
1995
|
-
src="${((p=this.card)==null?void 0:p.medias.rendered_2.url)||"https://picsum.photos/id/10/300/400"}"
|
|
1996
|
-
alt=""
|
|
1997
|
-
></pbo-image>
|
|
1980
|
+
<pbo-image src="${(p=this.card)==null?void 0:p.medias.rendered_2.url}" alt=""></pbo-image>
|
|
1998
1981
|
${bi()?F`<button
|
|
1999
1982
|
class="zoomed-preview-open-button"
|
|
2000
1983
|
type="button"
|
|
@@ -2045,7 +2028,7 @@
|
|
|
2045
2028
|
style=${ge({display:this.focusIndex===1?"block":"none"})}
|
|
2046
2029
|
></pbo-handwritten-canvas>`:Z)}
|
|
2047
2030
|
<img
|
|
2048
|
-
src="${(
|
|
2031
|
+
src="${(E=this.card)==null?void 0:E.medias.rendered_2.url}"
|
|
2049
2032
|
alt=""
|
|
2050
2033
|
style=${ge({display:this.focusIndex===2?"block":"none"})}
|
|
2051
2034
|
/>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pairbo/ui-kit",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.6",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"readme": "./README-NPM.md",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
},
|
|
13
13
|
"scripts": {
|
|
14
14
|
"dev": "vite",
|
|
15
|
-
"build": "vite build",
|
|
15
|
+
"build": "vite build && vite build --config vite.config.page-manager.ts",
|
|
16
16
|
"preview": "vite preview",
|
|
17
17
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
18
18
|
"prepare": "husky",
|
|
Binary file
|
|
@@ -1,302 +0,0 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
/* tslint:disable */
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Mock Service Worker.
|
|
6
|
-
* @see https://github.com/mswjs/msw
|
|
7
|
-
* - Please do NOT modify this file.
|
|
8
|
-
* - Please do NOT serve this file on production.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
const PACKAGE_VERSION = "2.7.3";
|
|
12
|
-
const INTEGRITY_CHECKSUM = "00729d72e3b82faf54ca8b9621dbb96f";
|
|
13
|
-
const IS_MOCKED_RESPONSE = Symbol("isMockedResponse");
|
|
14
|
-
const activeClientIds = new Set();
|
|
15
|
-
|
|
16
|
-
self.addEventListener("install", function () {
|
|
17
|
-
self.skipWaiting();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
self.addEventListener("activate", function (event) {
|
|
21
|
-
event.waitUntil(self.clients.claim());
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
self.addEventListener("message", async function (event) {
|
|
25
|
-
const clientId = event.source.id;
|
|
26
|
-
|
|
27
|
-
if (!clientId || !self.clients) {
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const client = await self.clients.get(clientId);
|
|
32
|
-
|
|
33
|
-
if (!client) {
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const allClients = await self.clients.matchAll({
|
|
38
|
-
type: "window",
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
switch (event.data) {
|
|
42
|
-
case "KEEPALIVE_REQUEST": {
|
|
43
|
-
sendToClient(client, {
|
|
44
|
-
type: "KEEPALIVE_RESPONSE",
|
|
45
|
-
});
|
|
46
|
-
break;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
case "INTEGRITY_CHECK_REQUEST": {
|
|
50
|
-
sendToClient(client, {
|
|
51
|
-
type: "INTEGRITY_CHECK_RESPONSE",
|
|
52
|
-
payload: {
|
|
53
|
-
packageVersion: PACKAGE_VERSION,
|
|
54
|
-
checksum: INTEGRITY_CHECKSUM,
|
|
55
|
-
},
|
|
56
|
-
});
|
|
57
|
-
break;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
case "MOCK_ACTIVATE": {
|
|
61
|
-
activeClientIds.add(clientId);
|
|
62
|
-
|
|
63
|
-
sendToClient(client, {
|
|
64
|
-
type: "MOCKING_ENABLED",
|
|
65
|
-
payload: {
|
|
66
|
-
client: {
|
|
67
|
-
id: client.id,
|
|
68
|
-
frameType: client.frameType,
|
|
69
|
-
},
|
|
70
|
-
},
|
|
71
|
-
});
|
|
72
|
-
break;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
case "MOCK_DEACTIVATE": {
|
|
76
|
-
activeClientIds.delete(clientId);
|
|
77
|
-
break;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
case "CLIENT_CLOSED": {
|
|
81
|
-
activeClientIds.delete(clientId);
|
|
82
|
-
|
|
83
|
-
const remainingClients = allClients.filter(client => {
|
|
84
|
-
return client.id !== clientId;
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
// Unregister itself when there are no more clients
|
|
88
|
-
if (remainingClients.length === 0) {
|
|
89
|
-
self.registration.unregister();
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
break;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
self.addEventListener("fetch", function (event) {
|
|
98
|
-
const { request } = event;
|
|
99
|
-
|
|
100
|
-
// Bypass navigation requests.
|
|
101
|
-
if (request.mode === "navigate") {
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// Opening the DevTools triggers the "only-if-cached" request
|
|
106
|
-
// that cannot be handled by the worker. Bypass such requests.
|
|
107
|
-
if (request.cache === "only-if-cached" && request.mode !== "same-origin") {
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// Bypass all requests when there are no active clients.
|
|
112
|
-
// Prevents the self-unregistered worked from handling requests
|
|
113
|
-
// after it's been deleted (still remains active until the next reload).
|
|
114
|
-
if (activeClientIds.size === 0) {
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// Generate unique request ID.
|
|
119
|
-
const requestId = crypto.randomUUID();
|
|
120
|
-
event.respondWith(handleRequest(event, requestId));
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
async function handleRequest(event, requestId) {
|
|
124
|
-
const client = await resolveMainClient(event);
|
|
125
|
-
const response = await getResponse(event, client, requestId);
|
|
126
|
-
|
|
127
|
-
// Send back the response clone for the "response:*" life-cycle events.
|
|
128
|
-
// Ensure MSW is active and ready to handle the message, otherwise
|
|
129
|
-
// this message will pend indefinitely.
|
|
130
|
-
if (client && activeClientIds.has(client.id)) {
|
|
131
|
-
(async function () {
|
|
132
|
-
const responseClone = response.clone();
|
|
133
|
-
|
|
134
|
-
sendToClient(
|
|
135
|
-
client,
|
|
136
|
-
{
|
|
137
|
-
type: "RESPONSE",
|
|
138
|
-
payload: {
|
|
139
|
-
requestId,
|
|
140
|
-
isMockedResponse: IS_MOCKED_RESPONSE in response,
|
|
141
|
-
type: responseClone.type,
|
|
142
|
-
status: responseClone.status,
|
|
143
|
-
statusText: responseClone.statusText,
|
|
144
|
-
body: responseClone.body,
|
|
145
|
-
headers: Object.fromEntries(responseClone.headers.entries()),
|
|
146
|
-
},
|
|
147
|
-
},
|
|
148
|
-
[responseClone.body]
|
|
149
|
-
);
|
|
150
|
-
})();
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
return response;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// Resolve the main client for the given event.
|
|
157
|
-
// Client that issues a request doesn't necessarily equal the client
|
|
158
|
-
// that registered the worker. It's with the latter the worker should
|
|
159
|
-
// communicate with during the response resolving phase.
|
|
160
|
-
async function resolveMainClient(event) {
|
|
161
|
-
const client = await self.clients.get(event.clientId);
|
|
162
|
-
|
|
163
|
-
if (activeClientIds.has(event.clientId)) {
|
|
164
|
-
return client;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (client?.frameType === "top-level") {
|
|
168
|
-
return client;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
const allClients = await self.clients.matchAll({
|
|
172
|
-
type: "window",
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
return allClients
|
|
176
|
-
.filter(client => {
|
|
177
|
-
// Get only those clients that are currently visible.
|
|
178
|
-
return client.visibilityState === "visible";
|
|
179
|
-
})
|
|
180
|
-
.find(client => {
|
|
181
|
-
// Find the client ID that's recorded in the
|
|
182
|
-
// set of clients that have registered the worker.
|
|
183
|
-
return activeClientIds.has(client.id);
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
async function getResponse(event, client, requestId) {
|
|
188
|
-
const { request } = event;
|
|
189
|
-
|
|
190
|
-
// Clone the request because it might've been already used
|
|
191
|
-
// (i.e. its body has been read and sent to the client).
|
|
192
|
-
const requestClone = request.clone();
|
|
193
|
-
|
|
194
|
-
function passthrough() {
|
|
195
|
-
// Cast the request headers to a new Headers instance
|
|
196
|
-
// so the headers can be manipulated with.
|
|
197
|
-
const headers = new Headers(requestClone.headers);
|
|
198
|
-
|
|
199
|
-
// Remove the "accept" header value that marked this request as passthrough.
|
|
200
|
-
// This prevents request alteration and also keeps it compliant with the
|
|
201
|
-
// user-defined CORS policies.
|
|
202
|
-
const acceptHeader = headers.get("accept");
|
|
203
|
-
if (acceptHeader) {
|
|
204
|
-
const values = acceptHeader.split(",").map(value => value.trim());
|
|
205
|
-
const filteredValues = values.filter(value => value !== "msw/passthrough");
|
|
206
|
-
|
|
207
|
-
if (filteredValues.length > 0) {
|
|
208
|
-
headers.set("accept", filteredValues.join(", "));
|
|
209
|
-
} else {
|
|
210
|
-
headers.delete("accept");
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
return fetch(requestClone, { headers });
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// Bypass mocking when the client is not active.
|
|
218
|
-
if (!client) {
|
|
219
|
-
return passthrough();
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// Bypass initial page load requests (i.e. static assets).
|
|
223
|
-
// The absence of the immediate/parent client in the map of the active clients
|
|
224
|
-
// means that MSW hasn't dispatched the "MOCK_ACTIVATE" event yet
|
|
225
|
-
// and is not ready to handle requests.
|
|
226
|
-
if (!activeClientIds.has(client.id)) {
|
|
227
|
-
return passthrough();
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// Notify the client that a request has been intercepted.
|
|
231
|
-
const requestBuffer = await request.arrayBuffer();
|
|
232
|
-
const clientMessage = await sendToClient(
|
|
233
|
-
client,
|
|
234
|
-
{
|
|
235
|
-
type: "REQUEST",
|
|
236
|
-
payload: {
|
|
237
|
-
id: requestId,
|
|
238
|
-
url: request.url,
|
|
239
|
-
mode: request.mode,
|
|
240
|
-
method: request.method,
|
|
241
|
-
headers: Object.fromEntries(request.headers.entries()),
|
|
242
|
-
cache: request.cache,
|
|
243
|
-
credentials: request.credentials,
|
|
244
|
-
destination: request.destination,
|
|
245
|
-
integrity: request.integrity,
|
|
246
|
-
redirect: request.redirect,
|
|
247
|
-
referrer: request.referrer,
|
|
248
|
-
referrerPolicy: request.referrerPolicy,
|
|
249
|
-
body: requestBuffer,
|
|
250
|
-
keepalive: request.keepalive,
|
|
251
|
-
},
|
|
252
|
-
},
|
|
253
|
-
[requestBuffer]
|
|
254
|
-
);
|
|
255
|
-
|
|
256
|
-
switch (clientMessage.type) {
|
|
257
|
-
case "MOCK_RESPONSE": {
|
|
258
|
-
return respondWithMock(clientMessage.data);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
case "PASSTHROUGH": {
|
|
262
|
-
return passthrough();
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
return passthrough();
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
function sendToClient(client, message, transferrables = []) {
|
|
270
|
-
return new Promise((resolve, reject) => {
|
|
271
|
-
const channel = new MessageChannel();
|
|
272
|
-
|
|
273
|
-
channel.port1.onmessage = event => {
|
|
274
|
-
if (event.data && event.data.error) {
|
|
275
|
-
return reject(event.data.error);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
resolve(event.data);
|
|
279
|
-
};
|
|
280
|
-
|
|
281
|
-
client.postMessage(message, [channel.port2].concat(transferrables.filter(Boolean)));
|
|
282
|
-
});
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
async function respondWithMock(response) {
|
|
286
|
-
// Setting response status code to 0 is a no-op.
|
|
287
|
-
// However, when responding with a "Response.error()", the produced Response
|
|
288
|
-
// instance will have status code set to 0. Since it's not possible to create
|
|
289
|
-
// a Response instance with status code 0, handle that use-case separately.
|
|
290
|
-
if (response.status === 0) {
|
|
291
|
-
return Response.error();
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
const mockedResponse = new Response(response.body, response);
|
|
295
|
-
|
|
296
|
-
Reflect.defineProperty(mockedResponse, IS_MOCKED_RESPONSE, {
|
|
297
|
-
value: true,
|
|
298
|
-
enumerable: true,
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
return mockedResponse;
|
|
302
|
-
}
|
|
Binary file
|