@misiki/gocommerce-connector 0.4.2 → 0.4.4

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.
@@ -214,14 +214,16 @@ var BaseService = class _BaseService {
214
214
  }
215
215
  /**
216
216
  * The store this call is for: the request host's subdomain under
217
- * `baseDomain`, else the static `store`. Read per call, never stored on the
218
- * instance, because the server shares one process (and one credentials
219
- * object) across every concurrent request and each may be for a different host.
217
+ * `baseDomain`, else the static `store`, else the host itself when it is a
218
+ * store's own domain (the engine answers only a verified one). Read per call,
219
+ * never stored on the instance, because the server shares one process (and
220
+ * one credentials object) across every concurrent request and each may be
221
+ * for a different host.
220
222
  */
221
223
  get store() {
222
224
  const { baseDomain, store } = this.creds;
223
225
  const host = typeof window !== "undefined" ? window.location?.hostname : requestHost?.();
224
- return storeForHost(host, baseDomain) ?? store;
226
+ return storeForHost(host, baseDomain) ?? store ?? ownDomain(host, baseDomain);
225
227
  }
226
228
  /**
227
229
  * The engine's base URL from wherever this code runs. In the browser a
@@ -371,6 +373,14 @@ function storeForHost(host, baseDomain) {
371
373
  const slug = h.slice(0, -suffix.length);
372
374
  return slug && !slug.includes(".") ? slug : void 0;
373
375
  }
376
+ function ownDomain(host, baseDomain) {
377
+ if (!host || !baseDomain) return void 0;
378
+ const h = host.toLowerCase().replace(/:\d+$/, "").replace(/\.$/, "");
379
+ const base = baseDomain.toLowerCase().replace(/^\.+/, "").replace(/\.$/, "");
380
+ if (!h.includes(".") || h === base || h.endsWith("." + base) || h.endsWith(".localhost")) return void 0;
381
+ if (/^[\d.]+$/.test(h) || h.includes(":") || h.startsWith("[")) return void 0;
382
+ return h;
383
+ }
374
384
  var unreachable = (base, cause) => new GoCommerceError(`Could not reach the commerce engine at ${base}: ${cause?.message ?? cause}`, 0);
375
385
  function qs(query) {
376
386
  if (!query) return "";
@@ -2015,12 +2025,31 @@ var OrderService = class extends BaseService {
2015
2025
  async listPublic(_args = {}) {
2016
2026
  return emptyPage();
2017
2027
  }
2018
- async getOrderByEmailAndOTP() {
2019
- throw new NotSupportedError(
2020
- "OrderService",
2021
- "getOrderByEmailAndOTP",
2022
- "the engine sends no one-time codes; a guest reads an order with its access token"
2028
+ /**
2029
+ * Sends a 4-digit code to the email or phone on the order, when `contact`
2030
+ * is that email or phone. The engine answers the same either way, so this
2031
+ * resolves whether or not anything was sent.
2032
+ */
2033
+ async requestOrderCode({ orderNo, contact }) {
2034
+ const res = await this.data(
2035
+ `/api/orders/${encodeURIComponent(orderNo.trim())}/code`,
2036
+ { method: "POST", body: { contact: contact.trim() } }
2037
+ );
2038
+ return { expiresInMinutes: res?.expires_in_minutes ?? 10 };
2039
+ }
2040
+ /**
2041
+ * Opens an order with the code `requestOrderCode` sent. The token it answers
2042
+ * is kept like an order placed in this browser, so the order page and
2043
+ * `getOrder` read it again without another code.
2044
+ */
2045
+ async getOrderByEmailAndOTP({ orderNo, otp }) {
2046
+ const number = orderNo.trim();
2047
+ const res = await this.data(
2048
+ `/api/orders/${encodeURIComponent(number)}/code/verify`,
2049
+ { method: "POST", body: { code: String(otp).trim() } }
2023
2050
  );
2051
+ rememberOrder({ number: res.order.number, token: res.access_token, cartId: "" });
2052
+ return { data: [toOrder(res.order)] };
2024
2053
  }
2025
2054
  };
2026
2055
  var clean2 = (v) => v && v !== "undefined" && v !== "null" ? v : void 0;
@@ -3194,6 +3223,7 @@ exports.meilisearchService = meilisearchService;
3194
3223
  exports.menuService = menuService;
3195
3224
  exports.newsletterService = newsletterService;
3196
3225
  exports.orderService = orderService;
3226
+ exports.ownDomain = ownDomain;
3197
3227
  exports.pageService = pageService;
3198
3228
  exports.paymentMethodService = paymentMethodService;
3199
3229
  exports.pluginService = pluginService;