ebay-api 9.0.2 → 9.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -22,7 +22,7 @@ It supports `client credentials grant` and `authorization code grant` \(Auth'N'A
22
22
 
23
23
  ## Changelog
24
24
 
25
- * `v8.7.2-RC.0` is the latest release.
25
+ * `v9.1.0` is the latest release.
26
26
  * See [here](https://github.com/hendt/ebay-api/blob/master/CHANGELOG.md) for the full changelog.
27
27
 
28
28
  ## Implementation status
@@ -497,6 +497,45 @@ try {
497
497
  console.error(error);
498
498
  }
499
499
  ```
500
+ ## Handling errors
501
+ ```js
502
+ import eBayApi from 'ebay-api';
503
+ import { EBayApiError } from 'ebay-api/lib/errors';
504
+
505
+ const eBay = new eBayApi(/* { your config here } */);
506
+
507
+ try {
508
+ const result = await eBay.trading.GetItem({
509
+ ItemID: 'itemId',
510
+ });
511
+ console.log(result);
512
+ } catch (error) {
513
+ if (error instanceof EBayApiError && error.errorCode === 17) {
514
+ // Item not found
515
+ } else {
516
+ throw error;
517
+ }
518
+
519
+ // in error there is also the field "meta" with the response
520
+ if (error instanceof EBayApiError && error.meta?.res?.status === 404) {
521
+ // not found
522
+
523
+ // The first error
524
+ console.log(error?.firstError);
525
+ }
526
+
527
+
528
+ }
529
+ ```
530
+
531
+ The `errorCode` is extracted from the first error in the API response.
532
+
533
+ * [Shopping API Error Codes](https://developer.ebay.com/devzone/shopping/docs/callref/Errors/ErrorMessages.html)
534
+ * [Trading API Error Codes](https://developer.ebay.com/devzone/xml/docs/reference/ebay/errors/errormessages.htm)
535
+ * [RESTful Error Codes](https://developer.ebay.com/devzone/xml/docs/reference/ebay/errors/errormessages.htm)
536
+ * [PostOrder Error Codes](https://developer.ebay.com/Devzone/post-order/ErrorMessages.html#ErrorsByNumber)
537
+
538
+
500
539
 
501
540
  ## Controlling Traditional XML request and response
502
541
 
@@ -505,7 +544,8 @@ The second parameter in the traditional API has the following options:
505
544
  ```typescript
506
545
  export type Options = {
507
546
  raw?: boolean // return raw XML
508
- parseOptions?: object // https://github.com/NaturalIntelligence/fast-xml-parser
547
+ parseOptions?: X2jOptions // https://github.com/NaturalIntelligence/fast-xml-parser
548
+ xmlBuilderOptions?: XmlBuilderOptions // https://github.com/NaturalIntelligence/fast-xml-parser
509
549
  useIaf?: boolean // use IAF in header instead of Bearer
510
550
  headers?: Headers // additional Headers (key, value)
511
551
  hook?: (xml) => BodyHeaders // hook into the request to modify the body and headers
@@ -515,6 +555,40 @@ export type Options = {
515
555
  [Fast XML](https://github.com/NaturalIntelligence/fast-xml-parser) is used to parse the XML. You can pass the parse
516
556
  option to `parseOptions` parameter.
517
557
 
558
+ ### Parse JSON Array
559
+ ```js
560
+
561
+ eBay.trading.SetNotificationPreferences({
562
+ UserDeliveryPreferenceArray: [{
563
+ NotificationEnable: {
564
+ EventType: 'ItemListed',
565
+ EventEnable: 'Enable',
566
+ }
567
+ }, {
568
+ NotificationEnable: {
569
+ EventType: 'ItemSold',
570
+ EventEnable: 'Enable',
571
+ },
572
+ }],
573
+ }, { xmlBuilderOptions: { oneListGroup: true }})
574
+ ```
575
+
576
+ Will produce:
577
+ ```xml
578
+ <UserDeliveryPreferenceArray>
579
+ <NotificationEnable>
580
+ <EventType>ItemListed</EventType>
581
+ <EventEnable>Enable</EventEnable>
582
+ </NotificationEnable>
583
+ <NotificationEnable>
584
+ <EventType>ItemSold</EventType>
585
+ <EventEnable>Enable</EventEnable>
586
+ </NotificationEnable>
587
+ </UserDeliveryPreferenceArray>
588
+ ```
589
+
590
+
591
+
518
592
  ## Examples
519
593
 
520
594
  ### Trading - AddFixedPriceItem \(CDATA\)
@@ -1,8 +1,8 @@
1
- import { XMLBuilder } from 'fast-xml-parser';
1
+ import { X2jOptions, XMLBuilder, XmlBuilderOptions } from 'fast-xml-parser';
2
2
  import { IEBayApiRequest } from '../../request.js';
3
3
  import { ApiRequestConfig, Headers } from '../../types/index.js';
4
4
  import { Fields } from './fields.js';
5
- export declare const defaultJSON2XMLOptions: {
5
+ export declare const defaultXmlBuilderOptions: {
6
6
  attributeNamePrefix: string;
7
7
  textNodeName: string;
8
8
  ignoreAttributes: boolean;
@@ -32,7 +32,8 @@ export type BodyHeaders = {
32
32
  };
33
33
  export type TraditionalApiConfig = {
34
34
  raw?: boolean;
35
- parseOptions?: object;
35
+ parseOptions?: X2jOptions;
36
+ xmlBuilderOptions?: XmlBuilderOptions;
36
37
  useIaf?: boolean;
37
38
  sign?: boolean;
38
39
  hook?: (xml: string) => BodyHeaders;
@@ -52,7 +53,7 @@ export default class XMLRequest {
52
53
  private readonly fields;
53
54
  private readonly config;
54
55
  private readonly req;
55
- static j2x: XMLBuilder;
56
+ readonly j2x: XMLBuilder;
56
57
  constructor(callName: string, fields: Fields | null, config: XMLReqConfig, req: IEBayApiRequest);
57
58
  private getResponseWrapper;
58
59
  private getCredentials;
@@ -2,7 +2,7 @@ import debug from 'debug';
2
2
  import { XMLBuilder, XMLParser } from 'fast-xml-parser';
3
3
  import { checkEBayTraditionalResponse, EBayNoCallError } from '../../errors/index.js';
4
4
  const log = debug('ebay:xml:request');
5
- export const defaultJSON2XMLOptions = {
5
+ export const defaultXmlBuilderOptions = {
6
6
  attributeNamePrefix: '@_',
7
7
  textNodeName: '#value',
8
8
  ignoreAttributes: false,
@@ -31,6 +31,7 @@ export const defaultXML2JSONParseOptions = {
31
31
  export const defaultApiConfig = {
32
32
  raw: false,
33
33
  parseOptions: defaultXML2JSONParseOptions,
34
+ xmlBuilderOptions: defaultXmlBuilderOptions,
34
35
  useIaf: true,
35
36
  sign: false,
36
37
  headers: {},
@@ -39,14 +40,15 @@ export const defaultApiConfig = {
39
40
  export const defaultHeaders = {
40
41
  'Content-Type': 'text/xml'
41
42
  };
42
- class XMLRequest {
43
+ export default class XMLRequest {
43
44
  constructor(callName, fields, config, req) {
44
45
  if (!callName) {
45
46
  throw new EBayNoCallError();
46
47
  }
48
+ this.config = { ...defaultApiConfig, ...config };
49
+ this.j2x = new XMLBuilder({ ...defaultXmlBuilderOptions, ...this.config.xmlBuilderOptions });
47
50
  this.callName = callName;
48
51
  this.fields = fields || {};
49
- this.config = { ...defaultApiConfig, ...config };
50
52
  this.req = req;
51
53
  }
52
54
  getResponseWrapper() {
@@ -78,7 +80,7 @@ class XMLRequest {
78
80
  }
79
81
  toXML(fields) {
80
82
  const HEADING = '<?xml version="1.0" encoding="utf-8"?>';
81
- return HEADING + XMLRequest.j2x.build({
83
+ return HEADING + this.j2x.build({
82
84
  [this.callName + 'Request']: {
83
85
  '@_xmlns': this.config.xmlns,
84
86
  ...this.getCredentials(),
@@ -116,7 +118,7 @@ class XMLRequest {
116
118
  log('error', error);
117
119
  if (error.response?.data) {
118
120
  const json = this.toJSON(error.response.data);
119
- checkEBayTraditionalResponse(error.response, json[this.callName + 'Response']);
121
+ checkEBayTraditionalResponse(error, json[this.callName + 'Response']);
120
122
  }
121
123
  throw error;
122
124
  }
@@ -126,5 +128,3 @@ class XMLRequest {
126
128
  return json[this.getResponseWrapper()] ?? json;
127
129
  }
128
130
  }
129
- XMLRequest.j2x = new XMLBuilder(defaultJSON2XMLOptions);
130
- export default XMLRequest;