@recapi/client 0.11.0 → 0.11.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -19,7 +19,7 @@ needs a newer browser moves this floor and says so under [Changes](#changes).
19
19
 
20
20
  On every change to the library, CI runs what a shop page relies on in
21
21
  Chromium, Firefox and WebKit, Safari's engine
22
- ([`scripts/e2e/browsers.test.js`](../../scripts/e2e/browsers.test.js)): the
22
+ (recapi's browser test suite): the
23
23
  lists of `recommend()` and `popular()`, a view and a cart credited to the
24
24
  recommendation, the history after a reload, the queued events sent when the
25
25
  page is left or hidden, consent and Global Privacy Control. That WebKit is
@@ -35,7 +35,7 @@ are, gzipped, and browsers cache them:
35
35
 
36
36
  ```html
37
37
  <script type="module">
38
- import { createClient } from 'https://api.example.com/sdk/0.11.0/index.js';
38
+ import { createClient } from 'https://api.example.com/sdk/0.11.2/index.js';
39
39
 
40
40
  const recapi = createClient({ apiKey: 'rk_pub_…', baseUrl: 'https://api.example.com', consent: false });
41
41
  // From your cookie banner, once the visitor agrees: recapi.setConsent(true).
@@ -190,7 +190,7 @@ nothing to paste again. A file of its own rather than more exports of
190
190
  CDN with `?width=400`), the title and `price_min` formatted with
191
191
  `Intl.NumberFormat` in `Shopify.currency.active` and the page's `lang`
192
192
  (no price if either is missing). An item whose
193
- [metadata](../../docs/api.md#item-metadata) has a `name` and an http(s)
193
+ [metadata](https://recapi.io/docs/api#item-metadata) has a `name` and an http(s)
194
194
  `url` skips that lookup: its card is drawn from `name`, `url`, `picture`
195
195
  (http(s) only) and `price` (text shown as it is, or a number in the
196
196
  shop's currency, `{{ shop.currency }}`, shown only to visitors browsing in
@@ -199,7 +199,7 @@ nothing to paste again. A file of its own rather than more exports of
199
199
  snippet can't send metadata: the shop sends it with `PUT /v1/items`, and
200
200
  marks a sold-out product inactive there, since no lookup checks it. The
201
201
  products export upload and the product webhooks (the quick start's step 4,
202
- [API](../../docs/api.md#post-v1shopifywebhooks--shopifys-product-webhooks))
202
+ [API](https://recapi.io/docs/api#post-v1shopifywebhooks--shopifys-product-webhooks))
203
203
  fill it without code; a card whose metadata says `available: false` (maybe
204
204
  sold out) is looked up instead. A default style makes them a product row
205
205
  in the theme's colours and fonts; all its selectors sit inside
@@ -400,7 +400,7 @@ The row asks through `recommendLists()`, the call that takes all of a page's
400
400
  rows, billed as one recommendation with no product on two rows ([Several
401
401
  rows in one call](#several-rows-in-one-call)). A page has one recapi row (the
402
402
  product page's spot, a block and `[recapi]` never show two), so a page costs
403
- one call, two only when the top-up runs. Items whose [metadata](../../docs/api.md#item-metadata)
403
+ one call, two only when the top-up runs. Items whose [metadata](https://recapi.io/docs/api#item-metadata)
404
404
  has a `name` and an http(s) `url` are left out of that request and drawn
405
405
  from it, as in the Shopify snippet (a number `price` is in the shop's
406
406
  currency, `woocommerce_currency`, and shown only while the visitor browses
@@ -510,7 +510,7 @@ install has neither header's effect: both are comments there.
510
510
 
511
511
  ### Magento
512
512
 
513
- The Magento 2 module ([`sdk/php/magento`](../php/magento/README.md)) prints
513
+ The Magento 2 module ([guide](https://recapi.io/docs/magento)) prints
514
514
  the SDK on every storefront page for views and add-to-carts, and sends
515
515
  orders from the server with the PHP library. With consent, its script
516
516
  mirrors the visitor's id and clicked recommendations into a first-party
@@ -644,7 +644,7 @@ the library as is.
644
644
 
645
645
  ### Showing why an item is listed
646
646
 
647
- Every item carries `why`, a short [reason code](../../docs/api.md#reason-codes)
647
+ Every item carries `why`, a short [reason code](https://recapi.io/docs/api#reason-codes)
648
648
  that names no item of the visitor's history, so any page can show it. Pick
649
649
  your own words, and show no label for a code you don't know (new ones may
650
650
  come):
@@ -683,13 +683,13 @@ for (const item of items) {
683
683
  ### Client methods
684
684
 
685
685
  - **`track(itemId, level, { recommendationId? })`**: `level` is any integer
686
- 0–10 of the [recommended scale](../../docs/api.md#interaction-levels) (3
686
+ 0–10 of the [recommended scale](https://recapi.io/docs/api#interaction-levels) (3
687
687
  opened, 5 strong interest, 10 the action that matters most), or one of its
688
688
  shop names `'view'`, `'cart'` and `'purchase'` (3, 5 and 10). `'reject'`
689
689
  (or any level -1 to -10) is an explicit "not interested": the item is
690
690
  never recommended to this user again until a positive event on it. Use
691
691
  it for a rejection control only, never for skips or bounces
692
- ([Rejections](../../docs/api.md#rejections)). Updates the
692
+ ([Rejections](https://recapi.io/docs/api#rejections)). Updates the
693
693
  local history at once and queues the event with `anon_id`, `user_id`, a
694
694
  timestamp and a fresh `event_id` (the idempotency
695
695
  key, reused on retries). Level 0 means an impression: it is sent but does
@@ -702,7 +702,7 @@ for (const item of items) {
702
702
  - **`recommend({ count, tagsAny, tagsAll, exclude, contextItemId, placement, discovery })`**
703
703
  resolves to `{ recommendationId, fallback, items: [{ itemId, score, slot, why, metadata }] }`.
704
704
  `metadata` is the object you stored with the item
705
- ([Item metadata](../../docs/api.md#item-metadata): name, picture, link,
705
+ ([Item metadata](https://recapi.io/docs/api#item-metadata): name, picture, link,
706
706
  price…), absent when it has none, so you can render the list without a
707
707
  lookup. `why` is the item's [reason code](#showing-why-an-item-is-listed).
708
708
  It never retries and waits at most 1.5 s (less if `timeoutMs` is lower):
@@ -747,7 +747,7 @@ const [similar, more] = await recapi.recommendLists({
747
747
  });
748
748
  ```
749
749
 
750
- ### Errors
750
+ ### Errors and troubleshooting
751
751
 
752
752
  Every failure is a `RecapiError` with `code`, `status` and `message`. The
753
753
  API error body supplies these when present. Client-side codes are
@@ -758,6 +758,14 @@ transient failures apart. `recommend()` and `popular()` resolve with a
758
758
  list instead of rejecting on `network`, `timeout`, 5xx, 429, 401 and 403
759
759
  errors, and report the error to `onError` (see [Fallback](#fallback)).
760
760
 
761
+ An API error also carries `docsUrl`, the address of its section on the
762
+ errors page, and its `message` ends with it, so the console line is a link
763
+ (absent from an older server and on client codes).
764
+
765
+ Each error code, its cause and its fix: [Errors](https://recapi.io/docs/errors).
766
+ No lists yet, empty lists, only popular items, key errors: [Troubleshooting](https://recapi.io/docs/troubleshooting).
767
+ Shops: the [Shopify](https://recapi.io/docs/shopify), [WooCommerce](https://recapi.io/docs/woocommerce) and [Magento](https://recapi.io/docs/magento) guides.
768
+
761
769
  ### Fallback
762
770
 
763
771
  When recapi does not answer `recommend()` within 1.5 s, cannot be
@@ -803,8 +811,8 @@ untouched. An empty or `null` `contextItemId` just means none.
803
811
 
804
812
  ### History
805
813
 
806
- The history follows [docs/history-spec.md](../../docs/history-spec.md)
807
- exactly, and the shared vectors in `spec/history_vectors.json` check it.
814
+ The history follows the history spec all recapi libraries share, and its
815
+ test vectors check it.
808
816
  Each item has a score that decays with a 7-day half-life. The SDK keeps
809
817
  the 50 heaviest items plus an ordered list of the 20 most recent distinct
810
818
  items. Requests carry `history: [[item_id, weight, age_s], …]` and
@@ -1044,6 +1052,11 @@ minifier, to keep zero dev dependencies.
1044
1052
 
1045
1053
  ## Changes
1046
1054
 
1055
+ - 2026-11-19: version 0.11.2. An API error's `docsUrl` is the address of
1056
+ its section on the errors page, and its message ends with it.
1057
+ - 2026-11-19: version 0.11.1. No code change: this page's links lead to
1058
+ the public docs, and an errors section names the errors and
1059
+ troubleshooting pages.
1047
1060
  - 2026-11-19: version 0.11.0. The Shopify snippet's product pages get a
1048
1061
  second row, bought together, in the same call (`boughtTogether` turns it
1049
1062
  off or retitles it), and a refused top-up keeps the cards already drawn.
@@ -1147,7 +1160,7 @@ minifier, to keep zero dev dependencies.
1147
1160
 
1148
1161
  ```sh
1149
1162
  npm test # node --test, zero dev dependencies
1150
- ../../scripts/node18.sh --test test/*.test.js # the same on Node 18, the floor
1163
+ "$(git rev-parse --show-toplevel)/scripts/node18.sh" --test test/*.test.js # the same on Node 18, the floor
1151
1164
  ```
1152
1165
 
1153
1166
  `make test` and CI run the tests on Node 18 as well as the current Node,
@@ -1157,7 +1170,7 @@ means changing `engines`, this README's first lines and
1157
1170
  `docs/development.md` together.
1158
1171
 
1159
1172
  The same steps in real browsers (Chromium, Firefox, WebKit): see
1160
- [Testing](../../docs/development.md#testing).
1173
+ "Testing" in the repository's development guide.
1161
1174
 
1162
1175
  The API embeds `src/*.js` (module `recapi/sdkjs`, `embed.go`) and serves
1163
1176
  them at `/sdk/<version>/`, where browsers cache every file but `index.js` for
@@ -1168,7 +1181,7 @@ each file's exports), and change the `/sdk/<version>/` paths in this
1168
1181
  README and `docs/api.md` (the quick start follows by itself). Never edit
1169
1182
  an existing entry of `published.json`.
1170
1183
  Once production runs that version, the release service has GitHub publish
1171
- it to npm as `@recapi/client` ([deployment.md](../../docs/deployment.md#publishing-the-javascript-library)):
1184
+ it to npm as `@recapi/client`:
1172
1185
  nobody runs `npm publish` by hand.
1173
1186
 
1174
1187
  Every older path, whatever its version number, redirects to the new
package/index.d.ts CHANGED
@@ -192,7 +192,7 @@ export class RecapiError extends Error {
192
192
  constructor(
193
193
  code: string,
194
194
  message: string,
195
- extra?: { status?: number; retryAfterMs?: number; details?: unknown; cause?: unknown }
195
+ extra?: { status?: number; retryAfterMs?: number; details?: unknown; cause?: unknown; docsUrl?: string }
196
196
  );
197
197
  readonly name: 'RecapiError';
198
198
  /** API code (`invalid_request`, `rate_limited`, …) or client code (`network`, `timeout`, `invalid_response`, `event_rejected`, `queue_overflow`). */
@@ -201,6 +201,8 @@ export class RecapiError extends Error {
201
201
  status: number;
202
202
  retryAfterMs?: number;
203
203
  details?: unknown;
204
+ /** The errors page's section for this code, sent by the API (absent from older servers and on client codes). `message` ends with it. */
205
+ docsUrl?: string;
204
206
  readonly retryable: boolean;
205
207
  }
206
208
 
package/package.json CHANGED
@@ -1,7 +1,11 @@
1
1
  {
2
2
  "name": "@recapi/client",
3
- "version": "0.11.0",
3
+ "version": "0.11.2",
4
4
  "description": "Browser and Node client for the recapi recommendation API: local decayed history, event batching, attribution.",
5
+ "homepage": "https://recapi.io/docs",
6
+ "bugs": {
7
+ "url": "https://recapi.io/contact"
8
+ },
5
9
  "type": "module",
6
10
  "main": "./src/index.js",
7
11
  "types": "./index.d.ts",
package/src/transport.js CHANGED
@@ -38,15 +38,19 @@ export class RecapiError extends Error {
38
38
  * or a client code (`network`, `timeout`, `invalid_response`,
39
39
  * `event_rejected`, `queue_overflow`).
40
40
  * @param {string} message
41
- * @param {{ status?: number, retryAfterMs?: number, details?: unknown, cause?: unknown }} [extra]
41
+ * @param {{ status?: number, retryAfterMs?: number, details?: unknown, cause?: unknown, docsUrl?: string }} [extra]
42
42
  */
43
- constructor(code, message, { status = 0, retryAfterMs, details, cause } = {}) {
44
- super(message, cause === undefined ? undefined : { cause });
43
+ constructor(code, message, { status = 0, retryAfterMs, details, cause, docsUrl } = {}) {
44
+ // The address ends the message: a browser console makes it a link in
45
+ // the line the error prints, and an onError that logs message gets it too.
46
+ super(docsUrl ? `${message}, see ${docsUrl}` : message, cause === undefined ? undefined : { cause });
45
47
  this.name = 'RecapiError';
46
48
  this.code = code;
47
49
  this.status = status;
48
50
  this.retryAfterMs = retryAfterMs;
49
51
  this.details = details;
52
+ /** The errors page's section for this code, from the API's body; undefined when it sent none. */
53
+ this.docsUrl = docsUrl || undefined;
50
54
  }
51
55
 
52
56
  /**
@@ -162,7 +166,10 @@ function httpError(response, json) {
162
166
  const status = response.status;
163
167
  const code = json?.error?.code || CODE_BY_STATUS[status] || (status >= 500 ? 'internal' : 'http_error');
164
168
  const message = json?.error?.message || `HTTP ${status}`;
165
- return new RecapiError(code, message, { status, retryAfterMs: retryAfter(response) });
169
+ // Only a string: an older server sends none, and a proxy's odd body must
170
+ // not put an object in the message.
171
+ const docsUrl = typeof json?.error?.docs_url === 'string' ? json.error.docs_url : undefined;
172
+ return new RecapiError(code, message, { status, retryAfterMs: retryAfter(response), docsUrl });
166
173
  }
167
174
 
168
175
  function retryAfter(response) {