@svkhrobbeck/fetcher
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "@svkhrobbeck/fetcher", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"main": "dist/index.cjs.js", | ||
@@ -5,0 +5,0 @@ "module": "dist/index.esm.js", |
@@ -19,2 +19,3 @@ # Fetcher Library | ||
Fetcher kutubxonasini NPM orqali o'rnating: | ||
```bash | ||
@@ -25,2 +26,3 @@ npm install @svkhrobbeck/fetcher | ||
Yoki Yarn orqali: | ||
```bash | ||
@@ -35,5 +37,6 @@ yarn add @svkhrobbeck/fetcher | ||
```javascript | ||
import fetcher from '@svkhrobbeck/fetcher'; | ||
import fetcher from "@svkhrobbeck/fetcher"; | ||
fetcher.get('https://api.example.com/data') | ||
fetcher | ||
.get("https://api.example.com/data") | ||
.then(response => { | ||
@@ -43,3 +46,3 @@ console.log(response.data); | ||
.catch(error => { | ||
console.error('Error fetching data:', error); | ||
console.error("Error fetching data:", error); | ||
}); | ||
@@ -51,11 +54,12 @@ ``` | ||
```javascript | ||
fetcher.post('https://api.example.com/users', { | ||
name: 'Ichigo Kurosaki', | ||
email: 'ichigo.kurosaki@example.com' | ||
}) | ||
fetcher | ||
.post("https://api.example.com/users", { | ||
name: "Ichigo Kurosaki", | ||
email: "ichigo.kurosaki@example.com" | ||
}) | ||
.then(response => { | ||
console.log('User created:', response.data); | ||
console.log("User created:", response.data); | ||
}) | ||
.catch(error => { | ||
console.error('Error creating user:', error); | ||
console.error("Error creating user:", error); | ||
}); | ||
@@ -69,14 +73,16 @@ ``` | ||
fetcher.get('https://api.example.com/data', { | ||
cancelToken: token | ||
}).catch(error => { | ||
if (fetcher.isCancel(error)) { | ||
console.log('Request canceled:', error.message); | ||
} else { | ||
console.error('Error:', error); | ||
} | ||
}); | ||
fetcher | ||
.get("https://api.example.com/data", { | ||
cancelToken: token | ||
}) | ||
.catch(error => { | ||
if (fetcher.isCancel(error)) { | ||
console.log("Request canceled:", error.message); | ||
} else { | ||
console.error("Error:", error); | ||
} | ||
}); | ||
// So'rovni istalgan vaqtda bekor qilish | ||
cancel('Operation canceled by the user.'); | ||
cancel("Operation canceled by the user."); | ||
``` | ||
@@ -88,16 +94,22 @@ | ||
// So'rov Interceptori | ||
fetcher.interceptors.request.use(config => { | ||
console.log('Request started:', config); | ||
return config; | ||
}, error => { | ||
return Promise.reject(error); | ||
}); | ||
fetcher.interceptors.request.use( | ||
config => { | ||
console.log("Request started:", config); | ||
return config; | ||
}, | ||
error => { | ||
return Promise.reject(error); | ||
} | ||
); | ||
// Javob Interceptori | ||
fetcher.interceptors.response.use(response => { | ||
console.log('Response received:', response); | ||
return response; | ||
}, error => { | ||
return Promise.reject(error); | ||
}); | ||
fetcher.interceptors.response.use( | ||
response => { | ||
console.log("Response received:", response); | ||
return response; | ||
}, | ||
error => { | ||
return Promise.reject(error); | ||
} | ||
); | ||
``` | ||
@@ -108,13 +120,15 @@ | ||
Fetcher bilan moslashtirilgan instansiyani yaratish va undan foydalanish: | ||
```javascript | ||
const customFetcher = fetcher.create({ | ||
baseURL: 'https://api.example.com', | ||
baseURL: "https://api.example.com", | ||
headers: { | ||
Authorization: 'Bearer YOUR_ACCESS_TOKEN' | ||
Authorization: "Bearer YOUR_ACCESS_TOKEN" | ||
} | ||
}); | ||
customFetcher.get('/protected-data') | ||
customFetcher | ||
.get("/protected-data") | ||
.then(response => console.log(response.data)) | ||
.catch(error => console.error('Error fetching protected data:', error)); | ||
.catch(error => console.error("Error fetching protected data:", error)); | ||
``` | ||
@@ -138,5 +152,1 @@ | ||
- **GitHub**: [svkhrobbeck](https://github.com/svkhrobbeck) | ||
## Litsenziya | ||
Fetcher MIT litsenziyasi asosida chiqarilgan. To'liq ma'lumotni `LICENSE` faylida topishingiz mumkin. |
144
21181