ikea-availability-checker
Version:
ikea product in-store availability checker and product search
79 lines (78 loc) • 3.15 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.stores = exports.errors = void 0;
exports.availability = availability;
exports.availabilities = availabilities;
const ingka_1 = require("./lib/ingka");
const stores_1 = require("./lib/stores");
exports.errors = __importStar(require("./lib/ingkaErrors"));
exports.stores = __importStar(require("./lib/stores"));
/**
* Asynchronously requests the availability of a specific product in one
* specific store and returns an object with the information.
*
* @example
* // request the stock info for "Billy (white)" from Braunschweig germany
* const stockInfo = await checker.availability('117', '00263850')
*/
async function availability(
/** buCode ikea store identification number */
buCode,
/** ikea product identification number */
productId,
/** axios requrest options */
options = {}) {
const store = (0, stores_1.findOneById)(buCode);
if (!store) {
throw new Error(`Unable to find a store with the given buCode: ${buCode}.`);
}
const iows = new ingka_1.IngkaApi(undefined, options);
return iows.getStoreProductAvailability(store.countryCode, productId, buCode);
}
/**
* Query one or multiple stores for one or multiple product ids.
*/
async function availabilities(stores, productIds, options = {}) {
const storesProductMap = productIds.map((productId) => {
return stores.map((store) => ({ productId, store }));
});
const iows = new ingka_1.IngkaApi(undefined, options);
const promises = storesProductMap.flat().map(({ store, productId }) => {
return iows.getStoreProductAvailability(store.countryCode, productId, store.buCode);
});
const availabilities = await Promise.all(promises);
return availabilities.filter((a) => a);
}