diff --git a/src/parsing.ts b/src/parsing.ts index b89d9e00..096d775d 100644 --- a/src/parsing.ts +++ b/src/parsing.ts @@ -1,5 +1,5 @@ /** A collection of parsing codecs */ -import { array, Codec, date, record, string, unknown } from 'purify-ts/Codec' +import { array, Codec, date, exactly, oneOf, record, string, unknown } from 'purify-ts/Codec' import { Left, Right } from 'purify-ts/Either' export const ensureArray = (tag: string, codec: Codec): Codec => { @@ -101,3 +101,5 @@ export const nextToken = (action: T) => encode: (x) => x, schema: () => ({ type: 'string' }), }) + +export const oneOfEnum = (enm: object) => oneOf(Object.values(enm).map((x: string) => exactly(x))) diff --git a/src/sections/products/codec.ts b/src/sections/products/codec.ts index 1952f34e..5cf05e41 100644 --- a/src/sections/products/codec.ts +++ b/src/sections/products/codec.ts @@ -1,27 +1,64 @@ import { boolean, Codec, - exactly, GetInterface, lazy, number, - oneOf, optional, record, string, unknown, } from 'purify-ts' -import { ensureArray, mwsDate } from '../../parsing' -import { CurrencyCodeEnum, FeeDetail as FeeDetailInterface, IdType, Status } from './type' +import { ensureArray, mwsDate, oneOfEnum } from '../../parsing' +import { FeeDetail as FeeDetailInterface } from './type' /** * Collection of codecs for the products api */ +enum ItemConditionEnum { + Any = 'Any', + New = 'New', + Used = 'Used', + Collectible = 'Collectible', + Refurbished = 'Refurbished', + Club = 'Club', +} + +const ItemCondition = oneOfEnum(ItemConditionEnum) + +enum CurrencyCodeEnum { + USD = 'USD', + EUR = 'EUR', + GBP = 'GBP', + RMB = 'RMB', + INR = 'INR', + JPY = 'JPY', + CAD = 'CAD', + MXN = 'MXN', +} + +const CurrencyCode = oneOfEnum(CurrencyCodeEnum) + +enum StatusEnum { + Success = 'Success', + ClientError = 'ClientError', + ServiceError = 'ServiceError', +} + +const Status = oneOfEnum(StatusEnum) + +enum IdTypeEnum { + ASIN = 'ASIN', + SKU = 'SKU', +} + +const IdType = oneOfEnum(IdTypeEnum) + const MoneyType = Codec.interface({ Amount: optional(number), - CurrencyCode: oneOf(Object.values(CurrencyCodeEnum).map((x) => exactly(x))), + CurrencyCode, }) export const Points = Codec.interface({ @@ -43,7 +80,7 @@ const PriceToEstimateFees = Codec.interface({ const FeesEstimateIdentifier = Codec.interface({ MarketplaceId: string, - IdType: oneOf(Object.values(IdType).map((x) => exactly(x))), + IdType, IdValue: string, PriceToEstimateFees, SellerInputIdentifier: string, @@ -73,7 +110,7 @@ const FeesEstimate = Codec.interface({ const FeesEstimateResult = Codec.interface({ FeesEstimateIdentifier, FeesEstimate: optional(FeesEstimate), - Status: oneOf(Object.values(Status).map((x) => exactly(x))), + Status, Error: optional(ErrorType), }) @@ -171,8 +208,34 @@ export const GetLowestOfferListingsForASINResponse = Codec.interface({ * @todo - complete definitions */ +/** + * This has attributes instead of children, how do we handle that? + * http://docs.developer.amazonservices.com/en_CA/products/Products_Datatypes.html#OfferCount + */ +// const OfferCountType = number + +// const LowestPrice + +// const Summary = Codec.interface({ +// TotalOfferCount: number, +// NumberOfOffers: OfferCountType, +// LowestPrices: optional(ensureArray('LowestPrice', LowestPrice)), +// BuyBoxPrices: optional(ensureArray('BuyBoxPrice', BuyBoxPrice)), +// ListPrice: optional(MoneyType), +// SuggestedLowerPricePlusShipping: optional(MoneyType), +// BuyBoxEligibleOffers: optional(OfferCountType), +// OffersAvailableTime: optional(mwsDate) +// }) + +const Identifier = Codec.interface({ + MarketplaceId: string, + SellerSKU: string, + ItemCondition, + TimeOfOfferChange: optional(mwsDate), +}) + const GetLowestPricedOffersForSKU = Codec.interface({ - Identifier: unknown, + Identifier, Summary: unknown, Offers: unknown, }) diff --git a/src/sections/products/type.ts b/src/sections/products/type.ts index 298aeaaa..9b779cfd 100644 --- a/src/sections/products/type.ts +++ b/src/sections/products/type.ts @@ -1,22 +1,8 @@ /** * Collection of types for products API */ -export enum CurrencyCodeEnum { - USD = 'USD', - EUR = 'EUR', - GBP = 'GBP', - RMB = 'RMB', - INR = 'INR', - JPY = 'JPY', - CAD = 'CAD', - MXN = 'MXN', -} -export enum Status { - Success = 'Success', - ClientError = 'ClientError', - ServiceError = 'ServiceError', -} +export type ItemCondition = 'Any' | 'New' | 'Used' | 'Collectible' | 'Refurbished' | 'Club' export interface MoneyType { Amount: number | undefined @@ -57,11 +43,6 @@ export interface GetMyFeesEstimateParameters { [key: string]: FeeEstimateRequest[] } -export enum IdType { - ASIN = 'ASIN', - SKU = 'SKU', -} - export interface ListMatchingProductsRequestParameters { MarketplaceId: string Query: string @@ -94,8 +75,6 @@ export interface GetCompetitivePricingForAsinParameters { [key: string]: string | string[] } -export type ItemCondition = 'Any' | 'New' | 'Used' | 'Collectible' | 'Refurbished' | 'Club' - export interface GetLowestOfferListingsForSkuParameters { SellerSKUList: string[] MarketplaceId: string diff --git a/test/unit/__snapshots__/products.test.ts.snap b/test/unit/__snapshots__/products.test.ts.snap index 7c5b1962..cbc470e8 100644 --- a/test/unit/__snapshots__/products.test.ts.snap +++ b/test/unit/__snapshots__/products.test.ts.snap @@ -397,7 +397,7 @@ Array [ "ItemCondition": "New", "MarketplaceId": "ATVPDKIKX0DER", "SellerSKU": "GE Product", - "TimeOfOfferChange": "2015-07-19T23:15:11.859Z", + "TimeOfOfferChange": 2015-07-19T23:15:11.859Z, }, "Offers": Object { "Offer": Object {