Implement protected Studio catalog API and bounded public source reader
This commit is contained in:
+17
-7
@@ -1,10 +1,12 @@
|
||||
const { createHash } = require('node:crypto');
|
||||
const { fail } = require('./workspace.cjs');
|
||||
const BN_DIGITS = '০১২৩৪৫৬৭৮৯';
|
||||
|
||||
function amount(value) {
|
||||
if (typeof value === 'number') return Number.isFinite(value) && value >= 0 ? value : null;
|
||||
if (typeof value !== 'number' && typeof value !== 'string') return null;
|
||||
const text = String(value).trim().replace(/[০-৯]/g, digit => String(BN_DIGITS.indexOf(digit)));
|
||||
if (text.length > 64) return null;
|
||||
if (!/^(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(text)) return null;
|
||||
const number = Number(text.replace(/,/g, ''));
|
||||
return Number.isFinite(number) && number >= 0 ? number : null;
|
||||
@@ -33,9 +35,15 @@ function publicLink(value, base) {
|
||||
}
|
||||
|
||||
function product(record, context) {
|
||||
for (const [name, maximum] of Object.entries({ title: 1024, sku: 256, sourceId: 8192, brand: 256, link: 4096, image: 4096 })) {
|
||||
if (typeof record[name] === 'string' && record[name].length > maximum) {
|
||||
throw fail(502, 'SOURCE_FIELD_TOO_LARGE', 'Website-এর product field অতিরিক্ত বড়; আগের catalog বদলায়নি।');
|
||||
}
|
||||
}
|
||||
const price = amount(record.price), regular = amount(record.regular);
|
||||
const currency = currencyCode(record.currency);
|
||||
const link = publicLink(record.link, context.url) || context.url;
|
||||
const base = context.documentUrl || context.url;
|
||||
const link = publicLink(record.link, base) || context.url;
|
||||
const sourceId = String(record.sourceId || record.sku || link + ':' + record.title);
|
||||
return {
|
||||
id: createHash('sha256').update(context.url + '\0' + sourceId).digest('hex'),
|
||||
@@ -44,7 +52,7 @@ function product(record, context) {
|
||||
brand: typeof record.brand === 'string' ? record.brand : context.name,
|
||||
price: displayPrice(price, currency), rawPrice: price, currency,
|
||||
regularPrice: regular === null ? '' : displayPrice(regular, currency), rawRegularPrice: regular,
|
||||
image: publicLink(record.image, context.url), link,
|
||||
image: publicLink(record.image, base), link,
|
||||
storeName: context.name, storeId: context.id, category: context.category,
|
||||
...(typeof record.inStock === 'boolean' ? { inStock: record.inStock } : {})
|
||||
};
|
||||
@@ -54,12 +62,13 @@ function shopifyProducts(payload, context, currency) {
|
||||
if (!Array.isArray(payload?.products)) return [];
|
||||
return payload.products.flatMap(item => {
|
||||
if (!item || typeof item.title !== 'string' || !item.title.trim()) return [];
|
||||
const variants = Array.isArray(item.variants) && item.variants.length ? item.variants : [{}];
|
||||
const valid = Array.isArray(item.variants) ? item.variants.filter(v => v && typeof v === 'object') : [];
|
||||
const variants = valid.length ? valid : [{}];
|
||||
return variants.map((variant, index) => product({
|
||||
sourceId: (item.id ?? item.handle ?? item.title) + ':' + (variant.id ?? (variant.sku || variant.title || index)), sku: variant.sku,
|
||||
title: item.title + (variant.title && variant.title !== 'Default Title' ? ' — ' + variant.title : ''),
|
||||
title: item.title + (typeof variant.title === 'string' && variant.title && variant.title !== 'Default Title' ? ' — ' + variant.title : ''),
|
||||
brand: item.vendor, price: variant.price, regular: variant.compare_at_price,
|
||||
currency, image: variant.featured_image?.src || item.images?.[0]?.src || item.image?.src,
|
||||
currency: variant.currency || item.currency || currency, image: variant.featured_image?.src || item.images?.[0]?.src || item.image?.src,
|
||||
link: item.handle ? '/products/' + encodeURIComponent(item.handle) : context.url,
|
||||
inStock: typeof variant.available === 'boolean' ? variant.available : undefined
|
||||
}, context));
|
||||
@@ -103,7 +112,8 @@ function schemaProducts(payload, context) {
|
||||
if (Array.isArray(node)) { for (const item of node) visit(item, depth + 1); return; }
|
||||
const types = Array.isArray(node['@type']) ? node['@type'] : [node['@type']];
|
||||
if (types.includes('Product') && typeof node.name === 'string' && node.name.trim()) {
|
||||
const offers = Array.isArray(node.offers) && node.offers.length ? node.offers : [node.offers || {}];
|
||||
const validOffers = Array.isArray(node.offers) ? node.offers.filter(o => o && typeof o === 'object') : [];
|
||||
const offers = validOffers.length ? validOffers : [node.offers && typeof node.offers === 'object' && !Array.isArray(node.offers) ? node.offers : {}];
|
||||
for (const [offerIndex, offer] of offers.entries()) {
|
||||
const image = Array.isArray(node.image) ? node.image[0] : node.image;
|
||||
const isRegularSpecification = offer.priceSpecification?.priceType === 'https://schema.org/StrikethroughPrice';
|
||||
@@ -125,4 +135,4 @@ function schemaProducts(payload, context) {
|
||||
return products;
|
||||
}
|
||||
|
||||
module.exports = { amount, publicLink, product, shopifyProducts, wooProducts, schemaProducts };
|
||||
module.exports = { amount, displayPrice, publicLink, product, shopifyProducts, wooProducts, schemaProducts };
|
||||
|
||||
Reference in New Issue
Block a user