terminusdb 12.0.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.
Files changed (86) hide show
  1. package/Contributing.md +36 -0
  2. package/LICENSE +201 -0
  3. package/README.md +175 -0
  4. package/RELEASE_NOTES.md +462 -0
  5. package/dist/index.html +22 -0
  6. package/dist/terminusdb-client.min.js +3 -0
  7. package/dist/terminusdb-client.min.js.LICENSE.txt +188 -0
  8. package/dist/terminusdb-client.min.js.map +1 -0
  9. package/dist/typescript/index.d.ts +14 -0
  10. package/dist/typescript/lib/accessControl.d.ts +554 -0
  11. package/dist/typescript/lib/axiosInstance.d.ts +2 -0
  12. package/dist/typescript/lib/connectionConfig.d.ts +381 -0
  13. package/dist/typescript/lib/const.d.ts +54 -0
  14. package/dist/typescript/lib/dispatchRequest.d.ts +17 -0
  15. package/dist/typescript/lib/errorMessage.d.ts +25 -0
  16. package/dist/typescript/lib/query/woqlBuilder.d.ts +75 -0
  17. package/dist/typescript/lib/query/woqlCore.d.ts +341 -0
  18. package/dist/typescript/lib/query/woqlDoc.d.ts +63 -0
  19. package/dist/typescript/lib/query/woqlLibrary.d.ts +718 -0
  20. package/dist/typescript/lib/query/woqlPrinter.d.ts +71 -0
  21. package/dist/typescript/lib/query/woqlQuery.d.ts +833 -0
  22. package/dist/typescript/lib/typedef.d.ts +624 -0
  23. package/dist/typescript/lib/utils.d.ts +199 -0
  24. package/dist/typescript/lib/valueHash.d.ts +146 -0
  25. package/dist/typescript/lib/viewer/chartConfig.d.ts +62 -0
  26. package/dist/typescript/lib/viewer/chooserConfig.d.ts +38 -0
  27. package/dist/typescript/lib/viewer/documentFrame.d.ts +44 -0
  28. package/dist/typescript/lib/viewer/frameConfig.d.ts +74 -0
  29. package/dist/typescript/lib/viewer/frameRule.d.ts +145 -0
  30. package/dist/typescript/lib/viewer/graphConfig.d.ts +73 -0
  31. package/dist/typescript/lib/viewer/objectFrame.d.ts +212 -0
  32. package/dist/typescript/lib/viewer/streamConfig.d.ts +23 -0
  33. package/dist/typescript/lib/viewer/tableConfig.d.ts +66 -0
  34. package/dist/typescript/lib/viewer/terminusRule.d.ts +75 -0
  35. package/dist/typescript/lib/viewer/viewConfig.d.ts +47 -0
  36. package/dist/typescript/lib/viewer/woqlChart.d.ts +1 -0
  37. package/dist/typescript/lib/viewer/woqlChooser.d.ts +56 -0
  38. package/dist/typescript/lib/viewer/woqlGraph.d.ts +26 -0
  39. package/dist/typescript/lib/viewer/woqlPaging.d.ts +1 -0
  40. package/dist/typescript/lib/viewer/woqlResult.d.ts +128 -0
  41. package/dist/typescript/lib/viewer/woqlRule.d.ts +96 -0
  42. package/dist/typescript/lib/viewer/woqlStream.d.ts +31 -0
  43. package/dist/typescript/lib/viewer/woqlTable.d.ts +102 -0
  44. package/dist/typescript/lib/viewer/woqlView.d.ts +49 -0
  45. package/dist/typescript/lib/woql.d.ts +1267 -0
  46. package/dist/typescript/lib/woqlClient.d.ts +1216 -0
  47. package/index.js +28 -0
  48. package/lib/.eslintrc +1 -0
  49. package/lib/accessControl.js +988 -0
  50. package/lib/axiosInstance.js +5 -0
  51. package/lib/connectionConfig.js +765 -0
  52. package/lib/const.js +59 -0
  53. package/lib/dispatchRequest.js +236 -0
  54. package/lib/errorMessage.js +110 -0
  55. package/lib/query/woqlBuilder.js +234 -0
  56. package/lib/query/woqlCore.js +934 -0
  57. package/lib/query/woqlDoc.js +177 -0
  58. package/lib/query/woqlLibrary.js +1015 -0
  59. package/lib/query/woqlPrinter.js +476 -0
  60. package/lib/query/woqlQuery.js +1865 -0
  61. package/lib/typedef.js +248 -0
  62. package/lib/utils.js +817 -0
  63. package/lib/valueHash.js_old +581 -0
  64. package/lib/viewer/chartConfig.js +411 -0
  65. package/lib/viewer/chooserConfig.js +234 -0
  66. package/lib/viewer/documentFrame.js +206 -0
  67. package/lib/viewer/frameConfig.js +469 -0
  68. package/lib/viewer/frameRule.js +519 -0
  69. package/lib/viewer/graphConfig.js +345 -0
  70. package/lib/viewer/objectFrame.js +1550 -0
  71. package/lib/viewer/streamConfig.js +82 -0
  72. package/lib/viewer/tableConfig.js +310 -0
  73. package/lib/viewer/terminusRule.js +196 -0
  74. package/lib/viewer/viewConfig.js +219 -0
  75. package/lib/viewer/woqlChart.js +17 -0
  76. package/lib/viewer/woqlChooser.js +171 -0
  77. package/lib/viewer/woqlGraph.js +295 -0
  78. package/lib/viewer/woqlPaging.js +148 -0
  79. package/lib/viewer/woqlResult.js +258 -0
  80. package/lib/viewer/woqlRule.js +312 -0
  81. package/lib/viewer/woqlStream.js +27 -0
  82. package/lib/viewer/woqlTable.js +332 -0
  83. package/lib/viewer/woqlView.js +107 -0
  84. package/lib/woql.js +1693 -0
  85. package/lib/woqlClient.js +2091 -0
  86. package/package.json +110 -0
@@ -0,0 +1,765 @@
1
+ /// /@ts-check
2
+ // connectionConfig
3
+ const { encodeURISegment } = require('./utils');
4
+ // eslint-disable-next-line no-unused-vars
5
+ const typedef = require('./typedef');
6
+
7
+ /**
8
+ * @file Terminus DB connection configuration
9
+ * @license Apache Version 2
10
+ * @description Object representing the state of a connection to a terminus db - these are:
11
+ * provides methods for getting and setting connection parameters
12
+ * @constructor
13
+ * @param {string} serverUrl - the terminusdb server url
14
+ * @param {typedef.ParamsObj} [params] - an object with the following connection parameters:
15
+ */
16
+
17
+ function ConnectionConfig(serverUrl, params) {
18
+ /**
19
+ * @type {string}
20
+ */
21
+ this.server = undefined;
22
+
23
+ // the base server url without the team name
24
+ this.baseServer = undefined;
25
+
26
+ // remote auth for authenticating to remote servers for push / fetch / clone
27
+ /** @type {typedef.CredentialObj} */
28
+ this.remote_auth = undefined;
29
+
30
+ // basic auth string for authenticating to local server
31
+ /** @type {typedef.CredentialObj } */
32
+ this.local_auth = undefined;
33
+
34
+ // these operate as cursors - where within the connected server context, we currently are
35
+ /** @type {string | boolean} */
36
+ this.organizationid = false;
37
+ /** @type {string | boolean} */
38
+ this.dbid = false;
39
+
40
+ this.default_branch_id = params && params.default_branch_id ? params.default_branch_id : 'main';
41
+ this.default_repo_id = 'local';
42
+ this.system_db = '_system';
43
+ this.api_extension = 'api/';
44
+ // default repository and branch ids
45
+ this.branchid = this.default_branch_id;
46
+ this.repoid = this.default_repo_id;
47
+ // set if pointing at a commit within a branch
48
+ /** @type {string | boolean} */
49
+ this.refid = false;
50
+ /** @type {string | boolean} */
51
+ this.connection_error = false;
52
+ // I can set serverURL only in constructor
53
+ const surl = this.parseServerURL(serverUrl);
54
+
55
+ this.server = surl;
56
+ if (params) this.update(params);
57
+ }
58
+
59
+ /**
60
+ * Creates a new connection config object and copies all the state information from this one into it
61
+ * @returns {ConnectionConfig}
62
+ */
63
+ ConnectionConfig.prototype.copy = function () {
64
+ const other = new ConnectionConfig(this.server);
65
+ other.api_extension = this.api_extension;
66
+ other.remote_auth = this.remote_auth;
67
+ other.local_auth = this.local_auth;
68
+ other.organizationid = this.organizationid;
69
+ other.dbid = this.dbid;
70
+ other.branchid = this.branchid;
71
+ other.repoid = this.repoid;
72
+ other.refid = this.refid;
73
+ return other;
74
+ };
75
+
76
+ /**
77
+ * updates connection config with new parameters
78
+ * @param {typedef.ParamsObj} params - an object with the following connection parameters:
79
+ */
80
+ // this is a general review
81
+ // I think we have to throw an error
82
+ // before a servercall
83
+ // when the user of key/jwt is not setted
84
+ ConnectionConfig.prototype.update = function (params) {
85
+ if (!params) return;
86
+ const orgID = params.organization || params.user;
87
+ // console.log("orgID",orgID,params)
88
+ this.setOrganization(orgID);
89
+ // if (typeof params.credential !== 'undefined')this.setTokenParameter(params.credential)
90
+ if (typeof params.db !== 'undefined') this.setDB(params.db);
91
+ // this to set the author and the jwt in creation
92
+ if (typeof params.token !== 'undefined') this.setLocalBasicAuth(params.token, params.user, 'apikey');
93
+ // this is for jwt
94
+ else if (typeof params.jwt !== 'undefined') this.setLocalBasicAuth(params.jwt, params.user, 'jwt');
95
+ // this is basic authO
96
+ else if (typeof params.key !== 'undefined') this.setLocalBasicAuth(params.key, params.user);
97
+ // If I set only the user I have to set the
98
+ else if (typeof params.user !== 'undefined') this.setLocalBasicAuth(null, params.user);
99
+ if (typeof params.branch !== 'undefined') this.setBranch(params.branch);
100
+ if (typeof params.ref !== 'undefined') this.setRef(params.ref);
101
+ if (typeof params.repo !== 'undefined') this.setRepo(params.repo);
102
+ };
103
+
104
+ /* ConnectionConfig.prototype.setTokenParameter = function(param){
105
+ this.tokenParameter = param
106
+ } */
107
+
108
+ /**
109
+ * Simple gets to retrieve current connection status
110
+ * Gets the current server url
111
+ * @returns {string}
112
+ */
113
+ ConnectionConfig.prototype.serverURL = function () {
114
+ return this.server;
115
+ };
116
+
117
+ /**
118
+ * Simple gets to retrieve current connection status
119
+ * Gets the current server url
120
+ * @returns {string}
121
+ */
122
+ ConnectionConfig.prototype.author = function () {
123
+ return this.author;
124
+ };
125
+
126
+ /**
127
+ * Gets the server connection url
128
+ * @returns {string}
129
+ */
130
+ ConnectionConfig.prototype.apiURL = function () {
131
+ return this.server + this.api_extension;
132
+ };
133
+
134
+ /**
135
+ * Gets the server connection url info
136
+ * @returns {string}
137
+ */
138
+ ConnectionConfig.prototype.apiURLInfo = function () {
139
+ return `${this.apiURL()}info`;
140
+ };
141
+
142
+ /**
143
+ * Gets the current database id
144
+ * @returns {string | boolean}
145
+ */
146
+ ConnectionConfig.prototype.db = function () {
147
+ if (!this.dbid) throw new Error('Invalid database name');
148
+ return this.dbid;
149
+ };
150
+
151
+ /**
152
+ * Gets the current branch id
153
+ * @returns {string}
154
+ */
155
+ ConnectionConfig.prototype.branch = function () {
156
+ return this.branchid;
157
+ };
158
+
159
+ /**
160
+ * Gets the current commit ref id
161
+ * @returns {string | boolean}
162
+ */
163
+ ConnectionConfig.prototype.ref = function () {
164
+ return this.refid;
165
+ };
166
+
167
+ /**
168
+ * Gets the current organization id
169
+ * @returns {string | boolean}
170
+ */
171
+ ConnectionConfig.prototype.organization = function () {
172
+ return this.organizationid;
173
+ };
174
+
175
+ /**
176
+ * Gets the current organization id
177
+ * @returns {string}
178
+ */
179
+ ConnectionConfig.prototype.repo = function () {
180
+ return this.repoid;
181
+ };
182
+
183
+ /**
184
+ *Gets the local Authorization credentials
185
+ *return {CredentialObj | boolean}
186
+ */
187
+ ConnectionConfig.prototype.localAuth = function () {
188
+ if (this.local_auth) return this.local_auth;
189
+ return false;
190
+ };
191
+
192
+ /**
193
+ *Gets the local user name
194
+ *return {string | boolean}
195
+ */
196
+ ConnectionConfig.prototype.localUser = function () {
197
+ if (this.local_auth) return this.local_auth.user;
198
+ return false;
199
+ };
200
+
201
+ /**
202
+ *Gets the current user name
203
+ *@param {boolean} [ignoreJwt]
204
+ *return {string | boolean}
205
+ */
206
+ ConnectionConfig.prototype.user = function (ignoreJwt) {
207
+ if (!ignoreJwt && this.remote_auth && this.remote_auth.type === 'jwt') return this.remote_auth.user;
208
+ if (this.local_auth) {
209
+ return this.local_auth.user;
210
+ }
211
+ return false;
212
+ };
213
+
214
+ /**
215
+ * Check the server URL
216
+ * @param {string} str - the server url string
217
+ * @returns {string}
218
+ */
219
+
220
+ ConnectionConfig.prototype.parseServerURL = function (str) {
221
+ if (str && (str.substring(0, 7) === 'http://' || str.substring(0, 8) === 'https://')) {
222
+ if (str.lastIndexOf('/') !== str.length - 1) {
223
+ // eslint-disable-next-line no-param-reassign
224
+ str += '/'; // always append slash to ensure regularity
225
+ }
226
+ return this.serverUrlEncoding(str);
227
+ }
228
+ // throw an error this is the
229
+ // return false
230
+ throw new Error(`Invalid Server URL: ${str}`);
231
+ };
232
+
233
+ ConnectionConfig.prototype.serverUrlEncoding = function (str) {
234
+ const orgArr = str.split('/');
235
+ if (orgArr.length > 4) {
236
+ // we can need only the server baseurl
237
+ this.baseServer = str.replace(`${orgArr[3]}/`, '');
238
+ // const org = encodeURI(orgArr[3])
239
+ /*
240
+ * if we pass the organization name like Francesca-Bit-9e73
241
+ * I need to encode it 2 times because in the database
242
+ * when we create an id from a property it encodes the property value
243
+ */
244
+ const org = encodeURISegment(orgArr[3]);
245
+ return str.replace(orgArr[3], org);
246
+ }
247
+ this.baseServer = str;
248
+ return str;
249
+ };
250
+
251
+ /**
252
+ * Clear cursor for connection
253
+ */
254
+ ConnectionConfig.prototype.clearCursor = function () {
255
+ this.branchid = this.default_branch_id;
256
+ this.repoid = this.default_repo_id;
257
+ this.organizationid = false;
258
+ this.dbid = false;
259
+ this.refid = false;
260
+ };
261
+
262
+ /**
263
+ * @param {string | boolean} errorMessage
264
+ */
265
+ ConnectionConfig.prototype.setError = function (errorMessage) {
266
+ this.connection_error = errorMessage;
267
+ };
268
+
269
+ /**
270
+ * Set the organization to which the connected db belongs
271
+ * (not the users organization - set in capabilities)
272
+ * @param {string | boolean} [orgId]
273
+ */
274
+ ConnectionConfig.prototype.setOrganization = function (orgId = 'admin') {
275
+ this.organizationid = orgId;
276
+ };
277
+
278
+ /**
279
+ * Set the local identifier of db
280
+ * @param {string | boolean} dbId - database Id
281
+ */
282
+
283
+ ConnectionConfig.prototype.setDB = function (dbId) {
284
+ this.dbid = dbId;
285
+ };
286
+
287
+ /**
288
+ * Set the repository type |local|remote|
289
+ * @param {typedef.RepoType | string} repoId - for the local server - identifier of repo
290
+ */
291
+ ConnectionConfig.prototype.setRepo = function (repoId) {
292
+ this.repoid = repoId;
293
+ };
294
+
295
+ /**
296
+ * @param {string} [branchId] - id of branch
297
+ */
298
+ ConnectionConfig.prototype.setBranch = function (branchId) {
299
+ this.branchid = branchId || this.default_branch_id;
300
+ };
301
+
302
+ /**
303
+ * Set an Reference ID or Commit ID.
304
+ * Commit IDs are unique hashes that are created whenever a new commit is recorded
305
+ * @param {string | boolean} refId - commit reference id
306
+ */
307
+ ConnectionConfig.prototype.setRef = function (refId) {
308
+ this.refid = refId;
309
+ // return this.refid
310
+ };
311
+
312
+ /**
313
+ * set the local database connection credential
314
+ * @param {string} [remoteKey] - jwt auth api key
315
+ * @param {string} [remoteUserID] - remote user id
316
+ */
317
+ ConnectionConfig.prototype.setRemoteBasicAuth = function (remoteKey, remoteUserID) {
318
+ if (!remoteKey) {
319
+ this.remote_auth = undefined;
320
+ } else {
321
+ this.remote_auth = { type: 'jwt', user: remoteUserID, key: remoteKey };
322
+ }
323
+ };
324
+
325
+ /**
326
+ * set the local database connection credential
327
+ * @param {string} [userKey] - basic auth api key
328
+ * @param {string} [userId] - user id
329
+ * @param {string} [type] - basic|jwt|apikey
330
+ */
331
+ // to be review this is ok for the basic Auth
332
+ ConnectionConfig.prototype.setLocalBasicAuth = function (userKey, userId = 'admin', type = 'basic') {
333
+ this.local_auth = { type, user: userId, key: userKey };
334
+ };
335
+
336
+ /**
337
+ * Set the local server connection credential
338
+ * @param {typedef.CredentialObj} newCredential
339
+ */
340
+ ConnectionConfig.prototype.setLocalAuth = function (newCredential) {
341
+ this.local_auth = newCredential;
342
+ };
343
+
344
+ /**
345
+ * Set the remote server connection credential
346
+ * @param {typedef.CredentialObj} newCredential
347
+ */
348
+ ConnectionConfig.prototype.setRemoteAuth = function (newCredential) {
349
+ this.remote_auth = newCredential;
350
+ };
351
+
352
+ /**
353
+ *Gets the remote Authorization credentials
354
+ *to connect the local db with a remote terminusdb database for push-pull-clone actions
355
+ *return {CredentialObj| boolean}
356
+ */
357
+ ConnectionConfig.prototype.remoteAuth = function () {
358
+ if (this.remote_auth) return this.remote_auth;
359
+ return false;
360
+ };
361
+
362
+ /**
363
+ * Generate the db endpoit url for create / delete db
364
+ * @returns {string}
365
+ */
366
+ ConnectionConfig.prototype.dbURL = function () {
367
+ return this.dbBase('db');
368
+ };
369
+
370
+ /**
371
+ * Generate URL for the user's api endpoint
372
+ * @param {string} [user] - the user id
373
+ * @returns {string}
374
+ */
375
+
376
+ ConnectionConfig.prototype.userURL = function (user) {
377
+ let url = `${this.apiURL()}user`;
378
+ if (user) url += `/${encodeURISegment(user)}`;
379
+ return url;
380
+ };
381
+
382
+ /**
383
+ * Generate URL for the user's organization api endpoint
384
+ * @param {string} orgId - the organization id
385
+ * @param {string} [action] - the organization id
386
+ * @returns {string}
387
+ */
388
+ // encodeURIComponent
389
+ ConnectionConfig.prototype.organizationURL = function (orgId, action) {
390
+ let url = `${this.apiURL()}organization`;
391
+ // I have to encode the organization 2 times because it is saved encoded inside the database
392
+ //
393
+ if (orgId) url += `/${encodeURISegment(orgId)}`;
394
+ if (action) url += `/${encodeURISegment(action)}`;
395
+ return url;
396
+ };
397
+
398
+ /**
399
+ * Generate URL for the user's organization api endpoint
400
+ * @param {string} orgId - the organization id
401
+ * @param {string} [action] - the organization id
402
+ * @returns {string}
403
+ */
404
+
405
+ ConnectionConfig.prototype.userOrganizationsURL = function () {
406
+ const url = `${this.apiURL()}user_organizations`;
407
+ return url;
408
+ };
409
+
410
+ /**
411
+ * Generate URL for the user's roles api endpoint
412
+ * @returns {string}
413
+ */
414
+ ConnectionConfig.prototype.rolesURL = function () {
415
+ return `${this.apiURL()}role`;
416
+ };
417
+
418
+ /**
419
+ * Generate URL to update the user's role api endpoint
420
+ * @returns {string}
421
+ */
422
+
423
+ // REVIEW maybe it can be the same of roleURL but we can change the rest action in put.
424
+
425
+ ConnectionConfig.prototype.updateRolesURL = function () {
426
+ return `${this.apiURL()}update_role`;
427
+ };
428
+
429
+ /**
430
+ * Generate URL for create / delete graph api endpoint
431
+ * @param {string} graphType
432
+ * @returns {string}
433
+ */
434
+ ConnectionConfig.prototype.graphURL = function (graphType) {
435
+ return `${this.branchBase('graph')}/${graphType}/main`;
436
+ };
437
+
438
+ /**
439
+ * Generate URL for get / set schema api endpoint
440
+ * @param {string} graphType
441
+ * @returns {string}
442
+ */
443
+ ConnectionConfig.prototype.triplesURL = function (graphType) {
444
+ // eslint-disable-next-line vars-on-top
445
+ // eslint-disable-next-line no-var
446
+ let url = '';
447
+ if (this.db() === this.system_db) {
448
+ // eslint-disable-next-line no-unused-vars
449
+ const s = this.dbBase('triples');
450
+ } else {
451
+ url = this.branchBase('triples');
452
+ }
453
+
454
+ url += `/${graphType}/main`;
455
+ return url;
456
+ };
457
+
458
+ /**
459
+ * Generate URL for add / get csv api endpoint
460
+ * @returns {string}
461
+ */
462
+
463
+ ConnectionConfig.prototype.csvURL = function () {
464
+ const s = this.branchBase('csv');
465
+ return s;
466
+ };
467
+
468
+ /**
469
+ * Generate URL for woql query api endpoint
470
+ * @returns {string}
471
+ */
472
+ ConnectionConfig.prototype.queryURL = function () {
473
+ if (this.db() === this.system_db) return this.dbBase('woql');
474
+ return this.branchBase('woql');
475
+ };
476
+
477
+ /**
478
+ * Generate URL for get back the commits logs
479
+ * @returns {string}
480
+ */
481
+ ConnectionConfig.prototype.log = function () {
482
+ if (this.db() === this.system_db) return this.dbBase('log');
483
+ return this.branchBase('log');
484
+ };
485
+
486
+ /**
487
+ * get the url to update the organization role in the system database
488
+ * don't change the end point (this is a terminus db server end point)
489
+ * @returns {string}
490
+ */
491
+ ConnectionConfig.prototype.updateOrganizationRoleURL = function () {
492
+ return `${this.apiURL()}update_role`;
493
+ };
494
+
495
+ /**
496
+ * Generate URL for clone db endpoint
497
+ * @param {string} [newRepoId] the repository id
498
+ * @returns {string}
499
+ */
500
+ ConnectionConfig.prototype.cloneURL = function (newRepoId) {
501
+ let crl = `${this.apiURL()}clone/${this.organization()}`;
502
+ if (newRepoId) crl += `/${newRepoId}`;
503
+ return crl;
504
+ };
505
+
506
+ /**
507
+ * URL at which a db can be cloned
508
+ * @returns {string}
509
+ */
510
+ ConnectionConfig.prototype.cloneableURL = function () {
511
+ return `${this.serverURL()}${this.organization()}/${this.db()}`;
512
+ };
513
+
514
+ /**
515
+ * Generate URL for pull endpoint
516
+ * @returns {string}
517
+ */
518
+ ConnectionConfig.prototype.pullURL = function () {
519
+ const purl = this.branchBase('pull');
520
+ return purl;
521
+ };
522
+
523
+ /**
524
+ * Generate URL for pull endpoint
525
+ * @returns {string}
526
+ */
527
+ ConnectionConfig.prototype.patchURL = function () {
528
+ const purl = this.branchBase('patch');
529
+ return purl;
530
+ };
531
+
532
+ /**
533
+ * Generate URL for diff endpoint
534
+ * @returns {string}
535
+ */
536
+ ConnectionConfig.prototype.diffURL = function () {
537
+ const purl = this.branchBase('diff');
538
+ return purl;
539
+ };
540
+
541
+ /**
542
+ * Generate URL for diff endpoint
543
+ * @returns {string}
544
+ */
545
+ ConnectionConfig.prototype.applyURL = function () {
546
+ const purl = this.branchBase('apply');
547
+ return purl;
548
+ };
549
+
550
+ /**
551
+ * Generate url portion consisting of organization/dbid
552
+ * (unless dbid = system dbname in which case there is no organization)
553
+ * @property {typedef.DocParamsPost|Object} params
554
+ */
555
+
556
+ ConnectionConfig.prototype.docHistoryURL = function (params) {
557
+ const paramsStr = this.queryParameter(params);
558
+ if (this.db() === this.system_db) {
559
+ return this.dbBase('history') + paramsStr;
560
+ }
561
+ return this.branchBase('history') + paramsStr;
562
+ };
563
+
564
+ /**
565
+ * Generate URL for fetch endpoint
566
+ * @param {string} remoteName
567
+ * @returns {string}
568
+ */
569
+ ConnectionConfig.prototype.fetchURL = function (remoteName) {
570
+ const purl = this.dbBase('fetch');
571
+ return `${purl}/${remoteName}/_commits`;
572
+ };
573
+
574
+ /**
575
+ * Generate URL for remote endpoint
576
+ * @param {string} [remoteName] - optional remote name
577
+ * @returns {string}
578
+ */
579
+ ConnectionConfig.prototype.remoteURL = function (remoteName) {
580
+ const base = this.dbBase('remote');
581
+ if (remoteName) {
582
+ return `${base}/${encodeURISegment(remoteName)}`;
583
+ }
584
+ return base;
585
+ };
586
+
587
+ /**
588
+ * Generate URL for rebase endpoint
589
+ * @returns {string}
590
+ */
591
+ ConnectionConfig.prototype.rebaseURL = function () {
592
+ const purl = this.branchBase('rebase');
593
+ return purl;
594
+ };
595
+
596
+ /**
597
+ * Generate URL for reset endpoint
598
+ * @returns {string}
599
+ */
600
+ ConnectionConfig.prototype.resetURL = function () {
601
+ const purl = this.branchBase('reset');
602
+ return purl;
603
+ };
604
+
605
+ /**
606
+ * Generate URL for push endpoint
607
+ * @returns {string}
608
+ */
609
+ ConnectionConfig.prototype.pushURL = function () {
610
+ const purl = this.branchBase('push');
611
+ return purl;
612
+ };
613
+
614
+ /**
615
+ * Generate URL for branch endpoint
616
+ * @param {string} branchId - the branch id
617
+ * @returns {string}
618
+ */
619
+ ConnectionConfig.prototype.branchURL = function (branchId) {
620
+ const url = this.repoBase('branch');
621
+ return `${url}/branch/${branchId}`;
622
+ };
623
+
624
+ /**
625
+ * Generate URL for branch squash endpoint
626
+ */
627
+ ConnectionConfig.prototype.squashBranchURL = function (nuid) {
628
+ const b = this.repoBase('squash');
629
+ return `${b}/branch/${nuid}`;
630
+ };
631
+
632
+ /**
633
+ * Generate URL for branch reset endpoint
634
+ */
635
+ ConnectionConfig.prototype.resetBranchUrl = function (nuid) {
636
+ const b = this.repoBase('reset');
637
+ return `${b}/branch/${nuid}`;
638
+ };
639
+
640
+ /**
641
+ * Generate URL for commit descriptor
642
+ * @param {string} commitId
643
+ * @returns {string} a commit pathname
644
+ */
645
+ // this is not a url
646
+ ConnectionConfig.prototype.commitDescriptorUrl = function (commitId) {
647
+ return `${this.organization()}/${this.db()}/${this.repoid}/commit/${commitId}`;
648
+ };
649
+
650
+ /**
651
+ * Generate URL for optimizing db branch
652
+ */
653
+ // encodeURI() will not encode: ~!@#$&*()=:/,;?+'
654
+ ConnectionConfig.prototype.optimizeBranchUrl = function (branchId) {
655
+ // let o = this.optimizeBase()
656
+ const dbBase = this.dbBase('optimize');
657
+ return `${dbBase}/${this.repoid}/branch/${encodeURIComponent(branchId)}`;
658
+ // return dbBase + `${this.user()}/${this.db()}/${this.repoid}/branch/${nuid}`
659
+ };
660
+
661
+ /**
662
+ * Generate base db url consisting of server/action/organization/dbid
663
+ * @param {typedef.ActionType} action
664
+ * @returns {string}
665
+ */
666
+ ConnectionConfig.prototype.dbBase = function (action) {
667
+ return `${this.apiURL()}${action}/${this.dbURLFragment()}`;
668
+ };
669
+
670
+ // https://127.0.0.1:6363/api/document/admin/cloud-profiles/local/branch/main?graph_type=instance
671
+ // instance
672
+
673
+ /**
674
+ * Generate base branch url consisting of server/action/organization/dbid/branchid
675
+ * @param {typedef.ActionType} action
676
+ * @returns {string}
677
+ */
678
+ ConnectionConfig.prototype.repoBase = function (action) {
679
+ let b = this.dbBase(action);
680
+ if (this.repo()) b += `/${this.repo()}`;
681
+ else b += `/${this.default_repo_id}`;
682
+ return b;
683
+ };
684
+
685
+ /**
686
+ * Get database branch Url
687
+ * Generate base branch url consisting of server/action/organization/dbid/branchid
688
+ * @param {typedef.ActionType} action
689
+ * @returns {string}
690
+ */
691
+ ConnectionConfig.prototype.branchBase = function (action) {
692
+ let b = this.repoBase(action);
693
+ // _meta repo is magic - stores repository metadata
694
+ if (this.repo() === '_meta') {
695
+ return b;
696
+ }
697
+ // _commits branch is magic - stores all commits for repo
698
+
699
+ /*
700
+ *https://127.0.0.1:6363/api/db/admin/profiles01/local/_commits
701
+ */
702
+ if (this.branch() === '_commits') {
703
+ return `${b}/${this.branch()}`;
704
+ } if (this.ref()) {
705
+ return `${b}/commit/${this.ref()}`;
706
+ } if (this.branch()) {
707
+ return `${b}/branch/${encodeURIComponent(this.branch())}`;
708
+ } b += `/branch/${this.default_branch_id}`;
709
+ return b;
710
+ };
711
+
712
+ /**
713
+ * Generate url portion consisting of organization/dbid
714
+ * (unless dbid = system dbname in which case there is no organization)
715
+ * @returns {string|boolean}
716
+ */
717
+ // encodeURIComponent
718
+ ConnectionConfig.prototype.dbURLFragment = function () {
719
+ if (this.db() === this.system_db) return this.db();
720
+ return `${encodeURISegment(this.organization())}/${encodeURISegment(this.db())}`;
721
+ };
722
+
723
+ /**
724
+ * Generate url portion consisting of organization/dbid
725
+ * (unless dbid = system dbname in which case there is no organization)
726
+ * @property {typedef.DocParamsPost|Object} params
727
+ */
728
+
729
+ ConnectionConfig.prototype.documentURL = function (params) {
730
+ const paramsStr = this.queryParameter(params);
731
+ if (this.db() === this.system_db) {
732
+ return this.dbBase('document') + paramsStr;
733
+ }
734
+ return this.branchBase('document') + paramsStr;
735
+ };
736
+
737
+ ConnectionConfig.prototype.prefixesURL = function () {
738
+ if (this.db() === this.system_db) {
739
+ return this.dbBase('prefixes');
740
+ }
741
+ return this.branchBase('prefixes');
742
+ };
743
+
744
+ ConnectionConfig.prototype.prefixURL = function () {
745
+ if (this.db() === this.system_db) {
746
+ return this.dbBase('prefix');
747
+ }
748
+ return this.branchBase('prefix');
749
+ };
750
+
751
+ ConnectionConfig.prototype.queryParameter = function (params) {
752
+ if (!params || typeof params !== 'object') return '';
753
+ const queryString = Object.keys(params).map((key) => `${key}=${encodeURISegment(params[key])}`).join('&');
754
+ return `?${queryString}`;
755
+ };
756
+
757
+ ConnectionConfig.prototype.jsonSchemaURL = function (params) {
758
+ const paramsStr = this.queryParameter(params);
759
+ if (this.db() === this.system_db) {
760
+ return this.dbBase('schema') + paramsStr;
761
+ }
762
+ return this.branchBase('schema') + paramsStr;
763
+ };
764
+
765
+ module.exports = ConnectionConfig;