| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- import {Model} from '@vuex-orm/core'
- import {FileArgs, ImageArgs, ListArgs, UrlArgs} from '~/types/interfaces'
- import {QUERY_TYPE} from '~/types/enums'
- import {repositoryHelper} from '~/services/store/repository'
- import TypesTesting from "~/services/utils/typesTesting";
- import UrlOptionsBuilder from "~/services/connection/urlOptionsBuilder";
- /**
- * Classe permettant de construire une URL pour l'interrogation d'une API externe
- */
- class UrlBuilder {
- static ROOT = '/api/'
- /**
- * Main méthode qui appellera les méthode privées correspondantes (getEnumUrl, getModelUrl, getImageUrl)
- * @param {UrlArgs} args
- * @return {string}
- */
- public static build (args: UrlArgs): string {
- let url: string = ''
- switch (args.type) {
- case QUERY_TYPE.DEFAULT:
- url = UrlBuilder.getDefaultUrl( args.url, args.baseUrl)
- break;
- case QUERY_TYPE.ENUM:
- url = UrlBuilder.getEnumUrl(args.enumType)
- break;
- case QUERY_TYPE.MODEL:
- url = UrlBuilder.getModelUrl(args.model, args.rootModel, args.rootId)
- break;
- case QUERY_TYPE.IMAGE:
- if (!TypesTesting.isDataProviderArgs(args)) {
- throw new Error('*args* is not a dataProviderArgs')
- }
- if (!args.imgArgs) {
- throw new Error('*args* has no imgArgs')
- }
- url = UrlBuilder.getImageUrl(args.imgArgs, args.baseUrl)
- break;
- case QUERY_TYPE.FILE:
- if (!TypesTesting.isDataProviderArgs(args)) {
- throw new Error('*args* is not a dataProviderArgs')
- }
- if (!args.fileArgs) {
- throw new Error('*args* has no fileArgs')
- }
- url = UrlBuilder.getFileUrl(args.fileArgs, args.baseUrl)
- break;
- default:
- throw new Error('url, model, image or enum must be defined')
- break;
- }
- const options = UrlBuilder.buildOptions(args)
- return options.length > 0 ? `${url}?${UrlBuilder.buildOptions(args).join('&')}` : url
- }
- /**
- * Construction d'une URL qui ira concaténer la base URL avec l'url
- * @param url
- * @param baseUrl
- * @private
- */
- private static getDefaultUrl (url?: string, baseUrl: string|null = null): string {
- if (!url) {
- throw new Error('no url')
- }
- return baseUrl ? UrlBuilder.concat(baseUrl, url) : url
- }
- /**
- * Construction d'une URL Type Enum qui ira concaténer le type enum passé en paramètre avec la ROOT Url définie
- * @param {string} enumType
- * @return {string}
- */
- private static getEnumUrl (enumType?: string): string {
- if (typeof enumType === 'undefined') {
- throw new Error('enumType must be defined')
- }
- return UrlBuilder.concat(UrlBuilder.ROOT, 'enum', enumType)
- }
- /**
- * Construction d'une URL Type Model qui ira concaténer le nom de l'entité du model passé en paramètre
- * avec la ROOT Url définie (possibilité de récursivité si le root model est défini)
- *
- * @param {Model} model roles à tester
- * @param {Model} rootModel roles à tester
- * @param {number} rootId roles à tester
- * @return {string}
- */
- private static getModelUrl (model?: typeof Model, rootModel?: typeof Model, rootId?: number): string {
- if (typeof model === 'undefined') {
- throw new Error('model must be defined')
- }
- const entity = repositoryHelper.getEntity(model)
- if (typeof rootModel !== 'undefined') {
- if (typeof rootId === 'undefined') {
- throw new Error('Root ID must be defined')
- }
- const rootUrl = UrlBuilder.getModelUrl(rootModel) as string
- return String(`${rootUrl}/${rootId}/${entity}`).toString()
- }
- return UrlBuilder.concat(UrlBuilder.ROOT, entity)
- }
- /**
- * Construction d'une URL "image" qui ira concaténer l'id de l'image à downloeader passé en paramètre avec la ROOT Url définie
- * @param {ImageArgs} imgArgs
- * @param {string} baseUrl
- * @return {string}
- */
- private static getImageUrl (imgArgs: ImageArgs, baseUrl: string = ''): string {
- const downloadUrl = `files/${imgArgs.id}/download/${imgArgs.height}x${imgArgs.width}`
- return UrlBuilder.concat(baseUrl, UrlBuilder.ROOT, downloadUrl)
- }
- /**
- * Construction d'une URL qui ira concaténer la base URL avec le Root et l'uri files
- * @param args
- * @param baseUrl
- * @private
- */
- private static getFileUrl (args: FileArgs, baseUrl: string = ''): string {
- return UrlBuilder.concat(baseUrl, UrlBuilder.ROOT, `download/${args.fileId}`)
- }
- /**
- * 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
- }
- /**
- * Prepend the 'https://' part if neither 'http://' of 'https://' is present, else: does nothing
- *
- * @param url
- */
- public static prependHttps (url: string): string {
- if (!url.match(/^https?:\/\/.*/)) {
- url = 'https://' + url;
- }
- return url;
- }
- /**
- * Main méthode qui appellera les méthode privées correspondantes (getUrlOptionsImage, getUrlOptionsLists)
- * @param {UrlArgs} args
- * @return {string}
- */
- public static buildOptions(args: UrlArgs): Array<string> {
- let options: Array<string> = []
- if (args.type === QUERY_TYPE.IMAGE){
- options = [...options, this.getUrlOptionsImage()]
- }
- if (TypesTesting.isDataProviderArgs(args) && args.listArgs !== undefined) {
- options = [...options, ...this.getUrlOptionsLists(args.listArgs)]
- }
- return options
- }
- /**
- * Une image doit toujours avoir le time en options pour éviter les problème de cache
- * @private
- */
- private static getUrlOptionsImage(): string {
- return new Date().getTime().toString()
- }
- /**
- * Fonction renvoyant le tableau d'options d'une list
- * @param listArgs
- * @private
- */
- private static getUrlOptionsLists(listArgs: ListArgs): Array<string> {
- const options: Array<string> = []
- if (listArgs.itemsPerPage) {
- options.push(`itemsPerPage=${listArgs.itemsPerPage}`)
- }
- if (listArgs.page) {
- options.push(`page=${listArgs.page}`)
- }
- if (listArgs.filters) {
- for(const filter of listArgs.filters){
- options.push(`${filter.key}=${filter.value}`)
- }
- }
- return options
- }
- }
- export default UrlBuilder
|