@codebit-programando-solucoes/codebit-web 1.1.8 → 1.1.10

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/api/api.d.ts CHANGED
@@ -10,6 +10,8 @@ export interface RequestConfig {
10
10
  timeout?: number;
11
11
  /** AbortSignal for request cancellation */
12
12
  signal?: AbortSignal;
13
+ /** Desired response format */
14
+ responseType?: 'json' | 'text' | 'blob' | 'arraybuffer';
13
15
  }
14
16
 
15
17
  /** API response object. */
@@ -23,11 +25,7 @@ export interface ApiResponse<T = any> {
23
25
  }
24
26
 
25
27
  /** Stream options configuration. */
26
- export interface StreamOptions extends Omit<RequestConfig, 'data'> {
27
- /** HTTP method */
28
- method?: string;
29
- /** Request body data */
30
- data?: any;
28
+ export interface StreamOptions extends RequestConfig {
31
29
  /** Callback function to handle each chunk */
32
30
  onChunk: (chunk: string) => void;
33
31
  }
package/dist/index.cjs CHANGED
@@ -68,7 +68,7 @@ async function createApiError(response, request) {
68
68
  let responseData;
69
69
  try {
70
70
  const contentType = response.headers.get('content-type');
71
- responseData = response.bodyUsed ? null : contentType && contentType.includes('application/json') ? await response.json() : await response.text();
71
+ responseData = response.bodyUsed ? null : contentType?.includes('application/json') ? await response.json() : await response.text();
72
72
  } catch {
73
73
  responseData = null;
74
74
  }
@@ -159,8 +159,21 @@ function prepareBodyAndContentType(config) {
159
159
  }
160
160
  }
161
161
  }
162
+ function isBinaryContent(contentType) {
163
+ if (!contentType) return false;
164
+ const binaryTypes = [
165
+ 'image/',
166
+ 'application/octet-stream',
167
+ 'application/pdf',
168
+ 'audio/',
169
+ 'video/',
170
+ 'application/zip',
171
+ 'application/gzip'
172
+ ];
173
+ return binaryTypes.some((type)=>contentType.startsWith(type));
174
+ }
162
175
  async function makeRequest(url, options = {}) {
163
- const { signal: externalSignal, timeout = API_CONFIG.timeout, headers, ...restOptions } = options;
176
+ const { signal: externalSignal, timeout = API_CONFIG.timeout, headers, responseType, ...restOptions } = options;
164
177
  const { timeoutController, timer, fullUrl } = setupRequestControl(url, externalSignal, timeout);
165
178
  const config = {
166
179
  credentials: 'include',
@@ -179,22 +192,16 @@ async function makeRequest(url, options = {}) {
179
192
  }
180
193
  if (!response.ok) throw await createApiError(response, config);
181
194
  const contentType = response.headers.get('content-type');
182
- if (contentType && contentType.includes('application/json')) {
183
- const data = await response.json();
184
- return {
185
- data,
186
- status: response.status,
187
- headers: response.headers
188
- };
189
- }
190
- {
191
- const data = await response.text();
192
- return {
193
- data,
194
- status: response.status,
195
- headers: response.headers
196
- };
197
- }
195
+ let inferredResponseType;
196
+ inferredResponseType = contentType?.includes('application/json') ? 'json' : isBinaryContent(contentType) ? 'blob' : 'text';
197
+ const finalResponseType = responseType || inferredResponseType;
198
+ let data;
199
+ data = 'json' === finalResponseType ? await response.json() : 'blob' === finalResponseType ? await response.blob() : 'arraybuffer' === finalResponseType ? await response.arrayBuffer() : await response.text();
200
+ return {
201
+ data,
202
+ status: response.status,
203
+ headers: response.headers
204
+ };
198
205
  }
199
206
  const Api = {
200
207
  configure: (config)=>{
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@codebit-programando-solucoes/codebit-web",
3
- "version": "1.1.8",
3
+ "version": "1.1.10",
4
4
  "main": "./dist/index.cjs",
5
5
  "type": "module",
6
- "types": "./src/index.d.ts",
6
+ "types": "./dist/index.d.ts",
7
7
  "description": "",
8
8
  "author": "",
9
9
  "license": "UNLICENSED",