@configura/web-api 2.0.0-alpha.10 → 2.0.0-alpha.11

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.
@@ -1,8 +1,8 @@
1
1
  import { DtoProductConfiguration } from "../CatalogueAPI.js";
2
2
  import { CfgHistoryManager, CfgHistoryManagerSendData } from "./CfgHistoryManager.js";
3
3
  import { CfgIOProdConfConnector, CfgProdConfMessage } from "./CfgIOProdConfConnector.js";
4
- export declare function dtoConfToString(conf: DtoProductConfiguration): string;
5
- export declare function stringToDtoConf(conf: string): DtoProductConfiguration;
4
+ export declare const dtoConfToCompactString: (conf: DtoProductConfiguration) => string;
5
+ export declare const compactStringToDtoConf: (versionAndConf: string) => DtoProductConfiguration;
6
6
  /**
7
7
  * Instantiating this will make the browser history (and URL) update with the product configuration.
8
8
  */
@@ -1,30 +1,75 @@
1
1
  import { CfgHistoryManager } from "./CfgHistoryManager.js";
2
2
  import { CfgIOProdConfConnector, CfgProdConfMessageVersions, STAGE_PROD_CONF_MESSAGE_KEY, } from "./CfgIOProdConfConnector.js";
3
+ const versionedRegex = /^v(\d+)(.+)$/;
3
4
  const jsonKeyRegex = /"([^"]+)":/g;
4
- const keyMap = [
5
- ["configuration", "cn"],
6
- ["additionalProducts", "as"],
7
- ["prodParams", "pp"],
8
- ["refKey", "rk"],
9
- ["selected", "se"],
10
- ["features", "fs"],
11
- ["code", "cd"],
12
- ["options", "os"],
13
- ["numericValue", "nu"],
14
- ["groupCode", "gc"],
15
- ["unit", "un"],
16
- ];
17
- // todo: Could be worth making faster
18
- const toCompact = (key) => { var _a, _b; return (_b = (_a = keyMap.find((i) => i[0] === key)) === null || _a === void 0 ? void 0 : _a[1]) !== null && _b !== void 0 ? _b : key; };
19
- const toExpanded = (key) => { var _a, _b; return (_b = (_a = keyMap.find((i) => i[1] === key)) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : key; };
20
- export function dtoConfToString(conf) {
21
- const stringified = JSON.stringify(conf, undefined, "");
22
- return stringified.replace(jsonKeyRegex, (_, key) => `"${toCompact(key)}":`);
23
- }
24
- export function stringToDtoConf(conf) {
25
- const expandedKeys = conf.replace(jsonKeyRegex, (_, key) => `"${toExpanded(key)}":`);
26
- return JSON.parse(expandedKeys);
5
+ const swapIntoUrlAdaptedRegex = /[{}"]/g;
6
+ const swapFromUrlAdaptedRegex = /[-~_]/g;
7
+ const shortToLong = new Map();
8
+ const longToShort = new Map();
9
+ // The replacement scheme here assumes this will only be used for keys
10
+ // in DtoProductConfiguration and the knowledge that it contains no one
11
+ // one char keys.
12
+ for (const [long, short] of [
13
+ ["configuration", "c"],
14
+ ["additionalProducts", "a"],
15
+ ["prodParams", "p"],
16
+ ["refKey", "r"],
17
+ ["selected", "s"],
18
+ ["features", "f"],
19
+ ["code", "d"],
20
+ ["options", "o"],
21
+ ["numericValue", "n"],
22
+ ["groupCode", "g"],
23
+ ["unit", "u"],
24
+ ]) {
25
+ shortToLong.set(short, long);
26
+ longToShort.set(long, short);
27
27
  }
28
+ /**
29
+ * As certain chars are abundant in JSON but less abundant in the actual data
30
+ * we swap these so that frequent characters do not need to be URL-encoded.
31
+ */
32
+ const jsonStringSwapCharsForUrl = (data) => data.replace(swapIntoUrlAdaptedRegex, (char) => {
33
+ switch (char) {
34
+ case "{":
35
+ return "~";
36
+ case "}":
37
+ return "-";
38
+ case '"':
39
+ return "_";
40
+ default:
41
+ throw new Error(`Unexpected char "${char}" in swap for URL`);
42
+ }
43
+ });
44
+ const jsonStringSwapCharsFromUrl = (data) => data.replace(swapFromUrlAdaptedRegex, (char) => {
45
+ switch (char) {
46
+ case "~":
47
+ return "{";
48
+ case "-":
49
+ return "}";
50
+ case "_":
51
+ return '"';
52
+ default:
53
+ throw new Error(`Unexpected char "${char}" in swap from URL`);
54
+ }
55
+ });
56
+ const compactDtoConfJsonKeys = (data) => data.replace(jsonKeyRegex, (_, key) => { var _a; return `"${(_a = longToShort.get(key)) !== null && _a !== void 0 ? _a : key}":`; });
57
+ const expandDtoConfJsonKeys = (data) => data.replace(jsonKeyRegex, (_, key) => { var _a; return `"${(_a = shortToLong.get(key)) !== null && _a !== void 0 ? _a : key}":`; });
58
+ export const dtoConfToCompactString = (conf) => "v1" + jsonStringSwapCharsForUrl(compactDtoConfJsonKeys(JSON.stringify(conf, undefined, "")));
59
+ export const compactStringToDtoConf = (versionAndConf) => {
60
+ const match = versionedRegex.exec(versionAndConf);
61
+ if (match === null) {
62
+ throw new Error("Could not match version string");
63
+ }
64
+ const [, version, conf] = match;
65
+ if (version !== "1") {
66
+ throw new Error("Unknown packed URL version");
67
+ }
68
+ if (conf === "") {
69
+ throw new Error("No conf found");
70
+ }
71
+ return JSON.parse(expandDtoConfJsonKeys(jsonStringSwapCharsFromUrl(conf)));
72
+ };
28
73
  /**
29
74
  * Instantiating this will make the browser history (and URL) update with the product configuration.
30
75
  */
@@ -44,12 +89,12 @@ export class CfgHistoryToProdConfConnector extends CfgIOProdConfConnector {
44
89
  if (s === undefined) {
45
90
  return undefined;
46
91
  }
47
- return stringToDtoConf(s);
92
+ return compactStringToDtoConf(s);
48
93
  }
49
94
  makeSendData(conf, initial) {
50
95
  return {
51
96
  message: CfgIOProdConfConnector.makeMessage(conf, initial, CfgProdConfMessageVersions.V2dot0),
52
- qsKeyValues: new Map([[STAGE_PROD_CONF_MESSAGE_KEY, dtoConfToString(conf)]]),
97
+ qsKeyValues: new Map([[STAGE_PROD_CONF_MESSAGE_KEY, dtoConfToCompactString(conf)]]),
53
98
  useHistoryPush: this._useHistoryPush,
54
99
  };
55
100
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@configura/web-api",
3
- "version": "2.0.0-alpha.10",
3
+ "version": "2.0.0-alpha.11",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -23,7 +23,7 @@
23
23
  "access": "public"
24
24
  },
25
25
  "dependencies": {
26
- "@configura/web-utilities": "2.0.0-alpha.10"
26
+ "@configura/web-utilities": "2.0.0-alpha.11"
27
27
  },
28
- "gitHead": "77f9cbe7521be3fd58a139bdc93167a349ef384f"
28
+ "gitHead": "a57e59802cbbcb61f2a49b64dcefeb7123679478"
29
29
  }