| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- import {FileArgs, ImageArgs, ListArgs, UrlArgs} from '~/types/interfaces'
- import {QUERY_TYPE} from '~/types/enums'
- import TypesTesting from "~/services/utils/typesTesting";
- /**
- * Classe permettant de construire une URL pour l'interrogation d'une API externe
- */
- class UrlBuilder {
- static ROOT = '/api/'
- /**
- * Concatenate a base url and a tail
- * @param base
- * @param tails
- * @private
- */
- public static join (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;
- }
- /**
- * 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.join(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.join(baseUrl, UrlBuilder.ROOT, `download/${args.fileId}`)
- }
- /**
- * 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
|