Skip to content

Commit ba2e09b

Browse files
committed
review diff method and add apply
1 parent 941e710 commit ba2e09b

File tree

2 files changed

+112
-31
lines changed

2 files changed

+112
-31
lines changed

lib/connectionConfig.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,15 @@ ConnectionConfig.prototype.diffURL = function () {
512512
return purl;
513513
};
514514

515+
/**
516+
* Generate URL for diff endpoint
517+
* @returns {string}
518+
*/
519+
ConnectionConfig.prototype.applyURL = function () {
520+
const purl = this.branchBase('apply');
521+
return purl;
522+
};
523+
515524
/**
516525
* Generate URL for fetch endpoint
517526
* @param {string} remoteName

lib/woqlClient.js

Lines changed: 103 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,15 +1468,22 @@ WOQLClient.prototype.userOrganizations = function (orgList) {
14681468
* Get the patch of difference between two documents.
14691469
* @param {object} before - The current state of JSON document
14701470
* @param {object} after - The updated state of JSON document
1471-
* @param {object} options [{}] - Options to send to the diff endpoint
1471+
* @param {object} [options] - {keep:{}} Options to send to the diff endpoint
1472+
* the diff api outputs the changes between the input (JSON Object),
1473+
* but you can list the properties that you would like to see in the diff result in any case.
14721474
* @returns {Promise} A promise that returns the call response object, or an Error if rejected.
14731475
* @example
1474-
* const diff = await client.getDiff(
1476+
* client.getDiff(
14751477
* { "@id": "Person/Jane", "@type": "Person", name: "Jane" },
14761478
* { "@id": "Person/Jane", "@type": "Person", name: "Janine" }
1477-
* );
1478-
*/
1479-
WOQLClient.prototype.getDiff = function (before, after, options) {
1479+
* ).then(diffResult=>{
1480+
* console.log(diffResult)
1481+
* })
1482+
* //result example
1483+
* //{'@id': 'Person/Jane',
1484+
* // name: { '@after': 'Janine', '@before': 'Jane', '@op': 'SwapValue' }}
1485+
*/
1486+
WOQLClient.prototype.getJSONDiff = function (before, after, options) {
14801487
if (typeof before !== 'object' || typeof after !== 'object') {
14811488
const errmsg = '"before" or "after" parameter error - you must specify a valid before or after json document';
14821489

@@ -1497,29 +1504,35 @@ WOQLClient.prototype.getDiff = function (before, after, options) {
14971504
/**
14981505
* Get the patch of difference between two documents.
14991506
* @param {string} id - The object id to be diffed
1500-
* @param {string} beforeVersion - The version from which to compare the object
1501-
* @param {object} after - The updated state of JSON document
1502-
* @param {object} options [{}] - Options to send to the diff endpoint
1507+
* @param {string} before - The version from which to compare the object
1508+
* @param {object} afterVersion - The updated state of JSON document
1509+
* @param {object} [options] - {keep:{}} Options to send to the diff endpoint
1510+
* the diff api outputs the changes between the input (branches or commits),
1511+
* but you can list the properties that you would like to see in the diff result in any case.
15031512
* @returns {Promise} A promise that returns the call response object, or an Error if rejected.
15041513
* @example
1505-
* const diff = await client.getVersionObjectDiff(
1506-
* "Person/Jane",
1507-
* "branch:a73ssscfx0kke7z76083cgswszdxy6l",
1508-
* { "@id": "Person/Jane", "@type": "Person", name: "Janine" }
1509-
* );
1514+
* const jsonObj = { "@id": "Person/Jane", "@type": "Person", name: "Janine" }
1515+
* client.getVersionObjectDiff(jsonObj, "a73ssscfx0kke7z76083cgswszdxy6l").then(diffResp=>{
1516+
* console.log(diffResp)
1517+
* })
15101518
*/
1511-
WOQLClient.prototype.getVersionObjectDiff = function (id, beforeVersion, after, options) {
1519+
WOQLClient.prototype.getVersionObjectDiff = function (jsonObject, dataVersion, id, options) {
15121520
if (typeof id !== 'string' || typeof beforeVersion !== 'string' || typeof after !== 'object') {
15131521
const errmsg = '"id", "beforeVersion" or "after" parameter error - you must specify a valid after json document and valid id and version';
15141522

15151523
return Promise.reject(
15161524
new Error(ErrorMessage.getInvalidParameterMessage(CONST.GET_DIFF, errmsg)),
15171525
);
15181526
}
1519-
const opt = (typeof options === 'undefined') ? {} : options;
1527+
const opt = options || {};
15201528
const payload = {
1521-
document_id: id, before_data_version: beforeVersion, after, ...opt,
1529+
before: jsonObject,
1530+
after_data_version: dataVersion,
1531+
...opt,
15221532
};
1533+
if (id) {
1534+
payload.id = id;
1535+
}
15231536
return this.dispatch(
15241537
CONST.POST,
15251538
this.connectionConfig.diffURL(),
@@ -1528,41 +1541,100 @@ WOQLClient.prototype.getVersionObjectDiff = function (id, beforeVersion, after,
15281541
};
15291542

15301543
/**
1531-
* Get the patch of difference between two documents.
1532-
* @param {string} id - The object id to be diffed
1533-
* @param {string} beforeVersion - The version from which to compare the object
1534-
* @param {string} afterVersion - The version to which to compare the object
1535-
* @param {object} options [{}] - Options to send to the diff endpoint
1544+
* Get the patch of difference between branches or commits.
1545+
* @param {string} beforeVersion - Before branch/commit to compare
1546+
* @param {string} afterVersion - Before branch/commit to compare
1547+
* @param {string} [id] - The object id to be diffed,
1548+
* if it is omitted all the documents will be compared
1549+
* @param {object} [options] - {keep:{}} Options to send to the diff endpoint
1550+
* the diff api outputs the changes between the input (branches or commits),
1551+
* but you can list the properties that you would like to see in the diff result in any case.
15361552
* @returns {Promise} A promise that returns the call response object, or an Error if rejected.
15371553
* @example
1538-
* const diff = await client.getVersionDiff(
1539-
* "Person/Jane",
1540-
* "branch:a73ssscfx0kke7z76083cgswszdxy6l",
1541-
* "branch:73rqpooz65kbsheuno5dsayh71x7wf4"
1542-
* );
1554+
* //This is to view all the changes between two commits
1555+
* const beforeCommit = "a73ssscfx0kke7z76083cgswszdxy6l"
1556+
* const afterCommit = "73rqpooz65kbsheuno5dsayh71x7wf4"
1557+
*
1558+
* client.getVersionDiff( beforeCommit, afterCommit).then(diffResult=>{
1559+
* console.log(diffResult)
1560+
* })
1561+
*
1562+
* //This is to view the changes between two commits but only for the given document
1563+
* client.getVersionDiff( beforeCommit, afterCommit, "Person/Tom").then(diffResult=>{
1564+
* console.log(diffResult)
1565+
* })
1566+
*
1567+
* //This is to view the changes between a branch (head) and a commit for the given document
1568+
* client.getVersionDiff("main", afterCommit, "Person/Tom" ).then(diffResult=>{
1569+
* console.log(diffResult)
1570+
* })
1571+
*
1572+
* //This is to view the changes between two branches with the keep options
1573+
* const options = {"keep":{"@id":true, "name": true}}
1574+
* client.getVersionDiff("main","mybranch",options).then(diffResult=>{
1575+
* console.log(diffResult)
1576+
* })
15431577
*/
1544-
WOQLClient.prototype.getVersionDiff = function (id, beforeVersion, afterVersion, options) {
1545-
if (typeof id !== 'string' || typeof beforeVersion !== 'string' || typeof afterVersion !== 'string') {
1546-
const errmsg = '"id", "beforeVersion" or "after" parameter error - you must specify a valid after json document and valid id and version';
1578+
1579+
WOQLClient.prototype.getVersionDiff = function (beforeVersion, afterVersion, id, options) {
1580+
if (typeof beforeVersion !== 'string' || typeof afterVersion !== 'string') {
1581+
const errmsg = 'Error, you have to provide a beforeVersion and afterVersion input';
15471582

15481583
return Promise.reject(
15491584
new Error(ErrorMessage.getInvalidParameterMessage(CONST.GET_DIFF, errmsg)),
15501585
);
15511586
}
1552-
const opt = (typeof options === 'undefined') ? {} : options;
1587+
const opt = options || {};
15531588
const payload = {
1554-
document_id: id,
15551589
before_data_version: beforeVersion,
15561590
after_data_version: afterVersion,
15571591
...opt,
15581592
};
1593+
if (id) {
1594+
payload.document_id = id;
1595+
}
15591596
return this.dispatch(
15601597
CONST.POST,
15611598
this.connectionConfig.diffURL(),
15621599
payload,
15631600
).then((response) => response);
15641601
};
15651602

1603+
/**
1604+
* Diff two different commits and apply changes on the current branch/commit
1605+
* if you would like to change branch or commit before apply use client.checkout("branchName")
1606+
* @param {string} beforeVersion - Before branch/commit to compare
1607+
* @param {string} afterVersion - Before branch/commit to compare
1608+
* @param {string} message - apply commit message
1609+
* @param {boolean} [match_final_state] - the deafult value is false
1610+
* @param {object} [options] - {keep:{}} Options to send to the apply endpoint
1611+
* @example
1612+
* client.checkout("mybranch")
1613+
* client.apply("main","mybranch").then(result=>{
1614+
* console.log(result)
1615+
* })
1616+
*/
1617+
1618+
// eslint-disable-next-line max-len
1619+
WOQLClient.prototype.apply = function (beforeVersion, afterVersion, message, matchFinalState, options) {
1620+
const opt = options || {};
1621+
const commitMsg = this.generateCommitInfo(message);
1622+
const payload = {
1623+
before_commit: beforeVersion,
1624+
after_commit: afterVersion,
1625+
...commitMsg,
1626+
...opt,
1627+
};
1628+
if (matchFinalState) {
1629+
payload.match_final_state = matchFinalState;
1630+
}
1631+
return this.dispatch(
1632+
CONST.POST,
1633+
this.connectionConfig.applyURL(),
1634+
payload,
1635+
).then((response) => response);
1636+
};
1637+
15661638
/**
15671639
* Patch the difference between two documents.
15681640
* @param {object} before - The current state of JSON document

0 commit comments

Comments
 (0)