diff --git a/src/sections/products.ts b/src/sections/products.ts index 7c4dca33..b156a27c 100644 --- a/src/sections/products.ts +++ b/src/sections/products.ts @@ -1,4 +1,5 @@ import { + array, boolean, Codec, exactly, @@ -145,7 +146,7 @@ const GetMyFeesEstimateResponse = Codec.interface({ }), }) -type GetMyFeesEstimateResponse = GetInterface +type GetMyFeesEstimate = GetInterface interface ListMatchingProductsRequestParameters { MarketplaceId: string @@ -186,6 +187,33 @@ const GetMatchingProductResponse = Codec.interface({ type MatchingProduct = GetInterface +interface GetMatchingProductForIdParameters { + MarketplaceId: string + IdType: string + IdList: string[] + [key: string]: string | string[] +} + +const MatchingProductForId = Codec.interface({ + Products: ensureArray('Product', Product), +}) + +// GetMatchingProductForId is different than other APIs, +// The example on MWS points to it possibly being an array structured this way +const GetMatchingProductForIdResult = oneOf([ + MatchingProductForId, + array(MatchingProductForId), + string, +]) + +const GetMatchingProductForIdResponse = Codec.interface({ + GetMatchingProductForIdResponse: Codec.interface({ + GetMatchingProductForIdResult, + }), +}) + +type GetMatchingProductForIdResult = GetInterface + export class Products { constructor(private httpClient: HttpClient) {} @@ -209,7 +237,7 @@ export class Products { async getMyFeesEstimate( parameters: GetMyFeesEstimateParameters, - ): Promise<[GetMyFeesEstimateResponse, RequestMeta]> { + ): Promise<[GetMyFeesEstimate, RequestMeta]> { const [response, meta] = await this.httpClient.request('POST', { resource: Resource.Products, version: PRODUCTS_API_VERSION, @@ -225,6 +253,10 @@ export class Products { }) } + /** + * + * @todo Think about converting GetMatchingProductResposne to array to match getMatchingProductForId + */ async getMatchingProduct( parameters: GetMatchingProductParameters, ): Promise<[MatchingProduct, RequestMeta]> { @@ -232,7 +264,10 @@ export class Products { resource: Resource.Products, version: PRODUCTS_API_VERSION, action: 'GetMatchingProduct', - parameters, + parameters: { + 'ASINList.ASIN': parameters.ASINList, + MarketplaceId: parameters.MarketplaceId, + }, }) return GetMatchingProductResponse.decode(response).caseOf({ @@ -242,4 +277,22 @@ export class Products { }, }) } + + async getMatchingProductForId( + parameters: GetMatchingProductForIdParameters, + ): Promise<[GetMatchingProductForIdResult, RequestMeta]> { + const [response, meta] = await this.httpClient.request('POST', { + resource: Resource.Products, + version: PRODUCTS_API_VERSION, + action: 'GetMatchingProductForId', + parameters, + }) + + return GetMatchingProductForIdResponse.decode(response).caseOf({ + Right: (x) => [x.GetMatchingProductForIdResponse.GetMatchingProductForIdResult, meta], + Left: (error) => { + throw new ParsingError(error) + }, + }) + } } diff --git a/test/unit/__snapshots__/products.test.ts.snap b/test/unit/__snapshots__/products.test.ts.snap index ebc9df9d..02b97557 100644 --- a/test/unit/__snapshots__/products.test.ts.snap +++ b/test/unit/__snapshots__/products.test.ts.snap @@ -140,6 +140,260 @@ Array [ ] `; +exports[`products getMatchingProductForId returns a matching product when the response is valid 1`] = ` +Array [ + Array [ + Object { + "Products": Array [ + Object { + "AttributeSets": Object { + "ns2:ItemAttributes": Object { + "ns2:Author": Array [ + "Marmanis, Haralambos", + "Babenko, Dmitry", + ], + "ns2:Binding": "Paperback", + "ns2:Edition": 1, + "ns2:IsEligibleForTradeIn": true, + "ns2:ItemDimensions": Object { + "ns2:Height": 9.17, + "ns2:Length": 7.36, + "ns2:Weight": 1.4, + "ns2:Width": 0.75, + }, + "ns2:Label": "Manning Publications", + "ns2:Languages": Object { + "ns2:Language": Array [ + Object { + "ns2:Name": "english", + "ns2:Type": "Unknown", + }, + Object { + "ns2:Name": "english", + "ns2:Type": "Original Language", + }, + Object { + "ns2:Name": "english", + "ns2:Type": "Published", + }, + ], + }, + "ns2:ListPrice": Object { + "ns2:Amount": 44.99, + "ns2:CurrencyCode": "USD", + }, + "ns2:Manufacturer": "Manning Publications", + "ns2:NumberOfItems": 1, + "ns2:NumberOfPages": 368, + "ns2:PackageDimensions": Object { + "ns2:Height": 0.8, + "ns2:Length": 9.1, + "ns2:Weight": 1.35, + "ns2:Width": 7.3, + }, + "ns2:ProductGroup": "Book", + "ns2:ProductTypeName": "ABIS_BOOK", + "ns2:PublicationDate": "2009-07-05", + "ns2:Publisher": "Manning Publications", + "ns2:SmallImage": Object { + "ns2:Height": 75, + "ns2:URL": "http://ecx.images-amazon.com/images/I/51EEz05N2HL._SL75_.jpg", + "ns2:Width": 60, + }, + "ns2:Studio": "Manning Publications", + "ns2:Title": "Algorithms of the Intelligent Web", + }, + }, + "Identifiers": Object { + "MarketplaceASIN": Object { + "ASIN": 1933988665, + "MarketplaceId": "ATVPDKIKX0DER", + }, + }, + "Relationships": "", + "SalesRankings": Object { + "SalesRank": Array [ + Object { + "ProductCategoryId": "book_display_on_website", + "Rank": 59485, + }, + Object { + "ProductCategoryId": 377886011, + "Rank": 32, + }, + Object { + "ProductCategoryId": 3887, + "Rank": 66, + }, + Object { + "ProductCategoryId": 3870, + "Rank": 82, + }, + ], + }, + }, + ], + }, + Object { + "Products": Array [ + Object { + "AttributeSets": Object { + "ns2:ItemAttributes": Object { + "ns2:Author": "Rowling, J.K.", + "ns2:Binding": "Paperback", + "ns2:Brand": "Scholastic Press", + "ns2:Creator": "GrandPré, Mary", + "ns2:Edition": "1st", + "ns2:Feature": "Recommended Age: 9 years and up", + "ns2:IsAutographed": false, + "ns2:IsMemorabilia": false, + "ns2:ItemDimensions": Object { + "ns2:Height": 0.8, + "ns2:Length": 7.5, + "ns2:Weight": 0.5, + "ns2:Width": 5.2, + }, + "ns2:Label": "Scholastic Paperbacks", + "ns2:Languages": Object { + "ns2:Language": Array [ + Object { + "ns2:Name": "english", + "ns2:Type": "Unknown", + }, + Object { + "ns2:Name": "english", + "ns2:Type": "Original Language", + }, + Object { + "ns2:Name": "english", + "ns2:Type": "Published", + }, + ], + }, + "ns2:ListPrice": Object { + "ns2:Amount": 10.99, + "ns2:CurrencyCode": "USD", + }, + "ns2:Manufacturer": "Scholastic Paperbacks", + "ns2:NumberOfItems": 1, + "ns2:NumberOfPages": 320, + "ns2:PackageDimensions": Object { + "ns2:Height": 1, + "ns2:Length": 7.5, + "ns2:Weight": 0.5, + "ns2:Width": 5.2, + }, + "ns2:PackageQuantity": 1, + "ns2:PartNumber": 9780590353427, + "ns2:ProductGroup": "Book", + "ns2:ProductTypeName": "ABIS_BOOK", + "ns2:PublicationDate": "1999-10-01", + "ns2:Publisher": "Scholastic Paperbacks", + "ns2:ReleaseDate": "1999-09-08", + "ns2:SmallImage": Object { + "ns2:Height": 75, + "ns2:URL": "http://ecx.images-amazon.com/images/I/51MU5VilKpL._SL75_.jpg", + "ns2:Width": 51, + }, + "ns2:Studio": "Scholastic Paperbacks", + "ns2:Title": "Harry Potter and the Sorcerer's Stone (Book 1)", + }, + }, + "Identifiers": Object { + "MarketplaceASIN": Object { + "ASIN": "059035342X", + "MarketplaceId": "ATVPDKIKX0DER", + }, + }, + "Relationships": "", + "SalesRankings": Object { + "SalesRank": Array [ + Object { + "ProductCategoryId": "book_display_on_website", + "Rank": 362, + }, + Object { + "ProductCategoryId": 15356791, + "Rank": 6, + }, + Object { + "ProductCategoryId": 3153, + "Rank": 9, + }, + Object { + "ProductCategoryId": 3045, + "Rank": 14, + }, + ], + }, + }, + Object { + "AttributeSets": Object { + "ns2:ItemAttributes": Object { + "ns2:Author": "J.K. Rowling, Mary GrandPré", + "ns2:Binding": "Paperback", + "ns2:Label": "Scholastic Paperbacks", + "ns2:Languages": Object { + "ns2:Language": Array [ + Object { + "ns2:Name": "english", + "ns2:Type": "Unknown", + }, + Object { + "ns2:Name": "english", + "ns2:Type": "Published", + }, + ], + }, + "ns2:Manufacturer": "Scholastic Paperbacks", + "ns2:NumberOfItems": 1, + "ns2:NumberOfPages": 784, + "ns2:PackageDimensions": Object { + "ns2:Height": 0.73, + "ns2:Length": 8.43, + "ns2:Weight": 0.79, + "ns2:Width": 5.85, + }, + "ns2:ProductGroup": "Book", + "ns2:ProductTypeName": "BOOKS_1973_AND_LATER", + "ns2:PublicationDate": "1999-09-08", + "ns2:Publisher": "Scholastic Paperbacks", + "ns2:SmallImage": Object { + "ns2:Height": 75, + "ns2:URL": "http://ecx.images-amazon.com/images/I/51STfI7UiCL._SL75_.jpg", + "ns2:Width": 52, + }, + "ns2:Studio": "Scholastic Paperbacks", + "ns2:Title": "Harry Potter and the Sorcerer's Stone", + }, + }, + "Identifiers": Object { + "MarketplaceASIN": Object { + "ASIN": 439708184, + "MarketplaceId": "ATVPDKIKX0DER", + }, + }, + "Relationships": "", + "SalesRankings": Object { + "SalesRank": Object { + "ProductCategoryId": "book_display_on_website", + "Rank": 2091024, + }, + }, + }, + ], + }, + ], + Object { + "quotaMax": 1000, + "quotaRemaining": 999, + "quotaResetOn": "2020-04-06T10:22:23.582Z", + "requestId": "0", + "timestamp": "2020-05-06T09:22:23.582Z", + }, +] +`; + exports[`products getMyFeesEstimate returns a list fee estimates when the response is valid 1`] = ` Array [ Object {