lenses.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. export type Lens = {
  8. lensName: string
  9. isSupportedContentType: (contentType: string) => boolean
  10. renderer: string
  11. rendererImport: () => Promise<typeof import("*.vue")>
  12. }
  13. export const lenses: Lens[] = [jsonLens, imageLens, htmlLens, xmlLens, rawLens]
  14. export function getSuitableLenses(response: HoppRESTResponse): Lens[] {
  15. // return empty array if response is loading or error
  16. if (response.type === "loading" || response.type === "network_fail") return []
  17. const contentType = response.headers.find((h) => h.key === "content-type")
  18. if (!contentType) return [rawLens]
  19. const result = []
  20. for (const lens of lenses) {
  21. if (lens.isSupportedContentType(contentType.value)) result.push(lens)
  22. }
  23. return result
  24. }
  25. type LensRenderers = {
  26. [key: string]: Lens["rendererImport"]
  27. }
  28. export function getLensRenderers(): LensRenderers {
  29. const response: LensRenderers = {}
  30. for (const lens of lenses) {
  31. response[lens.renderer] = lens.rendererImport
  32. }
  33. return response
  34. }