chore: watch api
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import qs from 'query-string';
|
||||
|
||||
/**
|
||||
*
|
||||
* @param params data: for post request, params: for get request
|
||||
* @returns
|
||||
*/
|
||||
export const fetchChunkedData = async (params: {
|
||||
data?: any;
|
||||
url: string;
|
||||
params?: any;
|
||||
method?: string;
|
||||
}) => {
|
||||
const method = params.method || 'POST';
|
||||
let url = params.url;
|
||||
if (params.params) {
|
||||
url = `${url}?${qs.stringify(params.params)}`;
|
||||
}
|
||||
const response = await fetch(url, {
|
||||
method,
|
||||
body: method === 'POST' ? JSON.stringify(params.data) : null,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('Network response was not ok');
|
||||
return null;
|
||||
}
|
||||
const reader = response?.body?.getReader();
|
||||
const decoder = new TextDecoder('utf-8');
|
||||
return {
|
||||
reader,
|
||||
decoder
|
||||
};
|
||||
};
|
||||
|
||||
export const readStreamData = async (
|
||||
reader: any,
|
||||
decoder: TextDecoder,
|
||||
callback: (data: any) => void
|
||||
) => {
|
||||
const { done, value } = await reader.read();
|
||||
|
||||
if (done) {
|
||||
return;
|
||||
}
|
||||
|
||||
let chunk = decoder.decode(value, { stream: true });
|
||||
console.log('chunk==========', chunk);
|
||||
callback(chunk);
|
||||
await readStreamData(reader, decoder, callback);
|
||||
};
|
||||
+7
-6
@@ -19,20 +19,21 @@ export const handleBatchRequest = async (
|
||||
return Promise.all(list.map((item) => fn(item)));
|
||||
};
|
||||
|
||||
export const convertFileSize = (sizeInBytes: number) => {
|
||||
export const convertFileSize = (sizeInBytes: number, prec?: number) => {
|
||||
const precision = prec ?? 2;
|
||||
if (!sizeInBytes) {
|
||||
return '0 B';
|
||||
}
|
||||
if (sizeInBytes < 1024) {
|
||||
return `${sizeInBytes.toFixed(2)} B`;
|
||||
return `${sizeInBytes.toFixed(precision)} B`;
|
||||
} else if (sizeInBytes < 1024 * 1024) {
|
||||
return `${(sizeInBytes / 1024).toFixed(2)} KB`;
|
||||
return `${(sizeInBytes / 1024).toFixed(precision)} KiB`;
|
||||
} else if (sizeInBytes < 1024 * 1024 * 1024) {
|
||||
return `${(sizeInBytes / (1024 * 1024)).toFixed(2)} MB`;
|
||||
return `${(sizeInBytes / (1024 * 1024)).toFixed(precision)} MiB`;
|
||||
} else if (sizeInBytes < 1024 * 1024 * 1024 * 1024) {
|
||||
return `${(sizeInBytes / (1024 * 1024 * 1024)).toFixed(2)} GB`;
|
||||
return `${(sizeInBytes / (1024 * 1024 * 1024)).toFixed(precision)} GiB`;
|
||||
} else {
|
||||
return `${(sizeInBytes / (1024 * 1024 * 1024 * 1024)).toFixed(2)} TB`;
|
||||
return `${(sizeInBytes / (1024 * 1024 * 1024 * 1024)).toFixed(precision)} TiB`;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user