ketting 8.2.0 → 8.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. package/dist/browser/index.d.mts +1511 -0
  2. package/dist/browser/index.d.mts.map +1 -0
  3. package/dist/browser/index.mjs +2 -0
  4. package/dist/browser/index.mjs.map +1 -0
  5. package/dist/{action.js → main/action.js} +4 -4
  6. package/dist/main/action.js.map +1 -0
  7. package/dist/main/cache/forever.js.map +1 -0
  8. package/dist/main/cache/index.js.map +1 -0
  9. package/dist/main/cache/never.js.map +1 -0
  10. package/dist/main/cache/short.js.map +1 -0
  11. package/dist/main/client.js.map +1 -0
  12. package/dist/main/field.js.map +1 -0
  13. package/dist/main/follow-promise.js.map +1 -0
  14. package/dist/main/http/basic-auth.js.map +1 -0
  15. package/dist/main/http/bearer-auth.js.map +1 -0
  16. package/dist/main/http/error.js.map +1 -0
  17. package/dist/main/http/fetcher.js.map +1 -0
  18. package/dist/main/http/oauth2.js.map +1 -0
  19. package/dist/main/http/util.js.map +1 -0
  20. package/dist/main/index.d.ts +18 -0
  21. package/dist/{index.js → main/index.js} +3 -1
  22. package/dist/main/index.js.map +1 -0
  23. package/dist/main/link.js.map +1 -0
  24. package/dist/main/middlewares/accept-header.js.map +1 -0
  25. package/dist/main/middlewares/cache.js.map +1 -0
  26. package/dist/main/middlewares/warning.js.map +1 -0
  27. package/dist/{resource.d.ts → main/resource.d.ts} +1 -1
  28. package/dist/{resource.js → main/resource.js} +2 -2
  29. package/dist/main/resource.js.map +1 -0
  30. package/dist/main/state/base-state.js.map +1 -0
  31. package/dist/main/state/binary.js.map +1 -0
  32. package/dist/main/state/collection-json.js.map +1 -0
  33. package/dist/main/state/hal.js.map +1 -0
  34. package/dist/main/state/head.js.map +1 -0
  35. package/dist/main/state/html.js.map +1 -0
  36. package/dist/{state → main/state}/index.d.ts +1 -1
  37. package/dist/main/state/index.js.map +1 -0
  38. package/dist/main/state/interface.js.map +1 -0
  39. package/dist/main/state/jsonapi.js.map +1 -0
  40. package/dist/main/state/siren.js.map +1 -0
  41. package/dist/main/state/text.js.map +1 -0
  42. package/dist/main/types.js.map +1 -0
  43. package/dist/main/util/fetch-body-helper.js.map +1 -0
  44. package/dist/main/util/fetch-body-helper.web.js.map +1 -0
  45. package/dist/main/util/html.js.map +1 -0
  46. package/dist/main/util/html.web.js.map +1 -0
  47. package/dist/main/util/uri-template.js.map +1 -0
  48. package/dist/main/util/uri.js.map +1 -0
  49. package/package.json +12 -11
  50. package/src/action.ts +1 -1
  51. package/src/index.ts +18 -16
  52. package/src/resource.ts +1 -1
  53. package/src/state/index.ts +1 -1
  54. package/dist/action.js.map +0 -1
  55. package/dist/cache/forever.js.map +0 -1
  56. package/dist/cache/index.js.map +0 -1
  57. package/dist/cache/never.js.map +0 -1
  58. package/dist/cache/short.js.map +0 -1
  59. package/dist/client.js.map +0 -1
  60. package/dist/field.js.map +0 -1
  61. package/dist/follow-promise.js.map +0 -1
  62. package/dist/http/basic-auth.js.map +0 -1
  63. package/dist/http/bearer-auth.js.map +0 -1
  64. package/dist/http/error.js.map +0 -1
  65. package/dist/http/fetcher.js.map +0 -1
  66. package/dist/http/oauth2.js.map +0 -1
  67. package/dist/http/util.js.map +0 -1
  68. package/dist/index.d.ts +0 -16
  69. package/dist/index.js.map +0 -1
  70. package/dist/link.js.map +0 -1
  71. package/dist/middlewares/accept-header.js.map +0 -1
  72. package/dist/middlewares/cache.js.map +0 -1
  73. package/dist/middlewares/warning.js.map +0 -1
  74. package/dist/resource.js.map +0 -1
  75. package/dist/state/base-state.js.map +0 -1
  76. package/dist/state/binary.js.map +0 -1
  77. package/dist/state/collection-json.js.map +0 -1
  78. package/dist/state/hal.js.map +0 -1
  79. package/dist/state/head.js.map +0 -1
  80. package/dist/state/html.js.map +0 -1
  81. package/dist/state/index.js.map +0 -1
  82. package/dist/state/interface.js.map +0 -1
  83. package/dist/state/jsonapi.js.map +0 -1
  84. package/dist/state/siren.js.map +0 -1
  85. package/dist/state/text.js.map +0 -1
  86. package/dist/types.js.map +0 -1
  87. package/dist/util/fetch-body-helper.js.map +0 -1
  88. package/dist/util/fetch-body-helper.web.js.map +0 -1
  89. package/dist/util/html.js.map +0 -1
  90. package/dist/util/html.web.js.map +0 -1
  91. package/dist/util/uri-template.js.map +0 -1
  92. package/dist/util/uri.js.map +0 -1
  93. /package/dist/{action.d.ts → main/action.d.ts} +0 -0
  94. /package/dist/{cache → main/cache}/forever.d.ts +0 -0
  95. /package/dist/{cache → main/cache}/forever.js +0 -0
  96. /package/dist/{cache → main/cache}/index.d.ts +0 -0
  97. /package/dist/{cache → main/cache}/index.js +0 -0
  98. /package/dist/{cache → main/cache}/never.d.ts +0 -0
  99. /package/dist/{cache → main/cache}/never.js +0 -0
  100. /package/dist/{cache → main/cache}/short.d.ts +0 -0
  101. /package/dist/{cache → main/cache}/short.js +0 -0
  102. /package/dist/{client.d.ts → main/client.d.ts} +0 -0
  103. /package/dist/{client.js → main/client.js} +0 -0
  104. /package/dist/{field.d.ts → main/field.d.ts} +0 -0
  105. /package/dist/{field.js → main/field.js} +0 -0
  106. /package/dist/{follow-promise.d.ts → main/follow-promise.d.ts} +0 -0
  107. /package/dist/{follow-promise.js → main/follow-promise.js} +0 -0
  108. /package/dist/{http → main/http}/basic-auth.d.ts +0 -0
  109. /package/dist/{http → main/http}/basic-auth.js +0 -0
  110. /package/dist/{http → main/http}/bearer-auth.d.ts +0 -0
  111. /package/dist/{http → main/http}/bearer-auth.js +0 -0
  112. /package/dist/{http → main/http}/error.d.ts +0 -0
  113. /package/dist/{http → main/http}/error.js +0 -0
  114. /package/dist/{http → main/http}/fetcher.d.ts +0 -0
  115. /package/dist/{http → main/http}/fetcher.js +0 -0
  116. /package/dist/{http → main/http}/oauth2.d.ts +0 -0
  117. /package/dist/{http → main/http}/oauth2.js +0 -0
  118. /package/dist/{http → main/http}/util.d.ts +0 -0
  119. /package/dist/{http → main/http}/util.js +0 -0
  120. /package/dist/{link.d.ts → main/link.d.ts} +0 -0
  121. /package/dist/{link.js → main/link.js} +0 -0
  122. /package/dist/{middlewares → main/middlewares}/accept-header.d.ts +0 -0
  123. /package/dist/{middlewares → main/middlewares}/accept-header.js +0 -0
  124. /package/dist/{middlewares → main/middlewares}/cache.d.ts +0 -0
  125. /package/dist/{middlewares → main/middlewares}/cache.js +0 -0
  126. /package/dist/{middlewares → main/middlewares}/warning.d.ts +0 -0
  127. /package/dist/{middlewares → main/middlewares}/warning.js +0 -0
  128. /package/dist/{state → main/state}/base-state.d.ts +0 -0
  129. /package/dist/{state → main/state}/base-state.js +0 -0
  130. /package/dist/{state → main/state}/binary.d.ts +0 -0
  131. /package/dist/{state → main/state}/binary.js +0 -0
  132. /package/dist/{state → main/state}/collection-json.d.ts +0 -0
  133. /package/dist/{state → main/state}/collection-json.js +0 -0
  134. /package/dist/{state → main/state}/hal.d.ts +0 -0
  135. /package/dist/{state → main/state}/hal.js +0 -0
  136. /package/dist/{state → main/state}/head.d.ts +0 -0
  137. /package/dist/{state → main/state}/head.js +0 -0
  138. /package/dist/{state → main/state}/html.d.ts +0 -0
  139. /package/dist/{state → main/state}/html.js +0 -0
  140. /package/dist/{state → main/state}/index.js +0 -0
  141. /package/dist/{state → main/state}/interface.d.ts +0 -0
  142. /package/dist/{state → main/state}/interface.js +0 -0
  143. /package/dist/{state → main/state}/jsonapi.d.ts +0 -0
  144. /package/dist/{state → main/state}/jsonapi.js +0 -0
  145. /package/dist/{state → main/state}/siren.d.ts +0 -0
  146. /package/dist/{state → main/state}/siren.js +0 -0
  147. /package/dist/{state → main/state}/text.d.ts +0 -0
  148. /package/dist/{state → main/state}/text.js +0 -0
  149. /package/dist/{types.d.ts → main/types.d.ts} +0 -0
  150. /package/dist/{types.js → main/types.js} +0 -0
  151. /package/dist/{util → main/util}/fetch-body-helper.d.ts +0 -0
  152. /package/dist/{util → main/util}/fetch-body-helper.js +0 -0
  153. /package/dist/{util → main/util}/fetch-body-helper.web.d.ts +0 -0
  154. /package/dist/{util → main/util}/fetch-body-helper.web.js +0 -0
  155. /package/dist/{util → main/util}/html.d.ts +0 -0
  156. /package/dist/{util → main/util}/html.js +0 -0
  157. /package/dist/{util → main/util}/html.web.d.ts +0 -0
  158. /package/dist/{util → main/util}/html.web.js +0 -0
  159. /package/dist/{util → main/util}/uri-template.d.ts +0 -0
  160. /package/dist/{util → main/util}/uri-template.js +0 -0
  161. /package/dist/{util → main/util}/uri.d.ts +0 -0
  162. /package/dist/{util → main/util}/uri.js +0 -0
@@ -0,0 +1,1511 @@
1
+ import { LinkHints } from "hal-types";
2
+ import { EventEmitter } from "node:events";
3
+
4
+ //#region src/http/fetcher.d.ts
5
+ type FetchMiddleware = (request: Request, next: (request: Request) => Promise<Response>) => Promise<Response>;
6
+ /**
7
+ * The fetcher object is responsible for calling fetch()
8
+ *
9
+ * This is wrapped in an object because we want to support
10
+ * 'fetch middlewares'. These middlewares are similar to server-side
11
+ * middlewares and can intercept requests and alter requests/responses.
12
+ */
13
+ declare class Fetcher {
14
+ middlewares: [RegExp, FetchMiddleware][];
15
+ advertiseKetting: boolean;
16
+ /**
17
+ * A wrapper for MDN fetch()
18
+ *
19
+ * This wrapper supports 'fetch middlewares'. It will call them
20
+ * in sequence.
21
+ */
22
+ fetch(resource: string | Request, init?: RequestInit): Promise<Response>;
23
+ /**
24
+ * Returns the list of middlewares that are applicable to
25
+ * a specific origin
26
+ */
27
+ getMiddlewaresByOrigin(origin: string): FetchMiddleware[];
28
+ /**
29
+ * Add a middleware
30
+ */
31
+ use(mw: FetchMiddleware, origin?: string): void;
32
+ /**
33
+ * Does a HTTP request and throws an exception if the server emitted
34
+ * a HTTP error.
35
+ *
36
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Request/Request
37
+ */
38
+ fetchOrThrow(resource: string | Request, init?: RequestInit): Promise<Response>;
39
+ }
40
+ //#endregion
41
+ //#region src/field.d.ts
42
+ type Field = BooleanField | BasicStringField | DateTimeField | FileField | HiddenField | NumberField | SelectFieldSingle | SelectFieldMulti | RangeStringField | TextAreaField | TextField;
43
+ /**
44
+ * A Field describes a single field in an action or form.
45
+ *
46
+ * Fields can be used to automatically render forms or other UIs based on
47
+ * hypermedia actions.
48
+ */
49
+ interface BaseField<T> {
50
+ /**
51
+ * Name of the field.
52
+ *
53
+ * Typically this is the property that will get sent to a server.
54
+ */
55
+ name: string;
56
+ /**
57
+ * Type describes the type of the property.
58
+ *
59
+ * This is similar to the HTML5 "type" attribute on forms.
60
+ */
61
+ type: string;
62
+ /**
63
+ * This could be used to describe a sample value.
64
+ */
65
+ placeholder?: T;
66
+ /**
67
+ * Whether this field is required for submitting the form.
68
+ */
69
+ required: boolean;
70
+ /**
71
+ * Render the field as read-only.
72
+ */
73
+ readOnly: boolean;
74
+ /**
75
+ * A human-readable label for the field.
76
+ */
77
+ label?: string;
78
+ }
79
+ interface SingleValueField<T> extends BaseField<T> {
80
+ /**
81
+ * The current (pre-filed) value on the form.
82
+ */
83
+ value?: T;
84
+ }
85
+ /**
86
+ * The base type for things that are range-like.
87
+ *
88
+ * This includes numbers, dates and time fields.
89
+ */
90
+ interface RangeField<T> extends SingleValueField<T> {
91
+ max?: number;
92
+ min?: number;
93
+ step?: number;
94
+ }
95
+ /**
96
+ * Toggles/checkboxes
97
+ */
98
+ interface BooleanField extends SingleValueField<boolean> {
99
+ type: 'checkbox' | 'radio';
100
+ }
101
+ /**
102
+ * Any field that encodes itself as a string but with no
103
+ * special features.
104
+ */
105
+ interface BasicStringField extends SingleValueField<string> {
106
+ type: 'color' | 'email' | 'password' | 'search' | 'tel' | 'url';
107
+ minLength?: number;
108
+ maxLength?: number;
109
+ }
110
+ interface RangeStringField extends RangeField<string> {
111
+ type: 'date' | 'month' | 'time' | 'week';
112
+ }
113
+ interface DateTimeField extends RangeField<Date> {
114
+ type: 'datetime' | 'datetime-local';
115
+ }
116
+ interface HiddenField extends SingleValueField<string | number | null | boolean> {
117
+ type: 'hidden';
118
+ }
119
+ interface FileField extends SingleValueField<never> {
120
+ type: 'file';
121
+ }
122
+ interface NumberField extends RangeField<number> {
123
+ type: 'number' | 'range';
124
+ }
125
+ /**
126
+ * OptionsDataSource is a helper type that specifiess the different data
127
+ * sources for lists of options.
128
+ */
129
+ type OptionsDataSource = {
130
+ /**
131
+ * Keys and values are labels and values.
132
+ *
133
+ * If specified as a plain array, use array values as labels and values.
134
+ */
135
+ options: Record<string, string> | string[];
136
+ } | {
137
+ /**
138
+ * If dataSource is specified, we'll grab the list of options from a
139
+ * simple csv or json resource.
140
+ */
141
+ dataSource: {
142
+ /**
143
+ * URI where the list of options can be acquired.
144
+ */
145
+ href: string;
146
+ /**
147
+ * Could be text/csv or any json content-type.
148
+ */
149
+ type?: string;
150
+ /**
151
+ * If datasource returns an array of objects, use this
152
+ * property for the label. Defaults to 'label'
153
+ *
154
+ * This is ignored if it doesn't apply for the media type.
155
+ */
156
+ labelField?: string;
157
+ /**
158
+ * If datasource returns an array of objects, use this
159
+ * property for the value. Defaults to 'value'
160
+ *
161
+ * This is ignored if it doesn't apply for the media type.
162
+ */
163
+ valueField?: string;
164
+ };
165
+ } | {
166
+ /**
167
+ * If 'linkSource' is specified, we assume that the value will be a URI.
168
+ *
169
+ * We will grab the list of options by fetching a resource, and getting
170
+ * a list of links specified by a rel.
171
+ */
172
+ linkSource: {
173
+ href: string;
174
+ rel: string;
175
+ };
176
+ };
177
+ /**
178
+ * Encodes a field that has a list of options a user can choose from.
179
+ */
180
+ type SelectFieldSingle = BaseField<string> & {
181
+ type: 'select';
182
+ renderAs?: 'radio' | 'dropdown';
183
+ multiple?: false;
184
+ /**
185
+ * The current (pre-filed) value on the form.
186
+ */
187
+ selectedValue?: string;
188
+ /**
189
+ * The current (pre-filed) value on the form.
190
+ * @deprecated Use selectedValue instead
191
+ */
192
+ value?: string;
193
+ } & OptionsDataSource;
194
+ /**
195
+ * An options field where users can select more than 1 item
196
+ */
197
+ type SelectFieldMulti = BaseField<string> & {
198
+ type: 'select';
199
+ renderAs?: 'checkbox' | 'dropdown';
200
+ multiple: true;
201
+ /**
202
+ * The current (pre-filed) values on the form.
203
+ */
204
+ selectedValues?: string[];
205
+ /**
206
+ * The current (pre-filed) value on the form.
207
+ * @deprecated Use selectedValues instead
208
+ */
209
+ value?: string;
210
+ } & OptionsDataSource;
211
+ interface TextField extends SingleValueField<string> {
212
+ type: 'text';
213
+ minLength?: number;
214
+ maxLength?: number;
215
+ pattern?: RegExp;
216
+ }
217
+ interface TextAreaField extends SingleValueField<string> {
218
+ type: 'textarea';
219
+ minLength?: number;
220
+ maxLength?: number;
221
+ cols?: number;
222
+ rows?: number;
223
+ }
224
+ //#endregion
225
+ //#region src/action.d.ts
226
+ interface ActionInfo {
227
+ /**
228
+ * What url to post the form to.
229
+ */
230
+ uri: string;
231
+ /**
232
+ * Action name.
233
+ *
234
+ * Some formats call this the 'rel'
235
+ */
236
+ name: string | null;
237
+ /**
238
+ * Form title.
239
+ *
240
+ * Should be human-friendly.
241
+ */
242
+ title?: string;
243
+ /**
244
+ * The HTTP method to use
245
+ */
246
+ method: string;
247
+ /**
248
+ * The contentType to use for the form submission
249
+ */
250
+ contentType: string;
251
+ /**
252
+ * Returns the list of fields associated to an action
253
+ */
254
+ fields: Field[];
255
+ }
256
+ /**
257
+ * An action represents a hypermedia form submission or action.
258
+ */
259
+ interface Action<T extends Record<string, any> = Record<string, any>> extends ActionInfo {
260
+ /**
261
+ * Execute the action or submit the form.
262
+ */
263
+ submit(formData: T): Promise<State>;
264
+ /**
265
+ * Return a field by name.
266
+ */
267
+ field(name: string): Field | undefined;
268
+ /**
269
+ * Execute the action or submit the form, then return the next resource.
270
+ *
271
+ * If a server responds with a 201 Status code and a Location header,
272
+ * it will automatically return the newly created resource.
273
+ *
274
+ * If the server responded with a 204 or 205, this function will return
275
+ * `this`.
276
+ */
277
+ submitFollow(formData: T): Promise<Resource>;
278
+ }
279
+ //#endregion
280
+ //#region src/link.d.ts
281
+ type Link = {
282
+ /**
283
+ * Target URI
284
+ */
285
+ href: string;
286
+ /**
287
+ * Context URI.
288
+ *
289
+ * Used to resolve relative URIs
290
+ */
291
+ context: string;
292
+ /**
293
+ * Relation type
294
+ */
295
+ rel: string;
296
+ /**
297
+ * Link title
298
+ */
299
+ title?: string;
300
+ /**
301
+ * Content type hint of the target resource
302
+ */
303
+ type?: string;
304
+ /**
305
+ * Anchor.
306
+ *
307
+ * This describes where the link is linked from, from for example
308
+ * a fragment in the current document
309
+ */
310
+ anchor?: string;
311
+ /**
312
+ * Language of the target resource
313
+ */
314
+ hreflang?: string;
315
+ /**
316
+ * HTML5 media attribute
317
+ */
318
+ media?: string;
319
+ /**
320
+ * If templated is set to true, the href is a templated URI.
321
+ */
322
+ templated?: boolean;
323
+ /**
324
+ * Link hints, as defined in draft-nottingham-link-hint
325
+ */
326
+ hints?: LinkHints;
327
+ /**
328
+ * Link name
329
+ *
330
+ * This is sometimes used as a machine-readable secondary key for links.
331
+ *
332
+ * This is at least used in HAL, but there may be other formats:
333
+ *
334
+ * @see https://datatracker.ietf.org/doc/html/draft-kelly-json-hal-06#section-5.5
335
+ */
336
+ name?: string;
337
+ };
338
+ type NewLink = Omit<Link, 'context'>;
339
+ /**
340
+ * Links container, providing an easy way to manage a set of links.
341
+ */
342
+ declare class Links {
343
+ defaultContext: string;
344
+ private store;
345
+ constructor(defaultContext: string, links?: Link[] | Links);
346
+ /**
347
+ * Adds a link to the list
348
+ */
349
+ add(...links: (Link | NewLink)[]): void;
350
+ add(rel: string, href: string): void;
351
+ /**
352
+ * Set a link
353
+ *
354
+ * If a link with the provided 'rel' already existed, it will be overwritten.
355
+ */
356
+ set(link: Link | NewLink): void;
357
+ set(rel: string, href: string): void;
358
+ /**
359
+ * Return a single link by its 'rel'.
360
+ *
361
+ * If the link does not exist, undefined is returned.
362
+ */
363
+ get(rel: string): Link | undefined;
364
+ /**
365
+ * Delete all links with the given 'rel'.
366
+ *
367
+ * If the second argument is provided, only links that match the href will
368
+ * be removed.
369
+ */
370
+ delete(rel: string, href?: string): void;
371
+ /**
372
+ * Return all links that have a given rel.
373
+ *
374
+ * If no links with the rel were found, an empty array is returned.
375
+ */
376
+ getMany(rel: string): Link[];
377
+ /**
378
+ * Return all links.
379
+ */
380
+ getAll(): Link[];
381
+ /**
382
+ * Returns true if at least 1 link with the given rel exists.
383
+ */
384
+ has(rel: string): boolean;
385
+ }
386
+ /**
387
+ * The LinkNotFound error gets thrown whenever something tries to follow a
388
+ * link by its rel, that doesn't exist
389
+ */
390
+ declare class LinkNotFound extends Error {}
391
+ /**
392
+ * A key->value map of variables to place in a templated link
393
+ */
394
+ type LinkVariables = {
395
+ [key: string]: string | number | string[] | number[];
396
+ };
397
+ //#endregion
398
+ //#region src/state/interface.d.ts
399
+ type State<T = any> = {
400
+ /**
401
+ * The URI associated with this state
402
+ */
403
+ uri: string;
404
+ /**
405
+ * Represents the body of the HTTP response.
406
+ *
407
+ * In the case of a JSON response, this will be deserialized
408
+ */
409
+ data: T;
410
+ /**
411
+ * All links associated with the resource.
412
+ */
413
+ links: Links;
414
+ /**
415
+ * The full list of HTTP headers that were sent with the response.
416
+ */
417
+ headers: Headers;
418
+ /**
419
+ * Reference to main client that created this state
420
+ */
421
+ client: Client;
422
+ /**
423
+ * Follows a relationship, based on its reltype. For example, this might be
424
+ * 'alternate', 'item', 'edit' or a custom url-based one.
425
+ *
426
+ * This function can also follow templated uris. You can specify uri
427
+ * variables in the optional variables argument.
428
+ */
429
+ follow<TFollowedResource = any>(rel: string, variables?: LinkVariables): Resource<TFollowedResource>;
430
+ /**
431
+ * Follows a relationship based on its reltype. This function returns a
432
+ * Promise that resolves to an array of Resource objects.
433
+ *
434
+ * If no resources were found, the array will be empty.
435
+ */
436
+ followAll<TFollowedResource = any>(rel: string): Resource<TFollowedResource>[];
437
+ /**
438
+ * Return an action by name.
439
+ *
440
+ * If the format provides a default action, the name may be omitted.
441
+ */
442
+ action<TFormData extends Record<string, any> = any>(name?: string): Action<TFormData>;
443
+ /**
444
+ * Find an action by name.
445
+ *
446
+ * If the format provides a default action, the name may be omitted.
447
+ */
448
+ findAction<TFormData extends Record<string, any> = any>(name?: string): Action<TFormData> | undefined;
449
+ /**
450
+ * Returns all actions
451
+ */
452
+ actions(): Action[];
453
+ /**
454
+ * Checks if the specified action exists.
455
+ *
456
+ * If no name is given, checks if _any_ action exists.
457
+ */
458
+ hasAction(name?: string): boolean;
459
+ /**
460
+ * Returns a serialization of the state that can be used in a HTTP
461
+ * response.
462
+ *
463
+ * For example, a JSON object might simply serialize using
464
+ * JSON.serialize().
465
+ */
466
+ serializeBody(): Buffer | Blob | string;
467
+ /**
468
+ * Content-headers are a subset of HTTP headers that related directly
469
+ * to the content. The obvious ones are Content-Type.
470
+ *
471
+ * This set of headers will be sent by the server along with a GET
472
+ * response, but will also be sent back to the server in a PUT
473
+ * request.
474
+ */
475
+ contentHeaders(): Headers;
476
+ /**
477
+ * Some formats support embedding resources inside other resources.
478
+ *
479
+ * Please note: generally you should always use the .follow() and
480
+ * .followAll() functions to get access to linked embedded resources.
481
+ *
482
+ * There's several operations that change the State in the Ketting Cache,
483
+ * and usually this erases any associated embedded resources.
484
+ *
485
+ * .follow() and .followAll() will return the embedded resources, and also
486
+ * keeps their respective states fresh when changes are made.
487
+ */
488
+ getEmbedded(): State[];
489
+ /**
490
+ * Timestamp of when the State was first generated
491
+ */
492
+ timestamp: number;
493
+ clone(): State<T>;
494
+ };
495
+ /**
496
+ * HeadState represents the response to a HEAD request.
497
+ *
498
+ * Some information in HEAD responses might be available, but many aren't.
499
+ * Notably, the body.
500
+ */
501
+ type HeadState = Omit<State, 'data' | 'action' | 'findAction' | 'actions' | 'hasAction' | 'serializeBody' | 'getEmbedded' | 'client' | 'clone'>;
502
+ /**
503
+ * A 'StateFactory' is responsible for taking a Fetch Response, and returning
504
+ * an object that impements the State interface
505
+ */
506
+ type StateFactory<T = any> = (client: Client, uri: string, request: Response) => Promise<State<T>>;
507
+ declare function isState(input: Record<string, any>): input is State;
508
+ //#endregion
509
+ //#region src/state/base-state.d.ts
510
+ type HeadStateInit = {
511
+ client: Client;
512
+ uri: string;
513
+ links: Links;
514
+ /**
515
+ * The full list of HTTP headers that were sent with the response.
516
+ */
517
+ headers: Headers;
518
+ };
519
+ type StateInit<T> = {
520
+ client: Client;
521
+ uri: string;
522
+ data: T;
523
+ headers: Headers;
524
+ links: Links;
525
+ embedded?: State[];
526
+ actions?: ActionInfo[];
527
+ };
528
+ /**
529
+ * Implements a State object for HEAD responses
530
+ */
531
+ declare class BaseHeadState implements HeadState {
532
+ uri: string;
533
+ /**
534
+ * Timestamp of when the State was first generated
535
+ */
536
+ timestamp: number;
537
+ /**
538
+ * The full list of HTTP headers that were sent with the response.
539
+ */
540
+ headers: Headers;
541
+ /**
542
+ * All links associated with the resource.
543
+ */
544
+ links: Links;
545
+ /**
546
+ * Reference to main client that created this state
547
+ */
548
+ client: Client;
549
+ constructor(init: HeadStateInit);
550
+ /**
551
+ * Follows a relationship, based on its reltype. For example, this might be
552
+ * 'alternate', 'item', 'edit' or a custom url-based one.
553
+ *
554
+ * This function can also follow templated uris. You can specify uri
555
+ * variables in the optional variables argument.
556
+ */
557
+ follow<TFollowedResource = any>(rel: string, variables?: LinkVariables): Resource<TFollowedResource>;
558
+ /**
559
+ * Follows a relationship based on its reltype. This function returns a
560
+ * Promise that resolves to an array of Resource objects.
561
+ *
562
+ * If no resources were found, the array will be empty.
563
+ */
564
+ followAll<TFollowedResource = any>(rel: string): Resource<TFollowedResource>[];
565
+ /**
566
+ * Content-headers are a subset of HTTP headers that related directly
567
+ * to the content. The obvious ones are Content-Type.
568
+ *
569
+ * This set of headers will be sent by the server along with a GET
570
+ * response, but will also be sent back to the server in a PUT
571
+ * request.
572
+ */
573
+ contentHeaders(): Headers;
574
+ }
575
+ /**
576
+ * The Base State provides a convenient way to implement a new State type.
577
+ */
578
+ declare class BaseState<T> extends BaseHeadState implements State<T> {
579
+ data: T;
580
+ protected embedded: State[];
581
+ protected actionInfo: ActionInfo[];
582
+ constructor(init: StateInit<T>);
583
+ /**
584
+ * Return an action by name.
585
+ *
586
+ * If no name is given, the first action is returned. This is useful for
587
+ * formats that only supply 1 action, and no name.
588
+ */
589
+ action<TFormData extends Record<string, any> = any>(name?: string): Action<TFormData>;
590
+ findAction<TFormData extends Record<string, any> = any>(name?: string): Action<TFormData> | undefined;
591
+ private doFindAction;
592
+ /**
593
+ * Returns all actions
594
+ */
595
+ actions(): Action[];
596
+ /**
597
+ * Checks if the specified action exists.
598
+ *
599
+ * If no name is given, checks if _any_ action exists.
600
+ */
601
+ hasAction(name?: string): boolean;
602
+ /**
603
+ * Returns a serialization of the state that can be used in a HTTP
604
+ * response.
605
+ *
606
+ * For example, a JSON object might simply serialize using
607
+ * JSON.serialize().
608
+ */
609
+ serializeBody(): Buffer | Blob | string;
610
+ /**
611
+ * Certain formats can embed other resources, identified by their
612
+ * own URI.
613
+ *
614
+ * When a format has embedded resources, we will use these to warm
615
+ * the cache.
616
+ *
617
+ * This method returns every embedded resource.
618
+ */
619
+ getEmbedded(): State[];
620
+ clone(): State<T>;
621
+ }
622
+ //#endregion
623
+ //#region src/state/hal.d.ts
624
+ /**
625
+ * Represents a resource state in the HAL format
626
+ */
627
+ declare class HalState<T = any> extends BaseState<T> {
628
+ serializeBody(): string;
629
+ private serializeLinks;
630
+ clone(): HalState<T>;
631
+ }
632
+ //#endregion
633
+ //#region src/state/siren.d.ts
634
+ /**
635
+ * Represents a resource state in the Siren format
636
+ */
637
+ declare class SirenState<T> extends BaseState<T> {
638
+ /**
639
+ * Returns a serialization of the state that can be used in a HTTP
640
+ * response.
641
+ *
642
+ * For example, a JSON object might simply serialize using
643
+ * JSON.serialize().
644
+ */
645
+ serializeBody(): string;
646
+ clone(): SirenState<T>;
647
+ }
648
+ //#endregion
649
+ //#region src/state/collection-json.d.ts
650
+ /**
651
+ * Represents a resource state in the HAL format
652
+ */
653
+ declare class CjState<T = any> extends BaseState<T> {
654
+ serializeBody(): string;
655
+ }
656
+ //#endregion
657
+ //#region src/types.d.ts
658
+ type HttpHeaders = Record<string, string>;
659
+ /**
660
+ * RequestOptions is a set of properties that define
661
+ * a request, or state change.
662
+ *
663
+ * Everything is usually optional.
664
+ */
665
+ type RequestOptions<T = any> = {
666
+ /**
667
+ * Should return a string or a Buffer.
668
+ *
669
+ * Will be used as the body in the HTTP request.
670
+ * If not set, `body` will be used instead.
671
+ */
672
+ serializeBody?: () => string | Buffer | Blob;
673
+ /**
674
+ * If set, contains the body of the current state.
675
+ *
676
+ * If body is not a `string` or a `Buffer`, the body will
677
+ * be json encoded.
678
+ */
679
+ data?: T;
680
+ /**
681
+ * List of headers that will be set in the request.
682
+ *
683
+ * If this is not set, we'll fall back to 'headers'
684
+ */
685
+ getContentHeaders?: () => HttpHeaders | Headers;
686
+ /**
687
+ * Full list of HTTP headers.
688
+ */
689
+ headers?: HttpHeaders | Headers;
690
+ };
691
+ type GetRequestOptions = Omit<RequestOptions, 'serializeBody' | 'data'>;
692
+ type HeadRequestOptions = GetRequestOptions;
693
+ type PatchRequestOptions<T = any> = RequestOptions<T>;
694
+ type PutRequestOptions<T = any> = RequestOptions<T>;
695
+ type PostRequestOptions<T = any> = RequestOptions<T>;
696
+ //#endregion
697
+ //#region src/follow-promise.d.ts
698
+ /**
699
+ * Base interface for both FollowOne and FollowAll
700
+ */
701
+ declare abstract class FollowPromise<T> implements PromiseLike<T> {
702
+ protected prefetchEnabled: boolean;
703
+ protected preferTranscludeEnabled: boolean;
704
+ protected useHeadEnabled: boolean;
705
+ constructor();
706
+ preFetch(): this;
707
+ preferTransclude(): this;
708
+ /**
709
+ * Use a HTTP HEAD request to fetch the links.
710
+ *
711
+ * This is useful when interacting with servers that embed links in Link
712
+ * Headers.
713
+ */
714
+ useHead(): this;
715
+ abstract then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): PromiseLike<TResult1 | TResult2>;
716
+ abstract catch<TResult1 = T, TResult2 = never>(onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): PromiseLike<TResult1 | TResult2>;
717
+ }
718
+ /**
719
+ * The FollowPromise class is what's being returned from follow() functions.
720
+ *
721
+ * It's 'PromiseLike', which means you can treat it like a Promise, and it
722
+ * can be awaited. When used as a Promise, it resolves to the Resource object
723
+ * that was followed.
724
+ *
725
+ * In addition to being a Promise<Resource> stand-in, it also exposes other
726
+ * functions, namely:
727
+ *
728
+ * * `follow()` to allow a user to chain several follow() functions to do
729
+ * several 'hops' all at once.
730
+ * * `followAll()`, allowing a user to call `followAll()` at the end of a
731
+ * chain.
732
+ */
733
+ declare class FollowPromiseOne<T = any> extends FollowPromise<Resource<T>> {
734
+ private resource;
735
+ private rel;
736
+ private variables?;
737
+ constructor(resource: Resource | Promise<Resource>, rel: string, variables?: LinkVariables);
738
+ /**
739
+ * This 'then' function behaves like a Promise then() function.
740
+ *
741
+ * This method signature is pretty crazy, but trust that it's pretty much
742
+ * like any then() method on a promise.
743
+ */
744
+ then<TResult1 = Resource<T>, TResult2 = never>(onfulfilled?: ((value: Resource<T>) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: Error) => TResult2 | PromiseLike<TResult2>) | null | undefined): Promise<TResult1 | TResult2>;
745
+ /**
746
+ * This 'catch' function behaves like a Promise catch() function.
747
+ */
748
+ catch<TResult1 = any, TResult2 = never>(onrejected?: ((reason: Error) => TResult2 | PromiseLike<TResult2>) | null | undefined): Promise<TResult1 | TResult2>;
749
+ /**
750
+ * Implementation of a Promise.finally function
751
+ */
752
+ finally<TResult1 = any>(onfinally: () => TResult1 | PromiseLike<TResult1>): Promise<TResult1>;
753
+ /**
754
+ * Follow another link immediately after following this link.
755
+ *
756
+ * This allows you to follow several hops of links in one go.
757
+ *
758
+ * For example: resource.follow('foo').follow('bar');
759
+ */
760
+ follow<TNested = any>(rel: string, variables?: LinkVariables): FollowPromiseOne<TNested>;
761
+ /**
762
+ * Gets the current state of the resource.
763
+ *
764
+ * This function will return a State object.
765
+ */
766
+ get(getOptions?: GetRequestOptions): Promise<State<T>>;
767
+ /**
768
+ * Follows a set of links immediately after following this link.
769
+ *
770
+ * For example: resource.follow('foo').followAll('item');
771
+ */
772
+ followAll<TNested = any>(rel: string): FollowPromiseMany<TNested>;
773
+ /**
774
+ * This function does the actual fetching of the linked
775
+ * resource.
776
+ */
777
+ private fetchLinkedResource;
778
+ }
779
+ /**
780
+ */
781
+ declare class FollowPromiseMany<T = any> extends FollowPromise<Resource<T>[]> {
782
+ private resource;
783
+ private rel;
784
+ constructor(resource: Resource | Promise<Resource>, rel: string);
785
+ /**
786
+ * This 'then' function behaves like a Promise then() function.
787
+ */
788
+ then<TResult1 = Resource<T>[], TResult2 = never>(onfulfilled?: ((value: Resource<T>[]) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: Error) => TResult2 | PromiseLike<TResult2>) | null | undefined): Promise<TResult1 | TResult2>;
789
+ /**
790
+ * This 'catch' function behaves like a Promise catch() function.
791
+ */
792
+ catch<TResult1 = any, TResult2 = never>(onrejected?: ((reason: Error) => TResult2 | PromiseLike<TResult2>) | null | undefined): Promise<TResult1 | TResult2>;
793
+ /**
794
+ * Implementation of a Promise.finally function
795
+ */
796
+ finally<TResult1 = any>(onfinally: () => TResult1 | PromiseLike<TResult1>): Promise<TResult1>;
797
+ /**
798
+ * Gets the current states of the resources.
799
+ *
800
+ * This function will return an array of State object.
801
+ */
802
+ get(getOptions?: GetRequestOptions): Promise<State<T>[]>;
803
+ /**
804
+ * This function does the actual fetching, to obtained the url
805
+ * of the linked resource. It returns the Resource object.
806
+ */
807
+ private fetchLinkedResources;
808
+ }
809
+ //#endregion
810
+ //#region src/resource.d.ts
811
+ /**
812
+ * A 'resource' represents an endpoint on a server.
813
+ *
814
+ * A resource has a uri, methods that correspond to HTTP methods,
815
+ * and events to subscribe to state changes.
816
+ */
817
+ declare class Resource<T = any> extends EventEmitter {
818
+ /**
819
+ * URI of the current resource
820
+ */
821
+ uri: string;
822
+ /**
823
+ * Reference to the Client that created the resource
824
+ */
825
+ client: Client;
826
+ /**
827
+ * This object tracks all in-flight requests.
828
+ *
829
+ * When 2 identical requests are made in quick succession, this object is
830
+ * used to de-duplicate the requests.
831
+ */
832
+ private readonly activeRefresh;
833
+ /**
834
+ * Create the resource.
835
+ *
836
+ * This is usually done by the Client.
837
+ */
838
+ constructor(client: Client, uri: string);
839
+ /**
840
+ * Gets the current state of the resource.
841
+ *
842
+ * This function will return a State object.
843
+ */
844
+ get(getOptions?: GetRequestOptions): Promise<State<T>>;
845
+ /**
846
+ * Does a HEAD request and returns a HeadState object.
847
+ *
848
+ * If there was a valid existing cache for a GET request, it will
849
+ * still return that.
850
+ */
851
+ head(headOptions?: HeadRequestOptions): Promise<HeadState>;
852
+ /**
853
+ * Gets the current state of the resource, skipping
854
+ * the cache.
855
+ *
856
+ * This function will return a State object.
857
+ */
858
+ refresh(getOptions?: GetRequestOptions): Promise<State<T>>;
859
+ /**
860
+ * Updates the server state with a PUT request
861
+ */
862
+ put(options: PutRequestOptions<T> | State): Promise<void>;
863
+ /**
864
+ * Deletes the resource
865
+ */
866
+ delete(): Promise<void>;
867
+ /**
868
+ * Sends a POST request to the resource.
869
+ *
870
+ * See the documentation for PostRequestOptions for more details.
871
+ * This function is used for RPC-like endpoints and form submissions.
872
+ *
873
+ * This function will return the response as a State object.
874
+ */
875
+ post(options: PostRequestOptions): Promise<State>;
876
+ /**
877
+ * Sends a POST request, and follows to the next resource.
878
+ *
879
+ * If a server responds with a 201 Status code and a Location header,
880
+ * it will automatically return the newly created resource.
881
+ *
882
+ * If the server responded with a 204 or 205, this function will return
883
+ * `this`.
884
+ */
885
+ postFollow(options: PostRequestOptions): Promise<Resource>;
886
+ /**
887
+ * Sends a PATCH request to the resource.
888
+ *
889
+ * This function defaults to a application/json content-type header.
890
+ *
891
+ * If the server responds with 200 Status code this will return a State object
892
+ */
893
+ patch(options: PatchRequestOptions): Promise<undefined | State<T>>;
894
+ /**
895
+ * Follows a relationship, based on its reltype. For example, this might be
896
+ * 'alternate', 'item', 'edit' or a custom url-based one.
897
+ *
898
+ * This function can also follow templated uris. You can specify uri
899
+ * variables in the optional variables argument.
900
+ */
901
+ follow<TFollowedResource = any>(rel: string, variables?: LinkVariables): FollowPromiseOne<TFollowedResource>;
902
+ /**
903
+ * Follows a relationship based on its reltype. This function returns a
904
+ * Promise that resolves to an array of Resource objects.
905
+ *
906
+ * If no resources were found, the array will be empty.
907
+ */
908
+ followAll<TFollowedResource = any>(rel: string): FollowPromiseMany<TFollowedResource>;
909
+ /**
910
+ * Resolves a new resource based on a relative uri.
911
+ *
912
+ * Use this function to manually get a Resource object via a uri. The uri
913
+ * will be resolved based on the uri of the current resource.
914
+ *
915
+ * This function doesn't do any HTTP requests.
916
+ */
917
+ go<TGoResource = any>(uri: string | Link): Resource<TGoResource>;
918
+ /**
919
+ * Does a HTTP request on the current resource URI
920
+ */
921
+ fetch(init?: RequestInit): Promise<Response>;
922
+ /**
923
+ * Does a HTTP request on the current resource URI.
924
+ *
925
+ * If the response was a 4XX or 5XX, this function will throw
926
+ * an exception.
927
+ */
928
+ fetchOrThrow(init?: RequestInit): Promise<Response>;
929
+ /**
930
+ * Updates the state cache, and emits events.
931
+ *
932
+ * This will update the local state but *not* update the server
933
+ */
934
+ updateCache(state: State<T>): void;
935
+ /**
936
+ * Clears the state cache for this resource.
937
+ */
938
+ clearCache(): void;
939
+ /**
940
+ * Retrieves the current cached resource state, and return `null` if it's
941
+ * not available.
942
+ */
943
+ getCache(): State<T> | null;
944
+ /**
945
+ * Returns a Link object, by its REL.
946
+ *
947
+ * If the link does not exist, a LinkNotFound error will be thrown.
948
+ *
949
+ * @deprecated
950
+ */
951
+ link(rel: string): Promise<Link>;
952
+ /**
953
+ * Returns all links defined on this object.
954
+ *
955
+ * @deprecated
956
+ */
957
+ links(rel?: string): Promise<Link[]>;
958
+ /**
959
+ *
960
+ * Returns true or false depending on if a link with the specified relation
961
+ * type exists.
962
+ *
963
+ * @deprecated
964
+ */
965
+ hasLink(rel: string): Promise<boolean>;
966
+ }
967
+ declare interface Resource<T = any> {
968
+ /**
969
+ * Subscribe to the 'update' event.
970
+ *
971
+ * This event will get triggered whenever a new State is received
972
+ * from the server, either through a GET request or if it was
973
+ * transcluded.
974
+ *
975
+ * It will also trigger when calling 'PUT' with a full state object,
976
+ * and when updateCache() was used.
977
+ */
978
+ on(event: 'update', listener: (state: State) => void): this;
979
+ /**
980
+ * Subscribe to the 'stale' event.
981
+ *
982
+ * This event will get triggered whenever an unsafe method was
983
+ * used, such as POST, PUT, PATCH, etc.
984
+ *
985
+ * When any of these methods are used, the local cache is stale.
986
+ */
987
+ on(event: 'stale', listener: () => void): this;
988
+ /**
989
+ * Subscribe to the 'delete' event.
990
+ *
991
+ * This event gets triggered when the `DELETE` http method is used.
992
+ */
993
+ on(event: 'delete', listener: () => void): this;
994
+ /**
995
+ * Subscribe to the 'update' event and unsubscribe after it was
996
+ * emitted the first time.
997
+ */
998
+ once(event: 'update', listener: (state: State) => void): this;
999
+ /**
1000
+ * Subscribe to the 'stale' event and unsubscribe after it was
1001
+ * emitted the first time.
1002
+ */
1003
+ once(event: 'stale', listener: () => void): this;
1004
+ /**
1005
+ * Subscribe to the 'delete' event and unsubscribe after it was
1006
+ * emitted the first time.
1007
+ */
1008
+ once(event: 'delete', listener: () => void): this;
1009
+ /**
1010
+ * Unsubscribe from the 'update' event
1011
+ */
1012
+ off(event: 'update', listener: (state: State) => void): this;
1013
+ /**
1014
+ * Unsubscribe from the 'stale' event
1015
+ */
1016
+ off(event: 'stale', listener: () => void): this;
1017
+ /**
1018
+ * Unsubscribe from the 'delete' event
1019
+ */
1020
+ off(event: 'delete', listener: () => void): this;
1021
+ /**
1022
+ * Emit an 'update' event.
1023
+ */
1024
+ emit(event: 'update', state: State): boolean;
1025
+ /**
1026
+ * Emit a 'stale' event.
1027
+ */
1028
+ emit(event: 'stale'): boolean;
1029
+ /**
1030
+ * Emit a 'delete' event.
1031
+ */
1032
+ emit(event: 'delete'): boolean;
1033
+ }
1034
+ //#endregion
1035
+ //#region src/cache/forever.d.ts
1036
+ /**
1037
+ * The 'Forever' cache stores any State for as long as the application
1038
+ * lives.
1039
+ *
1040
+ * It is a good default for most applications, but it means that if
1041
+ * a resource was changed server-side, Ketting will not pick up that change
1042
+ * until something was done to expire caches.
1043
+ *
1044
+ * Executing an unsafe method, calling clearCache() on a resource, or
1045
+ * when a resource appears in Location, Content-Location, or "invalidates"
1046
+ * link relationships.
1047
+ */
1048
+ declare class ForeverCache implements StateCache {
1049
+ private cache;
1050
+ constructor();
1051
+ /**
1052
+ * Store a State object.
1053
+ *
1054
+ * This function will clone the state object before storing
1055
+ */
1056
+ store(state: State): void;
1057
+ /**
1058
+ * Retrieve a State object from the cache by its absolute uri
1059
+ */
1060
+ get(uri: string): State | null;
1061
+ /**
1062
+ * Return true if a State object with the specified uri exists in the cache
1063
+ */
1064
+ has(uri: string): boolean;
1065
+ /**
1066
+ * Delete a State object from the cache, by its uri
1067
+ */
1068
+ delete(uri: string): void;
1069
+ /**
1070
+ * Purge the entire cache
1071
+ */
1072
+ clear(): void;
1073
+ /**
1074
+ * Clean up any dangling references to avoid memory leaks.
1075
+ */
1076
+ destroy(): void;
1077
+ }
1078
+ //#endregion
1079
+ //#region src/cache/short.d.ts
1080
+ /**
1081
+ * ShortCache stores items in the cache for a short time.
1082
+ *
1083
+ * This cache can be a good choice if your server heavily relies
1084
+ * on HTTP cache headers and Ketting runs in your browser, or if in general
1085
+ * you want very up-to-date data.
1086
+ *
1087
+ * The reason in this scenarios it's useful to still have a 'very temporary'
1088
+ * cache, is because during many operations `get()` may be called in rapid
1089
+ * succession, and it also allows for enough time for 'embedded items' to
1090
+ * pe placed in the cache and extracted again.
1091
+ */
1092
+ declare class ShortCache extends ForeverCache {
1093
+ private cacheTimeout;
1094
+ private activeTimers;
1095
+ /**
1096
+ * Create the short cache.
1097
+ *
1098
+ * cacheTimeout is specified in ms.
1099
+ */
1100
+ constructor(cacheTimeout?: number);
1101
+ /**
1102
+ * Store a State object.
1103
+ *
1104
+ * This function will clone the state object before storing
1105
+ */
1106
+ store(state: State): void;
1107
+ private setTimer;
1108
+ /**
1109
+ * Clean up any dangling references to avoid memory leaks.
1110
+ */
1111
+ destroy(): void;
1112
+ }
1113
+ //#endregion
1114
+ //#region src/cache/never.d.ts
1115
+ /**
1116
+ * The NeverCache caches absolutely nothing.
1117
+ *
1118
+ * This should usually only be used in testing scenarios or if you really
1119
+ * know what you're doing.
1120
+ *
1121
+ * Using it could cause excessive requests, and will cause embedded items
1122
+ * to be ignored.
1123
+ */
1124
+ declare class NeverCache implements StateCache {
1125
+ /**
1126
+ * Store a State object.
1127
+ *
1128
+ * This function will clone the state object before storing
1129
+ */
1130
+ store(state: State): void;
1131
+ /**
1132
+ * Retrieve a State object from the cache by its absolute uri
1133
+ */
1134
+ get(uri: string): null;
1135
+ /**
1136
+ * Return true if a State object with the specified uri exists in the cache
1137
+ */
1138
+ has(uri: string): boolean;
1139
+ /**
1140
+ * Delete a State object from the cache, by its uri
1141
+ */
1142
+ delete(uri: string): void;
1143
+ /**
1144
+ * Purge the entire cache
1145
+ */
1146
+ clear(): void;
1147
+ }
1148
+ //#endregion
1149
+ //#region src/cache/index.d.ts
1150
+ /**
1151
+ * Cache interface
1152
+ *
1153
+ * The cache is responsible for storing 'state' objects
1154
+ */
1155
+ interface StateCache {
1156
+ /**
1157
+ * Store a State object.
1158
+ *
1159
+ * This function will clone the state object before storing
1160
+ */
1161
+ store: (state: State) => void;
1162
+ /**
1163
+ * Retrieve a State object from the cache by its absolute uri
1164
+ */
1165
+ get: (uri: string) => State | null;
1166
+ /**
1167
+ * Return true if a State object with the specified uri exists in the cache
1168
+ */
1169
+ has: (uri: string) => boolean;
1170
+ /**
1171
+ * Delete a State object from the cache, by its uri
1172
+ */
1173
+ delete: (uri: string) => void;
1174
+ /**
1175
+ * Purge the entire cache
1176
+ */
1177
+ clear: () => void;
1178
+ }
1179
+ //#endregion
1180
+ //#region src/client.d.ts
1181
+ declare class Client {
1182
+ /**
1183
+ * All relative urls will by default use the bookmarkUri to
1184
+ * expand. It should usually be the starting point of your
1185
+ * API
1186
+ */
1187
+ bookmarkUri: string;
1188
+ /**
1189
+ * Supported content types
1190
+ *
1191
+ * Each content-type has a 'factory' that turns a HTTP response
1192
+ * into a State object.
1193
+ *
1194
+ * The last value in the array is the 'q=' value, used in Accept
1195
+ * headers. Higher means higher priority.
1196
+ */
1197
+ contentTypeMap: {
1198
+ [mimeType: string]: [StateFactory<any>, string];
1199
+ };
1200
+ /**
1201
+ * The cache for 'State' objects
1202
+ */
1203
+ cache: StateCache;
1204
+ /**
1205
+ * The cache for 'Resource' objects. Each unique uri should
1206
+ * only ever get 1 associated resource.
1207
+ */
1208
+ resources: Map<string, Resource>;
1209
+ /**
1210
+ * Fetcher is a utility object that handles fetch() requests
1211
+ * and middlewares.
1212
+ */
1213
+ fetcher: Fetcher;
1214
+ constructor(bookmarkUri: string);
1215
+ /**
1216
+ * Follows a relationship, based on its reltype. For example, this might be
1217
+ * 'alternate', 'item', 'edit' or a custom url-based one.
1218
+ *
1219
+ * This function can also follow templated uris. You can specify uri
1220
+ * variables in the optional variables argument.
1221
+ */
1222
+ follow<TFollowedResource = any>(rel: string, variables?: LinkVariables): FollowPromiseOne<TFollowedResource>;
1223
+ /**
1224
+ * Returns a resource by its uri.
1225
+ *
1226
+ * This function doesn't do any HTTP requests. The uri is optional. If it's
1227
+ * not specified, it will return the bookmark resource.
1228
+ *
1229
+ * If a relative uri is passed, it will be resolved based on the bookmark
1230
+ * uri.
1231
+ *
1232
+ * @example
1233
+ * const res = ketting.go('https://example.org/);
1234
+ * @example
1235
+ * const res = ketting.go<Author>('/users/1');
1236
+ * @example
1237
+ * const res = ketting.go(); // bookmark
1238
+ */
1239
+ go<TResource = any>(uri?: string | Link): Resource<TResource>;
1240
+ /**
1241
+ * Adds a fetch middleware, which will be executed for
1242
+ * each fetch() call.
1243
+ *
1244
+ * If 'origin' is specified, fetch middlewares can be executed
1245
+ * only if the host/origin matches.
1246
+ */
1247
+ use(middleware: FetchMiddleware, origin?: string): void;
1248
+ /**
1249
+ * Clears the entire state cache
1250
+ */
1251
+ clearCache(): void;
1252
+ /**
1253
+ * Caches a State object
1254
+ *
1255
+ * This function will also emit 'update' events to resources, and store all
1256
+ * embedded states.
1257
+ */
1258
+ cacheState(state: State): void;
1259
+ /**
1260
+ * cacheDependencies contains all cache relationships between
1261
+ * resources.
1262
+ *
1263
+ * This lets a user (for example) let a resource automatically
1264
+ * expire, if another one expires.
1265
+ *
1266
+ * A server can populate this list using the `inv-by' link.
1267
+ *
1268
+ * @deprecated This property will go private in a future release.
1269
+ */
1270
+ cacheDependencies: Map<string, Set<string>>;
1271
+ /**
1272
+ * Adds a cache dependency between two resources.
1273
+ *
1274
+ * If the 'target' resource ever expires, it will cause 'dependentUri' to
1275
+ * also expire.
1276
+ *
1277
+ * Both argument MUST be absolute urls.
1278
+ */
1279
+ addCacheDependency(targetUri: string, dependentUri: string): void;
1280
+ /**
1281
+ * Helper function for clearing the cache for a resource.
1282
+ *
1283
+ * This function will also emit the 'stale' event for resources that have
1284
+ * subscribers, and handle any dependent resource caches.
1285
+ *
1286
+ * If any resources are specified in deletedUris, those will not
1287
+ * receive 'stale' events, but 'delete' events instead.
1288
+ */
1289
+ clearResourceCache(staleUris: string[], deletedUris: string[]): void;
1290
+ /**
1291
+ * Transforms a fetch Response to a State object.
1292
+ */
1293
+ getStateForResponse(uri: string, response: Response): Promise<State>;
1294
+ }
1295
+ //#endregion
1296
+ //#region src/util/uri.d.ts
1297
+ /**
1298
+ * Resolves a relative url using another url.
1299
+ *
1300
+ * This is the node.js version.
1301
+ */
1302
+ declare function resolve(base: string, relative: string): string;
1303
+ declare function resolve(link: Link): string;
1304
+ //#endregion
1305
+ //#region src/util/uri-template.d.ts
1306
+ declare function expand(context: string, template: string, vars: LinkVariables): string;
1307
+ declare function expand(link: Link, vars: LinkVariables): string;
1308
+ //#endregion
1309
+ //#region src/http/basic-auth.d.ts
1310
+ declare const _default: (userName: string, password: string) => FetchMiddleware;
1311
+ //#endregion
1312
+ //#region src/http/bearer-auth.d.ts
1313
+ declare const _default$1: (token: string) => FetchMiddleware;
1314
+ //#endregion
1315
+ //#region src/http/oauth2.d.ts
1316
+ declare function oauth2mw(oauth2Options: OAuth2Options, token?: OAuth2Token): FetchMiddleware;
1317
+ /**
1318
+ * Token information
1319
+ */
1320
+ type OAuth2Token = {
1321
+ /**
1322
+ * OAuth2 Access Token
1323
+ */
1324
+ accessToken: string;
1325
+ /**
1326
+ * When the Access Token expires.
1327
+ *
1328
+ * This is expressed as a unix timestamp in milliseconds.
1329
+ */
1330
+ expiresAt: number | null;
1331
+ /**
1332
+ * OAuth2 refresh token
1333
+ */
1334
+ refreshToken: string | null;
1335
+ };
1336
+ /**
1337
+ * grant_type=password
1338
+ */
1339
+ type PasswordGrantOptions = {
1340
+ grantType: 'password';
1341
+ /**
1342
+ * OAuth2 client id
1343
+ */
1344
+ clientId: string;
1345
+ /**
1346
+ * OAuth2 Client Secret
1347
+ */
1348
+ clientSecret: string;
1349
+ /**
1350
+ * OAuth2 token endpoint
1351
+ */
1352
+ tokenEndpoint: string;
1353
+ /**
1354
+ * List of OAuth2 scopes
1355
+ */
1356
+ scope?: string[];
1357
+ /**
1358
+ * Username to log in as
1359
+ */
1360
+ userName: string;
1361
+ /**
1362
+ * Password
1363
+ */
1364
+ password: string;
1365
+ /**
1366
+ * Callback to trigger when a new access/refresh token pair was obtained.
1367
+ */
1368
+ onTokenUpdate?: (token: OAuth2Token) => void;
1369
+ /**
1370
+ * If authentication fails without a chance of recovery, this gets triggered.
1371
+ *
1372
+ * This is used for example when your resource server returns a 401, but only after
1373
+ * other attempts have been made to reauthenticate (such as a token refresh).
1374
+ */
1375
+ onAuthError?: (error: Error) => void;
1376
+ };
1377
+ /**
1378
+ * grant_type=client_credentials
1379
+ */
1380
+ type ClientCredentialsGrantOptions = {
1381
+ grantType: 'client_credentials';
1382
+ /**
1383
+ * OAuth2 client id
1384
+ */
1385
+ clientId: string;
1386
+ /**
1387
+ * OAuth2 Client Secret
1388
+ */
1389
+ clientSecret: string;
1390
+ /**
1391
+ * OAuth2 token endpoint
1392
+ */
1393
+ tokenEndpoint: string;
1394
+ /**
1395
+ * List of OAuth2 scopes
1396
+ */
1397
+ scope?: string[];
1398
+ /**
1399
+ * Callback to trigger when a new access/refresh token pair was obtained.
1400
+ */
1401
+ onTokenUpdate?: (token: OAuth2Token) => void;
1402
+ /**
1403
+ * If authentication fails without a chance of recovery, this gets triggered.
1404
+ *
1405
+ * This is used for example when your resource server returns a 401, but only after
1406
+ * other attempts have been made to reauthenticate (such as a token refresh).
1407
+ */
1408
+ onAuthError?: (error: Error) => void;
1409
+ };
1410
+ /**
1411
+ * grant_type=authorization_code
1412
+ */
1413
+ type AuthorizationCodeGrantOptions = {
1414
+ grantType: 'authorization_code';
1415
+ /**
1416
+ * OAuth2 client id
1417
+ */
1418
+ clientId: string;
1419
+ /**
1420
+ * OAuth2 token endpoint
1421
+ */
1422
+ tokenEndpoint: string;
1423
+ /**
1424
+ * The redirect_uri that was passed originally to the 'authorization' endpoint.
1425
+ *
1426
+ * This must be identical to the original string, as conforming OAuth2 servers
1427
+ * will validate this.
1428
+ */
1429
+ redirectUri: string;
1430
+ /**
1431
+ * Code that was obtained from the authorization endpoint
1432
+ */
1433
+ code: string;
1434
+ /**
1435
+ * Callback to trigger when a new access/refresh token pair was obtained.
1436
+ */
1437
+ onTokenUpdate?: (token: OAuth2Token) => void;
1438
+ /**
1439
+ * If authentication fails without a chance of recovery, this gets triggered.
1440
+ *
1441
+ * This is used for example when your resource server returns a 401, but only after
1442
+ * other attempts have been made to reauthenticate (such as a token refresh).
1443
+ */
1444
+ onAuthError?: (error: Error) => void;
1445
+ /**
1446
+ * When using PKCE, specify the previously generated code verifier here.
1447
+ */
1448
+ codeVerifier?: string;
1449
+ };
1450
+ /**
1451
+ * In case you obtained an access token and/or refresh token through different
1452
+ * means, you can not specify a grant_type and simply only specify an access
1453
+ * and refresh token.
1454
+ *
1455
+ * If a refresh or tokenEndpoint are not supplied, the token will never get refreshed.
1456
+ */
1457
+ type RefreshOnlyGrantOptions = {
1458
+ grantType: undefined;
1459
+ /**
1460
+ * OAuth2 client id
1461
+ */
1462
+ clientId: string;
1463
+ tokenEndpoint: string;
1464
+ /**
1465
+ * Callback to trigger when a new access/refresh token pair was obtained.
1466
+ */
1467
+ onTokenUpdate?: (token: OAuth2Token) => void;
1468
+ /**
1469
+ * If authentication fails without a chance of recovery, this gets triggered.
1470
+ *
1471
+ * This is used for example when your resource server returns a 401, but only after
1472
+ * other attempts have been made to reauthenticate (such as a token refresh).
1473
+ */
1474
+ onAuthError?: (error: Error) => void;
1475
+ };
1476
+ type OAuth2Options = PasswordGrantOptions | ClientCredentialsGrantOptions | AuthorizationCodeGrantOptions | RefreshOnlyGrantOptions;
1477
+ //#endregion
1478
+ //#region src/http/error.d.ts
1479
+ /**
1480
+ * HttpError extends the Error object, and is thrown wheenever servers emit
1481
+ * HTTP errors.
1482
+ *
1483
+ * It has a response property, allowing users to find out more about the
1484
+ * nature of the error.
1485
+ */
1486
+ declare class HttpError extends Error {
1487
+ response: Response;
1488
+ status: number;
1489
+ constructor(response: Response);
1490
+ }
1491
+ /**
1492
+ * Problem extends the HttpError object. If a server emits a HTTP error, and
1493
+ * the response body's content-type is application/problem+json.
1494
+ *
1495
+ * application/problem+json is defined in RFC7807 and provides a standardized
1496
+ * way to describe error conditions by a HTTP server.
1497
+ */
1498
+ declare class Problem extends HttpError {
1499
+ body: {
1500
+ type: string;
1501
+ title?: string;
1502
+ status: number;
1503
+ detail?: string;
1504
+ instance?: string;
1505
+ [x: string]: any;
1506
+ };
1507
+ constructor(response: Response, problemBody: Record<string, any>);
1508
+ }
1509
+ //#endregion
1510
+ export { type Action, BaseHeadState, BaseState, type BasicStringField, type BooleanField, CjState, Client, Client as Ketting, Client as default, type DateTimeField, type FetchMiddleware, type Field, type FileField, FollowPromiseMany, FollowPromiseOne, ForeverCache, HalState, type HiddenField, type Link, LinkNotFound, type LinkVariables, Links, NeverCache, type NumberField, Problem, type RangeStringField, Resource, type SelectFieldMulti, type SelectFieldSingle, ShortCache, SirenState, type State, type StateCache, type TextAreaField, type TextField, _default as basicAuth, _default$1 as bearerAuth, expand, isState, oauth2mw as oauth2, resolve };
1511
+ //# sourceMappingURL=index.d.mts.map