@twin.org/web 0.0.3-next.4 → 0.0.3-next.40

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 (56) 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/IFetchOptions.js.map +1 -1
  5. package/dist/es/models/IHttpLinkHeader.js +2 -0
  6. package/dist/es/models/IHttpLinkHeader.js.map +1 -0
  7. package/dist/es/models/headerTypes.js +129 -1
  8. package/dist/es/models/headerTypes.js.map +1 -1
  9. package/dist/es/models/httpLinkRelType.js +130 -0
  10. package/dist/es/models/httpLinkRelType.js.map +1 -0
  11. package/dist/es/models/httpMethod.js +36 -0
  12. package/dist/es/models/httpMethod.js.map +1 -1
  13. package/dist/es/models/httpStatusCode.js +63 -0
  14. package/dist/es/models/httpStatusCode.js.map +1 -1
  15. package/dist/es/models/mimeTypes.js +170 -1
  16. package/dist/es/models/mimeTypes.js.map +1 -1
  17. package/dist/es/utils/cookieHelper.js +83 -0
  18. package/dist/es/utils/cookieHelper.js.map +1 -0
  19. package/dist/es/utils/fetchHelper.js +1 -1
  20. package/dist/es/utils/fetchHelper.js.map +1 -1
  21. package/dist/es/utils/headerHelper.js +383 -1
  22. package/dist/es/utils/headerHelper.js.map +1 -1
  23. package/dist/es/utils/mimeTypeHelper.js +42 -1
  24. package/dist/es/utils/mimeTypeHelper.js.map +1 -1
  25. package/dist/types/index.d.ts +3 -0
  26. package/dist/types/models/IFetchOptions.d.ts +1 -1
  27. package/dist/types/models/IHttpLinkHeader.d.ts +26 -0
  28. package/dist/types/models/headerTypes.d.ts +128 -0
  29. package/dist/types/models/httpLinkRelType.d.ts +130 -0
  30. package/dist/types/models/httpMethod.d.ts +36 -0
  31. package/dist/types/models/httpStatusCode.d.ts +63 -0
  32. package/dist/types/models/mimeTypes.d.ts +169 -0
  33. package/dist/types/utils/cookieHelper.d.ts +49 -0
  34. package/dist/types/utils/headerHelper.d.ts +115 -0
  35. package/docs/changelog.md +914 -143
  36. package/docs/examples.md +108 -1
  37. package/docs/reference/classes/CookieHelper.md +155 -0
  38. package/docs/reference/classes/FetchError.md +453 -1
  39. package/docs/reference/classes/FetchHelper.md +12 -12
  40. package/docs/reference/classes/HeaderHelper.md +385 -2
  41. package/docs/reference/classes/Jwk.md +6 -6
  42. package/docs/reference/classes/Jws.md +3 -3
  43. package/docs/reference/classes/Jwt.md +17 -17
  44. package/docs/reference/classes/MimeTypeHelper.md +4 -4
  45. package/docs/reference/index.md +4 -0
  46. package/docs/reference/interfaces/IFetchOptions.md +12 -14
  47. package/docs/reference/interfaces/IHttpHeaders.md +1 -1
  48. package/docs/reference/interfaces/IHttpLinkHeader.md +43 -0
  49. package/docs/reference/type-aliases/HttpLinkRelType.md +5 -0
  50. package/docs/reference/variables/HeaderTypes.md +280 -8
  51. package/docs/reference/variables/HttpLinkRelType.md +191 -0
  52. package/docs/reference/variables/HttpMethod.md +63 -9
  53. package/docs/reference/variables/HttpStatusCode.md +314 -62
  54. package/docs/reference/variables/MimeTypes.md +279 -23
  55. package/locales/en.json +4 -3
  56. package/package.json +8 -8
@@ -0,0 +1,83 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ import { Guards, Is } from "@twin.org/core";
4
+ /**
5
+ * Class to help with cookie operations.
6
+ */
7
+ export class CookieHelper {
8
+ /**
9
+ * Runtime name for the class.
10
+ */
11
+ static CLASS_NAME = "CookieHelper";
12
+ /**
13
+ * Create a cookie string.
14
+ * @param cookieName The name of the cookie.
15
+ * @param cookieValue The value of the cookie.
16
+ * @param options Additional cookie options.
17
+ * @param options.secure Should this be a secure cookie.
18
+ * @param options.httpOnly Should this be an http only cookie.
19
+ * @param options.sameSite The same site option for the cookie.
20
+ * @param options.path The path for the cookie.
21
+ * @returns The created cookie string.
22
+ */
23
+ static createCookie(cookieName, cookieValue, options) {
24
+ Guards.stringValue(CookieHelper.CLASS_NAME, "cookieName", cookieName);
25
+ Guards.string(CookieHelper.CLASS_NAME, "cookieValue", cookieValue);
26
+ const cookieParts = [`${cookieName}=${encodeURIComponent(cookieValue)}`];
27
+ const localOptions = options ?? {};
28
+ localOptions.secure ??= true;
29
+ localOptions.httpOnly ??= true;
30
+ localOptions.sameSite ??= "Strict";
31
+ localOptions.path ??= "/";
32
+ if (localOptions.secure) {
33
+ cookieParts.push("Secure");
34
+ }
35
+ if (localOptions.httpOnly) {
36
+ cookieParts.push("HttpOnly");
37
+ }
38
+ if (localOptions.sameSite) {
39
+ cookieParts.push(`SameSite=${localOptions.sameSite}`);
40
+ }
41
+ if (localOptions.path) {
42
+ cookieParts.push(`Path=${localOptions.path}`);
43
+ }
44
+ return cookieParts.join("; ");
45
+ }
46
+ /**
47
+ * Create a cookie string which will delete a cookie.
48
+ * @param cookieName The name of the cookie.
49
+ * @param options Additional cookie options.
50
+ * @param options.secure Should this be a secure cookie.
51
+ * @param options.httpOnly Should this be an http only cookie.
52
+ * @param options.sameSite The same site option for the cookie.
53
+ * @param options.path The path for the cookie.
54
+ * @returns The created cookie string.
55
+ */
56
+ static deleteCookie(cookieName, options) {
57
+ return `${CookieHelper.createCookie(cookieName, "", options)}; Max-Age=0`;
58
+ }
59
+ /**
60
+ * Get cookies from headers.
61
+ * @param headers The headers to get cookies from.
62
+ * @param cookieName The name of the cookie to get.
63
+ * @returns The cookies found in the headers.
64
+ */
65
+ static getCookieFromHeaders(headers, cookieName) {
66
+ Guards.stringValue(CookieHelper.CLASS_NAME, "cookieName", cookieName);
67
+ if (!Is.empty(headers)) {
68
+ const cookies = Is.arrayValue(headers) ? headers : [headers];
69
+ for (const cookie of cookies) {
70
+ if (Is.stringValue(cookie)) {
71
+ const accessTokenCookie = cookie
72
+ .split(";")
73
+ .map(c => c.trim())
74
+ .find(c => c.startsWith(`${cookieName}=`));
75
+ if (Is.stringValue(accessTokenCookie)) {
76
+ return decodeURIComponent(accessTokenCookie.slice(cookieName.length + 1).trim());
77
+ }
78
+ }
79
+ }
80
+ }
81
+ }
82
+ }
83
+ //# sourceMappingURL=cookieHelper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cookieHelper.js","sourceRoot":"","sources":["../../../src/utils/cookieHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AAG5C;;GAEG;AACH,MAAM,OAAO,YAAY;IACxB;;OAEG;IACI,MAAM,CAAU,UAAU,kBAAkC;IAEnE;;;;;;;;;;OAUG;IACI,MAAM,CAAC,YAAY,CACzB,UAAkB,EAClB,WAAmB,EACnB,OAKC;QAED,MAAM,CAAC,WAAW,CAAC,YAAY,CAAC,UAAU,gBAAsB,UAAU,CAAC,CAAC;QAC5E,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,UAAU,iBAAuB,WAAW,CAAC,CAAC;QAEzE,MAAM,WAAW,GAAG,CAAC,GAAG,UAAU,IAAI,kBAAkB,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC;QAEzE,MAAM,YAAY,GAAG,OAAO,IAAI,EAAE,CAAC;QACnC,YAAY,CAAC,MAAM,KAAK,IAAI,CAAC;QAC7B,YAAY,CAAC,QAAQ,KAAK,IAAI,CAAC;QAC/B,YAAY,CAAC,QAAQ,KAAK,QAAQ,CAAC;QACnC,YAAY,CAAC,IAAI,KAAK,GAAG,CAAC;QAE1B,IAAI,YAAY,CAAC,MAAM,EAAE,CAAC;YACzB,WAAW,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QAC5B,CAAC;QACD,IAAI,YAAY,CAAC,QAAQ,EAAE,CAAC;YAC3B,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QAC9B,CAAC;QACD,IAAI,YAAY,CAAC,QAAQ,EAAE,CAAC;YAC3B,WAAW,CAAC,IAAI,CAAC,YAAY,YAAY,CAAC,QAAQ,EAAE,CAAC,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,IAAI,EAAE,CAAC,CAAC;oBAClF,CAAC;gBACF,CAAC;YACF,CAAC;QACF,CAAC;IACF,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Guards, Is } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\n\n/**\n * Class to help with cookie operations.\n */\nexport class CookieHelper {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<CookieHelper>();\n\n\t/**\n\t * Create a cookie string.\n\t * @param cookieName The name of the cookie.\n\t * @param cookieValue The value of the cookie.\n\t * @param options Additional cookie options.\n\t * @param options.secure Should this be a secure cookie.\n\t * @param options.httpOnly Should this be an http only cookie.\n\t * @param options.sameSite The same site option for the cookie.\n\t * @param options.path The path for the cookie.\n\t * @returns The created cookie string.\n\t */\n\tpublic static createCookie(\n\t\tcookieName: string,\n\t\tcookieValue: string,\n\t\toptions?: {\n\t\t\tsecure?: boolean;\n\t\t\thttpOnly?: boolean;\n\t\t\tsameSite?: \"Strict\" | \"Lax\" | \"None\";\n\t\t\tpath?: string;\n\t\t}\n\t): string {\n\t\tGuards.stringValue(CookieHelper.CLASS_NAME, nameof(cookieName), cookieName);\n\t\tGuards.string(CookieHelper.CLASS_NAME, nameof(cookieValue), cookieValue);\n\n\t\tconst cookieParts = [`${cookieName}=${encodeURIComponent(cookieValue)}`];\n\n\t\tconst localOptions = options ?? {};\n\t\tlocalOptions.secure ??= true;\n\t\tlocalOptions.httpOnly ??= true;\n\t\tlocalOptions.sameSite ??= \"Strict\";\n\t\tlocalOptions.path ??= \"/\";\n\n\t\tif (localOptions.secure) {\n\t\t\tcookieParts.push(\"Secure\");\n\t\t}\n\t\tif (localOptions.httpOnly) {\n\t\t\tcookieParts.push(\"HttpOnly\");\n\t\t}\n\t\tif (localOptions.sameSite) {\n\t\t\tcookieParts.push(`SameSite=${localOptions.sameSite}`);\n\t\t}\n\t\tif (localOptions.path) {\n\t\t\tcookieParts.push(`Path=${localOptions.path}`);\n\t\t}\n\n\t\treturn cookieParts.join(\"; \");\n\t}\n\n\t/**\n\t * Create a cookie string which will delete a cookie.\n\t * @param cookieName The name of the cookie.\n\t * @param options Additional cookie options.\n\t * @param options.secure Should this be a secure cookie.\n\t * @param options.httpOnly Should this be an http only cookie.\n\t * @param options.sameSite The same site option for the cookie.\n\t * @param options.path The path for the cookie.\n\t * @returns The created cookie string.\n\t */\n\tpublic static deleteCookie(\n\t\tcookieName: string,\n\t\toptions?: {\n\t\t\tsecure?: boolean;\n\t\t\thttpOnly?: boolean;\n\t\t\tsameSite?: \"Strict\" | \"Lax\" | \"None\";\n\t\t\tpath?: string;\n\t\t}\n\t): string {\n\t\treturn `${CookieHelper.createCookie(cookieName, \"\", options)}; Max-Age=0`;\n\t}\n\n\t/**\n\t * Get cookies from headers.\n\t * @param headers The headers to get cookies from.\n\t * @param cookieName The name of the cookie to get.\n\t * @returns The cookies found in the headers.\n\t */\n\tpublic static getCookieFromHeaders(\n\t\theaders: string | string[] | undefined,\n\t\tcookieName: string\n\t): string | undefined {\n\t\tGuards.stringValue(CookieHelper.CLASS_NAME, nameof(cookieName), cookieName);\n\n\t\tif (!Is.empty(headers)) {\n\t\t\tconst cookies = Is.arrayValue(headers) ? headers : [headers];\n\t\t\tfor (const cookie of cookies) {\n\t\t\t\tif (Is.stringValue(cookie)) {\n\t\t\t\t\tconst accessTokenCookie = cookie\n\t\t\t\t\t\t.split(\";\")\n\t\t\t\t\t\t.map(c => c.trim())\n\t\t\t\t\t\t.find(c => c.startsWith(`${cookieName}=`));\n\n\t\t\t\t\tif (Is.stringValue(accessTokenCookie)) {\n\t\t\t\t\t\treturn decodeURIComponent(accessTokenCookie.slice(cookieName.length + 1).trim());\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n"]}
@@ -73,7 +73,7 @@ export class FetchHelper {
73
73
  }, options?.timeoutMs);
74
74
  }
75
75
  let finalBody;
76
- if (method === HttpMethod.POST || method === HttpMethod.PUT) {
76
+ if (method === HttpMethod.POST || method === HttpMethod.PUT || method === HttpMethod.PATCH) {
77
77
  if (Is.string(body)) {
78
78
  finalBody = body;
79
79
  }
@@ -1 +1 @@
1
- {"version":3,"file":"fetchHelper.js","sourceRoot":"","sources":["../../../src/utils/fetchHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,EAAE,YAAY,EAAe,MAAM,gBAAgB,CAAC;AAE9F,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACvD,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAG7D,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAEnD;;GAEG;AACH,MAAM,OAAO,WAAW;IACvB;;OAEG;IACI,MAAM,CAAU,UAAU,iBAAiC;IAElE;;;OAGG;IACK,MAAM,CAAU,aAAa,GAAW,QAAQ,CAAC;IAEzD;;;;;;;;OAQG;IACI,MAAM,CAAC,KAAK,CAAC,KAAK,CACxB,MAAc,EACd,GAAW,EACX,MAAkB,EAClB,IAA0B,EAC1B,OAAgD;QAEhD,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,UAAU,YAAkB,MAAM,CAAC,CAAC;QAC9D,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,UAAU,SAAe,GAAG,CAAC,CAAC;QACxD,MAAM,CAAC,UAAU,CAChB,WAAW,CAAC,UAAU,YAEtB,MAAM,EACN,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CACzB,CAAC;QACF,IAAI,CAAC,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,CAA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AG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QAChD,MAAM,aAAa,GAAG,SAAS,CAAC,SAAS,CAAC,iBAAiB,CAAC,CAAC;QAC7D,MAAM,YAAY,GAAG,SAAS,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;QAEtD,MAAM,IAAI,UAAU,CACnB,MAAM,EACN,GAAG,aAA8B,oBAAoB,EACrD,QAAQ,CAAC,MAAwB,EACjC;YACC,UAAU,EAAE,QAAQ,CAAC,UAAU;YAC/B,GAAG;YACH,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC,CAAC,SAAS;SAClD,EACD,YAAY,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,aAAa,CACxC,CAAC;IACH,CAAC;IAED;;OAEG;IACI,MAAM,CAAC,UAAU;QACvB,UAAU,CAAC,UAAU,CAAC,WAAW,CAAC,aAAa,CAAC,CAAC;IAClD,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,KAAK,CAAC,aAAa,CAAI,GAAW;QAC/C,OAAO,UAAU,CAAC,GAAG,CAAI,GAAG,WAAW,CAAC,aAAa,GAAG,GAAG,EAAE,CAAC,CAAC;IAChE,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,KAAK,CAAC,aAAa,CAAI,GAAW,EAAE,KAAQ;QACzD,MAAM,UAAU,CAAC,GAAG,CAAI,GAAG,WAAW,CAAC,aAAa,GAAG,GAAG,EAAE,EAAE,KAAK,CAAC,CAAC;IACtE,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,gBAAgB,CAAC,GAAW;QACzC,UAAU,CAAC,MAAM,CAAC,GAAG,WAAW,CAAC,aAAa,GAAG,GAAG,EAAE,CAAC,CAAC;IACzD,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { AsyncCache, BaseError, Guards, Is, ObjectHelper, type IError } from \"@twin.org/core\";\nimport { nameof, nameofCamelCase } from \"@twin.org/nameof\";\nimport { FetchError } from \"../errors/fetchError.js\";\nimport { HeaderTypes } from \"../models/headerTypes.js\";\nimport { HttpMethod } from \"../models/httpMethod.js\";\nimport { HttpStatusCode } from \"../models/httpStatusCode.js\";\nimport type { IFetchOptions } from \"../models/IFetchOptions.js\";\nimport type { IHttpHeaders } from \"../models/IHttpHeaders.js\";\nimport { MimeTypes } from \"../models/mimeTypes.js\";\n\n/**\n * Class to helper with fetch operations.\n */\nexport class FetchHelper {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<FetchHelper>();\n\n\t/**\n\t * Prefix to use for cache entries.\n\t * @internal\n\t */\n\tprivate static readonly _CACHE_PREFIX: string = \"fetch_\";\n\n\t/**\n\t * Perform a fetch request.\n\t * @param source The source for the request.\n\t * @param url The url for the request.\n\t * @param method The http method.\n\t * @param body Request to send to the endpoint.\n\t * @param options Options for sending the requests.\n\t * @returns The response.\n\t */\n\tpublic static async fetch(\n\t\tsource: string,\n\t\turl: string,\n\t\tmethod: HttpMethod,\n\t\tbody?: string | Uint8Array,\n\t\toptions?: Omit<IFetchOptions, \"cacheTtlSeconds\">\n\t): Promise<Response> {\n\t\tGuards.string(FetchHelper.CLASS_NAME, nameof(source), source);\n\t\tGuards.string(FetchHelper.CLASS_NAME, nameof(url), url);\n\t\tGuards.arrayOneOf<HttpMethod>(\n\t\t\tFetchHelper.CLASS_NAME,\n\t\t\tnameof(method),\n\t\t\tmethod,\n\t\t\tObject.values(HttpMethod)\n\t\t);\n\t\tif (!Is.undefined(body) && !Is.uint8Array(body)) {\n\t\t\tGuards.string(FetchHelper.CLASS_NAME, nameof(body), body);\n\t\t}\n\t\tif (!Is.undefined(options)) {\n\t\t\tGuards.object<IFetchOptions>(FetchHelper.CLASS_NAME, nameof(options), options);\n\t\t\tif (!Is.undefined(options.headers)) {\n\t\t\t\tGuards.object<IHttpHeaders>(\n\t\t\t\t\tFetchHelper.CLASS_NAME,\n\t\t\t\t\tnameof(options.headers),\n\t\t\t\t\toptions.headers\n\t\t\t\t);\n\t\t\t}\n\t\t\tif (!Is.undefined(options.timeoutMs)) {\n\t\t\t\tGuards.integer(FetchHelper.CLASS_NAME, nameof(options.timeoutMs), options.timeoutMs);\n\t\t\t}\n\t\t\tif (!Is.undefined(options.includeCredentials)) {\n\t\t\t\tGuards.boolean(\n\t\t\t\t\tFetchHelper.CLASS_NAME,\n\t\t\t\t\tnameof(options.includeCredentials),\n\t\t\t\t\toptions.includeCredentials\n\t\t\t\t);\n\t\t\t}\n\t\t\tif (!Is.undefined(options.retryCount)) {\n\t\t\t\tGuards.integer(FetchHelper.CLASS_NAME, nameof(options.retryCount), options.retryCount);\n\t\t\t}\n\t\t\tif (!Is.undefined(options.retryDelayMs)) {\n\t\t\t\tGuards.integer(FetchHelper.CLASS_NAME, nameof(options.retryDelayMs), options.retryDelayMs);\n\t\t\t}\n\t\t}\n\n\t\tlet controller: AbortController | undefined;\n\t\tlet timerId: number | NodeJS.Timeout | undefined;\n\t\tconst retryCount = options?.retryCount ?? 1;\n\t\tconst baseDelayMilliseconds = options?.retryDelayMs ?? 3000;\n\n\t\tlet lastError: IError | undefined;\n\t\tlet attempt;\n\t\tfor (attempt = 0; attempt < retryCount; attempt++) {\n\t\t\tif (attempt > 0) {\n\t\t\t\tconst exponentialBackoffDelay = baseDelayMilliseconds * Math.pow(2, attempt - 1);\n\t\t\t\tawait new Promise(resolve => globalThis.setTimeout(resolve, exponentialBackoffDelay));\n\t\t\t}\n\n\t\t\tif (options?.timeoutMs !== undefined) {\n\t\t\t\tcontroller = new AbortController();\n\t\t\t\ttimerId = globalThis.setTimeout(() => {\n\t\t\t\t\tif (controller) {\n\t\t\t\t\t\tcontroller.abort();\n\t\t\t\t\t}\n\t\t\t\t}, options?.timeoutMs);\n\t\t\t}\n\n\t\t\tlet finalBody;\n\t\t\tif (method === HttpMethod.POST || method === HttpMethod.PUT) {\n\t\t\t\tif (Is.string(body)) {\n\t\t\t\t\tfinalBody = body;\n\t\t\t\t} else if (Is.uint8Array(body)) {\n\t\t\t\t\tfinalBody = new Uint8Array(body);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst requestOptions: RequestInit = {\n\t\t\t\t\tmethod,\n\t\t\t\t\theaders: options?.headers as HeadersInit,\n\t\t\t\t\tbody: finalBody,\n\t\t\t\t\tsignal: controller ? controller.signal : undefined\n\t\t\t\t};\n\t\t\t\tif (Is.boolean(options?.includeCredentials)) {\n\t\t\t\t\trequestOptions.credentials = \"include\";\n\t\t\t\t}\n\n\t\t\t\tconst response = await fetch(url, requestOptions);\n\n\t\t\t\tif (!response.ok && retryCount > 1) {\n\t\t\t\t\tlastError = new FetchError(\n\t\t\t\t\t\tsource,\n\t\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.general`,\n\t\t\t\t\t\t(response.status as HttpStatusCode) ?? HttpStatusCode.internalServerError,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\turl,\n\t\t\t\t\t\t\tstatusText: response.statusText\n\t\t\t\t\t\t}\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\treturn response;\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconst isErr = Is.object<IError>(err);\n\t\t\t\tif (isErr && Is.stringValue(err.message) && err.message.includes(\"Failed to fetch\")) {\n\t\t\t\t\tlastError = new FetchError(\n\t\t\t\t\t\tsource,\n\t\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.connectivity`,\n\t\t\t\t\t\tHttpStatusCode.serviceUnavailable,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\turl\n\t\t\t\t\t\t},\n\t\t\t\t\t\terr\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tconst isAbort = isErr && err.name === \"AbortError\";\n\t\t\t\t\tconst props: { [id: string]: unknown } = { url };\n\t\t\t\t\tlet httpStatus: HttpStatusCode = HttpStatusCode.internalServerError;\n\t\t\t\t\tif (isAbort) {\n\t\t\t\t\t\thttpStatus = HttpStatusCode.requestTimeout;\n\t\t\t\t\t} else if (isErr && \"httpStatus\" in err) {\n\t\t\t\t\t\thttpStatus = err.httpStatus as HttpStatusCode;\n\t\t\t\t\t}\n\t\t\t\t\tif (isErr && \"statusText\" in err) {\n\t\t\t\t\t\tprops.statusText = err.statusText;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (isAbort) {\n\t\t\t\t\t\tlastError = new FetchError(\n\t\t\t\t\t\t\tsource,\n\t\t\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.timeout`,\n\t\t\t\t\t\t\thttpStatus,\n\t\t\t\t\t\t\tprops,\n\t\t\t\t\t\t\terr\n\t\t\t\t\t\t);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tlastError = new FetchError(\n\t\t\t\t\t\t\tsource,\n\t\t\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.general`,\n\t\t\t\t\t\t\thttpStatus,\n\t\t\t\t\t\t\tprops,\n\t\t\t\t\t\t\terr\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} finally {\n\t\t\t\tif (timerId) {\n\t\t\t\t\tglobalThis.clearTimeout(timerId);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (retryCount > 1 && attempt === retryCount) {\n\t\t\tthrow new FetchError(\n\t\t\t\tsource,\n\t\t\t\t`${nameofCamelCase<FetchHelper>()}.retryLimitExceeded`,\n\t\t\t\tHttpStatusCode.internalServerError,\n\t\t\t\t{ url },\n\t\t\t\tlastError\n\t\t\t);\n\t\t}\n\n\t\tthrow lastError as Error;\n\t}\n\n\t/**\n\t * Perform a request in json format.\n\t * @param source The source for the request.\n\t * @param url The url for the request.\n\t * @param method The http method.\n\t * @param requestData Request to send to the endpoint.\n\t * @param options Options for sending the requests.\n\t * @returns The response.\n\t */\n\tpublic static async fetchJson<T, U>(\n\t\tsource: string,\n\t\turl: string,\n\t\tmethod: HttpMethod,\n\t\trequestData?: T,\n\t\toptions?: IFetchOptions\n\t): Promise<U> {\n\t\tif (Is.integer(options?.cacheTtlMs) && options.cacheTtlMs >= 0) {\n\t\t\t// The cache option is set, so call the same method again but without\n\t\t\t// the cache option to get the result and cache it.\n\t\t\tconst cacheResponse = AsyncCache.exec(\n\t\t\t\t`${FetchHelper._CACHE_PREFIX}${url}`,\n\t\t\t\toptions.cacheTtlMs,\n\t\t\t\tasync () =>\n\t\t\t\t\tFetchHelper.fetchJson<T, U>(\n\t\t\t\t\t\tsource,\n\t\t\t\t\t\turl,\n\t\t\t\t\t\tmethod,\n\t\t\t\t\t\trequestData,\n\t\t\t\t\t\tObjectHelper.omit(options, [\"cacheTtlMs\"])\n\t\t\t\t\t)\n\t\t\t);\n\n\t\t\t// If the return value is a promise return it, otherwise continue\n\t\t\t// with the regular processing.\n\t\t\tif (Is.promise(cacheResponse)) {\n\t\t\t\treturn cacheResponse;\n\t\t\t}\n\t\t}\n\n\t\toptions ??= {};\n\t\toptions.headers ??= {};\n\n\t\tif (\n\t\t\tIs.undefined(options.headers[HeaderTypes.ContentType]) &&\n\t\t\t(method === HttpMethod.POST || method === HttpMethod.PUT || method === HttpMethod.PATCH)\n\t\t) {\n\t\t\toptions.headers[HeaderTypes.ContentType] = MimeTypes.Json;\n\t\t}\n\n\t\tconst response = await FetchHelper.fetch(\n\t\t\tsource,\n\t\t\turl,\n\t\t\tmethod,\n\t\t\trequestData ? JSON.stringify(requestData) : undefined,\n\t\t\toptions\n\t\t);\n\n\t\tif (response.ok) {\n\t\t\tif (response.status === HttpStatusCode.noContent) {\n\t\t\t\treturn {} as U;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\treturn (await response.json()) as U;\n\t\t\t} catch (err) {\n\t\t\t\tthrow new FetchError(\n\t\t\t\t\tsource,\n\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.decodingJSON`,\n\t\t\t\t\tHttpStatusCode.badRequest,\n\t\t\t\t\t{ url },\n\t\t\t\t\terr\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\tconst errorResponseData = await response.json();\n\t\tconst errorResponse = BaseError.fromError(errorResponseData);\n\t\tconst isErrorEmpty = BaseError.isEmpty(errorResponse);\n\n\t\tthrow new FetchError(\n\t\t\tsource,\n\t\t\t`${nameofCamelCase<FetchHelper>()}.failureStatusText`,\n\t\t\tresponse.status as HttpStatusCode,\n\t\t\t{\n\t\t\t\tstatusText: response.statusText,\n\t\t\t\turl,\n\t\t\t\tdata: isErrorEmpty ? errorResponseData : undefined\n\t\t\t},\n\t\t\tisErrorEmpty ? undefined : errorResponse\n\t\t);\n\t}\n\n\t/**\n\t * Perform a request for binary data.\n\t * @param source The source for the request.\n\t * @param url The url for the request.\n\t * @param method The http method.\n\t * @param requestData Request to send to the endpoint.\n\t * @param options Options for sending the requests.\n\t * @returns The response.\n\t */\n\tpublic static async fetchBinary<T>(\n\t\tsource: string,\n\t\turl: string,\n\t\tmethod: Extract<HttpMethod, \"GET\" | \"POST\">,\n\t\trequestData?: Uint8Array,\n\t\toptions?: IFetchOptions\n\t): Promise<Uint8Array | T> {\n\t\tif (Is.integer(options?.cacheTtlMs) && options.cacheTtlMs >= 0) {\n\t\t\t// The cache option is set, so call the same method again but without\n\t\t\t// the cache option to get the result and cache it.\n\t\t\tconst cacheResponse = AsyncCache.exec(\n\t\t\t\t`${FetchHelper._CACHE_PREFIX}${url}`,\n\t\t\t\toptions.cacheTtlMs * 1000,\n\t\t\t\tasync () =>\n\t\t\t\t\tFetchHelper.fetchBinary<T>(\n\t\t\t\t\t\tsource,\n\t\t\t\t\t\turl,\n\t\t\t\t\t\tmethod,\n\t\t\t\t\t\trequestData,\n\t\t\t\t\t\tObjectHelper.omit(options, [\"cacheTtlMs\"])\n\t\t\t\t\t)\n\t\t\t);\n\n\t\t\t// If the return value is a promise return it, otherwise continue\n\t\t\t// with the regular processing.\n\t\t\tif (Is.promise(cacheResponse)) {\n\t\t\t\treturn cacheResponse;\n\t\t\t}\n\t\t}\n\n\t\toptions ??= {};\n\t\toptions.headers ??= {};\n\n\t\tif (Is.undefined(options.headers[HeaderTypes.ContentType])) {\n\t\t\toptions.headers[HeaderTypes.ContentType] = MimeTypes.OctetStream;\n\t\t}\n\n\t\tconst response = await FetchHelper.fetch(source, url, method, requestData, options);\n\n\t\tif (response.ok) {\n\t\t\tif (method === HttpMethod.GET) {\n\t\t\t\tif (response.status === HttpStatusCode.noContent) {\n\t\t\t\t\treturn new Uint8Array();\n\t\t\t\t}\n\t\t\t\treturn new Uint8Array(await response.arrayBuffer());\n\t\t\t}\n\t\t\ttry {\n\t\t\t\treturn (await response.json()) as T;\n\t\t\t} catch (err) {\n\t\t\t\tthrow new FetchError(\n\t\t\t\t\tsource,\n\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.decodingJSON`,\n\t\t\t\t\tHttpStatusCode.badRequest,\n\t\t\t\t\t{ url },\n\t\t\t\t\terr\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\tconst errorResponseData = await response.json();\n\t\tconst errorResponse = BaseError.fromError(errorResponseData);\n\t\tconst isErrorEmpty = BaseError.isEmpty(errorResponse);\n\n\t\tthrow new FetchError(\n\t\t\tsource,\n\t\t\t`${nameofCamelCase<FetchHelper>()}.failureStatusText`,\n\t\t\tresponse.status as HttpStatusCode,\n\t\t\t{\n\t\t\t\tstatusText: response.statusText,\n\t\t\t\turl,\n\t\t\t\tdata: isErrorEmpty ? errorResponseData : undefined\n\t\t\t},\n\t\t\tisErrorEmpty ? undefined : errorResponse\n\t\t);\n\t}\n\n\t/**\n\t * Clears the cache.\n\t */\n\tpublic static clearCache(): void {\n\t\tAsyncCache.clearCache(FetchHelper._CACHE_PREFIX);\n\t}\n\n\t/**\n\t * Get a cache entry.\n\t * @param url The url for the request.\n\t * @returns The cache entry if it exists.\n\t */\n\tpublic static async getCacheEntry<T>(url: string): Promise<T | undefined> {\n\t\treturn AsyncCache.get<T>(`${FetchHelper._CACHE_PREFIX}${url}`);\n\t}\n\n\t/**\n\t * Set a cache entry.\n\t * @param url The url for the request.\n\t * @param value The value to cache.\n\t * @returns The cache entry if it exists.\n\t */\n\tpublic static async setCacheEntry<T>(url: string, value: T): Promise<void> {\n\t\tawait AsyncCache.set<T>(`${FetchHelper._CACHE_PREFIX}${url}`, value);\n\t}\n\n\t/**\n\t * Remove a cache entry.\n\t * @param url The url for the request.\n\t */\n\tpublic static removeCacheEntry(url: string): void {\n\t\tAsyncCache.remove(`${FetchHelper._CACHE_PREFIX}${url}`);\n\t}\n}\n"]}
1
+ {"version":3,"file":"fetchHelper.js","sourceRoot":"","sources":["../../../src/utils/fetchHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,EAAE,YAAY,EAAe,MAAM,gBAAgB,CAAC;AAE9F,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACvD,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAG7D,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAEnD;;GAEG;AACH,MAAM,OAAO,WAAW;IACvB;;OAEG;IACI,MAAM,CAAU,UAAU,iBAAiC;IAElE;;;OAGG;IACK,MAAM,CAAU,aAAa,GAAW,QAAQ,CAAC;IAEzD;;;;;;;;OAQG;IACI,MAAM,CAAC,KAAK,CAAC,KAAK,CACxB,MAAc,EACd,GAAW,EACX,MAAkB,EAClB,IAA0B,EAC1B,OAAgD;QAEhD,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,UAAU,YAAkB,MAAM,CAAC,CAAC;QAC9D,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,UAAU,SAAe,GAAG,CAAC,CAAC;QACxD,MAAM,CAAC,UAAU,CAChB,WAAW,CAAC,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,GAAG,WAAW,CAAC,aAAa,GAAG,GAAG,EAAE,CAAC,CAAC;IACzD,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { AsyncCache, BaseError, Guards, Is, ObjectHelper, type IError } from \"@twin.org/core\";\nimport { nameof, nameofCamelCase } from \"@twin.org/nameof\";\nimport { FetchError } from \"../errors/fetchError.js\";\nimport { HeaderTypes } from \"../models/headerTypes.js\";\nimport { HttpMethod } from \"../models/httpMethod.js\";\nimport { HttpStatusCode } from \"../models/httpStatusCode.js\";\nimport type { IFetchOptions } from \"../models/IFetchOptions.js\";\nimport type { IHttpHeaders } from \"../models/IHttpHeaders.js\";\nimport { MimeTypes } from \"../models/mimeTypes.js\";\n\n/**\n * Class to helper with fetch operations.\n */\nexport class FetchHelper {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<FetchHelper>();\n\n\t/**\n\t * Prefix to use for cache entries.\n\t * @internal\n\t */\n\tprivate static readonly _CACHE_PREFIX: string = \"fetch_\";\n\n\t/**\n\t * Perform a fetch request.\n\t * @param source The source for the request.\n\t * @param url The url for the request.\n\t * @param method The http method.\n\t * @param body Request to send to the endpoint.\n\t * @param options Options for sending the requests.\n\t * @returns The response.\n\t */\n\tpublic static async fetch(\n\t\tsource: string,\n\t\turl: string,\n\t\tmethod: HttpMethod,\n\t\tbody?: string | Uint8Array,\n\t\toptions?: Omit<IFetchOptions, \"cacheTtlSeconds\">\n\t): Promise<Response> {\n\t\tGuards.string(FetchHelper.CLASS_NAME, nameof(source), source);\n\t\tGuards.string(FetchHelper.CLASS_NAME, nameof(url), url);\n\t\tGuards.arrayOneOf<HttpMethod>(\n\t\t\tFetchHelper.CLASS_NAME,\n\t\t\tnameof(method),\n\t\t\tmethod,\n\t\t\tObject.values(HttpMethod)\n\t\t);\n\t\tif (!Is.undefined(body) && !Is.uint8Array(body)) {\n\t\t\tGuards.string(FetchHelper.CLASS_NAME, nameof(body), body);\n\t\t}\n\t\tif (!Is.undefined(options)) {\n\t\t\tGuards.object<IFetchOptions>(FetchHelper.CLASS_NAME, nameof(options), options);\n\t\t\tif (!Is.undefined(options.headers)) {\n\t\t\t\tGuards.object<IHttpHeaders>(\n\t\t\t\t\tFetchHelper.CLASS_NAME,\n\t\t\t\t\tnameof(options.headers),\n\t\t\t\t\toptions.headers\n\t\t\t\t);\n\t\t\t}\n\t\t\tif (!Is.undefined(options.timeoutMs)) {\n\t\t\t\tGuards.integer(FetchHelper.CLASS_NAME, nameof(options.timeoutMs), options.timeoutMs);\n\t\t\t}\n\t\t\tif (!Is.undefined(options.includeCredentials)) {\n\t\t\t\tGuards.boolean(\n\t\t\t\t\tFetchHelper.CLASS_NAME,\n\t\t\t\t\tnameof(options.includeCredentials),\n\t\t\t\t\toptions.includeCredentials\n\t\t\t\t);\n\t\t\t}\n\t\t\tif (!Is.undefined(options.retryCount)) {\n\t\t\t\tGuards.integer(FetchHelper.CLASS_NAME, nameof(options.retryCount), options.retryCount);\n\t\t\t}\n\t\t\tif (!Is.undefined(options.retryDelayMs)) {\n\t\t\t\tGuards.integer(FetchHelper.CLASS_NAME, nameof(options.retryDelayMs), options.retryDelayMs);\n\t\t\t}\n\t\t}\n\n\t\tlet controller: AbortController | undefined;\n\t\tlet timerId: number | NodeJS.Timeout | undefined;\n\t\tconst retryCount = options?.retryCount ?? 1;\n\t\tconst baseDelayMilliseconds = options?.retryDelayMs ?? 3000;\n\n\t\tlet lastError: IError | undefined;\n\t\tlet attempt;\n\t\tfor (attempt = 0; attempt < retryCount; attempt++) {\n\t\t\tif (attempt > 0) {\n\t\t\t\tconst exponentialBackoffDelay = baseDelayMilliseconds * Math.pow(2, attempt - 1);\n\t\t\t\tawait new Promise(resolve => globalThis.setTimeout(resolve, exponentialBackoffDelay));\n\t\t\t}\n\n\t\t\tif (options?.timeoutMs !== undefined) {\n\t\t\t\tcontroller = new AbortController();\n\t\t\t\ttimerId = globalThis.setTimeout(() => {\n\t\t\t\t\tif (controller) {\n\t\t\t\t\t\tcontroller.abort();\n\t\t\t\t\t}\n\t\t\t\t}, options?.timeoutMs);\n\t\t\t}\n\n\t\t\tlet finalBody;\n\t\t\tif (method === HttpMethod.POST || method === HttpMethod.PUT || method === HttpMethod.PATCH) {\n\t\t\t\tif (Is.string(body)) {\n\t\t\t\t\tfinalBody = body;\n\t\t\t\t} else if (Is.uint8Array(body)) {\n\t\t\t\t\tfinalBody = new Uint8Array(body);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tconst requestOptions: RequestInit = {\n\t\t\t\t\tmethod,\n\t\t\t\t\theaders: options?.headers as HeadersInit,\n\t\t\t\t\tbody: finalBody,\n\t\t\t\t\tsignal: controller ? controller.signal : undefined\n\t\t\t\t};\n\t\t\t\tif (Is.boolean(options?.includeCredentials)) {\n\t\t\t\t\trequestOptions.credentials = \"include\";\n\t\t\t\t}\n\n\t\t\t\tconst response = await fetch(url, requestOptions);\n\n\t\t\t\tif (!response.ok && retryCount > 1) {\n\t\t\t\t\tlastError = new FetchError(\n\t\t\t\t\t\tsource,\n\t\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.general`,\n\t\t\t\t\t\t(response.status as HttpStatusCode) ?? HttpStatusCode.internalServerError,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\turl,\n\t\t\t\t\t\t\tstatusText: response.statusText\n\t\t\t\t\t\t}\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\treturn response;\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconst isErr = Is.object<IError>(err);\n\t\t\t\tif (isErr && Is.stringValue(err.message) && err.message.includes(\"Failed to fetch\")) {\n\t\t\t\t\tlastError = new FetchError(\n\t\t\t\t\t\tsource,\n\t\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.connectivity`,\n\t\t\t\t\t\tHttpStatusCode.serviceUnavailable,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\turl\n\t\t\t\t\t\t},\n\t\t\t\t\t\terr\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tconst isAbort = isErr && err.name === \"AbortError\";\n\t\t\t\t\tconst props: { [id: string]: unknown } = { url };\n\t\t\t\t\tlet httpStatus: HttpStatusCode = HttpStatusCode.internalServerError;\n\t\t\t\t\tif (isAbort) {\n\t\t\t\t\t\thttpStatus = HttpStatusCode.requestTimeout;\n\t\t\t\t\t} else if (isErr && \"httpStatus\" in err) {\n\t\t\t\t\t\thttpStatus = err.httpStatus as HttpStatusCode;\n\t\t\t\t\t}\n\t\t\t\t\tif (isErr && \"statusText\" in err) {\n\t\t\t\t\t\tprops.statusText = err.statusText;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (isAbort) {\n\t\t\t\t\t\tlastError = new FetchError(\n\t\t\t\t\t\t\tsource,\n\t\t\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.timeout`,\n\t\t\t\t\t\t\thttpStatus,\n\t\t\t\t\t\t\tprops,\n\t\t\t\t\t\t\terr\n\t\t\t\t\t\t);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tlastError = new FetchError(\n\t\t\t\t\t\t\tsource,\n\t\t\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.general`,\n\t\t\t\t\t\t\thttpStatus,\n\t\t\t\t\t\t\tprops,\n\t\t\t\t\t\t\terr\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} finally {\n\t\t\t\tif (timerId) {\n\t\t\t\t\tglobalThis.clearTimeout(timerId);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (retryCount > 1 && attempt === retryCount) {\n\t\t\tthrow new FetchError(\n\t\t\t\tsource,\n\t\t\t\t`${nameofCamelCase<FetchHelper>()}.retryLimitExceeded`,\n\t\t\t\tHttpStatusCode.internalServerError,\n\t\t\t\t{ url },\n\t\t\t\tlastError\n\t\t\t);\n\t\t}\n\n\t\tthrow lastError as Error;\n\t}\n\n\t/**\n\t * Perform a request in json format.\n\t * @param source The source for the request.\n\t * @param url The url for the request.\n\t * @param method The http method.\n\t * @param requestData Request to send to the endpoint.\n\t * @param options Options for sending the requests.\n\t * @returns The response.\n\t */\n\tpublic static async fetchJson<T, U>(\n\t\tsource: string,\n\t\turl: string,\n\t\tmethod: HttpMethod,\n\t\trequestData?: T,\n\t\toptions?: IFetchOptions\n\t): Promise<U> {\n\t\tif (Is.integer(options?.cacheTtlMs) && options.cacheTtlMs >= 0) {\n\t\t\t// The cache option is set, so call the same method again but without\n\t\t\t// the cache option to get the result and cache it.\n\t\t\tconst cacheResponse = AsyncCache.exec(\n\t\t\t\t`${FetchHelper._CACHE_PREFIX}${url}`,\n\t\t\t\toptions.cacheTtlMs,\n\t\t\t\tasync () =>\n\t\t\t\t\tFetchHelper.fetchJson<T, U>(\n\t\t\t\t\t\tsource,\n\t\t\t\t\t\turl,\n\t\t\t\t\t\tmethod,\n\t\t\t\t\t\trequestData,\n\t\t\t\t\t\tObjectHelper.omit(options, [\"cacheTtlMs\"])\n\t\t\t\t\t)\n\t\t\t);\n\n\t\t\t// If the return value is a promise return it, otherwise continue\n\t\t\t// with the regular processing.\n\t\t\tif (Is.promise(cacheResponse)) {\n\t\t\t\treturn cacheResponse;\n\t\t\t}\n\t\t}\n\n\t\toptions ??= {};\n\t\toptions.headers ??= {};\n\n\t\tif (\n\t\t\tIs.undefined(options.headers[HeaderTypes.ContentType]) &&\n\t\t\t(method === HttpMethod.POST || method === HttpMethod.PUT || method === HttpMethod.PATCH)\n\t\t) {\n\t\t\toptions.headers[HeaderTypes.ContentType] = MimeTypes.Json;\n\t\t}\n\n\t\tconst response = await FetchHelper.fetch(\n\t\t\tsource,\n\t\t\turl,\n\t\t\tmethod,\n\t\t\trequestData ? JSON.stringify(requestData) : undefined,\n\t\t\toptions\n\t\t);\n\n\t\tif (response.ok) {\n\t\t\tif (response.status === HttpStatusCode.noContent) {\n\t\t\t\treturn {} as U;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\treturn (await response.json()) as U;\n\t\t\t} catch (err) {\n\t\t\t\tthrow new FetchError(\n\t\t\t\t\tsource,\n\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.decodingJSON`,\n\t\t\t\t\tHttpStatusCode.badRequest,\n\t\t\t\t\t{ url },\n\t\t\t\t\terr\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\tconst errorResponseData = await response.json();\n\t\tconst errorResponse = BaseError.fromError(errorResponseData);\n\t\tconst isErrorEmpty = BaseError.isEmpty(errorResponse);\n\n\t\tthrow new FetchError(\n\t\t\tsource,\n\t\t\t`${nameofCamelCase<FetchHelper>()}.failureStatusText`,\n\t\t\tresponse.status as HttpStatusCode,\n\t\t\t{\n\t\t\t\tstatusText: response.statusText,\n\t\t\t\turl,\n\t\t\t\tdata: isErrorEmpty ? errorResponseData : undefined\n\t\t\t},\n\t\t\tisErrorEmpty ? undefined : errorResponse\n\t\t);\n\t}\n\n\t/**\n\t * Perform a request for binary data.\n\t * @param source The source for the request.\n\t * @param url The url for the request.\n\t * @param method The http method.\n\t * @param requestData Request to send to the endpoint.\n\t * @param options Options for sending the requests.\n\t * @returns The response.\n\t */\n\tpublic static async fetchBinary<T>(\n\t\tsource: string,\n\t\turl: string,\n\t\tmethod: Extract<HttpMethod, \"GET\" | \"POST\">,\n\t\trequestData?: Uint8Array,\n\t\toptions?: IFetchOptions\n\t): Promise<Uint8Array | T> {\n\t\tif (Is.integer(options?.cacheTtlMs) && options.cacheTtlMs >= 0) {\n\t\t\t// The cache option is set, so call the same method again but without\n\t\t\t// the cache option to get the result and cache it.\n\t\t\tconst cacheResponse = AsyncCache.exec(\n\t\t\t\t`${FetchHelper._CACHE_PREFIX}${url}`,\n\t\t\t\toptions.cacheTtlMs * 1000,\n\t\t\t\tasync () =>\n\t\t\t\t\tFetchHelper.fetchBinary<T>(\n\t\t\t\t\t\tsource,\n\t\t\t\t\t\turl,\n\t\t\t\t\t\tmethod,\n\t\t\t\t\t\trequestData,\n\t\t\t\t\t\tObjectHelper.omit(options, [\"cacheTtlMs\"])\n\t\t\t\t\t)\n\t\t\t);\n\n\t\t\t// If the return value is a promise return it, otherwise continue\n\t\t\t// with the regular processing.\n\t\t\tif (Is.promise(cacheResponse)) {\n\t\t\t\treturn cacheResponse;\n\t\t\t}\n\t\t}\n\n\t\toptions ??= {};\n\t\toptions.headers ??= {};\n\n\t\tif (Is.undefined(options.headers[HeaderTypes.ContentType])) {\n\t\t\toptions.headers[HeaderTypes.ContentType] = MimeTypes.OctetStream;\n\t\t}\n\n\t\tconst response = await FetchHelper.fetch(source, url, method, requestData, options);\n\n\t\tif (response.ok) {\n\t\t\tif (method === HttpMethod.GET) {\n\t\t\t\tif (response.status === HttpStatusCode.noContent) {\n\t\t\t\t\treturn new Uint8Array();\n\t\t\t\t}\n\t\t\t\treturn new Uint8Array(await response.arrayBuffer());\n\t\t\t}\n\t\t\ttry {\n\t\t\t\treturn (await response.json()) as T;\n\t\t\t} catch (err) {\n\t\t\t\tthrow new FetchError(\n\t\t\t\t\tsource,\n\t\t\t\t\t`${nameofCamelCase<FetchHelper>()}.decodingJSON`,\n\t\t\t\t\tHttpStatusCode.badRequest,\n\t\t\t\t\t{ url },\n\t\t\t\t\terr\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\tconst errorResponseData = await response.json();\n\t\tconst errorResponse = BaseError.fromError(errorResponseData);\n\t\tconst isErrorEmpty = BaseError.isEmpty(errorResponse);\n\n\t\tthrow new FetchError(\n\t\t\tsource,\n\t\t\t`${nameofCamelCase<FetchHelper>()}.failureStatusText`,\n\t\t\tresponse.status as HttpStatusCode,\n\t\t\t{\n\t\t\t\tstatusText: response.statusText,\n\t\t\t\turl,\n\t\t\t\tdata: isErrorEmpty ? errorResponseData : undefined\n\t\t\t},\n\t\t\tisErrorEmpty ? undefined : errorResponse\n\t\t);\n\t}\n\n\t/**\n\t * Clears the cache.\n\t */\n\tpublic static clearCache(): void {\n\t\tAsyncCache.clearCache(FetchHelper._CACHE_PREFIX);\n\t}\n\n\t/**\n\t * Get a cache entry.\n\t * @param url The url for the request.\n\t * @returns The cache entry if it exists.\n\t */\n\tpublic static async getCacheEntry<T>(url: string): Promise<T | undefined> {\n\t\treturn AsyncCache.get<T>(`${FetchHelper._CACHE_PREFIX}${url}`);\n\t}\n\n\t/**\n\t * Set a cache entry.\n\t * @param url The url for the request.\n\t * @param value The value to cache.\n\t * @returns The cache entry if it exists.\n\t */\n\tpublic static async setCacheEntry<T>(url: string, value: T): Promise<void> {\n\t\tawait AsyncCache.set<T>(`${FetchHelper._CACHE_PREFIX}${url}`, value);\n\t}\n\n\t/**\n\t * Remove a cache entry.\n\t * @param url The url for the request.\n\t */\n\tpublic static removeCacheEntry(url: string): void {\n\t\tAsyncCache.remove(`${FetchHelper._CACHE_PREFIX}${url}`);\n\t}\n}\n"]}
@@ -1,10 +1,47 @@
1
1
  // Copyright 2024 IOTA Stiftung.
2
2
  // SPDX-License-Identifier: Apache-2.0.
3
- import { Is } from "@twin.org/core";
3
+ import { ArrayHelper, GeneralError, Guards, Is } from "@twin.org/core";
4
+ import { HeaderTypes } from "../models/headerTypes.js";
4
5
  /**
5
6
  * Class to helper with header operations.
6
7
  */
7
8
  export class HeaderHelper {
9
+ /**
10
+ * Runtime name for the class.
11
+ */
12
+ static CLASS_NAME = "HeaderHelper";
13
+ /**
14
+ * Regex for valid correlation ID (alphanumeric, dash, underscore).
15
+ * Examples: `request_123`, `trace-id-456`, `ABC_789`.
16
+ * @internal
17
+ */
18
+ static _CORRELATION_ID_REGEX = /^[\w-]+$/;
19
+ /**
20
+ * Regex for valid Accept-Language tags and wildcard values.
21
+ * Supports wildcard entries and common BCP 47 style tags.
22
+ * Examples: `*`, `en`, `en-GB`, `es-419`, `zh-Hant`, `zh-Hant-TW`.
23
+ * @internal
24
+ */
25
+ static _ACCEPT_LANGUAGE_TAG_REGEX = /^(\*|[A-Za-z]{1,8}(?:-[\dA-Za-z]{1,8})*)$/;
26
+ /**
27
+ * Regex for valid Accept-Language quality parameters.
28
+ * Supports quality values from `0` to `1` with up to three decimal places.
29
+ * Examples: `q=1`, `q=0.9`, `q=0.875`, `q=0`, `q=1.0`.
30
+ * @internal
31
+ */
32
+ static _ACCEPT_LANGUAGE_QUALITY_REGEX = /^q=(0(?:\.\d{1,3})?|1(?:\.0{1,3})?)$/i;
33
+ /**
34
+ * Regex for valid IPv4 addresses.
35
+ * Examples: `127.0.0.1`, `192.168.1.10`, `255.255.255.255`.
36
+ * @internal
37
+ */
38
+ static _IP_V4_REGEX = /^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}$/;
39
+ /**
40
+ * Regex for valid IPv6 addresses.
41
+ * Examples: `2001:0db8:85a3:0000:0000:8a2e:0370:7334`, `2001:db8::`, `::1`.
42
+ * @internal
43
+ */
44
+ static _IP_V6_REGEX = /^((?:[\dA-Fa-f]{1,4}:){7}[\dA-Fa-f]{1,4}|(?:[\dA-Fa-f]{1,4}:){1,7}:|:(?::[\dA-Fa-f]{1,4}){1,7})$/;
8
45
  /**
9
46
  * Create a bearer token header.
10
47
  * @param token The token to create the header for.
@@ -30,5 +67,350 @@ export class HeaderHelper {
30
67
  }
31
68
  return "";
32
69
  }
70
+ /**
71
+ * Extract parsed language preferences from the Accept-Language header.
72
+ * @param headers The HTTP request headers.
73
+ * @returns The parsed language preferences ordered by highest quality first, or undefined if missing or invalid.
74
+ */
75
+ static extractAcceptLanguage(headers) {
76
+ return HeaderHelper.parseAcceptLanguage(headers?.[HeaderTypes.AcceptLanguage]);
77
+ }
78
+ /**
79
+ * Parse one or more Accept-Language header values into language preferences.
80
+ * @param acceptLanguage The Accept-Language header value or values.
81
+ * @returns The parsed language preferences ordered by highest quality first, or undefined if missing or if any entry is invalid.
82
+ */
83
+ static parseAcceptLanguage(acceptLanguage) {
84
+ let headerValues = [];
85
+ if (Is.array(acceptLanguage)) {
86
+ headerValues = acceptLanguage
87
+ .map(headerValue => headerValue.trim())
88
+ .filter(headerValue => headerValue.length > 0);
89
+ }
90
+ else if (Is.stringValue(acceptLanguage)) {
91
+ headerValues = [acceptLanguage.trim()];
92
+ }
93
+ if (headerValues.length > 0) {
94
+ const entries = headerValues
95
+ .flatMap(headerValue => headerValue.split(","))
96
+ .map(segment => segment.trim())
97
+ .filter(segment => segment.length > 0);
98
+ const parsedEntries = [];
99
+ for (const entry of entries) {
100
+ const [languagePart, ...parameterParts] = entry.split(";").map(part => part.trim());
101
+ if (!HeaderHelper._ACCEPT_LANGUAGE_TAG_REGEX.test(languagePart)) {
102
+ return undefined;
103
+ }
104
+ let quality = 1;
105
+ for (const parameterPart of parameterParts) {
106
+ if (parameterPart.length > 0) {
107
+ if (parameterPart.startsWith("q=") || parameterPart.startsWith("Q=")) {
108
+ const qualityMatch = HeaderHelper._ACCEPT_LANGUAGE_QUALITY_REGEX.exec(parameterPart);
109
+ if (!qualityMatch?.[1]) {
110
+ return undefined;
111
+ }
112
+ quality = Number(qualityMatch[1]);
113
+ }
114
+ }
115
+ }
116
+ parsedEntries.push({
117
+ language: languagePart,
118
+ quality
119
+ });
120
+ }
121
+ if (parsedEntries.length > 0) {
122
+ return parsedEntries.sort((a, b) => b.quality - a.quality);
123
+ }
124
+ }
125
+ }
126
+ /**
127
+ * Extract client IP addresses from HTTP request headers.
128
+ * Checks all `X-Forwarded-For` and `X-Real-IP` header values for proxied requests.
129
+ * @param headers The HTTP request headers.
130
+ * @returns The extracted client IP addresses in header order.
131
+ */
132
+ static extractClientIps(headers) {
133
+ const ips = [];
134
+ const forwardedForValues = HeaderHelper.getHeaderValues(headers?.["x-forwarded-for"]);
135
+ for (const forwardedFor of forwardedForValues) {
136
+ const forwardedIps = forwardedFor
137
+ .split(",")
138
+ .map(ip => ip.trim())
139
+ .filter(ip => HeaderHelper.isIpAddress(ip));
140
+ ips.push(...forwardedIps);
141
+ }
142
+ const realIpValues = HeaderHelper.getHeaderValues(headers?.["x-real-ip"]);
143
+ for (const realIp of realIpValues) {
144
+ if (HeaderHelper.isIpAddress(realIp)) {
145
+ ips.push(realIp);
146
+ }
147
+ }
148
+ return ips;
149
+ }
150
+ /**
151
+ * Extract the User-Agent header from the HTTP request context.
152
+ * @param headers The HTTP request headers.
153
+ * @param maxLength Optional maximum length for the User-Agent string to prevent excessively long values.
154
+ * @returns The user agent string or undefined if not available.
155
+ */
156
+ static extractUserAgent(headers, maxLength) {
157
+ const headerValues = HeaderHelper.getHeaderValues(headers?.[HeaderTypes.UserAgent]);
158
+ for (const headerValue of headerValues) {
159
+ return Is.integer(maxLength) ? headerValue.slice(0, maxLength) : headerValue;
160
+ }
161
+ }
162
+ /**
163
+ * Extract a correlation ID for request tracing from the X-Correlation-ID header.
164
+ * @param headers The HTTP request headers.
165
+ * @param maxLength Optional maximum length for the extracted correlation ID.
166
+ * @returns The correlation ID, or undefined if the header is missing or invalid.
167
+ */
168
+ static extractCorrelationId(headers, maxLength) {
169
+ const headerValues = HeaderHelper.getHeaderValues(headers?.["x-correlation-id"]);
170
+ for (const headerValue of headerValues) {
171
+ if (HeaderHelper._CORRELATION_ID_REGEX.test(headerValue)) {
172
+ return Is.integer(maxLength) ? headerValue.slice(0, maxLength) : headerValue;
173
+ }
174
+ }
175
+ }
176
+ /**
177
+ * Validate if a string is a valid IP address (IPv4 or IPv6).
178
+ * @param ip The IP address to validate.
179
+ * @returns True if valid, false otherwise.
180
+ */
181
+ static isIpAddress(ip) {
182
+ if (!Is.stringValue(ip)) {
183
+ return false;
184
+ }
185
+ return HeaderHelper.isIpAddressV4(ip) || HeaderHelper.isIpAddressV6(ip);
186
+ }
187
+ /**
188
+ * Validate if a string is a valid IP address IPv4.
189
+ * @param ip The IP address to validate.
190
+ * @returns True if valid, false otherwise.
191
+ */
192
+ static isIpAddressV4(ip) {
193
+ if (!Is.stringValue(ip)) {
194
+ return false;
195
+ }
196
+ return HeaderHelper._IP_V4_REGEX.test(ip);
197
+ }
198
+ /**
199
+ * Validate if a string is a valid IP address IPv6.
200
+ * @param ip The IP address to validate.
201
+ * @returns True if valid, false otherwise.
202
+ */
203
+ static isIpAddressV6(ip) {
204
+ if (!Is.stringValue(ip)) {
205
+ return false;
206
+ }
207
+ return HeaderHelper._IP_V6_REGEX.test(ip);
208
+ }
209
+ /**
210
+ * Extract the first occurrence of properties from a Link header for a specific relation type.
211
+ * @param linkHeader The Link header value in format `<url>; rel="..."; param1=""; param2=""`.
212
+ * @param relation The relation type to extract.
213
+ * @returns The extracted URL, rel and optional params or undefined if invalid/missing.
214
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Link
215
+ */
216
+ static extractLinkHeaderRelation(linkHeader, relation) {
217
+ const headers = HeaderHelper.extractLinkHeaders(linkHeader);
218
+ if (Is.arrayValue(headers)) {
219
+ return headers.find(h => HeaderHelper.matchesLinkHeaderRelation(h.rel, relation));
220
+ }
221
+ }
222
+ /**
223
+ * Extract multiple properties from a Link header for a specific relation type.
224
+ * @param linkHeader The Link header value in format `<url>; rel="..."; param1=""; param2=""`.
225
+ * @param relation The relation type to extract.
226
+ * @returns The extracted URL, rel and optional params or undefined if invalid/missing.
227
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Link
228
+ */
229
+ static extractLinkHeaderRelations(linkHeader, relation) {
230
+ const headers = HeaderHelper.extractLinkHeaders(linkHeader);
231
+ if (Is.arrayValue(headers)) {
232
+ return headers.filter(h => HeaderHelper.matchesLinkHeaderRelation(h.rel, relation));
233
+ }
234
+ }
235
+ /**
236
+ * Extract the link headers.
237
+ * @param linkHeader The Link header value in format `<url>; rel="..."; param1=""; param2=""`.
238
+ * @returns The extracted possible array of URL, rel and optional params or undefined if invalid/missing.
239
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Link
240
+ */
241
+ static extractLinkHeaders(linkHeader) {
242
+ const linkHeaderArray = ArrayHelper.fromObjectOrArray(linkHeader);
243
+ if (Is.arrayValue(linkHeaderArray)) {
244
+ const results = [];
245
+ for (const singleLinkHeader of linkHeaderArray) {
246
+ const segments = HeaderHelper.extractLinkHeaderSegments(singleLinkHeader);
247
+ results.push(...segments.map(l => HeaderHelper.extractLinkHeader(l)).filter(h => !Is.empty(h)));
248
+ }
249
+ return results;
250
+ }
251
+ return undefined;
252
+ }
253
+ /**
254
+ * Split a combined Link header value into individual link-value segments, comma separated.
255
+ * @param linkHeader Raw Link header string.
256
+ * @returns Array of individual link-value segments.
257
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Link
258
+ */
259
+ static extractLinkHeaderSegments(linkHeader) {
260
+ if (!Is.stringValue(linkHeader)) {
261
+ return [];
262
+ }
263
+ const trimmed = linkHeader.trim();
264
+ if (Is.empty(trimmed)) {
265
+ return [];
266
+ }
267
+ return trimmed
268
+ .split(/,(?=\s*<)/)
269
+ .map(s => s.trim())
270
+ .filter(s => !Is.empty(s));
271
+ }
272
+ /**
273
+ * Extract the properties from a Link header.
274
+ * @param linkHeader The Link header value in format `<url>; rel="..."; param1=""; param2=""`.
275
+ * @returns The extracted URL, rel and optional params or undefined if invalid/missing.
276
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Link
277
+ */
278
+ static extractLinkHeader(linkHeader) {
279
+ if (!Is.stringValue(linkHeader)) {
280
+ return undefined;
281
+ }
282
+ const parts = linkHeader.split(";");
283
+ if (parts.length >= 2) {
284
+ let url;
285
+ let urlQueryParams;
286
+ const urlMatch = /<([^>]+)>/.exec(parts[0]);
287
+ if (Is.stringValue(urlMatch?.[1])) {
288
+ url = urlMatch[1];
289
+ const queryIndex = url.indexOf("?");
290
+ if (queryIndex !== -1) {
291
+ const urlParamsString = url.slice(queryIndex + 1);
292
+ const queryParts = urlParamsString.split("&");
293
+ for (const queryPart of queryParts) {
294
+ const [key, value] = queryPart.split("=");
295
+ if (Is.stringValue(key) && Is.stringValue(value)) {
296
+ urlQueryParams ??= {};
297
+ urlQueryParams[key] = decodeURIComponent(value);
298
+ }
299
+ }
300
+ }
301
+ }
302
+ let rel;
303
+ const params = {};
304
+ for (let i = 1; i < parts.length; i++) {
305
+ const relMatch = /rel="([^"]+)"/.exec(parts[i].trim());
306
+ if (relMatch?.[1]) {
307
+ rel = HeaderHelper.normalizeLinkHeaderRelations(relMatch[1]);
308
+ }
309
+ else {
310
+ const paramMatch = /([^=]+)="([^"]+)"/.exec(parts[i].trim());
311
+ if (paramMatch?.[1] && paramMatch?.[2]) {
312
+ params[paramMatch[1]] = paramMatch[2];
313
+ }
314
+ }
315
+ }
316
+ if (Is.stringValue(url) && Is.arrayValue(rel)) {
317
+ return {
318
+ url,
319
+ urlQueryParams,
320
+ rel,
321
+ params: Object.keys(params).length > 0 ? params : undefined
322
+ };
323
+ }
324
+ }
325
+ return undefined;
326
+ }
327
+ /**
328
+ * Create a compliant Link header.
329
+ * @param url The URL to include in the Link header.
330
+ * @param urlQueryParams Optional query parameters to include in the URL.
331
+ * @param rel The relation type (e.g., "next", "prev", "self").
332
+ * @returns The formatted Link header string.
333
+ * @throws GeneralError if the URL or rel are invalid.
334
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Link
335
+ */
336
+ static createLinkHeader(url, urlQueryParams, rel, params) {
337
+ Guards.stringValue(HeaderHelper.CLASS_NAME, "url", url);
338
+ const relationValues = Is.string(rel)
339
+ ? HeaderHelper.normalizeLinkHeaderRelations(rel)
340
+ : ArrayHelper.fromObjectOrArray(rel);
341
+ Guards.arrayValue(HeaderHelper.CLASS_NAME, "rel", relationValues);
342
+ if (url.includes(">")) {
343
+ throw new GeneralError(HeaderHelper.CLASS_NAME, "invalidLinkHeaderURL");
344
+ }
345
+ for (let i = 0; i < relationValues.length; i++) {
346
+ Guards.stringValue(HeaderHelper.CLASS_NAME, `${"rel"}.${i.toString()}`, relationValues[i]);
347
+ if (relationValues[i].includes('"') || relationValues[i].includes(" ")) {
348
+ throw new GeneralError(HeaderHelper.CLASS_NAME, "invalidLinkHeaderRel");
349
+ }
350
+ }
351
+ if (Is.objectValue(urlQueryParams)) {
352
+ const queryParamsString = Object.entries(urlQueryParams)
353
+ .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
354
+ .join("&");
355
+ const queryIndex = url.indexOf("?");
356
+ if (queryIndex === -1) {
357
+ url += `?${queryParamsString}`;
358
+ }
359
+ else if (queryIndex === url.length - 1) {
360
+ url += queryParamsString;
361
+ }
362
+ else {
363
+ url += `&${queryParamsString}`;
364
+ }
365
+ }
366
+ return `<${url}>; rel="${relationValues.join(" ")}"${params
367
+ ? Object.entries(params)
368
+ .map(([key, value]) => `; ${key}="${value}"`)
369
+ .join("")
370
+ : ""}`;
371
+ }
372
+ /**
373
+ * Does the relation selector match any of the link header relations.
374
+ * @param relations The relations from the header.
375
+ * @param relation The relation selector to test.
376
+ * @returns True if the selector matches any relation.
377
+ * @internal
378
+ */
379
+ static matchesLinkHeaderRelation(relations, relation) {
380
+ if (Is.string(relation)) {
381
+ return relations.includes(relation);
382
+ }
383
+ return relations.some(relValue => relation.test(relValue));
384
+ }
385
+ /**
386
+ * Normalize a relation string into tokens.
387
+ * @param relation The relation string.
388
+ * @returns The relation tokens.
389
+ * @internal
390
+ */
391
+ static normalizeLinkHeaderRelations(relation) {
392
+ return relation
393
+ .split(/\s+/)
394
+ .map(relValue => relValue.trim())
395
+ .filter(relValue => !Is.empty(relValue));
396
+ }
397
+ /**
398
+ * Get all non-empty values from a header that may be a string or string array.
399
+ * @param header The header value (string, string array, or undefined).
400
+ * @returns The trimmed non-empty string values.
401
+ * @internal
402
+ */
403
+ static getHeaderValues(header) {
404
+ let headerValues = [];
405
+ if (Is.array(header)) {
406
+ headerValues = header;
407
+ }
408
+ else if (Is.stringValue(header)) {
409
+ headerValues = [header];
410
+ }
411
+ return headerValues
412
+ .map(headerValue => headerValue.trim())
413
+ .filter(headerValue => headerValue.length > 0);
414
+ }
33
415
  }
34
416
  //# sourceMappingURL=headerHelper.js.map