twd-js 0.5.2 → 0.6.0

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/README.md CHANGED
@@ -289,6 +289,25 @@ npx twd-js init <public-dir> [--save]
289
289
 
290
290
  This will copy `mock-sw.js` to your public directory.
291
291
 
292
+ **Removing the Service Worker in Production Builds**
293
+
294
+ The service worker file (`mock-sw.js`) is only needed during development for API mocking. To remove it from production builds, use the `removeMockServiceWorker` Vite plugin:
295
+
296
+ ```ts
297
+ // vite.config.ts
298
+ import { defineConfig } from 'vite';
299
+ import { removeMockServiceWorker } from 'twd-js';
300
+
301
+ export default defineConfig({
302
+ plugins: [
303
+ // ... other plugins
304
+ removeMockServiceWorker()
305
+ ]
306
+ });
307
+ ```
308
+
309
+ This plugin will automatically remove `mock-sw.js` from your build output during production builds.
310
+
292
311
  ### Mock Requests
293
312
 
294
313
  Use `twd.mockRequest()` to define API mocks in your tests:
package/dist/index.d.ts CHANGED
@@ -3,3 +3,4 @@ export { TWDSidebar } from './ui/TWDSidebar';
3
3
  export { initTests } from './initializers/initTests';
4
4
  export { expect } from 'chai';
5
5
  export { userEvent } from './proxies/userEvent';
6
+ export { removeMockServiceWorker } from './plugin/removeMockServiceWorker';
package/dist/mock-sw.js CHANGED
@@ -1 +1 @@
1
- function u(e,s,o){return o.find(t=>{const a=t.method.toLowerCase()===e.toLowerCase();if(t.urlRegex){const n=new RegExp(t.url);return a&&n.test(s)}const l=t.url===s||s.includes(t.url);return a&&l})}function d(e,s,o){e.forEach(t=>t.postMessage({type:"EXECUTED",alias:s.alias,request:o}))}let c=[];const f=async e=>{const{method:s}=e.request,o=e.request.url,t=u(s,o,c);t&&(console.log("[TWD] Mock hit:",t.alias,s,o),e.respondWith((async()=>{let a=null;const l=e.request.headers.get("content-type")||"application/json";if(l.includes("application/json"))try{a=await e.request.clone().json()}catch{}else if(l.includes("form"))try{const n=await e.request.clone().formData();a={},n.forEach((r,i)=>{a[i]=r})}catch{}else if(l.includes("text"))try{a=await e.request.clone().text()}catch{}else if(l.includes("octet-stream"))try{a=await e.request.clone().arrayBuffer()}catch{}else if(l.includes("image"))try{a=await e.request.clone().blob()}catch{}else try{a=await e.request.clone().text()}catch{}return self.clients.matchAll().then(n=>{d(n,t,a)}),new Response(JSON.stringify(t.response),{status:t.status||200,headers:t.responseHeaders||{"Content-Type":"application/json"}})})()))},h=e=>{const{type:s,rule:o}=e.data||{};s==="ADD_RULE"&&(c=c.filter(t=>t.alias!==o.alias),c.push(o),console.log("[TWD] Rule added:",o)),s==="CLEAR_RULES"&&(c=[],console.log("[TWD] All rules cleared"))};console.log("[TWD] Mock Service Worker loaded - version 0.5.2");self.addEventListener("fetch",f);self.addEventListener("message",h);
1
+ function u(e,s,n){return n.find(t=>{const o=t.method.toLowerCase()===e.toLowerCase();if(t.urlRegex){const a=new RegExp(t.url);return o&&a.test(s)}const l=t.url===s||s.includes(t.url);return o&&l})}function d(e,s,n){e.forEach(t=>t.postMessage({type:"EXECUTED",alias:s.alias,request:n}))}const f=(e,s,n)=>{const o=![204,205,304].includes(s),l=o?JSON.stringify(e):null;return new Response(l,{status:s,headers:o?n||{"Content-Type":"application/json"}:n||{}})};let c=[];const h=async e=>{const{method:s}=e.request,n=e.request.url,t=u(s,n,c);t&&(console.log("[TWD] Mock hit:",t.alias,s,n),e.respondWith((async()=>{let o=null;const l=e.request.headers.get("content-type")||"application/json";if(l.includes("application/json"))try{o=await e.request.clone().json()}catch{}else if(l.includes("form"))try{const a=await e.request.clone().formData();o={},a.forEach((r,i)=>{o[i]=r})}catch{}else if(l.includes("text"))try{o=await e.request.clone().text()}catch{}else if(l.includes("octet-stream"))try{o=await e.request.clone().arrayBuffer()}catch{}else if(l.includes("image"))try{o=await e.request.clone().blob()}catch{}else try{o=await e.request.clone().text()}catch{}return self.clients.matchAll().then(a=>{d(a,t,o)}),f(t.response,t.status??200,t.responseHeaders)})()))},y=e=>{const{type:s,rule:n}=e.data||{};s==="ADD_RULE"&&(c=c.filter(t=>t.alias!==n.alias),c.push(n),console.log("[TWD] Rule added:",n)),s==="CLEAR_RULES"&&(c=[],console.log("[TWD] All rules cleared"))};console.log("[TWD] Mock Service Worker loaded - version 0.6.0");self.addEventListener("fetch",h);self.addEventListener("message",y);
@@ -0,0 +1,18 @@
1
+ import { Plugin } from 'vite';
2
+ /**
3
+ * Vite plugin to remove the mock service worker file from the build output.
4
+ * This is useful for production builds where you don't want the mock service worker to be included.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import { removeMockServiceWorker } from 'twd-js';
9
+ *
10
+ * export default defineConfig({
11
+ * plugins: [
12
+ * // ... other plugins
13
+ * removeMockServiceWorker()
14
+ * ]
15
+ * });
16
+ * ```
17
+ */
18
+ export declare function removeMockServiceWorker(): Plugin;