@@ -21,6 +21,10 @@ import { mimeMatch } from "./utilities.js";
2121// TODO: Support fetch options in get
2222// TODO: Support filters
2323
24+ /**
25+ * @template {JrefNode} [T=JrefNode]
26+ * @implements API.Hyperjump<T>
27+ */
2428export class Hyperjump {
2529 // TODO: Add config to enable schemes and media types
2630 #config;
@@ -59,7 +63,7 @@ export class Hyperjump {
5963 this . addMediaTypePlugin ( new JsonMediaTypePlugin ( ) ) ;
6064 }
6165
62- /** @type API.Hyperjump["get"] */
66+ /** @type API.Hyperjump<T> ["get"] */
6367 async get ( uri , options = this . #defaultGetOptions) {
6468 uri = resolveIri ( uri , contextUri ( ) ) ;
6569 const id = toAbsoluteIri ( uri ) ;
@@ -89,32 +93,32 @@ export class Hyperjump {
8993 const cursor = document . fragmentKind === "json-pointer" ? fragment : "" ;
9094
9195 // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
92- const node = pointerGet ( cursor ?? "" , document . children [ 0 ] , document . uri ) ;
96+ const node = /** @type T */ ( pointerGet ( cursor ?? "" , document . children [ 0 ] , document . uri ) ) ;
9397 return await this . #followReferences( node ) ;
9498 }
9599
96- /** @type (node: JrefNode ) => Promise<JsonCompatible<JrefNode >> */
100+ /** @type (node: T ) => Promise<JsonCompatible<T >> */
97101 async #followReferences( node ) {
98102 if ( node ?. type === "jref-reference" ) {
99103 return this . get ( node . value , { referencedFrom : node . documentUri } ) ;
100104 } else {
101- return node ;
105+ return /** @type JsonCompatible<T> */ ( node ) ;
102106 }
103107 }
104108
105- /** @type API.Hyperjump["addUriSchemePlugin"] */
109+ /** @type API.Hyperjump<T> ["addUriSchemePlugin"] */
106110 addUriSchemePlugin ( plugin ) {
107111 for ( const scheme of plugin . schemes ) {
108112 this . #uriSchemePlugins[ scheme ] = plugin ;
109113 }
110114 }
111115
112- /** @type API.Hyperjump["removeUriSchemePlugin"] */
116+ /** @type API.Hyperjump<T> ["removeUriSchemePlugin"] */
113117 removeUriSchemePlugin ( scheme ) {
114118 delete this . #uriSchemePlugins[ scheme ] ;
115119 }
116120
117- /** @type API.Hyperjump["retrieve"] */
121+ /** @type API.Hyperjump<T> ["retrieve"] */
118122 async retrieve ( uri , options ) {
119123 const { scheme } = parseIri ( uri ) ;
120124
@@ -125,7 +129,7 @@ export class Hyperjump {
125129 return this . #uriSchemePlugins[ scheme ] . retrieve ( uri , options ) ;
126130 }
127131
128- /** @type API.Hyperjump["acceptableMediaTypes"] */
132+ /** @type API.Hyperjump<T> ["acceptableMediaTypes"] */
129133 acceptableMediaTypes ( ) {
130134 let accept = "" ;
131135
@@ -149,7 +153,7 @@ export class Hyperjump {
149153 return accept ;
150154 }
151155
152- /** @type API.Hyperjump["getMediaType"] */
156+ /** @type API.Hyperjump<T> ["getMediaType"] */
153157 getMediaType ( uri ) {
154158 for ( const contentType in this . #mediaTypePlugins) {
155159 for ( const extension of this . #mediaTypePlugins[ contentType ] . extensions ) {
@@ -164,17 +168,17 @@ export class Hyperjump {
164168 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.` ) ;
165169 }
166170
167- /** @type API.Hyperjump["addMediaTypePlugin"] */
171+ /** @type API.Hyperjump<T> ["addMediaTypePlugin"] */
168172 addMediaTypePlugin ( plugin ) {
169173 this . #mediaTypePlugins[ plugin . mediaType ] = plugin ;
170174 }
171175
172- /** @type API.Hyperjump["removeMediaTypePlugin"] */
176+ /** @type API.Hyperjump<T> ["removeMediaTypePlugin"] */
173177 removeMediaTypePlugin ( contentType ) {
174178 delete this . #mediaTypePlugins[ contentType ] ;
175179 }
176180
177- /** @type API.Hyperjump["setMediaTypeQuality"] */
181+ /** @type API.Hyperjump<T> ["setMediaTypeQuality"] */
178182 setMediaTypeQuality ( contentType , quality ) {
179183 this . #mediaTypePlugins[ contentType ] . quality = quality ;
180184 }
@@ -200,12 +204,12 @@ export class Hyperjump {
200204 typeOf = jsonTypeOf ;
201205 has = jsonObjectHas ;
202206
203- /** @type API.Hyperjump["step"] */
207+ /** @type API.Hyperjump<T> ["step"] */
204208 async step ( key , node ) {
205- return await this . #followReferences( pointerStep ( key , node ) ) ;
209+ return await this . #followReferences( /** @type T */ ( pointerStep ( key , node ) ) ) ;
206210 }
207211
208- /** @type API.Hyperjump["iter"] */
212+ /** @type API.Hyperjump<T> ["iter"] */
209213 async * iter ( node ) {
210214 if ( node . jsonType === "array" ) {
211215 for ( const itemNode of node . children ) {
@@ -216,7 +220,7 @@ export class Hyperjump {
216220
217221 keys = jsonObjectKeys ;
218222
219- /** @type API.Hyperjump["values"] */
223+ /** @type API.Hyperjump<T> ["values"] */
220224 async * values ( node ) {
221225 if ( node . jsonType === "object" ) {
222226 for ( const propertyNode of node . children ) {
@@ -225,11 +229,14 @@ export class Hyperjump {
225229 }
226230 }
227231
228- /** @type API.Hyperjump["entries"] */
232+ /** @type API.Hyperjump<T> ["entries"] */
229233 async * entries ( node ) {
230234 if ( node . jsonType === "object" ) {
231235 for ( const propertyNode of node . children ) {
232- yield [ propertyNode . children [ 0 ] . value , await this . #followReferences( propertyNode . children [ 1 ] ) ] ;
236+ yield [
237+ propertyNode . children [ 0 ] . value ,
238+ await this . #followReferences( propertyNode . children [ 1 ] )
239+ ] ;
233240 }
234241 }
235242 }
0 commit comments