@haatlabs-core/provider-amazon
v0.10.0
Published
Amazon search, products and suggestions as a ProductSource.
Readme
@haatlabs-core/provider-amazon
Install
pnpm add @haatlabs-core/provider-amazonIt leaves one package to your app, so the app holds one copy: zod.
What it does
Amazon search, products and suggestions through an Amazon scraper, called directly or through a
services gateway: createAmazonSource(options) gives a ProductSource (search,
product, autocomplete, parseLink, health). It caches nothing: wrap a call in
cachedCall (@haatlabs-core/providers) for AMAZON_CACHE_SECONDS (search and product an hour,
suggestions a day). AMAZON_IMAGE_HOSTS are the hosts a content security policy names.
Options
baseUrl (the scraper, the gateway's /amazon, or the hosted API), timeoutMs, apiKey
(the hosted API's) or gatewayKey (the gateway's; setting both is a RangeError),
proxyEnabled (Japan and Australia need a proxy that exits there), fetch,
maxConcurrent (12 calls in flight), maxWaitMs (10 s for a place before the call is
refused) and maxResponseBytes (2 MiB).
Errors
SOURCE_BAD_REQUEST (400), SOURCE_PRODUCT_NOT_FOUND (404, for a product only),
SOURCE_UNAVAILABLE (503, transient: away, slow, busy, or no place within maxWaitMs) and
SOURCE_ERROR (503: an answer that cannot be used). A project spreads
PROVIDER_AMAZON_ERRORS into defineErrors. A 429 or 5xx carries retryAfterMs from its
Retry-After (seconds or a date, at most five minutes). Called directly, a refusal's body is
never read: the connection is let go.
Gateway and deadlines
Each call takes a CallContext (tier, priority, deadlineMs); with gatewayKey it sends
the gateway's headers, tier 4 in the background when there is none. The call's time limit is
timeoutMs, or deadlineMs when that is sooner (localTimeoutMs), and it starts before the
wait for one of the maxConcurrent places: the wait counts, and a call whose time ran out
while it waited is never made. The gateway is given what is left of that limit, never longer.
The deadline is each call's: a caller's retry starts it again, so a caller that retries
gives each try what is left of its own budget. Through the gateway a refusal carries an UpstreamStatusError as its
cause: isGatewayRefusal(error) is true for the gateway's 401 and 403 (both
SOURCE_UNAVAILABLE), and only the gateway's own bad_request 400 is a contract error.
Fixtures
@haatlabs-core/provider-amazon/fixtures: amazonFixtures.product, .search and
.autocomplete, answers recorded from the real scraper.
Testing
@haatlabs-core/provider-amazon/testing: createFakeSource(options), a second ProductSource
with one market, for tests that must not assume Amazon. The package's own tests are unit
tests on the fixtures: pnpm --filter @haatlabs-core/provider-amazon test.
Hosted services
Without a scraper of your own, read Amazon through the Haatlabs API on RapidAPI: set
haatlabsApiKey (your RapidAPI key) and baseUrl: 'https://haatlabs-api.p.rapidapi.com'
(apiKey and gatewayKey stay null). Every marketplace is available; there are no
suggestions (autocomplete is absent); a refusal keeps its message, and a used-up quota or a
refused key is SOURCE_UNAVAILABLE that is not transient. See @haatlabs-core/services and
https://haatlabs.shop/docs/services/
