UNPKG

@sp-api-sdk/common

Version:
184 lines (180 loc) 5.81 kB
// src/regions.ts var sellingPartnerRegions = { na: { awsRegion: "us-east-1", endpoints: { production: "https://sellingpartnerapi-na.amazon.com", sandbox: "https://sandbox.sellingpartnerapi-na.amazon.com" } }, eu: { awsRegion: "eu-west-1", endpoints: { production: "https://sellingpartnerapi-eu.amazon.com", sandbox: "https://sandbox.sellingpartnerapi-eu.amazon.com" } }, fe: { awsRegion: "us-west-2", endpoints: { production: "https://sellingpartnerapi-fe.amazon.com", sandbox: "https://sandbox.sellingpartnerapi-fe.amazon.com" } } }; // src/axios.ts import axios, { isAxiosError } from "axios"; import { errorLogger, requestLogger, responseLogger } from "axios-logger"; import axiosRetry from "axios-retry"; import { SellingPartnerApiAuthError } from "@sp-api-sdk/auth"; // src/errors.ts import { URL as URL2 } from "url"; import { AxiosError } from "axios"; var SellingPartnerApiError = class extends AxiosError { /** The original error message from the failed HTTP request. */ innerMessage; /** The API name extracted from the request URL path (e.g. `"orders"`). */ apiName; /** The API version extracted from the request URL path (e.g. `"v0"`). */ apiVersion; constructor(error) { super("Unknown error", error.code, error.config, error.request, error.response); this.innerMessage = error.message; if (error.config.url) { const [apiName, apiVersion] = new URL2(error.config.url).pathname.split("/").slice(1); const apiPrefix = `${apiName} (${apiVersion})`; this.apiName = apiName; this.apiVersion = apiVersion; this.message = error.response ? `${apiPrefix} error: Response code ${error.response.status}` : `${apiPrefix} error: No response`; } this.name = this.constructor.name; } }; // src/utils/package.ts import { sync as readPackageJson } from "read-pkg-up"; var result = readPackageJson(); var packageJson = result?.packageJson ?? { _id: "", readme: "", name: "@sp-api-sdk/common", version: "unknown" }; // src/axios.ts function createAxiosInstance({ auth, restrictedDataToken, region, userAgent = `${packageJson.name}/${packageJson.version}`, sandbox = false, rateLimiting, logging }, rateLimits) { const regionConfiguration = sellingPartnerRegions[region]; if (!regionConfiguration) { throw new TypeError(`Unknown or unsupported region: ${region}`); } const instance = axios.create({ headers: { "user-agent": userAgent } }); const endpoint = regionConfiguration.endpoints[sandbox ? "sandbox" : "production"]; if (rateLimiting?.retry) { axiosRetry(instance, { retryCondition: (error) => error.response?.status === 429, retryDelay(retryCount, error) { const url = new URL(error.config.url); const method = error.config.method?.toLowerCase(); const amznRateLimit = Number.parseFloat( error.response?.headers["x-amzn-ratelimit-limit"] ?? "" ); const rateLimit = Number.isNaN(amznRateLimit) ? rateLimits.find( (rateLimit2) => rateLimit2.method.toLowerCase() === method && rateLimit2.urlRegex.exec(url.pathname) )?.rate : amznRateLimit; const delay = rateLimit ? 1 / rateLimit * 1e3 + 1500 : 60 * 1e3; if (rateLimiting.onRetry) { rateLimiting.onRetry({ delay, rateLimit, retryCount, error }); } return delay; } }); } instance.interceptors.request.use(async (config) => { config.headers["x-amz-access-token"] = restrictedDataToken ?? await auth.getAccessToken(); return config; }); instance.interceptors.response.use( async (response) => response, async (error) => { if (isAxiosError(error) && !(error instanceof SellingPartnerApiAuthError)) { throw new SellingPartnerApiError(error); } throw error; } ); if (logging?.request) { const requestLoggerOptions = logging.request === true ? void 0 : logging.request; if (requestLoggerOptions?.headers) { console.warn( "WARNING: You have enabled logging of request headers, this can leak authentication information, you should disable in production." ); } instance.interceptors.request.use((config) => { const logger = requestLogger(config, { prefixText: `sp-api-sdk/${region}`, dateFormat: "isoDateTime", method: true, url: true, params: false, data: true, headers: false, logger: console.info, ...requestLoggerOptions }); return { ...logger, headers: config.headers }; }); } if (logging?.response) { const responseLoggerOptions = logging.response === true ? void 0 : logging.response; instance.interceptors.response.use( (response) => responseLogger(response, { prefixText: `sp-api-sdk/${region}`, dateFormat: "isoDateTime", status: true, statusText: false, params: false, data: false, headers: true, logger: console.info, ...responseLoggerOptions }) ); } if (logging?.error) { const errorLoggerOptions = logging.error === true ? void 0 : logging.error; instance.interceptors.response.use( (response) => response, async (error) => errorLogger(error, { prefixText: `sp-api-sdk/${region}`, dateFormat: "isoDateTime", status: true, statusText: false, params: false, data: false, headers: true, logger: console.error, ...errorLoggerOptions }) ); } return { axios: instance, endpoint }; } export { SellingPartnerApiError, createAxiosInstance, sellingPartnerRegions }; //# sourceMappingURL=index.js.map