Всем привет!
как на JS сделать обертку для fetch на js?
чтобы работало:
fetch.put, fetch. post
означая что метод put и post соответственно
спасибо
Stack Overflow на русском — это сайт вопросов и ответов для программистов. Присоединяйтесь! Регистрация займёт не больше минуты.
Присоединиться к сообществуfetch.put = createFetchWrapper("PUT");
fetch.get = createFetchWrapper("GET");
fetch.post = createFetchWrapper("POST");
// Get запрос
fetch.get("https://reqres.in/api/users?page=1")
.then(res => res.json())
.then(console.log);
// Post запрос
fetch.post("https://reqres.in/api/users", {cors: "no-cors"})
.then(res => res.json())
.then(console.log);
// PUT запрос несмотря на переданный параметр method в объекте настроек
fetch.put("https://reqres.in/api/users", { method: "GET" })
.then(res => res.json())
.then(console.log);
function createFetchWrapper(method) {
const defaultOptions = { method };
return function wrapper(url, options) {
options || (options = {});
return fetch(url, Object.assign(options, defaultOptions));
}
}