@hyperjump/json-reference
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -6,4 +6,6 @@ "use strict"; | ||
}); | ||
exports.follow = exports.pointer = exports.value = exports.load = undefined; | ||
exports.get = exports.pointer = exports.value = exports.nil = undefined; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _jsonPointer = require("@hyperjump/json-pointer"); | ||
@@ -13,4 +15,16 @@ | ||
var _axios = require("axios"); | ||
var _axios2 = _interopRequireDefault(_axios); | ||
require("babel-polyfill"); | ||
var _url = require("url"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } | ||
var construct = function construct(url, content) { | ||
@@ -20,6 +34,3 @@ return Object.freeze({ url: url, content: content }); | ||
var load = exports.load = function load(url, content) { | ||
var doc = construct(url, JSON.parse(content)); | ||
return follow(doc, url); | ||
}; | ||
var nil = exports.nil = construct("", ""); | ||
@@ -31,11 +42,38 @@ var value = exports.value = function value(doc) { | ||
var pointer = exports.pointer = function pointer(doc) { | ||
return decodeURIComponent(doc.url.split("#", 2)[1]); | ||
return decodeURIComponent(doc.url.split("#", 2)[1] || ""); | ||
}; | ||
// TODO: Handle remote references | ||
var follow = exports.follow = function follow(doc, url) { | ||
// Assumes id is always "" | ||
var result = construct(url, doc.content); | ||
var resultValue = value(result); | ||
return resultValue["$ref"] ? follow(result, resultValue["$ref"]) : result; | ||
var get = exports.get = function () { | ||
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(url) { | ||
var doc = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : nil; | ||
var resolvedUrl, response, result, resultValue; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
resolvedUrl = (0, _url.resolve)(doc.url, url); | ||
_context.next = 3; | ||
return _axios2.default.get(resolvedUrl); | ||
case 3: | ||
response = _context.sent; | ||
result = construct(resolvedUrl, response.data); | ||
resultValue = value(result); | ||
return _context.abrupt("return", isRef(resultValue) ? get(resultValue["$ref"], result) : result); | ||
case 7: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, undefined); | ||
})); | ||
return function get(_x2) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
var isRef = function isRef(value) { | ||
return (typeof value === "undefined" ? "undefined" : _typeof(value)) === "object" && "$ref" in value; | ||
}; |
{ | ||
"name": "@hyperjump/json-reference", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "An implementation of JSON Reference", | ||
@@ -39,2 +39,3 @@ "main": "./dist/json-reference.js", | ||
"mocha-webpack": "^2.0.0-beta.0", | ||
"nock": "^9.4.2", | ||
"nyc": "^11.8.0", | ||
@@ -45,3 +46,4 @@ "webpack": "^4.14.0", | ||
"dependencies": { | ||
"@hyperjump/json-pointer": "^0.1.2" | ||
"@hyperjump/json-pointer": "^0.1.2", | ||
"axios": "^0.18.0" | ||
}, | ||
@@ -48,0 +50,0 @@ "nyc": { |
@@ -19,6 +19,12 @@ JSON Reference | ||
```javascript | ||
import * as JsonReference from "@hyperjump/json-reference"; | ||
```http | ||
GET http://json-reference.hyperjump.com/example1 HTTP/1.1 | ||
Accept: application/reference+json | ||
``` | ||
const document = JsonReference.load("#", `{ | ||
```http | ||
HTTP/1.1 200 OK | ||
Content-Type: application/reference+json | ||
{ | ||
"foo": "bar", | ||
@@ -32,5 +38,11 @@ "aaa": { | ||
} | ||
}`); | ||
} | ||
``` | ||
const aaa = JsonReference.follow(doc, "/aaa"); | ||
```javascript | ||
import * as JsonReference from "@hyperjump/json-reference"; | ||
const doc = JsonReference.get("http://json-reference.hyperjump.com/example1"); | ||
const aaa = JsonReference.get("/aaa", doc); | ||
JsonReference.value(aaa); // => "bar" | ||
@@ -37,0 +49,0 @@ JsonReference.pointer(aaa); // => "/aaa" |
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
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
8251
55
141
2
19
+ Addedaxios@^0.18.0
+ Addedaxios@0.18.1(transitive)
+ Addedfollow-redirects@1.5.10(transitive)
+ Addedis-buffer@2.0.5(transitive)