From 7aea601b03283592c227bf64b30ce46800f846af Mon Sep 17 00:00:00 2001 From: invalid w Date: Thu, 19 Dec 2024 10:39:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=B2=BE=E7=AE=80filterUndefinedKeys?= =?UTF-8?q?=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/listing/product/BaseInfo/index.ts | 97 +++++++++++++------------- src/listing/product/Parentage/index.ts | 45 ++++++------ src/listing/product/index.ts | 2 +- test/listing/product/index.test.ts | 4 ++ 5 files changed, 76 insertions(+), 74 deletions(-) diff --git a/package.json b/package.json index 7692e74..6ff08bf 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "spapi-listing-builder", "type": "module", - "version": "0.2.6", + "version": "0.2.7", "packageManager": "pnpm@9.9.0", "description": "It is a simple npm package for generating compliant Amazon SP-API listing JSON quickly and efficiently.", "author": "wangjue666 ", diff --git a/src/listing/product/BaseInfo/index.ts b/src/listing/product/BaseInfo/index.ts index 1256e84..9bf9470 100644 --- a/src/listing/product/BaseInfo/index.ts +++ b/src/listing/product/BaseInfo/index.ts @@ -1,49 +1,48 @@ -import { filterUndefinedKeys } from '@/help' -import { ListingImg } from '@/listing/img' -import { ListingPrice } from '@/listing/price' -import { ListingQuantity } from '@/listing/quantity' -import type { ProductData } from '@/help/state' -import { BatteriesRequired, Brand, BulletPoint, Condition, CountryOfOrigin, Description, GenericKeyword, GiftOptions, ItemDimensions, ItemName, ItemPackageQuantity, ItemTypeKeyword, ItemWeight, ListPrice, Manufacturer, MaxOrderQuantity, NumberOfItems, PartNumber, ProductIdentifier, ProductTaxCode, RecommendedBrowseNodes, SupplierDeclaredDgHzRegulation, SupplierDeclaredHasProductIdentifierExemption } from './help' - -export * from './help' - -export class ProductBaseInfo { - data: ProductData - marketplace_id: string - constructor(marketplace_id: string, data: ProductData) { - this.marketplace_id = marketplace_id - this.data = data - } - - main() { - const data = this.data - return filterUndefinedKeys({ - purchasable_offer: data.sell_price && new ListingPrice({ sell_price: data.sell_price }).genValue(), - list_price: data.list_price && new ListPrice(data.list_price).main(), - fulfillment_availability: data.quantity && new ListingQuantity({ quantity: data.quantity, deal_time: data.deal_time }).genValue(), - item_name: new ItemName(data.title).main(), - BatteriesRequired: new BatteriesRequired(data.is_electric).main(), - manufacturer: new Manufacturer(data.manufacturer).main(), - item_weight: new ItemWeight(data.weight).main(), - gift_options: new GiftOptions().main(), - product_tax_code: new ProductTaxCode().main(), - item_type_keyword: new ItemTypeKeyword(data.item_type_keyword).main(), - condition_type: new Condition(data.condition).main(), - number_of_items: new NumberOfItems().main(), - externally_assigned_product_identifier: data.product_identifier_type && new ProductIdentifier(data.product_identifier_type, data.product_identifier_id).main(), - recommended_browse_nodes: data.recommendedBrowseNodes && new RecommendedBrowseNodes(data.recommendedBrowseNodes).main(), - bullet_point: new BulletPoint(data.bullet_points).main(), - item_package_quantity: new ItemPackageQuantity().main(), - item_dimensions: data.height && new ItemDimensions(data.height, data.length, data.width).main(), - part_number: new PartNumber(data.manufactuer_id).main(), - max_order_quantity: new MaxOrderQuantity(data.max_order_quantity).main(), - product_description: new Description(data.product_description).main(), - supplier_declared_dg_hz_regulation: new SupplierDeclaredDgHzRegulation().main(), - brand: new Brand(data.brand_name).main(), - generic_keyword: new GenericKeyword(data.search_terms).main(), - country_of_origin: new CountryOfOrigin(data.country_of_origin).main(), - supplier_declared_has_product_identifier_exemption: new SupplierDeclaredHasProductIdentifierExemption(data.supplier_declared_has_product_identifier_exemption).main(), - ...(data.imgs ? new ListingImg(data.imgs).genValuesMap() : []), - }) - } -} +import { ListingImg } from '@/listing/img' +import { ListingPrice } from '@/listing/price' +import { ListingQuantity } from '@/listing/quantity' +import type { ProductData } from '@/help/state' +import { BatteriesRequired, Brand, BulletPoint, Condition, CountryOfOrigin, Description, GenericKeyword, GiftOptions, ItemDimensions, ItemName, ItemPackageQuantity, ItemTypeKeyword, ItemWeight, ListPrice, Manufacturer, MaxOrderQuantity, NumberOfItems, PartNumber, ProductIdentifier, ProductTaxCode, RecommendedBrowseNodes, SupplierDeclaredDgHzRegulation, SupplierDeclaredHasProductIdentifierExemption } from './help' + +export * from './help' + +export class ProductBaseInfo { + data: ProductData + marketplace_id: string + constructor(marketplace_id: string, data: ProductData) { + this.marketplace_id = marketplace_id + this.data = data + } + + main() { + const data = this.data + return { + purchasable_offer: data.sell_price && new ListingPrice({ sell_price: data.sell_price }).genValue(), + list_price: data.list_price && new ListPrice(data.list_price).main(), + fulfillment_availability: data.quantity && new ListingQuantity({ quantity: data.quantity, deal_time: data.deal_time }).genValue(), + item_name: new ItemName(data.title).main(), + BatteriesRequired: new BatteriesRequired(data.is_electric).main(), + manufacturer: new Manufacturer(data.manufacturer).main(), + item_weight: new ItemWeight(data.weight).main(), + gift_options: new GiftOptions().main(), + product_tax_code: new ProductTaxCode().main(), + item_type_keyword: new ItemTypeKeyword(data.item_type_keyword).main(), + condition_type: new Condition(data.condition).main(), + number_of_items: new NumberOfItems().main(), + externally_assigned_product_identifier: data.product_identifier_type && new ProductIdentifier(data.product_identifier_type, data.product_identifier_id).main(), + recommended_browse_nodes: data.recommendedBrowseNodes && new RecommendedBrowseNodes(data.recommendedBrowseNodes).main(), + bullet_point: new BulletPoint(data.bullet_points).main(), + item_package_quantity: new ItemPackageQuantity().main(), + item_dimensions: data.height && new ItemDimensions(data.height, data.length, data.width).main(), + part_number: new PartNumber(data.manufactuer_id).main(), + max_order_quantity: new MaxOrderQuantity(data.max_order_quantity).main(), + product_description: new Description(data.product_description).main(), + supplier_declared_dg_hz_regulation: new SupplierDeclaredDgHzRegulation().main(), + brand: new Brand(data.brand_name).main(), + generic_keyword: new GenericKeyword(data.search_terms).main(), + country_of_origin: new CountryOfOrigin(data.country_of_origin).main(), + supplier_declared_has_product_identifier_exemption: new SupplierDeclaredHasProductIdentifierExemption(data.supplier_declared_has_product_identifier_exemption).main(), + ...(data.imgs ? new ListingImg(data.imgs).genValuesMap() : []), + } + } +} diff --git a/src/listing/product/Parentage/index.ts b/src/listing/product/Parentage/index.ts index e837f3c..f7c3976 100644 --- a/src/listing/product/Parentage/index.ts +++ b/src/listing/product/Parentage/index.ts @@ -1,23 +1,22 @@ -import { filterUndefinedKeys } from '@/help' -import type { ProductData } from '@/help/state' -import { ChildParentSkuRelationship, Color, ParentageLevel, Size, VariationTheme } from './help' - -export class ProductParentage { - data: ProductData - marketplace_id: string - constructor(marketplace_id: string, data: ProductData) { - this.marketplace_id = marketplace_id - this.data = data - } - - main() { - const data = this.data - return filterUndefinedKeys({ - variation_theme: new VariationTheme(data.variation_theme).main(), - color: new Color(data.color).main(), - size: new Size(data.size).main(), - parentage_level: new ParentageLevel(data.parentage).main(), - child_parent_sku_relationship: new ChildParentSkuRelationship(data.parent_sku).main(), - }) - } -} +import type { ProductData } from '@/help/state' +import { ChildParentSkuRelationship, Color, ParentageLevel, Size, VariationTheme } from './help' + +export class ProductParentage { + data: ProductData + marketplace_id: string + constructor(marketplace_id: string, data: ProductData) { + this.marketplace_id = marketplace_id + this.data = data + } + + main() { + const data = this.data + return { + variation_theme: new VariationTheme(data.variation_theme).main(), + color: new Color(data.color).main(), + size: new Size(data.size).main(), + parentage_level: new ParentageLevel(data.parentage).main(), + child_parent_sku_relationship: new ChildParentSkuRelationship(data.parent_sku).main(), + } + } +} diff --git a/src/listing/product/index.ts b/src/listing/product/index.ts index 3dbd873..29fde14 100644 --- a/src/listing/product/index.ts +++ b/src/listing/product/index.ts @@ -74,7 +74,7 @@ export class ListingProduct { return { productType: data.product_type, requirements: 'LISTING', - attributes, + attributes: filterUndefinedKeys(attributes), } } diff --git a/test/listing/product/index.test.ts b/test/listing/product/index.test.ts index aca4c0f..5c0dbb6 100644 --- a/test/listing/product/index.test.ts +++ b/test/listing/product/index.test.ts @@ -12,6 +12,9 @@ describe('should', () => { renderOtherAttributesFn: ({ renderListingArrValue, data }) => { return { list_price: renderListingArrValue(data.sell_price), + gpsr_manufacturer_reference: data.gpsr_manufacturer_email_address && renderListingArrValue({ + gpsr_manufacturer_email_address: data.gpsr_manufacturer_email_address, + }), } }, }) @@ -19,6 +22,7 @@ describe('should', () => { console.log(JSON.stringify(obj, null, 2)) expect(obj.attributes.item_name[0].value).toEqual(listingData.title) expect(obj.attributes.list_price[0].value).toEqual(String(listingData.sell_price)) + expect(obj.attributes.gpsr_manufacturer_reference).toEqual(undefined) }) it('有变体的父产品', () => {