import { Context } from '@nuxt/types/app' import { hooks } from '~/services/dataProvider/provider/hook/_import' import { DataProviderArgs } from '~/types/interfaces' class BaseProvider { protected arguments!: DataProviderArgs; protected ctx!: Context; constructor (ctx: Context, args: DataProviderArgs) { this.arguments = args this.ctx = ctx } /** * Is the given argument a supported model * @param _args */ static support (_args: DataProviderArgs): boolean { throw new Error('Need to be implement into static method') } /** * Iterate over the available hooks and return * the first one that support the given args */ async postHook () { for (const Hook of BaseProvider.sortedHooks()) { if (Hook.support(this.arguments)) { await new Hook().invoke(this.arguments) } } } /** * Sort the available hooks by priority * @private */ public static sortedHooks (): Iterable { return hooks.sort(function (a, b) { if (a.priority > b.priority) { return 1 } if (a.priority < b.priority) { return -1 } return 0 }) } } export default BaseProvider