Comparing version 1.3.0 to 1.3.1
## Change Log | ||
### v1.3.1 (2016/04/01) | ||
- [#130](https://github.com/SparkPost/node-sparkpost/pull/130) Refactored toApiFormat.js to use json-pointer (@orval) | ||
### v1.3.0 (2016/04/01) | ||
@@ -4,0 +7,0 @@ - [#129](https://github.com/SparkPost/node-sparkpost/pull/129) Added support for subaccounts (@coldacid) |
'use strict'; | ||
var _ = require('lodash'); | ||
var _ = require('lodash') | ||
, pointer = require('json-pointer'); | ||
var excludeList = [ | ||
'/substitution_data', | ||
'/tags', | ||
'/metadata', | ||
'/attributes', | ||
'/headers', | ||
'/content/email_rfc822' | ||
]; | ||
function snakedKeyClone(source) { | ||
if (!_.isObject(source)) { | ||
return source; | ||
} | ||
var target = {}; | ||
if (Array.isArray(source)) { | ||
target = []; | ||
for (var i = 0; i < source.length; i++) { | ||
target.push(snakedKeyClone(source[i])); | ||
} | ||
return target; | ||
} | ||
Object.keys(source).forEach(function(key) { | ||
target[_.snakeCase(key)] = snakedKeyClone(source[key]); | ||
}); | ||
return target; | ||
} | ||
module.exports = function toApiFormat(source) { | ||
var dest = {}; | ||
// List of property names which we do not want to modify the sub-property names | ||
var excludeList = ['substitution_data', 'tags', 'metadata', 'attributes', 'headers']; | ||
var excludedObjects = {}; | ||
try{ | ||
// Handle arrays (Only need to handle arrays of objects for our use case.) | ||
if(Array.isArray(source) && _.isPlainObject(source[0])) { | ||
dest = []; | ||
for(var i = 0; i < source.length; i++) { | ||
dest.push(toApiFormat(source[i])); | ||
} | ||
return dest; | ||
// Look in the source object for the excluded pointers and take a copy of the | ||
// objects pointed to by those keys. Then remove them from the source object. | ||
excludeList.forEach(function(exclusionPointer) { | ||
if (pointer.has(source, exclusionPointer)) { | ||
pointer.set(excludedObjects, exclusionPointer, pointer.get(source, exclusionPointer)); | ||
pointer.remove(source, exclusionPointer); | ||
} | ||
}); | ||
// Handle objects | ||
Object.keys(source).forEach(function(key) { | ||
// Make a clone of the remaining source object but with snaked case keys | ||
var target = snakedKeyClone(source); | ||
// Cache snake_cased keys | ||
var snakedKey = _.snakeCase(key); | ||
// Reinstated the un-modified objects into the target | ||
pointer.walk(excludedObjects, function(val, ptr) { | ||
pointer.set(target, ptr, val); | ||
}); | ||
// Exclude appropriately | ||
if( -1 === excludeList.indexOf(key)) { | ||
dest[snakedKey] = source[key]; | ||
if( _.isObject (source[key] ) ) { | ||
dest[snakedKey] = toApiFormat(source[key]); | ||
} | ||
} else { | ||
dest[snakedKey] = source[key]; | ||
} | ||
}); | ||
} catch(e) { | ||
// Errors | ||
return e; | ||
} | ||
// No errors return results | ||
return dest; | ||
return target; | ||
}; |
{ | ||
"name": "sparkpost", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"description": "A Node.js wrapper for interfacing with your favorite SparkPost APIs", | ||
@@ -43,2 +43,3 @@ "main": "./lib/sparkpost.js", | ||
"dependencies": { | ||
"json-pointer": "^0.5.0", | ||
"lodash": "^3.9.3", | ||
@@ -45,0 +46,0 @@ "request": "2.42.0" |
@@ -27,2 +27,5 @@ 'use strict'; | ||
] | ||
, content: { | ||
email_rfc822: 'a message' | ||
} | ||
, fizzArr: [ | ||
@@ -57,2 +60,5 @@ { | ||
] | ||
, content: { | ||
email_rfc822: 'a message' | ||
} | ||
, fizz_arr: [ | ||
@@ -59,0 +65,0 @@ { |
@@ -111,3 +111,18 @@ var chai = require('chai') | ||
}); | ||
it('should leave email_rfc822 content keys intact', function(done) { | ||
var options = { | ||
transmissionBody: { | ||
content: { | ||
email_rfc822: 'Content-Type: text/plain\nFrom: From Envelope <from@example.com>\nSubject: Example Email\n\nHello World' | ||
} | ||
} | ||
}; | ||
transmission.send(options, function(err, data) { | ||
expect(client.post.firstCall.args[0].json.content).to.have.property('email_rfc822'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
272920
124
4496
3
+ Addedjson-pointer@^0.5.0
+ Addedforeach@2.0.6(transitive)
+ Addedjson-pointer@0.5.0(transitive)