Skip to content

Commit

Permalink
Merge branch '1.0.x' of github.com:Ephigenia/ikea-availability-checke…
Browse files Browse the repository at this point in the history
…r into 1.0.x
  • Loading branch information
Ephigenia committed Dec 17, 2021
2 parents c88c165 + 270c3a5 commit 56afb31
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 21 deletions.
6 changes: 4 additions & 2 deletions source/cli-stock.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,12 @@ Examples:
if (err instanceof errors.IOWS2ParseError) {
return { stock: 0, probability: 'PARSE_ERROR', createdAt: new Date() };
}
// when product could not be found return an empty availability
if (err.response && err.response.status === 404 && promises.length > 1) {
if (err instanceof errors.IOWS2NotFoundError) {
return { stock: 0, probability: 'NOT_FOUND', createdAt: new Date() };
}
if (err instanceof errors.IOWS2DeprecatedError) {
return { stock: 0, probability: 'DEPRECATED', createdAt: new Date() };
}
throw err;
})
.then((availability) => ({
Expand Down
10 changes: 10 additions & 0 deletions source/data/buCodes.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
005,Aalborg,dk
006,Springvale,au
018,Avignon,fr
038,Dublin,ie
051,Hénin-Beaumont,fr
Expand Down Expand Up @@ -121,7 +122,10 @@
373,Gwangmyeong,kr
375,Hognoul,be
376,Zaventem,be
377,Marsden Park,au
378,Haarlem,nl
384,Richmond,au
385,Rhodes,au
386,Salzburg,at
387,Graz,at
388,Haid,at
Expand All @@ -143,6 +147,9 @@
433,Marseille - La Valentine,fr
434,Tours,fr
435,Grenoble,fr
446,Tempe,au
451,Canberra,au
460,North Lakes,au
461,Reading,gb
482,Anderlecht,be
483,Arlon,be
Expand All @@ -152,8 +159,11 @@
519,Sheffield,gb
522,Goyang,kr
548,Exeter,gb
556,Perth,au
557,Adelaid,au
567,Greenwich,gb
645,Rivoli,fr
917,St. Gallen SG,ch
918,Vernier GE,ch
919,Logan,au
921,NY Brooklyn,us
63 changes: 63 additions & 0 deletions source/data/stores.json
Original file line number Diff line number Diff line change
Expand Up @@ -2651,6 +2651,15 @@
],
"countryCode": "qa"
},
{
"buCode": "557",
"name": "Adelaide",
"coordinates": [
138.538300,
-34.932869
],
"countryCode": "au"
},
{
"buCode": "451",
"name": "Canberra",
Expand All @@ -2669,6 +2678,15 @@
],
"countryCode": "au"
},
{
"buCode": "377",
"name": "Marsden Park",
"coordinates": [
150.918930,
-33.648659
],
"countryCode": "au"
},
{
"buCode": "460",
"name": "North Lakes",
Expand All @@ -2678,6 +2696,51 @@
],
"countryCode": "au"
},
{
"buCode": "556",
"name": "Perth",
"coordinates": [
115.803040,
-31.895690
],
"countryCode": "au"
},
{
"buCode": "556",
"name": "Rhodes",
"coordinates": [
151.083710,
-33.835650
],
"countryCode": "au"
},
{
"buCode": "384",
"name": "Richmond",
"coordinates": [
145.0145128,
-37.8120608
],
"countryCode": "au"
},
{
"buCode": "006",
"name": "Springvale",
"coordinates": [
145.143914,
-37.9279425
],
"countryCode": "au"
},
{
"buCode": "446",
"name": "Tempe",
"coordinates": [
151.1663153,
-33.92255
],
"countryCode": "au"
},
{
"buCode": "414",
"name": "Boucherville",
Expand Down
49 changes: 34 additions & 15 deletions source/lib/iows2.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,40 @@ class IOWS2 {
* @param {import('axios').AxiosRequestConfig} [options={}] additional
* options
* @return {Promise<Object>} response body data
* @throws {import('./errors').IOWS2ResponseError}
* @throws {import('./errors').IOWS2ParseError} in case the response
* body is not an object
* @throws {import('./errors').IOWS2NotFoundError} in case the response
* status code is 404
* @throws {import('./errors').IOWS2DeprecatedError} in case the response
* contains a "deprecation" header
* @throws {import('./errors').IOWS2ResponseError} in any other case
*/
async fetch(url, options = {}) {
// required headers, without them IOWS endpoint will return
// 409 (gone), 401 or even 404
return this.api.get(url, options)
.then(response => {
if (typeof response.data !== 'object') {
throw new errors.IOWS2ParseError('Unable to parse response');
let response;
try {
response = await this.api.get(url, options);
} catch (error) {
if (error.request) {
// 20211217 some of the country endpoints started to reply with a
// deprecation and warning header stating that the IOWS2 API is
// going to be deprecated.
const responseHeaders = error.request.res.headers;
if (responseHeaders.deprecation) {
throw new errors.IOWS2DeprecatedError(error);
}
return response.data;
})
.catch(err => {
throw new errors.IOWS2ResponseError(err);
});
if (error.request.res.statusCode === 404) {
throw new errors.IOWS2NotFoundError(error);
}
}
throw new errors.IOWS2ResponseError(error);
}

// double check if the response was successfully parsed and is an object
if (typeof response.data !== 'object') {
throw new errors.IOWS2ParseError('Unable to parse response', response.data);
}

return response.data;
}

/**
Expand Down Expand Up @@ -184,11 +203,11 @@ class IOWS2 {
const url = this.buildUrl(this.baseUrl, this.countryCode, this.languageCode, buCode, productId, productType);
return this.fetch(url)
.catch(err => {
if (err.response) {
if (err.request.res) {
err.message =
`Unable to receive product ${productId} availability for store ` +
`${buCode} status code: ${err.response.status} ${err.response.statusText} ` +
`using url: ${err.request.url}`;
`${buCode} status code: ${err.request.res.statusCode} ${err.request.res.statusText} ` +
`using url: ${err.request.path}`;
}
throw err;
})
Expand Down
19 changes: 17 additions & 2 deletions source/lib/iows2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,26 @@ describe('IOWS2', () => {
});
it('throws an error when the response doesn’t contain valid json', () => {
const scope = nock(URL).get(/.+/).reply(200, '<html>');
return iows.fetch(URL, {})
.catch(err => {
expect(err).to.be.instanceOf(errors.IOWS2ParseError);
expect(err).to.have.property('message').to.match(/Unable to parse/i);
scope.isDone();
});
});

it('throws a IOWS2Deprecated error when the api returns a deprecation warning', () => {
const scope = nock(URL).get(/.+/)
.reply(404, 'Gone', {
'deprecation': 'version="1", date="Sat, 31 Dec 2022 23:59:59 GMT"',
'warning': 'IOWSStockAvailabilityService.GetStockAvailability.v1 API is now deprecated.',
'link': 'alternate="Customer Item Availability. Enquire via slack #rrm-cia"',
'x-global-transaction-id': 'a345495061bc8d13018340e5',
});
return iows.fetch(URL, {})
.then(() => {throw Error('Unexpected resolved promise')})
.catch(err => {
expect(err).to.be.instanceOf(Error);
expect(err.message).to.match(/Unable to parse/i);
expect(err).to.be.instanceof(errors.IOWS2DeprecatedError);
scope.isDone();
});
});
Expand Down
9 changes: 7 additions & 2 deletions source/lib/iows2Errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@ class IOWS2ParseError extends IOWS2Error{

class IOWS2ResponseError extends IOWS2Error {
/**
* @param {import('axios').AxiosError} error error message
* @param {import('axios').AxiosError} error Axios Error object
*/
constructor(error) {
super(error.message);
Object.assign(this, error);
}
}

class IOWS2DeprecatedError extends IOWS2ResponseError {}
class IOWS2NotFoundError extends IOWS2ResponseError {}

module.exports = {
IOWS2DeprecatedError,
IOWS2Error,
IOWS2NotFoundError,
IOWS2ParseError,
IOWS2ResponseError,
}
};

0 comments on commit 56afb31

Please sign in to comment.