Skip to content

Commit

Permalink
Default xml2jsOptions set explicitArray to false.
Browse files Browse the repository at this point in the history
Fixes #63
  • Loading branch information
Dustin McQuay committed Apr 22, 2016
1 parent 50bb882 commit a49bb23
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,35 @@ Example Response:
```javascript
{
ItemSearchResponse: {
$: {
xmlns: 'http://webservices.amazon.com/AWSECommerceService/2011-08-01'
},
OperationRequest: [Object],
Items: [Object]
}
}
```

NOTE: In v2.0.0, we changed the default for xml2js to set explicitArray to false. Before v.2.0.0, you would get a
response like this instead (note the extra arrays you have to drill into):

```javascript
{
ItemSearchResponse: {
OperationRequest: [ [Object] ],
Items: [ [Object] ]
}
}
```

You can change back to the old behavior by setting explitArray to true like this:

```javascript
var opHelper = new OperationHelper({
awsId: '[YOUR AWS ID HERE]',
awsSecret: '[YOUR AWS SECRET HERE]',
assocId: '[YOUR ASSOCIATE TAG HERE]',
xml2jsOptions: { explicitArray: true }
});
```

## API Documentation

Because we don't define any specific operations, we also don't document them. What a waste
Expand Down
6 changes: 5 additions & 1 deletion lib/operation-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const locale = require('./locale')
const http = require('http')
const xml2js = require('xml2js')

const defaultXml2JsOptions = {
explicitArray: false
}

class OperationHelper {
constructor(params) {
params = params || {}
Expand All @@ -28,7 +32,7 @@ class OperationHelper {
this.assocId = params.assocId
this.endPoint = params.endPoint || locale.getEndpointForLocale(params.locale)
this.baseUri = params.baseUri || OperationHelper.defaultBaseUri
this.xml2jsOptions = params.xml2jsOptions || {}
this.xml2jsOptions = Object.assign({}, defaultXml2JsOptions, params.xml2jsOptions)
this.throttler = new Throttler(params.maxRequestsPerSecond)

// set version
Expand Down
9 changes: 5 additions & 4 deletions lib/operation-helper.specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ describe('OperationHelper', function () {
let requestMock, responseMock, result, outputResponseBody
const responseBody = '<it><is><some>xml</some></is></it>'
const xml2jsOptions = {foo: 'bar'}
const expectedXml2jsOptions = Object.assign({explicitArray: false}, xml2jsOptions)

context('happy path', () => {
let opHelper
Expand Down Expand Up @@ -196,15 +197,15 @@ describe('OperationHelper', function () {
})

it('should pass the xml2jsOptions to xml2js', () => {
expect(xml2js.parseString.firstCall.args[1]).to.eql(xml2jsOptions)
expect(xml2js.parseString.firstCall.args[1]).to.eql(expectedXml2jsOptions)
})

it('should parse XML and return result as object', () => {
expect(result).to.eql({
it: {
is: [{
some: ['xml']
}]
is: {
some: 'xml'
}
}
})
})
Expand Down

0 comments on commit a49bb23

Please sign in to comment.