Skip to content

Commit

Permalink
feat: make xmlBuilderOptions configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
dantio committed Jul 1, 2024
1 parent f265384 commit ce7f787
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 18 deletions.
16 changes: 8 additions & 8 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"dependencies": {
"axios": "^1.6.8",
"debug": "^4.3.4",
"fast-xml-parser": "^4.2.5",
"fast-xml-parser": "^4.4.0",
"qs": "^6.11.0"
},
"devDependencies": {
Expand Down
18 changes: 11 additions & 7 deletions src/api/traditional/XMLRequest.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import debug from 'debug';
import {XMLBuilder, XMLParser} from 'fast-xml-parser';
import {X2jOptions, XMLBuilder, XmlBuilderOptions, XMLParser} from 'fast-xml-parser';
import {checkEBayTraditionalResponse, EBayNoCallError} from '../../errors/index.js';
import {IEBayApiRequest} from '../../request.js';
import {ApiRequestConfig, Headers} from '../../types/index.js';
import {Fields} from './fields.js';

const log = debug('ebay:xml:request');

export const defaultJSON2XMLOptions = {
export const defaultXmlBuilderOptions = {
attributeNamePrefix: '@_',
textNodeName: '#value',
ignoreAttributes: false,
Expand Down Expand Up @@ -42,7 +42,8 @@ export type BodyHeaders = {

export type TraditionalApiConfig = {
raw?: boolean,
parseOptions?: object,
parseOptions?: X2jOptions,
xmlBuilderOptions?: XmlBuilderOptions,
useIaf?: boolean,
sign?: boolean,
hook?: (xml: string) => BodyHeaders
Expand All @@ -58,6 +59,7 @@ export type XMLReqConfig = TraditionalApiConfig & {
export const defaultApiConfig: Required<Omit<TraditionalApiConfig, 'hook'>> = {
raw: false,
parseOptions: defaultXML2JSONParseOptions,
xmlBuilderOptions: defaultXmlBuilderOptions,
useIaf: true,
sign: false,
headers: {},
Expand All @@ -77,7 +79,7 @@ export default class XMLRequest {
private readonly config: XMLReqConfig;
private readonly req: IEBayApiRequest;

public static j2x = new XMLBuilder(defaultJSON2XMLOptions);
public readonly j2x;

/**
* Creates the new Request object
Expand All @@ -93,9 +95,11 @@ export default class XMLRequest {
throw new EBayNoCallError();
}

this.config = {...defaultApiConfig, ...config};
this.j2x = new XMLBuilder({...defaultXmlBuilderOptions, ...this.config.xmlBuilderOptions });
this.callName = callName;
this.fields = fields || {};
this.config = {...defaultApiConfig, ...config};

this.req = req;
}

Expand Down Expand Up @@ -123,7 +127,7 @@ export default class XMLRequest {
} : {};
}

private getParseOptions() {
private getParseOptions() : X2jOptions {
return {
...defaultXML2JSONParseOptions,
...this.config.parseOptions
Expand Down Expand Up @@ -158,7 +162,7 @@ export default class XMLRequest {
*/
public toXML(fields: Fields) {
const HEADING = '<?xml version="1.0" encoding="utf-8"?>';
return HEADING + XMLRequest.j2x.build({
return HEADING + this.j2x.build({
[this.callName + 'Request']: {
'@_xmlns': this.config.xmlns,
...this.getCredentials(),
Expand Down
47 changes: 45 additions & 2 deletions test/api/traditional/xmlRequest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,17 @@ describe('XMLRequestTest', () => {
});
});

it('Handles tag names that ends with Array', () => {
it('Handles tag names that ends with Array in response', () => {
const xml = `<?xml version="1.0" encoding="utf-8"?>
<CALLResponse xmlns="urn:ebay:apis:eBLBaseComponents">
<ActiveList>
<ItemArray>
<Item id="2">
<price currency="EUR">2.0</price>
</Item>
<Item id="3">
<price currency="EUR">2.0</price>
</Item>
</ItemArray>
</ActiveList>
</CALLResponse>`;
Expand All @@ -168,13 +171,53 @@ describe('XMLRequestTest', () => {
currency: 'EUR',
value: 2.0
}
}]
}, {
id: 3,
price: {
currency: 'EUR',
value: 2.0
}
}]
}
}
}).to.deep.equal(result);
});
});

it('Handles tag names that ends with Array in request', () => {
const xml = `<?xml version="1.0" encoding="utf-8"?>
<CALLRequest xmlns="xmlns">
<RequesterCredentials><eBayAuthToken>eBayAuthToken</eBayAuthToken></RequesterCredentials>
<UserDeliveryPreferenceArray>
<NotificationEnable>
<EventType>ItemListed</EventType>
<EventEnable>Enable</EventEnable>
</NotificationEnable>
<NotificationEnable>
<EventType>ItemSold</EventType>
<EventEnable>Enable</EventEnable>
</NotificationEnable>
</UserDeliveryPreferenceArray>
</CALLRequest>`.replace(/>\s+</g, '><');
req.post = sinon.stub().returnsArg(1)
const request = new XMLRequest('CALL', {
UserDeliveryPreferenceArray: [{
NotificationEnable: {
EventType: 'ItemListed',
EventEnable: 'Enable',
}
}, {
NotificationEnable: {
EventType: 'ItemSold',
EventEnable: 'Enable',
},
}],
}, {...config, returnResponse: true, xmlBuilderOptions: { oneListGroup: true }}, req);
return request.request().then(result => {
expect(result).to.equal(xml);
});
});

describe('request', () => {
it('call custom body function and headers', () => {
const post = sinon.stub().returns(Promise.resolve({data: apiResponse}));
Expand Down

0 comments on commit ce7f787

Please sign in to comment.