UNPKG

n8n-nodes-amazon-paapi-agent

Version:

n8n node for Amazon Affiliate API (PA API 5.0) with enhanced features including OffersV2 support, advanced filtering, and better error handling

216 lines (215 loc) 11.8 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.AmazonPATools = void 0; const amazonPaapi = __importStar(require("amazon-paapi")); class AmazonPATools { constructor() { this.description = { displayName: 'Amazon Product Search', name: 'amazonPATools', icon: 'file:amazon.svg', group: ['transform'], version: 1, subtitle: '={{$parameter["operation"]}}', description: 'Search for products on Amazon and get their details', defaults: { name: 'Amazon Product Search', }, inputs: ["main" /* NodeConnectionType.Main */], outputs: ["main" /* NodeConnectionType.Main */], credentials: [ { name: 'amazonPaApi', required: true, }, ], usableAsTool: true, // This makes it available as an agent tool properties: [ { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, options: [ { name: 'Search Products', value: 'searchProducts', description: 'Search for products on Amazon using keywords and get their details including prices, titles, and URLs', action: 'Search for products and get their details', }, { name: 'Get Product Details', value: 'getProductDetails', description: 'Get detailed information about specific products using their ASIN (Amazon Standard Identification Number)', action: 'Get detailed product information', }, ], default: 'searchProducts', }, { displayName: 'Search Keywords', name: 'keywords', type: 'string', default: '', description: 'The keywords to search for products on Amazon', displayOptions: { show: { operation: ['searchProducts'], }, }, }, { displayName: 'Product ASIN', name: 'asin', type: 'string', default: '', description: 'The Amazon Standard Identification Number (ASIN) of the product', displayOptions: { show: { operation: ['getProductDetails'], }, }, }, { displayName: 'Include Fields', name: 'includeFields', type: 'multiOptions', options: [ { name: 'Title', value: 'ItemInfo.Title' }, { name: 'Features', value: 'ItemInfo.Features' }, { name: 'Price', value: 'Offers.Listings.Price' }, { name: 'Images', value: 'Images.Primary.Medium' }, { name: 'Rating', value: 'CustomerReviews.StarRating' }, { name: 'Review Count', value: 'CustomerReviews.Count' }, { name: 'Brand', value: 'ItemInfo.ByLineInfo.Brand' }, { name: 'Product Description', value: 'ItemInfo.ProductInfo.ProductDescription' }, ], default: ['ItemInfo.Title', 'Offers.Listings.Price', 'Images.Primary.Medium'], description: 'The fields to include in the response', }, ], }; } async execute() { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y; const returnData = []; const operation = this.getNodeParameter('operation', 0, ''); const includeFields = this.getNodeParameter('includeFields', 0, []); try { // Get credentials const credentials = await this.getCredentials('amazonPaApi'); const commonParameters = { AccessKey: credentials.accessKey, SecretKey: credentials.secretKey, PartnerTag: credentials.partnerTag, Marketplace: credentials.marketplace, PartnerType: 'Associates', Resources: includeFields, }; if (operation === 'searchProducts') { const keywords = this.getNodeParameter('keywords', 0, ''); const searchItemsParams = { Keywords: keywords, Resources: includeFields, SearchIndex: 'All', ItemCount: 10, }; const responseData = await amazonPaapi.SearchItems(commonParameters, searchItemsParams); if ((_a = responseData === null || responseData === void 0 ? void 0 : responseData.SearchResult) === null || _a === void 0 ? void 0 : _a.Items) { returnData.push({ json: { success: true, operation: 'searchProducts', results: responseData.SearchResult.Items.map((item) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p; return ({ title: (_b = (_a = item.ItemInfo) === null || _a === void 0 ? void 0 : _a.Title) === null || _b === void 0 ? void 0 : _b.DisplayValue, asin: item.ASIN, url: `https://${credentials.marketplace}/dp/${item.ASIN}`, price: (_f = (_e = (_d = (_c = item.Offers) === null || _c === void 0 ? void 0 : _c.Listings) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.Price) === null || _f === void 0 ? void 0 : _f.DisplayAmount, imageUrl: (_j = (_h = (_g = item.Images) === null || _g === void 0 ? void 0 : _g.Primary) === null || _h === void 0 ? void 0 : _h.Medium) === null || _j === void 0 ? void 0 : _j.URL, rating: (_l = (_k = item.CustomerReviews) === null || _k === void 0 ? void 0 : _k.StarRating) === null || _l === void 0 ? void 0 : _l.DisplayValue, reviewCount: (_m = item.CustomerReviews) === null || _m === void 0 ? void 0 : _m.Count, features: (_p = (_o = item.ItemInfo) === null || _o === void 0 ? void 0 : _o.Features) === null || _p === void 0 ? void 0 : _p.DisplayValues, }); }), }, }); } } else if (operation === 'getProductDetails') { const asin = this.getNodeParameter('asin', 0, ''); const getItemsParams = { ItemIds: [asin], Resources: includeFields, }; const responseData = await amazonPaapi.GetItems(commonParameters, getItemsParams); if ((_c = (_b = responseData === null || responseData === void 0 ? void 0 : responseData.ItemsResult) === null || _b === void 0 ? void 0 : _b.Items) === null || _c === void 0 ? void 0 : _c[0]) { const item = responseData.ItemsResult.Items[0]; returnData.push({ json: { success: true, operation: 'getProductDetails', product: { title: (_e = (_d = item.ItemInfo) === null || _d === void 0 ? void 0 : _d.Title) === null || _e === void 0 ? void 0 : _e.DisplayValue, asin: item.ASIN, url: `https://${credentials.marketplace}/dp/${item.ASIN}`, price: (_j = (_h = (_g = (_f = item.Offers) === null || _f === void 0 ? void 0 : _f.Listings) === null || _g === void 0 ? void 0 : _g[0]) === null || _h === void 0 ? void 0 : _h.Price) === null || _j === void 0 ? void 0 : _j.DisplayAmount, imageUrl: (_m = (_l = (_k = item.Images) === null || _k === void 0 ? void 0 : _k.Primary) === null || _l === void 0 ? void 0 : _l.Medium) === null || _m === void 0 ? void 0 : _m.URL, rating: (_p = (_o = item.CustomerReviews) === null || _o === void 0 ? void 0 : _o.StarRating) === null || _p === void 0 ? void 0 : _p.DisplayValue, reviewCount: (_q = item.CustomerReviews) === null || _q === void 0 ? void 0 : _q.Count, features: (_s = (_r = item.ItemInfo) === null || _r === void 0 ? void 0 : _r.Features) === null || _s === void 0 ? void 0 : _s.DisplayValues, brand: (_v = (_u = (_t = item.ItemInfo) === null || _t === void 0 ? void 0 : _t.ByLineInfo) === null || _u === void 0 ? void 0 : _u.Brand) === null || _v === void 0 ? void 0 : _v.DisplayValue, description: (_y = (_x = (_w = item.ItemInfo) === null || _w === void 0 ? void 0 : _w.ProductInfo) === null || _x === void 0 ? void 0 : _x.ProductDescription) === null || _y === void 0 ? void 0 : _y.DisplayValue, }, }, }); } } } catch (error) { returnData.push({ json: { success: false, error: error instanceof Error ? error.message : 'An unknown error occurred', operation, }, }); } return [returnData]; } } exports.AmazonPATools = AmazonPATools;