@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,22 @@
1
+ import { BaseError } from "@twin.org/core";
2
+ import type { HttpStatusCode } from "../models/httpStatusCode";
3
+ /**
4
+ * Class to represent errors from fetch.
5
+ */
6
+ export declare class FetchError extends BaseError {
7
+ /**
8
+ * Runtime name for the class.
9
+ */
10
+ static readonly CLASS_NAME: string;
11
+ /**
12
+ * Create a new instance of FetchError.
13
+ * @param source The source of the error.
14
+ * @param message The message as a code.
15
+ * @param httpStatus The http status code.
16
+ * @param properties Any additional information for the error.
17
+ * @param inner The inner error if we have wrapped another error.
18
+ */
19
+ constructor(source: string, message: string, httpStatus: HttpStatusCode, properties?: {
20
+ [id: string]: unknown;
21
+ }, inner?: unknown);
22
+ }
@@ -0,0 +1,14 @@
1
+ export * from "./errors/fetchError";
2
+ export * from "./models/headerTypes";
3
+ export * from "./models/httpMethod";
4
+ export * from "./models/httpStatusCode";
5
+ export * from "./models/IFetchOptions";
6
+ export * from "./models/IHttpHeaders";
7
+ export * from "./models/IJwk";
8
+ export * from "./models/IJwtHeader";
9
+ export * from "./models/IJwtPayload";
10
+ export * from "./models/jwtAlgorithms";
11
+ export * from "./models/mimeTypes";
12
+ export * from "./utils/fetchHelper";
13
+ export * from "./utils/jwt";
14
+ export * from "./utils/mimeTypeHelper";
@@ -0,0 +1,30 @@
1
+ import type { IHttpHeaders } from "./IHttpHeaders";
2
+ /**
3
+ * Options for call to the fetch helper.
4
+ */
5
+ export interface IFetchOptions {
6
+ /**
7
+ * @param headers The headers for the request.
8
+ */
9
+ headers?: IHttpHeaders;
10
+ /**
11
+ * Timeout for requests in milliseconds.
12
+ */
13
+ timeoutMs?: number;
14
+ /**
15
+ * Include credentials in the requests.
16
+ */
17
+ includeCredentials?: boolean;
18
+ /**
19
+ * The number of times to retry fetching defaults to no retries.
20
+ */
21
+ retryCount?: number;
22
+ /**
23
+ * The number of milliseconds we should delay before any retry.
24
+ */
25
+ retryDelayMs?: number;
26
+ /**
27
+ * The number of milliseconds to cache the response for, leave undefined for no cache, 0 means infinite.
28
+ */
29
+ cacheTtlMs?: number;
30
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Model used for Http headers parameter.
3
+ */
4
+ export interface IHttpHeaders {
5
+ [key: string]: string | string[];
6
+ }
@@ -0,0 +1,62 @@
1
+ import type { JwtAlgorithms } from "./jwtAlgorithms";
2
+ /**
3
+ * The fields in a JSON Web Key.
4
+ */
5
+ export interface IJwk {
6
+ /**
7
+ * Additional fields in the key.
8
+ */
9
+ [key: string]: unknown;
10
+ /**
11
+ * The cryptographic algorithm for the key.
12
+ */
13
+ alg?: JwtAlgorithms;
14
+ /**
15
+ * The intended use for the key.
16
+ */
17
+ use?: string;
18
+ /**
19
+ * The operation(s) that the key is intended to be used for.
20
+ */
21
+ key_ops?: string[];
22
+ /**
23
+ * The key type parameter.
24
+ */
25
+ kty: string;
26
+ /**
27
+ * The public key parameters.
28
+ */
29
+ n?: string;
30
+ /**
31
+ * Exponent parameter.
32
+ */
33
+ e?: string;
34
+ /**
35
+ * The private key parameters.
36
+ */
37
+ d?: string;
38
+ /**
39
+ * The private key parameters.
40
+ */
41
+ p?: string;
42
+ /**
43
+ * The private key parameters.
44
+ */
45
+ q?: string;
46
+ /**
47
+ * The private key parameters.
48
+ */
49
+ dp?: string;
50
+ /**
51
+ * The private key parameters.
52
+ */
53
+ dq?: string;
54
+ /**
55
+ * The private key parameters.
56
+ */
57
+ qi?: string;
58
+ /**
59
+ * The key ID.
60
+ */
61
+ kid?: string;
62
+ }
@@ -0,0 +1,22 @@
1
+ import type { JwtAlgorithms } from "./jwtAlgorithms";
2
+ /**
3
+ * The fields in a JSON Web Token header.
4
+ */
5
+ export interface IJwtHeader {
6
+ /**
7
+ * Additional fields in the header.
8
+ */
9
+ [key: string]: unknown;
10
+ /**
11
+ * The type of the token.
12
+ */
13
+ typ?: string;
14
+ /**
15
+ * The algorithm used to sign the token.
16
+ */
17
+ alg: JwtAlgorithms;
18
+ /**
19
+ * The key ID.
20
+ */
21
+ kid?: string;
22
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * The fields in a JSON Web Token payload.
3
+ */
4
+ export interface IJwtPayload {
5
+ /**
6
+ * Additional fields in the payload.
7
+ */
8
+ [key: string]: unknown;
9
+ /**
10
+ * The issuer of the token.
11
+ */
12
+ iss?: string;
13
+ /**
14
+ * The subject of the token.
15
+ */
16
+ sub?: string;
17
+ /**
18
+ * The audience of the token.
19
+ */
20
+ aud?: string;
21
+ /**
22
+ * The expiration time of the token.
23
+ */
24
+ exp?: number;
25
+ /**
26
+ * The not before time of the token.
27
+ */
28
+ nbf?: number;
29
+ /**
30
+ * The issued at time of the token.
31
+ */
32
+ iat?: number;
33
+ /**
34
+ * The JWT ID.
35
+ */
36
+ jti?: string;
37
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Common http header types.
3
+ */
4
+ export declare const HeaderTypes: {
5
+ /**
6
+ * Content Type.
7
+ */
8
+ readonly ContentType: "Content-Type";
9
+ /**
10
+ * Content Length.
11
+ */
12
+ readonly ContentLength: "Content-Length";
13
+ /**
14
+ * Content Disposition.
15
+ */
16
+ readonly ContentDisposition: "Content-Disposition";
17
+ /**
18
+ * Accept.
19
+ */
20
+ readonly Accept: "Accept";
21
+ /**
22
+ * Authorization.
23
+ */
24
+ readonly Authorization: "Authorization";
25
+ /**
26
+ * Cookie.
27
+ */
28
+ readonly Cookie: "Cookie";
29
+ /**
30
+ * Set Cookie.
31
+ */
32
+ readonly SetCookie: "Set-Cookie";
33
+ /**
34
+ * Location
35
+ */
36
+ readonly Location: "Location";
37
+ };
38
+ /**
39
+ * Common http header types.
40
+ */
41
+ export type HeaderTypes = (typeof HeaderTypes)[keyof typeof HeaderTypes];
@@ -0,0 +1,18 @@
1
+ /**
2
+ * The names of the HTTP Methods.
3
+ */
4
+ export declare const HttpMethod: {
5
+ readonly GET: "GET";
6
+ readonly POST: "POST";
7
+ readonly PUT: "PUT";
8
+ readonly PATCH: "PATCH";
9
+ readonly DELETE: "DELETE";
10
+ readonly OPTIONS: "OPTIONS";
11
+ readonly HEAD: "HEAD";
12
+ readonly CONNECT: "CONNECT";
13
+ readonly TRACE: "TRACE";
14
+ };
15
+ /**
16
+ * The HTTP Methods.
17
+ */
18
+ export type HttpMethod = (typeof HttpMethod)[keyof typeof HttpMethod];
@@ -0,0 +1,257 @@
1
+ /**
2
+ * Standard HTTP status codes.
3
+ */
4
+ export declare const HttpStatusCode: {
5
+ /**
6
+ * Continue status code.
7
+ */
8
+ readonly continue: 100;
9
+ /**
10
+ * Switching Protocols status code.
11
+ */
12
+ readonly switchingProtocols: 101;
13
+ /**
14
+ * Processing status code.
15
+ */
16
+ readonly processing: 102;
17
+ /**
18
+ * Early Hints status code.
19
+ */
20
+ readonly earlyHints: 103;
21
+ /**
22
+ * OK status code.
23
+ */
24
+ readonly ok: 200;
25
+ /**
26
+ * Created status code.
27
+ */
28
+ readonly created: 201;
29
+ /**
30
+ * Accepted status code.
31
+ */
32
+ readonly accepted: 202;
33
+ /**
34
+ * Non-Authoritative Information status code.
35
+ */
36
+ readonly nonAuthoritativeInformation: 203;
37
+ /**
38
+ * No Content status code.
39
+ */
40
+ readonly noContent: 204;
41
+ /**
42
+ * Reset Content status code.
43
+ */
44
+ readonly resetContent: 205;
45
+ /**
46
+ * Partial Content status code.
47
+ */
48
+ readonly partialContent: 206;
49
+ /**
50
+ * Multi-Status status code.
51
+ */
52
+ readonly multiStatus: 207;
53
+ /**
54
+ * Already Reported status code.
55
+ */
56
+ readonly alreadyReported: 208;
57
+ /**
58
+ * IM Used status code.
59
+ */
60
+ readonly imUsed: 226;
61
+ /**
62
+ * Multiple Choices status code.
63
+ */
64
+ readonly multipleChoices: 300;
65
+ /**
66
+ * Moved Permanently status code.
67
+ */
68
+ readonly movedPermanently: 301;
69
+ /**
70
+ * Found status code.
71
+ */
72
+ readonly found: 302;
73
+ /**
74
+ * See Other status code.
75
+ */
76
+ readonly seeOther: 303;
77
+ /**
78
+ * Not Modified status code.
79
+ */
80
+ readonly notModified: 304;
81
+ /**
82
+ * Use Proxy status code.
83
+ */
84
+ readonly useProxy: 305;
85
+ /**
86
+ * Temporary Redirect status code.
87
+ */
88
+ readonly temporaryRedirect: 307;
89
+ /**
90
+ * Permanent Redirect status code.
91
+ */
92
+ readonly permanentRedirect: 308;
93
+ /**
94
+ * Bad Request status code.
95
+ */
96
+ readonly badRequest: 400;
97
+ /**
98
+ * Unauthorized status code.
99
+ */
100
+ readonly unauthorized: 401;
101
+ /**
102
+ * Payment Required status code.
103
+ */
104
+ readonly paymentRequired: 402;
105
+ /**
106
+ * Forbidden status code.
107
+ */
108
+ readonly forbidden: 403;
109
+ /**
110
+ * Not Found status code.
111
+ */
112
+ readonly notFound: 404;
113
+ /**
114
+ * Method Not Allowed status code.
115
+ */
116
+ readonly methodNotAllowed: 405;
117
+ /**
118
+ * Not Acceptable status code.
119
+ */
120
+ readonly notAcceptable: 406;
121
+ /**
122
+ * Proxy Authentication Required status code.
123
+ */
124
+ readonly proxyAuthenticationRequired: 407;
125
+ /**
126
+ * Request Timeout status code.
127
+ */
128
+ readonly requestTimeout: 408;
129
+ /**
130
+ * Conflict status code.
131
+ */
132
+ readonly conflict: 409;
133
+ /**
134
+ * Gone status code.
135
+ */
136
+ readonly gone: 410;
137
+ /**
138
+ * Length Required status code.
139
+ */
140
+ readonly lengthRequired: 411;
141
+ /**
142
+ * Precondition Failed status code.
143
+ */
144
+ readonly preconditionFailed: 412;
145
+ /**
146
+ * Payload Too Large status code.
147
+ */
148
+ readonly payloadTooLarge: 413;
149
+ /**
150
+ * URI Too Long status code.
151
+ */
152
+ readonly uriTooLong: 414;
153
+ /**
154
+ * Unsupported Media Type status code.
155
+ */
156
+ readonly unsupportedMediaType: 415;
157
+ /**
158
+ * Range Not Satisfiable status code.
159
+ */
160
+ readonly rangeNotSatisfiable: 416;
161
+ /**
162
+ * Expectation Failed status code.
163
+ */
164
+ readonly expectationFailed: 417;
165
+ /**
166
+ * I'm a Teapot status code.
167
+ */
168
+ readonly imATeapot: 418;
169
+ /**
170
+ * Misdirected Request status code.
171
+ */
172
+ readonly misdirectedRequest: 421;
173
+ /**
174
+ * Unprocessable Entity status code.
175
+ */
176
+ readonly unprocessableEntity: 422;
177
+ /**
178
+ * Locked status code.
179
+ */
180
+ readonly locked: 423;
181
+ /**
182
+ * Failed Dependency status code.
183
+ */
184
+ readonly failedDependency: 424;
185
+ /**
186
+ * Too Early status code.
187
+ */
188
+ readonly tooEarly: 425;
189
+ /**
190
+ * Upgrade Required status code.
191
+ */
192
+ readonly upgradeRequired: 426;
193
+ /**
194
+ * Precondition Required status code.
195
+ */
196
+ readonly preconditionRequired: 428;
197
+ /**
198
+ * Too Many Requests status code.
199
+ */
200
+ readonly tooManyRequests: 429;
201
+ /**
202
+ * Request Header Fields Too Large status code.
203
+ */
204
+ readonly requestHeaderFieldsTooLarge: 431;
205
+ /**
206
+ * Unavailable For Legal Reasons status code.
207
+ */
208
+ readonly unavailableForLegalReasons: 451;
209
+ /**
210
+ * Internal Server Error status code.
211
+ */
212
+ readonly internalServerError: 500;
213
+ /**
214
+ * Not Implemented status code.
215
+ */
216
+ readonly notImplemented: 501;
217
+ /**
218
+ * Bad Gateway status code.
219
+ */
220
+ readonly badGateway: 502;
221
+ /**
222
+ * Service Unavailable status code.
223
+ */
224
+ readonly serviceUnavailable: 503;
225
+ /**
226
+ * Gateway Timeout status code.
227
+ */
228
+ readonly gatewayTimeout: 504;
229
+ /**
230
+ * HTTP Version Not Supported status code.
231
+ */
232
+ readonly httpVersionNotSupported: 505;
233
+ /**
234
+ * Variant Also Negotiates status code.
235
+ */
236
+ readonly variantAlsoNegotiates: 506;
237
+ /**
238
+ * Insufficient Storage status code.
239
+ */
240
+ readonly insufficientStorage: 507;
241
+ /**
242
+ * Loop Detected status code.
243
+ */
244
+ readonly loopDetected: 508;
245
+ /**
246
+ * Not Extended status code.
247
+ */
248
+ readonly notExtended: 510;
249
+ /**
250
+ * Network Authentication Required status code.
251
+ */
252
+ readonly networkAuthenticationRequired: 511;
253
+ };
254
+ /**
255
+ * Standard HTTP status codes.
256
+ */
257
+ export type HttpStatusCode = (typeof HttpStatusCode)[keyof typeof HttpStatusCode];
@@ -0,0 +1,17 @@
1
+ /**
2
+ * The cryptographic algorithms supported for JSON Web Tokens and JSON Web Keys.
3
+ */
4
+ export declare const JwtAlgorithms: {
5
+ /**
6
+ * HMAC using SHA-256.
7
+ */
8
+ readonly HS256: "HS256";
9
+ /**
10
+ * EdDSA using Ed25519.
11
+ */
12
+ readonly EdDSA: "EdDSA";
13
+ };
14
+ /**
15
+ * The cryptographic algorithms supported for JSON Web Tokens and JSON Web Keys.
16
+ */
17
+ export type JwtAlgorithms = (typeof JwtAlgorithms)[keyof typeof JwtAlgorithms];
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Common mime types.
3
+ */
4
+ export declare const MimeTypes: {
5
+ /**
6
+ * Plaint Text - text/plain
7
+ */
8
+ readonly PlainText: "text/plain";
9
+ /**
10
+ * HTML - text/html
11
+ */
12
+ readonly Html: "text/html";
13
+ /**
14
+ * Javascript - text/javascript
15
+ */
16
+ readonly Javascript: "text/javascript";
17
+ /**
18
+ * JSON - application/json
19
+ */
20
+ readonly Json: "application/json";
21
+ /**
22
+ * JSON-LD - application/ld+json
23
+ */
24
+ readonly JsonLd: "application/ld+json";
25
+ /**
26
+ * XML - application/xml
27
+ */
28
+ readonly Xml: "application/xml";
29
+ /**
30
+ * Application Octet Stream, arbitrary binary - application/octet-stream
31
+ */
32
+ readonly OctetStream: "application/octet-stream";
33
+ /**
34
+ * Application GZIP - application/gzip
35
+ */
36
+ readonly Gzip: "application/gzip";
37
+ /**
38
+ * Application BZIP2 - application/x-bzip2
39
+ */
40
+ readonly Bzip2: "application/x-bzip2";
41
+ /**
42
+ * Application ZIP - application/zip
43
+ */
44
+ readonly Zip: "application/zip";
45
+ /**
46
+ * Application PDF - application/pdf
47
+ */
48
+ readonly Pdf: "application/pdf";
49
+ /**
50
+ * Image GIF - image/gif
51
+ */
52
+ readonly Gif: "image/gif";
53
+ /**
54
+ * Image BMP - image/bmp
55
+ */
56
+ readonly Bmp: "image/bmp";
57
+ /**
58
+ * Image JPEG - image/jpeg
59
+ */
60
+ readonly Jpeg: "image/jpeg";
61
+ /**
62
+ * Image PNG - image/png
63
+ */
64
+ readonly Png: "image/png";
65
+ /**
66
+ * Image Tiff - image/tiff
67
+ */
68
+ readonly Tiff: "image/tiff";
69
+ /**
70
+ * Image SVG - image/svg+xml
71
+ */
72
+ readonly Svg: "image/svg+xml";
73
+ /**
74
+ * Image WEBP - image/webp
75
+ */
76
+ readonly WebP: "image/webp";
77
+ /**
78
+ * Video MP4 - video/mp4
79
+ */
80
+ readonly Mp4: "video/mp4";
81
+ /**
82
+ * Audio/Video MPEG - video/mpeg
83
+ */
84
+ readonly Mpeg: "video/mpeg";
85
+ /**
86
+ * Video WEBM - video/webm
87
+ */
88
+ readonly Webm: "video/webm";
89
+ };
90
+ /**
91
+ * Common mime types.
92
+ */
93
+ export type MimeTypes = (typeof MimeTypes)[keyof typeof MimeTypes];
@@ -0,0 +1,52 @@
1
+ import { HttpMethod } from "../models/httpMethod";
2
+ import type { IFetchOptions } from "../models/IFetchOptions";
3
+ /**
4
+ * Class to helper with fetch operations.
5
+ */
6
+ export declare class FetchHelper {
7
+ /**
8
+ * Perform a fetch request.
9
+ * @param source The source for the request.
10
+ * @param url The url for the request.
11
+ * @param method The http method.
12
+ * @param body Request to send to the endpoint.
13
+ * @param options Options for sending the requests.
14
+ * @returns The response.
15
+ */
16
+ static fetch(source: string, url: string, method: HttpMethod, body?: string | Uint8Array, options?: Omit<IFetchOptions, "cacheTtlSeconds">): Promise<Response>;
17
+ /**
18
+ * Perform a request in json format.
19
+ * @param source The source for the request.
20
+ * @param url The url for the request.
21
+ * @param method The http method.
22
+ * @param requestData Request to send to the endpoint.
23
+ * @param options Options for sending the requests.
24
+ * @returns The response.
25
+ */
26
+ static fetchJson<T, U>(source: string, url: string, method: HttpMethod, requestData?: T, options?: IFetchOptions): Promise<U>;
27
+ /**
28
+ * Perform a request for binary data.
29
+ * @param source The source for the request.
30
+ * @param url The url for the request.
31
+ * @param method The http method.
32
+ * @param requestData Request to send to the endpoint.
33
+ * @param options Options for sending the requests.
34
+ * @returns The response.
35
+ */
36
+ static fetchBinary<T>(source: string, url: string, method: Extract<HttpMethod, "GET" | "POST">, requestData?: Uint8Array, options?: IFetchOptions): Promise<Uint8Array | T>;
37
+ /**
38
+ * Clears the cache.
39
+ */
40
+ static clearCache(): void;
41
+ /**
42
+ * Get a cache entry.
43
+ * @param url The url for the request.
44
+ * @returns The cache entry if it exists.
45
+ */
46
+ static getCacheEntry<T>(url: string): Promise<T | undefined>;
47
+ /**
48
+ * Remove a cache entry.
49
+ * @param url The url for the request.
50
+ */
51
+ static removeCacheEntry(url: string): void;
52
+ }