Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

db.js 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. const jsonRes = await res.json()
  43. if (!res.ok) {
  44. // NOTE: Somewhat hacky workaround here to get auth working
  45. if (res.status === 401) {
  46. return { ...jsonRes.data, status: res.status }
  47. } else {
  48. throw Error(res.statusText)
  49. }
  50. }
  51. if (returnHeaders) {
  52. return res.headers
  53. } else {
  54. return jsonRes.data
  55. }
  56. } catch (error) {
  57. console.error(`[API Util]: ${error}\nroute:`, endpoint)
  58. }
  59. }
  60. async get(endpoint, authHeaders = false) {
  61. if (authHeaders) {
  62. return await this._tryFetch({
  63. endpoint,
  64. header: this._makeHeader({
  65. method: this._verbs.get,
  66. authorization: `${authHeaders}`,
  67. }),
  68. })
  69. } else {
  70. return await this._tryFetch({
  71. endpoint,
  72. header: this._makeHeader({ method: this._verbs.get }),
  73. })
  74. }
  75. }
  76. async post(endpoint, payload = {}, returnHeaders = false) {
  77. return await this._tryFetch({
  78. endpoint,
  79. header: this._makeHeader({ method: this._verbs.post, payload }),
  80. returnHeaders,
  81. })
  82. }
  83. async patch(endpoint, payload = {}) {
  84. return await this._tryFetch({
  85. endpoint,
  86. header: this._makeHeader({ method: this._verbs.patch, payload }),
  87. })
  88. }
  89. /** !: DEV ONLY */
  90. // async removeAll() { }
  91. }
  92. const db = new Connector()
  93. export { Connector, db, remote }