@konfiqo/storefront-sdk 0.7.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
@@ -147,10 +147,11 @@ right** (N = the number of your proxies that append to it). Never the first
147
147
  (leftmost) `X-Forwarded-For` entry: the client writes that one, so any
148
148
  buyer could pose as any IP and dodge throttling.
149
149
 
150
- Sends `x-konfiqo-storefront-key` and `x-konfiqo-buyer-ip` so throttling and
151
- the payment provider see the buyer instead of your server. Create one client
152
- per incoming request. Without `trustedStorefrontKey` or `serverKey`, neither
153
- header is ever sent (`buyerIp` is ignored).
150
+ Sends `x-konfiqo-storefront-key`, `x-konfiqo-buyer-ip` and
151
+ `x-konfiqo-buyer-user-agent` so throttling and the payment provider see the
152
+ buyer instead of your server. Create one client per incoming request. Without
153
+ `trustedStorefrontKey` or `serverKey`, none of these headers is ever sent
154
+ (`buyerIp` and `buyerUserAgent` are ignored).
154
155
 
155
156
  The constructor **throws** when `trustedStorefrontKey` is given in a
156
157
  browser-like host: `window` or `document` defined, or a web worker. Hosts
@@ -159,15 +160,16 @@ Native) are refused too; keep the trusted key in a plain server process.
159
160
 
160
161
  ## Client options
161
162
 
162
- | Option | Default | |
163
- | ---------------------- | -------------- | ---------------------------------------------------------- |
164
- | `baseUrl` | none | `https://<store domain>/api`, or `/api` on the same origin |
165
- | `publishableKey` | none | `pk_live_…` / `pk_test_…` |
166
- | `fetch` | global `fetch` | any `fetch`-compatible function |
167
- | `timeoutMs` | `15000` | per request; on expiry the request is aborted → `timeout` |
168
- | `trustedStorefrontKey` | none | server only, see above |
169
- | `serverKey` | none | server only, see Server usage |
170
- | `buyerIp` | none | required with `serverKey` or `trustedStorefrontKey` |
163
+ | Option | Default | |
164
+ | ---------------------- | -------------- | -------------------------------------------------------------- |
165
+ | `baseUrl` | none | `https://<store domain>/api`, or `/api` on the same origin |
166
+ | `publishableKey` | none | `pk_live_…` / `pk_test_…` |
167
+ | `fetch` | global `fetch` | any `fetch`-compatible function |
168
+ | `timeoutMs` | `15000` | per request; on expiry the request is aborted → `timeout` |
169
+ | `trustedStorefrontKey` | none | server only, see above |
170
+ | `serverKey` | none | server only, see Server usage |
171
+ | `buyerIp` | none | required with `serverKey` or `trustedStorefrontKey` |
172
+ | `buyerUserAgent` | none | optional; sent only with `serverKey` or `trustedStorefrontKey` |
171
173
 
172
174
  **`pk_test_` is not a test mode**: it places real orders against the
173
175
  tenant's payment connection. Test against a store whose payments are in the
@@ -181,34 +183,36 @@ with the response body or rejects with a [`StorefrontError`](#errors).
181
183
  Path arguments (slugs, ids) that are empty, `.` or `..` reject with a
182
184
  `TypeError` (`invalid_path_segment`) without a request.
183
185
 
184
- | Method | Route | Resolves with |
185
- | ----------------------------------------------------------------------------- | ------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------- |
186
- | `store.get()` | `GET /v1/store` | `Store`: name, `defaultLanguage`, `currency`, `logo`/`logoUrl`, `theme` (`ThemeV1`, as stored), contact, `company`, shipping rules |
187
- | `categories.list()` | `GET /v1/categories` | `{ items: Category[] }` (active tree, sorted) |
188
- | `products.list({ category?, q?, page?, pageSize? })` | `GET /v1/products` | `{ items: Product[], pagination }` (`pageSize` 1–100, default 20) |
189
- | `products.get(slug)` | `GET /v1/products/:slug` | `Product` |
190
- | `blog.list({ page?, pageSize? })` | `GET /v1/blog` | `{ items: BlogPostSummary[], pagination }` (newest first, `pageSize` 1–50, default 12) |
191
- | `blog.get(slug)` | `GET /v1/blog/:slug` | `BlogPost`: the summary fields, `metaDescription` and the body `tree` |
192
- | `carts.create()` | `POST /v1/carts` | `Cart` |
193
- | `carts.get(cartId)` | `GET /v1/carts/:id` | `Cart` |
194
- | `carts.addLine(cartId, { variantId, quantity, personalization? })` | `POST /v1/carts/:id/lines` | `Cart` |
195
- | `carts.updateLine(cartId, lineId, { quantity })` | `PATCH /v1/carts/:id/lines/:lineId` | `Cart` |
196
- | `carts.removeLine(cartId, lineId)` | `DELETE /v1/carts/:id/lines/:lineId` | `Cart` |
197
- | `carts.setCustomer(cartId, { email, name, surname, phone, identityNumber? })` | `PUT /v1/carts/:id/customer` | `Cart` |
198
- | `carts.setAddress(cartId, { shipping, billing? })` | `PUT /v1/carts/:id/address` | `Cart` |
199
- | `carts.shippingOptions(cartId)` | `GET /v1/carts/:id/shipping-options` | `{ items: { id, priceMinor }[] }` |
200
- | `carts.setShippingOption(cartId, optionId)` | `PUT /v1/carts/:id/shipping-option` | `Cart` |
201
- | `carts.agreements(cartId)` | `POST /v1/carts/:id/agreements` | `CartAgreements` (see [Legal texts and agreements](#legal-texts-and-agreements)) |
202
- | `carts.checkout(cartId, { agreementsHash, turnstileToken? })` | `POST /v1/carts/:id/checkout` | `CheckoutResult` (see [Checkout challenge](#checkout-challenge)) |
203
- | `orders.get(orderId, token)` | `GET /v1/orders/:id?token=` | `Order` |
204
- | `carts.recover(token)` | `POST /v1/carts/recover` | `{ cartId }`: a **new** cart with the abandoned cart's lines (see [Accounts](#accounts-and-cart-recovery)) |
205
- | `account.requestCode(email, { turnstileToken? })` | `POST /v1/account/code` | nothing (202 for any address) |
206
- | `account.signIn(email, code)` | `POST /v1/account/session` | `{ customer: { email } }`; sets the session cookie |
207
- | `account.signOut()` | `DELETE /v1/account/session` | nothing |
208
- | `account.get(page?)` | `GET /v1/account` | `Account`: profile, `savedAddresses`, one page of `orders` (20, newest first) |
209
- | `account.updateProfile({ name, surname, phone })` | `PUT /v1/account/profile` | nothing |
210
- | `account.saveAddresses(list)` | `PUT /v1/account/addresses` | `SavedAddress[]` as stored (max 5, one default) |
211
- | `media.url(key)` | none | absolute image URL (synchronous) |
186
+ | Method | Route | Resolves with |
187
+ | ----------------------------------------------------------------------------- | ---------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
188
+ | `store.get()` | `GET /v1/store` | `Store`: name, `defaultLanguage`, `currency`, `logo`/`logoUrl`, `theme` (`ThemeV1`, as stored), contact, `company`, shipping rules |
189
+ | `categories.list()` | `GET /v1/categories` | `{ items: Category[] }` (active tree, sorted) |
190
+ | `products.list({ category?, q?, page?, pageSize? })` | `GET /v1/products` | `{ items: Product[], pagination }` (`pageSize` 1–100, default 20) |
191
+ | `products.get(slug)` | `GET /v1/products/:slug` | `Product` |
192
+ | `blog.list({ page?, pageSize? })` | `GET /v1/blog` | `{ items: BlogPostSummary[], pagination }` (newest first, `pageSize` 1–50, default 12) |
193
+ | `blog.get(slug)` | `GET /v1/blog/:slug` | `BlogPost`: the summary fields, `metaDescription` and the body `tree` |
194
+ | `carts.create()` | `POST /v1/carts` | `Cart` |
195
+ | `carts.get(cartId)` | `GET /v1/carts/:id` | `Cart` |
196
+ | `carts.addLine(cartId, { variantId, quantity, personalization? })` | `POST /v1/carts/:id/lines` | `Cart` |
197
+ | `carts.updateLine(cartId, lineId, { quantity })` | `PATCH /v1/carts/:id/lines/:lineId` | `Cart` |
198
+ | `carts.removeLine(cartId, lineId)` | `DELETE /v1/carts/:id/lines/:lineId` | `Cart` |
199
+ | `carts.setCustomer(cartId, { email, name, surname, phone, identityNumber? })` | `PUT /v1/carts/:id/customer` | `Cart` |
200
+ | `carts.setAddress(cartId, { shipping, billing? })` | `PUT /v1/carts/:id/address` | `Cart` |
201
+ | `carts.shippingOptions(cartId)` | `GET /v1/carts/:id/shipping-options` | `{ items: { id, priceMinor }[] }` |
202
+ | `carts.setShippingOption(cartId, optionId)` | `PUT /v1/carts/:id/shipping-option` | `Cart` |
203
+ | `carts.agreements(cartId)` | `POST /v1/carts/:id/agreements` | `CartAgreements` (see [Legal texts and agreements](#legal-texts-and-agreements)) |
204
+ | `carts.checkout(cartId, { agreementsHash, turnstileToken? })` | `POST /v1/carts/:id/checkout` | `CheckoutResult` (see [Checkout challenge](#checkout-challenge)) |
205
+ | `orders.get(orderId, token)` | `GET /v1/orders/:id?token=` | `Order` |
206
+ | `orders.createReturn(orderId, token, { lines, note? })` | `POST /v1/orders/:id/returns` | `OrderReturn` (see [Returns](#returns)); the token goes in the body |
207
+ | `orders.cancelReturn(orderId, returnId, token)` | `POST /v1/orders/:id/returns/:returnId/cancel` | `OrderReturn`, now `cancelled` |
208
+ | `carts.recover(token)` | `POST /v1/carts/recover` | `{ cartId }`: a **new** cart with the abandoned cart's lines (see [Accounts](#accounts-and-cart-recovery)) |
209
+ | `account.requestCode(email, { turnstileToken? })` | `POST /v1/account/code` | nothing (202 for any address) |
210
+ | `account.signIn(email, code)` | `POST /v1/account/session` | `{ customer: { email } }`; sets the session cookie |
211
+ | `account.signOut()` | `DELETE /v1/account/session` | nothing |
212
+ | `account.get(page?)` | `GET /v1/account` | `Account`: profile, `savedAddresses`, one page of `orders` (20, newest first) |
213
+ | `account.updateProfile({ name, surname, phone })` | `PUT /v1/account/profile` | nothing |
214
+ | `account.saveAddresses(list)` | `PUT /v1/account/addresses` | `SavedAddress[]` as stored (max 5, one default) |
215
+ | `media.url(key)` | none | absolute image URL (synchronous) |
212
216
 
213
217
  ### Accounts and cart recovery
214
218
 
@@ -284,6 +288,36 @@ API that returns them; an older server omits them, and against one treat `id`
284
288
  as `variantId`, `personalization` as null and `personalizationValid` as true.
285
289
  Order lines carry `personalization?: string | null`.
286
290
 
291
+ ### Returns
292
+
293
+ `order.returnable` says whether the buyer can open a return now:
294
+ `{ eligible, reason, deadline }`. `deadline` is 14 days after the latest
295
+ delivery, or null when no delivery date is known. Show the return form only
296
+ when `eligible` is true. Each order line has an `id` (the `lineId` to send) and
297
+ `returnableQuantity` (units still open for return). Lines with a
298
+ personalization text cannot be returned.
299
+
300
+ ```ts
301
+ const returned = await client.orders.createReturn(order.id, token, {
302
+ lines: [{ lineId: order.lines[0].id, quantity: 1, reason: 'defective' }],
303
+ note: 'The lid is cracked.',
304
+ });
305
+ ```
306
+
307
+ `order.returns` lists the returns, oldest first. A return starts as
308
+ `requested`. The store may approve it at once or leave that to the seller.
309
+ From `approved` on, the return carries `carrier`, `returnCode` and
310
+ `instructions` for sending the parcel back. Later statuses are `received`,
311
+ `refunded`, `rejected` (see `rejectionReason`) and `cancelled`. The buyer can
312
+ cancel while a return is `requested` or `approved`.
313
+
314
+ Errors: `invalid_return` (400) for a line that is not of this order, a repeated
315
+ line or too many units. `return_not_allowed` (409) when the order takes no
316
+ return now; `details.reason` is one of `unpaid`, `refunded`, `cancelled`,
317
+ `window_closed`, `open_return`, `too_many_returns` or `nothing_left`, and it is
318
+ absent when a cancel is refused because the return is no longer open.
319
+ `return_not_found` (404) for a return of another order.
320
+
287
321
  **Customer.** `phone` must be a Turkish mobile number (`05XX…`, `5XX…`,
288
322
  `+90…`, `0090…`; spaces, dashes, parentheses allowed); it comes back as
289
323
  `90XXXXXXXXXX`. Addresses use `{ name?, phone?, email?, address | line1 +
package/dist/index.cjs CHANGED
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  BUYER_IP_HEADER: () => BUYER_IP_HEADER,
24
+ BUYER_USER_AGENT_HEADER: () => BUYER_USER_AGENT_HEADER,
24
25
  DEFAULT_TIMEOUT_MS: () => DEFAULT_TIMEOUT_MS,
25
26
  LEGAL_KEYS: () => LEGAL_KEYS,
26
27
  PUBLISHABLE_KEY_HEADER: () => PUBLISHABLE_KEY_HEADER,
@@ -94,6 +95,9 @@ var API_CODES = /* @__PURE__ */ new Set([
94
95
  "too_many_code_requests",
95
96
  "invalid_recovery_token",
96
97
  "cart_not_recoverable",
98
+ "invalid_return",
99
+ "return_not_allowed",
100
+ "return_not_found",
97
101
  "unavailable"
98
102
  ]);
99
103
  var ERROR_BRAND = /* @__PURE__ */ Symbol.for("@konfiqo/storefront-sdk/error");
@@ -208,6 +212,13 @@ function apiError(code, status, message, b) {
208
212
  return new StorefrontError(code, status, message, {
209
213
  status: b.status === "ordered" ? "ordered" : "expired"
210
214
  });
215
+ case "return_not_allowed":
216
+ return new StorefrontError(
217
+ code,
218
+ status,
219
+ message,
220
+ typeof b.reason === "string" ? { reason: b.reason } : {}
221
+ );
211
222
  default:
212
223
  return new StorefrontError(
213
224
  code,
@@ -225,6 +236,7 @@ var STOREFRONT_KEY_HEADER = "x-konfiqo-storefront-key";
225
236
  var SERVER_KEY_HEADER = "x-konfiqo-server-key";
226
237
  var TURNSTILE_TOKEN_HEADER = "x-konfiqo-turnstile-token";
227
238
  var BUYER_IP_HEADER = "x-konfiqo-buyer-ip";
239
+ var BUYER_USER_AGENT_HEADER = "x-konfiqo-buyer-user-agent";
228
240
  var REOPEN_AGREEMENTS_HASH = "0".repeat(64);
229
241
  function seg(v) {
230
242
  if (typeof v !== "string" || v === "" || v === "." || v === "..") {
@@ -289,6 +301,10 @@ function createStorefrontClient(options) {
289
301
  baseHeaders[STOREFRONT_KEY_HEADER] = options.trustedStorefrontKey;
290
302
  baseHeaders[BUYER_IP_HEADER] = buyerIp;
291
303
  }
304
+ const buyerUserAgent = options.buyerUserAgent?.trim();
305
+ if (buyerUserAgent && (options.serverKey !== void 0 || options.trustedStorefrontKey !== void 0)) {
306
+ baseHeaders[BUYER_USER_AGENT_HEADER] = buyerUserAgent;
307
+ }
292
308
  async function request(method, path, body, callOptions, extraHeaders = {}, flags = {}) {
293
309
  const url = `${baseUrl}${path()}`;
294
310
  const callerSignal = callOptions?.signal;
@@ -476,6 +492,22 @@ function createStorefrontClient(options) {
476
492
  get: (orderId, token, o) => get(
477
493
  () => `/v1/orders/${seg(orderId)}?token=${encodeURIComponent(token)}`,
478
494
  o
495
+ ),
496
+ createReturn: (orderId, token, input, o) => request(
497
+ "POST",
498
+ () => `/v1/orders/${seg(orderId)}/returns`,
499
+ {
500
+ token,
501
+ lines: input.lines,
502
+ ...input.note !== void 0 && { note: input.note }
503
+ },
504
+ o
505
+ ),
506
+ cancelReturn: (orderId, returnId, token, o) => request(
507
+ "POST",
508
+ () => `/v1/orders/${seg(orderId)}/returns/${seg(returnId)}/cancel`,
509
+ { token },
510
+ o
479
511
  )
480
512
  },
481
513
  account: {
@@ -817,10 +849,11 @@ async function renderTurnstile(element, siteKey, options) {
817
849
  }
818
850
 
819
851
  // src/index.ts
820
- var SDK_VERSION = "0.7.0";
852
+ var SDK_VERSION = "0.9.0";
821
853
  // Annotate the CommonJS export names for ESM import in node:
822
854
  0 && (module.exports = {
823
855
  BUYER_IP_HEADER,
856
+ BUYER_USER_AGENT_HEADER,
824
857
  DEFAULT_TIMEOUT_MS,
825
858
  LEGAL_KEYS,
826
859
  PUBLISHABLE_KEY_HEADER,