Dynamically import lens renderers

This commit is contained in:
Andrew Bastin
2020-06-30 16:11:21 -04:00
parent 94b61e9f7c
commit fe1353b5c7
7 changed files with 17 additions and 11 deletions

View File

@@ -22,19 +22,14 @@
</template> </template>
<script> <script>
import getSuitableLenses from "~/helpers/lenses/lenses" import { getSuitableLenses, getLensRenderers } from "~/helpers/lenses/lenses"
export default { export default {
components: { components: {
tabs: () => import("../ui/tabs"), tabs: () => import("../ui/tabs"),
tab: () => import("../ui/tab"), tab: () => import("../ui/tab"),
// Lens Renderers // Lens Renderers
raw: () => import("./renderers/RawLensRenderer"), ...getLensRenderers(),
json: () => import("./renderers/JSONLensRenderer"),
imageres: () => import("./renderers/ImageLensRenderer"),
htmlres: () => import("./renderers/HTMLLensRenderer"),
xmlres: () => import("./renderers/XMLLensRenderer"),
headers: () => import("./headers"),
}, },
props: { props: {
response: {}, response: {},

View File

@@ -2,6 +2,7 @@ const htmlLens = {
lensName: "HTML", lensName: "HTML",
supportedContentTypes: ["text/html"], supportedContentTypes: ["text/html"],
renderer: "htmlres", renderer: "htmlres",
rendererImport: () => import("~/components/lenses/renderers/HTMLLensRenderer"),
} }
export default htmlLens export default htmlLens

View File

@@ -10,6 +10,7 @@ const imageLens = {
"image/vnd.microsoft.icon", "image/vnd.microsoft.icon",
], ],
renderer: "imageres", renderer: "imageres",
rendererImport: () => import("~/components/lenses/renderers/ImageLensRenderer"),
} }
export default imageLens export default imageLens

View File

@@ -2,6 +2,7 @@ const jsonLens = {
lensName: "JSON", lensName: "JSON",
supportedContentTypes: ["application/json", "application/hal+json", "application/vnd.api+json"], supportedContentTypes: ["application/json", "application/hal+json", "application/vnd.api+json"],
renderer: "json", renderer: "json",
rendererImport: () => import("~/components/lenses/renderers/JSONLensRenderer"),
} }
export default jsonLens export default jsonLens

View File

@@ -6,7 +6,7 @@ import xmlLens from "./xmlLens"
const lenses = [jsonLens, imageLens, htmlLens, xmlLens, rawLens] const lenses = [jsonLens, imageLens, htmlLens, xmlLens, rawLens]
function getSuitableLenses(response) { export function getSuitableLenses(response) {
const result = [] const result = []
if (response && response.headers && response.headers["content-type"]) { if (response && response.headers && response.headers["content-type"]) {
@@ -28,4 +28,10 @@ function getSuitableLenses(response) {
return result return result
} }
export default getSuitableLenses export function getLensRenderers() {
const response = {}
for (const lens of lenses) {
response[lens.renderer] = lens.rendererImport
}
return response
}

View File

@@ -2,6 +2,7 @@ const rawLens = {
lensName: "Raw", lensName: "Raw",
supportedContentTypes: null, supportedContentTypes: null,
renderer: "raw", renderer: "raw",
rendererImport: () => import("~/components/lenses/renderers/RawLensRenderer"),
} }
export default rawLens export default rawLens

View File

@@ -1,7 +1,8 @@
const htmlLens = { const xmlLens = {
lensName: "XML", lensName: "XML",
supportedContentTypes: ["application/xml", "image/svg+xml", "text/xml", "application/rss+xml"], supportedContentTypes: ["application/xml", "image/svg+xml", "text/xml", "application/rss+xml"],
renderer: "xmlres", renderer: "xmlres",
rendererImport: () => import("~/components/lenses/renderers/XMLLensRenderer"),
} }
export default htmlLens export default xmlLens