Skip to content

Commit

Permalink
feat: 更符合亚马逊语义的属性定义
Browse files Browse the repository at this point in the history
  • Loading branch information
wangjue666 committed Nov 4, 2024
1 parent 7856f11 commit 69cf39c
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 44 deletions.
6 changes: 3 additions & 3 deletions src/feed/img/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { imageTypeJsonMap } from '../../help/state'
import { getImageType } from '@/help/state'
import { FeedHeader } from '../FeedHeader'

export interface FeedImgData {
Expand Down Expand Up @@ -38,11 +38,11 @@ export class FeedImg {

genPatches(imgs: FeedImgData['imgs']) {
return imgs.filter((item) => {
return imageTypeJsonMap[item.type]
return getImageType(item.type)
}).map((item) => {
return {
op: 'replace',
path: `/attributes/${imageTypeJsonMap[item.type]}`,
path: `/attributes/${getImageType(item.type)}`,
value: [
{
media_location: item.url,
Expand Down
18 changes: 10 additions & 8 deletions src/help/state.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export const imageTypeJsonMap: {
[key: string]: string
} = {
export const imageTypeJsonMap = {
Main: 'main_product_image_locator',
Swatch: 'swatch_product_image_locator',
PT1: 'other_product_image_locator_1',
Expand All @@ -20,6 +18,10 @@ export const imageTypeJsonMap: {
EEGL: 'image_locator_eegl',
}

export function getImageType(key: string): string | undefined {
return (imageTypeJsonMap as any)[key]
}

export type Recordable<T = any> = Record<string, T>

export type ListingType = 'FOLLOW_ASIN' | 'LISTING'
Expand All @@ -37,15 +39,15 @@ export type ProductData = Partial<{
bullet_points: string[]
brand_name: string
product_identifier_type: 'UPC' | 'EAN' | 'ISBN' | 'GTIN' | ''
product_id: string
product_identifier_id: string
condition: string
manufacturer: string
weight: number
length1: number
length2: number
length3: number
height: number
length: number
width: number
recommendedBrowseNodes: string[]
is_electric: number
batteries_required: number // 是否需要电池
manufactuer_id: string
search_terms: string
quantity: number
Expand Down
8 changes: 4 additions & 4 deletions src/listing/img/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { imageTypeJsonMap } from '../../help/state'
import { getImageType } from '../../help/state'
import type { ListingImgData, Recordable } from '../../help/state'

export class ListingImg {
imgData: ListingImgData[]
constructor(imgData: ListingImgData[]) {
this.imgData = imgData.filter((item) => {
return imageTypeJsonMap[item.type]
return getImageType(item.type)
})
}

Expand All @@ -20,7 +20,7 @@ export class ListingImg {
return this.imgData.map((item) => {
return {
op: 'replace',
path: `/attributes/${imageTypeJsonMap[item.type]}`,
path: `/attributes/${getImageType(item.type)}`,
value: this.genValue(item.url),
}
})
Expand All @@ -29,7 +29,7 @@ export class ListingImg {
genValuesMap() {
const obj: Recordable = {}
this.imgData.forEach((item) => {
obj[imageTypeJsonMap[item.type]] = this.genValue(item.url)
obj[getImageType(item.type) as string] = this.genValue(item.url)
})
return obj
}
Expand Down
8 changes: 4 additions & 4 deletions src/listing/product/BaseInfo/BatteriesRequired/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { renderListingArrValue } from '@/help'

export class BatteriesRequired {
is_electric: number
constructor(is_electric: number = 0) {
this.is_electric = is_electric
batteries_required: number
constructor(batteries_required: number = 0) {
this.batteries_required = batteries_required
}

main() {
return renderListingArrValue(Boolean(this.is_electric))
return renderListingArrValue(Boolean(this.batteries_required))
}
}
20 changes: 10 additions & 10 deletions src/listing/product/BaseInfo/ItemDimensions/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { renderListingArrValue } from '@/help'

export class ItemDimensions {
length1: number
length2: number
length3: number
height: number
length: number
width: number

constructor(length1: number, length2: number = 1, length3: number = 1) {
this.length1 = length1
this.length2 = length2
this.length3 = length3
constructor(height: number, length: number = 1, width: number = 1) {
this.height = height
this.length = length
this.width = width
}

main() {
return renderListingArrValue({
height: {
unit: 'centimeters',
value: this.length1,
value: this.height,
},
length: {
unit: 'centimeters',
value: this.length2,
value: this.length,
},
width: {
unit: 'centimeters',
value: this.length3,
value: this.width,
},
})
}
Expand Down
8 changes: 4 additions & 4 deletions src/listing/product/BaseInfo/ProductIdentifier/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { renderListingArrValue } from '@/help'

export class ProductIdentifier {
product_identifier_type: string
product_id: string
constructor(product_identifier_type: string, product_id: string = '') {
product_identifier_id: string
constructor(product_identifier_type: string, product_identifier_id: string = '') {
this.product_identifier_type = product_identifier_type
this.product_id = product_id
this.product_identifier_id = product_identifier_id
}

main() {
return renderListingArrValue({
value: this.product_id,
value: this.product_identifier_id,
type: this.product_identifier_type,
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/listing/product/BaseInfo/ProductTaxCode/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { renderListingArrValue } from '@/help'

export class ProductTaxCode {
product_tax_code: string
constructor(product_tax_code: string = 'A_GEN_NOTAX') {
product_tax_code?: string
constructor(product_tax_code?: string) {
this.product_tax_code = product_tax_code
}

Expand Down
6 changes: 3 additions & 3 deletions src/listing/product/BaseInfo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ export class ProductBaseInfo {
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_id).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.length1 && new ItemDimensions(data.length1, data.length2, data.length3).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().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(),
Expand Down
12 changes: 6 additions & 6 deletions test/listing/product/state.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ProductData } from '../../../src/index'
import type { ProductData } from '@/help/state'

export const listingData: ProductData = {
sku: 'SKU-1234',
Expand All @@ -11,16 +11,16 @@ export const listingData: ProductData = {
],
brand_name: 'i am brand name',
product_identifier_type: 'EAN',
product_id: '123457689',
product_identifier_id: '123457689',
condition: 'new_new',
manufacturer: 'your manufacturer',
manufactuer_id: '9527',
weight: 1,
length1: 1,
length2: 2,
length3: 3,
height: 1,
length: 2,
width: 3,
recommendedBrowseNodes: ['123', '456'],
is_electric: 0,
manufactuer_id: '9527',
search_terms: 'i am search terms',
quantity: 99,
deal_time: 2,
Expand Down

0 comments on commit 69cf39c

Please sign in to comment.