|
|
@@ -1,9 +1,6 @@
|
|
|
-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
|
|
|
@@ -12,105 +9,29 @@ 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
|
|
|
+ * Concatenate a base url and a tail
|
|
|
+ * @param base
|
|
|
+ * @param tails
|
|
|
* @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)
|
|
|
+ public static join (base: string, ...tails: string[]): string {
|
|
|
+ let url = base
|
|
|
+ tails.forEach((tail: string) => {
|
|
|
+ url = url.replace(/^|\/$/g, '') + '/' + tail.replace(/^\/?|$/g, '')
|
|
|
+ })
|
|
|
+ return url
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 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)
|
|
|
+ * Prepend the 'https://' part if neither 'http://' of 'https://' is present, else: does nothing
|
|
|
*
|
|
|
- * @param {Model} model roles à tester
|
|
|
- * @param {Model} rootModel roles à tester
|
|
|
- * @param {number} rootId roles à tester
|
|
|
- * @return {string}
|
|
|
+ * @param url
|
|
|
*/
|
|
|
- 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()
|
|
|
+ public static prependHttps (url: string): string {
|
|
|
+ if (!url.match(/^https?:\/\/.*/)) {
|
|
|
+ url = 'https://' + url;
|
|
|
}
|
|
|
-
|
|
|
- return UrlBuilder.concat(UrlBuilder.ROOT, entity)
|
|
|
+ return url;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -121,7 +42,7 @@ class UrlBuilder {
|
|
|
*/
|
|
|
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)
|
|
|
+ return UrlBuilder.join(baseUrl, UrlBuilder.ROOT, downloadUrl)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -131,36 +52,9 @@ class UrlBuilder {
|
|
|
* @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;
|
|
|
+ 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
|