fetch()ን ትታችሁ Axiosን ተጠቀሙ!
Auto JSON · Smart Errors · Interceptors · Production Ready 🚀
The HTTP library powering millions of production apps worldwide.
Axios — በ Promise ላይ የተመሰረተ (Promise-based) የ HTTP client library ነው! በ Browser ውስጥም ሆነ በ Node.js ውስጥ ይሰራል! fetch()ን ይበልጥ ቀላል፣ ኃይለኛ እና ለ Production ዝግጁ በሆነ መልኩ ተክቶታል!
Axios is a popular HTTP client with auto JSON parsing, interceptors, and smart error handling — used by millions of production apps.
npm install axios / yarn add axiosimport axios from 'axios' — ፈጣን አጠቃቀም (setup)!
Works everywhere JavaScript runs — React, Vue, Angular, Node.js
⚖️ Axios vs fetch() — ትልቁ ልዩነቱ
| ነጥብ | Axios | fetch() |
|---|---|---|
| JSON auto-parse | ✅ ራሱ ያደርገዋል (Auto-parse) | ❌ .json() መጠቀም ያስፈልጋል |
| 4xx/5xx errors | ✅ እንደ error ይቆጥራል (throw ያደርጋል) | ❌ እንደ error አይቆጠርም |
| Interceptors | ✅ አብሮት ይገኛል (Built-in) | ❌ የለውም |
| Request cancel | ✅ በጣም ቀላል ነው | ❌ ረጅም ኮድ ይፈልጋል |
| Upload progress | ✅ Built-in አለው | ❌ Manual (በራስ መስራት ያሻል) |
| Timeout | ✅ በአንድ option ብቻ ያልቃል | ❌ AbortControllerን ይፈልጋል |
Method ምረጡ → ኮዱ ሲቀያየር ተመልከቱ!
import axios from 'axios'; // ── ቀላሉ GET Request ─────────── const res = await axios.get('https://api.example.com/users'); console.log(res.data); // ✅ JSON ራሱ parse ሆኗል! // ── የ Query ማቀናበሪያ (params) ─ const res2 = await axios.get('/posts', { params: { userId: 1, _limit: 5 } // URL ላይ: /posts?userId=1&_limit=5 — Axios ራሱ ያዘጋጀዋል! });
// ── አዲስ data ለመፍጠር (Create) ─── const res = await axios.post('/api/posts', { title: 'Axios ትምህርት 🔥', body: 'EthioCode SoftSelect!', userId: 1 }); console.log(res.data); // { id:101, title:'...', ... } // ── ከ Auth header ጋር ስንልክ ─── await axios.post('/api/data', data, { headers: { 'Authorization': `Bearer ${token}` } });
// PUT — ሁሉንም ነገር (ሙሉ ዝማኔ) ያደርጋል ── await axios.put(`/api/posts/${id}`, { title: 'አዲስ ርዕስ', body: 'አዲስ ዝርዝር', userId: 1 // ሁሉም field ያስፈልጋል }); // PATCH — አንድ field (ከፊል) ብቻ ለመቀየር ── await axios.patch(`/api/posts/${id}`, { title: 'ርዕሱ ብቻ' // ያልተፃፈው body ሳይቀየር ይቀራል! });
// ── መረጃን ለማጥፋት (ሰርዝ) ──────── await axios.delete(`/api/posts/${id}`); // Server መልሱ status 200 ወይም 204 ይሆናል — ✅ በሚገባ ተሰርዟል! // ── Summary (ማጠቃለያ) ────────── // axios.get → Fetch data (መረጃ ለማምጣት) // axios.post → Create (አዲስ ለመፍጠር) // axios.put → Full update (ሙሉውን ለመቀየር) // axios.patch → Partial update (አንዱን ብቻ ለመቀየር) // axios.delete → Remove (ለመሰረዝ/ለማጥፋት)
Instance (ኢንስታንስ) — baseURL + headers + timeout አንዴ ፃፉ! ከዚያ በኋላ የሚላኩት ሁሉም requests ቀጥታ ይጠቀምባቸዋል!
Create once, use everywhere — no repeated config per request.
import axios from 'axios'; const api = axios.create({ baseURL: 'https://api.ethiojobs.com', timeout: 10000, // 10 ሴኮንድ ካለፈ → Error (ያቋርጣል) headers: { 'Content-Type': 'application/json' } }); // ── Token interceptor (ከ request በፊት ሚሰራ) ─── api.interceptors.request.use(config => { const token = localStorage.getItem('token'); if (token) config.headers.Authorization = `Bearer ${token}`; return config; }); // ── አጠቃቀም (የተቀመጠው baseURL ይቀጠላል) ───── const jobs = await api.get('/api/jobs'); // → አድራሻው ይሆናል .../api/jobs const me = await api.get('/api/profile'); // → አድራሻው ይሆናል .../api/profile
error.response.statuserror.response.dataerror.requesttry { const res = await axios.get('/api/user/999'); } catch (error) { if (error.response) { // ── Server response መጥቷል፣ ነገር ግን error አለው (4xx/5xx) console.log(error.response.status); // ምሳሌ፡ 404 console.log(error.response.data); // Server የላከው Error msg (ለምሳሌ { message: '...' }) if (error.response.status === 401) { window.location.href = '/login'; // Login ካላደረገ ወደ login ገፅ ለመውሰድ } } else if (error.request) { // ── Request ሄደ፣ ነገር ግን Server response አልመለሰም console.log('Network error! እባክዎ ኢንተርኔትዎን ይፈትሹ።'); } else { console.log('Error:', error.message); } }
እውነተኛ HTTP requests ► (jsonplaceholder.typicode.com) API ተጠቅመን እንሞክራለን!
No backend needed — try GET, POST, PUT, DELETE live!
Axios code ፃፉ → ▶ Run አድርጉ! የ console.log() output ከስር ይታያል!
Full axios API simulated — get, post, put, patch, delete all work!
አንድ ጥያቄ — በሁለት መንገድ። ልዩነቱን ተመልከቱ!
fetch('/api/user') .then(res => { // ✋ Response ok መሆኑን ሁሌ ማጣራት አለብን! if(!res.ok) throw Error(res.status); return res.json(); // ዳታውን ወደ JSON ይቀይረዋል }) .then(data => { console.log(data); }) .catch(err => { console.error(err); });
try { const { data } = await axios.get('/api/user'); // ✅ JSON ራሱ parse ያደርጋል! Error ሲመጣ ራሱ ያስተናግዳል! console.log(data); } catch(err) { console.error(err.message); }
.json() ማለትም ሆነ، ወይም error ለመያዝ !res.ok እያሉ ቼክ (check) ማድረግ አያስፈልግም — සියሉንም Auto ራሱ ያከናውነዋል!
res.data ከተባለ ቀጥታ ይሰራል — .json() ፈፅሞ አያስፈልግም! response.data is already parsed JSON, always.
4xx/5xx የሚመጡ ስህተቶችን → Axios ራሱ throw ያደርገዋል! ለመቆጣጠር error.response.statusን መጠቀም ብቻ ነው። fetch() returns ok even for 404 — Axios doesn't.
በ axios.create() — baseURL፣ timeout፣ እና headers አንድ ጊዜ ብቻ አዘጋጁ (config ያድርጉ)! ከዛ በቀላሉ ዳግም መጠቀም ነው። One configured instance shared across your entire app.
ማንኛውንም Request ከመላኩ እና ከተላከ በኋላ ለመቆጣጠር ያስችላል — Token, logging, እና redirect ማድረግ። ለአንዴና ለመጨረሻ ጊዜ ፃፉት! Middleware for HTTP — runs before every request/response.
Timeout (ጊዜ ገደብ) ማስተካከል · Upload progress · እና Request ን አቋርጥ (Cancel tokens) ማለት ይቻላል — እነዚህ ሁሉ ለይተው ከሚያሳዩት (built-in features) ውስጥ ናቸው። Everything a real production app needs, out of the box.