@codebit-programando-solucoes/codebit-web 1.1.9 → 1.1.11
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 +3 -5
- package/dist/index.cjs +24 -17
- package/package.json +1 -1
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
|
|
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
|
@@ -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
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
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() : 'stream' === finalResponseType ? response.body : 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)=>{
|