Initial lens implementation
This commit is contained in:
7
helpers/lenses/htmlLens.js
Normal file
7
helpers/lenses/htmlLens.js
Normal file
@@ -0,0 +1,7 @@
|
||||
const htmlLens = {
|
||||
lensName: "HTML",
|
||||
supportedContentTypes: ["text/html"],
|
||||
renderer: "htmlres",
|
||||
}
|
||||
|
||||
export default htmlLens
|
||||
12
helpers/lenses/imageLens.js
Normal file
12
helpers/lenses/imageLens.js
Normal file
@@ -0,0 +1,12 @@
|
||||
const imageLens = {
|
||||
lensName: "Image",
|
||||
supportedContentTypes: [
|
||||
"image/gif",
|
||||
"image/jpeg",
|
||||
"image/png",
|
||||
// TODO : Add more image types!
|
||||
],
|
||||
renderer: "imageres",
|
||||
}
|
||||
|
||||
export default imageLens
|
||||
7
helpers/lenses/jsonLens.js
Normal file
7
helpers/lenses/jsonLens.js
Normal file
@@ -0,0 +1,7 @@
|
||||
const jsonLens = {
|
||||
lensName: "JSON",
|
||||
supportedContentTypes: ["application/json", "application/hal+json", "application/vnd.api+json"],
|
||||
renderer: "json",
|
||||
}
|
||||
|
||||
export default jsonLens
|
||||
37
helpers/lenses/lenses.js
Normal file
37
helpers/lenses/lenses.js
Normal file
@@ -0,0 +1,37 @@
|
||||
import jsonLens from "./jsonLens"
|
||||
import rawLens from "./rawLens"
|
||||
import imageLens from "./imageLens"
|
||||
import htmlLens from "./htmlLens"
|
||||
|
||||
const lenses = [
|
||||
jsonLens,
|
||||
imageLens,
|
||||
htmlLens,
|
||||
|
||||
// Keep Raw Lens as the last option
|
||||
rawLens,
|
||||
]
|
||||
|
||||
function getSuitableLenses(response) {
|
||||
const result = []
|
||||
|
||||
if (response && response.headers && response.headers["content-type"]) {
|
||||
const properContentType = response.headers["content-type"].split(";")[0]
|
||||
|
||||
for (const lens of lenses) {
|
||||
if (
|
||||
lens.supportedContentTypes === null ||
|
||||
lens.supportedContentTypes.includes(properContentType)
|
||||
) {
|
||||
result.push(lens)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// We don't know the content type, so lets just add rawLens
|
||||
result.push(rawLens)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
export default getSuitableLenses
|
||||
7
helpers/lenses/rawLens.js
Normal file
7
helpers/lenses/rawLens.js
Normal file
@@ -0,0 +1,7 @@
|
||||
const rawLens = {
|
||||
lensName: "Raw",
|
||||
supportedContentTypes: null,
|
||||
renderer: "raw",
|
||||
}
|
||||
|
||||
export default rawLens
|
||||
@@ -1,4 +1,5 @@
|
||||
import axios from "axios"
|
||||
import { isJSONContentType } from "../utils/contenttypes"
|
||||
|
||||
let cancelSource = axios.CancelToken.source()
|
||||
|
||||
@@ -34,28 +35,9 @@ const axiosWithoutProxy = async (req, _store) => {
|
||||
const res = await axios({
|
||||
...req,
|
||||
cancelToken: cancelSource.token,
|
||||
transformResponse: [
|
||||
(data, headers) => {
|
||||
// If the response has a JSON content type, try parsing it
|
||||
if (
|
||||
headers["content-type"] &&
|
||||
(headers["content-type"].startsWith("application/json") ||
|
||||
headers["content-type"].startsWith("application/vnd.api+json") ||
|
||||
headers["content-type"].startsWith("application/hal+json"))
|
||||
) {
|
||||
try {
|
||||
const jsonData = JSON.parse(data)
|
||||
return jsonData
|
||||
} catch (e) {
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
// Else return the string itself without any transformations
|
||||
return data
|
||||
},
|
||||
],
|
||||
responseType: "arraybuffer",
|
||||
})
|
||||
|
||||
return res
|
||||
} catch (e) {
|
||||
if (axios.isCancel(e)) {
|
||||
|
||||
Reference in New Issue
Block a user