@recapi/client 0.8.0 → 0.9.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.8.0/index.js';
38
+ import { createClient } from 'https://api.example.com/sdk/0.9.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).
@@ -134,7 +134,9 @@ in two places, both shown with your address in the dashboard's quick start:
134
134
  is the item id. recapi has no catalog of a Shopify shop, so the snippet
135
135
  asks for 6 and skips products that are sold out (`available: false`) or
136
136
  deleted (404). All 6 count as one served list: CTR and lift are per list,
137
- so they are unchanged. Each card shows the picture (resized by Shopify's
137
+ so they are unchanged. It asks through `recommendLists()`, the call that
138
+ takes all of a page's rows ([Several rows in one call](#several-rows-in-one-call)):
139
+ the page has one row, so it costs one call, billed as one recommendation. Each card shows the picture (resized by Shopify's
138
140
  CDN with `?width=400`), the title and `price_min` formatted with
139
141
  `Intl.NumberFormat` in `Shopify.currency.active` and the page's `lang`
140
142
  (no price if either is missing). An item whose
@@ -225,6 +227,9 @@ them needs no new SDK version (they import the served one), but it changes
225
227
  what every new Shopify shop pastes. The quick start fills in the current
226
228
  version, your address and your project (`jssdk.Shopify`).
227
229
 
230
+ Both files name themselves (`platform: 'shopify/<version>'`, sent as
231
+ `?platform=` on every call), so recapi words its setup hints for Shopify.
232
+
228
233
  ### WooCommerce
229
234
 
230
235
  [`woocommerce/recapi.php`](woocommerce/recapi.php) is a one-file WordPress
@@ -313,7 +318,12 @@ never caused), loading them in one request to WooCommerce's public Store API
313
318
  (`/wc/store/v1/products?include=…`) and skipping those not purchasable or
314
319
  out of stock. A full list that leaves a gap that way (without a secret key
315
320
  recapi may not know a product sold out) is topped up once with a second
316
- list of as many products, excluding the first's; a fallback list is not. Items whose [metadata](../../docs/api.md#item-metadata)
321
+ list of as many products, excluding the first's; a fallback list is not.
322
+ The row asks through `recommendLists()`, the call that takes all of a page's
323
+ rows, billed as one recommendation with no product on two rows ([Several
324
+ rows in one call](#several-rows-in-one-call)). A page has one recapi row (the
325
+ product page's spot, a block and `[recapi]` never show two), so a page costs
326
+ one call, two only when the top-up runs. Items whose [metadata](../../docs/api.md#item-metadata)
317
327
  has a `name` and an http(s) `url` are left out of that request and drawn
318
328
  from it, as in the Shopify snippet (a number `price` is in the shop's
319
329
  currency, `woocommerce_currency`, and shown only while the visitor browses
@@ -388,6 +398,10 @@ stands in for WordPress, run by `scripts/php.sh`) and runs the printed
388
398
  script against the SDK. The quick start fills in the current version and
389
399
  your address (`jssdk.WooCommerce`).
390
400
 
401
+ The plugin names itself, so recapi words its setup hints for WooCommerce: the
402
+ browser script sends `?platform=woocommerce/<version>`, the shop's server
403
+ calls `User-Agent: recapi-woocommerce/<version>`.
404
+
391
405
  `scripts/e2e_woocommerce.sh` (CI job `woocommerce`) runs the plugin on a
392
406
  real, throwaway shop: WordPress and WooCommerce in Docker at pinned
393
407
  versions, twice: the newest (WordPress 7.1.4, WooCommerce 11.3.0, PHP 8.5)
@@ -577,6 +591,7 @@ for (const item of items) {
577
591
  | `apiKey` | required | `rk_pub_…` in browsers |
578
592
  | `baseUrl` | required | API origin |
579
593
  | `placement` | – | default placement for `recommend()` |
594
+ | `platform` | – | `<name>/<version>` of the integration making the calls (`'woocommerce/0.9.0'`), sent as `?platform=` on every request, never as a header (it would cost a CORS preflight) |
580
595
  | `storage` | localStorage, else memory | any `{ getItem, setItem, removeItem }` synchronous store |
581
596
  | `project` | first 16 chars of `apiKey` | storage namespace: key `recapi:v1:<project>` |
582
597
  | `flushIntervalMs` | `2000` | delay before queued events are sent |
@@ -952,6 +967,9 @@ minifier, to keep zero dev dependencies.
952
967
 
953
968
  ## Changes
954
969
 
970
+ - 2026-11-15: version 0.9.0. The `platform` option names the integration
971
+ making the calls (`?platform=woocommerce/0.9.0` on every request), so recapi
972
+ can word its setup hints for the shop's platform. Unset, requests are unchanged.
955
973
  - 2026-11-14: version 0.8.0. `recommendLists()` asks for a page's rows in one
956
974
  call, billed as one recommendation ([Several rows in one call](#several-rows-in-one-call)).
957
975
  - 2026-11-11: version 0.7.0. `consent` defaults to `false`: a client stores
package/index.d.ts CHANGED
@@ -69,6 +69,11 @@ export interface ClientOptions {
69
69
  project?: string;
70
70
  /** Default placement for `recommend()`. */
71
71
  placement?: string;
72
+ /**
73
+ * `<name>/<version>` of the integration making the calls, e.g.
74
+ * `'woocommerce/0.9.0'`: sent as `?platform=` on every request.
75
+ */
76
+ platform?: string;
72
77
  /** Default 2000. */
73
78
  flushIntervalMs?: number;
74
79
  /** Default 100, at most 1000. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@recapi/client",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "Browser and Node client for the recapi recommendation API: local decayed history, event batching, attribution.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
package/src/client.js CHANGED
@@ -78,6 +78,9 @@ const PUBLIC_KEY_PREFIX = 'rk_pub_';
78
78
  * @property {string} [project] Storage namespace; defaults to the first 16
79
79
  * characters of the API key.
80
80
  * @property {string} [placement] Default placement for `recommend()`.
81
+ * @property {string} [platform] `<name>/<version>` of the integration making
82
+ * the calls (`woocommerce/0.9.0`), sent as `?platform=` on each request so
83
+ * recapi can word its setup hints for the shop's platform.
81
84
  * @property {number} [flushIntervalMs]
82
85
  * @property {number} [maxBatch]
83
86
  * @property {number} [timeoutMs] Per-request timeout.
@@ -114,6 +117,7 @@ export function createClient(options) {
114
117
  } = options ?? {};
115
118
  if (typeof apiKey !== 'string' || !apiKey) throw new TypeError('apiKey is required');
116
119
  if (typeof baseUrl !== 'string' || !baseUrl) throw new TypeError('baseUrl is required');
120
+ if (options.platform !== undefined && typeof options.platform !== 'string') throw new TypeError('platform must be a string');
117
121
  const fetchImpl = options.fetch ?? globalThis.fetch?.bind(globalThis);
118
122
  if (typeof fetchImpl !== 'function') throw new TypeError('fetch is not available; pass options.fetch');
119
123
 
@@ -152,7 +156,7 @@ export function createClient(options) {
152
156
  // A faulty error handler must not fail the page.
153
157
  }
154
158
  };
155
- const transport = createTransport({ baseUrl, apiKey, fetch: fetchImpl, timeoutMs });
159
+ const transport = createTransport({ baseUrl, apiKey, fetch: fetchImpl, timeoutMs, platform: options.platform });
156
160
  // A public key goes in the URL (no CORS preflight; popular() is CDN-cacheable),
157
161
  // any other key in the header (Node with a secret key).
158
162
  const auth = apiKey.startsWith(PUBLIC_KEY_PREFIX) ? 'query' : 'header';
package/src/transport.js CHANGED
@@ -77,10 +77,14 @@ export class RecapiError extends Error {
77
77
  */
78
78
 
79
79
  /**
80
- * @param {{ baseUrl: string, apiKey: string, fetch: typeof fetch, timeoutMs: number }} options
80
+ * `platform` goes in the URL of every request, as `?key=` does and for the
81
+ * same reason: a header of our own would make every browser call ask
82
+ * permission first.
83
+ *
84
+ * @param {{ baseUrl: string, apiKey: string, fetch: typeof fetch, timeoutMs: number, platform?: string }} options
81
85
  * @returns {{ request: (options: RequestOptions) => Promise<any> }}
82
86
  */
83
- export function createTransport({ baseUrl, apiKey, fetch, timeoutMs: defaultTimeoutMs }) {
87
+ export function createTransport({ baseUrl, apiKey, fetch, timeoutMs: defaultTimeoutMs, platform }) {
84
88
  const root = baseUrl.replace(/\/+$/, '');
85
89
 
86
90
  /** @param {RequestOptions} options */
@@ -91,6 +95,7 @@ export function createTransport({ baseUrl, apiKey, fetch, timeoutMs: defaultTime
91
95
  if (value !== undefined) params.append(name, String(value));
92
96
  }
93
97
  if (auth === 'query') params.append('key', apiKey);
98
+ if (platform) params.append('platform', platform);
94
99
  const qs = params.toString();
95
100
  const url = `${root}${path}${qs ? `?${qs}` : ''}`;
96
101