apiRequestService.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. */
  21. public async get(url: string, query: AssociativeArray | null = null) {
  22. return await this.request(HTTP_METHOD.GET, url, null, query)
  23. }
  24. /**
  25. * Send a POST request
  26. *
  27. * @param url
  28. * @param body
  29. * @param query
  30. */
  31. public async post(
  32. url: string,
  33. body: string | AnyJson | null = null,
  34. query: AssociativeArray | null = null
  35. ) {
  36. return await this.request(HTTP_METHOD.POST, url, body, query)
  37. }
  38. /**
  39. * Send a PUT request
  40. *
  41. * @param url
  42. * @param body
  43. * @param query
  44. */
  45. public async put(
  46. url: string,
  47. body: string | AnyJson | null = null,
  48. query: AssociativeArray | null = null
  49. ) {
  50. return await this.request(HTTP_METHOD.PUT, url, body, query)
  51. }
  52. /**
  53. * Send a DELETE request
  54. *
  55. * @param url
  56. * @param query
  57. */
  58. public async delete(url: string, query: AssociativeArray | null = null) {
  59. return await this.request(HTTP_METHOD.DELETE, url, null, query)
  60. }
  61. /**
  62. * Send an http request
  63. *
  64. * @param method
  65. * @param url
  66. * @param body
  67. * @param query
  68. * @param headers
  69. * @protected
  70. */
  71. protected async request(
  72. method: HTTP_METHOD,
  73. url: string,
  74. body: string | AnyJson | null = null,
  75. query: AssociativeArray | null = null,
  76. headers: AssociativeArray | null = null,
  77. ): Promise<Response> {
  78. const config: FetchOptions = { method }
  79. if (query) {
  80. config.query = query
  81. }
  82. config.headers = headers ? { ...headers } : {}
  83. config.headers.Accept = 'application/ld+json'
  84. config.headers['Content-Type'] = 'application/ld+json'
  85. if (
  86. method === HTTP_METHOD.POST ||
  87. method === HTTP_METHOD.PUT ||
  88. method === HTTP_METHOD.PATCH
  89. ) {
  90. config.body = body
  91. }
  92. if (method === HTTP_METHOD.PATCH) {
  93. // config.headers['Accept'] = 'application/merge-patch+json'
  94. config.headers['Content-Type'] = 'application/merge-patch+json'
  95. }
  96. // @ts-expect-error TODO: solve the type mismatch
  97. return await this.fetch(url, config)
  98. }
  99. }
  100. export default ApiRequestService