refactor: typescript support

This commit is contained in:
liyasthomas
2021-09-15 17:30:04 +05:30
parent 96bcbc80f8
commit 4e8a4e8914
21 changed files with 98 additions and 176 deletions

View File

@@ -1,10 +1,12 @@
const htmlLens = {
import { Lens } from "./lenses"
const htmlLens: Lens = {
lensName: "response.html",
isSupportedContentType: (contentType) =>
/\btext\/html|application\/xhtml\+xml\b/i.test(contentType),
renderer: "htmlres",
rendererImport: () =>
import("~/components/lenses/renderers/HTMLLensRenderer"),
import("~/components/lenses/renderers/HTMLLensRenderer.vue"),
}
export default htmlLens

View File

@@ -1,4 +1,6 @@
const imageLens = {
import { Lens } from "./lenses"
const imageLens: Lens = {
lensName: "response.image",
isSupportedContentType: (contentType) =>
/\bimage\/(?:gif|jpeg|png|bmp|svg\+xml|x-icon|vnd\.microsoft\.icon)\b/i.test(
@@ -6,7 +8,7 @@ const imageLens = {
),
renderer: "imageres",
rendererImport: () =>
import("~/components/lenses/renderers/ImageLensRenderer"),
import("~/components/lenses/renderers/ImageLensRenderer.vue"),
}
export default imageLens

View File

@@ -1,11 +1,12 @@
import { isJSONContentType } from "../utils/contenttypes"
import { Lens } from "./lenses"
const jsonLens = {
const jsonLens: Lens = {
lensName: "response.json",
isSupportedContentType: isJSONContentType,
renderer: "json",
rendererImport: () =>
import("~/components/lenses/renderers/JSONLensRenderer"),
import("~/components/lenses/renderers/JSONLensRenderer.vue"),
}
export default jsonLens

View File

@@ -1,28 +0,0 @@
import jsonLens from "./jsonLens"
import rawLens from "./rawLens"
import imageLens from "./imageLens"
import htmlLens from "./htmlLens"
import xmlLens from "./xmlLens"
export const lenses = [jsonLens, imageLens, htmlLens, xmlLens, rawLens]
export function getSuitableLenses(response) {
const contentType = response.headers.find((h) => h.key === "content-type")
if (!contentType) return [rawLens]
const result = []
for (const lens of lenses) {
if (lens.isSupportedContentType(contentType.value)) result.push(lens)
}
return result
}
export function getLensRenderers() {
const response = {}
for (const lens of lenses) {
response[lens.renderer] = lens.rendererImport
}
return response
}

42
helpers/lenses/lenses.ts Normal file
View File

@@ -0,0 +1,42 @@
import { HoppRESTResponse } from "../types/HoppRESTResponse"
import jsonLens from "./jsonLens"
import rawLens from "./rawLens"
import imageLens from "./imageLens"
import htmlLens from "./htmlLens"
import xmlLens from "./xmlLens"
export type Lens = {
lensName: string
isSupportedContentType: (contentType: string) => boolean
renderer: string
rendererImport: () => Promise<typeof import("*.vue")>
}
export const lenses: Lens[] = [jsonLens, imageLens, htmlLens, xmlLens, rawLens]
export function getSuitableLenses(response: HoppRESTResponse): Lens[] {
// return empty array if response is loading or error
if (response.type === "loading" || response.type === "network_fail") return []
const contentType = response.headers.find((h) => h.key === "content-type")
if (!contentType) return [rawLens]
const result = []
for (const lens of lenses) {
if (lens.isSupportedContentType(contentType.value)) result.push(lens)
}
return result
}
type LensRenderers = {
[key: string]: Lens["rendererImport"]
}
export function getLensRenderers(): LensRenderers {
const response: LensRenderers = {}
for (const lens of lenses) {
response[lens.renderer] = lens.rendererImport
}
return response
}

View File

@@ -1,8 +0,0 @@
const rawLens = {
lensName: "response.raw",
isSupportedContentType: () => true,
renderer: "raw",
rendererImport: () => import("~/components/lenses/renderers/RawLensRenderer"),
}
export default rawLens

11
helpers/lenses/rawLens.ts Normal file
View File

@@ -0,0 +1,11 @@
import { Lens } from "./lenses"
const rawLens: Lens = {
lensName: "response.raw",
isSupportedContentType: () => true,
renderer: "raw",
rendererImport: () =>
import("~/components/lenses/renderers/RawLensRenderer.vue"),
}
export default rawLens

View File

@@ -1,8 +1,11 @@
const xmlLens = {
import { Lens } from "./lenses"
const xmlLens: Lens = {
lensName: "response.xml",
isSupportedContentType: (contentType) => /\bxml\b/i.test(contentType),
renderer: "xmlres",
rendererImport: () => import("~/components/lenses/renderers/XMLLensRenderer"),
rendererImport: () =>
import("~/components/lenses/renderers/XMLLensRenderer.vue"),
}
export default xmlLens