From 681bbb9daf17dc670e994c48af4acc9ac16d318e Mon Sep 17 00:00:00 2001 From: Azamat Rasulov Date: Fri, 9 Sep 2022 17:48:17 +0500 Subject: [PATCH] Fix: Nested fields were not changing cases while making API call and receiving the response. fixes #67 --- lib/Document.js | 5 ----- lib/DocumentTemplate.js | 1 - lib/Request.js | 13 +++++-------- lib/utils.js | 16 ++++++++++++++++ package-lock.json | 4 ++-- typings/index.d.ts | 2 +- 6 files changed, 24 insertions(+), 17 deletions(-) create mode 100644 lib/utils.js diff --git a/lib/Document.js b/lib/Document.js index 18fcd72..8496a00 100755 --- a/lib/Document.js +++ b/lib/Document.js @@ -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); }); }; @@ -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); }); }; @@ -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); diff --git a/lib/DocumentTemplate.js b/lib/DocumentTemplate.js index e698a73..d55c3d6 100755 --- a/lib/DocumentTemplate.js +++ b/lib/DocumentTemplate.js @@ -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); }); }; diff --git a/lib/Request.js b/lib/Request.js index e8a56c5..6e0ce21 100755 --- a/lib/Request.js +++ b/lib/Request.js @@ -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; @@ -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', @@ -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, @@ -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 { diff --git a/lib/utils.js b/lib/utils.js new file mode 100644 index 0000000..e772dd3 --- /dev/null +++ b/lib/utils.js @@ -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 }; diff --git a/package-lock.json b/package-lock.json index 9cd3f2d..2260344 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "eversign", - "version": "1.9.6", + "version": "1.9.7", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "eversign", - "version": "1.9.6", + "version": "1.9.7", "license": "MIT", "dependencies": { "bluebird": "^3.7.2", diff --git a/typings/index.d.ts b/typings/index.d.ts index 3d4879a..8a9884d 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -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