Fetch wrapper
Specifikáció
export default {
get: url => {
try {
const response = await fetch(url);
...
} catch (e) {
}
},
post: (url, body) => {
...
}
};
Használat
import api from "../lib/api;
...
try {
const response = await api.get("/api/v1/questions/1/");
// process response
} catch (e) {
// process errors
}
getInitialProps-ban át kell a hívásnak adni a ctx-et is:
Index.getInitialProps = async ctx => {
try {
const response = await api.get("/api/v1/questions", ctx);
// process data
} catch (e) {
// process errors
}
};
Edited by Gál Dániel