lenses.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { HoppRESTResponse } from "../types/HoppRESTResponse"
  2. import jsonLens from "./jsonLens"
  3. import rawLens from "./rawLens"
  4. import imageLens from "./imageLens"
  5. import htmlLens from "./htmlLens"
  6. import xmlLens from "./xmlLens"
  7. import pdfLens from "./pdfLens"
  8. export type Lens = {
  9. lensName: string
  10. isSupportedContentType: (contentType: string) => boolean
  11. renderer: string
  12. rendererImport: () => Promise<typeof import("*.vue")>
  13. }
  14. export const lenses: Lens[] = [
  15. jsonLens,
  16. imageLens,
  17. htmlLens,
  18. xmlLens,
  19. pdfLens,
  20. rawLens,
  21. ]
  22. export function getSuitableLenses(response: HoppRESTResponse): Lens[] {
  23. // return empty array if response is loading or error
  24. if (
  25. response.type === "loading" ||
  26. response.type === "network_fail" ||
  27. response.type === "script_fail" ||
  28. response.type === "fail"
  29. )
  30. return []
  31. const contentType = response.headers.find((h) => h.key === "content-type")
  32. if (!contentType) return [rawLens]
  33. const result = []
  34. for (const lens of lenses) {
  35. if (lens.isSupportedContentType(contentType.value)) result.push(lens)
  36. }
  37. return result
  38. }
  39. type LensRenderers = {
  40. [key: string]: Lens["rendererImport"]
  41. }
  42. export function getLensRenderers(): LensRenderers {
  43. const response: LensRenderers = {}
  44. for (const lens of lenses) {
  45. response[lens.renderer] = lens.rendererImport
  46. }
  47. return response
  48. }