Skip to content

Commit

Permalink
feat: 精简filterUndefinedKeys的处理
Browse files Browse the repository at this point in the history
  • Loading branch information
wangjue666 committed Dec 19, 2024
1 parent f0fd335 commit 7aea601
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 74 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "spapi-listing-builder",
"type": "module",
"version": "0.2.6",
"version": "0.2.7",
"packageManager": "[email protected]",
"description": "It is a simple npm package for generating compliant Amazon SP-API listing JSON quickly and efficiently.",
"author": "wangjue666 <[email protected]>",
Expand Down
97 changes: 48 additions & 49 deletions src/listing/product/BaseInfo/index.ts
Original file line number Diff line number Diff line change
@@ -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() : []),
}
}
}
45 changes: 22 additions & 23 deletions src/listing/product/Parentage/index.ts
Original file line number Diff line number Diff line change
@@ -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(),
}
}
}
2 changes: 1 addition & 1 deletion src/listing/product/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class ListingProduct {
return {
productType: data.product_type,
requirements: 'LISTING',
attributes,
attributes: filterUndefinedKeys(attributes),
}
}

Expand Down
4 changes: 4 additions & 0 deletions test/listing/product/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ 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,
}),
}
},
})
const obj: Recordable = t1.main()
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('有变体的父产品', () => {
Expand Down

0 comments on commit 7aea601

Please sign in to comment.