@recapi/client 0.14.0 → 0.15.0

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
@@ -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.14.0/index.js';
38
+ import { createClient } from 'https://api.example.com/sdk/0.15.0/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).
@@ -732,7 +732,7 @@ for (const item of items) {
732
732
 
733
733
  ### Client methods
734
734
 
735
- - **`track(itemId, level, { recommendationId? })`**: `level` is any integer
735
+ - **`track(itemId, level, { recommendationId?, value?, currency? })`**: `level` is any integer
736
736
  0–10 of the [recommended scale](https://recapi.io/docs/api#interaction-levels) (3
737
737
  opened, 5 strong interest, 10 the action that matters most), or one of its
738
738
  shop names `'view'`, `'cart'` and `'purchase'` (3, 5 and 10). `'reject'`
@@ -744,7 +744,10 @@ for (const item of items) {
744
744
  timestamp and a fresh `event_id` (the idempotency
745
745
  key, reused on retries). Level 0 means an impression: it is sent but does
746
746
  not change the history. Throws `RecapiError('invalid_request')` on a bad
747
- level. An item id follows the API's rules, 1–256 bytes of UTF-8
747
+ level. A purchase may carry `{ value, currency }` (what the buyer paid for
748
+ the line, and its ISO 4217 code such as `'EUR'`), kept for the sales figure
749
+ of a lift test; a negative value, or a currency without a value or not of
750
+ three letters, throws the same error. An item id follows the API's rules, 1–256 bytes of UTF-8
748
751
  (128 characters of Cyrillic or accented Latin, 85 of CJK) without
749
752
  control characters; any other drops the event and reports it instead of
750
753
  throwing, as a visitor's URL can carry one (see [Fallback](#fallback)). A stored history entry whose id breaks the rule,
@@ -1102,6 +1105,8 @@ minifier, to keep zero dev dependencies.
1102
1105
 
1103
1106
  ## Changes
1104
1107
 
1108
+ - 2026-11-22: version 0.15.0. `track()` takes a purchase's `value` and
1109
+ `currency`, sent with the event for the sales figure of a lift test.
1105
1110
  - 2026-11-21: version 0.14.0. Shopify: a row in a full-width section
1106
1111
  (Dawn's Custom Liquid) keeps to the theme's page width, four cards across
1107
1112
  (two on a phone); the theme editor's preview shows the row and sends or
package/index.d.ts CHANGED
@@ -168,7 +168,11 @@ export interface PopularResult {
168
168
  export type LevelName = 'view' | 'cart' | 'purchase' | 'reject';
169
169
 
170
170
  export interface RecapiClient {
171
- track(itemId: string, level: number | LevelName, options?: { recommendationId?: string | null }): void;
171
+ /**
172
+ * `value` is what the buyer paid for a purchase's line (a number of at least 0; 0 is a free line), `currency` its ISO 4217 code
173
+ * (three letters, any case); a currency needs a value. An invalid pair throws `RecapiError('invalid_request')`, as a bad level does.
174
+ */
175
+ track(itemId: string, level: number | LevelName, options?: { recommendationId?: string | null; value?: number; currency?: string }): void;
172
176
  /** Falls back to popular items on a network error, a timeout, a 5xx or a 429 (an empty list if those fail too), to an empty list on a 401 or 403; rejects on a bad request or a reply that isn't the API's. */
173
177
  recommend(params?: RecommendParams): Promise<RecommendResult>;
174
178
  /** Several lists in one call, billed as one recommendation; falls back as `recommend()` does, list by list. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@recapi/client",
3
- "version": "0.14.0",
3
+ "version": "0.15.0",
4
4
  "description": "Browser and Node client for the recapi recommendation API: local decayed history, event batching, attribution.",
5
5
  "homepage": "https://recapi.io/docs",
6
6
  "bugs": {
package/src/client.js CHANGED
@@ -545,10 +545,14 @@ export function createClient(options) {
545
545
  * Does nothing under Do Not Track (see `respectDoNotTrack`).
546
546
  * An `itemId` the API would refuse (a page often takes it from a
547
547
  * visitor's URL) drops the event and goes to `onError`, without throwing.
548
- * @param {{ recommendationId?: string | null }} [options] Overrides the
549
- * automatic attribution. `null` (a fallback list's id) is no override.
548
+ * @param {{ recommendationId?: string | null, value?: number,
549
+ * currency?: string }} [options] `recommendationId` overrides the
550
+ * automatic attribution; `null` (a fallback list's id) is no override.
551
+ * `value` is what the buyer paid for a purchase's line (0 for a free
552
+ * line), `currency` its ISO 4217 code (three letters, any case); a
553
+ * currency needs a value. An invalid pair throws, as a bad level does.
550
554
  */
551
- track(itemId, level, { recommendationId: explicit } = {}) {
555
+ track(itemId, level, { recommendationId: explicit, value, currency } = {}) {
552
556
  start();
553
557
  // A fallback list has a null id, and a page passing it on must not
554
558
  // send `null` or lose the item's earlier, real attribution.
@@ -560,6 +564,7 @@ export function createClient(options) {
560
564
  "level must be an integer between -10 and 10, or 'view', 'cart', 'purchase' or 'reject'"
561
565
  );
562
566
  }
567
+ checkAmount(value, currency);
563
568
  // A page often takes this id from a visitor's URL, so a crafted link
564
569
  // must not fail it. Nothing is kept: in the history it would make the
565
570
  // API refuse every later list. The line never quotes the visitor's id.
@@ -584,6 +589,8 @@ export function createClient(options) {
584
589
  user_id: userId ?? undefined,
585
590
  anon_id: anonId ?? undefined,
586
591
  recommendation_id: attributed,
592
+ value,
593
+ currency,
587
594
  timestamp: new Date(t).toISOString(),
588
595
  event_id: uuid()
589
596
  }));
@@ -880,6 +887,22 @@ function withRejected(exclude, rejected) {
880
887
  return [...out];
881
888
  }
882
889
 
890
+ /**
891
+ * Refuses what the API would refuse in an event: a value that is not a
892
+ * finite number of at least 0, a currency that is not three ASCII letters or
893
+ * comes without a value. `undefined` is no value, no currency.
894
+ */
895
+ function checkAmount(value, currency) {
896
+ if (value !== undefined && !(typeof value === 'number' && value >= 0 && value !== Infinity)) {
897
+ throw new RecapiError('invalid_request', 'value must be a number of at least 0');
898
+ }
899
+ if (currency === undefined) return;
900
+ if (value === undefined) throw new RecapiError('invalid_request', 'currency needs a value');
901
+ if (typeof currency !== 'string' || !/^[A-Za-z]{3}$/.test(currency)) {
902
+ throw new RecapiError('invalid_request', 'currency must be three letters (ISO 4217)');
903
+ }
904
+ }
905
+
883
906
  function compact(object) {
884
907
  return Object.fromEntries(Object.entries(object).filter(([, value]) => value !== undefined));
885
908
  }