chore: watch api

This commit is contained in:
jialin
2024-06-28 17:14:34 +08:00
parent 871bca1916
commit cf0113e8d8
35 changed files with 1171 additions and 627 deletions
+53
View File
@@ -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
View File
@@ -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`;
}
};