@vigneshreddy/cms-sdk 1.0.5 → 1.0.7

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,4 +1,6 @@
1
- import { EventsApi, TrackLeadRequest, TrackSaleRequest } from "./generated";
1
+ import { EventsApi } from "./generated";
2
+ export type LeadPayload = Record<string, unknown>;
3
+ export type SalePayload = Record<string, unknown>;
2
4
  export interface CMSConfig {
3
5
  apiKey: string;
4
6
  /**
@@ -41,6 +43,6 @@ export declare class CMS {
41
43
  private sleep;
42
44
  private withRetry;
43
45
  private isRetryableErrorWithOptions;
44
- trackLead(request: TrackLeadRequest, options?: RequestOptions): Promise<import("./generated").TrackResponse>;
45
- trackSale(request: TrackSaleRequest, options?: RequestOptions): Promise<import("./generated").TrackResponse>;
46
+ trackLead(leadData: LeadPayload, options?: RequestOptions): Promise<import("./generated").TrackResponse>;
47
+ trackSale(saleData: SalePayload, options?: RequestOptions): Promise<import("./generated").TrackResponse>;
46
48
  }
@@ -12,7 +12,7 @@ class CMS {
12
12
  }
13
13
  // Absolute base URL for the CMS SDK – we intentionally do NOT append
14
14
  // per-endpoint paths like /track/lead; callers just hit this base.
15
- const basePath = "https://www.app.dev.cutmeshort.com/sdk";
15
+ const basePath = "http://localhost:5000/sdk";
16
16
  // Retry configuration with sensible defaults
17
17
  this.maxRetries = (_a = config.maxRetries) !== null && _a !== void 0 ? _a : 2;
18
18
  this.retryDelayMs = (_b = config.retryDelayMs) !== null && _b !== void 0 ? _b : 500;
@@ -26,7 +26,6 @@ class CMS {
26
26
  // Ensure generated client uses the same timeout & headers
27
27
  timeout: (_e = config.timeout) !== null && _e !== void 0 ? _e : 10000,
28
28
  headers: {
29
- 'User-Agent': 'CMS-Node-SDK/1.0.0',
30
29
  'Content-Type': 'application/json'
31
30
  },
32
31
  },
@@ -97,14 +96,22 @@ class CMS {
97
96
  }
98
97
  return false;
99
98
  }
100
- async trackLead(request, options) {
99
+ async trackLead(leadData, options) {
100
+ const payload = {
101
+ type: "lead",
102
+ lead: leadData,
103
+ };
101
104
  return this.withRetry(async () => {
102
- return this.events.trackLead(request);
105
+ return this.events.trackLead(payload);
103
106
  }, options);
104
107
  }
105
- async trackSale(request, options) {
108
+ async trackSale(saleData, options) {
109
+ const payload = {
110
+ type: "sale",
111
+ sale: saleData,
112
+ };
106
113
  return this.withRetry(async () => {
107
- return this.events.trackSale(request);
114
+ return this.events.trackSale(payload);
108
115
  }, options);
109
116
  }
110
117
  }
@@ -14,6 +14,19 @@ class CMSAPIError extends Error {
14
14
  this.rawError = rawError;
15
15
  this.request = request;
16
16
  this.response = response;
17
+ // Make heavy properties non-enumerable so console.log / inspection
18
+ // stays focused on the high-level error information.
19
+ try {
20
+ Object.defineProperties(this, {
21
+ rawError: { enumerable: false },
22
+ request: { enumerable: false },
23
+ response: { enumerable: false },
24
+ });
25
+ }
26
+ catch {
27
+ // If defineProperties fails for any reason, we still keep the data;
28
+ // it will just remain enumerable.
29
+ }
17
30
  }
18
31
  }
19
32
  exports.CMSAPIError = CMSAPIError;
@@ -25,6 +25,14 @@ Object.defineProperty(exports, "createSpecificError", { enumerable: true, get: f
25
25
  */
26
26
  function handleApiError(error, request, response) {
27
27
  var _a, _b, _c;
28
+ const sanitizeRequest = (req) => {
29
+ if (!req || typeof req !== "object")
30
+ return undefined;
31
+ return {
32
+ url: req.url,
33
+ method: req.method,
34
+ };
35
+ };
28
36
  // Error with response from server
29
37
  if ((error === null || error === void 0 ? void 0 : error.response) || response) {
30
38
  const res = (error === null || error === void 0 ? void 0 : error.response) || response;
@@ -33,7 +41,7 @@ function handleApiError(error, request, response) {
33
41
  res.statusText ||
34
42
  "Unknown API Error";
35
43
  // Create error with request/response metadata
36
- const apiError = new base_1.CMSAPIError(`CMS API Error: ${statusCode} - ${message}`, statusCode, ((_b = res.data) === null || _b === void 0 ? void 0 : _b.type) || "api_error", (_c = res.data) !== null && _c !== void 0 ? _c : error, request || (error === null || error === void 0 ? void 0 : error.request), {
44
+ const apiError = new base_1.CMSAPIError(`CMS API Error: ${statusCode} - ${message}`, statusCode, ((_b = res.data) === null || _b === void 0 ? void 0 : _b.type) || "api_error", (_c = res.data) !== null && _c !== void 0 ? _c : error, sanitizeRequest(request || (error === null || error === void 0 ? void 0 : error.request)), {
37
45
  status: statusCode,
38
46
  statusText: res.statusText || "",
39
47
  data: res.data,
@@ -43,7 +51,7 @@ function handleApiError(error, request, response) {
43
51
  }
44
52
  // Error where request was made but no response received
45
53
  if ((error === null || error === void 0 ? void 0 : error.request) || request) {
46
- const req = (error === null || error === void 0 ? void 0 : error.request) || request;
54
+ const req = sanitizeRequest((error === null || error === void 0 ? void 0 : error.request) || request);
47
55
  const isTimeout = error.code === "ECONNABORTED" ||
48
56
  typeof error.message === "string" &&
49
57
  error.message.toLowerCase().includes("timeout");
@@ -1,9 +1,8 @@
1
- import { CMS } from "../client";
1
+ import { CMS, type LeadPayload } from "../client";
2
2
  import type { TrackResponse } from "../generated/api";
3
3
  import { Result } from "../types/result";
4
4
  import { CMSAPIError, BadRequestError, UnauthorizedError, ForbiddenError, NotFoundError, ConflictError, UnprocessableEntityError, RateLimitError, InternalServerError, BadGatewayError, ServiceUnavailableError, GatewayTimeoutError } from "../errors";
5
5
  export type TrackLeadError = BadRequestError | UnauthorizedError | ForbiddenError | NotFoundError | ConflictError | UnprocessableEntityError | RateLimitError | InternalServerError | BadGatewayError | ServiceUnavailableError | GatewayTimeoutError | CMSAPIError;
6
- export type LeadPayload = Record<string, unknown>;
7
6
  /**
8
7
  * Track a lead for a short link.
9
8
  *
@@ -43,11 +43,7 @@ const errors_1 = require("../errors");
43
43
  */
44
44
  async function trackLead(client, leadData) {
45
45
  try {
46
- const payload = {
47
- type: "lead",
48
- lead: leadData,
49
- };
50
- const value = await client.trackLead(payload);
46
+ const value = await client.trackLead(leadData);
51
47
  return (0, result_1.ok)(value);
52
48
  }
53
49
  catch (error) {
@@ -1,9 +1,8 @@
1
- import { CMS } from "../client";
1
+ import { CMS, type SalePayload } from "../client";
2
2
  import type { TrackResponse } from "../generated/api";
3
3
  import { Result } from "../types/result";
4
4
  import { CMSAPIError, BadRequestError, UnauthorizedError, ForbiddenError, NotFoundError, ConflictError, UnprocessableEntityError, RateLimitError, InternalServerError, BadGatewayError, ServiceUnavailableError, GatewayTimeoutError } from "../errors";
5
5
  export type TrackSaleError = BadRequestError | UnauthorizedError | ForbiddenError | NotFoundError | ConflictError | UnprocessableEntityError | RateLimitError | InternalServerError | BadGatewayError | ServiceUnavailableError | GatewayTimeoutError | CMSAPIError;
6
- export type SalePayload = Record<string, unknown>;
7
6
  /**
8
7
  * Track a sale for a short link.
9
8
  *
@@ -45,11 +45,7 @@ const errors_1 = require("../errors");
45
45
  */
46
46
  async function trackSale(client, saleData) {
47
47
  try {
48
- const payload = {
49
- type: "sale",
50
- sale: saleData,
51
- };
52
- const value = await client.trackSale(payload);
48
+ const value = await client.trackSale(saleData);
53
49
  return (0, result_1.ok)(value);
54
50
  }
55
51
  catch (error) {
@@ -200,8 +200,9 @@ exports.EventsApi = EventsApi;
200
200
  * operate without changes.
201
201
  */
202
202
  async function performFetchRequest(requestArgs, basePath) {
203
+ var _a;
203
204
  const url = (basePath !== null && basePath !== void 0 ? basePath : base_1.BASE_PATH).replace(/\/+$/, "") + requestArgs.url;
204
- const { timeout, ...restOptions } = requestArgs.options;
205
+ const { timeout, data, ...restOptions } = requestArgs.options;
205
206
  if (typeof fetch !== "function") {
206
207
  throw new Error("Global fetch API is not available. Please provide a fetch polyfill in this environment.");
207
208
  }
@@ -212,20 +213,35 @@ async function performFetchRequest(requestArgs, basePath) {
212
213
  timeoutId = setTimeout(() => controller.abort(), timeout);
213
214
  }
214
215
  try {
215
- const response = await fetch(url, { ...restOptions, signal });
216
- const contentType = response.headers.get("content-type") || "";
217
- const isJson = contentType.toLowerCase().includes("application/json");
218
- const data = isJson ? await response.json() : await response.text();
216
+ const fetchOptions = { ...restOptions, signal };
217
+ // The generated client stores the request payload on `data` (Axios-style),
218
+ // but the fetch API expects it on `body`. Translate between the two.
219
+ if (typeof data !== "undefined") {
220
+ const headers = ((_a = fetchOptions.headers) !== null && _a !== void 0 ? _a : {});
221
+ const requestContentType = Object.keys(headers).find((h) => h.toLowerCase() === "content-type");
222
+ // If Content-Type is JSON (our default), ensure the body is a JSON string.
223
+ if (requestContentType && headers[requestContentType].includes("application/json")) {
224
+ fetchOptions.body =
225
+ typeof data === "string" ? data : JSON.stringify(data);
226
+ }
227
+ else {
228
+ fetchOptions.body = data;
229
+ }
230
+ }
231
+ const response = await fetch(url, fetchOptions);
232
+ const responseContentType = response.headers.get("content-type") || "";
233
+ const isJson = responseContentType.toLowerCase().includes("application/json");
234
+ const dataResult = isJson ? await response.json() : await response.text();
219
235
  if (!response.ok) {
220
236
  const error = new Error(`Request failed with status code ${response.status}`);
221
237
  error.response = {
222
238
  status: response.status,
223
239
  statusText: response.statusText,
224
- data,
240
+ data: dataResult,
225
241
  };
226
242
  throw error;
227
243
  }
228
- return data;
244
+ return dataResult;
229
245
  }
230
246
  catch (err) {
231
247
  // Normalize fetch/AbortError/network errors into an Axios-style shape
@@ -233,10 +249,8 @@ async function performFetchRequest(requestArgs, basePath) {
233
249
  err.code = "ECONNABORTED";
234
250
  }
235
251
  if (!err.response) {
236
- err.request = {
237
- url,
238
- options: requestArgs.options,
239
- };
252
+ // Attach only minimal request info to avoid leaking sensitive data
253
+ err.request = { url };
240
254
  }
241
255
  throw err;
242
256
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vigneshreddy/cms-sdk",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
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",