-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathapp.js
81 lines (65 loc) · 3.03 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const paapiSdk = require('./SDK/src/index'); // Official SDK from NPM is currently not working. This is downloaded straight from PAAPI website.
const marketplaceList = require('./marketplace.json');
const defaultClient = paapiSdk.ApiClient.instance;
const api = new paapiSdk.DefaultApi();
const apiRequest = async (Options) => {
let commonParameters = Options.commonParameters;
defaultClient.accessKey = commonParameters.AccessKey;
defaultClient.secretKey = commonParameters.SecretKey;
let marketPlaceDetails = await getMarketplaceDetails(commonParameters.Marketplace);
defaultClient.host = marketPlaceDetails.Host;
defaultClient.region = marketPlaceDetails.Region;
let operationOptions = getDefaultOperation(Options.Operations);
operationOptions['PartnerTag'] = commonParameters.PartnerTag;
operationOptions['PartnerType'] = commonParameters.PartnerType;
Object.assign(operationOptions, Options.requestParameters);
return await api[Options.Operations](operationOptions);
};
const GetItems = async (commonParameters, requestParameters) => {
let Options = { commonParameters, requestParameters, Operations : "getItems" };
return await apiRequest(Options);
};
const GetBrowseNodes = async (commonParameters, requestParameters) => {
let Options = { commonParameters, requestParameters, Operations : "getBrowseNodes" };
return await apiRequest(Options);
};
const GetVariations = async (commonParameters, requestParameters) => {
let Options = { commonParameters, requestParameters, Operations : "getVariations" };
return await apiRequest(Options);
};
const SearchItems = async (commonParameters, requestParameters) => {
let Options = { commonParameters, requestParameters, Operations : "searchItems" };
return await apiRequest(Options);
};
const getDefaultOperation = method => {
switch (method) {
case 'getItems':
return new paapiSdk.GetItemsRequest();
break;
case 'getBrowseNodes':
return new paapiSdk.GetBrowseNodesRequest();
break;
case 'getVariations':
return new paapiSdk.GetVariationsRequest();
break;
case 'searchItems':
return new paapiSdk.SearchItemsRequest();
}
};
const getMarketplaceDetails = marketplace => new Promise ((resolve, reject) => {
if(isUndefined(marketplace)) { // set US as default
let marketPlaceDetail = marketplaceList.Marketplace.filter(x => x.Web === "www.amazon.com")[0];
resolve(marketPlaceDetail);
} else {
let marketPlaceDetail = marketplaceList.Marketplace.filter(x => x.Web === marketplace.toLowerCase())[0];
if(isUndefined(marketPlaceDetail)) reject("Invalid Marketplace Value.");
resolve(marketPlaceDetail);
}
});
const getPartnerType = partnerType => new Promise ((resolve, reject) => {
if (isUndefined(partnerType)) resolve('Associates');
else resolve(partnerType);
});
const isUndefined = value => typeof value === 'undefined';
const amazonPaapi = { GetItems, GetBrowseNodes, GetVariations, SearchItems };
module.exports = { ...amazonPaapi, default : amazonPaapi}; // Allow use of default import syntax in TypeScript.