UNPKG

fexjs

Version:

A lightweight and powerful Fetch API wrapper with interceptors, cancel tokens, and timeout support.

66 lines (56 loc) β€’ 2.05 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>fex.js ν…ŒμŠ€νŠΈ</title> <script type="module"> import fex, { FexCancelToken } from '../dist/fex.js'; async function testTimeout() { console.log("πŸ•’ νƒ€μž„μ•„μ›ƒ ν…ŒμŠ€νŠΈ μ‹œμž‘..."); try { const response = await fex.get("https://httpbin.org/delay/5", { timeout: 3000, // 3초 ν›„ νƒ€μž„μ•„μ›ƒ λ°œμƒ (httpbin은 5초 λ’€ 응닡) }); console.log("βœ… 성곡 응닡:", response.data); } catch (error) { console.error("❌ νƒ€μž„μ•„μ›ƒ λ°œμƒ:", error.message); } } async function testNormalRequest() { console.log("πŸ“‘ 정상 μš”μ²­ ν…ŒμŠ€νŠΈ μ‹œμž‘..."); try { const response = await fex.get("https://httpbin.org/get", { timeout: 5000, // μΆ©λΆ„ν•œ μ‹œκ°„ (5초) 쀌 }); console.log("βœ… 정상 응닡:", response.data); } catch (error) { console.error("❌ μš”μ²­ μ‹€νŒ¨:", error.message); } } async function testCancelToken() { console.log("🚨 μš”μ²­ μ·¨μ†Œ ν…ŒμŠ€νŠΈ μ‹œμž‘..."); const cancelToken = new FexCancelToken(); setTimeout(() => { console.log("πŸ›‘ μš”μ²­μ„ μ·¨μ†Œν•©λ‹ˆλ‹€."); cancelToken.cancel("ν…ŒμŠ€νŠΈμ—μ„œ μš”μ²­μ„ μ·¨μ†Œν•¨"); }, 2000); // 2초 ν›„ μš”μ²­ μ·¨μ†Œ try { const response = await fex.get("https://httpbin.org/delay/5", { cancelToken, }); console.log("βœ… 성곡 응닡:", response.data); } catch (error) { console.error("❌ μš”μ²­ μ·¨μ†Œλ¨:", error.message); } } // βœ… ν…ŒμŠ€νŠΈ μ‹€ν–‰ testTimeout(); setTimeout(() => testNormalRequest(), 5000); // 5초 ν›„ μ‹€ν–‰ setTimeout(() => testCancelToken(), 10000); // 10초 ν›„ μ‹€ν–‰ </script> </head> <body> <h1>fex.js ν…ŒμŠ€νŠΈ νŽ˜μ΄μ§€</h1> </body> </html>