fexjs
Version:
A lightweight and powerful Fetch API wrapper with interceptors, cancel tokens, and timeout support.
66 lines (56 loc) β’ 2.05 kB
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>