import { Context } from '@nuxt/types/app' import { UrlArgs } from '~/types/interfaces' import Connection from '~/services/connection/connection' import Hookable from '~/services/hooks/hookable' import { HTTP_METHOD, QUERY_TYPE } from '~/types/enums' /** * Base class for data providers, persisters or deleters */ class DataManager extends Hookable { protected ctx!: Context protected arguments!: UrlArgs protected defaultArguments: object = { type: QUERY_TYPE.MODEL, showProgress: true } /** * Initialise le contexte (la connection en particulier) * @param ctx */ public initCtx (ctx: Context) { Connection.initConnector(ctx.$axios) this.ctx = ctx } /** * Exécute la requête * @param args */ public async invoke (args: UrlArgs): Promise { args = { ...this.defaultArguments, ...args } DataManager.startLoading(args) await this.triggerHooks(args) } /** * Signale le début du chargement à nuxt (si showProgress est true) * @param args */ private static startLoading (args: UrlArgs) { if (args.showProgress) { const $nuxt = window.$nuxt $nuxt.$loading.start() } } /** * Send the request trough the Connection * @param url * @param method * @param args */ protected static sendRequest (url: string, method: HTTP_METHOD, args: UrlArgs): Promise { return Connection.invoke(method, url, args) } } export default DataManager