Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const api = (() => {
- const make = async (func, callback) => {
- let res;
- try {
- res = await func();
- if (typeof callback === 'function') {
- await callback(res);
- }
- } catch (error) {
- notify(error.message);
- }
- return res; // ako vuobshte na nqkoimu trqbva sled callback-a moje da si awaitne v async func
- };
- const request = async (options, endpoint = '') => {
- const res = await fetch(`https://fisher-game.firebaseio.com/catches/${endpoint}.json`, options);
- if (res.ok) {
- return res.json();
- }
- throw new Error(`${options.method}: ${res.statusText}`);
- };
- return {
- get: (callback) => make(() => request({ method: 'GET' }), callback),
- create: (data, callback) => make(() => request({ method: 'POST', body: data }), callback),
- // PATCH bi bilo udachno, no mnogo potrebiteli clickat na edni i sushti neshta, zaradi tova PUT s celiq obekt
- update: (data, id, callback) => make(() => request({ method: 'PUT', body: data }, id), callback),
- delete: (id, callback) => make(() => request({ method: 'DELETE' }, id), callback),
- };
- })();
Advertisement
Add Comment
Please, Sign In to add comment