@mohasinac/http 1.0.0 → 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.
package/dist/index.cjs CHANGED
@@ -81,13 +81,12 @@ var ApiClient = class {
81
81
  }
82
82
  return url.toString();
83
83
  }
84
- async getHeaders(customHeaders) {
85
- return __spreadValues({
86
- "Content-Type": "application/json"
87
- }, customHeaders);
84
+ async getHeaders(method, customHeaders) {
85
+ const hasBody = !["GET", "HEAD", "DELETE"].includes(method.toUpperCase());
86
+ return __spreadValues(__spreadValues({}, hasBody ? { "Content-Type": "application/json" } : {}), customHeaders);
88
87
  }
89
88
  async request(endpoint, config = {}) {
90
- var _b, _c, _d, _e;
89
+ var _b, _c, _d, _e, _f;
91
90
  const _a = config, {
92
91
  params,
93
92
  timeout = this.defaultTimeout,
@@ -99,8 +98,9 @@ var ApiClient = class {
99
98
  "headers",
100
99
  "signal"
101
100
  ]);
101
+ const method = ((_b = fetchConfig.method) != null ? _b : "GET").toUpperCase();
102
102
  const url = this.buildURL(endpoint, params);
103
- const headers = await this.getHeaders(customHeaders);
103
+ const headers = await this.getHeaders(method, customHeaders);
104
104
  const controller = new AbortController();
105
105
  const timeoutId = setTimeout(() => controller.abort(), timeout);
106
106
  let onAbort = null;
@@ -125,14 +125,14 @@ var ApiClient = class {
125
125
  }));
126
126
  if (!response.ok) {
127
127
  throw new ApiClientError(
128
- (_c = (_b = data.error) != null ? _b : data.message) != null ? _c : `Request failed with status ${response.status}`,
128
+ (_d = (_c = data.error) != null ? _c : data.message) != null ? _d : `Request failed with status ${response.status}`,
129
129
  response.status,
130
130
  data
131
131
  );
132
132
  }
133
133
  if (!data.success) {
134
134
  throw new ApiClientError(
135
- (_e = (_d = data.error) != null ? _d : data.message) != null ? _e : "Request failed",
135
+ (_f = (_e = data.error) != null ? _e : data.message) != null ? _f : "Request failed",
136
136
  response.status,
137
137
  data
138
138
  );
@@ -185,7 +185,7 @@ var ApiClient = class {
185
185
  }
186
186
  async upload(endpoint, formData, config) {
187
187
  const _a = config != null ? config : {}, { headers } = _a, restConfig = __objRest(_a, ["headers"]);
188
- const uploadHeaders = await this.getHeaders(headers);
188
+ const uploadHeaders = await this.getHeaders("POST", headers);
189
189
  delete uploadHeaders["Content-Type"];
190
190
  return this.request(endpoint, __spreadProps(__spreadValues({}, restConfig), {
191
191
  method: "POST",
package/dist/index.js CHANGED
@@ -56,13 +56,12 @@ var ApiClient = class {
56
56
  }
57
57
  return url.toString();
58
58
  }
59
- async getHeaders(customHeaders) {
60
- return __spreadValues({
61
- "Content-Type": "application/json"
62
- }, customHeaders);
59
+ async getHeaders(method, customHeaders) {
60
+ const hasBody = !["GET", "HEAD", "DELETE"].includes(method.toUpperCase());
61
+ return __spreadValues(__spreadValues({}, hasBody ? { "Content-Type": "application/json" } : {}), customHeaders);
63
62
  }
64
63
  async request(endpoint, config = {}) {
65
- var _b, _c, _d, _e;
64
+ var _b, _c, _d, _e, _f;
66
65
  const _a = config, {
67
66
  params,
68
67
  timeout = this.defaultTimeout,
@@ -74,8 +73,9 @@ var ApiClient = class {
74
73
  "headers",
75
74
  "signal"
76
75
  ]);
76
+ const method = ((_b = fetchConfig.method) != null ? _b : "GET").toUpperCase();
77
77
  const url = this.buildURL(endpoint, params);
78
- const headers = await this.getHeaders(customHeaders);
78
+ const headers = await this.getHeaders(method, customHeaders);
79
79
  const controller = new AbortController();
80
80
  const timeoutId = setTimeout(() => controller.abort(), timeout);
81
81
  let onAbort = null;
@@ -100,14 +100,14 @@ var ApiClient = class {
100
100
  }));
101
101
  if (!response.ok) {
102
102
  throw new ApiClientError(
103
- (_c = (_b = data.error) != null ? _b : data.message) != null ? _c : `Request failed with status ${response.status}`,
103
+ (_d = (_c = data.error) != null ? _c : data.message) != null ? _d : `Request failed with status ${response.status}`,
104
104
  response.status,
105
105
  data
106
106
  );
107
107
  }
108
108
  if (!data.success) {
109
109
  throw new ApiClientError(
110
- (_e = (_d = data.error) != null ? _d : data.message) != null ? _e : "Request failed",
110
+ (_f = (_e = data.error) != null ? _e : data.message) != null ? _f : "Request failed",
111
111
  response.status,
112
112
  data
113
113
  );
@@ -160,7 +160,7 @@ var ApiClient = class {
160
160
  }
161
161
  async upload(endpoint, formData, config) {
162
162
  const _a = config != null ? config : {}, { headers } = _a, restConfig = __objRest(_a, ["headers"]);
163
- const uploadHeaders = await this.getHeaders(headers);
163
+ const uploadHeaders = await this.getHeaders("POST", headers);
164
164
  delete uploadHeaders["Content-Type"];
165
165
  return this.request(endpoint, __spreadProps(__spreadValues({}, restConfig), {
166
166
  method: "POST",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mohasinac/http",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },