connection.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import {AnyJson, DataPersisterArgs, DataProviderArgs} from "~/types/interfaces";
  2. import {HTTP_METHOD} from "~/types/enums";
  3. import {NuxtAxiosInstance} from "@nuxtjs/axios";
  4. import {AxiosRequestConfig} from "axios";
  5. /**
  6. * @category Services/connection
  7. * @class Connection
  8. * Classe Wrapper du connecteur de requete (Axios dans notre cas)
  9. */
  10. class Connection{
  11. static connector:NuxtAxiosInstance;
  12. /**
  13. * Initialisation du connecteur (Axios dans notre cas)
  14. * @param {NuxtAxiosInstance} connector
  15. */
  16. static initConnector(connector: NuxtAxiosInstance){
  17. Connection.connector = connector
  18. }
  19. /**
  20. * Main méthode qui apperlera les méthode privées correspondantes (getItem, getCollection, put, post, delete)
  21. * @param {HTTP_METHOD} method de requetage
  22. * @param {string} url
  23. * @param {DataProviderArgs|DataPersisterArgs} args
  24. * @return {Promise<any>}
  25. */
  26. invoke(method: HTTP_METHOD, url: string, args:DataProviderArgs|DataPersisterArgs): Promise<any>{
  27. switch (method) {
  28. case HTTP_METHOD.GET:
  29. if(args.id)
  30. return this.getItem(url, args.id, args.progress)
  31. else
  32. return this.getCollection(url, args.progress)
  33. case HTTP_METHOD.PUT:
  34. if(this.isArgsIsDataPersisterArgs(args)){
  35. if(!args.data)
  36. throw new Error('data not found')
  37. return this.put(url, args.id, args.data, args.progress)
  38. }
  39. else throw new Error('args not a dataPersisterArgs')
  40. case HTTP_METHOD.DELETE:
  41. return this.deleteItem(url, args.id, args.progress)
  42. }
  43. throw new Error('Method unknown')
  44. }
  45. /**
  46. * GET Item : préparation de la config pour la récupération d'un item
  47. * @param {string} url
  48. * @param {number} id
  49. * @param {boolean} progress
  50. * @return {Promise<any>}
  51. */
  52. private getItem(url: string, id: number, progress: boolean = true): Promise<any>{
  53. const config:AxiosRequestConfig = {
  54. url: `${url}/${id}`,
  55. method: HTTP_METHOD.GET,
  56. progress: progress
  57. }
  58. return this.request(config)
  59. }
  60. /**
  61. * Get collection : préparation de la config pour la récupération d'une collection d'items
  62. * @param {string} url
  63. * @param {boolean} progress
  64. * @return {Promise<any>}
  65. */
  66. private getCollection(url: string, progress: boolean = true): Promise<any>{
  67. const config:AxiosRequestConfig = {
  68. url: `${url}`,
  69. method: HTTP_METHOD.GET,
  70. progress: progress
  71. }
  72. return this.request(config)
  73. }
  74. /**
  75. * Put : préparation de la config pour la mise à jour d'un item
  76. * @param {string} url
  77. * @param {number} id
  78. * @param {AnyJson} data
  79. * @param {boolean} progress
  80. * @return {Promise<any>}
  81. */
  82. private put(url: string, id: number, data: AnyJson, progress: boolean = true): Promise<any>{
  83. const config:AxiosRequestConfig = {
  84. url: `${url}/${id}`,
  85. method: HTTP_METHOD.PUT,
  86. data: data,
  87. progress: progress
  88. }
  89. return this.request(config)
  90. }
  91. /**
  92. * DELETE Item : préparation de la config pour la suppression d'un item
  93. * @param {string} url
  94. * @param {number} id
  95. * @param {boolean} progress
  96. * @return {Promise<any>}
  97. */
  98. private deleteItem(url: string, id: number, progress: boolean = true): Promise<any>{
  99. const config:AxiosRequestConfig = {
  100. url: `${url}/${id}`,
  101. method: HTTP_METHOD.DELETE,
  102. progress: progress
  103. }
  104. return this.request(config)
  105. }
  106. /**
  107. * Exécution de la requete
  108. * @param {AxiosRequestConfig} config
  109. * @return {Promise<any>}
  110. */
  111. private async request(config:AxiosRequestConfig): Promise<any>{
  112. return await Connection.connector.$request(config)
  113. }
  114. /**
  115. * Test si l'argument est bien de type DataPersister
  116. * @param args
  117. */
  118. private isArgsIsDataPersisterArgs(args:DataProviderArgs|DataPersisterArgs): args is DataPersisterArgs{
  119. return (args as DataPersisterArgs).data !== undefined
  120. }
  121. }
  122. export default Connection