@twin.org/web 0.0.3-next.3 → 0.0.3-next.30

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 (52) hide show
  1. package/README.md +1 -1
  2. package/dist/es/index.js +3 -0
  3. package/dist/es/index.js.map +1 -1
  4. package/dist/es/models/IHttpLinkHeader.js +2 -0
  5. package/dist/es/models/IHttpLinkHeader.js.map +1 -0
  6. package/dist/es/models/headerTypes.js +129 -1
  7. package/dist/es/models/headerTypes.js.map +1 -1
  8. package/dist/es/models/httpLinkRelType.js +130 -0
  9. package/dist/es/models/httpLinkRelType.js.map +1 -0
  10. package/dist/es/models/httpMethod.js +36 -0
  11. package/dist/es/models/httpMethod.js.map +1 -1
  12. package/dist/es/models/httpStatusCode.js +63 -0
  13. package/dist/es/models/httpStatusCode.js.map +1 -1
  14. package/dist/es/models/mimeTypes.js +170 -1
  15. package/dist/es/models/mimeTypes.js.map +1 -1
  16. package/dist/es/utils/cookieHelper.js +83 -0
  17. package/dist/es/utils/cookieHelper.js.map +1 -0
  18. package/dist/es/utils/headerHelper.js +383 -1
  19. package/dist/es/utils/headerHelper.js.map +1 -1
  20. package/dist/es/utils/mimeTypeHelper.js +42 -1
  21. package/dist/es/utils/mimeTypeHelper.js.map +1 -1
  22. package/dist/types/index.d.ts +3 -0
  23. package/dist/types/models/IHttpLinkHeader.d.ts +26 -0
  24. package/dist/types/models/headerTypes.d.ts +128 -0
  25. package/dist/types/models/httpLinkRelType.d.ts +130 -0
  26. package/dist/types/models/httpMethod.d.ts +36 -0
  27. package/dist/types/models/httpStatusCode.d.ts +63 -0
  28. package/dist/types/models/mimeTypes.d.ts +169 -0
  29. package/dist/types/utils/cookieHelper.d.ts +49 -0
  30. package/dist/types/utils/headerHelper.d.ts +115 -0
  31. package/docs/changelog.md +592 -1
  32. package/docs/examples.md +108 -1
  33. package/docs/reference/classes/CookieHelper.md +155 -0
  34. package/docs/reference/classes/FetchError.md +453 -1
  35. package/docs/reference/classes/FetchHelper.md +12 -12
  36. package/docs/reference/classes/HeaderHelper.md +385 -2
  37. package/docs/reference/classes/Jwk.md +6 -6
  38. package/docs/reference/classes/Jws.md +3 -3
  39. package/docs/reference/classes/Jwt.md +17 -17
  40. package/docs/reference/classes/MimeTypeHelper.md +4 -4
  41. package/docs/reference/index.md +4 -0
  42. package/docs/reference/interfaces/IFetchOptions.md +12 -12
  43. package/docs/reference/interfaces/IHttpHeaders.md +1 -1
  44. package/docs/reference/interfaces/IHttpLinkHeader.md +43 -0
  45. package/docs/reference/type-aliases/HttpLinkRelType.md +5 -0
  46. package/docs/reference/variables/HeaderTypes.md +280 -8
  47. package/docs/reference/variables/HttpLinkRelType.md +191 -0
  48. package/docs/reference/variables/HttpMethod.md +63 -9
  49. package/docs/reference/variables/HttpStatusCode.md +314 -62
  50. package/docs/reference/variables/MimeTypes.md +279 -23
  51. package/locales/en.json +4 -0
  52. package/package.json +6 -6
package/docs/examples.md CHANGED
@@ -1 +1,108 @@
1
- # @twin.org/web - Examples
1
+ # Web Examples
2
+
3
+ Use these snippets for token handling, fetch utilities and HTTP header helpers in browser and server runtimes.
4
+
5
+ ## Jwt
6
+
7
+ ```typescript
8
+ import { Jwt } from '@twin.org/web';
9
+
10
+ const payload = {
11
+ sub: 'user-01',
12
+ role: 'editor'
13
+ };
14
+
15
+ const token = Jwt.encode(payload, {
16
+ alg: 'HS256'
17
+ });
18
+
19
+ Jwt.decode(token).sub; // 'user-01'
20
+ ```
21
+
22
+ ```typescript
23
+ import { Jwt } from '@twin.org/web';
24
+
25
+ const token = Jwt.encode({ sub: 'u-2' }, { alg: 'HS256' });
26
+ Jwt.verify(token, {
27
+ key: 'local-signing-key'
28
+ }); // true
29
+ ```
30
+
31
+ ## FetchHelper
32
+
33
+ ```typescript
34
+ import { FetchHelper } from '@twin.org/web';
35
+
36
+ const response = await FetchHelper.fetchJson<{ status: string }>('https://api.example.org/status', {
37
+ method: 'GET',
38
+ cacheSeconds: 30
39
+ });
40
+
41
+ response.status; // 'ok'
42
+ ```
43
+
44
+ ## HeaderHelper
45
+
46
+ ```typescript
47
+ import { HeaderHelper } from '@twin.org/web';
48
+
49
+ const header = HeaderHelper.createBearer('token-123');
50
+
51
+ HeaderHelper.extractBearer(header); // 'token-123'
52
+ ```
53
+
54
+ ## Jwk and Jws
55
+
56
+ ```typescript
57
+ import { Jwk, Jws } from '@twin.org/web';
58
+
59
+ const jwk = Jwk.fromEd25519Public(new Uint8Array(32));
60
+ const compact = Jws.create(
61
+ {
62
+ typ: 'JWT',
63
+ alg: 'EdDSA'
64
+ },
65
+ {
66
+ iss: 'issuer-1'
67
+ },
68
+ async signingBytes => signingBytes
69
+ );
70
+
71
+ Jws.verify(compact, async () => true); // true
72
+ jwk.kty; // 'OKP'
73
+ ```
74
+
75
+ ## CookieHelper
76
+
77
+ ```typescript
78
+ import { CookieHelper } from '@twin.org/web';
79
+
80
+ CookieHelper.createCookie('sessionId', 'abc123', {
81
+ path: '/',
82
+ maxAge: 1800,
83
+ secure: true,
84
+ httpOnly: true
85
+ }); // 'sessionId=abc123; Max-Age=1800; Path=/; Secure; HttpOnly'
86
+ ```
87
+
88
+ ## MimeTypeHelper
89
+
90
+ ```typescript
91
+ import { MimeTypeHelper } from '@twin.org/web';
92
+
93
+ MimeTypeHelper.detect('avatar.png'); // 'image/png'
94
+ MimeTypeHelper.defaultExtension('application/json'); // 'json'
95
+ ```
96
+
97
+ ## FetchError
98
+
99
+ ```typescript
100
+ import { FetchError } from '@twin.org/web';
101
+
102
+ const error = new FetchError(this.CLASS_NAME, 'fetchFailed', {
103
+ statusCode: 503,
104
+ retryable: true
105
+ });
106
+
107
+ error.toJsonObject().statusCode; // 503
108
+ ```
@@ -0,0 +1,155 @@
1
+ # Class: CookieHelper
2
+
3
+ Class to help with cookie operations.
4
+
5
+ ## Constructors
6
+
7
+ ### Constructor
8
+
9
+ > **new CookieHelper**(): `CookieHelper`
10
+
11
+ #### Returns
12
+
13
+ `CookieHelper`
14
+
15
+ ## Properties
16
+
17
+ ### CLASS\_NAME {#class_name}
18
+
19
+ > `readonly` `static` **CLASS\_NAME**: `string`
20
+
21
+ Runtime name for the class.
22
+
23
+ ## Methods
24
+
25
+ ### createCookie() {#createcookie}
26
+
27
+ > `static` **createCookie**(`cookieName`, `cookieValue`, `options?`): `string`
28
+
29
+ Create a cookie string.
30
+
31
+ #### Parameters
32
+
33
+ ##### cookieName
34
+
35
+ `string`
36
+
37
+ The name of the cookie.
38
+
39
+ ##### cookieValue
40
+
41
+ `string`
42
+
43
+ The value of the cookie.
44
+
45
+ ##### options?
46
+
47
+ Additional cookie options.
48
+
49
+ ###### secure?
50
+
51
+ `boolean`
52
+
53
+ Should this be a secure cookie.
54
+
55
+ ###### httpOnly?
56
+
57
+ `boolean`
58
+
59
+ Should this be an http only cookie.
60
+
61
+ ###### sameSite?
62
+
63
+ `"Strict"` \| `"Lax"` \| `"None"`
64
+
65
+ The same site option for the cookie.
66
+
67
+ ###### path?
68
+
69
+ `string`
70
+
71
+ The path for the cookie.
72
+
73
+ #### Returns
74
+
75
+ `string`
76
+
77
+ The created cookie string.
78
+
79
+ ***
80
+
81
+ ### deleteCookie() {#deletecookie}
82
+
83
+ > `static` **deleteCookie**(`cookieName`, `options?`): `string`
84
+
85
+ Create a cookie string which will delete a cookie.
86
+
87
+ #### Parameters
88
+
89
+ ##### cookieName
90
+
91
+ `string`
92
+
93
+ The name of the cookie.
94
+
95
+ ##### options?
96
+
97
+ Additional cookie options.
98
+
99
+ ###### secure?
100
+
101
+ `boolean`
102
+
103
+ Should this be a secure cookie.
104
+
105
+ ###### httpOnly?
106
+
107
+ `boolean`
108
+
109
+ Should this be an http only cookie.
110
+
111
+ ###### sameSite?
112
+
113
+ `"Strict"` \| `"Lax"` \| `"None"`
114
+
115
+ The same site option for the cookie.
116
+
117
+ ###### path?
118
+
119
+ `string`
120
+
121
+ The path for the cookie.
122
+
123
+ #### Returns
124
+
125
+ `string`
126
+
127
+ The created cookie string.
128
+
129
+ ***
130
+
131
+ ### getCookieFromHeaders() {#getcookiefromheaders}
132
+
133
+ > `static` **getCookieFromHeaders**(`headers`, `cookieName`): `string` \| `undefined`
134
+
135
+ Get cookies from headers.
136
+
137
+ #### Parameters
138
+
139
+ ##### headers
140
+
141
+ `string` \| `string`[] \| `undefined`
142
+
143
+ The headers to get cookies from.
144
+
145
+ ##### cookieName
146
+
147
+ `string`
148
+
149
+ The name of the cookie to get.
150
+
151
+ #### Returns
152
+
153
+ `string` \| `undefined`
154
+
155
+ The cookies found in the headers.
@@ -54,8 +54,460 @@ The cause of the error if we have wrapped another error.
54
54
 
55
55
  ## Properties
56
56
 
57
- ### CLASS\_NAME
57
+ ### source? {#source}
58
+
59
+ > `optional` **source?**: `string`
60
+
61
+ The source of the error.
62
+
63
+ #### Inherited from
64
+
65
+ `BaseError.source`
66
+
67
+ ***
68
+
69
+ ### properties? {#properties}
70
+
71
+ > `optional` **properties?**: `object`
72
+
73
+ Any additional information for the error.
74
+
75
+ #### Index Signature
76
+
77
+ \[`id`: `string`\]: `unknown`
78
+
79
+ #### Inherited from
80
+
81
+ `BaseError.properties`
82
+
83
+ ***
84
+
85
+ ### cause? {#cause}
86
+
87
+ > `optional` **cause?**: `IError`
88
+
89
+ The cause of the error.
90
+
91
+ #### Inherited from
92
+
93
+ `BaseError.cause`
94
+
95
+ ***
96
+
97
+ ### CLASS\_NAME {#class_name}
58
98
 
59
99
  > `readonly` `static` **CLASS\_NAME**: `string`
60
100
 
61
101
  Runtime name for the class.
102
+
103
+ ## Methods
104
+
105
+ ### fromError() {#fromerror}
106
+
107
+ > `static` **fromError**(`err`): `BaseError`
108
+
109
+ Construct an error from an existing one.
110
+
111
+ #### Parameters
112
+
113
+ ##### err
114
+
115
+ `unknown`
116
+
117
+ The existing error.
118
+
119
+ #### Returns
120
+
121
+ `BaseError`
122
+
123
+ The new instance.
124
+
125
+ #### Inherited from
126
+
127
+ `BaseError.fromError`
128
+
129
+ ***
130
+
131
+ ### flatten() {#flatten}
132
+
133
+ > `static` **flatten**(`err`): `IError`[]
134
+
135
+ Flatten an error tree.
136
+
137
+ #### Parameters
138
+
139
+ ##### err
140
+
141
+ `unknown`
142
+
143
+ The starting error.
144
+
145
+ #### Returns
146
+
147
+ `IError`[]
148
+
149
+ The list of all internal errors.
150
+
151
+ #### Inherited from
152
+
153
+ `BaseError.flatten`
154
+
155
+ ***
156
+
157
+ ### expand() {#expand}
158
+
159
+ > `static` **expand**(`errors`): `IError` \| `undefined`
160
+
161
+ Expand an error tree.
162
+
163
+ #### Parameters
164
+
165
+ ##### errors
166
+
167
+ `IError`[] \| `undefined`
168
+
169
+ The list of errors to expand.
170
+
171
+ #### Returns
172
+
173
+ `IError` \| `undefined`
174
+
175
+ The first level error.
176
+
177
+ #### Inherited from
178
+
179
+ `BaseError.expand`
180
+
181
+ ***
182
+
183
+ ### isErrorName() {#iserrorname}
184
+
185
+ > `static` **isErrorName**(`error`, `name`): `error is BaseError`
186
+
187
+ Test to see if the error has the specified error name.
188
+
189
+ #### Parameters
190
+
191
+ ##### error
192
+
193
+ `unknown`
194
+
195
+ The error to test.
196
+
197
+ ##### name
198
+
199
+ `string` \| `RegExp`
200
+
201
+ The name to check for.
202
+
203
+ #### Returns
204
+
205
+ `error is BaseError`
206
+
207
+ True if the error has the name.
208
+
209
+ #### Inherited from
210
+
211
+ `BaseError.isErrorName`
212
+
213
+ ***
214
+
215
+ ### isErrorMessage() {#iserrormessage}
216
+
217
+ > `static` **isErrorMessage**(`error`, `message`): `error is BaseError`
218
+
219
+ Test to see if the error has the specified error message.
220
+
221
+ #### Parameters
222
+
223
+ ##### error
224
+
225
+ `unknown`
226
+
227
+ The error to test.
228
+
229
+ ##### message
230
+
231
+ `string` \| `RegExp`
232
+
233
+ The message to check for.
234
+
235
+ #### Returns
236
+
237
+ `error is BaseError`
238
+
239
+ True if the error has the name.
240
+
241
+ #### Inherited from
242
+
243
+ `BaseError.isErrorMessage`
244
+
245
+ ***
246
+
247
+ ### isErrorCode() {#iserrorcode}
248
+
249
+ > `static` **isErrorCode**(`error`, `code`): `boolean`
250
+
251
+ Test to see if the error has the specified error code.
252
+
253
+ #### Parameters
254
+
255
+ ##### error
256
+
257
+ `unknown`
258
+
259
+ The error to test.
260
+
261
+ ##### code
262
+
263
+ `string` \| `RegExp`
264
+
265
+ The code to check for.
266
+
267
+ #### Returns
268
+
269
+ `boolean`
270
+
271
+ True if the error has the code.
272
+
273
+ #### Inherited from
274
+
275
+ `BaseError.isErrorCode`
276
+
277
+ ***
278
+
279
+ ### someErrorName() {#someerrorname}
280
+
281
+ > `static` **someErrorName**(`error`, `name`): `error is BaseError`
282
+
283
+ Test to see if any of the errors or children have the given error name.
284
+
285
+ #### Parameters
286
+
287
+ ##### error
288
+
289
+ `unknown`
290
+
291
+ The error to test.
292
+
293
+ ##### name
294
+
295
+ `string` \| `RegExp`
296
+
297
+ The name to check for.
298
+
299
+ #### Returns
300
+
301
+ `error is BaseError`
302
+
303
+ True if the error has the name.
304
+
305
+ #### Inherited from
306
+
307
+ `BaseError.someErrorName`
308
+
309
+ ***
310
+
311
+ ### someErrorMessage() {#someerrormessage}
312
+
313
+ > `static` **someErrorMessage**(`error`, `message`): `error is BaseError`
314
+
315
+ Test to see if any of the errors or children have the given error message.
316
+
317
+ #### Parameters
318
+
319
+ ##### error
320
+
321
+ `unknown`
322
+
323
+ The error to test.
324
+
325
+ ##### message
326
+
327
+ `string` \| `RegExp`
328
+
329
+ The message to check for.
330
+
331
+ #### Returns
332
+
333
+ `error is BaseError`
334
+
335
+ True if the error has the name.
336
+
337
+ #### Inherited from
338
+
339
+ `BaseError.someErrorMessage`
340
+
341
+ ***
342
+
343
+ ### someErrorClass() {#someerrorclass}
344
+
345
+ > `static` **someErrorClass**(`error`, `cls`): `error is BaseError`
346
+
347
+ Test to see if any of the errors or children are from a specific class.
348
+
349
+ #### Parameters
350
+
351
+ ##### error
352
+
353
+ `unknown`
354
+
355
+ The error to test.
356
+
357
+ ##### cls
358
+
359
+ `string`
360
+
361
+ The class to check for.
362
+
363
+ #### Returns
364
+
365
+ `error is BaseError`
366
+
367
+ True if the error has the specific class.
368
+
369
+ #### Inherited from
370
+
371
+ `BaseError.someErrorClass`
372
+
373
+ ***
374
+
375
+ ### someErrorCode() {#someerrorcode}
376
+
377
+ > `static` **someErrorCode**(`error`, `code`): `error is BaseError`
378
+
379
+ Test to see if any of the errors or children have the given error code.
380
+
381
+ #### Parameters
382
+
383
+ ##### error
384
+
385
+ `unknown`
386
+
387
+ The error to test.
388
+
389
+ ##### code
390
+
391
+ `string` \| `RegExp`
392
+
393
+ The code to check for.
394
+
395
+ #### Returns
396
+
397
+ `error is BaseError`
398
+
399
+ True if the error has the name.
400
+
401
+ #### Inherited from
402
+
403
+ `BaseError.someErrorCode`
404
+
405
+ ***
406
+
407
+ ### isEmpty() {#isempty}
408
+
409
+ > `static` **isEmpty**(`err`): `boolean`
410
+
411
+ Is the error empty, i.e. does it have no message, source, properties, or cause?
412
+
413
+ #### Parameters
414
+
415
+ ##### err
416
+
417
+ `IError`
418
+
419
+ The error to check for being empty.
420
+
421
+ #### Returns
422
+
423
+ `boolean`
424
+
425
+ True if the error is empty.
426
+
427
+ #### Inherited from
428
+
429
+ `BaseError.isEmpty`
430
+
431
+ ***
432
+
433
+ ### isAggregateError() {#isaggregateerror}
434
+
435
+ > `static` **isAggregateError**(`err`): `err is AggregateError`
436
+
437
+ Is the error an aggregate error.
438
+
439
+ #### Parameters
440
+
441
+ ##### err
442
+
443
+ `unknown`
444
+
445
+ The error to check for being an aggregate error.
446
+
447
+ #### Returns
448
+
449
+ `err is AggregateError`
450
+
451
+ True if the error is an aggregate error.
452
+
453
+ #### Inherited from
454
+
455
+ `BaseError.isAggregateError`
456
+
457
+ ***
458
+
459
+ ### fromAggregate() {#fromaggregate}
460
+
461
+ > `static` **fromAggregate**(`err`, `includeStackTrace?`): `IError`[]
462
+
463
+ Convert the aggregate error to an array of errors.
464
+
465
+ #### Parameters
466
+
467
+ ##### err
468
+
469
+ `unknown`
470
+
471
+ The error to convert.
472
+
473
+ ##### includeStackTrace?
474
+
475
+ `boolean`
476
+
477
+ Whether to include the error stack in the model, defaults to false.
478
+
479
+ #### Returns
480
+
481
+ `IError`[]
482
+
483
+ The array of errors.
484
+
485
+ #### Inherited from
486
+
487
+ `BaseError.fromAggregate`
488
+
489
+ ***
490
+
491
+ ### toJsonObject() {#tojsonobject}
492
+
493
+ > **toJsonObject**(`includeStackTrace?`): `IError`
494
+
495
+ Serialize the error to the error model.
496
+
497
+ #### Parameters
498
+
499
+ ##### includeStackTrace?
500
+
501
+ `boolean`
502
+
503
+ Whether to include the error stack in the model, defaults to false.
504
+
505
+ #### Returns
506
+
507
+ `IError`
508
+
509
+ The error model.
510
+
511
+ #### Inherited from
512
+
513
+ `BaseError.toJsonObject`