useImage.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import {AnyJson, ApiResponse} from '~/types/interfaces'
  2. import {QUERY_TYPE} from "~/types/enums";
  3. import {useContext, useFetch, Ref, ref} from '@nuxtjs/composition-api'
  4. import DataProvider from "~/services/data/dataProvider";
  5. /**
  6. * @category composables/data
  7. * @class UseImage
  8. * Use Classe qui va récupérer les Images
  9. */
  10. export class UseImage {
  11. private $dataProvider!: DataProvider
  12. private $config!: AnyJson
  13. constructor() {
  14. const {$dataProvider, $config} = useContext()
  15. this.$dataProvider = $dataProvider
  16. this.$config = $config
  17. }
  18. /**
  19. * Récupération d'une image via l'ancienne API
  20. */
  21. public getOne(id: number|undefined, imageByDefault: string = '', height: number|undefined = 0, width: number|undefined = 0): AnyJson{
  22. const imageLoaded: Ref<String> = ref('')
  23. const {fetchState, fetch} = useFetch(async () => {
  24. try{
  25. if(id){
  26. imageLoaded.value = await this.provideImg(id, height, width)
  27. }else
  28. throw new Error('id is null')
  29. }catch (e){
  30. if(imageByDefault)
  31. imageLoaded.value = require(`assets/images/byDefault/${imageByDefault}`)
  32. }
  33. }
  34. )
  35. return {
  36. fetch,
  37. fetchState,
  38. imageLoaded
  39. }
  40. }
  41. /**
  42. * retourne l'image demandée
  43. * @param id
  44. * @param height
  45. * @param width
  46. */
  47. public async provideImg(id: number, height: number = 0, width: number = 0){
  48. return await this.$dataProvider.invoke({
  49. type: QUERY_TYPE.IMAGE,
  50. baseUrl: this.$config.baseURL_Legacy,
  51. imgArgs: {
  52. id: id,
  53. height: height,
  54. width: width
  55. }
  56. })
  57. }
  58. }