@sitecore-cloudsdk/utils 0.1.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/README.md +11 -0
  2. package/dist/cjs/src/index.d.ts +19 -0
  3. package/dist/cjs/src/index.js +30 -0
  4. package/dist/cjs/src/lib/converters/base-64-converter.d.ts +3 -0
  5. package/dist/cjs/src/lib/converters/base-64-converter.js +19 -0
  6. package/dist/cjs/src/lib/converters/flatten-object.d.ts +35 -0
  7. package/dist/cjs/src/lib/converters/flatten-object.js +35 -0
  8. package/dist/cjs/src/lib/cookies/consts.d.ts +1 -0
  9. package/dist/cjs/src/lib/cookies/consts.js +6 -0
  10. package/dist/cjs/src/lib/cookies/cookie-exists-in-server-side.d.ts +7 -0
  11. package/dist/cjs/src/lib/cookies/cookie-exists-in-server-side.js +15 -0
  12. package/dist/cjs/src/lib/cookies/cookie-exists.d.ts +7 -0
  13. package/dist/cjs/src/lib/cookies/cookie-exists.js +14 -0
  14. package/dist/cjs/src/lib/cookies/create-cookie-string.d.ts +9 -0
  15. package/dist/cjs/src/lib/cookies/create-cookie-string.js +21 -0
  16. package/dist/cjs/src/lib/cookies/delete-cookie.d.ts +5 -0
  17. package/dist/cjs/src/lib/cookies/delete-cookie.js +12 -0
  18. package/dist/cjs/src/lib/cookies/get-cookie-server-side.d.ts +10 -0
  19. package/dist/cjs/src/lib/cookies/get-cookie-server-side.js +15 -0
  20. package/dist/cjs/src/lib/cookies/get-cookie-value-client-side.d.ts +6 -0
  21. package/dist/cjs/src/lib/cookies/get-cookie-value-client-side.js +15 -0
  22. package/dist/cjs/src/lib/cookies/get-cookie.d.ts +10 -0
  23. package/dist/cjs/src/lib/cookies/get-cookie.js +19 -0
  24. package/dist/cjs/src/lib/cookies/interfaces.d.ts +19 -0
  25. package/dist/cjs/src/lib/cookies/interfaces.js +3 -0
  26. package/dist/cjs/src/lib/fetch-with-timeout.d.ts +12 -0
  27. package/dist/cjs/src/lib/fetch-with-timeout.js +34 -0
  28. package/dist/cjs/src/lib/interfaces.d.ts +48 -0
  29. package/dist/cjs/src/lib/interfaces.js +3 -0
  30. package/dist/cjs/src/lib/typeguards/is-http-request.d.ts +9 -0
  31. package/dist/cjs/src/lib/typeguards/is-http-request.js +15 -0
  32. package/dist/cjs/src/lib/typeguards/is-http-response.d.ts +9 -0
  33. package/dist/cjs/src/lib/typeguards/is-http-response.js +15 -0
  34. package/dist/cjs/src/lib/typeguards/is-next-js-middleware-request.d.ts +9 -0
  35. package/dist/cjs/src/lib/typeguards/is-next-js-middleware-request.js +15 -0
  36. package/dist/cjs/src/lib/typeguards/is-next-js-middleware-response.d.ts +9 -0
  37. package/dist/cjs/src/lib/typeguards/is-next-js-middleware-response.js +15 -0
  38. package/dist/cjs/src/lib/validators/is-iso-date-string.d.ts +1 -0
  39. package/dist/cjs/src/lib/validators/is-iso-date-string.js +14 -0
  40. package/dist/cjs/src/lib/validators/is-short-iso-date-string.d.ts +6 -0
  41. package/dist/cjs/src/lib/validators/is-short-iso-date-string.js +20 -0
  42. package/dist/cjs/src/lib/validators/is-valid-email.d.ts +6 -0
  43. package/dist/cjs/src/lib/validators/is-valid-email.js +14 -0
  44. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -0
  45. package/dist/esm/src/index.d.ts +19 -0
  46. package/dist/esm/src/index.js +16 -0
  47. package/dist/esm/src/lib/converters/base-64-converter.d.ts +3 -0
  48. package/dist/esm/src/lib/converters/base-64-converter.js +15 -0
  49. package/dist/esm/src/lib/converters/flatten-object.d.ts +35 -0
  50. package/dist/esm/src/lib/converters/flatten-object.js +31 -0
  51. package/dist/esm/src/lib/cookies/consts.d.ts +1 -0
  52. package/dist/esm/src/lib/cookies/consts.js +3 -0
  53. package/dist/esm/src/lib/cookies/cookie-exists-in-server-side.d.ts +7 -0
  54. package/dist/esm/src/lib/cookies/cookie-exists-in-server-side.js +11 -0
  55. package/dist/esm/src/lib/cookies/cookie-exists.d.ts +7 -0
  56. package/dist/esm/src/lib/cookies/cookie-exists.js +10 -0
  57. package/dist/esm/src/lib/cookies/create-cookie-string.d.ts +9 -0
  58. package/dist/esm/src/lib/cookies/create-cookie-string.js +17 -0
  59. package/dist/esm/src/lib/cookies/delete-cookie.d.ts +5 -0
  60. package/dist/esm/src/lib/cookies/delete-cookie.js +8 -0
  61. package/dist/esm/src/lib/cookies/get-cookie-server-side.d.ts +10 -0
  62. package/dist/esm/src/lib/cookies/get-cookie-server-side.js +11 -0
  63. package/dist/esm/src/lib/cookies/get-cookie-value-client-side.d.ts +6 -0
  64. package/dist/esm/src/lib/cookies/get-cookie-value-client-side.js +11 -0
  65. package/dist/esm/src/lib/cookies/get-cookie.d.ts +10 -0
  66. package/dist/esm/src/lib/cookies/get-cookie.js +15 -0
  67. package/dist/esm/src/lib/cookies/interfaces.d.ts +19 -0
  68. package/dist/esm/src/lib/cookies/interfaces.js +2 -0
  69. package/dist/esm/src/lib/fetch-with-timeout.d.ts +12 -0
  70. package/dist/esm/src/lib/fetch-with-timeout.js +30 -0
  71. package/dist/esm/src/lib/interfaces.d.ts +48 -0
  72. package/dist/esm/src/lib/interfaces.js +2 -0
  73. package/dist/esm/src/lib/typeguards/is-http-request.d.ts +9 -0
  74. package/dist/esm/src/lib/typeguards/is-http-request.js +11 -0
  75. package/dist/esm/src/lib/typeguards/is-http-response.d.ts +9 -0
  76. package/dist/esm/src/lib/typeguards/is-http-response.js +11 -0
  77. package/dist/esm/src/lib/typeguards/is-next-js-middleware-request.d.ts +9 -0
  78. package/dist/esm/src/lib/typeguards/is-next-js-middleware-request.js +11 -0
  79. package/dist/esm/src/lib/typeguards/is-next-js-middleware-response.d.ts +9 -0
  80. package/dist/esm/src/lib/typeguards/is-next-js-middleware-response.js +11 -0
  81. package/dist/esm/src/lib/validators/is-iso-date-string.d.ts +1 -0
  82. package/dist/esm/src/lib/validators/is-iso-date-string.js +10 -0
  83. package/dist/esm/src/lib/validators/is-short-iso-date-string.d.ts +6 -0
  84. package/dist/esm/src/lib/validators/is-short-iso-date-string.js +16 -0
  85. package/dist/esm/src/lib/validators/is-valid-email.d.ts +6 -0
  86. package/dist/esm/src/lib/validators/is-valid-email.js +10 -0
  87. package/dist/esm/tsconfig.tsbuildinfo +1 -0
  88. package/package.json +27 -0
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # utils
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build utils` to build the library.
8
+
9
+ ## Running unit tests
10
+
11
+ Run `nx test utils` to execute the unit tests via [Jest](https://jestjs.io).
@@ -0,0 +1,19 @@
1
+ import { createCookieString } from './lib/cookies/create-cookie-string';
2
+ import { ICookieProperties } from './lib/cookies/interfaces';
3
+ import { fetchWithTimeout } from './lib/fetch-with-timeout';
4
+ import { getCookieValueClientSide } from './lib/cookies/get-cookie-value-client-side';
5
+ import { isNextJsMiddlewareRequest } from './lib/typeguards/is-next-js-middleware-request';
6
+ import { isNextJsMiddlewareResponse } from './lib/typeguards/is-next-js-middleware-response';
7
+ import { isHttpRequest } from './lib/typeguards/is-http-request';
8
+ import { isHttpResponse } from './lib/typeguards/is-http-response';
9
+ import { getCookie } from './lib/cookies/get-cookie';
10
+ import { getCookieServerSide } from './lib/cookies/get-cookie-server-side';
11
+ import { cookieExists } from './lib/cookies/cookie-exists';
12
+ import { flattenObject, INestedObject, IFlattenedObject } from './lib/converters/flatten-object';
13
+ import { isShortISODateString } from './lib/validators/is-short-iso-date-string';
14
+ import { isValidEmail } from './lib/validators/is-valid-email';
15
+ import type { TRequest, IMiddlewareNextResponse, IHttpResponse, IMiddlewareRequest, IHttpRequest, BasicTypes } from './lib/interfaces';
16
+ import type { ICookie } from './lib/cookies/interfaces';
17
+ export { createCookieString, fetchWithTimeout, getCookieValueClientSide, isNextJsMiddlewareRequest, isNextJsMiddlewareResponse, isHttpRequest, isHttpResponse, getCookie, getCookieServerSide, cookieExists, flattenObject, isShortISODateString, isValidEmail, };
18
+ export type { INestedObject, TRequest, IMiddlewareNextResponse, BasicTypes, IHttpResponse, IHttpRequest, IMiddlewareRequest, IFlattenedObject, };
19
+ export type { ICookie, ICookieProperties };
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isValidEmail = exports.isShortISODateString = exports.flattenObject = exports.cookieExists = exports.getCookieServerSide = exports.getCookie = exports.isHttpResponse = exports.isHttpRequest = exports.isNextJsMiddlewareResponse = exports.isNextJsMiddlewareRequest = exports.getCookieValueClientSide = exports.fetchWithTimeout = exports.createCookieString = void 0;
4
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
5
+ const create_cookie_string_1 = require("./lib/cookies/create-cookie-string");
6
+ Object.defineProperty(exports, "createCookieString", { enumerable: true, get: function () { return create_cookie_string_1.createCookieString; } });
7
+ const fetch_with_timeout_1 = require("./lib/fetch-with-timeout");
8
+ Object.defineProperty(exports, "fetchWithTimeout", { enumerable: true, get: function () { return fetch_with_timeout_1.fetchWithTimeout; } });
9
+ const get_cookie_value_client_side_1 = require("./lib/cookies/get-cookie-value-client-side");
10
+ Object.defineProperty(exports, "getCookieValueClientSide", { enumerable: true, get: function () { return get_cookie_value_client_side_1.getCookieValueClientSide; } });
11
+ const is_next_js_middleware_request_1 = require("./lib/typeguards/is-next-js-middleware-request");
12
+ Object.defineProperty(exports, "isNextJsMiddlewareRequest", { enumerable: true, get: function () { return is_next_js_middleware_request_1.isNextJsMiddlewareRequest; } });
13
+ const is_next_js_middleware_response_1 = require("./lib/typeguards/is-next-js-middleware-response");
14
+ Object.defineProperty(exports, "isNextJsMiddlewareResponse", { enumerable: true, get: function () { return is_next_js_middleware_response_1.isNextJsMiddlewareResponse; } });
15
+ const is_http_request_1 = require("./lib/typeguards/is-http-request");
16
+ Object.defineProperty(exports, "isHttpRequest", { enumerable: true, get: function () { return is_http_request_1.isHttpRequest; } });
17
+ const is_http_response_1 = require("./lib/typeguards/is-http-response");
18
+ Object.defineProperty(exports, "isHttpResponse", { enumerable: true, get: function () { return is_http_response_1.isHttpResponse; } });
19
+ const get_cookie_1 = require("./lib/cookies/get-cookie");
20
+ Object.defineProperty(exports, "getCookie", { enumerable: true, get: function () { return get_cookie_1.getCookie; } });
21
+ const get_cookie_server_side_1 = require("./lib/cookies/get-cookie-server-side");
22
+ Object.defineProperty(exports, "getCookieServerSide", { enumerable: true, get: function () { return get_cookie_server_side_1.getCookieServerSide; } });
23
+ const cookie_exists_1 = require("./lib/cookies/cookie-exists");
24
+ Object.defineProperty(exports, "cookieExists", { enumerable: true, get: function () { return cookie_exists_1.cookieExists; } });
25
+ const flatten_object_1 = require("./lib/converters/flatten-object");
26
+ Object.defineProperty(exports, "flattenObject", { enumerable: true, get: function () { return flatten_object_1.flattenObject; } });
27
+ const is_short_iso_date_string_1 = require("./lib/validators/is-short-iso-date-string");
28
+ Object.defineProperty(exports, "isShortISODateString", { enumerable: true, get: function () { return is_short_iso_date_string_1.isShortISODateString; } });
29
+ const is_valid_email_1 = require("./lib/validators/is-valid-email");
30
+ Object.defineProperty(exports, "isValidEmail", { enumerable: true, get: function () { return is_valid_email_1.isValidEmail; } });
@@ -0,0 +1,3 @@
1
+ export declare function convertToBase64(input: string | {
2
+ [key: string]: unknown;
3
+ }): string;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.convertToBase64 = void 0;
5
+ /*
6
+ * A function that converts a string or an object to a base64 string
7
+ * @param input - The string or object to be converted
8
+ * @returns A base64 string
9
+ */
10
+ function convertToBase64(input) {
11
+ const data = typeof input === 'string' ? input : JSON.stringify(input);
12
+ const stringFormat = 'base64';
13
+ if (typeof Buffer === 'function')
14
+ return Buffer.from(data).toString(stringFormat);
15
+ if (typeof window?.btoa === 'function')
16
+ return window.btoa(data);
17
+ return data;
18
+ }
19
+ exports.convertToBase64 = convertToBase64;
@@ -0,0 +1,35 @@
1
+ import { BasicTypes } from '../interfaces';
2
+ /**
3
+ * A function that flattens an object, by combining the keys with an "_".
4
+ * @param data - An object that has the required data to perform the flattening
5
+ * @returns - A new flattened object
6
+ * @example
7
+ *
8
+ * ```ts
9
+ * const object = {order:{amount: 1, delivered: false}}
10
+ * const flattenedObject = flattenObject(object)
11
+ * // flattenedObject will be {order_amount: 1, order_delivered: false}
12
+ * ```
13
+ */
14
+ export declare function flattenObject(data: IFlattenObjectDataParameters): IFlattenedObject;
15
+ /**
16
+ * Interface for the data object parameter of the flattenObject function
17
+ */
18
+ interface IFlattenObjectDataParameters {
19
+ object: INestedObject;
20
+ currentKey?: string;
21
+ newObject?: IFlattenedObject;
22
+ }
23
+ /**
24
+ * Interface for the return object of the flattenObject function
25
+ */
26
+ export interface IFlattenedObject {
27
+ [key: string]: BasicTypes;
28
+ }
29
+ /**
30
+ * Interface of the object to flatten
31
+ */
32
+ export interface INestedObject {
33
+ [key: string]: BasicTypes | INestedObject;
34
+ }
35
+ export {};
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.flattenObject = void 0;
5
+ /**
6
+ * A function that flattens an object, by combining the keys with an "_".
7
+ * @param data - An object that has the required data to perform the flattening
8
+ * @returns - A new flattened object
9
+ * @example
10
+ *
11
+ * ```ts
12
+ * const object = {order:{amount: 1, delivered: false}}
13
+ * const flattenedObject = flattenObject(object)
14
+ * // flattenedObject will be {order_amount: 1, order_delivered: false}
15
+ * ```
16
+ */
17
+ function flattenObject(data) {
18
+ const { currentKey, object } = data;
19
+ const newObject = data.newObject ?? {};
20
+ for (const key in object) {
21
+ const value = object[key];
22
+ if (value === undefined)
23
+ continue;
24
+ if (typeof value === 'object' && !Array.isArray(value))
25
+ flattenObject({
26
+ currentKey: `${currentKey ? `${currentKey}_${key}` : key}`,
27
+ newObject,
28
+ object: value,
29
+ });
30
+ else
31
+ newObject[currentKey ? `${currentKey}_${key}` : key] = value;
32
+ }
33
+ return newObject;
34
+ }
35
+ exports.flattenObject = flattenObject;
@@ -0,0 +1 @@
1
+ export declare const DAILY_SECONDS = 86400;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ /* eslint-disable @typescript-eslint/naming-convention */
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.DAILY_SECONDS = void 0;
6
+ exports.DAILY_SECONDS = 86400;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Evaluates whether a specified cookie name exists in the request header
3
+ * @param cookiesHeader - The cookie string of the request header
4
+ * @param cookieName - The cookie name to be found
5
+ * @returns - A boolean value if cookie exists
6
+ */
7
+ export declare function cookieExistsInServerSide(cookiesHeader: string, cookieName: string): boolean;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.cookieExistsInServerSide = void 0;
5
+ const get_cookie_server_side_1 = require("./get-cookie-server-side");
6
+ /**
7
+ * Evaluates whether a specified cookie name exists in the request header
8
+ * @param cookiesHeader - The cookie string of the request header
9
+ * @param cookieName - The cookie name to be found
10
+ * @returns - A boolean value if cookie exists
11
+ */
12
+ function cookieExistsInServerSide(cookiesHeader, cookieName) {
13
+ return (0, get_cookie_server_side_1.getCookieServerSide)(cookiesHeader, cookieName) !== undefined;
14
+ }
15
+ exports.cookieExistsInServerSide = cookieExistsInServerSide;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Checks if the cookie exists in the cookie string
3
+ * @param cookieStr - The cookie string containing every cookie
4
+ * @param cookieName - The cookie name to be found
5
+ * @returns - boolean value, if the cookie is found in the cookie string
6
+ */
7
+ export declare function cookieExists(cookieStr: string, cookieName: string): boolean;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.cookieExists = void 0;
5
+ /**
6
+ * Checks if the cookie exists in the cookie string
7
+ * @param cookieStr - The cookie string containing every cookie
8
+ * @param cookieName - The cookie name to be found
9
+ * @returns - boolean value, if the cookie is found in the cookie string
10
+ */
11
+ function cookieExists(cookieStr, cookieName) {
12
+ return cookieStr.split('; ').some((cookie) => cookie.split('=')[0] === cookieName);
13
+ }
14
+ exports.cookieExists = cookieExists;
@@ -0,0 +1,9 @@
1
+ import { ICookieProperties } from './interfaces';
2
+ /**
3
+ * Creates the cookie string with the respectively cookie attributes
4
+ * @param name - name of the cookie
5
+ * @param value - value of the cookie
6
+ * @param attributes - an object of supported cookie attributes
7
+ * @returns - a string that will be passed to document.cookie
8
+ */
9
+ export declare function createCookieString(name: string, value: string, attributes: ICookieProperties): string;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.createCookieString = void 0;
5
+ /**
6
+ * Creates the cookie string with the respectively cookie attributes
7
+ * @param name - name of the cookie
8
+ * @param value - value of the cookie
9
+ * @param attributes - an object of supported cookie attributes
10
+ * @returns - a string that will be passed to document.cookie
11
+ */
12
+ function createCookieString(name, value, attributes) {
13
+ let cookieString = `${name}=${value};`;
14
+ cookieString += ` Max-Age=${attributes.maxAge}; SameSite=${attributes.sameSite};`;
15
+ cookieString += attributes.secure ? ' Secure;' : '';
16
+ cookieString += attributes.path ? ` Path=${attributes.path};` : '';
17
+ cookieString += attributes.domain ? ` Domain=${attributes.domain};` : '';
18
+ cookieString = cookieString.substring(0, cookieString.length - 1);
19
+ return cookieString;
20
+ }
21
+ exports.createCookieString = createCookieString;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Deletes a cookie from the document (client-side only)
3
+ * @param cookieName - The cookie to be deleted
4
+ */
5
+ export declare function deleteCookie(cookieName: string): void;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.deleteCookie = void 0;
5
+ /**
6
+ * Deletes a cookie from the document (client-side only)
7
+ * @param cookieName - The cookie to be deleted
8
+ */
9
+ function deleteCookie(cookieName) {
10
+ document.cookie = cookieName + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
11
+ }
12
+ exports.deleteCookie = deleteCookie;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Retrieves the cookie name and value from the request header
3
+ * @param cookiesHeader - The cookie string of the request header
4
+ * @param cookieName - The cookie name to be found
5
+ * @returns - The name and value of the cookie, or undefined
6
+ */
7
+ export declare function getCookieServerSide(cookiesHeader: string | undefined, cookieName: string): {
8
+ name: string;
9
+ value: string;
10
+ } | undefined;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.getCookieServerSide = void 0;
5
+ const get_cookie_1 = require("./get-cookie");
6
+ /**
7
+ * Retrieves the cookie name and value from the request header
8
+ * @param cookiesHeader - The cookie string of the request header
9
+ * @param cookieName - The cookie name to be found
10
+ * @returns - The name and value of the cookie, or undefined
11
+ */
12
+ function getCookieServerSide(cookiesHeader, cookieName) {
13
+ return (0, get_cookie_1.getCookie)(cookiesHeader, cookieName);
14
+ }
15
+ exports.getCookieServerSide = getCookieServerSide;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Gets the value for a given cookie name
3
+ * @param cookieName - The cookie name to be found
4
+ * @returns - The value of the cookie if it exists or empty string
5
+ */
6
+ export declare function getCookieValueClientSide(cookieName: string): string;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCookieValueClientSide = void 0;
4
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
5
+ const get_cookie_1 = require("./get-cookie");
6
+ /**
7
+ * Gets the value for a given cookie name
8
+ * @param cookieName - The cookie name to be found
9
+ * @returns - The value of the cookie if it exists or empty string
10
+ */
11
+ function getCookieValueClientSide(cookieName) {
12
+ const cookie = (0, get_cookie_1.getCookie)(document.cookie, cookieName);
13
+ return cookie?.value ?? '';
14
+ }
15
+ exports.getCookieValueClientSide = getCookieValueClientSide;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Retrieves the cookie, if it exists in the cookie string
3
+ * @param cookieStr - The cookie string containing every cookie
4
+ * @param cookieName - The cookie name to be found
5
+ * @returns - an object that contains the cookie name and value or undefined, if not found
6
+ */
7
+ export declare function getCookie(cookieStr: string | undefined, cookieName: string): {
8
+ name: string;
9
+ value: string;
10
+ } | undefined;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.getCookie = void 0;
5
+ /**
6
+ * Retrieves the cookie, if it exists in the cookie string
7
+ * @param cookieStr - The cookie string containing every cookie
8
+ * @param cookieName - The cookie name to be found
9
+ * @returns - an object that contains the cookie name and value or undefined, if not found
10
+ */
11
+ function getCookie(cookieStr, cookieName) {
12
+ if (!cookieStr)
13
+ return undefined;
14
+ const found = cookieStr.split('; ').find((cookie) => {
15
+ return cookie.indexOf('=') > 0 && cookie.split('=')[0] === cookieName;
16
+ });
17
+ return found !== undefined ? { name: found.split('=')[0], value: found.split('=')[1] } : undefined;
18
+ }
19
+ exports.getCookie = getCookie;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Cookie properties
3
+ */
4
+ export interface ICookieProperties {
5
+ maxAge: number;
6
+ sameSite: string;
7
+ secure: boolean;
8
+ path?: string;
9
+ httpOnly?: boolean;
10
+ expires?: Date;
11
+ domain?: string;
12
+ }
13
+ /**
14
+ * Interface that represents a cookie (name, value)
15
+ */
16
+ export interface ICookie {
17
+ name: string;
18
+ value: string;
19
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Fetches data from the specified URL within the given timeout period.
3
+ *
4
+ * @param url - The URL to fetch data from.
5
+ * @param timeout - The time in milliseconds to wait before timing out the request.
6
+ * @param fetchOptions - The options to pass to the fetch API.
7
+ * @returns - A Promise that resolves to the fetched data, or null if the request was aborted or timed out.
8
+ * @throws - If the timeout value is invalid.
9
+ */
10
+ export declare function fetchWithTimeout(url: string, timeout: number, fetchOptions: {
11
+ [key: string]: unknown;
12
+ }): Promise<null | unknown>;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.fetchWithTimeout = void 0;
5
+ /**
6
+ * Fetches data from the specified URL within the given timeout period.
7
+ *
8
+ * @param url - The URL to fetch data from.
9
+ * @param timeout - The time in milliseconds to wait before timing out the request.
10
+ * @param fetchOptions - The options to pass to the fetch API.
11
+ * @returns - A Promise that resolves to the fetched data, or null if the request was aborted or timed out.
12
+ * @throws - If the timeout value is invalid.
13
+ */
14
+ async function fetchWithTimeout(url, timeout, fetchOptions) {
15
+ if (!Number.isInteger(timeout) || timeout < 0)
16
+ throw new Error('[IV-0006] Incorrect value for the timeout parameter. Set the value to an integer greater than or equal to 0.');
17
+ const abortController = new AbortController();
18
+ const signal = abortController.signal;
19
+ const timeoutHandler = setTimeout(() => {
20
+ abortController.abort();
21
+ }, timeout);
22
+ return fetch(url, { ...fetchOptions, signal })
23
+ .then((response) => {
24
+ clearTimeout(timeoutHandler);
25
+ return response.json();
26
+ })
27
+ .then((data) => data)
28
+ .catch((error) => {
29
+ if (error.name === 'AbortError')
30
+ throw new Error('[IE-0002] Timeout exceeded. The server did not respond within the allotted time.');
31
+ return null;
32
+ });
33
+ }
34
+ exports.fetchWithTimeout = fetchWithTimeout;
@@ -0,0 +1,48 @@
1
+ import { ICookie } from './cookies/interfaces';
2
+ /**
3
+ * A reusable type that accepts only basic types and arrays of those
4
+ */
5
+ export type BasicTypes = string | boolean | number | undefined | Array<string | boolean | number | {
6
+ [key: string]: BasicTypes;
7
+ } | Array<BasicTypes>>;
8
+ /**
9
+ * Interface for supporting request IncomingMessage http node type
10
+ */
11
+ export interface IHttpRequest {
12
+ headers: {
13
+ 'cookie'?: string;
14
+ 'content-language'?: string;
15
+ 'referer'?: string;
16
+ };
17
+ url?: string;
18
+ }
19
+ /**
20
+ * Interface for supporting response OutgoingMessage http node type
21
+ */
22
+ export interface IHttpResponse {
23
+ setHeader(name: string, value: number | string | ReadonlyArray<string>): void;
24
+ }
25
+ export type TRequest = IHttpRequest | IMiddlewareRequest;
26
+ /**
27
+ * Interface for supporting request from Edge Next.js
28
+ * includes types compatible with both NextJS versions 12 & 13
29
+ */
30
+ export interface IMiddlewareRequest {
31
+ cookies: {
32
+ set: ((key: string, value: string, options: any) => any) | ((...args: [key: string, value: string] | [options: ICookie]) => any);
33
+ get: (key: string) => ICookie | string | undefined;
34
+ };
35
+ headers: {
36
+ get: (name: string) => string | null;
37
+ };
38
+ url?: string;
39
+ }
40
+ /**
41
+ * Interface for Edge Next.js Response
42
+ * includes types compatible with both NextJS versions 12 & 13
43
+ */
44
+ export interface IMiddlewareNextResponse {
45
+ cookies: {
46
+ set: ((key: string, value: unknown, options?: any) => any) | ((...args: [key: string, value: string] | [options: ICookie]) => any);
47
+ };
48
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,9 @@
1
+ import { IHttpRequest, TRequest } from '../interfaces';
2
+ /**
3
+ * Checks if the given 'request' object is a valid HTTP Request
4
+ * by verifying the presence of necessary properties.
5
+ *
6
+ * @param request - The request object to be validated.
7
+ * @returns Returns true if 'request' is a valid HTTP Request, otherwise false.
8
+ */
9
+ export declare function isHttpRequest(request: TRequest): request is IHttpRequest;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.isHttpRequest = void 0;
5
+ /**
6
+ * Checks if the given 'request' object is a valid HTTP Request
7
+ * by verifying the presence of necessary properties.
8
+ *
9
+ * @param request - The request object to be validated.
10
+ * @returns Returns true if 'request' is a valid HTTP Request, otherwise false.
11
+ */
12
+ function isHttpRequest(request) {
13
+ return 'headers' in request;
14
+ }
15
+ exports.isHttpRequest = isHttpRequest;
@@ -0,0 +1,9 @@
1
+ import { IHttpResponse, IMiddlewareNextResponse } from '../interfaces';
2
+ /**
3
+ * Checks if the given 'response' object is a valid HTTP Response
4
+ * by verifying the presence of necessary properties.
5
+ *
6
+ * @param response - IMiddlewareNextResponse | IHttpResponse - The response object to be validated.
7
+ * @returns Returns true if 'response' is a valid HTTP Response, otherwise false.
8
+ */
9
+ export declare function isHttpResponse(response: IMiddlewareNextResponse | IHttpResponse): response is IHttpResponse;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.isHttpResponse = void 0;
5
+ /**
6
+ * Checks if the given 'response' object is a valid HTTP Response
7
+ * by verifying the presence of necessary properties.
8
+ *
9
+ * @param response - IMiddlewareNextResponse | IHttpResponse - The response object to be validated.
10
+ * @returns Returns true if 'response' is a valid HTTP Response, otherwise false.
11
+ */
12
+ function isHttpResponse(response) {
13
+ return 'setHeader' in response;
14
+ }
15
+ exports.isHttpResponse = isHttpResponse;
@@ -0,0 +1,9 @@
1
+ import { IMiddlewareRequest, TRequest } from '../interfaces';
2
+ /**
3
+ * Checks if the given 'request' object is a valid Middleware Request
4
+ * by verifying the presence of necessary properties.
5
+ *
6
+ * @param request - The request object to be validated.
7
+ * @returns Returns true if 'request' is a valid Middleware Request, otherwise false.
8
+ */
9
+ export declare function isNextJsMiddlewareRequest(request: TRequest): request is IMiddlewareRequest;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.isNextJsMiddlewareRequest = void 0;
5
+ /**
6
+ * Checks if the given 'request' object is a valid Middleware Request
7
+ * by verifying the presence of necessary properties.
8
+ *
9
+ * @param request - The request object to be validated.
10
+ * @returns Returns true if 'request' is a valid Middleware Request, otherwise false.
11
+ */
12
+ function isNextJsMiddlewareRequest(request) {
13
+ return 'cookies' in request && 'get' in request.cookies && 'set' in request.cookies;
14
+ }
15
+ exports.isNextJsMiddlewareRequest = isNextJsMiddlewareRequest;
@@ -0,0 +1,9 @@
1
+ import { IHttpResponse, IMiddlewareNextResponse } from '../interfaces';
2
+ /**
3
+ * Checks if the given 'response' object is a valid IMiddlewareNextResponse Response
4
+ * by verifying the presence of necessary properties.
5
+ *
6
+ * @param response - IMiddlewareNextResponse | IHttpResponse - The response object to be validated.
7
+ * @returns Returns true if 'response' is a valid IMiddlewareNextResponse Response, otherwise false.
8
+ */
9
+ export declare function isNextJsMiddlewareResponse(response: IMiddlewareNextResponse | IHttpResponse): response is IMiddlewareNextResponse;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.isNextJsMiddlewareResponse = void 0;
5
+ /**
6
+ * Checks if the given 'response' object is a valid IMiddlewareNextResponse Response
7
+ * by verifying the presence of necessary properties.
8
+ *
9
+ * @param response - IMiddlewareNextResponse | IHttpResponse - The response object to be validated.
10
+ * @returns Returns true if 'response' is a valid IMiddlewareNextResponse Response, otherwise false.
11
+ */
12
+ function isNextJsMiddlewareResponse(response) {
13
+ return 'cookies' in response && 'set' in response.cookies;
14
+ }
15
+ exports.isNextJsMiddlewareResponse = isNextJsMiddlewareResponse;
@@ -0,0 +1 @@
1
+ export declare function isISODateString(date: string): boolean;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isISODateString = void 0;
4
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
5
+ function isISODateString(date) {
6
+ try {
7
+ const convertedDate = new Date(date).toISOString();
8
+ return convertedDate === date;
9
+ }
10
+ catch (_) {
11
+ return false;
12
+ }
13
+ }
14
+ exports.isISODateString = isISODateString;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Checks if the provided string is a shortened version of ISO 8601 date format ‘YYYY-MM-DD’T’hh:mm’
3
+ * @param date - The date string provided by the developer
4
+ * @returns - A boolean if the string is valid otherwise false
5
+ */
6
+ export declare function isShortISODateString(date: string): boolean;