baseProvider.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { Context } from '@nuxt/types/app'
  2. import { hooks } from '~/services/dataProvider/provider/hook/_import'
  3. import { DataProviderArgs } from '~/types/interfaces'
  4. class BaseProvider {
  5. protected arguments!: DataProviderArgs;
  6. protected ctx!: Context;
  7. constructor (ctx: Context, args: DataProviderArgs) {
  8. this.arguments = args
  9. this.ctx = ctx
  10. }
  11. /**
  12. * Is the given argument a supported model
  13. * @param _args
  14. */
  15. static support (_args: DataProviderArgs): boolean {
  16. throw new Error('Need to be implement into static method')
  17. }
  18. /**
  19. * Iterate over the available hooks and return
  20. * the first one that support the given args
  21. */
  22. async postHook () {
  23. for (const Hook of BaseProvider.sortedHooks()) {
  24. if (Hook.support(this.arguments)) {
  25. await new Hook().invoke(this.arguments)
  26. }
  27. }
  28. }
  29. /**
  30. * Sort the available hooks by priority
  31. * @private
  32. */
  33. public static sortedHooks (): Iterable<any> {
  34. return hooks.sort(function (a, b) {
  35. if (a.priority > b.priority) { return 1 }
  36. if (a.priority < b.priority) { return -1 }
  37. return 0
  38. })
  39. }
  40. }
  41. export default BaseProvider