@twin.org/web 0.0.1-next.1

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 (43) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +21 -0
  3. package/dist/cjs/index.cjs +1056 -0
  4. package/dist/esm/index.mjs +1046 -0
  5. package/dist/types/errors/fetchError.d.ts +22 -0
  6. package/dist/types/index.d.ts +14 -0
  7. package/dist/types/models/IFetchOptions.d.ts +30 -0
  8. package/dist/types/models/IHttpHeaders.d.ts +6 -0
  9. package/dist/types/models/IJwk.d.ts +62 -0
  10. package/dist/types/models/IJwtHeader.d.ts +22 -0
  11. package/dist/types/models/IJwtPayload.d.ts +37 -0
  12. package/dist/types/models/headerTypes.d.ts +41 -0
  13. package/dist/types/models/httpMethod.d.ts +18 -0
  14. package/dist/types/models/httpStatusCode.d.ts +257 -0
  15. package/dist/types/models/jwtAlgorithms.d.ts +17 -0
  16. package/dist/types/models/mimeTypes.d.ts +93 -0
  17. package/dist/types/utils/fetchHelper.d.ts +52 -0
  18. package/dist/types/utils/jwt.d.ts +85 -0
  19. package/dist/types/utils/mimeTypeHelper.d.ts +17 -0
  20. package/docs/changelog.md +5 -0
  21. package/docs/examples.md +1 -0
  22. package/docs/reference/classes/FetchError.md +379 -0
  23. package/docs/reference/classes/FetchHelper.md +185 -0
  24. package/docs/reference/classes/Jwt.md +313 -0
  25. package/docs/reference/classes/MimeTypeHelper.md +53 -0
  26. package/docs/reference/index.md +32 -0
  27. package/docs/reference/interfaces/IFetchOptions.md +53 -0
  28. package/docs/reference/interfaces/IHttpHeaders.md +7 -0
  29. package/docs/reference/interfaces/IJwk.md +111 -0
  30. package/docs/reference/interfaces/IJwtHeader.md +31 -0
  31. package/docs/reference/interfaces/IJwtPayload.md +63 -0
  32. package/docs/reference/type-aliases/HeaderTypes.md +5 -0
  33. package/docs/reference/type-aliases/HttpMethod.md +5 -0
  34. package/docs/reference/type-aliases/HttpStatusCode.md +5 -0
  35. package/docs/reference/type-aliases/JwtAlgorithms.md +5 -0
  36. package/docs/reference/type-aliases/MimeTypes.md +5 -0
  37. package/docs/reference/variables/HeaderTypes.md +55 -0
  38. package/docs/reference/variables/HttpMethod.md +43 -0
  39. package/docs/reference/variables/HttpStatusCode.md +379 -0
  40. package/docs/reference/variables/JwtAlgorithms.md +19 -0
  41. package/docs/reference/variables/MimeTypes.md +133 -0
  42. package/locales/en.json +18 -0
  43. package/package.json +65 -0
@@ -0,0 +1,85 @@
1
+ import type { IJwtHeader } from "../models/IJwtHeader";
2
+ import type { IJwtPayload } from "../models/IJwtPayload";
3
+ import { JwtAlgorithms } from "../models/jwtAlgorithms";
4
+ /**
5
+ * Class to encode and decode JSON Web Tokens.
6
+ */
7
+ export declare class Jwt {
8
+ /**
9
+ * Encode a token.
10
+ * @param header The header to encode.
11
+ * @param payload The payload to encode.
12
+ * @param key The key for signing the token, can be omitted if a signer is provided.
13
+ * @returns The encoded token.
14
+ */
15
+ static encode<U extends IJwtHeader, T extends IJwtPayload>(header: U, payload: T, key: Uint8Array): Promise<string>;
16
+ /**
17
+ * Encode a token.
18
+ * @param header The header to encode.
19
+ * @param payload The payload to encode.
20
+ * @param signer Custom signer method.
21
+ * @returns The encoded token.
22
+ */
23
+ static encodeWithSigner<U extends IJwtHeader, T extends IJwtPayload>(header: U, payload: T, signer: (alg: JwtAlgorithms, key: Uint8Array | undefined, payload: Uint8Array) => Promise<Uint8Array>): Promise<string>;
24
+ /**
25
+ * Decode a token.
26
+ * @param token The token to decode.
27
+ * @returns The decoded payload.
28
+ */
29
+ static decode<U extends IJwtHeader, T extends IJwtPayload>(token: string): Promise<{
30
+ header?: U;
31
+ payload?: T;
32
+ signature?: Uint8Array;
33
+ }>;
34
+ /**
35
+ * Verify a token.
36
+ * @param token The token to verify.
37
+ * @param key The key for verifying the token
38
+ * @returns The decoded payload.
39
+ */
40
+ static verify<U extends IJwtHeader, T extends IJwtPayload>(token: string, key: Uint8Array): Promise<{
41
+ verified: boolean;
42
+ header?: U;
43
+ payload?: T;
44
+ signature?: Uint8Array;
45
+ }>;
46
+ /**
47
+ * Verify a token.
48
+ * @param token The token to verify.
49
+ * @param verifier Custom verification method.
50
+ * @returns The decoded payload.
51
+ */
52
+ static verifyWithVerifier<U extends IJwtHeader, T extends IJwtPayload>(token: string, verifier: (alg: JwtAlgorithms, key: Uint8Array | undefined, payload: Uint8Array, signature: Uint8Array) => Promise<boolean>): Promise<{
53
+ verified: boolean;
54
+ header?: U;
55
+ payload?: T;
56
+ signature?: Uint8Array;
57
+ }>;
58
+ /**
59
+ * Verify a token by parts.
60
+ * @param header The header to verify.
61
+ * @param payload The payload to verify.
62
+ * @param signature The signature to verify.
63
+ * @param key The key for verifying the token, if not provided no verification occurs.
64
+ * @param verifier Custom verification method.
65
+ * @returns True if the parts are verified.
66
+ */
67
+ static verifySignature<U extends IJwtHeader, T extends IJwtPayload>(header?: U, payload?: T, signature?: Uint8Array, key?: Uint8Array, verifier?: (alg: JwtAlgorithms, key: Uint8Array | undefined, payload: Uint8Array, signature: Uint8Array) => Promise<boolean>): Promise<boolean>;
68
+ /**
69
+ * The default signer for the JWT.
70
+ * @param alg The algorithm to use.
71
+ * @param key The key to sign with.
72
+ * @param payload The payload to sign.
73
+ * @returns The signature.
74
+ */
75
+ static defaultSigner(alg: JwtAlgorithms, key: Uint8Array | undefined, payload: Uint8Array): Promise<Uint8Array>;
76
+ /**
77
+ * The default verifier for the JWT.
78
+ * @param alg The algorithm to use.
79
+ * @param key The key to verify with.
80
+ * @param payload The payload to verify.
81
+ * @param signature The signature to verify.
82
+ * @returns True if the signature was verified.
83
+ */
84
+ static defaultVerifier(alg: JwtAlgorithms, key: Uint8Array | undefined, payload: Uint8Array, signature: Uint8Array): Promise<boolean>;
85
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Class to help with mime types.
3
+ */
4
+ export declare class MimeTypeHelper {
5
+ /**
6
+ * Detect the mime type from a byte array.
7
+ * @param data The data to test.
8
+ * @returns The mime type if detected.
9
+ */
10
+ static detect(data: Uint8Array): Promise<string | undefined>;
11
+ /**
12
+ * Return the default extension for a mime type.
13
+ * @param mimeType The mimetype to get the extension for.
14
+ * @returns The extension for the mime type.
15
+ */
16
+ static defaultExtension(mimeType: string | undefined): string | undefined;
17
+ }
@@ -0,0 +1,5 @@
1
+ # @twin.org/web - Changelog
2
+
3
+ ## v0.0.1
4
+
5
+ - Initial Release
@@ -0,0 +1 @@
1
+ # @twin.org/web - Examples
@@ -0,0 +1,379 @@
1
+ # Class: FetchError
2
+
3
+ Class to represent errors from fetch.
4
+
5
+ ## Extends
6
+
7
+ - `BaseError`
8
+
9
+ ## Constructors
10
+
11
+ ### new FetchError()
12
+
13
+ > **new FetchError**(`source`, `message`, `httpStatus`, `properties`?, `inner`?): [`FetchError`](FetchError.md)
14
+
15
+ Create a new instance of FetchError.
16
+
17
+ #### Parameters
18
+
19
+ • **source**: `string`
20
+
21
+ The source of the error.
22
+
23
+ • **message**: `string`
24
+
25
+ The message as a code.
26
+
27
+ • **httpStatus**: [`HttpStatusCode`](../type-aliases/HttpStatusCode.md)
28
+
29
+ The http status code.
30
+
31
+ • **properties?**
32
+
33
+ Any additional information for the error.
34
+
35
+ • **inner?**: `unknown`
36
+
37
+ The inner error if we have wrapped another error.
38
+
39
+ #### Returns
40
+
41
+ [`FetchError`](FetchError.md)
42
+
43
+ #### Overrides
44
+
45
+ `BaseError.constructor`
46
+
47
+ ## Properties
48
+
49
+ ### source?
50
+
51
+ > `optional` **source**: `string`
52
+
53
+ The source of the error.
54
+
55
+ #### Inherited from
56
+
57
+ `BaseError.source`
58
+
59
+ ***
60
+
61
+ ### properties?
62
+
63
+ > `optional` **properties**: `object`
64
+
65
+ Any additional information for the error.
66
+
67
+ #### Index Signature
68
+
69
+ \[`id`: `string`\]: `unknown`
70
+
71
+ #### Inherited from
72
+
73
+ `BaseError.properties`
74
+
75
+ ***
76
+
77
+ ### inner?
78
+
79
+ > `optional` **inner**: `IError`
80
+
81
+ The inner error if there was one.
82
+
83
+ #### Inherited from
84
+
85
+ `BaseError.inner`
86
+
87
+ ***
88
+
89
+ ### CLASS\_NAME
90
+
91
+ > `readonly` `static` **CLASS\_NAME**: `string`
92
+
93
+ Runtime name for the class.
94
+
95
+ ## Methods
96
+
97
+ ### fromError()
98
+
99
+ > `static` **fromError**(`err`): `BaseError`
100
+
101
+ Construct an error from an existing one.
102
+
103
+ #### Parameters
104
+
105
+ • **err**: `unknown`
106
+
107
+ The existing error.
108
+
109
+ #### Returns
110
+
111
+ `BaseError`
112
+
113
+ The new instance.
114
+
115
+ #### Inherited from
116
+
117
+ `BaseError.fromError`
118
+
119
+ ***
120
+
121
+ ### flatten()
122
+
123
+ > `static` **flatten**(`err`): `IError`[]
124
+
125
+ Flatten an error tree.
126
+
127
+ #### Parameters
128
+
129
+ • **err**: `unknown`
130
+
131
+ The starting error.
132
+
133
+ #### Returns
134
+
135
+ `IError`[]
136
+
137
+ The list of all internal errors.
138
+
139
+ #### Inherited from
140
+
141
+ `BaseError.flatten`
142
+
143
+ ***
144
+
145
+ ### expand()
146
+
147
+ > `static` **expand**(`errors`): `undefined` \| `IError`
148
+
149
+ Expand an error tree.
150
+
151
+ #### Parameters
152
+
153
+ • **errors**: `undefined` \| `IError`[]
154
+
155
+ The list of errors to expand.
156
+
157
+ #### Returns
158
+
159
+ `undefined` \| `IError`
160
+
161
+ The first level error.
162
+
163
+ #### Inherited from
164
+
165
+ `BaseError.expand`
166
+
167
+ ***
168
+
169
+ ### isErrorName()
170
+
171
+ > `static` **isErrorName**(`error`, `name`): `error is BaseError`
172
+
173
+ Test to see if the error has the specified error name.
174
+
175
+ #### Parameters
176
+
177
+ • **error**: `unknown`
178
+
179
+ The error to test.
180
+
181
+ • **name**: `string` \| `RegExp`
182
+
183
+ The name to check for.
184
+
185
+ #### Returns
186
+
187
+ `error is BaseError`
188
+
189
+ True if the error has the name.
190
+
191
+ #### Inherited from
192
+
193
+ `BaseError.isErrorName`
194
+
195
+ ***
196
+
197
+ ### isErrorMessage()
198
+
199
+ > `static` **isErrorMessage**(`error`, `message`): `error is BaseError`
200
+
201
+ Test to see if the error has the specified error message.
202
+
203
+ #### Parameters
204
+
205
+ • **error**: `unknown`
206
+
207
+ The error to test.
208
+
209
+ • **message**: `string` \| `RegExp`
210
+
211
+ The message to check for.
212
+
213
+ #### Returns
214
+
215
+ `error is BaseError`
216
+
217
+ True if the error has the name.
218
+
219
+ #### Inherited from
220
+
221
+ `BaseError.isErrorMessage`
222
+
223
+ ***
224
+
225
+ ### isErrorCode()
226
+
227
+ > `static` **isErrorCode**(`error`, `code`): `boolean`
228
+
229
+ Test to see if the error has the specified error code.
230
+
231
+ #### Parameters
232
+
233
+ • **error**: `unknown`
234
+
235
+ The error to test.
236
+
237
+ • **code**: `string` \| `RegExp`
238
+
239
+ The code to check for.
240
+
241
+ #### Returns
242
+
243
+ `boolean`
244
+
245
+ True if the error has the code.
246
+
247
+ #### Inherited from
248
+
249
+ `BaseError.isErrorCode`
250
+
251
+ ***
252
+
253
+ ### someErrorName()
254
+
255
+ > `static` **someErrorName**(`error`, `name`): `error is BaseError`
256
+
257
+ Test to see if any of the errors or children have the given error name.
258
+
259
+ #### Parameters
260
+
261
+ • **error**: `unknown`
262
+
263
+ The error to test.
264
+
265
+ • **name**: `string` \| `RegExp`
266
+
267
+ The name to check for.
268
+
269
+ #### Returns
270
+
271
+ `error is BaseError`
272
+
273
+ True if the error has the name.
274
+
275
+ #### Inherited from
276
+
277
+ `BaseError.someErrorName`
278
+
279
+ ***
280
+
281
+ ### someErrorMessage()
282
+
283
+ > `static` **someErrorMessage**(`error`, `message`): `error is BaseError`
284
+
285
+ Test to see if any of the errors or children have the given error message.
286
+
287
+ #### Parameters
288
+
289
+ • **error**: `unknown`
290
+
291
+ The error to test.
292
+
293
+ • **message**: `string` \| `RegExp`
294
+
295
+ The message to check for.
296
+
297
+ #### Returns
298
+
299
+ `error is BaseError`
300
+
301
+ True if the error has the name.
302
+
303
+ #### Inherited from
304
+
305
+ `BaseError.someErrorMessage`
306
+
307
+ ***
308
+
309
+ ### someErrorClass()
310
+
311
+ > `static` **someErrorClass**(`error`, `cls`): `error is BaseError`
312
+
313
+ Test to see if any of the errors or children are from a specific class.
314
+
315
+ #### Parameters
316
+
317
+ • **error**: `unknown`
318
+
319
+ The error to test.
320
+
321
+ • **cls**: `string`
322
+
323
+ The class to check for.
324
+
325
+ #### Returns
326
+
327
+ `error is BaseError`
328
+
329
+ True if the error has the specific class.
330
+
331
+ #### Inherited from
332
+
333
+ `BaseError.someErrorClass`
334
+
335
+ ***
336
+
337
+ ### someErrorCode()
338
+
339
+ > `static` **someErrorCode**(`error`, `code`): `error is BaseError`
340
+
341
+ Test to see if any of the errors or children have the given error code.
342
+
343
+ #### Parameters
344
+
345
+ • **error**: `unknown`
346
+
347
+ The error to test.
348
+
349
+ • **code**: `string` \| `RegExp`
350
+
351
+ The code to check for.
352
+
353
+ #### Returns
354
+
355
+ `error is BaseError`
356
+
357
+ True if the error has the name.
358
+
359
+ #### Inherited from
360
+
361
+ `BaseError.someErrorCode`
362
+
363
+ ***
364
+
365
+ ### toJsonObject()
366
+
367
+ > **toJsonObject**(): `IError`
368
+
369
+ Serialize the error to the error model.
370
+
371
+ #### Returns
372
+
373
+ `IError`
374
+
375
+ The error model.
376
+
377
+ #### Inherited from
378
+
379
+ `BaseError.toJsonObject`
@@ -0,0 +1,185 @@
1
+ # Class: FetchHelper
2
+
3
+ Class to helper with fetch operations.
4
+
5
+ ## Constructors
6
+
7
+ ### new FetchHelper()
8
+
9
+ > **new FetchHelper**(): [`FetchHelper`](FetchHelper.md)
10
+
11
+ #### Returns
12
+
13
+ [`FetchHelper`](FetchHelper.md)
14
+
15
+ ## Methods
16
+
17
+ ### fetch()
18
+
19
+ > `static` **fetch**(`source`, `url`, `method`, `body`?, `options`?): `Promise`\<`Response`\>
20
+
21
+ Perform a fetch request.
22
+
23
+ #### Parameters
24
+
25
+ • **source**: `string`
26
+
27
+ The source for the request.
28
+
29
+ • **url**: `string`
30
+
31
+ The url for the request.
32
+
33
+ • **method**: [`HttpMethod`](../type-aliases/HttpMethod.md)
34
+
35
+ The http method.
36
+
37
+ • **body?**: `string` \| `Uint8Array`
38
+
39
+ Request to send to the endpoint.
40
+
41
+ • **options?**: `Omit`\<[`IFetchOptions`](../interfaces/IFetchOptions.md), `"cacheTtlSeconds"`\>
42
+
43
+ Options for sending the requests.
44
+
45
+ #### Returns
46
+
47
+ `Promise`\<`Response`\>
48
+
49
+ The response.
50
+
51
+ ***
52
+
53
+ ### fetchJson()
54
+
55
+ > `static` **fetchJson**\<`T`, `U`\>(`source`, `url`, `method`, `requestData`?, `options`?): `Promise`\<`U`\>
56
+
57
+ Perform a request in json format.
58
+
59
+ #### Type Parameters
60
+
61
+ • **T**
62
+
63
+ • **U**
64
+
65
+ #### Parameters
66
+
67
+ • **source**: `string`
68
+
69
+ The source for the request.
70
+
71
+ • **url**: `string`
72
+
73
+ The url for the request.
74
+
75
+ • **method**: [`HttpMethod`](../type-aliases/HttpMethod.md)
76
+
77
+ The http method.
78
+
79
+ • **requestData?**: `T`
80
+
81
+ Request to send to the endpoint.
82
+
83
+ • **options?**: [`IFetchOptions`](../interfaces/IFetchOptions.md)
84
+
85
+ Options for sending the requests.
86
+
87
+ #### Returns
88
+
89
+ `Promise`\<`U`\>
90
+
91
+ The response.
92
+
93
+ ***
94
+
95
+ ### fetchBinary()
96
+
97
+ > `static` **fetchBinary**\<`T`\>(`source`, `url`, `method`, `requestData`?, `options`?): `Promise`\<`Uint8Array` \| `T`\>
98
+
99
+ Perform a request for binary data.
100
+
101
+ #### Type Parameters
102
+
103
+ • **T**
104
+
105
+ #### Parameters
106
+
107
+ • **source**: `string`
108
+
109
+ The source for the request.
110
+
111
+ • **url**: `string`
112
+
113
+ The url for the request.
114
+
115
+ • **method**: `"GET"` \| `"POST"`
116
+
117
+ The http method.
118
+
119
+ • **requestData?**: `Uint8Array`
120
+
121
+ Request to send to the endpoint.
122
+
123
+ • **options?**: [`IFetchOptions`](../interfaces/IFetchOptions.md)
124
+
125
+ Options for sending the requests.
126
+
127
+ #### Returns
128
+
129
+ `Promise`\<`Uint8Array` \| `T`\>
130
+
131
+ The response.
132
+
133
+ ***
134
+
135
+ ### clearCache()
136
+
137
+ > `static` **clearCache**(): `void`
138
+
139
+ Clears the cache.
140
+
141
+ #### Returns
142
+
143
+ `void`
144
+
145
+ ***
146
+
147
+ ### getCacheEntry()
148
+
149
+ > `static` **getCacheEntry**\<`T`\>(`url`): `Promise`\<`undefined` \| `T`\>
150
+
151
+ Get a cache entry.
152
+
153
+ #### Type Parameters
154
+
155
+ • **T**
156
+
157
+ #### Parameters
158
+
159
+ • **url**: `string`
160
+
161
+ The url for the request.
162
+
163
+ #### Returns
164
+
165
+ `Promise`\<`undefined` \| `T`\>
166
+
167
+ The cache entry if it exists.
168
+
169
+ ***
170
+
171
+ ### removeCacheEntry()
172
+
173
+ > `static` **removeCacheEntry**(`url`): `void`
174
+
175
+ Remove a cache entry.
176
+
177
+ #### Parameters
178
+
179
+ • **url**: `string`
180
+
181
+ The url for the request.
182
+
183
+ #### Returns
184
+
185
+ `void`