@commercengine/pos 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.
package/dist/index.mjs CHANGED
@@ -75,7 +75,6 @@ var ResponseUtils = class {
75
75
  */
76
76
  var DebugLogger = class {
77
77
  logger;
78
- responseTextCache = /* @__PURE__ */ new Map();
79
78
  constructor(logger) {
80
79
  this.logger = logger || ((level, message, data) => {
81
80
  console.log(`[${level.toUpperCase()}]`, message);
@@ -98,7 +97,6 @@ var DebugLogger = class {
98
97
  * Log debug information about API response
99
98
  */
100
99
  async logResponse(response, responseBody) {
101
- if (responseBody && typeof responseBody === "string") this.responseTextCache.set(response.url, responseBody);
102
100
  this.logger("info", "API Response Debug Info", {
103
101
  url: response.url,
104
102
  status: response.status,
@@ -122,17 +120,17 @@ var DebugLogger = class {
122
120
  this.logger("error", message, error);
123
121
  }
124
122
  /**
125
- * Get cached response text for a URL (if available)
123
+ * Compatibility shim retained for older internal callers.
124
+ * Response bodies are no longer cached by the debug logger.
126
125
  */
127
- getCachedResponseText(url) {
128
- return this.responseTextCache.get(url) || null;
126
+ getCachedResponseText(_url) {
127
+ return null;
129
128
  }
130
129
  /**
131
- * Clear cached response texts
130
+ * Compatibility shim retained for older internal callers.
131
+ * Response bodies are no longer cached by the debug logger.
132
132
  */
133
- clearCache() {
134
- this.responseTextCache.clear();
135
- }
133
+ clearCache() {}
136
134
  info(message, data) {
137
135
  this.logger("info", message, data);
138
136
  }
@@ -218,14 +216,33 @@ function createDebugMiddleware(logger) {
218
216
  * @returns Middleware object with onRequest handler
219
217
  */
220
218
  function createTimeoutMiddleware(timeoutMs) {
221
- return { onRequest: async ({ request }) => {
222
- const controller = new AbortController();
223
- const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
224
- if (request.signal) request.signal.addEventListener("abort", () => controller.abort());
225
- const newRequest = new Request(request, { signal: controller.signal });
226
- controller.signal.addEventListener("abort", () => clearTimeout(timeoutId));
227
- return newRequest;
228
- } };
219
+ const timeouts = /* @__PURE__ */ new WeakMap();
220
+ const clearRequestTimeout = (signal) => {
221
+ const timeoutId = timeouts.get(signal);
222
+ if (timeoutId) {
223
+ clearTimeout(timeoutId);
224
+ timeouts.delete(signal);
225
+ }
226
+ };
227
+ return {
228
+ onRequest: async ({ request }) => {
229
+ const controller = new AbortController();
230
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
231
+ if (request.signal) request.signal.addEventListener("abort", () => controller.abort(), { once: true });
232
+ const newRequest = new Request(request, { signal: controller.signal });
233
+ timeouts.set(newRequest.signal, timeoutId);
234
+ controller.signal.addEventListener("abort", () => clearRequestTimeout(newRequest.signal), { once: true });
235
+ return newRequest;
236
+ },
237
+ onResponse: async ({ request, response }) => {
238
+ clearRequestTimeout(request.signal);
239
+ return response;
240
+ },
241
+ onError: async ({ request, error }) => {
242
+ clearRequestTimeout(request.signal);
243
+ throw error;
244
+ }
245
+ };
229
246
  }
230
247
  /**
231
248
  * Transform headers using a transformation mapping
@@ -291,8 +308,8 @@ async function executeRequest(apiCall) {
291
308
  };
292
309
  } catch (err) {
293
310
  const mockResponse = new Response(null, {
294
- status: 0,
295
- statusText: "Network Error"
311
+ status: 503,
312
+ statusText: "Service Unavailable"
296
313
  });
297
314
  return {
298
315
  data: null,
@@ -2001,16 +2018,12 @@ var PosClient = class extends PosAPIClient {
2001
2018
  * Search products
2002
2019
  * @param body - Search criteria and parameters
2003
2020
  * @param headers - Optional header parameters
2004
- * @returns Promise with search results
2021
+ * @returns Promise with search results including SKUs, facet distribution, facet stats, and pagination
2005
2022
  * @example
2006
2023
  * ```typescript
2024
+ * // Basic search
2007
2025
  * const { data, error } = await pos.searchProducts({
2008
2026
  * query: "smartphone",
2009
- * filters: {
2010
- * category: ["electronics", "mobile"],
2011
- * price_range: { min: 100, max: 1000 },
2012
- * brand: ["Apple", "Samsung"] // facet names depend on product configuration
2013
- * },
2014
2027
  * page: 1,
2015
2028
  * limit: 20
2016
2029
  * });
@@ -2020,17 +2033,52 @@ var PosClient = class extends PosAPIClient {
2020
2033
  * } else {
2021
2034
  * console.log("Search results:", data.skus?.length || 0, "products found");
2022
2035
  * console.log("Facet distribution:", data.facet_distribution);
2023
- * console.log("Price range:", data.facet_stats.price_range);
2036
+ * console.log("Facet stats:", data.facet_stats);
2037
+ * console.log("Pagination:", data.pagination);
2038
+ *
2024
2039
  * data.skus?.forEach(sku => {
2025
- * console.log(`Found: ${sku.name} - ${sku.price}`);
2040
+ * console.log(`Found: ${sku.product_name} - ${sku.pricing?.selling_price}`);
2026
2041
  * });
2027
2042
  * }
2028
2043
  *
2044
+ * // With filter (string expression — Meilisearch syntax)
2045
+ * const { data: filtered, error: filteredError } = await pos.searchProducts({
2046
+ * query: "laptop",
2047
+ * filter: "pricing.selling_price 500 TO 2000 AND product_type = physical",
2048
+ * sort: ["pricing.selling_price:asc"],
2049
+ * facets: ["product_type", "categories.name", "tags"],
2050
+ * page: 1,
2051
+ * limit: 10
2052
+ * });
2053
+ *
2054
+ * // With filter (array of conditions — combined with AND)
2055
+ * const { data: arrayFiltered, error: arrayError } = await pos.searchProducts({
2056
+ * query: "shoes",
2057
+ * filter: ["product_type = physical", "rating >= 4", "stock_available > 0"],
2058
+ * sort: ["rating:desc"],
2059
+ * facets: ["*"],
2060
+ * page: 1,
2061
+ * limit: 25
2062
+ * });
2063
+ *
2064
+ * // With filter (nested arrays — inner arrays use OR, outer uses AND)
2065
+ * const { data: nestedFiltered, error: nestedError } = await pos.searchProducts({
2066
+ * query: "headphones",
2067
+ * filter: [
2068
+ * "pricing.selling_price 50 TO 300",
2069
+ * ["product_type = physical", "product_type = bundle"]
2070
+ * ],
2071
+ * page: 1,
2072
+ * limit: 25
2073
+ * });
2074
+ *
2029
2075
  * // Override customer group ID for this specific request
2030
2076
  * const { data: overrideData, error: overrideError } = await pos.searchProducts(
2031
2077
  * {
2032
2078
  * query: "laptop",
2033
- * filters: { category: ["computers"] }
2079
+ * filter: "categories.name = computers",
2080
+ * page: 1,
2081
+ * limit: 20
2034
2082
  * },
2035
2083
  * {
2036
2084
  * "x-customer-group-id": "01H9XYZ12345USERID" // Override default SDK config
@@ -2147,32 +2195,33 @@ var PosClient = class extends PosAPIClient {
2147
2195
  }
2148
2196
  /**
2149
2197
  * Get product details
2150
- * @param pathParams - Product ID or slug
2198
+ * @param pathParams - The path parameters. Accepts product ID or product slug.
2151
2199
  * @param headers - Optional header parameters
2152
2200
  * @returns Promise with product details
2153
2201
  * @example
2154
2202
  * ```typescript
2155
2203
  * // Get product by ID
2156
2204
  * const { data, error } = await pos.getProductDetail(
2157
- * { product_id_or_slug: "prod_123" }
2205
+ * { product_id: "prod_123" }
2158
2206
  * );
2159
2207
  *
2160
2208
  * if (error) {
2161
2209
  * console.error("Failed to get product details:", error.message);
2162
2210
  * } else {
2163
2211
  * console.log("Product:", data.product.name);
2164
- * console.log("Price:", data.product.price);
2165
- * console.log("Description:", data.product.description);
2212
+ * console.log("Price:", data.product.pricing?.selling_price);
2213
+ * console.log("Description:", data.product.short_description);
2166
2214
  * }
2167
2215
  *
2168
- * // Get product by slug
2216
+ * // Get product by slug (also accepted in place of product_id)
2169
2217
  * const { data: slugData, error: slugError } = await pos.getProductDetail({
2170
- * product_id_or_slug: "detox-candy"
2218
+ * product_id: "detox-candy"
2171
2219
  * });
2172
2220
  *
2173
2221
  * // Override customer group ID for this specific request
2174
2222
  * const { data: overrideData, error: overrideError } = await pos.getProductDetail(
2175
- * { product_id_or_slug: "detox-candy" },
2223
+ * { product_id: "detox-candy" },
2224
+ * undefined,
2176
2225
  * {
2177
2226
  * "x-customer-group-id": "premium_customers" // Override default SDK config
2178
2227
  * }
@@ -2181,7 +2230,7 @@ var PosClient = class extends PosAPIClient {
2181
2230
  */
2182
2231
  async getProductDetail(pathParams, query, headers) {
2183
2232
  const mergedHeaders = this.mergeHeaders(headers);
2184
- return this.executeRequest(() => this.client.GET("/pos/catalog/products/{product_id_or_slug}", { params: {
2233
+ return this.executeRequest(() => this.client.GET("/pos/catalog/products/{product_id}", { params: {
2185
2234
  path: pathParams,
2186
2235
  query,
2187
2236
  header: mergedHeaders
@@ -2226,11 +2275,12 @@ var PosClient = class extends PosAPIClient {
2226
2275
  }
2227
2276
  /**
2228
2277
  * List product variants
2229
- * @param pathParams - Product ID
2278
+ * @param pathParams - The path parameters. Accepts product ID or product slug.
2230
2279
  * @param headers - Optional header parameters
2231
2280
  * @returns Promise with product variants
2232
2281
  * @example
2233
2282
  * ```typescript
2283
+ * // By product ID
2234
2284
  * const { data, error } = await pos.listProductVariants(
2235
2285
  * { product_id: "prod_123" }
2236
2286
  * );
@@ -2240,13 +2290,19 @@ var PosClient = class extends PosAPIClient {
2240
2290
  * } else {
2241
2291
  * console.log("Variants found:", data.variants?.length || 0);
2242
2292
  * data.variants?.forEach(variant => {
2243
- * console.log(`Variant: ${variant.name} - SKU: ${variant.sku} - Price: ${variant.price}`);
2293
+ * console.log(`Variant: ${variant.name} - SKU: ${variant.sku} - Price: ${variant.pricing?.selling_price}`);
2244
2294
  * });
2245
2295
  * }
2246
2296
  *
2297
+ * // By product slug (also accepted in place of product_id)
2298
+ * const { data: slugData, error: slugError } = await pos.listProductVariants(
2299
+ * { product_id: "detox-candy" }
2300
+ * );
2301
+ *
2247
2302
  * // Override customer group ID for this specific request
2248
2303
  * const { data: overrideData, error: overrideError } = await pos.listProductVariants(
2249
2304
  * { product_id: "prod_123" },
2305
+ * undefined,
2250
2306
  * {
2251
2307
  * "x-customer-group-id": "wholesale_customers" // Override default SDK config
2252
2308
  * }
@@ -2263,11 +2319,12 @@ var PosClient = class extends PosAPIClient {
2263
2319
  }
2264
2320
  /**
2265
2321
  * Get variant details
2266
- * @param pathParams - Product ID and variant ID
2322
+ * @param pathParams - The path parameters. Accepts product ID or slug for product_id, and variant ID or slug for variant_id.
2267
2323
  * @param headers - Optional header parameters
2268
2324
  * @returns Promise with variant details
2269
2325
  * @example
2270
2326
  * ```typescript
2327
+ * // By product ID and variant ID
2271
2328
  * const { data, error } = await pos.getVariantDetail(
2272
2329
  * {
2273
2330
  * product_id: "prod_123",
@@ -2280,16 +2337,25 @@ var PosClient = class extends PosAPIClient {
2280
2337
  * } else {
2281
2338
  * console.log("Variant:", data.variant.name);
2282
2339
  * console.log("SKU:", data.variant.sku);
2283
- * console.log("Price:", data.variant.price);
2284
- * console.log("Stock:", data.variant.stock);
2340
+ * console.log("Price:", data.variant.pricing?.selling_price);
2341
+ * console.log("Stock available:", data.variant.stock_available);
2285
2342
  * }
2286
2343
  *
2344
+ * // By product slug and variant slug (also accepted in place of IDs)
2345
+ * const { data: slugData, error: slugError } = await pos.getVariantDetail(
2346
+ * {
2347
+ * product_id: "detox-candy",
2348
+ * variant_id: "detox-candy-100g"
2349
+ * }
2350
+ * );
2351
+ *
2287
2352
  * // Override customer group ID for this specific request
2288
2353
  * const { data: overrideData, error: overrideError } = await pos.getVariantDetail(
2289
2354
  * {
2290
2355
  * product_id: "prod_123",
2291
2356
  * variant_id: "var_456"
2292
2357
  * },
2358
+ * undefined,
2293
2359
  * {
2294
2360
  * "x-customer-group-id": "wholesale_customers" // Override default SDK config
2295
2361
  * }