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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. this._verbs = {
  23. get: 'GET',
  24. post: 'POST',
  25. patch: 'PATCH',
  26. }
  27. }
  28. _makeHeader({ method, payload, authorization }) {
  29. const header = { ...headerTemplate }
  30. header.method = method
  31. if (payload) {
  32. header.body = JSON.stringify(payload)
  33. }
  34. if (authorization) {
  35. header.headers.authorization = authorization
  36. }
  37. return header
  38. }
  39. async _tryFetch({ endpoint, header }, returnHeaders = false) {
  40. try {
  41. const res = await fetch(`${remote}${endpoint}`, header)
  42. if (!res.ok) {
  43. throw Error(res.statusText)
  44. }
  45. if (returnHeaders) {
  46. return res.headers
  47. } else {
  48. const jsonRes = await res.json()
  49. return jsonRes.data
  50. }
  51. } catch (error) {
  52. console.error(`[API Util]: ${error}\nroute:`, endpoint)
  53. }
  54. }
  55. async get(endpoint, authHeaders = false) {
  56. if (authHeaders) {
  57. return await this._tryFetch({
  58. endpoint,
  59. header: this._makeHeader({
  60. method: this._verbs.get,
  61. authorization: `${authHeaders}`,
  62. }),
  63. })
  64. } else {
  65. return await this._tryFetch({
  66. endpoint,
  67. header: this._makeHeader({ method: this._verbs.get }),
  68. })
  69. }
  70. }
  71. async post(endpoint, payload = {}, returnHeaders = false) {
  72. return await this._tryFetch({
  73. endpoint,
  74. header: this._makeHeader({ method: this._verbs.post, payload }),
  75. returnHeaders,
  76. })
  77. }
  78. async patch(endpoint, payload = {}) {
  79. return await this._tryFetch({
  80. endpoint,
  81. header: this._makeHeader({ method: this._verbs.patch, payload }),
  82. })
  83. }
  84. /** !: DEV ONLY */
  85. // async removeAll() { }
  86. }
  87. const db = new Connector()
  88. export { Connector, db, remote }