| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import {NuxtApp} from "#app";
- import {DataManager, DataPersisterArgs, DataProviderArgs, UrlArgs} from '~/types/interfaces'
- import Connection from '~/services/connection/connection'
- import Hookable from '~/services/data/hookable'
- import { HTTP_METHOD, QUERY_TYPE } from '~/types/enums'
- /**
- * @todo : le context (ctx) on devrait pouvoir s'en passer grace aux différent composable... a voir..
- * Base class for data providers, persisters or deleters
- */
- abstract class BaseDataManager extends Hookable implements DataManager {
- protected ctx!: NuxtApp
- protected defaultArguments: object = {
- type: QUERY_TYPE.MODEL,
- showProgress: true
- }
- /**
- * Initialise le contexte (la connection en particulier)
- * @param {NuxtApp} ctx
- */
- public initCtx (ctx: NuxtApp) {
- this.ctx = ctx
- }
- /**
- * Exécute la requête et retourne la réponse désérialisée
- * @param {UrlArgs} _args
- */
- // eslint-disable-next-line require-await
- protected async _invoke (_args: UrlArgs): Promise<any> {
- throw new Error('Not implemented')
- }
- /**
- * Exécute la requête
- * @param {DataProviderArgs|DataPersisterArgs} args
- */
- public async invoke (args: DataProviderArgs|DataPersisterArgs): Promise<any> {
- const queryArguments = { ...this.defaultArguments, ...args }
- BaseDataManager.startLoading(queryArguments)
- const response = await this._invoke(queryArguments)
- await this.triggerHooks(queryArguments)
- return response
- }
- /**
- * Signale le début du chargement à nuxt (si showProgress est true)
- * @param {UrlArgs} args
- */
- private static startLoading (args: UrlArgs) {
- if (args.showProgress) {
- // const $nuxt = window.$nuxt
- // $nuxt.$loading.start()
- }
- }
- /**
- * Send the request trough the Connection
- * @param {string} url
- * @param {HTTP_METHOD} method
- * @param {UrlArgs} args
- */
- public static request (url: string, method: HTTP_METHOD, args: UrlArgs): Promise<any> {
- return Connection.invoke(method, url, args)
- }
- }
- export default BaseDataManager
|