Explorar o código

add headers parameters to apiRequestService methods

Olivier Massot hai 1 ano
pai
achega
59beb7f3f2
Modificáronse 1 ficheiros con 25 adicións e 6 borrados
  1. 25 6
      services/data/apiRequestService.ts

+ 25 - 6
services/data/apiRequestService.ts

@@ -20,9 +20,14 @@ class ApiRequestService {
    *
    * @param url
    * @param query
+   * @param headers
    */
-  public async get(url: string, query: AssociativeArray | null = null) {
-    return await this.request(HTTP_METHOD.GET, url, null, query)
+  public async get(
+    url: string,
+    query: AssociativeArray | null = null,
+    headers: AssociativeArray | null = null,
+  ) {
+    return await this.request(HTTP_METHOD.GET, url, null, query, headers)
   }
 
   /**
@@ -31,13 +36,15 @@ class ApiRequestService {
    * @param url
    * @param body
    * @param query
+   * @param headers
    */
   public async post(
     url: string,
     body: string | AnyJson | null = null,
     query: AssociativeArray | null = null,
+    headers: AssociativeArray | null = null,
   ) {
-    return await this.request(HTTP_METHOD.POST, url, body, query)
+    return await this.request(HTTP_METHOD.POST, url, body, query, headers)
   }
 
   /**
@@ -46,13 +53,15 @@ class ApiRequestService {
    * @param url
    * @param body
    * @param query
+   * @param headers
    */
   public async put(
     url: string,
     body: string | AnyJson | null = null,
     query: AssociativeArray | null = null,
+    headers: AssociativeArray | null = null,
   ) {
-    return await this.request(HTTP_METHOD.PUT, url, body, query)
+    return await this.request(HTTP_METHOD.PUT, url, body, query, headers)
   }
 
   /**
@@ -60,9 +69,14 @@ class ApiRequestService {
    *
    * @param url
    * @param query
+   * @param headers
    */
-  public async delete(url: string, query: AssociativeArray | null = null) {
-    return await this.request(HTTP_METHOD.DELETE, url, null, query)
+  public async delete(
+    url: string,
+    query: AssociativeArray | null = null,
+    headers: AssociativeArray | null = null,
+  ) {
+    return await this.request(HTTP_METHOD.DELETE, url, null, query, headers)
   }
 
   /**
@@ -72,6 +86,7 @@ class ApiRequestService {
    * @param url
    * @param body
    * @param query
+   * @param headers
    * @protected
    */
   protected async request(
@@ -79,6 +94,7 @@ class ApiRequestService {
     url: string,
     body: string | AnyJson | null = null,
     query: AssociativeArray | null = null,
+    headers: AssociativeArray | null = null,
   ): Promise<Response> {
     const config: FetchOptions = { method }
     if (query) {
@@ -87,6 +103,9 @@ class ApiRequestService {
     if (method === HTTP_METHOD.POST || method === HTTP_METHOD.PUT) {
       config.body = body
     }
+    if (headers) {
+      config.headers = headers
+    }
 
     // @ts-expect-error TODO: solve the type mismatch
     return await this.fetch(url, config)