import { GetItemsResourceValues } from 'amazon-pa-api5-node-ts/src/model/GetItemsResource.mts'; // import { amazonPAAPIConfig } from '../data/fetch-site'; // import { config } from '../config'; import { getSiteConfig } from '../data/api-client'; import { client } from '../data/core/client'; import * as ProductAdvertisingAPIv1 from 'amazon-pa-api5-node-ts'; // import type { AmazonProductDetails } from '../data/products/amazon-product-details'; import { title } from 'process'; const siteConfig = await getSiteConfig(); const defaultClient = ProductAdvertisingAPIv1.ApiClient.instance; defaultClient.accessKey = siteConfig.items[0].data?.amazonPAAPIConfig.iv.accessKey!; defaultClient.secretKey = siteConfig.items[0].data?.amazonPAAPIConfig.iv.secretKey!; defaultClient.host = siteConfig.items[0].data?.amazonPAAPIConfig.iv.host!; defaultClient.region = siteConfig.items[0].data?.amazonPAAPIConfig.iv.region!; const api = new ProductAdvertisingAPIv1.DefaultApi(); export const getItems = (itemIds: string[]): Promise => { if (itemIds.length < 1 || itemIds.length > 10) { throw new Error("itemIds must be between 1 and 10."); } const getItemsRequest = new ProductAdvertisingAPIv1.GetItemsRequest(); getItemsRequest['PartnerTag'] = siteConfig.items[0].data?.amazonPAAPIConfig.iv.partnerTag; getItemsRequest['PartnerType'] = siteConfig.items[0].data?.amazonPAAPIConfig.iv.partnerType; getItemsRequest['ItemIds'] = itemIds; getItemsRequest['Condition'] = 'New'; getItemsRequest['Resources'] = [ GetItemsResourceValues['BrowseNodeInfo.BrowseNodes'], GetItemsResourceValues['BrowseNodeInfo.BrowseNodes.Ancestor'], GetItemsResourceValues['BrowseNodeInfo.BrowseNodes.SalesRank'], GetItemsResourceValues['BrowseNodeInfo.WebsiteSalesRank'], GetItemsResourceValues['CustomerReviews.Count'], GetItemsResourceValues['CustomerReviews.StarRating'], GetItemsResourceValues['Images.Primary.Small'], GetItemsResourceValues['Images.Primary.Medium'], GetItemsResourceValues['Images.Primary.Large'], 'Images.Primary.HighRes', GetItemsResourceValues['Images.Variants.Small'], GetItemsResourceValues['Images.Variants.Medium'], GetItemsResourceValues['Images.Variants.Large'], 'Images.Variants.HighRes', GetItemsResourceValues['ItemInfo.ByLineInfo'], GetItemsResourceValues['ItemInfo.Classifications'], GetItemsResourceValues['ItemInfo.ContentInfo'], GetItemsResourceValues['ItemInfo.ContentRating'], GetItemsResourceValues['ItemInfo.ExternalIds'], GetItemsResourceValues['ItemInfo.Features'], GetItemsResourceValues['ItemInfo.ManufactureInfo'], GetItemsResourceValues['ItemInfo.ProductInfo'], GetItemsResourceValues['ItemInfo.TechnicalInfo'], GetItemsResourceValues['ItemInfo.Title'], GetItemsResourceValues['ItemInfo.TradeInInfo'], GetItemsResourceValues['Offers.Listings.Availability.MaxOrderQuantity'], GetItemsResourceValues['Offers.Listings.Availability.Message'], GetItemsResourceValues['Offers.Listings.Availability.MinOrderQuantity'], GetItemsResourceValues['Offers.Listings.Availability.Type'], GetItemsResourceValues['Offers.Listings.Condition'], GetItemsResourceValues['Offers.Listings.Condition.ConditionNote'], GetItemsResourceValues['Offers.Listings.Condition.SubCondition'], GetItemsResourceValues['Offers.Listings.DeliveryInfo.IsAmazonFulfilled'], GetItemsResourceValues['Offers.Listings.DeliveryInfo.IsFreeShippingEligible'], GetItemsResourceValues['Offers.Listings.DeliveryInfo.IsPrimeEligible'], GetItemsResourceValues['Offers.Listings.IsBuyBoxWinner'], GetItemsResourceValues['Offers.Listings.LoyaltyPoints.Points'], GetItemsResourceValues['Offers.Listings.MerchantInfo'], GetItemsResourceValues['Offers.Listings.Price'], GetItemsResourceValues['Offers.Listings.ProgramEligibility.IsPrimeExclusive'], GetItemsResourceValues['Offers.Listings.ProgramEligibility.IsPrimePantry'], GetItemsResourceValues['Offers.Listings.Promotions'], GetItemsResourceValues['Offers.Listings.SavingBasis'], GetItemsResourceValues['Offers.Summaries.HighestPrice'], GetItemsResourceValues['Offers.Summaries.LowestPrice'], GetItemsResourceValues['Offers.Summaries.OfferCount'], GetItemsResourceValues['ParentASIN'], GetItemsResourceValues['RentalOffers.Listings.Availability.MaxOrderQuantity'], GetItemsResourceValues['RentalOffers.Listings.Availability.Message'], GetItemsResourceValues['RentalOffers.Listings.Availability.MinOrderQuantity'], GetItemsResourceValues['RentalOffers.Listings.Availability.Type'], GetItemsResourceValues['RentalOffers.Listings.BasePrice'], GetItemsResourceValues['RentalOffers.Listings.Condition'], GetItemsResourceValues['RentalOffers.Listings.Condition.ConditionNote'], GetItemsResourceValues['RentalOffers.Listings.Condition.SubCondition'], GetItemsResourceValues['RentalOffers.Listings.DeliveryInfo.IsAmazonFulfilled'], GetItemsResourceValues['RentalOffers.Listings.DeliveryInfo.IsFreeShippingEligible'], GetItemsResourceValues['RentalOffers.Listings.DeliveryInfo.IsPrimeEligible'], GetItemsResourceValues['RentalOffers.Listings.DeliveryInfo.ShippingCharges'], GetItemsResourceValues['RentalOffers.Listings.MerchantInfo'], ]; return new Promise((resolve, reject) => { return api.getItems(ProductAdvertisingAPIv1.GetItemsRequest.constructFromObject(getItemsRequest)!).then( function(data) { console.log('API called successfully.'); const getItemsResponse = ProductAdvertisingAPIv1.GetItemsResponse.constructFromObject(data)!; console.log('Complete Response: \n' + JSON.stringify(getItemsResponse, null, 1)); if (getItemsResponse['Errors'] !== undefined) { //TODO: Write failure story to Squidex about Amazon PA API call (attach JSON). console.error('\nErrors:'); console.error('Complete Error Response: ' + JSON.stringify(getItemsResponse['Errors'], null, 1)); console.error('Printing 1st Error:'); var error_0 = getItemsResponse['Errors'][0]; console.error('Error Code: ' + error_0['Code']); console.error('Error Message: ' + error_0['Message']); reject(`Error ${error_0['Code']}: ${error_0['Message']}`); } else { //TODO: Write success story to Squidex about Amazon PA API call (attach JSON). // let parsedItems: ProductAdvertisingAPIv1.GetItemsResponse[] = []; // for (let item of getItemsResponse.ItemsResult?.Items!) { // parsedItems.push({ // ASIN: item.ASIN!, // amazonLink: item.DetailPageURL!, // featureBullets: item.ItemInfo?.Features?.DisplayValues!, // // description: , // title: item.ItemInfo?.Title?.DisplayValue!, // }) // } // resolve(parsedItems); resolve(data); } }, function(error) { console.log('Error calling PA-API 5.0!'); console.log('Printing Full Error Object:\n' + JSON.stringify(error, null, 1)); console.log('Status Code: ' + error['status']); if (error['response'] !== undefined && error['response']['text'] !== undefined) { console.log('Error Object: ' + JSON.stringify(error['response']['text'], null, 1)); } } ); }); }