doboxa-js 1.0.2 → 1.1.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.
@@ -1,11 +1,12 @@
1
1
  import {
2
2
  DoboxaAPIError,
3
3
  DoboxaAuthenticationError,
4
+ DoboxaConnectorError,
4
5
  __async,
5
6
  __spreadProps,
6
7
  __spreadValues,
7
8
  parseErrorResponse
8
- } from "./chunk-CGJD4ZO5.mjs";
9
+ } from "./chunk-7RN7PBJH.mjs";
9
10
 
10
11
  // src/types/generated/src/runtime.ts
11
12
  var BASE_PATH = "http://localhost".replace(/\/+$/, "");
@@ -271,26 +272,24 @@ var HTTPClient = class {
271
272
  if (!accessToken || this.tokenManager.isTokenExpired()) {
272
273
  if (this.onTokenRefresh) {
273
274
  const refreshToken = this.tokenManager.getRefreshToken();
274
- if (!refreshToken) {
275
- throw new DoboxaAuthenticationError("No refresh token available");
275
+ if (refreshToken) {
276
+ let refreshPromise = this.tokenManager.getRefreshPromise();
277
+ if (!refreshPromise) {
278
+ refreshPromise = this.onTokenRefresh(refreshToken);
279
+ this.tokenManager.setRefreshPromise(refreshPromise);
280
+ }
281
+ try {
282
+ accessToken = yield refreshPromise;
283
+ } finally {
284
+ this.tokenManager.setRefreshPromise(null);
285
+ }
276
286
  }
277
- let refreshPromise = this.tokenManager.getRefreshPromise();
278
- if (!refreshPromise) {
279
- refreshPromise = this.onTokenRefresh(refreshToken);
280
- this.tokenManager.setRefreshPromise(refreshPromise);
281
- }
282
- try {
283
- accessToken = yield refreshPromise;
284
- } finally {
285
- this.tokenManager.setRefreshPromise(null);
286
- }
287
- } else {
288
- throw new DoboxaAuthenticationError("No access token available");
289
287
  }
290
288
  }
291
289
  const headers = new Headers(options.headers);
292
290
  headers.set("Content-Type", "application/json");
293
291
  headers.set("Authorization", `Bearer ${accessToken}`);
292
+ console.log("url", url);
294
293
  const response = yield fetch(url, __spreadProps(__spreadValues({}, options), {
295
294
  headers
296
295
  }));
@@ -479,20 +478,6 @@ var AuthenticationResource = class {
479
478
  }
480
479
  };
481
480
 
482
- // src/types/generated/src/models/Bool.ts
483
- function BoolFromJSON(json) {
484
- return json;
485
- }
486
- function BoolFromJSONTyped(json, ignoreDiscriminator) {
487
- return json;
488
- }
489
- function BoolToJSON(value) {
490
- return value;
491
- }
492
- function BoolToJSONTyped(value, ignoreDiscriminator = false) {
493
- return value;
494
- }
495
-
496
481
  // src/types/generated/src/models/Attachment.ts
497
482
  function instanceOfAttachment(value) {
498
483
  if (!("type" in value) || value["type"] === void 0) return false;
@@ -1363,7 +1348,7 @@ function OrderCancellationResponseFromJSONTyped(json, ignoreDiscriminator) {
1363
1348
  return json;
1364
1349
  }
1365
1350
  return {
1366
- "cancelled": json["cancelled"] == null ? void 0 : BoolFromJSON(json["cancelled"]),
1351
+ "cancelled": json["cancelled"] == null ? void 0 : json["cancelled"],
1367
1352
  "details": json["details"] == null ? void 0 : json["details"]
1368
1353
  };
1369
1354
  }
@@ -1375,7 +1360,7 @@ function OrderCancellationResponseToJSONTyped(value, ignoreDiscriminator = false
1375
1360
  return value;
1376
1361
  }
1377
1362
  return {
1378
- "cancelled": BoolToJSON(value["cancelled"]),
1363
+ "cancelled": value["cancelled"],
1379
1364
  "details": value["details"]
1380
1365
  };
1381
1366
  }
@@ -2812,10 +2797,122 @@ var UsersResource = class {
2812
2797
  }
2813
2798
  };
2814
2799
 
2800
+ // src/resources/catalog.ts
2801
+ var CatalogResource = class {
2802
+ constructor(config) {
2803
+ this.config = config;
2804
+ }
2805
+ /**
2806
+ * Call a connector action.
2807
+ *
2808
+ * @param action - Connector action, e.g. "ProductsList"
2809
+ * @param params - Additional action parameters
2810
+ * @throws DoboxaConnectorError on HTTP or connector errors
2811
+ */
2812
+ call(_0) {
2813
+ return __async(this, arguments, function* (action, params = {}) {
2814
+ const response = yield fetch(this.config.url, {
2815
+ method: "POST",
2816
+ headers: {
2817
+ "Content-Type": "application/x-www-form-urlencoded"
2818
+ },
2819
+ body: new URLSearchParams(__spreadValues({
2820
+ bl_pass: this.config.password,
2821
+ action
2822
+ }, params))
2823
+ });
2824
+ if (!response.ok) {
2825
+ throw new DoboxaConnectorError(
2826
+ `http_${response.status}`,
2827
+ response.statusText || "Unexpected HTTP status",
2828
+ `BaseLinker connector request failed with HTTP ${response.status}`
2829
+ );
2830
+ }
2831
+ let payload;
2832
+ try {
2833
+ payload = yield response.json();
2834
+ } catch (e) {
2835
+ throw new DoboxaConnectorError(
2836
+ "invalid_response",
2837
+ "Connector response is not valid JSON",
2838
+ "BaseLinker connector returned a non-JSON response"
2839
+ );
2840
+ }
2841
+ if (payload !== null && typeof payload === "object" && !Array.isArray(payload) && payload.error === true) {
2842
+ const error = payload;
2843
+ throw new DoboxaConnectorError(error.error_code, error.error_text);
2844
+ }
2845
+ return payload;
2846
+ });
2847
+ }
2848
+ /**
2849
+ * Get the connector platform and version.
2850
+ */
2851
+ fileVersion() {
2852
+ return __async(this, null, function* () {
2853
+ return this.call("FileVersion");
2854
+ });
2855
+ }
2856
+ /**
2857
+ * Get the list of actions the connector supports.
2858
+ */
2859
+ supportedMethods() {
2860
+ return __async(this, null, function* () {
2861
+ return this.call("SupportedMethods");
2862
+ });
2863
+ }
2864
+ /**
2865
+ * Get all product categories as a map of category id to category path.
2866
+ */
2867
+ categories() {
2868
+ return __async(this, null, function* () {
2869
+ return this.call("ProductsCategories");
2870
+ });
2871
+ }
2872
+ /**
2873
+ * List one page of products (designs).
2874
+ *
2875
+ * @param params - Page number and optional category filter
2876
+ */
2877
+ list() {
2878
+ return __async(this, arguments, function* (params = {}) {
2879
+ var _a;
2880
+ const response = yield this.call("ProductsList", __spreadValues({
2881
+ page: String((_a = params.page) != null ? _a : 1)
2882
+ }, params.category_id !== void 0 ? { category_id: params.category_id } : {}));
2883
+ const items = [];
2884
+ for (const id of Object.keys(response)) {
2885
+ if (id === "pages") {
2886
+ continue;
2887
+ }
2888
+ const value = response[id];
2889
+ if (typeof value !== "object" || value === null) {
2890
+ continue;
2891
+ }
2892
+ items.push(__spreadValues({ id }, value));
2893
+ }
2894
+ return { pages: response.pages, items };
2895
+ });
2896
+ }
2897
+ /**
2898
+ * Get full product data (images, variants) for the given design ids.
2899
+ *
2900
+ * @param params - Design ids from the product list
2901
+ */
2902
+ data(params) {
2903
+ return __async(this, null, function* () {
2904
+ const response = yield this.call("ProductsData", {
2905
+ products_id: params.products_id.join(",")
2906
+ });
2907
+ return Array.isArray(response) ? {} : response;
2908
+ });
2909
+ }
2910
+ };
2911
+
2815
2912
  // src/client.ts
2816
2913
  var Doboxa = class {
2817
2914
  constructor(config = {}) {
2818
- const baseURL = config.baseURL || getBaseURL() || "http://localhost";
2915
+ const baseURL = config.baseURL || getBaseURL() || "https://pl.doboxa.biz";
2819
2916
  this.tokenManager = new TokenManager();
2820
2917
  if (config.apiKey) {
2821
2918
  this.tokenManager.setTokens(config.apiKey, "", 3600);
@@ -2841,30 +2938,51 @@ var Doboxa = class {
2841
2938
  baseURL,
2842
2939
  tokenManager: this.tokenManager,
2843
2940
  onTokenRefresh: (refreshToken) => __async(this, null, function* () {
2844
- const tempAuth = new AuthenticationResource(tempHttpClient, this.tokenManager);
2941
+ const tempAuth = new AuthenticationResource(
2942
+ tempHttpClient,
2943
+ this.tokenManager
2944
+ );
2845
2945
  const response = yield tempAuth.refreshToken(refreshToken);
2846
- this.tokenManager.setTokens(response.token, response.refresh_token, 3600);
2946
+ this.tokenManager.setTokens(
2947
+ response.token,
2948
+ response.refresh_token,
2949
+ 3600
2950
+ );
2847
2951
  return response.token;
2848
2952
  })
2849
2953
  });
2850
- this.authentication = new AuthenticationResource(this.httpClient, this.tokenManager);
2954
+ this.authentication = new AuthenticationResource(
2955
+ this.httpClient,
2956
+ this.tokenManager
2957
+ );
2851
2958
  this.orders = new OrdersResource(this.httpClient, this.config);
2852
2959
  this.invoices = new InvoicesResource(this.httpClient, this.config);
2853
2960
  this.clients = new ClientsResource(this.httpClient, this.config);
2854
- this.discountCodes = new DiscountCodesResource(this.httpClient, this.config);
2855
- this.trackingNumbers = new TrackingNumbersResource(this.httpClient, this.config);
2856
- this.orderComments = new OrderCommentsResource(this.httpClient, this.config);
2857
- this.orderStatusChanges = new OrderStatusChangesResource(this.httpClient, this.config);
2961
+ this.discountCodes = new DiscountCodesResource(
2962
+ this.httpClient,
2963
+ this.config
2964
+ );
2965
+ this.trackingNumbers = new TrackingNumbersResource(
2966
+ this.httpClient,
2967
+ this.config
2968
+ );
2969
+ this.orderComments = new OrderCommentsResource(
2970
+ this.httpClient,
2971
+ this.config
2972
+ );
2973
+ this.orderStatusChanges = new OrderStatusChangesResource(
2974
+ this.httpClient,
2975
+ this.config
2976
+ );
2858
2977
  this.users = new UsersResource(this.httpClient, this.config);
2978
+ if (config.catalog) {
2979
+ this.catalog = new CatalogResource(config.catalog);
2980
+ }
2859
2981
  }
2860
2982
  };
2861
2983
 
2862
2984
  export {
2863
2985
  AuthenticationResource,
2864
- BoolFromJSON,
2865
- BoolFromJSONTyped,
2866
- BoolToJSON,
2867
- BoolToJSONTyped,
2868
2986
  instanceOfAttachment,
2869
2987
  AttachmentFromJSON,
2870
2988
  AttachmentFromJSONTyped,
@@ -3072,6 +3190,7 @@ export {
3072
3190
  OrderCommentsResource,
3073
3191
  OrderStatusChangesResource,
3074
3192
  UsersResource,
3193
+ CatalogResource,
3075
3194
  Doboxa
3076
3195
  };
3077
- //# sourceMappingURL=chunk-36DDSV5T.mjs.map
3196
+ //# sourceMappingURL=chunk-RTI5YLAB.mjs.map