@coveo/push-api-client 3.0.122 → 3.0.124
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/APICore.js +47 -47
- package/dist/definitions/APICore.d.ts +18 -18
- package/dist/definitions/document.d.ts +124 -124
- package/dist/definitions/documentBuilder.d.ts +168 -168
- package/dist/definitions/documentBuilder.spec.d.ts +1 -1
- package/dist/definitions/environment.d.ts +16 -16
- package/dist/definitions/errors/baseError.d.ts +4 -4
- package/dist/definitions/errors/fieldErrors.d.ts +18 -18
- package/dist/definitions/errors/index.d.ts +3 -3
- package/dist/definitions/errors/privilegeError.d.ts +6 -6
- package/dist/definitions/errors/validatorErrors.d.ts +20 -20
- package/dist/definitions/fieldAnalyser/fieldAnalyser.d.ts +36 -36
- package/dist/definitions/fieldAnalyser/fieldAnalyser.spec.d.ts +1 -1
- package/dist/definitions/fieldAnalyser/fieldStore.d.ts +5 -5
- package/dist/definitions/fieldAnalyser/fieldUtils.d.ts +6 -6
- package/dist/definitions/fieldAnalyser/fieldsUtils.spec.d.ts +1 -1
- package/dist/definitions/fieldAnalyser/inconsistencies.d.ts +5 -5
- package/dist/definitions/fieldAnalyser/inconsistencies.spec.d.ts +1 -1
- package/dist/definitions/fieldAnalyser/typeUtils.d.ts +3 -3
- package/dist/definitions/fieldAnalyser/typeUtils.spec.d.ts +1 -1
- package/dist/definitions/help/file.d.ts +3 -3
- package/dist/definitions/help/file.spec.d.ts +1 -1
- package/dist/definitions/help/fileConsumer.d.ts +31 -31
- package/dist/definitions/help/fileConsumer.spec.d.ts +1 -1
- package/dist/definitions/help/fileContainer.d.ts +8 -8
- package/dist/definitions/help/fileContainer.spec.d.ts +1 -1
- package/dist/definitions/help/function.d.ts +1 -1
- package/dist/definitions/help/generator.d.ts +11 -11
- package/dist/definitions/help/generator.spec.d.ts +1 -1
- package/dist/definitions/help/urlUtils.d.ts +27 -27
- package/dist/definitions/help/urlUtils.spec.d.ts +1 -1
- package/dist/definitions/index.d.ts +14 -14
- package/dist/definitions/interfaces.d.ts +70 -70
- package/dist/definitions/localtest.d.ts +1 -1
- package/dist/definitions/permissionSetBuilder.d.ts +34 -34
- package/dist/definitions/permissionSetBuilder.spec.d.ts +1 -1
- package/dist/definitions/securityIdentityBuilder.d.ts +105 -105
- package/dist/definitions/securityIdentityBuilder.spec.d.ts +1 -1
- package/dist/definitions/source/batchUploadDocumentsFromFile.d.ts +11 -11
- package/dist/definitions/source/batchUploadDocumentsFromFile.spec.d.ts +1 -1
- package/dist/definitions/source/catalog.d.ts +70 -70
- package/dist/definitions/source/catalog.spec.d.ts +1 -1
- package/dist/definitions/source/documentUploader.d.ts +11 -11
- package/dist/definitions/source/documentUploader.spec.d.ts +1 -1
- package/dist/definitions/source/push.d.ts +87 -87
- package/dist/definitions/source/push.spec.d.ts +1 -1
- package/dist/definitions/source/securityIdenty.d.ts +43 -43
- package/dist/definitions/uploadStrategy/fileContainerStrategy.d.ts +23 -23
- package/dist/definitions/uploadStrategy/fileContainerStrategy.spec.d.ts +1 -1
- package/dist/definitions/uploadStrategy/index.d.ts +3 -3
- package/dist/definitions/uploadStrategy/strategy.d.ts +23 -23
- package/dist/definitions/uploadStrategy/streamChunkStrategy.d.ts +28 -28
- package/dist/definitions/uploadStrategy/streamChunkStrategy.spec.d.ts +1 -1
- package/dist/definitions/validation/caseInsensitiveDocument.d.ts +13 -13
- package/dist/definitions/validation/knownKey.d.ts +10 -10
- package/dist/definitions/validation/parseFile.d.ts +4 -4
- package/dist/definitions/validation/parseFile.spec.d.ts +1 -1
- package/dist/definitions/validation/parsePermissions.d.ts +5 -5
- package/dist/definitions/validation/parsePermissions.spec.d.ts +1 -1
- package/dist/definitions/validation/preconditions/apiKeyPrivilege.d.ts +3 -3
- package/dist/definitions/validation/preconditions/apiKeyPrivilege.spec.d.ts +1 -1
- package/dist/definitions/validation/preconditions/platformPrivilege.d.ts +7 -7
- package/dist/definitions/validation/requiredKeyValidator.d.ts +17 -17
- package/dist/definitions/validation/transformers/transformer.d.ts +31 -31
- package/dist/definitions/validation/transformers/transformer.spec.d.ts +1 -1
- package/dist/document.js +2 -2
- package/dist/documentBuilder.js +292 -292
- package/dist/documentBuilder.spec.js +191 -191
- package/dist/environment.js +41 -41
- package/dist/errors/baseError.js +10 -10
- package/dist/errors/fieldErrors.js +42 -42
- package/dist/errors/index.js +19 -19
- package/dist/errors/privilegeError.js +12 -12
- package/dist/errors/validatorErrors.js +40 -40
- package/dist/fieldAnalyser/fieldAnalyser.js +95 -95
- package/dist/fieldAnalyser/fieldAnalyser.spec.js +230 -230
- package/dist/fieldAnalyser/fieldStore.js +21 -21
- package/dist/fieldAnalyser/fieldUtils.js +41 -41
- package/dist/fieldAnalyser/fieldsUtils.spec.js +116 -116
- package/dist/fieldAnalyser/inconsistencies.js +14 -14
- package/dist/fieldAnalyser/inconsistencies.spec.js +40 -40
- package/dist/fieldAnalyser/typeUtils.js +61 -61
- package/dist/fieldAnalyser/typeUtils.spec.js +142 -142
- package/dist/help/file.js +37 -37
- package/dist/help/file.spec.js +67 -67
- package/dist/help/fileConsumer.js +150 -150
- package/dist/help/fileConsumer.spec.js +153 -153
- package/dist/help/fileContainer.js +48 -48
- package/dist/help/fileContainer.spec.js +65 -65
- package/dist/help/function.js +5 -5
- package/dist/help/generator.js +32 -32
- package/dist/help/generator.spec.js +82 -82
- package/dist/help/urlUtils.js +42 -42
- package/dist/help/urlUtils.spec.js +41 -41
- package/dist/index.js +38 -38
- package/dist/interfaces.js +2 -2
- package/dist/localtest.js +55 -55
- package/dist/permissionSetBuilder.js +58 -58
- package/dist/permissionSetBuilder.spec.js +68 -68
- package/dist/securityIdentityBuilder.js +128 -128
- package/dist/securityIdentityBuilder.spec.js +50 -50
- package/dist/source/batchUploadDocumentsFromFile.js +46 -46
- package/dist/source/batchUploadDocumentsFromFile.spec.js +145 -145
- package/dist/source/catalog.js +111 -111
- package/dist/source/catalog.spec.js +64 -64
- package/dist/source/documentUploader.js +45 -45
- package/dist/source/documentUploader.spec.js +118 -118
- package/dist/source/push.js +140 -140
- package/dist/source/push.spec.js +152 -152
- package/dist/source/securityIdenty.js +57 -57
- package/dist/uploadStrategy/fileContainerStrategy.js +32 -32
- package/dist/uploadStrategy/fileContainerStrategy.spec.js +86 -86
- package/dist/uploadStrategy/index.js +19 -19
- package/dist/uploadStrategy/strategy.js +2 -2
- package/dist/uploadStrategy/streamChunkStrategy.js +52 -52
- package/dist/uploadStrategy/streamChunkStrategy.spec.js +77 -77
- package/dist/validation/caseInsensitiveDocument.js +31 -31
- package/dist/validation/knownKey.js +37 -37
- package/dist/validation/parseFile.js +120 -120
- package/dist/validation/parseFile.spec.js +96 -96
- package/dist/validation/parsePermissions.js +118 -118
- package/dist/validation/parsePermissions.spec.js +86 -86
- package/dist/validation/preconditions/apiKeyPrivilege.js +20 -20
- package/dist/validation/preconditions/apiKeyPrivilege.spec.js +66 -66
- package/dist/validation/preconditions/platformPrivilege.js +32 -32
- package/dist/validation/requiredKeyValidator.js +61 -61
- package/dist/validation/transformers/transformer.js +51 -51
- package/dist/validation/transformers/transformer.spec.js +21 -21
- package/package.json +4 -4
package/dist/documentBuilder.js
CHANGED
@@ -1,293 +1,293 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
3
|
-
var t = {};
|
4
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
5
|
-
t[p] = s[p];
|
6
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
7
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
8
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
9
|
-
t[p[i]] = s[p[i]];
|
10
|
-
}
|
11
|
-
return t;
|
12
|
-
};
|
13
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
14
|
-
exports.DocumentBuilder = void 0;
|
15
|
-
const dayjs = require("dayjs");
|
16
|
-
const crypto_1 = require("crypto");
|
17
|
-
const fieldUtils_1 = require("./fieldAnalyser/fieldUtils");
|
18
|
-
const fieldErrors_1 = require("./errors/fieldErrors");
|
19
|
-
const transformer_1 = require("./validation/transformers/transformer");
|
20
|
-
const zod_1 = require("zod");
|
21
|
-
const url_1 = require("url");
|
22
|
-
/**
|
23
|
-
* Utility class to build a {@link Document}.
|
24
|
-
*/
|
25
|
-
class DocumentBuilder {
|
26
|
-
/**
|
27
|
-
*
|
28
|
-
* @param uri The URI of the document. See {@link Document.uri}
|
29
|
-
* @param title The title of the document. See {@link Document.title}
|
30
|
-
*/
|
31
|
-
constructor(uri, title) {
|
32
|
-
this.uri = uri;
|
33
|
-
this.permissions = [];
|
34
|
-
this.doc = {
|
35
|
-
uri,
|
36
|
-
title,
|
37
|
-
metadata: {},
|
38
|
-
permissions: this.permissions,
|
39
|
-
};
|
40
|
-
}
|
41
|
-
/**
|
42
|
-
* Set the data of the document. See {@link Document.data}
|
43
|
-
* @param data
|
44
|
-
* @returns
|
45
|
-
*/
|
46
|
-
withData(data) {
|
47
|
-
this.doc.data = data;
|
48
|
-
return this;
|
49
|
-
}
|
50
|
-
/**
|
51
|
-
* Set the date of the document. See {@link Document.date}
|
52
|
-
* @param date
|
53
|
-
* @returns
|
54
|
-
*/
|
55
|
-
withDate(date) {
|
56
|
-
this.doc.date = this.validateDateAndReturnValidDate(date);
|
57
|
-
return this;
|
58
|
-
}
|
59
|
-
/**
|
60
|
-
* Set the modified date of the document. See {@link Document.modifiedDate}
|
61
|
-
* @param date
|
62
|
-
* @returns
|
63
|
-
*/
|
64
|
-
withModifiedDate(date) {
|
65
|
-
this.doc.modifiedDate = this.validateDateAndReturnValidDate(date);
|
66
|
-
return this;
|
67
|
-
}
|
68
|
-
/**
|
69
|
-
* Set the permanentID of the document. See {@link Document.permanentId}
|
70
|
-
* @param permanentID
|
71
|
-
* @returns
|
72
|
-
*/
|
73
|
-
withPermanentId(permanentID) {
|
74
|
-
this.doc.permanentId = permanentID;
|
75
|
-
return this;
|
76
|
-
}
|
77
|
-
/**
|
78
|
-
* Set the base64 encoded, compressed binary data of the document. See {@link Document.compressedBinaryData}
|
79
|
-
* @param data
|
80
|
-
* @param compressionType
|
81
|
-
* @returns
|
82
|
-
*/
|
83
|
-
withCompressedBinaryData(data, compressionType) {
|
84
|
-
this.validateCompressedBinaryData(data);
|
85
|
-
this.doc.compressedBinaryData = {
|
86
|
-
data,
|
87
|
-
compressionType,
|
88
|
-
};
|
89
|
-
return this;
|
90
|
-
}
|
91
|
-
/**
|
92
|
-
* Set the file extension on the document. See {@link Document.fileExtension}
|
93
|
-
* @param extension
|
94
|
-
* @returns
|
95
|
-
*/
|
96
|
-
withFileExtension(extension) {
|
97
|
-
if (extension[0] !== '.') {
|
98
|
-
throw `Extension ${extension} should start with a leading .`;
|
99
|
-
}
|
100
|
-
this.doc.fileExtension = extension;
|
101
|
-
return this;
|
102
|
-
}
|
103
|
-
/**
|
104
|
-
* Set the parentID on the document. See {@link Document.parentId}
|
105
|
-
* @param id
|
106
|
-
* @returns
|
107
|
-
*/
|
108
|
-
withParentID(id) {
|
109
|
-
this.doc.parentId = id;
|
110
|
-
return this;
|
111
|
-
}
|
112
|
-
/**
|
113
|
-
* Set the clickableURI on the document. See {@link Document.clickableUri}
|
114
|
-
* @param clickURI
|
115
|
-
* @returns
|
116
|
-
*/
|
117
|
-
withClickableUri(clickURI) {
|
118
|
-
// TODO: Validate that it's a valid link/proper scheme... ?
|
119
|
-
// Unsure
|
120
|
-
this.doc.clickableUri = clickURI;
|
121
|
-
return this;
|
122
|
-
}
|
123
|
-
/**
|
124
|
-
* Set the author on the document. See {@link Document.author}
|
125
|
-
* @param author
|
126
|
-
* @returns
|
127
|
-
*/
|
128
|
-
withAuthor(author) {
|
129
|
-
this.doc.author = author;
|
130
|
-
return this;
|
131
|
-
}
|
132
|
-
/**
|
133
|
-
* Add a single metadata key and value pair on the document. See {@link Document.metadata}
|
134
|
-
* @param {string} key
|
135
|
-
* @param {MetadataValue} value
|
136
|
-
* @param {Transformer} [metadataKeyTransformer=BuiltInTransformers.identity] The {@link Transformer} to apply to the metadata key.
|
137
|
-
* If not specified, no transformation will be applied to the metadata key.
|
138
|
-
*
|
139
|
-
* For a list of built-in transformers, use {@link BuiltInTransformers}.
|
140
|
-
* @returns
|
141
|
-
*/
|
142
|
-
withMetadataValue(key, value, keyTransformer = transformer_1.BuiltInTransformers.identity) {
|
143
|
-
const transformedKey = keyTransformer(key);
|
144
|
-
const reservedKeyNames = [
|
145
|
-
'compressedBinaryData',
|
146
|
-
'compressedBinaryDataFileId',
|
147
|
-
'parentId',
|
148
|
-
'fileExtension',
|
149
|
-
'data',
|
150
|
-
'permissions',
|
151
|
-
'documentId',
|
152
|
-
'orderingId',
|
153
|
-
];
|
154
|
-
if (reservedKeyNames.some((reservedKey) => reservedKey.toLowerCase() === transformedKey.toLowerCase())) {
|
155
|
-
throw `Cannot use ${transformedKey} as a metadata key: It is a reserved key name. See https://docs.coveo.com/en/78/index-content/push-api-reference#json-document-reserved-key-names`;
|
156
|
-
}
|
157
|
-
if (!(0, fieldUtils_1.isFieldNameValid)(transformedKey)) {
|
158
|
-
throw new fieldErrors_1.UnsupportedFieldError([key, transformedKey]);
|
159
|
-
}
|
160
|
-
this.doc.metadata[transformedKey] = value;
|
161
|
-
return this;
|
162
|
-
}
|
163
|
-
/**
|
164
|
-
* Set metadata on the document. See {@link Document.metadata}
|
165
|
-
* @param {Metadata} metadata
|
166
|
-
* @param {Transformer} [metadataKeyTransformer=BuiltInTransformers.identity] The {@link Transformer} to apply to all the document metadata keys.
|
167
|
-
* If not specified, no transformation will be applied to the metadata keys.
|
168
|
-
*
|
169
|
-
* For a list of built-in transformers, use {@link BuiltInTransformers}.
|
170
|
-
* @returns
|
171
|
-
*/
|
172
|
-
withMetadata(metadata, metadataKeyTransformer = transformer_1.BuiltInTransformers.identity) {
|
173
|
-
const invalidKeys = [];
|
174
|
-
for (const [k, v] of Object.entries(metadata)) {
|
175
|
-
try {
|
176
|
-
this.withMetadataValue(k, v, metadataKeyTransformer);
|
177
|
-
}
|
178
|
-
catch (error) {
|
179
|
-
if (error instanceof fieldErrors_1.UnsupportedFieldError) {
|
180
|
-
invalidKeys.push(...error.unsupportedFields);
|
181
|
-
}
|
182
|
-
else {
|
183
|
-
throw error;
|
184
|
-
}
|
185
|
-
}
|
186
|
-
}
|
187
|
-
if (invalidKeys.length > 0) {
|
188
|
-
throw new fieldErrors_1.UnsupportedFieldError(...invalidKeys);
|
189
|
-
}
|
190
|
-
return this;
|
191
|
-
}
|
192
|
-
/**
|
193
|
-
* Set a permission set on the document
|
194
|
-
* Multiple permission sets can be combined.
|
195
|
-
*
|
196
|
-
* See [Simple Permission Model Definition Examples](https://docs.coveo.com/en/107)
|
197
|
-
*/
|
198
|
-
withPermissionSet(permissionSetBuilder) {
|
199
|
-
this.permissions.push(permissionSetBuilder.build());
|
200
|
-
return this;
|
201
|
-
}
|
202
|
-
/**
|
203
|
-
* Set a permission level on the document
|
204
|
-
* Multiple permission levels can be added to the document. In this case, the order at which this method is called will influence the permission hierarchy.
|
205
|
-
*
|
206
|
-
* Consider the following example:
|
207
|
-
* ```
|
208
|
-
* doc.withPermissionLevel('level1', [setA])
|
209
|
-
* .withPermissionLevel('level2', [setB, setC])
|
210
|
-
* ```
|
211
|
-
*
|
212
|
-
* Permission sets from level1 (`setA`) will supersed the ones from level2 (`setB` and `setC`).
|
213
|
-
*
|
214
|
-
* See [Complex Permission Model Definition](https://docs.coveo.com/en/25/index-content/complex-permission-model-definition-example)
|
215
|
-
*/
|
216
|
-
withPermissionLevel(permissionLevelName, permissionSetBuilders) {
|
217
|
-
const permissionSets = permissionSetBuilders.map((permissionSet) => permissionSet.build());
|
218
|
-
this.permissions.push({
|
219
|
-
name: permissionLevelName,
|
220
|
-
permissionSets,
|
221
|
-
});
|
222
|
-
return this;
|
223
|
-
}
|
224
|
-
build() {
|
225
|
-
return this.doc;
|
226
|
-
}
|
227
|
-
/**
|
228
|
-
* Marshal the document into a JSON format accepted by the push API.
|
229
|
-
* @returns
|
230
|
-
*/
|
231
|
-
marshal() {
|
232
|
-
this.validateAndFillMissing();
|
233
|
-
const _a = this.doc, { uri, metadata, permissions, permanentId } = _a, omitSomeProperties = __rest(_a, ["uri", "metadata", "permissions", "permanentId"]);
|
234
|
-
const out = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ permanentid: permanentId }, omitSomeProperties), this.marshalMetadata()), this.marshalCompressedBinaryData()), this.marshalPermissions()), this.marshalDocumentId());
|
235
|
-
return out;
|
236
|
-
}
|
237
|
-
marshalDocumentId() {
|
238
|
-
return {
|
239
|
-
documentId: zod_1.z.string().url().safeParse(this.uri).success
|
240
|
-
? this.uri
|
241
|
-
: (0, url_1.pathToFileURL)(this.uri).toString(),
|
242
|
-
};
|
243
|
-
}
|
244
|
-
marshalMetadata() {
|
245
|
-
if (!this.doc.metadata) {
|
246
|
-
return {};
|
247
|
-
}
|
248
|
-
const out = {};
|
249
|
-
for (const [k, v] of Object.entries(this.doc.metadata)) {
|
250
|
-
out[k] = v;
|
251
|
-
}
|
252
|
-
return out;
|
253
|
-
}
|
254
|
-
marshalCompressedBinaryData() {
|
255
|
-
if (!this.doc.compressedBinaryData) {
|
256
|
-
return {};
|
257
|
-
}
|
258
|
-
return {
|
259
|
-
compressedBinaryData: this.doc.compressedBinaryData.data,
|
260
|
-
compressionType: this.doc.compressedBinaryData.compressionType,
|
261
|
-
};
|
262
|
-
}
|
263
|
-
marshalPermissions() {
|
264
|
-
const permissions = this.permissions;
|
265
|
-
return Object.assign({}, (permissions.length && { permissions }));
|
266
|
-
}
|
267
|
-
validateAndFillMissing() {
|
268
|
-
// TODO: validation that cannot be performed on a single property, but requires looking at multiple property at the same time.
|
269
|
-
// or validate that we don't have both `data` AND `compressedBinaryData`.
|
270
|
-
// Could also use https://www.npmjs.com/package/@coveo/bueno to validate schema (useful for pure JS users).
|
271
|
-
if (!this.doc.permanentId) {
|
272
|
-
this.doc.permanentId = this.generatePermanentId();
|
273
|
-
}
|
274
|
-
return;
|
275
|
-
}
|
276
|
-
generatePermanentId() {
|
277
|
-
const md5 = (0, crypto_1.createHash)('md5').update(this.doc.uri).digest('hex');
|
278
|
-
const sha1 = (0, crypto_1.createHash)('sha1').update(this.doc.uri).digest('hex');
|
279
|
-
return md5.substring(0, 30) + sha1.substring(0, 30);
|
280
|
-
}
|
281
|
-
validateDateAndReturnValidDate(d) {
|
282
|
-
const validatedDate = dayjs(d);
|
283
|
-
return validatedDate.toISOString();
|
284
|
-
}
|
285
|
-
validateCompressedBinaryData(data) {
|
286
|
-
const isBase64 = Buffer.from(data, 'base64').toString('base64') === data;
|
287
|
-
if (!isBase64) {
|
288
|
-
throw 'Invalid compressedBinaryData: When using compressedBinaryData, the data must be base64 encoded.';
|
289
|
-
}
|
290
|
-
}
|
291
|
-
}
|
292
|
-
exports.DocumentBuilder = DocumentBuilder;
|
1
|
+
"use strict";
|
2
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
3
|
+
var t = {};
|
4
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
5
|
+
t[p] = s[p];
|
6
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
7
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
8
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
9
|
+
t[p[i]] = s[p[i]];
|
10
|
+
}
|
11
|
+
return t;
|
12
|
+
};
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
14
|
+
exports.DocumentBuilder = void 0;
|
15
|
+
const dayjs = require("dayjs");
|
16
|
+
const crypto_1 = require("crypto");
|
17
|
+
const fieldUtils_1 = require("./fieldAnalyser/fieldUtils");
|
18
|
+
const fieldErrors_1 = require("./errors/fieldErrors");
|
19
|
+
const transformer_1 = require("./validation/transformers/transformer");
|
20
|
+
const zod_1 = require("zod");
|
21
|
+
const url_1 = require("url");
|
22
|
+
/**
|
23
|
+
* Utility class to build a {@link Document}.
|
24
|
+
*/
|
25
|
+
class DocumentBuilder {
|
26
|
+
/**
|
27
|
+
*
|
28
|
+
* @param uri The URI of the document. See {@link Document.uri}
|
29
|
+
* @param title The title of the document. See {@link Document.title}
|
30
|
+
*/
|
31
|
+
constructor(uri, title) {
|
32
|
+
this.uri = uri;
|
33
|
+
this.permissions = [];
|
34
|
+
this.doc = {
|
35
|
+
uri,
|
36
|
+
title,
|
37
|
+
metadata: {},
|
38
|
+
permissions: this.permissions,
|
39
|
+
};
|
40
|
+
}
|
41
|
+
/**
|
42
|
+
* Set the data of the document. See {@link Document.data}
|
43
|
+
* @param data
|
44
|
+
* @returns
|
45
|
+
*/
|
46
|
+
withData(data) {
|
47
|
+
this.doc.data = data;
|
48
|
+
return this;
|
49
|
+
}
|
50
|
+
/**
|
51
|
+
* Set the date of the document. See {@link Document.date}
|
52
|
+
* @param date
|
53
|
+
* @returns
|
54
|
+
*/
|
55
|
+
withDate(date) {
|
56
|
+
this.doc.date = this.validateDateAndReturnValidDate(date);
|
57
|
+
return this;
|
58
|
+
}
|
59
|
+
/**
|
60
|
+
* Set the modified date of the document. See {@link Document.modifiedDate}
|
61
|
+
* @param date
|
62
|
+
* @returns
|
63
|
+
*/
|
64
|
+
withModifiedDate(date) {
|
65
|
+
this.doc.modifiedDate = this.validateDateAndReturnValidDate(date);
|
66
|
+
return this;
|
67
|
+
}
|
68
|
+
/**
|
69
|
+
* Set the permanentID of the document. See {@link Document.permanentId}
|
70
|
+
* @param permanentID
|
71
|
+
* @returns
|
72
|
+
*/
|
73
|
+
withPermanentId(permanentID) {
|
74
|
+
this.doc.permanentId = permanentID;
|
75
|
+
return this;
|
76
|
+
}
|
77
|
+
/**
|
78
|
+
* Set the base64 encoded, compressed binary data of the document. See {@link Document.compressedBinaryData}
|
79
|
+
* @param data
|
80
|
+
* @param compressionType
|
81
|
+
* @returns
|
82
|
+
*/
|
83
|
+
withCompressedBinaryData(data, compressionType) {
|
84
|
+
this.validateCompressedBinaryData(data);
|
85
|
+
this.doc.compressedBinaryData = {
|
86
|
+
data,
|
87
|
+
compressionType,
|
88
|
+
};
|
89
|
+
return this;
|
90
|
+
}
|
91
|
+
/**
|
92
|
+
* Set the file extension on the document. See {@link Document.fileExtension}
|
93
|
+
* @param extension
|
94
|
+
* @returns
|
95
|
+
*/
|
96
|
+
withFileExtension(extension) {
|
97
|
+
if (extension[0] !== '.') {
|
98
|
+
throw `Extension ${extension} should start with a leading .`;
|
99
|
+
}
|
100
|
+
this.doc.fileExtension = extension;
|
101
|
+
return this;
|
102
|
+
}
|
103
|
+
/**
|
104
|
+
* Set the parentID on the document. See {@link Document.parentId}
|
105
|
+
* @param id
|
106
|
+
* @returns
|
107
|
+
*/
|
108
|
+
withParentID(id) {
|
109
|
+
this.doc.parentId = id;
|
110
|
+
return this;
|
111
|
+
}
|
112
|
+
/**
|
113
|
+
* Set the clickableURI on the document. See {@link Document.clickableUri}
|
114
|
+
* @param clickURI
|
115
|
+
* @returns
|
116
|
+
*/
|
117
|
+
withClickableUri(clickURI) {
|
118
|
+
// TODO: Validate that it's a valid link/proper scheme... ?
|
119
|
+
// Unsure
|
120
|
+
this.doc.clickableUri = clickURI;
|
121
|
+
return this;
|
122
|
+
}
|
123
|
+
/**
|
124
|
+
* Set the author on the document. See {@link Document.author}
|
125
|
+
* @param author
|
126
|
+
* @returns
|
127
|
+
*/
|
128
|
+
withAuthor(author) {
|
129
|
+
this.doc.author = author;
|
130
|
+
return this;
|
131
|
+
}
|
132
|
+
/**
|
133
|
+
* Add a single metadata key and value pair on the document. See {@link Document.metadata}
|
134
|
+
* @param {string} key
|
135
|
+
* @param {MetadataValue} value
|
136
|
+
* @param {Transformer} [metadataKeyTransformer=BuiltInTransformers.identity] The {@link Transformer} to apply to the metadata key.
|
137
|
+
* If not specified, no transformation will be applied to the metadata key.
|
138
|
+
*
|
139
|
+
* For a list of built-in transformers, use {@link BuiltInTransformers}.
|
140
|
+
* @returns
|
141
|
+
*/
|
142
|
+
withMetadataValue(key, value, keyTransformer = transformer_1.BuiltInTransformers.identity) {
|
143
|
+
const transformedKey = keyTransformer(key);
|
144
|
+
const reservedKeyNames = [
|
145
|
+
'compressedBinaryData',
|
146
|
+
'compressedBinaryDataFileId',
|
147
|
+
'parentId',
|
148
|
+
'fileExtension',
|
149
|
+
'data',
|
150
|
+
'permissions',
|
151
|
+
'documentId',
|
152
|
+
'orderingId',
|
153
|
+
];
|
154
|
+
if (reservedKeyNames.some((reservedKey) => reservedKey.toLowerCase() === transformedKey.toLowerCase())) {
|
155
|
+
throw `Cannot use ${transformedKey} as a metadata key: It is a reserved key name. See https://docs.coveo.com/en/78/index-content/push-api-reference#json-document-reserved-key-names`;
|
156
|
+
}
|
157
|
+
if (!(0, fieldUtils_1.isFieldNameValid)(transformedKey)) {
|
158
|
+
throw new fieldErrors_1.UnsupportedFieldError([key, transformedKey]);
|
159
|
+
}
|
160
|
+
this.doc.metadata[transformedKey] = value;
|
161
|
+
return this;
|
162
|
+
}
|
163
|
+
/**
|
164
|
+
* Set metadata on the document. See {@link Document.metadata}
|
165
|
+
* @param {Metadata} metadata
|
166
|
+
* @param {Transformer} [metadataKeyTransformer=BuiltInTransformers.identity] The {@link Transformer} to apply to all the document metadata keys.
|
167
|
+
* If not specified, no transformation will be applied to the metadata keys.
|
168
|
+
*
|
169
|
+
* For a list of built-in transformers, use {@link BuiltInTransformers}.
|
170
|
+
* @returns
|
171
|
+
*/
|
172
|
+
withMetadata(metadata, metadataKeyTransformer = transformer_1.BuiltInTransformers.identity) {
|
173
|
+
const invalidKeys = [];
|
174
|
+
for (const [k, v] of Object.entries(metadata)) {
|
175
|
+
try {
|
176
|
+
this.withMetadataValue(k, v, metadataKeyTransformer);
|
177
|
+
}
|
178
|
+
catch (error) {
|
179
|
+
if (error instanceof fieldErrors_1.UnsupportedFieldError) {
|
180
|
+
invalidKeys.push(...error.unsupportedFields);
|
181
|
+
}
|
182
|
+
else {
|
183
|
+
throw error;
|
184
|
+
}
|
185
|
+
}
|
186
|
+
}
|
187
|
+
if (invalidKeys.length > 0) {
|
188
|
+
throw new fieldErrors_1.UnsupportedFieldError(...invalidKeys);
|
189
|
+
}
|
190
|
+
return this;
|
191
|
+
}
|
192
|
+
/**
|
193
|
+
* Set a permission set on the document
|
194
|
+
* Multiple permission sets can be combined.
|
195
|
+
*
|
196
|
+
* See [Simple Permission Model Definition Examples](https://docs.coveo.com/en/107)
|
197
|
+
*/
|
198
|
+
withPermissionSet(permissionSetBuilder) {
|
199
|
+
this.permissions.push(permissionSetBuilder.build());
|
200
|
+
return this;
|
201
|
+
}
|
202
|
+
/**
|
203
|
+
* Set a permission level on the document
|
204
|
+
* Multiple permission levels can be added to the document. In this case, the order at which this method is called will influence the permission hierarchy.
|
205
|
+
*
|
206
|
+
* Consider the following example:
|
207
|
+
* ```
|
208
|
+
* doc.withPermissionLevel('level1', [setA])
|
209
|
+
* .withPermissionLevel('level2', [setB, setC])
|
210
|
+
* ```
|
211
|
+
*
|
212
|
+
* Permission sets from level1 (`setA`) will supersed the ones from level2 (`setB` and `setC`).
|
213
|
+
*
|
214
|
+
* See [Complex Permission Model Definition](https://docs.coveo.com/en/25/index-content/complex-permission-model-definition-example)
|
215
|
+
*/
|
216
|
+
withPermissionLevel(permissionLevelName, permissionSetBuilders) {
|
217
|
+
const permissionSets = permissionSetBuilders.map((permissionSet) => permissionSet.build());
|
218
|
+
this.permissions.push({
|
219
|
+
name: permissionLevelName,
|
220
|
+
permissionSets,
|
221
|
+
});
|
222
|
+
return this;
|
223
|
+
}
|
224
|
+
build() {
|
225
|
+
return this.doc;
|
226
|
+
}
|
227
|
+
/**
|
228
|
+
* Marshal the document into a JSON format accepted by the push API.
|
229
|
+
* @returns
|
230
|
+
*/
|
231
|
+
marshal() {
|
232
|
+
this.validateAndFillMissing();
|
233
|
+
const _a = this.doc, { uri, metadata, permissions, permanentId } = _a, omitSomeProperties = __rest(_a, ["uri", "metadata", "permissions", "permanentId"]);
|
234
|
+
const out = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ permanentid: permanentId }, omitSomeProperties), this.marshalMetadata()), this.marshalCompressedBinaryData()), this.marshalPermissions()), this.marshalDocumentId());
|
235
|
+
return out;
|
236
|
+
}
|
237
|
+
marshalDocumentId() {
|
238
|
+
return {
|
239
|
+
documentId: zod_1.z.string().url().safeParse(this.uri).success
|
240
|
+
? this.uri
|
241
|
+
: (0, url_1.pathToFileURL)(this.uri).toString(),
|
242
|
+
};
|
243
|
+
}
|
244
|
+
marshalMetadata() {
|
245
|
+
if (!this.doc.metadata) {
|
246
|
+
return {};
|
247
|
+
}
|
248
|
+
const out = {};
|
249
|
+
for (const [k, v] of Object.entries(this.doc.metadata)) {
|
250
|
+
out[k] = v;
|
251
|
+
}
|
252
|
+
return out;
|
253
|
+
}
|
254
|
+
marshalCompressedBinaryData() {
|
255
|
+
if (!this.doc.compressedBinaryData) {
|
256
|
+
return {};
|
257
|
+
}
|
258
|
+
return {
|
259
|
+
compressedBinaryData: this.doc.compressedBinaryData.data,
|
260
|
+
compressionType: this.doc.compressedBinaryData.compressionType,
|
261
|
+
};
|
262
|
+
}
|
263
|
+
marshalPermissions() {
|
264
|
+
const permissions = this.permissions;
|
265
|
+
return Object.assign({}, (permissions.length && { permissions }));
|
266
|
+
}
|
267
|
+
validateAndFillMissing() {
|
268
|
+
// TODO: validation that cannot be performed on a single property, but requires looking at multiple property at the same time.
|
269
|
+
// or validate that we don't have both `data` AND `compressedBinaryData`.
|
270
|
+
// Could also use https://www.npmjs.com/package/@coveo/bueno to validate schema (useful for pure JS users).
|
271
|
+
if (!this.doc.permanentId) {
|
272
|
+
this.doc.permanentId = this.generatePermanentId();
|
273
|
+
}
|
274
|
+
return;
|
275
|
+
}
|
276
|
+
generatePermanentId() {
|
277
|
+
const md5 = (0, crypto_1.createHash)('md5').update(this.doc.uri).digest('hex');
|
278
|
+
const sha1 = (0, crypto_1.createHash)('sha1').update(this.doc.uri).digest('hex');
|
279
|
+
return md5.substring(0, 30) + sha1.substring(0, 30);
|
280
|
+
}
|
281
|
+
validateDateAndReturnValidDate(d) {
|
282
|
+
const validatedDate = dayjs(d);
|
283
|
+
return validatedDate.toISOString();
|
284
|
+
}
|
285
|
+
validateCompressedBinaryData(data) {
|
286
|
+
const isBase64 = Buffer.from(data, 'base64').toString('base64') === data;
|
287
|
+
if (!isBase64) {
|
288
|
+
throw 'Invalid compressedBinaryData: When using compressedBinaryData, the data must be base64 encoded.';
|
289
|
+
}
|
290
|
+
}
|
291
|
+
}
|
292
|
+
exports.DocumentBuilder = DocumentBuilder;
|
293
293
|
//# sourceMappingURL=documentBuilder.js.map
|