Olivier Massot 4 лет назад
Родитель
Сommit
a3a4d89524

+ 17 - 6
services/connection/urlBuilder.ts

@@ -4,9 +4,6 @@ import { QUERY_TYPE } from '~/types/enums'
 import { repositoryHelper } from '~/services/store/repository'
 
 /**
- * @category Services/connection
- * @class UrlBuilder
- *
  * Classe permettant de construire une URL pour l'interrogation d'une API externe
  */
 class UrlBuilder {
@@ -42,7 +39,7 @@ class UrlBuilder {
     if (typeof url === 'undefined') {
       throw new TypeError('url must be defined')
     }
-    return String(UrlBuilder.ROOT + url).toString()
+    return UrlBuilder.concat(UrlBuilder.ROOT, url)
   }
 
   /**
@@ -54,7 +51,7 @@ class UrlBuilder {
     if (typeof enumType === 'undefined') {
       throw new TypeError('enumType must be defined')
     }
-    return String(UrlBuilder.ROOT + 'enum/' + enumType).toString()
+    return UrlBuilder.concat(UrlBuilder.ROOT, 'enum', enumType)
   }
 
   /**
@@ -82,7 +79,21 @@ class UrlBuilder {
       return String(`${rootUrl}/${rootId}/${entity}`).toString()
     }
 
-    return String(UrlBuilder.ROOT + entity).toString()
+    return UrlBuilder.concat(UrlBuilder.ROOT, entity)
+  }
+
+  /**
+   * Concatenate a base url and a tail
+   * @param base
+   * @param tails
+   * @private
+   */
+  public static concat (base: string, ...tails: string[]): string {
+    let url = base
+    tails.forEach((tail: string) => {
+      url = url.replace(/^|\/$/g, '') + '/' + tail.replace(/^\/?|$/g, '')
+    })
+    return url
   }
 }
 

+ 10 - 3
services/data/dataDeleter.ts

@@ -2,6 +2,9 @@ import { DataDeleterArgs } from '~/types/interfaces'
 import BaseDataManager from '~/services/data/baseDataManager'
 import { repositoryHelper } from '~/services/store/repository'
 import { hooksDeleter } from '~/services/data/hooks/hookDeleter/_import'
+import UrlBuilder from '~/services/connection/urlBuilder'
+import { HTTP_METHOD } from '~/types/enums'
+import Connection from '~/services/connection/connection'
 
 /**
  * Le DataDeleter a pour rôle de supprimer des enregistrements via l'API Opentalent
@@ -14,12 +17,16 @@ class DataDeleter extends BaseDataManager {
    * Exécute la requête
    */
   protected async _invoke (): Promise<any> {
-    // const url = ConstructUrl.invoke(this.arguments)
-    // const response = await Connection.invoke(HTTP_METHOD.DELETE, url, this.arguments)
+    // build the url according to the url args
+    const url = UrlBuilder.invoke(this.arguments)
+
+    // send the DELETE request to the api
+    await Connection.invoke(HTTP_METHOD.DELETE, url, this.arguments)
+
+    // update the store
     if (this.arguments.model) {
       await repositoryHelper.deleteItem(this.arguments.model, this.arguments.id)
     }
-    return null
   }
 }
 

+ 7 - 1
services/data/dataPersister.ts

@@ -21,20 +21,26 @@ class DataPersister extends BaseDataManager {
    * Exécute la requête et retourne la réponse désérialisée
    */
   protected async _invoke (): Promise<any> {
+    // serialize the data to persist and attach it to the request arguments
     this.arguments.data = Serializer.normalize(this.arguments)
 
+    // build the url according to the url args
     const url = UrlBuilder.build(this.arguments)
+
+    // send the POST / PUT request to the api and retrieve the response
     const response = await DataPersister.request(
       url,
       this.arguments.id ? HTTP_METHOD.PUT : HTTP_METHOD.POST,
       this.arguments
     )
 
+    // Deserialize, post-process and return the response
     return await this.provideResponse(response, this.arguments)
   }
 
   /**
-   * ?
+   * Use a data provider to deserialize, post-process and return the response
+   *
    * @param response
    * @param args
    */

+ 12 - 1
services/data/dataProvider.ts

@@ -28,20 +28,31 @@ class DataProvider extends BaseDataManager {
    * Exécute la requête et retourne la réponse désérialisée
    */
   protected async _invoke (): Promise<any> {
+    // build the url according to the url args
     const url = UrlBuilder.build(this.arguments)
+
+    // send the GET request to the api and retrieve the response
     const response = await DataProvider.request(url, HTTP_METHOD.GET, this.arguments)
+
+    // deserialize the response
     const data = await Serializer.denormalize(response, DENORMALIZER_TYPE.HYDRA)
 
+    // post-process the data with the first supported processor
     return await this.process(data, this.arguments)
   }
 
+  /**
+   * Find the first supported post data processor, and process the data with it
+   * @param data
+   * @param args
+   */
   public async process (data: AnyJson, args: DataProviderArgs) {
     const postProcessor = this.getProcessor(args)
     return await postProcessor.process(data)
   }
 
   /**
-   * Iterate over the available providers and invoke
+   * Iterate over the available data processors and return an instance of
    * the first one that support the given args
    * @param args
    */