@adobe/helix-shared-config 8.2.0 → 8.3.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [@adobe/helix-shared-config-v8.3.0](https://github.com/adobe/helix-shared/compare/@adobe/helix-shared-config-v8.2.0...@adobe/helix-shared-config-v8.3.0) (2022-08-12)
2
+
3
+
4
+ ### Features
5
+
6
+ * add modifiers config ([0016e95](https://github.com/adobe/helix-shared/commit/0016e9519293c66953baa39d238147b2adf09ad5))
7
+
1
8
  # [@adobe/helix-shared-config-v8.2.0](https://github.com/adobe/helix-shared/compare/@adobe/helix-shared-config-v8.1.0...@adobe/helix-shared-config-v8.2.0) (2022-06-18)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-shared-config",
3
- "version": "8.2.0",
3
+ "version": "8.3.0",
4
4
  "description": "Shared modules of the Helix Project - config",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
@@ -37,7 +37,7 @@
37
37
  "cookie": "0.5.0",
38
38
  "fs-extra": "10.1.0",
39
39
  "ignore": "5.2.0",
40
- "lru-cache": "7.10.1",
40
+ "lru-cache": "7.10.2",
41
41
  "object-hash": "3.0.0",
42
42
  "uri-js": "4.4.1",
43
43
  "yaml": "2.1.1"
@@ -48,6 +48,6 @@
48
48
  "@pollyjs/adapter-node-http": "6.0.5",
49
49
  "@pollyjs/core": "6.0.5",
50
50
  "@pollyjs/persister-fs": "6.0.5",
51
- "nock": "13.2.6"
51
+ "nock": "13.2.9"
52
52
  }
53
53
  }
@@ -0,0 +1,123 @@
1
+ /*
2
+ * Copyright 2022 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ /**
14
+ * Structure that groups name value pairs by url, used as modifier sheet, for example
15
+ * in metadata.json or headers.json
16
+ *
17
+ * @example
18
+ *
19
+ * | url | key | value | Title | Description |
20
+ * |-------|-----|-------|---------|----------------|
21
+ * | "/*" | "A" | "B" | "" | "" |
22
+ * | "/*" | "C" | "D" | "" | "" |
23
+ * | "/f" | "" | "" | "Hero" | "Once upon..." |
24
+ *
25
+ * becomes:
26
+ *
27
+ * {
28
+ * "/*": [
29
+ * { "key": "A", "value": "B" },
30
+ * { "key": "C", "value": "D" },
31
+ * ],
32
+ * "/f": [
33
+ * { "key": "title", "value": "Hero" },
34
+ * { "key": "description", "value": "Once upon..." },
35
+ * ]
36
+ * }
37
+ */
38
+ export declare interface ModifierMap {
39
+ [ModifierUrl:string]: Modifier[];
40
+ }
41
+
42
+ export declare interface Modifier {
43
+ key: string;
44
+ value: string;
45
+ }
46
+
47
+ export type ModifierKeyFilter = (key:string) => boolean;
48
+
49
+ /**
50
+ * The modifiers class help manage the metadata and headers modifiers.
51
+ */
52
+ export declare class ModifiersConfig {
53
+ /**
54
+ * Converts all non-valid characters to `-`.
55
+ * @param {string} text input text
56
+ * @returns {string} the meta name
57
+ */
58
+ static toMetaName(text: string): string;
59
+
60
+ /**
61
+ * Converts a globbing expression to regexp. Note that only `*` and `**` are supported yet.
62
+ * @param {string} glob
63
+ * @returns {RegExp}
64
+ */
65
+ static globToRegExp(glob:string): RegExp;
66
+
67
+ /**
68
+ * Empty modifiers
69
+ */
70
+ static EMPTY: ModifiersConfig;
71
+
72
+ /**
73
+ * Parses a sheet that is in a modifier format into a list of key/value pairs
74
+ *
75
+ * @example
76
+ *
77
+ * | url | key | value | Title | Description |
78
+ * |-------|-----|-------|---------|----------------|
79
+ * | "/*" | "A" | "B" | "" | "" |
80
+ * | "/*" | "C" | "D" | "" | "" |
81
+ * | "/f" | "" | "" | "Hero" | "Once upon..." |
82
+ *
83
+ * becomes:
84
+ *
85
+ * {
86
+ * "/*": [
87
+ * { "key": "A", "value": "B" },
88
+ * { "key": "C", "value": "D" },
89
+ * ],
90
+ * "/f": [
91
+ * { "key": "title", "value": "Hero" },
92
+ * { "key": "description", "value": "Once upon..." },
93
+ * ]
94
+ * }
95
+ *
96
+ *
97
+ * @param {object[]} sheet The sheet to parse
98
+ * @param {ModifierKeyFilter} keyFilter filter to apply on keys
99
+ * @returns {ModifierMap} An object containing an array of key/value pairs for every glob
100
+ */
101
+ static parseModifierSheet(sheet:object[], keyFilter: ModifierKeyFilter): ModifierMap;
102
+
103
+ /**
104
+ * Creates a ModifierConfig from the given data
105
+ * @param sheet
106
+ * @param keyFilter
107
+ */
108
+ static fromModifierSheet(sheet:object[], keyFilter: ModifierKeyFilter): ModifiersConfig;
109
+
110
+ /**
111
+ * Creates a new ModifiersConfig
112
+ * @param {ModifierMap} config
113
+ * @param {ModifierKeyFilter} keyFilter filter to apply on modifier keys
114
+ */
115
+ constructor(config:ModifierMap, keyFilter: ModifierKeyFilter);
116
+
117
+ /**
118
+ * Returns the modifier object for the given path, respecting the glob patterns in the urls.
119
+ * @param {string} path
120
+ * @return {object} the modifiers
121
+ */
122
+ getModifiers(path: string): object;
123
+ }
@@ -0,0 +1,177 @@
1
+ /*
2
+ * Copyright 2022 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ /**
14
+ * Converts all non-valid characters to `-`.
15
+ * @param {string} text input text
16
+ * @returns {string} the meta name
17
+ */
18
+ function toMetaName(text) {
19
+ return text
20
+ .toLowerCase()
21
+ .replace(/[^0-9a-z:_]/gi, '-');
22
+ }
23
+
24
+ /**
25
+ * The modifiers class help manage the metadata and headers modifiers.
26
+ */
27
+ class ModifiersConfig {
28
+ /**
29
+ * Converts a globbing expression to regexp. Note that only `*` and `**` are supported yet.
30
+ * @param {string} glob
31
+ * @returns {RegExp}
32
+ */
33
+ static globToRegExp(glob) {
34
+ const reString = glob
35
+ .replaceAll('**', '|')
36
+ .replaceAll('*', '[0-9a-z-.]*')
37
+ .replaceAll('|', '.*');
38
+ return new RegExp(`^${reString}$`);
39
+ }
40
+
41
+ /**
42
+ * Converts all keys in a row object to lowercase
43
+ * @param {Object} obj A row of data from a sheet
44
+ * @returns {Object} A row with all keys converted to lowercase
45
+ */
46
+ static toLowerKeys(obj) {
47
+ return Object.keys(obj).reduce((prev, key) => {
48
+ // eslint-disable-next-line no-param-reassign
49
+ prev[key.toLowerCase()] = obj[key];
50
+ return prev;
51
+ }, {});
52
+ }
53
+
54
+ /**
55
+ * Empty modifiers
56
+ * @type {ModifiersConfig}
57
+ */
58
+ static EMPTY = new ModifiersConfig({});
59
+
60
+ /**
61
+ * Parses a sheet that is in a modifier format into a list of key/value pairs
62
+ *
63
+ * @example
64
+ *
65
+ * | url | key | value | Title | Description |
66
+ * |-------|-----|-------|---------|----------------|
67
+ * | "/*" | "A" | "B" | "" | "" |
68
+ * | "/*" | "C" | "D" | "" | "" |
69
+ * | "/f" | "" | "" | "Hero" | "Once upon..." |
70
+ *
71
+ * becomes:
72
+ *
73
+ * {
74
+ * "/*": [
75
+ * { "key": "A", "value": "B" },
76
+ * { "key": "C", "value": "D" },
77
+ * ],
78
+ * "/f": [
79
+ * { "key": "title", "value": "Hero" },
80
+ * { "key": "description", "value": "Once upon..." },
81
+ * ]
82
+ * }
83
+ *
84
+ *
85
+ * @param {object[]} sheet The sheet to parse
86
+ * @param {ModifierKeyFilter} keyFilter filter to apply on keys
87
+ * @returns {ModifierMap} An object containing an array of key/value pairs for every glob
88
+ */
89
+ static parseModifierSheet(sheet, keyFilter = () => true) {
90
+ /** @type ModifierMap */
91
+ const res = {};
92
+ for (let row of sheet) {
93
+ row = ModifiersConfig.toLowerKeys(row);
94
+ const {
95
+ url, key, value, ...rest
96
+ } = row;
97
+ if (url) {
98
+ const put = (k, v) => {
99
+ if (keyFilter(k)) {
100
+ let entry = res[url];
101
+ if (!entry) {
102
+ entry = [];
103
+ res[url] = entry;
104
+ }
105
+ entry.push({ key: k, value: v });
106
+ }
107
+ };
108
+
109
+ // note that all values are strings, i.e. never another falsy value
110
+ if ('key' in row && 'value' in row && key && value) {
111
+ put(key, value);
112
+ } else {
113
+ Object.entries(rest).forEach(([k, v]) => {
114
+ if (k && v) {
115
+ put(k, v);
116
+ }
117
+ });
118
+ }
119
+ }
120
+ }
121
+ return res;
122
+ }
123
+
124
+ /**
125
+ * Creates a new `ModifiersConfig` from the given sheet.
126
+ *
127
+ * @see ModifiersConfig.parseModifierSheet
128
+ * @param {object[]} sheet The sheet to parse
129
+ * @param {ModifierKeyFilter} keyFilter filter to apply on keys
130
+ * @returns {ModifiersConfig} A ModifiersConfig instance.
131
+ */
132
+ static fromModifierSheet(sheet, keyFilter) {
133
+ return new ModifiersConfig(ModifiersConfig.parseModifierSheet(sheet, keyFilter));
134
+ }
135
+
136
+ /**
137
+ * Creates a new ModifiersConfig class.
138
+ * @param {ModifierMap} [map] The modifier map.
139
+ * @param {ModifierKeyFilter} keyFilter filter to apply on modifier keys
140
+ */
141
+ constructor(map, keyFilter = () => true) {
142
+ if (!map) {
143
+ return;
144
+ }
145
+ this.modifiers = Object.entries(map).map(([url, mods]) => {
146
+ const pat = url.indexOf('*') >= 0 ? ModifiersConfig.globToRegExp(url) : url;
147
+ return {
148
+ pat,
149
+ mods: mods.filter(({ key }) => keyFilter(key)),
150
+ };
151
+ });
152
+ }
153
+
154
+ /**
155
+ * Returns the modifier object for the given path.
156
+ * @param {string} path
157
+ * @return {object} the modifiers
158
+ */
159
+ getModifiers(path) {
160
+ if (!this.modifiers) {
161
+ return {};
162
+ }
163
+ const modifiers = {};
164
+ for (const { pat, mods } of this.modifiers) {
165
+ if (pat === path || (pat instanceof RegExp && pat.test(path))) {
166
+ for (const { key, value } of mods) {
167
+ modifiers[toMetaName(key)] = value;
168
+ }
169
+ }
170
+ }
171
+ return modifiers;
172
+ }
173
+ }
174
+
175
+ module.exports = {
176
+ ModifiersConfig,
177
+ };
package/src/index.d.ts CHANGED
@@ -10,3 +10,4 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
  export * from './config-wrapper';
13
+ export * from './ModifiersConfig';
package/src/index.js CHANGED
@@ -15,6 +15,7 @@ const { optionalConfig, requiredConfig } = require('./config-wrapper');
15
15
  const ValidationError = require('./ValidationError.js');
16
16
  const IgnoreConfig = require('./IgnoreConfig.js');
17
17
  const SitemapConfig = require('./SitemapConfig.js');
18
+ const { ModifiersConfig } = require('./ModifiersConfig.js');
18
19
 
19
20
  module.exports = {
20
21
  IndexConfig,
@@ -24,4 +25,5 @@ module.exports = {
24
25
  optionalConfig,
25
26
  requiredConfig,
26
27
  ValidationError,
28
+ ModifiersConfig,
27
29
  };