|
1 | | -const axios= require("axios"); |
| 1 | +const axios = require("axios"); |
2 | 2 |
|
3 | | -const url= `https://github-db.glitch.me/db/api`; |
| 3 | +const url = `https://github-db.glitch.me/db/api`; |
4 | 4 |
|
5 | | -function Client({db,token}){ |
6 | | - const options={db,token}; |
7 | | - let validated=false; |
8 | | - async function validate(){ |
9 | | - if(!validated){ |
10 | | - const {data}=await axios.get(`${url}/validate`,{params:options}); |
11 | | - validated=data.valid |
12 | | - } |
| 5 | +function Client({ db, token }) { |
| 6 | + const options = { db, token }; |
| 7 | + let validated = false; |
| 8 | + async function validate() { |
| 9 | + try { |
| 10 | + if (!validated) { |
| 11 | + const { data } = await axios.get(`${url}/validate`, { |
| 12 | + params: options |
| 13 | + }); |
| 14 | + validated = data.valid; |
| 15 | + } |
| 16 | + } catch (error) { |
| 17 | + hanlderError(error); |
13 | 18 | } |
14 | | - async function add(document,payload){ |
15 | | - await validate(); |
16 | | - const resp=await axios.post(`${url}/add`,{db:options.db,token,document,payload}); |
17 | | - return resp.data.identifier; |
| 19 | + } |
| 20 | + async function add(document, payload) { |
| 21 | + try { |
| 22 | + await validate(); |
| 23 | + const resp = await axios.post(`${url}/add`, { |
| 24 | + db: options.db, |
| 25 | + token, |
| 26 | + document, |
| 27 | + payload |
| 28 | + }); |
| 29 | + return resp.data; |
| 30 | + } catch (error) { |
| 31 | + hanlderError(error); |
18 | 32 | } |
19 | | - async function fetchOne(document,identifier){ |
20 | | - await validate(); |
21 | | - const resp=await axios.get(`${url}/fetchOne`,{params:{db:options.db,token,document,identifier}}); |
22 | | - return resp.data; |
| 33 | + } |
| 34 | + async function update(document, identifier, payload) { |
| 35 | + try { |
| 36 | + await validate(); |
| 37 | + const resp = await axios.post(`${url}/update`, { |
| 38 | + db: options.db, |
| 39 | + token, |
| 40 | + document, |
| 41 | + identifier, |
| 42 | + payload |
| 43 | + }); |
| 44 | + return resp.data; |
| 45 | + } catch (error) { |
| 46 | + hanlderError(error); |
23 | 47 | } |
24 | | - async function fetchAll(document){ |
25 | | - await validate(); |
26 | | - const resp=await axios.get(`${url}/fetchAll`,{params:{db:options.db,token,document}}); |
27 | | - return resp.data; |
| 48 | + } |
| 49 | + async function _delete(document, identifier) { |
| 50 | + try { |
| 51 | + await validate(); |
| 52 | + const resp = await axios.post(`${url}/delete`, { |
| 53 | + db: options.db, |
| 54 | + token, |
| 55 | + document, |
| 56 | + identifier |
| 57 | + }); |
| 58 | + return resp.data; |
| 59 | + } catch (error) { |
| 60 | + hanlderError(error); |
28 | 61 | } |
29 | | - return { |
30 | | - add,fetchOne,fetchAll |
| 62 | + } |
| 63 | + async function fetchOne(document, identifier) { |
| 64 | + try { |
| 65 | + await validate(); |
| 66 | + const resp = await axios.get(`${url}/fetchOne`, { |
| 67 | + params: { db: options.db, token, document, identifier } |
| 68 | + }); |
| 69 | + return resp.data; |
| 70 | + } catch (error) { |
| 71 | + hanlderError(error); |
31 | 72 | } |
| 73 | + } |
| 74 | + async function fetchAll(document) { |
| 75 | + try { |
| 76 | + await validate(); |
| 77 | + const resp = await axios.get(`${url}/fetchAll`, { |
| 78 | + params: { db: options.db, token, document } |
| 79 | + }); |
| 80 | + return resp.data; |
| 81 | + } catch (error) { |
| 82 | + hanlderError(error); |
| 83 | + } |
| 84 | + } |
| 85 | + return { |
| 86 | + add, |
| 87 | + fetchOne, |
| 88 | + fetchAll, |
| 89 | + delete: _delete, |
| 90 | + update |
| 91 | + }; |
| 92 | +} |
| 93 | +function hanlderError(error) { |
| 94 | + if (error.response) { |
| 95 | + throw new Error(error.response.data); |
| 96 | + } else if (error.request) { |
| 97 | + throw error.request; |
| 98 | + } else { |
| 99 | + throw new Error(error.message); |
| 100 | + } |
32 | 101 | } |
33 | | -module.exports= Client; |
| 102 | + |
| 103 | +module.exports = Client; |
0 commit comments