Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
fix: refactored oneOfEnum codec, moved enums from types to codecs
Browse files Browse the repository at this point in the history
  • Loading branch information
justinemmanuelmercado committed May 29, 2020
1 parent ae6a71d commit 2f8cc9e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 32 deletions.
4 changes: 3 additions & 1 deletion src/parsing.ts
Original file line number Diff line number Diff line change
@@ -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 = <T>(tag: string, codec: Codec<T>): Codec<T[]> => {
Expand Down Expand Up @@ -101,3 +101,5 @@ export const nextToken = <T extends string>(action: T) =>
encode: (x) => x,
schema: () => ({ type: 'string' }),
})

export const oneOfEnum = (enm: object) => oneOf(Object.values(enm).map((x: string) => exactly(x)))
79 changes: 71 additions & 8 deletions src/sections/products/codec.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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,
Expand Down Expand Up @@ -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),
})

Expand Down Expand Up @@ -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,
})
Expand Down
23 changes: 1 addition & 22 deletions src/sections/products/type.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -57,11 +43,6 @@ export interface GetMyFeesEstimateParameters {
[key: string]: FeeEstimateRequest[]
}

export enum IdType {
ASIN = 'ASIN',
SKU = 'SKU',
}

export interface ListMatchingProductsRequestParameters {
MarketplaceId: string
Query: string
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/unit/__snapshots__/products.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 2f8cc9e

Please sign in to comment.