@@ -13,6 +13,7 @@ import { mimeMatch } from "./utilities.js";
13
13
* @import { JsonCompatible, JsonType } from "../json/jsonast.js"
14
14
* @import { UriSchemePlugin } from "./uri-schemes/uri-scheme-plugin.js"
15
15
* @import { DocumentNode, MediaTypePlugin } from "./media-types/media-type-plugin.js"
16
+ * @import { JsonPointerError } from "../json/jsonast-util.js"
16
17
*/
17
18
18
19
@@ -28,6 +29,7 @@ import { mimeMatch } from "./utilities.js";
28
29
*/
29
30
30
31
export class Hyperjump {
32
+ // TODO: Add config to enable schemes and media types
31
33
#config;
32
34
33
35
/** @type Record<string, DocumentNode> */
@@ -64,7 +66,22 @@ export class Hyperjump {
64
66
this . addMediaTypePlugin ( new JsonMediaTypePlugin ( ) ) ;
65
67
}
66
68
67
- /** @type (uri: string, options?: GetOptions) => Promise<JsonCompatible<JrefNode>> */
69
+ /**
70
+ * Retrieve a document located at the given URI. URIs can be relative if a
71
+ * base URI can be determined. On the server-side, the base URI is the CWD. In
72
+ * browser, the base URI is the URI of the page.
73
+ *
74
+ * @see Use {@link Hyperjump.addUriSchemePlugin} to add support for additional
75
+ * URI schemes.
76
+ * @see Support for {@link JrefMediaTypePlugin | JRef} and
77
+ * {@link JsonMediaTypePlugin | JSON} is built in. Use
78
+ * {@link Hyperjump.addMediaTypePlugin} to add support for additional media
79
+ * types.
80
+ *
81
+ * @type (uri: string, options?: GetOptions) => Promise<JsonCompatible<JrefNode>>
82
+ * @throws {@link RetrievalError}
83
+ * @throws {@link JsonPointerError}
84
+ */
68
85
async get ( uri , options = this . #defaultGetOptions) {
69
86
uri = resolveIri ( uri , options . referencedFrom ?? contextUri ( ) ) ;
70
87
const id = toAbsoluteIri ( uri ) ;
@@ -107,19 +124,39 @@ export class Hyperjump {
107
124
}
108
125
}
109
126
110
- /** @type (plugin: UriSchemePlugin) => void */
127
+ /**
128
+ * Add support for a
129
+ * {@link https://www.rfc-editor.org/rfc/rfc3986#section-3.1 | URI scheme}.
130
+ * Support for the {@link HttpUriSchemePlugin | `http(s):`} and
131
+ * {@link FileUriSchemePlugin | `file:`} URI schemes are enabled by default.
132
+ *
133
+ * @type (plugin: UriSchemePlugin) => void
134
+ */
111
135
addUriSchemePlugin ( plugin ) {
112
136
for ( const scheme of plugin . schemes ) {
113
137
this . #uriSchemePlugins[ scheme ] = plugin ;
114
138
}
115
139
}
116
140
117
- /** @type (scheme: string) => void */
141
+ /**
142
+ * This is mostly useful for disabling a scheme that's enabled by default.
143
+ *
144
+ * @type (scheme: string) => void
145
+ */
118
146
removeUriSchemePlugin ( scheme ) {
119
147
delete this . #uriSchemePlugins[ scheme ] ;
120
148
}
121
149
122
- /** @type (uri: string, options: GetOptions) => Promise<Response> */
150
+ /**
151
+ * Unless you're using it in a {@link UriSchemePlugin.retrieve} method, this
152
+ * is not the method you're looking for. It's used internally to fetch a
153
+ * resource before processing it based on its media type. You might use it for
154
+ * implementing {@link UriSchemePlugin}s for URI schemes that don't have
155
+ * locating semantics (such as `urn:`) and instead map to another URI where
156
+ * the resource can be retrieved from. See the example in the README.
157
+ *
158
+ * @type (uri: string, options: GetOptions) => Promise<Response>
159
+ */
123
160
async retrieve ( uri , options ) {
124
161
const { scheme } = parseIri ( uri ) ;
125
162
@@ -130,6 +167,11 @@ export class Hyperjump {
130
167
return this . #uriSchemePlugins[ scheme ] . retrieve ( uri , options ) ;
131
168
}
132
169
170
+ /**
171
+ * Constructs an `Accept` header based on the registered media types.
172
+ *
173
+ * @type () => string
174
+ */
133
175
acceptableMediaTypes ( ) {
134
176
let accept = "" ;
135
177
@@ -153,7 +195,12 @@ export class Hyperjump {
153
195
return accept ;
154
196
}
155
197
156
- /** @type (uri: string) => string */
198
+ /**
199
+ * Returns the media type of the resource based on its URI. This is usually
200
+ * based on the extension and configured by {@link MediaTypePlugin}s.
201
+ *
202
+ * @type (uri: string) => string
203
+ */
157
204
getMediaType ( uri ) {
158
205
for ( const contentType in this . #mediaTypePlugins) {
159
206
for ( const extension of this . #mediaTypePlugins[ contentType ] . extensions ) {
@@ -168,17 +215,34 @@ export class Hyperjump {
168
215
throw new UnknownMediaTypeError ( `The media type of the file at '${ uri } ' could not be determined. Use the 'addMediaTypePlugin' function to add support for this media type.` ) ;
169
216
}
170
217
171
- /** @type <T extends DocumentNode>(plugin: MediaTypePlugin<T>) => void */
218
+ /**
219
+ * Add support for a media tpe. Support for the
220
+ * {@link JrefMediaTypePlugin | `JRef`} and
221
+ * {@link JsonMediaTypePlugin | `JSON`} media types are enabled by default.
222
+ *
223
+ * @type <T extends DocumentNode>(plugin: MediaTypePlugin<T>) => void
224
+ */
172
225
addMediaTypePlugin ( plugin ) {
173
226
this . #mediaTypePlugins[ plugin . mediaType ] = plugin ;
174
227
}
175
228
176
- /** @type (contentType: string) => void */
229
+ /**
230
+ * This is mostly useful for disabling a scheme that's enabled by default.
231
+ *
232
+ * @type (contentType: string) => void
233
+ */
177
234
removeMediaTypePlugin ( contentType ) {
178
235
delete this . #mediaTypePlugins[ contentType ] ;
179
236
}
180
237
181
- /** @type (contentType: string, quality: number) => void */
238
+ /**
239
+ * Set the
240
+ * {@link https://developer.mozilla.org/en-US/docs/Glossary/Quality_values | quality}
241
+ * that will be used in the Accept header of requests to indicate to servers
242
+ * which media types are preferred over others.
243
+ *
244
+ * @type (contentType: string, quality: number) => void
245
+ */
182
246
setMediaTypeQuality ( contentType , quality ) {
183
247
this . #mediaTypePlugins[ contentType ] . quality = quality ;
184
248
}
@@ -242,12 +306,22 @@ export class Hyperjump {
242
306
}
243
307
}
244
308
245
- /** @type (key: string, node: JsonCompatible<JrefNode>) => Promise<JsonCompatible<JrefNode>> */
309
+ /**
310
+ * This is like indexing into an object or array. It will follow any
311
+ * references it encounters so it always returns a JSON compatible value.
312
+ *
313
+ * @type (key: string, node: JsonCompatible<JrefNode>) => Promise<JsonCompatible<JrefNode>>
314
+ */
246
315
async step ( key , node ) {
247
316
return await this . #followReferences( pointerStep ( key , node ) ) ;
248
317
}
249
318
250
- /** @type (node: JsonCompatible<JrefNode>) => AsyncGenerator<JsonCompatible<JrefNode>, void, unknown> */
319
+ /**
320
+ * Iterate over an array node. It will follow any references it encounters so
321
+ * it always yields JSON compatible values.
322
+ *
323
+ * @type (node: JsonCompatible<JrefNode>) => AsyncGenerator<JsonCompatible<JrefNode>, void, unknown>
324
+ */
251
325
async * iter ( node ) {
252
326
if ( node . jsonType === "array" ) {
253
327
for ( const itemNode of node . children ) {
@@ -265,7 +339,12 @@ export class Hyperjump {
265
339
}
266
340
}
267
341
268
- /** @type (node: JsonCompatible<JrefNode>) => AsyncGenerator<JsonCompatible<JrefNode>, void, unknown> */
342
+ /**
343
+ * Iterate over the values of an object. It will follow any references it
344
+ * encounters so it always yields JSON compatible values.
345
+ *
346
+ * @type (node: JsonCompatible<JrefNode>) => AsyncGenerator<JsonCompatible<JrefNode>, void, unknown>
347
+ */
269
348
async * values ( node ) {
270
349
if ( node . jsonType === "object" ) {
271
350
for ( const propertyNode of node . children ) {
@@ -274,7 +353,12 @@ export class Hyperjump {
274
353
}
275
354
}
276
355
277
- /** @type (node: JsonCompatible<JrefNode>) => AsyncGenerator<[string, JsonCompatible<JrefNode>], void, unknown> */
356
+ /**
357
+ * Iterate over key/value pairs of an object. It will follow any references it
358
+ * encounters so it always yields JSON compatible values.
359
+ *
360
+ * @type (node: JsonCompatible<JrefNode>) => AsyncGenerator<[string, JsonCompatible<JrefNode>], void, unknown>
361
+ */
278
362
async * entries ( node ) {
279
363
if ( node . jsonType === "object" ) {
280
364
for ( const propertyNode of node . children ) {
0 commit comments