Skip to content

Commit

Permalink
Fix: Nested fields were not changing cases while making API call and …
Browse files Browse the repository at this point in the history
…receiving the response.

fixes eversign#67
  • Loading branch information
rasulovdev committed Sep 9, 2022
1 parent 1ab0618 commit 681bbb9
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 17 deletions.
5 changes: 0 additions & 5 deletions lib/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,21 +301,18 @@ function Document(newDocument = {}) {

this.getFiles = function () {
return document.files.map(function (file) {
file = _.mapKeys(file, function(v, k ){ return _.camelCase(k); });
return file instanceof File ? file : new File(file);
});
};

this.getRecipients = function () {
return document.recipients.map(function (recip) {
recip = _.mapKeys(recip, function(v, k ){ return _.camelCase(k); });
return recip instanceof Recipient ? recip : new Recipient(recip);
});
};

this.getSigners = function () {
return document.signers.map(function (signer) {
signer = _.mapKeys(signer, function(v, k ){ return _.camelCase(k); });
return signer instanceof Signer ? signer : new Signer(signer);
});
};
Expand Down Expand Up @@ -477,7 +474,6 @@ function Document(newDocument = {}) {

this.getLog = function () {
return document.log.map(function (log) {
log = _.mapKeys(log, function(v, k ){ return _.camelCase(k); });
return log instanceof LogEntry ? log : new LogEntry(log);
});
};
Expand All @@ -497,7 +493,6 @@ function Document(newDocument = {}) {
this.getFields = function () {
return document.fields.map(function (fieldsPerFile) {
return fieldsPerFile.map(function (field) {
field = _.mapKeys(field, function(v, k ){ return _.camelCase(k); });
return field instanceof FormField ? field : new FormField(field);
});
//return field instanceof FormField ? field : new FormField(field);
Expand Down
1 change: 0 additions & 1 deletion lib/DocumentTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ function Template(templateId) {

this.getSigners = function () {
return template.signers.map(function (signer) {
signer = _.mapKeys(signer, function(v, k ){ return _.camelCase(k); });
return signer instanceof Signer ? signer : new Signer(signer);
});
};
Expand Down
13 changes: 5 additions & 8 deletions lib/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var Document = require('./Document.js');
var Business = require('./Business.js');
var File = require('./File.js');
var DocumentTemplate = require('./DocumentTemplate.js');
var utils = require('./utils');

module.exports = ApiRequest;

Expand Down Expand Up @@ -91,9 +92,7 @@ ApiRequest.prototype.requestOAuthToken = function requestOAuthToken(token_reques
var self = this;

token_request = token_request.toObject();
token_request = _.mapKeys(token_request, function(value, key) {
return _.snakeCase(key);
});
token_request = utils.mapKeysDeep(token_request, _.snakeCase);

var requestOptions = {
method: 'POST',
Expand Down Expand Up @@ -124,9 +123,7 @@ ApiRequest.prototype.requestOAuthToken = function requestOAuthToken(token_reques
ApiRequest.prototype.startRequest = function(){
var self = this;

this.payload = _.mapKeys(this.payload, function(value, key) {
return _.snakeCase(key);
});
this.payload = utils.mapKeysDeep(this.payload, _.snakeCase)

var requestOptions = {
method: this.httpType,
Expand All @@ -149,13 +146,13 @@ ApiRequest.prototype.startRequest = function(){
if(res.length){
//is an array
var result = res.map(function (obj) {
obj = _.mapKeys(obj, function(v, k ){ return _.camelCase(k); });
obj = utils.mapKeysDeep(obj, _.camelCase);
return new Type(obj);
})
return resolve(result);
}else{
//Not an array, or empty
res = _.mapKeys(res, function(v, k ){ return _.camelCase(k); });
res = utils.mapKeysDeep(res, _.camelCase);
return resolve(new Type(res));
}
} else {
Expand Down
16 changes: 16 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var _ = require('lodash');

/**
* Replace given object keys recursively
* @param {Object} object Object to change keys of
* @param {Function} transformer A callback which receives a key
* @returns Copy of the object with `transformer` applied to each key
*/
function mapKeysDeep(object, transformer) {
return _.transform(object, function (result, value, key) {
if (_.isFunction(value)) return;
result[transformer(key)] = _.isObject(value) ? mapKeysDeep(value, transformer) : value;
});
}

module.exports = { mapKeysDeep };
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ declare module 'eversign/lib/Signer' {
getStatus(): Signer.STATUS
getViewed(): boolean
setDeclined(newDeclined: boolean): void
setDeliverEmail(newDeliverEmail: string): void
setDeliverEmail(newDeliverEmail: boolean): void
setEmail(newEmail: string): void
setEmbeddedSigningUrl(newEmbeddedSigningUrl: string): void
setId(newId: number): void
Expand Down

0 comments on commit 681bbb9

Please sign in to comment.