shred-api-client 2.3.4 → 2.3.5-rc.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.
package/dist/index.js CHANGED
@@ -48,7 +48,7 @@ __export(index_exports, {
48
48
  module.exports = __toCommonJS(index_exports);
49
49
 
50
50
  // src/util/HTTPClient.ts
51
- var import_cross_fetch = __toESM(require("cross-fetch"));
51
+ var import_axios = __toESM(require("axios"));
52
52
  var HOSTS = {
53
53
  prod: "https://api.shreditapp.com",
54
54
  staging: "https://api.staging.shreditapp.com",
@@ -73,37 +73,40 @@ var HTTPClient = class {
73
73
  if (context?.requestId) {
74
74
  headers["X-Request-ID"] = context.requestId;
75
75
  }
76
- const fetchOptions = {
77
- method,
78
- headers
79
- };
80
- if (method !== "GET" && data) {
81
- fetchOptions.body = useLegacyApi ? new URLSearchParams(data).toString() : JSON.stringify(data);
82
- }
83
76
  try {
84
- const response = await (0, import_cross_fetch.default)(url, fetchOptions);
85
- if (!response.ok) {
86
- const errorText = await response.text();
87
- throw {
88
- status: response.status,
89
- statusText: response.statusText,
90
- body: errorText
91
- };
92
- }
77
+ const response = await import_axios.default.request({
78
+ url,
79
+ method,
80
+ headers,
81
+ data: method !== "GET" && data ? useLegacyApi ? new URLSearchParams(data).toString() : data : void 0,
82
+ transformResponse: [(raw) => raw]
83
+ // evita parse automático para podermos tratar manual
84
+ });
93
85
  try {
94
- return await response.json();
86
+ return JSON.parse(response.data);
95
87
  } catch {
96
- return await response.text();
88
+ return response.data;
97
89
  }
98
- } catch (error) {
90
+ } catch (err) {
91
+ const error = err;
99
92
  let parsedBody = {};
100
- try {
101
- parsedBody = error.body ? JSON.parse(error.body) : {};
102
- } catch {
103
- parsedBody = { message: error.body || "Unknown error" };
93
+ if (error.response) {
94
+ try {
95
+ parsedBody = typeof error.response.data === "string" ? JSON.parse(error.response.data) : error.response.data;
96
+ } catch {
97
+ parsedBody = {
98
+ message: error.response.data?.message || error.response.statusText || "Unknown error"
99
+ };
100
+ }
101
+ throw {
102
+ message: parsedBody.message || "Unexpected error occurred",
103
+ status: error.response.status,
104
+ body: error.response.data,
105
+ original: error
106
+ };
104
107
  }
105
108
  throw {
106
- message: parsedBody.message || "Unexpected error occurred",
109
+ message: error.message || "Network error",
107
110
  original: error
108
111
  };
109
112
  }