@recapi/client 0.6.4 → 0.6.5
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 +18 -5
- package/package.json +1 -1
- package/src/client.js +53 -17
package/README.md
CHANGED
|
@@ -35,7 +35,7 @@ are, gzipped, and browsers cache them for a year:
|
|
|
35
35
|
|
|
36
36
|
```html
|
|
37
37
|
<script type="module">
|
|
38
|
-
import { createClient } from 'https://api.example.com/sdk/0.6.
|
|
38
|
+
import { createClient } from 'https://api.example.com/sdk/0.6.5/index.js';
|
|
39
39
|
|
|
40
40
|
const recapi = createClient({ apiKey: 'rk_pub_…', baseUrl: 'https://api.example.com' });
|
|
41
41
|
recapi.track('item-42', 'view'); // and the calls of the quick start below
|
|
@@ -521,10 +521,11 @@ for (const item of items) {
|
|
|
521
521
|
local history at once and queues the event with `anon_id`, `user_id`, a
|
|
522
522
|
timestamp and a fresh `event_id` (the idempotency
|
|
523
523
|
key, reused on retries). Level 0 means an impression: it is sent but does
|
|
524
|
-
not change the history. Throws `RecapiError('invalid_request')` on bad
|
|
525
|
-
|
|
524
|
+
not change the history. Throws `RecapiError('invalid_request')` on a bad
|
|
525
|
+
level. An item id follows the API's rules, 1–256 bytes of UTF-8
|
|
526
526
|
(128 characters of Cyrillic or accented Latin, 85 of CJK) without
|
|
527
|
-
control characters
|
|
527
|
+
control characters; any other drops the event and reports it instead of
|
|
528
|
+
throwing, as a visitor's URL can carry one (see [Fallback](#fallback)). A stored history entry whose id breaks the rule,
|
|
528
529
|
left by an earlier SDK version, is dropped when the page loads.
|
|
529
530
|
- **`recommend({ count, tagsAny, tagsAll, exclude, contextItemId, placement, discovery })`**
|
|
530
531
|
resolves to `{ recommendationId, fallback, items: [{ itemId, score, slot, why, metadata }] }`.
|
|
@@ -595,9 +596,18 @@ live site with no deploy. Without an `onError`, they print one `console.warn`
|
|
|
595
596
|
line a minute per error code, with the API's message; the dashboard warns of a
|
|
596
597
|
limit as well.
|
|
597
598
|
|
|
599
|
+
A page often takes a product id from a visitor's URL, so a crafted link can
|
|
600
|
+
carry an id the API would refuse (over 256 bytes of UTF-8, a control
|
|
601
|
+
character, not a string at all). Such a `contextItemId` is never sent:
|
|
602
|
+
`recommend()` asks only for popular items and answers as above, with
|
|
603
|
+
`invalid_request` going to `onError` or the same line, which never quotes the id.
|
|
604
|
+
`track()` with such an `itemId` drops that event alone and reports it the same
|
|
605
|
+
way: it doesn't throw, and the history and the batch's other events are
|
|
606
|
+
untouched. An empty or `null` `contextItemId` just means none.
|
|
607
|
+
|
|
598
608
|
`recommend()` and `popular()` still reject when:
|
|
599
609
|
|
|
600
|
-
- the request is wrong: 400, 404, 413 and any other 3xx or 4xx. These are
|
|
610
|
+
- the request is wrong: another 400, 404, 413 and any other 3xx or 4xx. These are
|
|
601
611
|
integration bugs that a developer sees on the first run;
|
|
602
612
|
- the response is malformed (`invalid_response`), for example a `baseUrl`
|
|
603
613
|
that points at your own site.
|
|
@@ -828,6 +838,9 @@ minifier, to keep zero dev dependencies.
|
|
|
828
838
|
|
|
829
839
|
## Changes
|
|
830
840
|
|
|
841
|
+
- 2026-11-05: version 0.6.5. A visitor's URL can't make `recommend()` or
|
|
842
|
+
`track()` throw: a `contextItemId` the API would refuse gets popular items
|
|
843
|
+
with no recommendation request, and such an `itemId` drops only that event.
|
|
831
844
|
- 2026-11-04: version 0.6.4. `recommend()` no longer rejects on a limit
|
|
832
845
|
(429) or a refused key (401, 403), and `popular()` no longer rejects on an
|
|
833
846
|
outage, a limit or a refused key: the page gets popular items or an empty
|
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -423,6 +423,24 @@ export function createClient(options) {
|
|
|
423
423
|
.map((item) => toItem(item, 'backfill'));
|
|
424
424
|
}
|
|
425
425
|
|
|
426
|
+
/** What `recommend()` answers instead of a list of its own: popular items, or none. */
|
|
427
|
+
async function listInstead(error, params, deadline) {
|
|
428
|
+
reportList(error);
|
|
429
|
+
let items = [];
|
|
430
|
+
// A refused key would be refused the popular list too.
|
|
431
|
+
if (!keyRefused(error)) {
|
|
432
|
+
try {
|
|
433
|
+
items = await popularFallback(params, deadline);
|
|
434
|
+
} catch (fallbackError) {
|
|
435
|
+
// Down altogether, or the limit covers popular too. The page awaits
|
|
436
|
+
// recommend() without a catch (the README says it may), so an empty
|
|
437
|
+
// list it can hide beats a rejection that breaks it.
|
|
438
|
+
reportList(fallbackError);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
return { recommendationId: null, fallback: true, items };
|
|
442
|
+
}
|
|
443
|
+
|
|
426
444
|
/**
|
|
427
445
|
* `GET /v1/popular` for `popular()` and the fallback; `deadline` defaults
|
|
428
446
|
* to `timeoutMs`. A public key goes in the query string (CDN-cacheable, no
|
|
@@ -462,6 +480,8 @@ export function createClient(options) {
|
|
|
462
480
|
* interested"): the item is never recommended to this user again until
|
|
463
481
|
* a positive event on it. Don't send skips or bounces as rejections.
|
|
464
482
|
* Does nothing under Do Not Track (see `respectDoNotTrack`).
|
|
483
|
+
* An `itemId` the API would refuse (a page often takes it from a
|
|
484
|
+
* visitor's URL) drops the event and goes to `onError`, without throwing.
|
|
465
485
|
* @param {{ recommendationId?: string | null }} [options] Overrides the
|
|
466
486
|
* automatic attribution. `null` (a fallback list's id) is no override.
|
|
467
487
|
*/
|
|
@@ -470,7 +490,6 @@ export function createClient(options) {
|
|
|
470
490
|
// A fallback list has a null id, and a page passing it on must not
|
|
471
491
|
// send `null` or lose the item's earlier, real attribution.
|
|
472
492
|
const recommendationId = explicit ?? undefined;
|
|
473
|
-
if (!isId(itemId)) throw new RecapiError('invalid_request', 'itemId must be 1-256 bytes of UTF-8 without control characters');
|
|
474
493
|
if (typeof level === 'string' && Object.hasOwn(LEVEL_NAMES, level)) level = LEVEL_NAMES[level];
|
|
475
494
|
if (!Number.isInteger(level) || level < -10 || level > 10) {
|
|
476
495
|
throw new RecapiError(
|
|
@@ -478,6 +497,13 @@ export function createClient(options) {
|
|
|
478
497
|
"level must be an integer between -10 and 10, or 'view', 'cart', 'purchase' or 'reject'"
|
|
479
498
|
);
|
|
480
499
|
}
|
|
500
|
+
// A page often takes this id from a visitor's URL, so a crafted link
|
|
501
|
+
// must not fail it. Nothing is kept: in the history it would make the
|
|
502
|
+
// API refuse every later list. The line never quotes the visitor's id.
|
|
503
|
+
if (!isId(itemId)) {
|
|
504
|
+
report(new RecapiError('invalid_request', idRule('itemId')), true);
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
481
507
|
if (quiet) return;
|
|
482
508
|
const t = now();
|
|
483
509
|
let attributed = recommendationId;
|
|
@@ -507,8 +533,10 @@ export function createClient(options) {
|
|
|
507
533
|
* `fallback: true` and no recommendation id, and reports the error to
|
|
508
534
|
* `onError`. When the popular request fails too, the list is empty: hide
|
|
509
535
|
* the slot. On a 401 or 403 (a revoked key, a suspended shop) the list is
|
|
510
|
-
* empty at once.
|
|
511
|
-
*
|
|
536
|
+
* empty at once. A `contextItemId` the API would refuse (a page often takes
|
|
537
|
+
* it from a visitor's URL) is never sent: the answer is the same popular
|
|
538
|
+
* list. Other errors still reject: a bad request or a reply that isn't the
|
|
539
|
+
* API's can't start on a live site without a deploy.
|
|
512
540
|
*
|
|
513
541
|
* @param {{ count?: number, tagsAny?: string[], tagsAll?: string[], exclude?: string[],
|
|
514
542
|
* contextItemId?: string, placement?: string, discovery?: number }} [params]
|
|
@@ -518,24 +546,17 @@ export function createClient(options) {
|
|
|
518
546
|
async recommend(params = {}) {
|
|
519
547
|
start();
|
|
520
548
|
const deadline = Math.min(timeoutMs, RECOMMEND_TIMEOUT_MS);
|
|
549
|
+
if (!usableContext(params.contextItemId)) {
|
|
550
|
+
// Unlike a failed call, nothing has read the stored rejections yet.
|
|
551
|
+
if (consent) reload();
|
|
552
|
+
const error = new RecapiError('invalid_request', idRule('contextItemId'));
|
|
553
|
+
return listInstead(error, { ...params, contextItemId: undefined }, deadline);
|
|
554
|
+
}
|
|
521
555
|
try {
|
|
522
556
|
return await personalised(params, deadline);
|
|
523
557
|
} catch (error) {
|
|
524
558
|
if (!sparesThePage(error)) throw error;
|
|
525
|
-
|
|
526
|
-
let items = [];
|
|
527
|
-
// A refused key would be refused the popular list too.
|
|
528
|
-
if (!keyRefused(error)) {
|
|
529
|
-
try {
|
|
530
|
-
items = await popularFallback(params, deadline);
|
|
531
|
-
} catch (fallbackError) {
|
|
532
|
-
// Down altogether, or the limit covers popular too. The page
|
|
533
|
-
// awaits recommend() without a catch (the README says it may), so
|
|
534
|
-
// an empty list it can hide beats a rejection that breaks it.
|
|
535
|
-
reportList(fallbackError);
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
return { recommendationId: null, fallback: true, items };
|
|
559
|
+
return listInstead(error, params, deadline);
|
|
539
560
|
}
|
|
540
561
|
},
|
|
541
562
|
|
|
@@ -701,6 +722,21 @@ function toItem({ item_id: itemId, score, why, metadata }, slot) {
|
|
|
701
722
|
return item;
|
|
702
723
|
}
|
|
703
724
|
|
|
725
|
+
/** The API's rule for ids, for messages that never quote the id: a visitor may have written it. */
|
|
726
|
+
function idRule(name) {
|
|
727
|
+
return `${name} must be 1-256 bytes of UTF-8 without control characters`;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
/**
|
|
731
|
+
* Whether the API would take this as a context item: an id, or nothing (an
|
|
732
|
+
* empty string reads as no context item).
|
|
733
|
+
*
|
|
734
|
+
* @param {unknown} id
|
|
735
|
+
*/
|
|
736
|
+
function usableContext(id) {
|
|
737
|
+
return id === undefined || id === null || id === '' || isId(id);
|
|
738
|
+
}
|
|
739
|
+
|
|
704
740
|
/**
|
|
705
741
|
* Whether `recommend()` and `popular()` answer this failure with a list instead
|
|
706
742
|
* of rejecting. An outage, a limit or a refused key can start on a live site
|