import { Model } from '@vuex-orm/core' import { UrlArgs } from '~/types/interfaces' import { QUERY_TYPE } from '~/types/enums' import { repositoryHelper } from '~/services/store/repository' /** * 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 (getDefaultUrl, getEnumUrl, getModelUrl) * @param {UrlArgs} args * @return {string} */ public static build (args: UrlArgs): string { switch (args.type) { case QUERY_TYPE.DEFAULT: return UrlBuilder.getDefaultUrl(args.url) case QUERY_TYPE.ENUM: return UrlBuilder.getEnumUrl(args.enumType) case QUERY_TYPE.MODEL: return UrlBuilder.getModelUrl(args.model, args.rootModel, args.rootId) default: throw new Error('url, model or enum must be defined') } } /** * Construction d'une URL "traditionnelle" qui ira concaténer l'url passée en paramètre avec la ROOT Url définie * @param {string} url * @return {string} */ private static getDefaultUrl (url?: string): string { if (typeof url === 'undefined') { throw new TypeError('url must be defined') } return UrlBuilder.concat(UrlBuilder.ROOT, 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 TypeError('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 TypeError('model must be defined') } const entity = repositoryHelper.getEntity(model) if (typeof rootModel !== 'undefined') { if (typeof rootId === 'undefined') { throw new TypeError('Root ID must be defined') } const rootUrl = UrlBuilder.getModelUrl(rootModel) as string return String(`${rootUrl}/${rootId}/${entity}`).toString() } return UrlBuilder.concat(UrlBuilder.ROOT, entity) } /** * 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 } } export default UrlBuilder