Comparing version 9.0.3 to 9.1.0
@@ -1,6 +0,6 @@ | ||
import { XMLBuilder } from 'fast-xml-parser'; | ||
import { X2jOptions, XMLBuilder, XmlBuilderOptions } from 'fast-xml-parser'; | ||
import { IEBayApiRequest } from '../../request.js'; | ||
import { ApiRequestConfig, Headers } from '../../types/index.js'; | ||
import { Fields } from './fields.js'; | ||
export declare const defaultJSON2XMLOptions: { | ||
export declare const defaultXmlBuilderOptions: { | ||
attributeNamePrefix: string; | ||
@@ -35,3 +35,4 @@ textNodeName: string; | ||
raw?: boolean; | ||
parseOptions?: object; | ||
parseOptions?: X2jOptions; | ||
xmlBuilderOptions?: XmlBuilderOptions; | ||
useIaf?: boolean; | ||
@@ -56,3 +57,3 @@ sign?: boolean; | ||
private readonly req; | ||
static j2x: XMLBuilder; | ||
readonly j2x: XMLBuilder; | ||
constructor(callName: string, fields: Fields | null, config: XMLReqConfig, req: IEBayApiRequest); | ||
@@ -59,0 +60,0 @@ private getResponseWrapper; |
@@ -5,3 +5,3 @@ import debug from 'debug'; | ||
const log = debug('ebay:xml:request'); | ||
export const defaultJSON2XMLOptions = { | ||
export const defaultXmlBuilderOptions = { | ||
attributeNamePrefix: '@_', | ||
@@ -35,2 +35,3 @@ textNodeName: '#value', | ||
parseOptions: defaultXML2JSONParseOptions, | ||
xmlBuilderOptions: defaultXmlBuilderOptions, | ||
useIaf: true, | ||
@@ -44,3 +45,3 @@ sign: false, | ||
}; | ||
class XMLRequest { | ||
export default class XMLRequest { | ||
constructor(callName, fields, config, req) { | ||
@@ -50,5 +51,6 @@ if (!callName) { | ||
} | ||
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; | ||
@@ -85,3 +87,3 @@ } | ||
const HEADING = '<?xml version="1.0" encoding="utf-8"?>'; | ||
return HEADING + XMLRequest.j2x.build({ | ||
return HEADING + this.j2x.build({ | ||
[this.callName + 'Request']: { | ||
@@ -124,3 +126,3 @@ '@_xmlns': this.config.xmlns, | ||
const json = this.toJSON(error.response.data); | ||
checkEBayTraditionalResponse(error.response, json[this.callName + 'Response']); | ||
checkEBayTraditionalResponse(error, json[this.callName + 'Response']); | ||
} | ||
@@ -135,3 +137,1 @@ throw error; | ||
} | ||
XMLRequest.j2x = new XMLBuilder(defaultJSON2XMLOptions); | ||
export default XMLRequest; |
@@ -16,3 +16,4 @@ export declare const rawError: unique symbol; | ||
readonly meta?: EBayErrorMeta; | ||
constructor(message: string, description?: string, meta?: EBayErrorMeta, errorCode?: number | undefined); | ||
readonly firstError?: EBayFirstError; | ||
constructor(message: string, description?: string, meta?: EBayErrorMeta, errorCode?: number, firstError?: EBayFirstError); | ||
} | ||
@@ -131,2 +132,2 @@ export declare class EBayApiError extends EbayApiError { | ||
export declare const handleEBayError: (error: any) => never; | ||
export declare const checkEBayTraditionalResponse: (result: any, data: any) => void; | ||
export declare const checkEBayTraditionalResponse: (apiResponse: any, data: any) => void; |
@@ -24,5 +24,6 @@ import debug from 'debug'; | ||
export class EbayApiError extends EBayError { | ||
constructor(message, description, meta, errorCode) { | ||
constructor(message, description, meta, errorCode, firstError) { | ||
super(message, description, meta); | ||
this.errorCode = errorCode; | ||
this.firstError = firstError; | ||
} | ||
@@ -55,3 +56,3 @@ } | ||
} | ||
function getEBayErrorFromResponse(data) { | ||
function getEBayError(data) { | ||
if (!data) { | ||
@@ -119,3 +120,3 @@ return { | ||
export const extractEBayError = (result, data) => { | ||
const eBayError = getEBayErrorFromResponse(result.response?.data || data); | ||
const eBayError = getEBayError(data || result.response?.data); | ||
const meta = { | ||
@@ -160,40 +161,40 @@ ...eBayError, | ||
} | ||
const { message, meta, description, errorCode } = extractEBayError(error); | ||
const { message, meta, description, errorCode, firstError } = extractEBayError(error); | ||
if ('domain' in meta && meta.domain === 'ACCESS') { | ||
throw new EBayAccessDenied(message, description, meta, errorCode); | ||
throw new EBayAccessDenied(message, description, meta, errorCode, firstError); | ||
} | ||
else if ('message' in meta && meta.message === 'invalid_grant') { | ||
throw new EBayInvalidGrant(message, description, meta, errorCode); | ||
throw new EBayInvalidGrant(message, description, meta, errorCode, firstError); | ||
} | ||
else if ('message' in meta && meta.message === 'invalid_scope') { | ||
throw new EBayInvalidScope(message, description, meta, errorCode); | ||
throw new EBayInvalidScope(message, description, meta, errorCode, firstError); | ||
} | ||
else if ('message' in meta && meta.message === 'Invalid access token') { | ||
throw new EBayInvalidAccessToken(message, description, meta, errorCode); | ||
throw new EBayInvalidAccessToken(message, description, meta, errorCode, firstError); | ||
} | ||
else if (errorCode === EBayNotFound.code) { | ||
throw new EBayNotFound(message, description, meta, errorCode); | ||
throw new EBayNotFound(message, description, meta, errorCode, firstError); | ||
} | ||
throw new EBayApiError(message, description, meta, errorCode); | ||
throw new EBayApiError(message, description, meta, errorCode, firstError); | ||
}; | ||
export const checkEBayTraditionalResponse = (result, data) => { | ||
export const checkEBayTraditionalResponse = (apiResponse, data) => { | ||
if (!('Errors' in data) && !('errorMessage' in data)) { | ||
return; | ||
} | ||
const { message, meta, description, errorCode } = extractEBayError(result, data); | ||
const { message, meta, description, errorCode, firstError } = extractEBayError(apiResponse, data); | ||
if (typeof errorCode === 'undefined') { | ||
throw new EBayApiError(message, description, meta, errorCode); | ||
throw new EBayApiError(message, description, meta, errorCode, firstError); | ||
} | ||
switch (errorCode) { | ||
case EBayIAFTokenExpired.code: | ||
throw new EBayIAFTokenExpired(message, description, meta, errorCode); | ||
throw new EBayIAFTokenExpired(message, description, meta, errorCode, firstError); | ||
case EBayIAFTokenInvalid.code: | ||
case 1.32: | ||
throw new EBayIAFTokenInvalid(message, description, meta, errorCode); | ||
throw new EBayIAFTokenInvalid(message, description, meta, errorCode, firstError); | ||
case EBayTokenRequired.code: | ||
throw new EBayTokenRequired(message, description, meta, errorCode); | ||
throw new EBayTokenRequired(message, description, meta, errorCode, firstError); | ||
case EBayAuthTokenIsHardExpired.code: | ||
throw new EBayAuthTokenIsHardExpired(message, description, meta, errorCode); | ||
throw new EBayAuthTokenIsHardExpired(message, description, meta, errorCode, firstError); | ||
} | ||
throw new EBayApiError(message, description, meta, errorCode); | ||
throw new EBayApiError(message, description, meta, errorCode, firstError); | ||
}; |
@@ -1,6 +0,6 @@ | ||
import { XMLBuilder } from 'fast-xml-parser'; | ||
import { X2jOptions, XMLBuilder, XmlBuilderOptions } from 'fast-xml-parser'; | ||
import { IEBayApiRequest } from '../../request.js'; | ||
import { ApiRequestConfig, Headers } from '../../types/index.js'; | ||
import { Fields } from './fields.js'; | ||
export declare const defaultJSON2XMLOptions: { | ||
export declare const defaultXmlBuilderOptions: { | ||
attributeNamePrefix: string; | ||
@@ -35,3 +35,4 @@ textNodeName: string; | ||
raw?: boolean; | ||
parseOptions?: object; | ||
parseOptions?: X2jOptions; | ||
xmlBuilderOptions?: XmlBuilderOptions; | ||
useIaf?: boolean; | ||
@@ -56,3 +57,3 @@ sign?: boolean; | ||
private readonly req; | ||
static j2x: XMLBuilder; | ||
readonly j2x: XMLBuilder; | ||
constructor(callName: string, fields: Fields | null, config: XMLReqConfig, req: IEBayApiRequest); | ||
@@ -59,0 +60,0 @@ private getResponseWrapper; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.defaultHeaders = exports.defaultApiConfig = exports.defaultXML2JSONParseOptions = exports.defaultJSON2XMLOptions = void 0; | ||
exports.defaultHeaders = exports.defaultApiConfig = exports.defaultXML2JSONParseOptions = exports.defaultXmlBuilderOptions = void 0; | ||
const debug_1 = __importDefault(require("debug")); | ||
@@ -12,3 +12,3 @@ const fast_xml_parser_1 = require("fast-xml-parser"); | ||
const log = (0, debug_1.default)('ebay:xml:request'); | ||
exports.defaultJSON2XMLOptions = { | ||
exports.defaultXmlBuilderOptions = { | ||
attributeNamePrefix: '@_', | ||
@@ -42,2 +42,3 @@ textNodeName: '#value', | ||
parseOptions: exports.defaultXML2JSONParseOptions, | ||
xmlBuilderOptions: exports.defaultXmlBuilderOptions, | ||
useIaf: true, | ||
@@ -56,5 +57,6 @@ sign: false, | ||
} | ||
this.config = { ...exports.defaultApiConfig, ...config }; | ||
this.j2x = new fast_xml_parser_1.XMLBuilder({ ...exports.defaultXmlBuilderOptions, ...this.config.xmlBuilderOptions }); | ||
this.callName = callName; | ||
this.fields = fields || {}; | ||
this.config = { ...exports.defaultApiConfig, ...config }; | ||
this.req = req; | ||
@@ -91,3 +93,3 @@ } | ||
const HEADING = '<?xml version="1.0" encoding="utf-8"?>'; | ||
return HEADING + XMLRequest.j2x.build({ | ||
return HEADING + this.j2x.build({ | ||
[this.callName + 'Request']: { | ||
@@ -130,3 +132,3 @@ '@_xmlns': this.config.xmlns, | ||
const json = this.toJSON(error.response.data); | ||
(0, index_js_1.checkEBayTraditionalResponse)(error.response, json[this.callName + 'Response']); | ||
(0, index_js_1.checkEBayTraditionalResponse)(error, json[this.callName + 'Response']); | ||
} | ||
@@ -141,3 +143,2 @@ throw error; | ||
} | ||
XMLRequest.j2x = new fast_xml_parser_1.XMLBuilder(exports.defaultJSON2XMLOptions); | ||
exports.default = XMLRequest; |
@@ -16,3 +16,4 @@ export declare const rawError: unique symbol; | ||
readonly meta?: EBayErrorMeta; | ||
constructor(message: string, description?: string, meta?: EBayErrorMeta, errorCode?: number | undefined); | ||
readonly firstError?: EBayFirstError; | ||
constructor(message: string, description?: string, meta?: EBayErrorMeta, errorCode?: number, firstError?: EBayFirstError); | ||
} | ||
@@ -131,2 +132,2 @@ export declare class EBayApiError extends EbayApiError { | ||
export declare const handleEBayError: (error: any) => never; | ||
export declare const checkEBayTraditionalResponse: (result: any, data: any) => void; | ||
export declare const checkEBayTraditionalResponse: (apiResponse: any, data: any) => void; |
@@ -33,5 +33,6 @@ "use strict"; | ||
class EbayApiError extends EBayError { | ||
constructor(message, description, meta, errorCode) { | ||
constructor(message, description, meta, errorCode, firstError) { | ||
super(message, description, meta); | ||
this.errorCode = errorCode; | ||
this.firstError = firstError; | ||
} | ||
@@ -75,3 +76,3 @@ } | ||
exports.EBayInvalidScope = EBayInvalidScope; | ||
function getEBayErrorFromResponse(data) { | ||
function getEBayError(data) { | ||
if (!data) { | ||
@@ -139,3 +140,3 @@ return { | ||
const extractEBayError = (result, data) => { | ||
const eBayError = getEBayErrorFromResponse(result.response?.data || data); | ||
const eBayError = getEBayError(data || result.response?.data); | ||
const meta = { | ||
@@ -181,42 +182,42 @@ ...eBayError, | ||
} | ||
const { message, meta, description, errorCode } = (0, exports.extractEBayError)(error); | ||
const { message, meta, description, errorCode, firstError } = (0, exports.extractEBayError)(error); | ||
if ('domain' in meta && meta.domain === 'ACCESS') { | ||
throw new EBayAccessDenied(message, description, meta, errorCode); | ||
throw new EBayAccessDenied(message, description, meta, errorCode, firstError); | ||
} | ||
else if ('message' in meta && meta.message === 'invalid_grant') { | ||
throw new EBayInvalidGrant(message, description, meta, errorCode); | ||
throw new EBayInvalidGrant(message, description, meta, errorCode, firstError); | ||
} | ||
else if ('message' in meta && meta.message === 'invalid_scope') { | ||
throw new EBayInvalidScope(message, description, meta, errorCode); | ||
throw new EBayInvalidScope(message, description, meta, errorCode, firstError); | ||
} | ||
else if ('message' in meta && meta.message === 'Invalid access token') { | ||
throw new EBayInvalidAccessToken(message, description, meta, errorCode); | ||
throw new EBayInvalidAccessToken(message, description, meta, errorCode, firstError); | ||
} | ||
else if (errorCode === EBayNotFound.code) { | ||
throw new EBayNotFound(message, description, meta, errorCode); | ||
throw new EBayNotFound(message, description, meta, errorCode, firstError); | ||
} | ||
throw new EBayApiError(message, description, meta, errorCode); | ||
throw new EBayApiError(message, description, meta, errorCode, firstError); | ||
}; | ||
exports.handleEBayError = handleEBayError; | ||
const checkEBayTraditionalResponse = (result, data) => { | ||
const checkEBayTraditionalResponse = (apiResponse, data) => { | ||
if (!('Errors' in data) && !('errorMessage' in data)) { | ||
return; | ||
} | ||
const { message, meta, description, errorCode } = (0, exports.extractEBayError)(result, data); | ||
const { message, meta, description, errorCode, firstError } = (0, exports.extractEBayError)(apiResponse, data); | ||
if (typeof errorCode === 'undefined') { | ||
throw new EBayApiError(message, description, meta, errorCode); | ||
throw new EBayApiError(message, description, meta, errorCode, firstError); | ||
} | ||
switch (errorCode) { | ||
case EBayIAFTokenExpired.code: | ||
throw new EBayIAFTokenExpired(message, description, meta, errorCode); | ||
throw new EBayIAFTokenExpired(message, description, meta, errorCode, firstError); | ||
case EBayIAFTokenInvalid.code: | ||
case 1.32: | ||
throw new EBayIAFTokenInvalid(message, description, meta, errorCode); | ||
throw new EBayIAFTokenInvalid(message, description, meta, errorCode, firstError); | ||
case EBayTokenRequired.code: | ||
throw new EBayTokenRequired(message, description, meta, errorCode); | ||
throw new EBayTokenRequired(message, description, meta, errorCode, firstError); | ||
case EBayAuthTokenIsHardExpired.code: | ||
throw new EBayAuthTokenIsHardExpired(message, description, meta, errorCode); | ||
throw new EBayAuthTokenIsHardExpired(message, description, meta, errorCode, firstError); | ||
} | ||
throw new EBayApiError(message, description, meta, errorCode); | ||
throw new EBayApiError(message, description, meta, errorCode, firstError); | ||
}; | ||
exports.checkEBayTraditionalResponse = checkEBayTraditionalResponse; |
{ | ||
"name": "ebay-api", | ||
"author": "Daniil Tomilow", | ||
"version": "9.0.3", | ||
"version": "9.1.0", | ||
"description": "eBay API for Node and Browser", | ||
@@ -91,3 +91,3 @@ "type": "module", | ||
"debug": "^4.3.4", | ||
"fast-xml-parser": "^4.2.5", | ||
"fast-xml-parser": "^4.4.0", | ||
"qs": "^6.11.0" | ||
@@ -94,0 +94,0 @@ }, |
@@ -25,3 +25,3 @@ # eBay Node API in TypeScript with Browser support | ||
* `v9.0.3` is the latest release. | ||
* `v9.1.0` is the latest release. | ||
* See [here](https://github.com/hendt/ebay-api/blob/master/CHANGELOG.md) for the full changelog. | ||
@@ -501,3 +501,42 @@ | ||
``` | ||
## Handling errors | ||
```js | ||
import eBayApi from 'ebay-api'; | ||
import { EBayApiError } from 'ebay-api/lib/errors'; | ||
const eBay = new eBayApi(/* { your config here } */); | ||
try { | ||
const result = await eBay.trading.GetItem({ | ||
ItemID: 'itemId', | ||
}); | ||
console.log(result); | ||
} catch (error) { | ||
if (error instanceof EBayApiError && error.errorCode === 17) { | ||
// Item not found | ||
} else { | ||
throw error; | ||
} | ||
// in error there is also the field "meta" with the response | ||
if (error instanceof EBayApiError && error.meta?.res?.status === 404) { | ||
// not found | ||
// The first error | ||
console.log(error?.firstError); | ||
} | ||
} | ||
``` | ||
The `errorCode` is extracted from the first error in the API response. | ||
* [Shopping API Error Codes](https://developer.ebay.com/devzone/shopping/docs/callref/Errors/ErrorMessages.html) | ||
* [Trading API Error Codes](https://developer.ebay.com/devzone/xml/docs/reference/ebay/errors/errormessages.htm) | ||
* [RESTful Error Codes](https://developer.ebay.com/devzone/xml/docs/reference/ebay/errors/errormessages.htm) | ||
* [PostOrder Error Codes](https://developer.ebay.com/Devzone/post-order/ErrorMessages.html#ErrorsByNumber) | ||
## Controlling Traditional XML request and response | ||
@@ -510,3 +549,4 @@ | ||
raw?: boolean // return raw XML | ||
parseOptions?: object // https://github.com/NaturalIntelligence/fast-xml-parser | ||
parseOptions?: X2jOptions // https://github.com/NaturalIntelligence/fast-xml-parser | ||
xmlBuilderOptions?: XmlBuilderOptions // https://github.com/NaturalIntelligence/fast-xml-parser | ||
useIaf?: boolean // use IAF in header instead of Bearer | ||
@@ -521,2 +561,36 @@ headers?: Headers // additional Headers (key, value) | ||
### Parse JSON Array | ||
```js | ||
eBay.trading.SetNotificationPreferences({ | ||
UserDeliveryPreferenceArray: [{ | ||
NotificationEnable: { | ||
EventType: 'ItemListed', | ||
EventEnable: 'Enable', | ||
} | ||
}, { | ||
NotificationEnable: { | ||
EventType: 'ItemSold', | ||
EventEnable: 'Enable', | ||
}, | ||
}], | ||
}, { xmlBuilderOptions: { oneListGroup: true }}) | ||
``` | ||
Will produce: | ||
```xml | ||
<UserDeliveryPreferenceArray> | ||
<NotificationEnable> | ||
<EventType>ItemListed</EventType> | ||
<EventEnable>Enable</EventEnable> | ||
</NotificationEnable> | ||
<NotificationEnable> | ||
<EventType>ItemSold</EventType> | ||
<EventEnable>Enable</EventEnable> | ||
</NotificationEnable> | ||
</UserDeliveryPreferenceArray> | ||
``` | ||
## Examples | ||
@@ -523,0 +597,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
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
2333039
54198
725
Updatedfast-xml-parser@^4.4.0