Skip to content

Commit

Permalink
Revert the faulty behavior of honoring flip
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellmorten committed Sep 15, 2023
1 parent 188fdce commit 4b7f01f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,12 @@ Available options:

The package also includes a transformer, that works exactly like the adapter,
except it is intended for use in mutation pipelines with
`{ $transform: 'xml' }`. Also, the direction will be reversed when used within a
flipped mutation object.
`{ $transform: 'xml' }`.

**Note:** The XML transformer will not be affected by flipping a mutation
object, like some other transformers. This is because it's unlikely that we want
the XML to be stringified from a service and parsed to a service. We'll probably
provide a reversed transformer for those rare cases at some point.

Example of use:

Expand Down
43 changes: 22 additions & 21 deletions src/transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,28 +147,31 @@ test('should parse soap from service, keeping envelope', (t) => {
t.deepEqual(ret, expected)
})

test('should stringify xml from service when flipped', (t) => {
test('should not be affected by a flipped mutation object coming from a service', (t) => {
const stateFlipped = {...state, flip: true}
const data = {
'env:Envelope': {
'env:Body': {
const data = xmlData
const expected = {
'soap:Envelope': {
'soap:Body': {
GetPaymentMethodsResponse: {
GetPaymentMethodsResult: {
PaymentMethod: [
{ '@Id': '1', Name: { $value: 'Cash' }, Description: null },
{ '@Id': '2', Name: { $value: 'Invoice' }, Description: '' },
{
'@Id': '2',
Name: { $value: 'Invoice' },
Description: { $value: '' },
},
],
DontInclude: undefined,
},
},
},
},
}
const expected = xmlData

const ret = transformer({ namespaces })(options)(data, stateFlipped)
const ret = transformer({})(options)(data, stateFlipped)

t.is(ret, expected)
t.deepEqual(ret, expected)
})

// Tests -- to service
Expand Down Expand Up @@ -305,29 +308,27 @@ test('should use provided soap version to service with envelope', (t) => {
t.is(ret, expected)
})

test('should parse xml to service when flipped', (t) => {
test('should not be affected by a flipped mutation object going to a service', (t) => {
const stateFlipped = {...stateRev, flip: true}
const data = xmlData
const expected = {
'soap:Envelope': {
'soap:Body': {
const data = {
'env:Envelope': {
'env:Body': {
GetPaymentMethodsResponse: {
GetPaymentMethodsResult: {
PaymentMethod: [
{ '@Id': '1', Name: { $value: 'Cash' }, Description: null },
{
'@Id': '2',
Name: { $value: 'Invoice' },
Description: { $value: '' },
},
{ '@Id': '2', Name: { $value: 'Invoice' }, Description: '' },
],
DontInclude: undefined,
},
},
},
},
}
const expected = xmlData

const ret = transformer({})(options)(data, stateFlipped)
const ret = transformer({ namespaces })(options)(data, stateFlipped)

t.deepEqual(ret, expected)
t.is(ret, expected)
})

3 changes: 1 addition & 2 deletions src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ const optionsFromProps = (props: Props) => ({
const transformer: Transformer = (props: Props) => () =>
function xml(data, state) {
const options = optionsFromProps(props)
const isRev = state.flip ? !state.rev : state.rev // Honor flip
if (isRev) {
if (state.rev) {
const { data: serialized } = stringify(data, options)
return serialized
} else {
Expand Down

0 comments on commit 4b7f01f

Please sign in to comment.