urlBuilder.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { Model } from '@vuex-orm/core'
  2. import { UrlArgs } from '~/types/interfaces'
  3. import { QUERY_TYPE } from '~/types/enums'
  4. import { repositoryHelper } from '~/services/store/repository'
  5. /**
  6. * @category Services/connection
  7. * @class UrlBuilder
  8. *
  9. * Classe permettant de construire une URL pour l'interrogation d'une API externe
  10. */
  11. class UrlBuilder {
  12. static ROOT = '/api/'
  13. /**
  14. * Main méthode qui appellera les méthode privées correspondantes (getDefaultUrl, getEnumUrl, getModelUrl)
  15. * @param {UrlArgs} args
  16. * @return {string}
  17. */
  18. public static build (args: UrlArgs): string {
  19. switch (args.type) {
  20. case QUERY_TYPE.DEFAULT:
  21. return UrlBuilder.getDefaultUrl(args.url)
  22. case QUERY_TYPE.ENUM:
  23. return UrlBuilder.getEnumUrl(args.enumType)
  24. case QUERY_TYPE.MODEL:
  25. return UrlBuilder.getModelUrl(args.model, args.rootModel, args.rootId)
  26. default:
  27. throw new Error('url, model or enum must be defined')
  28. }
  29. }
  30. /**
  31. * Construction d'une URL "traditionnelle" qui ira concaténer l'url passée en paramètre avec la ROOT Url définie
  32. * @param {string} url
  33. * @return {string}
  34. */
  35. private static getDefaultUrl (url?: string): string {
  36. if (typeof url === 'undefined') {
  37. throw new TypeError('url must be defined')
  38. }
  39. return String(UrlBuilder.ROOT + url).toString()
  40. }
  41. /**
  42. * Construction d'une URL Type Enum qui ira concaténer le type enum passé en paramètre avec la ROOT Url définie
  43. * @param {string} enumType
  44. * @return {string}
  45. */
  46. private static getEnumUrl (enumType?: string): string {
  47. if (typeof enumType === 'undefined') {
  48. throw new TypeError('enumType must be defined')
  49. }
  50. return String(UrlBuilder.ROOT + 'enum/' + enumType).toString()
  51. }
  52. /**
  53. * Construction d'une URL Type Model qui ira concaténer le nom de l'entité du model passé en paramètre
  54. * avec la ROOT Url définie (possibilité de récursivité si le root model est défini)
  55. *
  56. * @param {Model} model roles à tester
  57. * @param {Model} rootModel roles à tester
  58. * @param {number} rootId roles à tester
  59. * @return {string}
  60. */
  61. private static getModelUrl (model?: typeof Model, rootModel?: typeof Model, rootId?: number): string {
  62. if (typeof model === 'undefined') {
  63. throw new TypeError('model must be defined')
  64. }
  65. const entity = repositoryHelper.getEntity(model)
  66. if (typeof rootModel !== 'undefined') {
  67. if (typeof rootId === 'undefined') {
  68. throw new TypeError('Root ID must be defined')
  69. }
  70. const rootUrl = UrlBuilder.getModelUrl(rootModel) as string
  71. return String(`${rootUrl}/${rootId}/${entity}`).toString()
  72. }
  73. return String(UrlBuilder.ROOT + entity).toString()
  74. }
  75. }
  76. export default UrlBuilder