UNPKG

vinmonopolet-ts

Version:

Extracts information on products, categories and stores from Vinmonopolet

94 lines (93 loc) 3.6 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.searchStores = void 0; const BaseStore_1 = __importDefault(require("../../models/store/BaseStore")); const Pagination_1 = __importDefault(require("../../models/Pagination")); const GET_1 = require("../../util/GET"); const constants_1 = require("../../constants"); const defaults = { latitude: 59.9126054, longitude: 10.7515334, }; function searchStores(opts) { if (opts?.query) { return searchByQuery(opts.query); } const lat = opts?.nearLocation?.lat ?? defaults.latitude; const lon = opts?.nearLocation?.lon ?? defaults.longitude; return searchByLocation(lat, lon, opts?.page, opts?.pageSize); } exports.searchStores = searchStores; async function searchByQuery(querystring) { const response = await (0, GET_1.GET)(`${constants_1.VINMONOPOLET_STORE_URL}autocomplete`, new URLSearchParams({ query: querystring })); return { stores: response.stores.map(toBaseStore) }; } async function searchByLocation(lat, lon, currentPage = 1, pageSize = 10) { const query = { fields: "BASIC", currentPage: Math.max(0, currentPage - 1).toString(), pageSize: pageSize.toString(), latitude: lat.toString(), longitude: lon.toString(), }; const response = await (0, GET_1.GET)(constants_1.VINMONOPOLET_STORE_URL, new URLSearchParams(query)); return { stores: response.stores.map(toBaseStore), pagination: new Pagination_1.default(getPagination(query.currentPage, pageSize, response.pagination), { nearLocation: { lat, lon } }, searchStores), }; } function getPagination(currentPage, pageSize, res) { return { currentPage, pageSize, totalPages: res.totalPages, totalResults: res.totalResults, }; } async function getAllStores() { let allStores = []; let currentPage = 0; let numPages = 2; while (currentPage < numPages) { const { totalPages, stores } = await getPaginatedStores(currentPage); currentPage += 1; numPages = totalPages; allStores = [...allStores, ...stores]; } return allStores; } async function getPaginatedStores(page) { const query = { fields: "BASIC", currentPage: page.toString(), pageSize: "50", }; const res = await (0, GET_1.GET)(constants_1.VINMONOPOLET_STORE_URL, new URLSearchParams(query)); const stores = await Promise.all(res.stores.map(toBaseStore)); return { totalPages: res.pagination.totalPages, stores, }; } function toBaseStore(store) { const address = getZipAndCityFromFormattedString(store.address.formattedAddress); return new BaseStore_1.default(store.id, store.displayName, store.address.line1, address?.zip, address?.city, store.geoPoint.latitude, store.geoPoint.longitude); } function getZipAndCityFromFormattedString(formattedAddressString) { try { const splitAddress = formattedAddressString.split(","); const city = splitAddress[splitAddress.length - 1].trim(); const zip = splitAddress[splitAddress.length - 2].trim(); return { zip, city, }; } catch (error) { console.warn("Unable to get zip and city from string: " + formattedAddressString); } } exports.default = getAllStores;