@vigneshreddy/cms-sdk 1.0.11 → 1.0.13

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.
@@ -20,4 +20,8 @@ export declare class CMSAPIError extends Error {
20
20
  headers?: Record<string, any>;
21
21
  };
22
22
  constructor(message: string, statusCode: number, type: string, rawError: any, request?: CMSAPIError["request"], response?: CMSAPIError["response"]);
23
+ toJSON(): {
24
+ statusCode: number;
25
+ message: string;
26
+ };
23
27
  }
@@ -12,7 +12,10 @@ class CMSAPIError extends Error {
12
12
  this.statusCode = statusCode;
13
13
  this.type = type;
14
14
  this.rawError = rawError;
15
- this.cause = rawError;
15
+ this.cause =
16
+ rawError instanceof Error || typeof rawError === "string"
17
+ ? rawError
18
+ : undefined;
16
19
  this.request = request;
17
20
  this.response = response;
18
21
  // Ensure proper prototype chain in transpiled output
@@ -35,5 +38,11 @@ class CMSAPIError extends Error {
35
38
  // it will just remain enumerable.
36
39
  }
37
40
  }
41
+ toJSON() {
42
+ return {
43
+ statusCode: this.statusCode,
44
+ message: this.message,
45
+ };
46
+ }
38
47
  }
39
48
  exports.CMSAPIError = CMSAPIError;
@@ -25,10 +25,41 @@ Object.defineProperty(exports, "createSpecificError", { enumerable: true, get: f
25
25
  * Uses specific error types when possible for better type safety
26
26
  */
27
27
  function handleApiError(error, request, response) {
28
- var _a, _b, _c, _d, _e, _f, _g, _h;
28
+ var _a, _b, _c, _d, _e, _f;
29
29
  if (error instanceof base_1.CMSAPIError) {
30
30
  throw error; // preserve original error metadata/type
31
31
  }
32
+ const getBackendErrorMessage = (data) => {
33
+ var _a;
34
+ const directMessage = typeof (data === null || data === void 0 ? void 0 : data.message) === "string" ? data.message.trim() : "";
35
+ if (directMessage)
36
+ return directMessage;
37
+ const nestedMessage = typeof ((_a = data === null || data === void 0 ? void 0 : data.error) === null || _a === void 0 ? void 0 : _a.message) === "string" ? data.error.message.trim() : "";
38
+ if (nestedMessage)
39
+ return nestedMessage;
40
+ if (Array.isArray(data === null || data === void 0 ? void 0 : data.errors)) {
41
+ const firstObjectErrorCode = data.errors.find((item) => typeof (item === null || item === void 0 ? void 0 : item.code) === "string" && item.code.trim());
42
+ if (firstObjectErrorCode) {
43
+ return firstObjectErrorCode.code.trim();
44
+ }
45
+ const firstStringError = data.errors.find((item) => typeof item === "string" && item.trim());
46
+ if (typeof firstStringError === "string") {
47
+ return firstStringError.trim();
48
+ }
49
+ const firstObjectErrorMessage = data.errors.find((item) => typeof (item === null || item === void 0 ? void 0 : item.message) === "string" && item.message.trim());
50
+ if (firstObjectErrorMessage) {
51
+ return firstObjectErrorMessage.message.trim();
52
+ }
53
+ const firstObjectErrorText = data.errors.find((item) => typeof (item === null || item === void 0 ? void 0 : item.errorMessage) === "string" && item.errorMessage.trim());
54
+ if (firstObjectErrorText) {
55
+ return firstObjectErrorText.errorMessage.trim();
56
+ }
57
+ }
58
+ if (typeof data === "string" && data.trim()) {
59
+ return data.trim();
60
+ }
61
+ return undefined;
62
+ };
32
63
  const sanitizeRequest = (req) => {
33
64
  if (!req || typeof req !== "object")
34
65
  return undefined;
@@ -47,9 +78,11 @@ function handleApiError(error, request, response) {
47
78
  if (errorWithResponse) {
48
79
  const res = errorWithResponse;
49
80
  const statusCode = (_c = (_b = res.status) !== null && _b !== void 0 ? _b : res.statusCode) !== null && _c !== void 0 ? _c : 500;
50
- const message = (_f = (_e = (_d = res.data) === null || _d === void 0 ? void 0 : _d.message) !== null && _e !== void 0 ? _e : res.statusText) !== null && _f !== void 0 ? _f : "Unknown API Error";
81
+ const backendMessage = getBackendErrorMessage(res.data);
82
+ const fallbackMessage = `CMS API Error: ${statusCode} - ${(_d = res.statusText) !== null && _d !== void 0 ? _d : "Unknown API Error"}`;
83
+ const message = backendMessage !== null && backendMessage !== void 0 ? backendMessage : fallbackMessage;
51
84
  // Prefer typed error subclasses when possible
52
- const typedError = (0, specific_1.createSpecificError)(statusCode, `CMS API Error: ${statusCode} - ${message}`, (_g = res.data) !== null && _g !== void 0 ? _g : error);
85
+ const typedError = (0, specific_1.createSpecificError)(statusCode, message, (_e = res.data) !== null && _e !== void 0 ? _e : error);
53
86
  typedError.request = sanitizeRequest(request !== null && request !== void 0 ? request : error === null || error === void 0 ? void 0 : error.request);
54
87
  typedError.response = {
55
88
  status: statusCode,
@@ -60,7 +93,7 @@ function handleApiError(error, request, response) {
60
93
  throw typedError;
61
94
  }
62
95
  // Error where request was made but no response received
63
- const errorWithRequest = (_h = error === null || error === void 0 ? void 0 : error.request) !== null && _h !== void 0 ? _h : request;
96
+ const errorWithRequest = (_f = error === null || error === void 0 ? void 0 : error.request) !== null && _f !== void 0 ? _f : request;
64
97
  if (errorWithRequest) {
65
98
  const req = sanitizeRequest(errorWithRequest);
66
99
  const isTimeout = (error === null || error === void 0 ? void 0 : error.code) === "ECONNABORTED" ||
@@ -10,7 +10,7 @@ export type TrackLeadRequest = {
10
10
  /**
11
11
  * Unique User ID. Used for deduplication (user cannot signup twice).
12
12
  */
13
- 'customerId': string;
13
+ 'customerExternalId': string;
14
14
  /**
15
15
  * When set to `"deferred"`, the API may associate `clickId` to `customerId`
16
16
  * for later attribution.
@@ -20,10 +20,6 @@ export type TrackLeadRequest = {
20
20
  * Optional event timestamp in ISO 8601 format.
21
21
  */
22
22
  'timestamp'?: string;
23
- /**
24
- * Optional external customer identifier used in your system.
25
- */
26
- 'customerExternalId'?: string;
27
23
  /**
28
24
  * Optional customer display name.
29
25
  */
@@ -46,7 +42,6 @@ export type TrackLeadRequest = {
46
42
  /**
47
43
  * Unique User ID. Used for deduplication (user cannot signup twice).
48
44
  */
49
- 'customerId': string;
50
45
  'mode': 'deferred';
51
46
  /**
52
47
  * Optional event timestamp in ISO 8601 format.
@@ -55,7 +50,7 @@ export type TrackLeadRequest = {
55
50
  /**
56
51
  * Optional external customer identifier used in your system.
57
52
  */
58
- 'customerExternalId'?: string;
53
+ 'customerExternalId': string;
59
54
  /**
60
55
  * Optional customer display name.
61
56
  */
@@ -85,7 +80,7 @@ export interface TrackSaleRequest {
85
80
  /**
86
81
  * Optional external customer identifier used in your system.
87
82
  */
88
- 'customerExternalId'?: string;
83
+ 'customerExternalId': string;
89
84
  /**
90
85
  * Optional customer display name.
91
86
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vigneshreddy/cms-sdk",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "description": "Official TypeScript SDK for CutMeShort CMS API",
5
5
  "main": "dist/src/index.js",
6
6
  "types": "dist/src/index.d.ts",