apiRequestService.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * A basic api request service
  3. *
  4. * It will send basic http requests and returns raw results
  5. */
  6. import type { FetchOptions } from 'ofetch'
  7. import type { $Fetch } from 'nitropack'
  8. import type { AnyJson, AssociativeArray } from '~/types/data'
  9. import { HTTP_METHOD } from '~/types/enum/data'
  10. class ApiRequestService {
  11. private readonly fetch: $Fetch
  12. public constructor(fetch: $Fetch) {
  13. this.fetch = fetch
  14. }
  15. /**
  16. * Send a GET request
  17. *
  18. * @param url
  19. * @param query
  20. * @param headers
  21. */
  22. public async get(
  23. url: string,
  24. query: AssociativeArray | null = null,
  25. headers: AssociativeArray | null = null,
  26. ) {
  27. return await this.request(HTTP_METHOD.GET, url, null, query, headers)
  28. }
  29. /**
  30. * Send a POST request
  31. *
  32. * @param url
  33. * @param body
  34. * @param query
  35. * @param headers
  36. */
  37. public async post(
  38. url: string,
  39. body: string | AnyJson | null = null,
  40. query: AssociativeArray | null = null,
  41. headers: AssociativeArray | null = null,
  42. ) {
  43. return await this.request(HTTP_METHOD.POST, url, body, query, headers)
  44. }
  45. /**
  46. * Send a PUT request
  47. *
  48. * @param url
  49. * @param body
  50. * @param query
  51. * @param headers
  52. */
  53. public async put(
  54. url: string,
  55. body: string | AnyJson | null = null,
  56. query: AssociativeArray | null = null,
  57. headers: AssociativeArray | null = null,
  58. ) {
  59. return await this.request(HTTP_METHOD.PUT, url, body, query, headers)
  60. }
  61. /**
  62. * Send a PUT request
  63. *
  64. * @param url
  65. * @param body
  66. * @param query
  67. * @param headers
  68. */
  69. public async patch(
  70. url: string,
  71. body: string | AnyJson | null = null,
  72. query: AssociativeArray | null = null,
  73. headers: AssociativeArray | null = null,
  74. ) {
  75. return await this.request(HTTP_METHOD.PATCH, url, body, query, headers)
  76. }
  77. /**
  78. * Send a DELETE request
  79. *
  80. * @param url
  81. * @param query
  82. * @param headers
  83. */
  84. public async delete(
  85. url: string,
  86. query: AssociativeArray | null = null,
  87. headers: AssociativeArray | null = null,
  88. ) {
  89. return await this.request(HTTP_METHOD.DELETE, url, null, query, headers)
  90. }
  91. /**
  92. * Send an http request
  93. *
  94. * @param method
  95. * @param url
  96. * @param body
  97. * @param query
  98. * @param headers
  99. * @protected
  100. */
  101. protected async request(
  102. method: HTTP_METHOD,
  103. url: string,
  104. body: string | AnyJson | null = null,
  105. query: AssociativeArray | null = null,
  106. headers: AssociativeArray | null = null,
  107. ): Promise<Response> {
  108. const config: FetchOptions = { method }
  109. if (query) {
  110. config.query = query
  111. }
  112. config.headers = headers ? { ...headers } : {}
  113. config.headers.Accept = 'application/ld+json'
  114. config.headers['Content-Type'] = 'application/ld+json'
  115. if (
  116. method === HTTP_METHOD.POST ||
  117. method === HTTP_METHOD.PUT ||
  118. method === HTTP_METHOD.PATCH
  119. ) {
  120. config.body = body
  121. }
  122. if (method === HTTP_METHOD.PATCH) {
  123. // config.headers['Accept'] = 'application/merge-patch+json'
  124. config.headers['Content-Type'] = 'application/merge-patch+json'
  125. }
  126. // @ts-expect-error TODO: solve the type mismatch
  127. return await this.fetch(url, config)
  128. }
  129. }
  130. export default ApiRequestService