apiRequestService.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 DELETE request
  63. *
  64. * @param url
  65. * @param query
  66. * @param headers
  67. */
  68. public async delete(
  69. url: string,
  70. query: AssociativeArray | null = null,
  71. headers: AssociativeArray | null = null,
  72. ) {
  73. return await this.request(HTTP_METHOD.DELETE, url, null, query, headers)
  74. }
  75. /**
  76. * Send an http request
  77. *
  78. * @param method
  79. * @param url
  80. * @param body
  81. * @param query
  82. * @param headers
  83. * @protected
  84. */
  85. protected async request(
  86. method: HTTP_METHOD,
  87. url: string,
  88. body: string | AnyJson | null = null,
  89. query: AssociativeArray | null = null,
  90. headers: AssociativeArray | null = null,
  91. ): Promise<Response> {
  92. const config: FetchOptions = { method }
  93. if (query) {
  94. config.query = query
  95. }
  96. if (method === HTTP_METHOD.POST || method === HTTP_METHOD.PUT) {
  97. config.body = body
  98. }
  99. if (headers) {
  100. config.headers = headers
  101. }
  102. // @ts-expect-error TODO: solve the type mismatch
  103. return await this.fetch(url, config)
  104. }
  105. }
  106. export default ApiRequestService