apiRequestService.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * A basic api request service
  3. *
  4. * It will send basic http requests and returns raw results
  5. */
  6. import {AssociativeArray} from "~/types/data";
  7. import {HTTP_METHOD} from "~/types/enum/data";
  8. import {$Fetch, FetchOptions} from "ohmyfetch";
  9. class ApiRequestService {
  10. // TODO: fusionner les paramètres `query` et `params` des méthodes, puisque l'un est un alias de l'autre
  11. // dans ohmyfetch : https://github.com/unjs/ofetch#%EF%B8%8F-adding-query-search-params
  12. private readonly fetch: $Fetch
  13. public constructor(
  14. fetch: $Fetch
  15. ) {
  16. this.fetch = fetch
  17. }
  18. /**
  19. * Send a GET request
  20. *
  21. * @param url
  22. * @param query
  23. */
  24. public async get(
  25. url: string,
  26. query: AssociativeArray | null = null
  27. ) {
  28. return await this.request(HTTP_METHOD.GET, url, null, null, query)
  29. }
  30. /**
  31. * Send a POST request
  32. *
  33. * @param url
  34. * @param body
  35. * @param params
  36. * @param query
  37. */
  38. public async post(
  39. url: string,
  40. body: string | null = null,
  41. params: AssociativeArray | null = null,
  42. query: AssociativeArray | null = null
  43. ) {
  44. return await this.request(HTTP_METHOD.POST, url, body, params, query)
  45. }
  46. /**
  47. * Send a PUT request
  48. *
  49. * @param url
  50. * @param body
  51. * @param params
  52. * @param query
  53. */
  54. public async put(
  55. url: string,
  56. body: string | null = null,
  57. params: AssociativeArray | null = null,
  58. query: AssociativeArray | null = null
  59. ) {
  60. return await this.request(HTTP_METHOD.PUT, url, body, params, query)
  61. }
  62. /**
  63. * Send a DELETE request
  64. *
  65. * @param url
  66. * @param query
  67. */
  68. public async delete(
  69. url: string,
  70. query: AssociativeArray | null = null
  71. ) {
  72. return await this.request(HTTP_METHOD.DELETE, url, null, null, query)
  73. }
  74. /**
  75. * Send an http request
  76. *
  77. * @param method
  78. * @param url
  79. * @param body
  80. * @param params
  81. * @param query
  82. * @protected
  83. */
  84. protected async request(
  85. method: HTTP_METHOD,
  86. url: string,
  87. body: string | null = null,
  88. params: AssociativeArray | null = null,
  89. query: AssociativeArray | null = null
  90. ): Promise<Response> {
  91. const config: FetchOptions = { method }
  92. if (params) {
  93. config.params = params
  94. }
  95. if (query) {
  96. config.query = query
  97. }
  98. if (method === HTTP_METHOD.POST || method === HTTP_METHOD.PUT) {
  99. config.body = body
  100. }
  101. // @ts-ignore
  102. return this.fetch(url, config)
  103. }
  104. }
  105. export default ApiRequestService