| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- import {UrlArgs} from "~/types/interfaces";
- import {QUERY_TYPE} from "~/types/enums";
- import {Context} from "@nuxt/types/app";
- import {Model} from "@vuex-orm/core";
- class ConstructUrl{
- static ROOT = '/api/'
- private ctx !: Context
- constructor(ctx: Context) {
- this.ctx = ctx
- }
- invoke(args:UrlArgs): string{
- switch (args.type) {
- case QUERY_TYPE.DEFAULT:
- return this.getDefaultUrl(args.url)
- case QUERY_TYPE.ENUM:
- return this.getEnumUrl(args.enumType)
- case QUERY_TYPE.MODEL:
- return this.getModelUrl(args.model, args.root_model, args.root_id)
- default:
- throw new Error('url, model or enum must be defined');
- }
- }
- private getDefaultUrl(url?: string){
- if(typeof url === 'undefined')
- throw new Error('url must be defined');
- return String(ConstructUrl.ROOT + url).toString()
- }
- private getEnumUrl(enumType?: string){
- if(typeof enumType === 'undefined')
- throw new Error('enumType must be defined');
- return String(ConstructUrl.ROOT + 'enum/' + enumType).toString()
- }
- private getModelUrl(model ?: typeof Model, rootModel ?: typeof Model, rootId ?: number){
- if(typeof model === 'undefined')
- throw new Error('model must be defined');
- const repository = this.ctx.store.$repo(model);
- const entity = repository.getModel().$entity();
- if(typeof rootModel !== 'undefined'){
- if(typeof rootId === 'undefined')
- throw new Error('Root ID must be defined');
- const rootUrl = this.getModelUrl(rootModel) as string
- return String(`${rootUrl}/${rootId}/${entity}`).toString()
- }
- return String(ConstructUrl.ROOT + entity).toString()
- }
- }
- export default ConstructUrl
|