Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const domain = 'localhost'
  2. const port = 3001
  3. const httpProtocol = `http`
  4. const prefix = 'api'
  5. const remote = `${httpProtocol}://${domain}:${port}/${prefix}`
  6. const headerTemplate = {
  7. method: null,
  8. mode: 'cors', // no-cors, *cors, same-origin
  9. cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
  10. credentials: 'omit', // include, *same-origin, omit
  11. headers: {
  12. 'Content-Type': 'application/json',
  13. // 'Content-Type': 'application/x-www-form-urlencoded',
  14. },
  15. redirect: 'manual', // manual, *follow, error
  16. referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
  17. body: null, // body data type must match "Content-Type" header
  18. }
  19. class Connector {
  20. constructor() {
  21. this.apiPrefix = prefix
  22. }
  23. async get(endpoint) {
  24. const header = { ...headerTemplate }
  25. header.method = 'GET'
  26. try {
  27. // console.log(`${remote}${endpoint}`)
  28. let res = await fetch(`${remote}${endpoint}`, header)
  29. if (!res.ok) {
  30. throw Error(res.statusText)
  31. }
  32. const jsonRes = await res.json()
  33. return jsonRes.data
  34. } catch (error) {
  35. console.error(error)
  36. }
  37. }
  38. async post(endpoint, payload = {}) {
  39. const header = { ...headerTemplate }
  40. header.method = 'POST'
  41. header.body = JSON.stringify(payload)
  42. try {
  43. let res = await fetch(`${remote}${endpoint}`, header)
  44. if (!res.ok) {
  45. throw Error(res.statusText)
  46. }
  47. const jsonRes = await res.json()
  48. return jsonRes.data
  49. } catch (error) {
  50. console.error(error)
  51. }
  52. }
  53. async patch(endpoint, payload = {}) {
  54. const header = { ...headerTemplate }
  55. header.method = 'PATCH'
  56. header.body = JSON.stringify(payload)
  57. try {
  58. let res = await fetch(`${remote}${endpoint}`, header)
  59. if (!res.ok) {
  60. throw Error(res.statusText)
  61. }
  62. const jsonRes = await res.json()
  63. return jsonRes.data
  64. } catch (error) {
  65. console.error(error)
  66. }
  67. }
  68. /** !: DEV ONLY */
  69. // async removeAll() { }
  70. }
  71. const db = new Connector()
  72. export { Connector, db, remote }