@caldik/client 0.1.8 → 0.1.10

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
@@ -384,6 +384,16 @@ await caldik.customer.session()
384
384
 
385
385
  ### Current Customer
386
386
 
387
+ **Sign out the current customer**
388
+
389
+ ```ts
390
+ await caldik.customer.logout()
391
+ ```
392
+
393
+ This revokes the current session, expires its cookies and clears the local cart
394
+ cache. The customer's saved cart remains available after their next sign-in.
395
+ Logout resolves to `undefined` and is safe to repeat.
396
+
387
397
  **Get the signed-in customer**
388
398
 
389
399
  ```ts
package/dist/index.d.mts CHANGED
@@ -478,11 +478,14 @@ declare class CaldikHttpClient {
478
478
  private readonly onSetCookie?;
479
479
  private readonly storefrontOrigin?;
480
480
  private readonly authenticationListeners;
481
+ private readonly logoutListeners;
481
482
  constructor(options?: HttpClientOptions);
482
483
  storeContextId(): string | null;
483
484
  hasCartSession(): boolean;
484
485
  onCustomerAuthentication(listener: () => Promise<unknown>): void;
485
486
  customerAuthenticated(): Promise<void>;
487
+ onCustomerLogout(listener: () => Promise<unknown>): void;
488
+ customerLoggedOut(): Promise<void>;
486
489
  private captureCookies;
487
490
  private baseUrl;
488
491
  private response;
@@ -586,6 +589,7 @@ declare class CustomerApi {
586
589
  currency?: string;
587
590
  }) => CaldikRequest<CustomerBalanceResponse>;
588
591
  };
592
+ readonly logout: () => Promise<void>;
589
593
  constructor(client: CaldikHttpClient);
590
594
  /** Resolves without a body on HTTP 200; rejects with status 401 when signed out. */
591
595
  session(): CaldikRequest<void>;
package/dist/index.mjs CHANGED
@@ -105,6 +105,7 @@ var CaldikHttpClient = class {
105
105
  onSetCookie;
106
106
  storefrontOrigin;
107
107
  authenticationListeners = /* @__PURE__ */ new Set();
108
+ logoutListeners = /* @__PURE__ */ new Set();
108
109
  constructor(options = {}) {
109
110
  this.apiKey = options.apiKey?.trim() || void 0;
110
111
  this.apiUrl = options.apiUrl || defaultApiUrl(isBrowserRuntime());
@@ -134,6 +135,12 @@ var CaldikHttpClient = class {
134
135
  async customerAuthenticated() {
135
136
  await Promise.allSettled([...this.authenticationListeners].map((listener) => listener()));
136
137
  }
138
+ onCustomerLogout(listener) {
139
+ this.logoutListeners.add(listener);
140
+ }
141
+ async customerLoggedOut() {
142
+ await Promise.all([...this.logoutListeners].map((listener) => listener()));
143
+ }
137
144
  async captureCookies(response) {
138
145
  for (const cookie of response.headers.getSetCookie()) {
139
146
  const [pair = "", ...attributes] = cookie.split(";");
@@ -302,8 +309,19 @@ var CustomerApi = class {
302
309
  password;
303
310
  orders;
304
311
  balance;
312
+ logout;
305
313
  constructor(client) {
306
314
  this.client = client;
315
+ this.logout = async () => {
316
+ await client.post("/customers/logout", void 0, {
317
+ access: "public",
318
+ empty: true
319
+ });
320
+ pendingChallengeId = void 0;
321
+ pendingRegistrationId = void 0;
322
+ pendingResetId = void 0;
323
+ await client.customerLoggedOut();
324
+ };
307
325
  this.orders = {
308
326
  list: (options = {}) => client.get("/customers/orders", { ...options }, { access: "public" }),
309
327
  get: (id) => client.get(`/customers/orders/${encodeURIComponent(id)}`, void 0, { access: "public" })
@@ -474,6 +492,10 @@ var CartApi = class {
474
492
  initialized = false;
475
493
  constructor(client) {
476
494
  this.client = client;
495
+ client.onCustomerLogout(() => this.enqueue(async () => {
496
+ this.reset();
497
+ this.initialized = false;
498
+ }));
477
499
  client.onCustomerAuthentication(() => {
478
500
  this.readCache();
479
501
  return this.initialized || this.cache.reference || this.cache.snapshot.items.length || client.hasCartSession() ? this.enqueue(async () => {
@@ -693,7 +715,7 @@ function batchRequests(client, input) {
693
715
  for (const [key, request] of entries) {
694
716
  if (!/^[a-zA-Z][a-zA-Z0-9_]{0,63}$/.test(key) || ["constructor", "prototype"].includes(key)) throw new Error("Invalid batch key");
695
717
  if (!(request instanceof CaldikRequest) || request.owner !== client || request.started || seen.has(request)) throw new Error("Batch values must be unstarted, distinct requests from the same Caldik client");
696
- if (request.descriptor.method !== "GET" && !/^\/plugins\/sdk\/[^/]+\/[^/]+$/.test(request.descriptor.path)) throw new Error("Batches support read requests only");
718
+ if (request.descriptor.method !== "GET" && !/^\/plugins\/sdk\/[a-zA-Z][a-zA-Z0-9_-]*\/[a-zA-Z][a-zA-Z0-9_-]*(?:\/[a-zA-Z][a-zA-Z0-9_-]*){0,3}$/.test(request.descriptor.path)) throw new Error("Batches support read requests only");
697
719
  seen.add(request);
698
720
  }
699
721
  } catch (error) {
@@ -831,7 +853,7 @@ function createPluginApi(client) {
831
853
  },
832
854
  apply(_target, _this, args) {
833
855
  while (args.length && args.at(-1) === void 0) args.pop();
834
- const path = `/plugins/sdk/${encodeURIComponent(namespace)}/${encodeURIComponent(keys.join("."))}`;
856
+ const path = `/plugins/sdk/${encodeURIComponent(namespace)}/${keys.map(encodeURIComponent).join("/")}`;
835
857
  if (keys.at(-1) === "subscribe") {
836
858
  const callbacks = args.at(-1);
837
859
  if (!callbacks || typeof callbacks !== "object" || Array.isArray(callbacks)) throw new Error("Provide subscription callbacks");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caldik/client",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "type": "module",
5
5
  "main": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.mts",