Skip to content

Commit 88b2223

Browse files
committed
Readme
1 parent db7ffb7 commit 88b2223

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed

README.md

+154
Original file line numberDiff line numberDiff line change
@@ -1 +1,155 @@
11
# Hyperjump - Browser
2+
3+
The Hyperjump Browser is a generic client for traversing JSON Reference ([JRef])
4+
and other [JRef]-compatible media types in a way that abstracts the references
5+
without loosing information.
6+
7+
Everything is built to work with the [unified](https://unifiedjs.com/) ecosystem
8+
of libraries and tools. This module defines ASTs for representing JSON and JRef
9+
documents and provides a CLI and utilities allowing you to parse, transform, and
10+
stringify documents.
11+
12+
## Install
13+
14+
This module is designed for node.js (ES Modules, TypeScript) and browsers. It
15+
should work in Bun and Deno as well, but the test runner doesn't work in these
16+
environments, so this module may be less stable in those environments.
17+
18+
### Node.js
19+
20+
```bash
21+
npm install @hyperjump/browser
22+
```
23+
24+
## JRef Browser
25+
26+
This example uses the API at
27+
[https://swapi.hyperjump.io](https://explore.hyperjump.io#https://swapi.hyperjump.io/api/films/1).
28+
It's a variation of the [Star Wars API (SWAPI)](https://swapi.dev) implemented
29+
using the [JRef] media type.
30+
31+
```javascript
32+
import { Hyperjump } from "@hyperjump/browser";
33+
34+
const h = new Hyperjump();
35+
const aNewHope = await h.get("https://swapi.hyperjump.io/api/films/1");
36+
const characters = await h.step("characters", aNewHope);
37+
38+
for await (const character of h.iter(characters)) {
39+
const name = await h.step("name", character);
40+
h.value(name); // => Luke Skywalker, etc.
41+
}
42+
```
43+
44+
You can also work with files on the file system. When working with files, media
45+
types are determined by file extensions. The [JRef] media type uses the `.jref`
46+
extension.
47+
48+
```javascript
49+
import { Hyperjump } from "@hyperjump/browser";
50+
51+
const h = new Hyperjump();
52+
const lukeSkywalker = await h.get("./api/people/1.jref"); // Paths resolve relative to the current working directory
53+
const name = await h.step("name", lukeSkywalker);
54+
h.value(name); // => Luke Skywalker
55+
```
56+
57+
## Media Types
58+
59+
Support for the JSON and [JRef] media types are included by default, but you can
60+
add support for any media type you like as long as it can be represented in a
61+
[JRef]-compatible way.
62+
63+
```javascript
64+
import { Hyperjump } from "@hyperjump/browser";
65+
66+
const h = new Hyperjump();
67+
68+
// Add support for YAML version of JRef (YRef)
69+
export class YrefMediaTypePlugin {
70+
constructor(quality) {
71+
this.quality = quality;
72+
}
73+
74+
async parse(response) {
75+
return {
76+
type: "jref-document",
77+
uri: response.url,
78+
children: [parseYamlToJrefAst(await response.text())],
79+
fragmentKind: "json-pointer"
80+
};
81+
}
82+
83+
async fileMatcher(path) {
84+
return /[^/]\.yref$/.test(path);
85+
}
86+
}
87+
h.addMediaTypePlugin("application/reference+yaml", new YrefMediaTypePlugin());
88+
89+
// Prefer "YRef" over JRef by reducing the quality for JRef.
90+
h.setMediaTypeQuality("application/reference+json", 0.9);
91+
92+
// Only support YRef by removing JRef support.
93+
h.removeMediaTypePlugin("application/reference+json");
94+
```
95+
96+
## URI Schemes
97+
98+
By default, `http(s):` and `file:` URIs are supported. You can add support for
99+
additional URI schemes using plugins.
100+
101+
```javascript
102+
import { Hyperjump } from "@hyperjump/browser";
103+
104+
const h = new Hyperjump();
105+
106+
// Add support for the `urn:` scheme
107+
class UrnSchemePlugin {
108+
async retrieve(urn, baseUri) {
109+
let { nid, nss, query, fragment } = parseUrn(urn);
110+
nid = nid.toLowerCase();
111+
112+
if (!mappings[nid]?.[nss]) {
113+
throw Error(`Not Found -- ${urn}`);
114+
}
115+
116+
let uri = mappings[nid][nss];
117+
uri += query ? "?" + query : "";
118+
uri += fragment ? "#" + fragment : "";
119+
120+
return await h.retrieve(uri, baseUri);
121+
}
122+
}
123+
h.addUriSchemePlugin("urn", new UrnSchemePlugin());
124+
125+
// Only support `urn:` by removing default plugins
126+
h.removeUriSchemePlugin("http");
127+
h.removeUriSchemePlugin("https");
128+
h.removeUriSchemePlugin("file");
129+
```
130+
131+
## JRef
132+
133+
TODO
134+
135+
## JSON
136+
137+
TODO
138+
139+
## Contributing
140+
141+
### Tests
142+
143+
Run the tests
144+
145+
```bash
146+
npm test
147+
```
148+
149+
Run the tests with a continuous test runner
150+
151+
```bash
152+
npm test -- --watch
153+
```
154+
155+
[JRef]: https://github.com/hyperjump-io/browser/blob/main/src/jref/README.md

0 commit comments

Comments
 (0)