Olivier Massot пре 9 месеци
родитељ
комит
b04ecfd0ba
3 измењених фајлова са 30 додато и 4 уклоњено
  1. 28 3
      services/data/apiRequestService.ts
  2. 1 1
      services/data/entityManager.ts
  3. 1 0
      types/enum/data.ts

+ 28 - 3
services/data/apiRequestService.ts

@@ -64,6 +64,23 @@ class ApiRequestService {
     return await this.request(HTTP_METHOD.PUT, url, body, query, headers)
   }
 
+  /**
+   * Send a PUT request
+   *
+   * @param url
+   * @param body
+   * @param query
+   * @param headers
+   */
+  public async patch(
+    url: string,
+    body: string | AnyJson | null = null,
+    query: AssociativeArray | null = null,
+    headers: AssociativeArray | null = null,
+  ) {
+    return await this.request(HTTP_METHOD.PATCH, url, body, query, headers)
+  }
+
   /**
    * Send a DELETE request
    *
@@ -100,11 +117,19 @@ class ApiRequestService {
     if (query) {
       config.query = query
     }
-    if (method === HTTP_METHOD.POST || method === HTTP_METHOD.PUT) {
+
+    config.headers = headers ? { ...headers } : {}
+
+    config.headers['Accept'] = 'application/ld+json'
+    config.headers['Content-Type'] = 'application/ld+json'
+
+    if (method === HTTP_METHOD.POST || method === HTTP_METHOD.PUT || method === HTTP_METHOD.PATCH) {
       config.body = body
     }
-    if (headers) {
-      config.headers = headers
+
+    if (method === HTTP_METHOD.PATCH) {
+      // config.headers['Accept'] = 'application/merge-patch+json'
+      config.headers['Content-Type'] = 'application/merge-patch+json'
     }
 
     // @ts-expect-error TODO: solve the type mismatch

+ 1 - 1
services/data/entityManager.ts

@@ -279,7 +279,7 @@ class EntityManager {
 
     if (!instance.isNew()) {
       url = UrlUtils.join(url, String(instance.id))
-      response = await this.apiRequestService.put(url, data, null, headers)
+      response = await this.apiRequestService.patch(url, data, null, headers)
     } else {
       delete data.id
       response = await this.apiRequestService.post(url, data, null, headers)

+ 1 - 0
types/enum/data.ts

@@ -1,6 +1,7 @@
 export const enum HTTP_METHOD {
   POST = 'POST',
   PUT = 'PUT',
+  PATCH = 'PATCH',
   GET = 'GET',
   DELETE = 'DELETE',
 }