@hyperjump/browser
Advanced tools
Comparing version 0.11.0 to 0.12.0
47
index.js
@@ -1,45 +0,6 @@ | ||
const makeFetchHappen = require("make-fetch-happen"); | ||
const resolveUrl = require("url").resolve; | ||
const Hyperjump = require("./core"); | ||
require("./json"); | ||
require("./json-reference"); | ||
const construct = (url, headers, body) => Object.freeze({ url, headers, body }); | ||
const extend = (doc, extras) => Object.freeze({ ...doc, ...extras }); | ||
const nil = construct("", {}, undefined); | ||
const get = async (url, doc = nil, options = {}) => { | ||
let result; | ||
const resolvedUrl = resolveUrl(doc.url, url); | ||
if (uriReference(doc.url) === uriReference(resolvedUrl)) { | ||
result = extend(doc, { url: resolvedUrl }); | ||
} else if (doc.embedded && uriReference(resolvedUrl) in doc.embedded) { | ||
const headers = { "content-type": doc.headers["content-type"] }; | ||
result = construct(resolvedUrl, headers, doc.embedded[resolvedUrl]); | ||
} else { | ||
const response = await fetch(resolvedUrl, options); | ||
const headers = {}; | ||
for (const [name, value] of response.headers.entries()) { | ||
headers[name] = value; | ||
} | ||
result = construct(resolvedUrl, headers, await response.text()); | ||
} | ||
return await contentTypeHandler(result)(result, options); | ||
}; | ||
const source = (doc = nil) => doc.body; | ||
const addContentType = (contentType, handler) => contentTypes[contentType] = handler; | ||
const fetch = makeFetchHappen.defaults({ cacheManager: "./http-cache" }); | ||
const contentTypes = {}; | ||
const contentTypeHandler = (doc) => { | ||
const contentType = doc.headers["content-type"]; | ||
return contentType in contentTypes ? contentTypes[contentType] : async (doc) => doc; | ||
}; | ||
const uriReference = (url) => url.split("#", 1)[0]; | ||
module.exports = { construct, extend, nil, get, source, addContentType }; | ||
module.exports = Hyperjump; |
@@ -1,100 +0,7 @@ | ||
const JsonPointer = require("@hyperjump/json-pointer"); | ||
const curry = require("just-curry-it"); | ||
const Hyperjump = require(".."); | ||
const Hyperjump = require("../core"); | ||
const Core = require("./core"); | ||
const contentType = "application/reference+json"; | ||
const contentTypeHandler = async (doc, options) => { | ||
const jrefDoc = (!("jref" in doc)) ? Hyperjump.extend(doc, parse(doc)) : doc; | ||
const docValue = value(jrefDoc); | ||
return isRef(docValue) ? await get(docValue["$href"], jrefDoc, options) : jrefDoc; | ||
}; | ||
Hyperjump.addContentType("application/reference+json", Core); | ||
const parse = (doc) => { | ||
const embedded = {}; | ||
const jref = JSON.parse(Hyperjump.source(doc), (key, value) => { | ||
if (isId(value)) { | ||
const id = uriReference(value["$embedded"]); | ||
delete value["$embedded"]; | ||
embedded[id] = JSON.stringify(value); | ||
return { "$href": id }; | ||
} else { | ||
return value; | ||
} | ||
}); | ||
return { jref, embedded }; | ||
}; | ||
Hyperjump.addContentType(contentType, contentTypeHandler); | ||
const nil = Hyperjump.nil; | ||
const source = Hyperjump.source; | ||
const get = curry(async (url, doc, options = {}) => { | ||
const defaultHeaders = { "Accept": "application/reference+json" }; | ||
options.headers = { ...defaultHeaders, ...options.headers }; | ||
return await Hyperjump.get(url, doc, options); | ||
}); | ||
const value = (subject) => { | ||
if (isDocument(subject)) { | ||
const doc = subject; | ||
return JsonPointer.get(pointer(doc), doc.jref); | ||
} else { | ||
return subject; | ||
} | ||
}; | ||
const pointer = (doc) => decodeURIComponent(uriFragment(doc.url)); | ||
const entries = (subject, options = {}) => { | ||
if (isDocument(subject)) { | ||
return Promise.all(Object.keys(value(subject)) | ||
.map(async (key) => { | ||
const url = append(key, subject); | ||
return [key, await get(url, subject, options)]; | ||
})); | ||
} else { | ||
return Object.entries(subject); | ||
} | ||
}; | ||
const map = curry(async (fn, subject, options = {}) => { | ||
const list = (await entries(subject, options)) | ||
.map(([_key, item]) => fn(item)); | ||
return Promise.all(list); | ||
}); | ||
const filter = curry(async (fn, subject, options = {}) => { | ||
return reduce(async (acc, item) => { | ||
return (await fn(item)) ? acc.concat(item) : acc; | ||
}, [], subject, options); | ||
}); | ||
const reduce = curry(async (fn, acc, subject, options = {}) => { | ||
return (await entries(subject, options)) | ||
.reduce(async (acc, [_key, item]) => fn(await acc, item), acc); | ||
}); | ||
const pipeline = curry((fns, subject) => { | ||
return fns.reduce(async (acc, fn) => fn(await acc), subject); | ||
}); | ||
const uriFragment = (url) => url.split("#", 2)[1] || ""; | ||
const uriReference = (url) => url.split("#", 1)[0]; | ||
const isObject = (value) => typeof value === "object" && !Array.isArray(value) && value !== null; | ||
const isRef = (value) => isObject(value) && "$href" in value; | ||
const isId = (value) => isObject(value) && "$embedded" in value; | ||
const isDocument = (value) => isObject(value) && "url" in value; | ||
const append = (key, doc) => { | ||
const ptr = JsonPointer.append(key, pointer(doc)); | ||
return "#" + encodeURI(ptr).replace(/#/g, "%23"); | ||
}; | ||
module.exports = { | ||
contentType, contentTypeHandler, | ||
nil, source, get, | ||
value, pointer, entries, map, filter, reduce, pipeline | ||
}; | ||
module.exports = Hyperjump; |
@@ -1,19 +0,7 @@ | ||
const Hyperjump = require(".."); | ||
const Hyperjump = require("../core"); | ||
const Core = require("./core"); | ||
const contentType = "application/json"; | ||
const contentTypeHandler = async (doc) => doc; | ||
Hyperjump.addContentType(contentType, contentTypeHandler); | ||
Hyperjump.addContentType("application/json", Core); | ||
const nil = Hyperjump.nil; | ||
const source = Hyperjump.source; | ||
const get = async (url, doc = nil, options = {}) => { | ||
const defaultHeaders = { "Accept": "application/json" }; | ||
options.headers = { ...defaultHeaders, ...options.headers }; | ||
return Hyperjump.get(url, doc, options); | ||
}; | ||
const value = (doc = nil) => JSON.parse(doc.body); | ||
module.exports = { contentType, contentTypeHandler, get, nil, source, value }; | ||
module.exports = Hyperjump; |
{ | ||
"name": "@hyperjump/browser", | ||
"version": "0.11.0", | ||
"version": "0.12.0", | ||
"description": "A generic hypermedia client for JSON Reference", | ||
@@ -5,0 +5,0 @@ "scripts": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9
13995
2