APIRepository.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* It's a class that wraps the APIInstanceWrapper class and provides a simple interface for making CRUD
  2. requests to a RESTful API */
  3. import APIInstanceWrapper from './APIInstanceWrapper'
  4. export default class APIRepository {
  5. constructor(name, api, urlAddress, model, APIAdresses) {
  6. this.name = name
  7. this.api = api
  8. this.url = urlAddress
  9. this._model = model
  10. this.APIAdresses = APIAdresses
  11. this.FullAPIAdresses = APIAdresses === undefined || APIAdresses === null ? {} : this.getFullAPIAddress()
  12. }
  13. /**
  14. * It takes the APIAdresses object and adds the baseURL to each of the values
  15. * @returns An object with the same keys as the APIAdresses object, but with the values being the
  16. * baseURL + the APIAdresses values.
  17. */
  18. getFullAPIAddress() {
  19. const urlAddress = {}
  20. Object.keys(this.APIAdresses).map(item => {
  21. if (typeof this.APIAdresses[item] === 'function') {
  22. urlAddress[item] = (id) => {
  23. return this.api.defaults.baseURL + this.APIAdresses[item](id)
  24. }
  25. } else {
  26. urlAddress[item] = this.api.defaults.baseURL + this.APIAdresses[item]
  27. }
  28. return this.api.defaults.baseURL + this.APIAdresses[item]
  29. })
  30. return urlAddress
  31. }
  32. /**
  33. * It takes in a requestData object, and returns a promise that resolves to the result of the
  34. * resolveCallback function, or rejects to the result of the rejectCallback function
  35. * @param requestData - { apiMethod, api, request, cacheKey, cache, resolveCallback, rejectCallback, data, params }
  36. * @returns A promise that will resolve or reject based on the response from the API call.
  37. */
  38. sendRequest({ apiMethod, api, request, cacheKey, cache, resolveCallback, rejectCallback, data, params, responseType }) {
  39. return new Promise((resolve, reject) => {
  40. APIInstanceWrapper[apiMethod]({
  41. api,
  42. request,
  43. cacheKey,
  44. cache,
  45. params,
  46. responseType,
  47. data
  48. })
  49. .then((response) => {
  50. resolve(resolveCallback(response))
  51. })
  52. .catch((error) => {
  53. reject(rejectCallback(error))
  54. })
  55. })
  56. }
  57. /**
  58. * It takes two objects, merges them, and returns the merged object
  59. * @param defaultData - The default data that will be sent to the server.
  60. * @param data - The data to be sent to the server.
  61. * @returns The defaultSendData is being returned.
  62. */
  63. getNormalizedSendData(defaultData, data) {
  64. // for (const key of Object.keys(defaultData)) {
  65. // if (data[key] === null || data[key] === undefined) {
  66. // delete defaultData[key]
  67. // }
  68. // }
  69. return Object.assign(defaultData, data)
  70. }
  71. /**
  72. explain
  73. */
  74. paramSerializer(params) {
  75. }
  76. /**
  77. * It sets the callbacks for the CRUD operations.
  78. * @param callbacks - an object with the following properties:
  79. */
  80. setCrudCallbacks(callbacks) {
  81. this.getResolveCallback = callbacks.get ? callbacks.get : (response) => { return response }
  82. this.postResolveCallback = callbacks.post ? callbacks.post : (response) => { return response }
  83. this.putResolveCallback = callbacks.put ? callbacks.put : (response) => { return response }
  84. this.deleteResolveCallback = callbacks.delete ? callbacks.delete : (response) => { return response }
  85. }
  86. /**
  87. * This function sends a GET request to the API endpoint for the entity, and returns the response
  88. * @param entityId - The id of the entity you want to get.
  89. * @returns The response from the server.
  90. */
  91. get(entityData) {
  92. return this.sendRequest({
  93. apiMethod: 'get',
  94. api: this.api,
  95. request: this.restUrl(entityData.data.id),
  96. cacheKey: this.restUrl(entityData.data.id) + '-get',
  97. cache: entityData.cache,
  98. resolveCallback: (response) => {
  99. return this.getResolveCallback(response)
  100. },
  101. rejectCallback: (error) => {
  102. return error
  103. }
  104. })
  105. }
  106. /**
  107. * It sends a POST request to the API, and returns the response
  108. * @param entityData - The data that will be sent to the API.
  109. * @returns The return value of the sendRequest function.
  110. */
  111. post(entityData) {
  112. return this.sendRequest({
  113. apiMethod: 'post',
  114. api: this.api,
  115. request: this.baseUrl,
  116. cacheKey: this.name + '-post',
  117. cache: entityData.cache,
  118. resolveCallback: (response) => {
  119. return this.postResolveCallback(response)
  120. },
  121. rejectCallback: (error) => {
  122. return error
  123. },
  124. data: new this._model(entityData.data)
  125. })
  126. }
  127. /**
  128. * The function sends a PUT request to the API, and returns the response
  129. * @param entityData - The data to be sent to the server.
  130. * @returns The return value of the sendRequest function.
  131. */
  132. put(entityData) {
  133. return this.sendRequest({
  134. apiMethod: 'put',
  135. api: this.api,
  136. request: this.restUrl(entityData.data.id),
  137. cacheKey: this.name + '-put',
  138. resolveCallback: (response) => {
  139. return this.putResolveCallback(response)
  140. },
  141. rejectCallback: (error) => {
  142. return error
  143. },
  144. data: new this._model(entityData.data)
  145. })
  146. }
  147. /**
  148. * It deletes an entity.
  149. * @param entityId - The id of the entity you want to delete.
  150. * @returns The promise object.
  151. */
  152. delete(entityId) {
  153. return this.sendRequest({
  154. apiMethod: 'delete',
  155. api: this.api,
  156. request: this.restUrl(entityId.data.id),
  157. cacheKey: this.name + '-delete',
  158. resolveCallback: (response) => {
  159. return this.deleteResolveCallback(response)
  160. },
  161. rejectCallback: (error) => {
  162. return error
  163. }
  164. })
  165. }
  166. }