@did-btcr2/common 2.2.2 → 3.1.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/dist/cjs/canonicalization.js +68 -56
- package/dist/cjs/canonicalization.js.map +1 -1
- package/dist/cjs/constants.js +17 -12
- package/dist/cjs/constants.js.map +1 -1
- package/dist/cjs/errors.js +20 -3
- package/dist/cjs/errors.js.map +1 -1
- package/dist/cjs/index.js +5 -3
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/json-patch.js +98 -0
- package/dist/cjs/json-patch.js.map +1 -0
- package/dist/cjs/logger.js +46 -12
- package/dist/cjs/logger.js.map +1 -1
- package/dist/cjs/types.js.map +1 -1
- package/dist/cjs/utils/date.js +123 -0
- package/dist/cjs/utils/date.js.map +1 -0
- package/dist/cjs/utils/json.js +280 -0
- package/dist/cjs/utils/json.js.map +1 -0
- package/dist/cjs/utils/set.js +23 -0
- package/dist/cjs/utils/set.js.map +1 -0
- package/dist/cjs/utils/string.js +55 -0
- package/dist/cjs/utils/string.js.map +1 -0
- package/dist/esm/canonicalization.js +68 -56
- package/dist/esm/canonicalization.js.map +1 -1
- package/dist/esm/constants.js +17 -12
- package/dist/esm/constants.js.map +1 -1
- package/dist/esm/errors.js +20 -3
- package/dist/esm/errors.js.map +1 -1
- package/dist/esm/index.js +5 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/json-patch.js +98 -0
- package/dist/esm/json-patch.js.map +1 -0
- package/dist/esm/logger.js +46 -12
- package/dist/esm/logger.js.map +1 -1
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/utils/date.js +123 -0
- package/dist/esm/utils/date.js.map +1 -0
- package/dist/esm/utils/json.js +280 -0
- package/dist/esm/utils/json.js.map +1 -0
- package/dist/esm/utils/set.js +23 -0
- package/dist/esm/utils/set.js.map +1 -0
- package/dist/esm/utils/string.js +55 -0
- package/dist/esm/utils/string.js.map +1 -0
- package/dist/types/canonicalization.d.ts +40 -31
- package/dist/types/canonicalization.d.ts.map +1 -1
- package/dist/types/constants.d.ts +6 -9
- package/dist/types/constants.d.ts.map +1 -1
- package/dist/types/errors.d.ts +14 -3
- package/dist/types/errors.d.ts.map +1 -1
- package/dist/types/index.d.ts +5 -3
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/interfaces.d.ts +2 -273
- package/dist/types/interfaces.d.ts.map +1 -1
- package/dist/types/json-patch.d.ts +47 -0
- package/dist/types/json-patch.d.ts.map +1 -0
- package/dist/types/logger.d.ts +31 -8
- package/dist/types/logger.d.ts.map +1 -1
- package/dist/types/types.d.ts +12 -4
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/utils/date.d.ts +39 -0
- package/dist/types/utils/date.d.ts.map +1 -0
- package/dist/types/utils/json.d.ts +89 -0
- package/dist/types/utils/json.d.ts.map +1 -0
- package/dist/types/utils/set.d.ts +14 -0
- package/dist/types/utils/set.d.ts.map +1 -0
- package/dist/types/utils/string.d.ts +39 -0
- package/dist/types/utils/string.d.ts.map +1 -0
- package/package.json +3 -4
- package/src/canonicalization.ts +81 -64
- package/src/constants.ts +19 -13
- package/src/errors.ts +25 -3
- package/src/index.ts +5 -5
- package/src/interfaces.ts +2 -302
- package/src/json-patch.ts +103 -0
- package/src/logger.ts +59 -27
- package/src/types.ts +12 -6
- package/src/utils/date.ts +130 -0
- package/src/utils/json.ts +315 -0
- package/src/utils/set.ts +23 -0
- package/src/utils/string.ts +59 -0
- package/dist/cjs/exts.js +0 -189
- package/dist/cjs/exts.js.map +0 -1
- package/dist/cjs/patch.js +0 -163
- package/dist/cjs/patch.js.map +0 -1
- package/dist/esm/exts.js +0 -189
- package/dist/esm/exts.js.map +0 -1
- package/dist/esm/patch.js +0 -163
- package/dist/esm/patch.js.map +0 -1
- package/dist/types/exts.d.ts +0 -90
- package/dist/types/exts.d.ts.map +0 -1
- package/dist/types/patch.d.ts +0 -63
- package/dist/types/patch.d.ts.map +0 -1
- package/src/exts.ts +0 -310
- package/src/patch.ts +0 -181
- package/src/rdf-canonize.d.ts +0 -6
package/src/patch.ts
DELETED
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
import { PatchOperation } from './interfaces.js';
|
|
2
|
-
import { JSONObject } from './types.js';
|
|
3
|
-
import { MethodError } from './errors.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Implementation of {@link https://datatracker.ietf.org/doc/html/rfc6902 | IETF RFC 6902 JSON Patch}.
|
|
7
|
-
*
|
|
8
|
-
* JavaScript Object Notation (JSON) Patch defines a JSON document structure for expressing a sequence of operations to
|
|
9
|
-
* apply to a JavaScript Object Notation (JSON) document; it is suitable for use with the HTTP PATCH method. The
|
|
10
|
-
* "application/json-patch+json" media type is used to identify such patch documents.
|
|
11
|
-
*
|
|
12
|
-
* @class Patch
|
|
13
|
-
* @type {Patch}
|
|
14
|
-
*/
|
|
15
|
-
export class Patch {
|
|
16
|
-
/**
|
|
17
|
-
* Applies a JSON Patch to a source document and returns the patched document.
|
|
18
|
-
* @param {JSONObject} sourceDocument The source document to patch.
|
|
19
|
-
* @param {PatchOperation[]} operations The JSON Patch operations to apply.
|
|
20
|
-
* @returns {JSONObject} The patched document.
|
|
21
|
-
* @throws {Error} If an unsupported operation is provided.
|
|
22
|
-
*/
|
|
23
|
-
public apply(sourceDocument: JSONObject, operations: PatchOperation[]): JSONObject {
|
|
24
|
-
const patchedDocument = JSON.normalize(sourceDocument);
|
|
25
|
-
|
|
26
|
-
for (const operation of operations) {
|
|
27
|
-
const { op, path, value, from } = operation;
|
|
28
|
-
|
|
29
|
-
const segments = path.split('/').slice(1);
|
|
30
|
-
|
|
31
|
-
switch (op) {
|
|
32
|
-
case 'add':
|
|
33
|
-
this.setValue(patchedDocument, segments, value);
|
|
34
|
-
break;
|
|
35
|
-
|
|
36
|
-
case 'remove':
|
|
37
|
-
this.removeValue(patchedDocument, segments);
|
|
38
|
-
break;
|
|
39
|
-
|
|
40
|
-
case 'replace':
|
|
41
|
-
this.setValue(patchedDocument, segments, value);
|
|
42
|
-
break;
|
|
43
|
-
|
|
44
|
-
case 'move':{
|
|
45
|
-
if (!from) throw new Error('Missing \'from\' in move operation');
|
|
46
|
-
const fromSegments = from.split('/').slice(1);
|
|
47
|
-
const movedValue = this.getValue(patchedDocument, fromSegments);
|
|
48
|
-
this.removeValue(patchedDocument, fromSegments);
|
|
49
|
-
this.setValue(patchedDocument, segments, movedValue);
|
|
50
|
-
break;
|
|
51
|
-
}
|
|
52
|
-
case 'copy':{
|
|
53
|
-
if (!from) throw new Error('Missing \'from\' in copy operation');
|
|
54
|
-
const copiedValue = this.getValue(patchedDocument, from.split('/').slice(1));
|
|
55
|
-
this.setValue(patchedDocument, segments, copiedValue);
|
|
56
|
-
break;
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
case 'test':{
|
|
60
|
-
const existingValue = this.getValue(patchedDocument, segments);
|
|
61
|
-
if (JSON.stringify(existingValue) !== JSON.stringify(value)) {
|
|
62
|
-
throw new MethodError(`Test operation failed at path`, 'JSON_PATCH_APPLY_ERROR', { path });
|
|
63
|
-
}
|
|
64
|
-
break;
|
|
65
|
-
}
|
|
66
|
-
default:
|
|
67
|
-
throw new MethodError(`Unsupported JSON Patch operation`, 'JSON_PATCH_APPLY_ERROR', { op });
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return patchedDocument;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Constructs a JSON Patch with a single operation (e.g. add service).
|
|
76
|
-
* @param {PatchOperation} patches - The patch operation to create.
|
|
77
|
-
* @param {string} patch.op - The patch operation type (e.g. 'add').
|
|
78
|
-
* @param {string} patch.path - The JSON Pointer path to apply the operation.
|
|
79
|
-
* @param {*} patch.value - The value to apply (if applicable).
|
|
80
|
-
* @returns {PatchOperation[]} A single-entry JSON Patch array.
|
|
81
|
-
*/
|
|
82
|
-
public create(patches: PatchOperation[]): PatchOperation[] {
|
|
83
|
-
return patches.map(({ op, path, value, from }) => {
|
|
84
|
-
const operation: PatchOperation = { op, path };
|
|
85
|
-
|
|
86
|
-
if (value !== undefined) {
|
|
87
|
-
operation.value = value;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (from !== undefined) {
|
|
91
|
-
operation.from = from;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return operation;
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Find the diff between a source and target document constructing the patch operations from source => target.
|
|
100
|
-
* @param {JSONObject} sourceDocument The original JSON object.
|
|
101
|
-
* @param {JSONObject} targetDocument The target JSON object to transform into.
|
|
102
|
-
* @returns {PatchOperation[]} An array of JSON Patch operations.
|
|
103
|
-
*/
|
|
104
|
-
public diff(sourceDocument: JSONObject, targetDocument: JSONObject, path: string): PatchOperation[] {
|
|
105
|
-
const operations: Array<PatchOperation> = [];
|
|
106
|
-
const sourceKeys = new Set(Object.keys(sourceDocument || {}));
|
|
107
|
-
const targetKeys = new Set(Object.keys(targetDocument || {}));
|
|
108
|
-
|
|
109
|
-
// Handle removed keys
|
|
110
|
-
for (const key of sourceKeys) {
|
|
111
|
-
if (!targetKeys.has(key)) {
|
|
112
|
-
operations.push({ op: 'remove', path: `${path}/${key}` });
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// Handle added or updated keys
|
|
117
|
-
for (const key of targetKeys) {
|
|
118
|
-
const sourceVal = sourceDocument?.[key];
|
|
119
|
-
const targetVal = targetDocument?.[key];
|
|
120
|
-
const currentPath = `${path}/${key}`;
|
|
121
|
-
|
|
122
|
-
if (!(key in (sourceDocument || {}))) {
|
|
123
|
-
operations.push({ op: 'add', path: currentPath, value: targetVal });
|
|
124
|
-
} else if (typeof sourceVal === 'object' && sourceVal !== null && typeof targetVal === 'object' && targetVal !== null) {
|
|
125
|
-
operations.push(...this.diff(sourceVal, targetVal, currentPath));
|
|
126
|
-
} else if (JSON.stringify(sourceVal) !== JSON.stringify(targetVal)) {
|
|
127
|
-
operations.push({ op: 'replace', path: currentPath, value: targetVal });
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return operations;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Gets the value at a given path in an object.
|
|
136
|
-
* @private
|
|
137
|
-
* @param {*} obj The object to get the value from.
|
|
138
|
-
* @param {string[]} path The path to the value.
|
|
139
|
-
* @returns {*} The value at the given path.
|
|
140
|
-
*/
|
|
141
|
-
private getValue(obj: any, path: string[]): any {
|
|
142
|
-
return path.reduce((acc, key) => (acc && acc[key] !== undefined ? acc[key] : undefined), obj);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Sets the value at a given path in an object.
|
|
148
|
-
* @private
|
|
149
|
-
* @param {*} obj The object to set the value in.
|
|
150
|
-
* @param {string[]} path The path to the value.
|
|
151
|
-
* @param {*} value The value to set.
|
|
152
|
-
* @returns {*} The object with the value set.
|
|
153
|
-
*/
|
|
154
|
-
private setValue(obj: any, path: string[], value: any): void {
|
|
155
|
-
let current = obj;
|
|
156
|
-
for (let i = 0; i < path.length - 1; i++) {
|
|
157
|
-
const key = path[i];
|
|
158
|
-
if (!(key in current)) current[key] = {};
|
|
159
|
-
current = current[key];
|
|
160
|
-
}
|
|
161
|
-
current[path[path.length - 1]] = value;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Removes the value at a given path in an object.
|
|
167
|
-
* @private
|
|
168
|
-
* @param {*} obj The object to remove the value from.
|
|
169
|
-
* @param {string[]} path The path to the value.
|
|
170
|
-
* @returns {*} The object with the value removed.
|
|
171
|
-
*/
|
|
172
|
-
private removeValue(obj: any, path: string[]): void {
|
|
173
|
-
let current = obj;
|
|
174
|
-
for (let i = 0; i < path.length - 1; i++) {
|
|
175
|
-
const key = path[i];
|
|
176
|
-
if (!(key in current)) return;
|
|
177
|
-
current = current[key];
|
|
178
|
-
}
|
|
179
|
-
delete current[path[path.length - 1]];
|
|
180
|
-
}
|
|
181
|
-
}
|