@adobe/helix-config 2.3.1 → 2.3.2

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
+ ## [2.3.2](https://github.com/adobe/helix-config/compare/v2.3.1...v2.3.2) (2024-04-08)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * add support for granular crud ([7f1eb43](https://github.com/adobe/helix-config/commit/7f1eb434afbd809cd8fa11ba314815c0edb7cbb8)), closes [#38](https://github.com/adobe/helix-config/issues/38)
7
+
1
8
  ## [2.3.1](https://github.com/adobe/helix-config/compare/v2.3.0...v2.3.1) (2024-04-06)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-config",
3
- "version": "2.3.1",
3
+ "version": "2.3.2",
4
4
  "description": "Helix Config",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
@@ -9,8 +9,28 @@
9
9
  * OF ANY KIND, either express or implied. See the License for the specific language
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
- import { Response } from '@adobe/fetch';
13
12
  import { HelixStorage } from './storage.js';
13
+ import { StatusCodeError } from './status-code-error.js';
14
+ import { jsonGet, jsonPut } from './utils.js';
15
+
16
+ const FRAGMENTS = {
17
+ sites: {
18
+ content: 'object',
19
+ code: 'object',
20
+ folders: 'object',
21
+ headers: 'object',
22
+ metadata: 'object',
23
+ sidekick: 'object',
24
+ cdn: 'object',
25
+ 'cdn/prod': 'object',
26
+ 'cdn/preview': 'object',
27
+ 'cdn/live': 'object',
28
+ access: 'object',
29
+ 'access/admin': 'object',
30
+ 'access/preview': 'object',
31
+ 'access/live': 'object',
32
+ },
33
+ };
14
34
 
15
35
  /**
16
36
  * General purpose config store.
@@ -20,7 +40,6 @@ export class ConfigStore {
20
40
  * @param {string} org the org id
21
41
  * @param {string} type store type (org, sites, profiles, secrets, users)
22
42
  * @param {string} name config name
23
- * @param {string }ref the ref
24
43
  */
25
44
  constructor(org, type = 'org', name = 'config') {
26
45
  if (!org) {
@@ -37,46 +56,74 @@ export class ConfigStore {
37
56
  : `/orgs/${this.org}/${this.type}/${this.name}.json`;
38
57
  }
39
58
 
40
- async create(ctx) {
59
+ async create(ctx, data, relPath = '') {
60
+ if (relPath) {
61
+ throw new StatusCodeError(409, 'create not supported on substructures.');
62
+ }
41
63
  const storage = HelixStorage.fromContext(ctx).configBus();
42
64
  if (await storage.head(this.key)) {
43
- return new Response('', { status: 409 });
65
+ throw new StatusCodeError(409, 'config already exists.');
44
66
  }
45
- await storage.put(this.key, JSON.stringify(ctx.data), 'application/json');
46
- await this.purge(ctx, null, ctx.data);
47
- return new Response('', { status: 201 });
67
+ await storage.put(this.key, JSON.stringify(data), 'application/json');
68
+ await this.purge(ctx, null, data);
48
69
  }
49
70
 
50
- async read(ctx) {
71
+ async read(ctx, relPath = '') {
51
72
  const storage = HelixStorage.fromContext(ctx).configBus();
52
73
  const buf = await storage.get(this.key);
53
74
  if (!buf) {
54
- return new Response('', { status: 404 });
75
+ return null;
55
76
  }
56
- return new Response(buf, {
57
- headers: {
58
- 'content-type': 'application/json',
59
- },
60
- });
77
+ let obj = JSON.parse(buf);
78
+ if (relPath) {
79
+ const fragment = FRAGMENTS[this.type][relPath];
80
+ if (!fragment) {
81
+ throw new StatusCodeError(400, 'invalid object path.');
82
+ }
83
+ obj = jsonGet(obj, relPath);
84
+ }
85
+ return obj;
61
86
  }
62
87
 
63
- async update(ctx) {
88
+ async update(ctx, data, relPath = '') {
64
89
  const storage = HelixStorage.fromContext(ctx).configBus();
65
90
  const buf = await storage.get(this.key);
66
91
  const old = buf ? JSON.parse(buf) : null;
67
- await storage.put(this.key, JSON.stringify(ctx.data), 'application/json');
68
- await this.purge(ctx, old, ctx.data);
69
- return new Response('', { status: 202 });
92
+ if (relPath) {
93
+ if (!old) {
94
+ throw new StatusCodeError(404, 'config not found.');
95
+ }
96
+ const fragment = FRAGMENTS[this.type][relPath];
97
+ if (!fragment) {
98
+ throw new StatusCodeError(400, 'invalid object path.');
99
+ }
100
+ // eslint-disable-next-line no-param-reassign
101
+ data = jsonPut(JSON.parse(buf), relPath, data);
102
+ }
103
+
104
+ await storage.put(this.key, JSON.stringify(data), 'application/json');
105
+ await this.purge(ctx, old, data);
70
106
  }
71
107
 
72
- async remove(ctx) {
108
+ async remove(ctx, relPath) {
73
109
  const storage = HelixStorage.fromContext(ctx).configBus();
74
110
  const buf = await storage.get(this.key);
75
- if (buf) {
76
- await storage.remove(this.key);
77
- await this.purge(ctx, JSON.parse(buf), null);
111
+ if (!buf) {
112
+ throw new StatusCodeError(404, 'config not found.');
78
113
  }
79
- return new Response('', { status: 204 });
114
+ if (relPath) {
115
+ const fragment = FRAGMENTS[this.type][relPath];
116
+ if (!fragment) {
117
+ throw new StatusCodeError(400, 'invalid object path.');
118
+ }
119
+ const data = jsonPut(JSON.parse(buf), relPath, null);
120
+ await storage.put(this.key, JSON.stringify(data), 'application/json');
121
+ await this.purge(ctx, JSON.parse(buf), data);
122
+ return;
123
+ }
124
+
125
+ await storage.remove(this.key);
126
+ await this.purge(ctx, JSON.parse(buf), null);
80
127
  }
81
128
 
82
129
  // eslint-disable-next-line class-methods-use-this,no-unused-vars
@@ -0,0 +1,22 @@
1
+ /*
2
+ * Copyright 2024 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
+ export class StatusCodeError extends Error {
13
+ /**
14
+ * Creates a new StatusCodeError.
15
+ * @param {number} statusCode The status code.
16
+ * @param {string} message The error message.
17
+ */
18
+ constructor(statusCode, message) {
19
+ super(message);
20
+ this.statusCode = statusCode;
21
+ }
22
+ }
@@ -0,0 +1,32 @@
1
+ /*
2
+ * Copyright 2024 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
+ export function jsonGet(obj, path) {
13
+ return path.split('/').reduce((o, p) => o[p], obj);
14
+ }
15
+
16
+ export function jsonPut(obj, path, value) {
17
+ const parts = path.split('/');
18
+ const last = parts.pop();
19
+ const parent = parts.reduce((o, p) => {
20
+ if (!(p in o)) {
21
+ // eslint-disable-next-line no-param-reassign
22
+ o[p] = {};
23
+ }
24
+ return o[p];
25
+ }, obj);
26
+ if (value === null) {
27
+ delete parent[last];
28
+ } else {
29
+ parent[last] = value;
30
+ }
31
+ return obj;
32
+ }