nest-paapi-5
Version:
A NestJS module for Amazon's Product Advertising API 5.0 (PAAPI5)
110 lines • 4.52 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PaAPI5Service = void 0;
const axios_1 = __importDefault(require("axios"));
const common_1 = require("@nestjs/common");
const sign_service_1 = require("../sign/sign.service");
const types_1 = require("../sign/types");
const api_targets_1 = require("./types/api-targets");
let PaAPI5Service = class PaAPI5Service extends sign_service_1.SignService {
constructor({ accessKey, secretKey, countryCode, }) {
super();
this.accessKey = accessKey;
this.secretKey = secretKey;
this.countryCode = countryCode;
}
async searchItems(payload) {
return await this.call({
method: "POST",
resource: "/paapi5/searchitems",
payload,
target: api_targets_1.ApiTarget.searchItems,
});
}
async getItems(payload) {
return await this.call({
method: "POST",
resource: "/paapi5/getitems",
payload,
target: api_targets_1.ApiTarget.getItems,
});
}
async getBrowseNodes(payload) {
return await this.call({
method: "POST",
resource: "/paapi5/getbrowsenodes",
payload,
target: api_targets_1.ApiTarget.getBrowseNodes,
});
}
async getVariations(payload) {
return await this.call({
method: "POST",
resource: "/paapi5/getvariations",
payload,
target: api_targets_1.ApiTarget.getVariations,
});
}
async call({ method, resource, headers, payload, responseType, target, }) {
const { host, region } = types_1.HostAndRegion[this.countryCode];
try {
const baseURL = `https://${host}`.replace(/\/+$/, "");
const timestamp = Date.now();
const axiosRequestConfig = {
baseURL: baseURL,
method,
url: resource,
headers: {
"Content-Encoding": "amz-1.0",
host,
"x-amz-date": this.toAmzDate(timestamp),
"x-amz-target": target,
"Content-Type": "application/json; charset=UTF-8",
},
};
if (headers) {
axiosRequestConfig.headers = Object.assign(Object.assign({}, headers), axiosRequestConfig.headers);
}
if (payload && (method === "PUT" || method === "POST")) {
axiosRequestConfig.data = payload;
}
if (responseType) {
axiosRequestConfig.responseType = responseType;
}
const authorization = this.sign({
accessKey: this.accessKey,
secretKey: this.secretKey,
headers: axiosRequestConfig.headers,
method,
payload,
path: resource,
timestamp,
region,
});
axiosRequestConfig.headers = Object.assign(Object.assign({}, axiosRequestConfig.headers), { "User-Agent": "product-advertising-api-5/0.0.1", Authorization: authorization });
return (await (0, axios_1.default)(axiosRequestConfig)).data;
}
catch (error) {
console.error(error);
throw error;
}
}
};
exports.PaAPI5Service = PaAPI5Service;
exports.PaAPI5Service = PaAPI5Service = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [Object])
], PaAPI5Service);
//# sourceMappingURL=paapi5.service.js.map