twd-js 1.4.3 → 1.5.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/mock-sw.js CHANGED
@@ -1,6 +1,6 @@
1
- (function(){"use strict";const d=e=>{try{return new RegExp(e),!0}catch{return!1}},f=new Set(["js","mjs","cjs","ts","tsx","jsx","mts","cts","css","scss","sass","less","styl","html","htm","xml","xhtml","vue","svelte","json","yaml","yml","toml","csv","txt","md","mdx","pdf","doc","docx","png","jpg","jpeg","gif","svg","webp","ico","bmp","avif","woff","woff2","ttf","eot","otf","mp3","mp4","webm","ogg","wav","zip","tar","gz","rar","map"]),u=e=>{const n=e.split("?")[0].match(/\.([a-zA-Z0-9]+)$/);return n?f.has(n[1].toLowerCase()):!1};function h(e,s,n){return n.find(t=>{const o=t.method.toLowerCase()===e.toLowerCase();if(t.urlRegex&&d(t.url)){const r=new RegExp(t.url);return o&&r.test(s)}if(u(t.url))return o&&s.includes(t.url);const c=t.url===s||s.includes(t.url);return o&&c&&!u(s)})}function p(e,s,n){e.forEach(t=>t.postMessage({type:"EXECUTED",alias:s.alias,request:n}))}const m=(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||{}})},a="1.4.3";let i=[];const y=async e=>{const{method:s}=e.request,n=e.request.url,t=h(s,n,i);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 r=await e.request.clone().formData();o={},r.forEach((E,x)=>{o[x]=E})}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{}const c=await self.clients.matchAll();return p(c,t,o),m(t.response,t.status??200,t.responseHeaders)})()))},g=e=>{e!==a&&console.warn(`[TWD] ⚠️ Version mismatch detected:
1
+ (function(){"use strict";const f=e=>{try{return new RegExp(e),!0}catch{return!1}},h=new Set(["js","mjs","cjs","ts","tsx","jsx","mts","cts","css","scss","sass","less","styl","html","htm","xml","xhtml","vue","svelte","json","yaml","yml","toml","csv","txt","md","mdx","pdf","doc","docx","png","jpg","jpeg","gif","svg","webp","ico","bmp","avif","woff","woff2","ttf","eot","otf","mp3","mp4","webm","ogg","wav","zip","tar","gz","rar","map"]),d=e=>{const o=e.split("?")[0].match(/\.([a-zA-Z0-9]+)$/);return o?h.has(o[1].toLowerCase()):!1},m=(e,n)=>{const o=e.indexOf(n);if(o===-1)return!1;const t=o+n.length,s=e[t];if(s===void 0)return!0;const i=e.indexOf("?");return i!==-1&&t>i?!0:"/?#&".includes(s)};function p(e,n,o){return o.find(t=>{const s=t.method.toLowerCase()===e.toLowerCase();if(t.urlRegex&&f(t.url)){const c=new RegExp(t.url);return s&&c.test(n)}if(d(t.url))return s&&n.includes(t.url);const u=t.url===n||m(n,t.url);return s&&u&&!d(n)})}function y(e,n,o,t){e.forEach(s=>s.postMessage({type:"EXECUTED",alias:n.alias,request:o,hitCount:t}))}const g=(e,n,o)=>{const s=![204,205,304].includes(n),i=s?JSON.stringify(e):null;return new Response(i,{status:n,headers:s?o||{"Content-Type":"application/json"}:o||{}})},r="1.5.1";let a=[],l={};const w=async e=>{const{method:n}=e.request,o=e.request.url,t=p(n,o,a);t&&(console.log("[TWD] Mock hit:",t.alias,n,o),e.respondWith((async()=>{let s=null;const i=e.request.headers.get("content-type")||"application/json";if(i.includes("application/json"))try{s=await e.request.clone().json()}catch{}else if(i.includes("form"))try{const c=await e.request.clone().formData();s={},c.forEach((W,v)=>{s[v]=W})}catch{}else if(i.includes("text"))try{s=await e.request.clone().text()}catch{}else if(i.includes("octet-stream"))try{s=await e.request.clone().arrayBuffer()}catch{}else if(i.includes("image"))try{s=await e.request.clone().blob()}catch{}else try{s=await e.request.clone().text()}catch{}l[t.alias]||(l[t.alias]=0),l[t.alias]++;const u=await self.clients.matchAll();return y(u,t,s,l[t.alias]),t.delay&&t.delay>0&&await new Promise(c=>setTimeout(c,t.delay)),g(t.response,t.status??200,t.responseHeaders)})()))},x=e=>{e!==r&&console.warn(`[TWD] ⚠️ Version mismatch detected:
2
2
  Client version: ${e}
3
- Service Worker version: ${a}
3
+ Service Worker version: ${r}
4
4
 
5
5
  This may lead to unexpected behavior.
6
6
  Please unregister the Service Worker and reload the page to ensure compatibility.
@@ -8,4 +8,4 @@ Please unregister the Service Worker and reload the page to ensure compatibility
8
8
  To reinstall:
9
9
  npx twd-js init public --save
10
10
 
11
- Docs: https://brikev.github.io/twd/api-mocking.html#_1-install-mock-service-worker`)},w=e=>{const{type:s,rule:n,version:t}=e.data||{};g(t),s==="ADD_RULE"&&(i=i.filter(o=>o.alias!==n.alias),i.push(n),console.log("[TWD] Rule added:",n)),s==="CLEAR_RULES"&&(i=[],console.log("[TWD] All rules cleared"))};self.addEventListener("install",()=>{self.skipWaiting()}),self.addEventListener("activate",e=>{e.waitUntil(self.clients.claim())}),console.log(`[TWD] Mock Service Worker loaded - version ${a}`),self.addEventListener("fetch",y),self.addEventListener("message",w)})();
11
+ Docs: https://brikev.github.io/twd/api-mocking.html#_1-install-mock-service-worker`)},E=e=>{const{type:n,rule:o,version:t}=e.data||{};x(t),n==="ADD_RULE"&&(a=a.filter(s=>s.alias!==o.alias),a.push(o),console.log("[TWD] Rule added:",o)),n==="CLEAR_RULES"&&(a=[],l={},console.log("[TWD] All rules cleared"))};self.addEventListener("install",()=>{self.skipWaiting()}),self.addEventListener("activate",e=>{e.waitUntil(self.clients.claim())}),console.log(`[TWD] Mock Service Worker loaded - version ${r}`),self.addEventListener("fetch",w),self.addEventListener("message",E)})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "twd-js",
3
- "version": "1.4.3",
3
+ "version": "1.5.1",
4
4
  "description": "Test While Developing (TWD) - in-browser testing",
5
5
  "license": "MIT",
6
6
  "author": "BRIKEV",
@@ -80,9 +80,9 @@
80
80
  "@preact/preset-vite": "^2.10.3",
81
81
  "@testing-library/jest-dom": "^6.9.1",
82
82
  "@testing-library/react": "^16.3.2",
83
- "@types/react": "^19.2.11",
83
+ "@types/react": "^19.2.13",
84
84
  "@types/react-dom": "^19.2.3",
85
- "@vitejs/plugin-react": "^5.1.3",
85
+ "@vitejs/plugin-react": "^5.1.4",
86
86
  "@vitest/coverage-v8": "^4.0.18",
87
87
  "conventional-changelog": "^7.1.1",
88
88
  "jsdom": "^28.0.0",