@cloudparker/moldex.js 4.1.6 → 4.1.7
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/package.json +6 -5
- package/dist/actions/badge.d.ts +0 -12
- package/dist/actions/badge.js +0 -22
- package/dist/actions/index.d.ts +0 -3
- package/dist/actions/index.js +0 -3
- package/dist/actions/no-context-menu.d.ts +0 -3
- package/dist/actions/no-context-menu.js +0 -11
- package/dist/actions/ripple.css +0 -29
- package/dist/actions/ripple.d.ts +0 -7
- package/dist/actions/ripple.js +0 -74
- package/dist/index.d.ts +0 -3
- package/dist/index.js +0 -3
- package/dist/services/date/date-service.d.ts +0 -52
- package/dist/services/date/date-service.js +0 -206
- package/dist/services/dialog/dialog-service.d.ts +0 -112
- package/dist/services/dialog/dialog-service.js +0 -357
- package/dist/services/index.d.ts +0 -12
- package/dist/services/index.js +0 -12
- package/dist/services/navigation/navigation-service.d.ts +0 -39
- package/dist/services/navigation/navigation-service.js +0 -100
- package/dist/services/screen/screen-service.d.ts +0 -17
- package/dist/services/screen/screen-service.js +0 -39
- package/dist/services/toast/toast-service.d.ts +0 -5
- package/dist/services/toast/toast-service.js +0 -26
- package/dist/services/utils/color-service.d.ts +0 -46
- package/dist/services/utils/color-service.js +0 -73
- package/dist/services/utils/currency-service.d.ts +0 -91
- package/dist/services/utils/currency-service.js +0 -140
- package/dist/services/utils/download-service.d.ts +0 -91
- package/dist/services/utils/download-service.js +0 -159
- package/dist/services/utils/file-service.d.ts +0 -140
- package/dist/services/utils/file-service.js +0 -301
- package/dist/services/utils/http-service.d.ts +0 -77
- package/dist/services/utils/http-service.js +0 -158
- package/dist/services/utils/image-service.d.ts +0 -107
- package/dist/services/utils/image-service.js +0 -260
- package/dist/services/utils/melody-service.d.ts +0 -5
- package/dist/services/utils/melody-service.js +0 -41
- package/dist/services/utils/utils-service.d.ts +0 -260
- package/dist/services/utils/utils-service.js +0 -413
- package/dist/stores/referrer-store/referrer-store.svelte.d.ts +0 -3
- package/dist/stores/referrer-store/referrer-store.svelte.js +0 -11
- package/dist/stores/screen-size/screen-size-store.svelte.d.ts +0 -18
- package/dist/stores/screen-size/screen-size-store.svelte.js +0 -41
- package/dist/types.d.ts +0 -6
- package/dist/types.js +0 -7
- package/dist/views/core/button/components/button/button.svelte +0 -209
- package/dist/views/core/button/components/button/button.svelte.d.ts +0 -42
- package/dist/views/core/button/components/button-back/button-back.svelte +0 -46
- package/dist/views/core/button/components/button-back/button-back.svelte.d.ts +0 -11
- package/dist/views/core/button/components/button-close/button-close.svelte +0 -7
- package/dist/views/core/button/components/button-close/button-close.svelte.d.ts +0 -4
- package/dist/views/core/button/components/button-close-icon/button-close-icon.svelte +0 -47
- package/dist/views/core/button/components/button-close-icon/button-close-icon.svelte.d.ts +0 -11
- package/dist/views/core/button/components/button-dropdown/button-dropdown.svelte +0 -152
- package/dist/views/core/button/components/button-dropdown/button-dropdown.svelte.d.ts +0 -24
- package/dist/views/core/button/components/button-list-item/button-list-item.svelte +0 -184
- package/dist/views/core/button/components/button-list-item/button-list-item.svelte.d.ts +0 -48
- package/dist/views/core/button/components/button-menu/button-menu.svelte +0 -122
- package/dist/views/core/button/components/button-menu/button-menu.svelte.d.ts +0 -32
- package/dist/views/core/button/components/button-ok/button-ok.svelte +0 -7
- package/dist/views/core/button/components/button-ok/button-ok.svelte.d.ts +0 -4
- package/dist/views/core/button/components/button-search/button-search.svelte +0 -75
- package/dist/views/core/button/components/button-search/button-search.svelte.d.ts +0 -20
- package/dist/views/core/button/components/switch/switch.svelte +0 -70
- package/dist/views/core/button/components/switch/switch.svelte.d.ts +0 -11
- package/dist/views/core/button/index.d.ts +0 -16
- package/dist/views/core/button/index.js +0 -11
- package/dist/views/core/common/components/content-area/content-area.svelte +0 -47
- package/dist/views/core/common/components/content-area/content-area.svelte.d.ts +0 -12
- package/dist/views/core/common/components/loading/loading.svelte +0 -14
- package/dist/views/core/common/components/loading/loading.svelte.d.ts +0 -7
- package/dist/views/core/common/components/virtual-scrolling/virtual-scrolling-list.svelte +0 -60
- package/dist/views/core/common/components/virtual-scrolling/virtual-scrolling-list.svelte.d.ts +0 -11
- package/dist/views/core/common/index.d.ts +0 -4
- package/dist/views/core/common/index.js +0 -9
- package/dist/views/core/dialog/components/cropper-dialog/cropper-dialog.svelte +0 -63
- package/dist/views/core/dialog/components/cropper-dialog/cropper-dialog.svelte.d.ts +0 -15
- package/dist/views/core/dialog/components/dialog/dialog.svelte +0 -455
- package/dist/views/core/dialog/components/dialog/dialog.svelte.d.ts +0 -92
- package/dist/views/core/dialog/components/loading-dialog/loading-dialog.svelte +0 -42
- package/dist/views/core/dialog/components/loading-dialog/loading-dialog.svelte.d.ts +0 -12
- package/dist/views/core/dialog/components/msg-dialog/msg-dialog.svelte +0 -22
- package/dist/views/core/dialog/components/msg-dialog/msg-dialog.svelte.d.ts +0 -9
- package/dist/views/core/dialog/components/number-field-dialog/number-field-dialog.svelte +0 -57
- package/dist/views/core/dialog/components/number-field-dialog/number-field-dialog.svelte.d.ts +0 -14
- package/dist/views/core/dialog/components/picker-dialog/picker-dialog.svelte +0 -207
- package/dist/views/core/dialog/components/picker-dialog/picker-dialog.svelte.d.ts +0 -32
- package/dist/views/core/dialog/components/text-field-dialog/text-field-dialog.svelte +0 -56
- package/dist/views/core/dialog/components/text-field-dialog/text-field-dialog.svelte.d.ts +0 -15
- package/dist/views/core/dialog/components/textarea-field-dialog/textarea-field-dialog.svelte +0 -56
- package/dist/views/core/dialog/components/textarea-field-dialog/textarea-field-dialog.svelte.d.ts +0 -15
- package/dist/views/core/dialog/index.d.ts +0 -4
- package/dist/views/core/dialog/index.js +0 -2
- package/dist/views/core/drawer/components/drawer/drawer.svelte +0 -110
- package/dist/views/core/drawer/components/drawer/drawer.svelte.d.ts +0 -19
- package/dist/views/core/drawer/index.d.ts +0 -2
- package/dist/views/core/drawer/index.js +0 -2
- package/dist/views/core/icon/components/icon/icon.svelte +0 -27
- package/dist/views/core/icon/components/icon/icon.svelte.d.ts +0 -12
- package/dist/views/core/icon/components/icon-circle/icon-circle.svelte +0 -17
- package/dist/views/core/icon/components/icon-circle/icon-circle.svelte.d.ts +0 -8
- package/dist/views/core/icon/index.d.ts +0 -4
- package/dist/views/core/icon/index.js +0 -4
- package/dist/views/core/icon/services/icon-path-service.d.ts +0 -23
- package/dist/views/core/icon/services/icon-path-service.js +0 -24
- package/dist/views/core/index.d.ts +0 -16
- package/dist/views/core/index.js +0 -16
- package/dist/views/core/input/components/checkbox-field/checkbox-field.svelte +0 -83
- package/dist/views/core/input/components/checkbox-field/checkbox-field.svelte.d.ts +0 -19
- package/dist/views/core/input/components/color-field/color-field.svelte +0 -136
- package/dist/views/core/input/components/color-field/color-field.svelte.d.ts +0 -11
- package/dist/views/core/input/components/combobox-field/combobox-field.svelte +0 -542
- package/dist/views/core/input/components/combobox-field/combobox-field.svelte.d.ts +0 -56
- package/dist/views/core/input/components/date-field/date-field.svelte +0 -43
- package/dist/views/core/input/components/date-field/date-field.svelte.d.ts +0 -11
- package/dist/views/core/input/components/datetime-field/datetime-field.svelte +0 -21
- package/dist/views/core/input/components/datetime-field/datetime-field.svelte.d.ts +0 -8
- package/dist/views/core/input/components/email-field/email-field.svelte +0 -26
- package/dist/views/core/input/components/email-field/email-field.svelte.d.ts +0 -8
- package/dist/views/core/input/components/file-field/file-field.svelte +0 -105
- package/dist/views/core/input/components/file-field/file-field.svelte.d.ts +0 -13
- package/dist/views/core/input/components/input-field/input-field.svelte +0 -416
- package/dist/views/core/input/components/input-field/input-field.svelte.d.ts +0 -69
- package/dist/views/core/input/components/label/label.svelte +0 -48
- package/dist/views/core/input/components/label/label.svelte.d.ts +0 -14
- package/dist/views/core/input/components/number-field/number-field.svelte +0 -21
- package/dist/views/core/input/components/number-field/number-field.svelte.d.ts +0 -8
- package/dist/views/core/input/components/password-field/password-field.svelte +0 -103
- package/dist/views/core/input/components/password-field/password-field.svelte.d.ts +0 -12
- package/dist/views/core/input/components/phone-field/phone-field.svelte +0 -207
- package/dist/views/core/input/components/phone-field/phone-field.svelte.d.ts +0 -33
- package/dist/views/core/input/components/radio-field/radio-field.svelte +0 -151
- package/dist/views/core/input/components/radio-field/radio-field.svelte.d.ts +0 -32
- package/dist/views/core/input/components/range-field/range-field.svelte +0 -119
- package/dist/views/core/input/components/range-field/range-field.svelte.d.ts +0 -23
- package/dist/views/core/input/components/search-field/search-field.svelte +0 -79
- package/dist/views/core/input/components/search-field/search-field.svelte.d.ts +0 -12
- package/dist/views/core/input/components/text-field/text-field.svelte +0 -30
- package/dist/views/core/input/components/text-field/text-field.svelte.d.ts +0 -11
- package/dist/views/core/input/components/textarea-field/textarea-field.svelte +0 -26
- package/dist/views/core/input/components/textarea-field/textarea-field.svelte.d.ts +0 -8
- package/dist/views/core/input/components/time-field/time-field.svelte +0 -20
- package/dist/views/core/input/components/time-field/time-field.svelte.d.ts +0 -8
- package/dist/views/core/input/index.d.ts +0 -23
- package/dist/views/core/input/index.js +0 -20
- package/dist/views/core/navbar/components/navbar/navbar.svelte +0 -170
- package/dist/views/core/navbar/components/navbar/navbar.svelte.d.ts +0 -38
- package/dist/views/core/navbar/index.d.ts +0 -4
- package/dist/views/core/navbar/index.js +0 -3
- package/dist/views/core/no-data/components/no-data/no-data.svelte +0 -47
- package/dist/views/core/no-data/components/no-data/no-data.svelte.d.ts +0 -13
- package/dist/views/core/no-data/index.d.ts +0 -2
- package/dist/views/core/no-data/index.js +0 -2
- package/dist/views/core/pagination/components/pagination/pagination.svelte +0 -151
- package/dist/views/core/pagination/components/pagination/pagination.svelte.d.ts +0 -16
- package/dist/views/core/pagination/index.d.ts +0 -2
- package/dist/views/core/pagination/index.js +0 -2
- package/dist/views/core/progressbar/components/progressbar/progressbar.svelte +0 -55
- package/dist/views/core/progressbar/components/progressbar/progressbar.svelte.d.ts +0 -11
- package/dist/views/core/progressbar/index.d.ts +0 -2
- package/dist/views/core/progressbar/index.js +0 -2
- package/dist/views/core/ruler/components/vertical-ruler/verticcal-ruler.svelte +0 -8
- package/dist/views/core/ruler/components/vertical-ruler/verticcal-ruler.svelte.d.ts +0 -6
- package/dist/views/core/ruler/index.d.ts +0 -2
- package/dist/views/core/ruler/index.js +0 -2
- package/dist/views/core/screen-detector/components/screen-detector.svelte +0 -17
- package/dist/views/core/screen-detector/components/screen-detector.svelte.d.ts +0 -3
- package/dist/views/core/screen-detector/index.d.ts +0 -2
- package/dist/views/core/screen-detector/index.js +0 -2
- package/dist/views/core/sidebar/components/sidebar.svelte +0 -49
- package/dist/views/core/sidebar/components/sidebar.svelte.d.ts +0 -12
- package/dist/views/core/sidebar/index.d.ts +0 -2
- package/dist/views/core/sidebar/index.js +0 -5
- package/dist/views/core/spinner/components/spinner/spinner.svelte +0 -21
- package/dist/views/core/spinner/components/spinner/spinner.svelte.d.ts +0 -6
- package/dist/views/core/spinner/index.d.ts +0 -2
- package/dist/views/core/spinner/index.js +0 -2
- package/dist/views/core/text/components/text-await/text-await.svelte +0 -23
- package/dist/views/core/text/components/text-await/text-await.svelte.d.ts +0 -10
- package/dist/views/core/text/components/text-copy/text-copy.svelte +0 -40
- package/dist/views/core/text/components/text-copy/text-copy.svelte.d.ts +0 -10
- package/dist/views/core/text/components/text-currency/text-currency.svelte +0 -24
- package/dist/views/core/text/components/text-currency/text-currency.svelte.d.ts +0 -11
- package/dist/views/core/text/components/text-date/text-date.svelte +0 -40
- package/dist/views/core/text/components/text-date/text-date.svelte.d.ts +0 -10
- package/dist/views/core/text/components/text-email/text-email.svelte +0 -22
- package/dist/views/core/text/components/text-email/text-email.svelte.d.ts +0 -9
- package/dist/views/core/text/components/text-html/text-html.svelte +0 -7
- package/dist/views/core/text/components/text-html/text-html.svelte.d.ts +0 -6
- package/dist/views/core/text/components/text-phone/text-phone.svelte +0 -25
- package/dist/views/core/text/components/text-phone/text-phone.svelte.d.ts +0 -9
- package/dist/views/core/text/index.d.ts +0 -8
- package/dist/views/core/text/index.js +0 -8
- package/dist/views/core/toast/components/toast/toast.svelte +0 -64
- package/dist/views/core/toast/components/toast/toast.svelte.d.ts +0 -14
- package/dist/views/core/toast/index.d.ts +0 -2
- package/dist/views/core/toast/index.js +0 -1
- package/dist/views/extra/fields/country-combobox-field.svelte +0 -42
- package/dist/views/extra/fields/country-combobox-field.svelte.d.ts +0 -8
- package/dist/views/extra/index.d.ts +0 -6
- package/dist/views/extra/index.js +0 -5
- package/dist/views/extra/loaders/country-loader.svelte +0 -44
- package/dist/views/extra/loaders/country-loader.svelte.d.ts +0 -16
- package/dist/views/extra/texts/text-country-state.svelte +0 -48
- package/dist/views/extra/texts/text-country-state.svelte.d.ts +0 -6
- package/dist/views/extra/texts/text-country.svelte +0 -21
- package/dist/views/extra/texts/text-country.svelte.d.ts +0 -6
- package/dist/views/index.d.ts +0 -2
- package/dist/views/index.js +0 -2
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
export declare const FILE_EXTENTIONS: string[];
|
|
2
|
-
export declare const FILE_VIEWER_HOST = "https://viewer.cloudparker.com";
|
|
3
|
-
/**
|
|
4
|
-
* Reads a file as a data URL.
|
|
5
|
-
* @param file - The file to read.
|
|
6
|
-
* @returns A promise that resolves to a data URL string.
|
|
7
|
-
*/
|
|
8
|
-
export declare function readFileAsDataURL(file: File): Promise<string>;
|
|
9
|
-
/**
|
|
10
|
-
* Reads the contents of a given File object as a text string.
|
|
11
|
-
*
|
|
12
|
-
* This function uses the FileReader API to read the file content and returns it as a string.
|
|
13
|
-
*
|
|
14
|
-
* @param file - The File object to be read.
|
|
15
|
-
* @returns A promise that resolves to the text content of the file as a string.
|
|
16
|
-
*
|
|
17
|
-
* @throws An error if the file could not be read.
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* // Create a file and read its content as text
|
|
21
|
-
* const myFile = new File(['Hello, world!'], 'example.txt', { type: 'text/plain' });
|
|
22
|
-
* readFileAsText(myFile).then((text) => {
|
|
23
|
-
* console.log(text); // Output: "Hello, world!"
|
|
24
|
-
* });
|
|
25
|
-
*/
|
|
26
|
-
export declare function readFileAsText(file: File): Promise<string>;
|
|
27
|
-
/**
|
|
28
|
-
* Reads the contents of a given File object as an ArrayBuffer.
|
|
29
|
-
*
|
|
30
|
-
* This function uses the FileReader API to read the file content as a binary ArrayBuffer.
|
|
31
|
-
*
|
|
32
|
-
* @param file - The File object to be read.
|
|
33
|
-
* @returns A promise that resolves to the binary content of the file as an ArrayBuffer.
|
|
34
|
-
*
|
|
35
|
-
* @throws An error if the file could not be read.
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* // Create a file and read its content as an ArrayBuffer
|
|
39
|
-
* const myFile = new File(['Hello, world!'], 'example.txt', { type: 'text/plain' });
|
|
40
|
-
* readFileAsBuffer(myFile).then((buffer) => {
|
|
41
|
-
* console.log(buffer); // Output: ArrayBuffer { ... }
|
|
42
|
-
* });
|
|
43
|
-
*/
|
|
44
|
-
export declare function readFileAsBuffer(file: File): Promise<ArrayBuffer>;
|
|
45
|
-
/**
|
|
46
|
-
* Converts a given URL to a File object by downloading the content from the URL.
|
|
47
|
-
*
|
|
48
|
-
* This function fetches the file content from the provided URL and converts it into a File object.
|
|
49
|
-
* It attempts to extract the filename and MIME type from the HTTP headers. If these are not available,
|
|
50
|
-
* it falls back to using the provided filename and MIME type or extracts the filename from the URL.
|
|
51
|
-
*
|
|
52
|
-
* @param url - The URL to download the file from.
|
|
53
|
-
* @param givenFileName - Optional. The fallback name for the file if no name can be extracted from the URL or headers.
|
|
54
|
-
* @param givenMimeType - Optional. The fallback MIME type for the file if no MIME type is available in the headers.
|
|
55
|
-
* @returns A promise that resolves to a File object containing the downloaded content.
|
|
56
|
-
*
|
|
57
|
-
* @throws An error if the download fails or the file cannot be converted.
|
|
58
|
-
*
|
|
59
|
-
* @example
|
|
60
|
-
* // Convert an image URL to a File object and log its properties
|
|
61
|
-
* readUrlAsFile('https://example.com/image.png', 'downloaded-image.png', 'image/png')
|
|
62
|
-
* .then((file) => {
|
|
63
|
-
* console.log(file.name); // Output: "downloaded-image.png"
|
|
64
|
-
* console.log(file.type); // Output: "image/png"
|
|
65
|
-
* })
|
|
66
|
-
* .catch((error) => console.error('Error converting URL to file:', error.message));
|
|
67
|
-
*/
|
|
68
|
-
export declare function readUrlAsFile(url: string, givenFileName?: string, givenMimeType?: string): Promise<File>;
|
|
69
|
-
/**
|
|
70
|
-
* Converts a given Data URL (Base64 or Data URL format) to a File object.
|
|
71
|
-
*
|
|
72
|
-
* @param dataUrl - The Data URL string to convert into a File object.
|
|
73
|
-
* @param fileName - Optional. The name for the created file.
|
|
74
|
-
* @param mimeType - Optional. The MIME type of the file. If not provided, it will be extracted from the Data URL.
|
|
75
|
-
* @returns A promise that resolves to a File object containing the content of the Data URL.
|
|
76
|
-
*
|
|
77
|
-
* @example
|
|
78
|
-
* // Convert a Base64 string to a File object
|
|
79
|
-
* const base64String = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...';
|
|
80
|
-
* readDataUrlAsFile(base64String, 'example.png', 'image/png')
|
|
81
|
-
* .then((file) => {
|
|
82
|
-
* console.log(file.name); // Output: "example.png"
|
|
83
|
-
* console.log(file.type); // Output: "image/png"
|
|
84
|
-
* });
|
|
85
|
-
*/
|
|
86
|
-
export declare function readDataUrlAsFile(dataUrl: string, fileName?: string, mimeType?: string): Promise<File>;
|
|
87
|
-
/**
|
|
88
|
-
* Converts a given ArrayBuffer to a File object.
|
|
89
|
-
*
|
|
90
|
-
* This function wraps an ArrayBuffer in a File object with the provided filename and MIME type.
|
|
91
|
-
*
|
|
92
|
-
* @param buffer - The ArrayBuffer containing the file data.
|
|
93
|
-
* @param fileName - The name for the created file.
|
|
94
|
-
* @param mimeType - The MIME type for the created file.
|
|
95
|
-
* @returns A promise that resolves to a File object created from the ArrayBuffer.
|
|
96
|
-
*
|
|
97
|
-
* @example
|
|
98
|
-
* // Convert an ArrayBuffer to a File object
|
|
99
|
-
* const buffer = new ArrayBuffer(8);
|
|
100
|
-
* convertBufferToFile(buffer, 'example.bin', 'application/octet-stream').then((file) => {
|
|
101
|
-
* console.log(file.name); // Output: "example.bin"
|
|
102
|
-
* console.log(file.type); // Output: "application/octet-stream"
|
|
103
|
-
* });
|
|
104
|
-
*/
|
|
105
|
-
export declare function convertBufferToFile(buffer: ArrayBuffer, fileName: string, mimeType: string): Promise<File>;
|
|
106
|
-
/**
|
|
107
|
-
* Extracts the filename and extension from a given filename string.
|
|
108
|
-
*
|
|
109
|
-
* This function splits a filename into its base name and extension parts.
|
|
110
|
-
* If no extension is present, the extension will be `undefined`.
|
|
111
|
-
*
|
|
112
|
-
* @param filename - The filename string to be processed.
|
|
113
|
-
* @returns An object containing the base name and extension of the filename.
|
|
114
|
-
*
|
|
115
|
-
* @example
|
|
116
|
-
* // Extract filename and extension
|
|
117
|
-
* const { name, ext } = getFilenameExtension('example.txt');
|
|
118
|
-
* console.log(name); // Output: "example"
|
|
119
|
-
* console.log(ext); // Output: "txt"
|
|
120
|
-
*/
|
|
121
|
-
export declare function getFilenameExtension(filename: string): {
|
|
122
|
-
name: string;
|
|
123
|
-
ext: string | undefined;
|
|
124
|
-
};
|
|
125
|
-
/**
|
|
126
|
-
* Converts a file size in bytes to a human-readable string.
|
|
127
|
-
*
|
|
128
|
-
* This function converts a file size in bytes to a string representation with appropriate units
|
|
129
|
-
* (e.g., B, kB, MB, GB, TB) based on the size.
|
|
130
|
-
*
|
|
131
|
-
* @param size - The file size in bytes.
|
|
132
|
-
* @returns A human-readable string representing the file size.
|
|
133
|
-
*
|
|
134
|
-
* @example
|
|
135
|
-
* const sizeString = fileSizeString(1024);
|
|
136
|
-
* console.log(sizeString); // Output: "1 kB"
|
|
137
|
-
*/
|
|
138
|
-
export declare function formatFileSize(bytes?: number): string;
|
|
139
|
-
export declare function getExtFromFileType(type: string): string | null;
|
|
140
|
-
export declare function ellipsisFileNameAtCenter(name: string, sizeLimit?: number, startLength?: number, endLength?: number): string;
|
|
@@ -1,301 +0,0 @@
|
|
|
1
|
-
export const FILE_EXTENTIONS = [
|
|
2
|
-
"java", "js", "ts", "json", "c", "cpp", "cs", "py", "ts", "php", "rb", "swift", "go",
|
|
3
|
-
"rs", "kt", "scala", "pl", "lua", "hs", "sh", "ps1", "dart", "jl", "m", "f90", "txt",
|
|
4
|
-
"r", "groovy", "asm", "pas", "ada", "sql", "md", "html", "css", "xml", "yaml", "xml",
|
|
5
|
-
"bat",
|
|
6
|
-
];
|
|
7
|
-
export const FILE_VIEWER_HOST = 'https://viewer.cloudparker.com';
|
|
8
|
-
/**
|
|
9
|
-
* Reads a file as a data URL.
|
|
10
|
-
* @param file - The file to read.
|
|
11
|
-
* @returns A promise that resolves to a data URL string.
|
|
12
|
-
*/
|
|
13
|
-
export function readFileAsDataURL(file) {
|
|
14
|
-
return new Promise((resolve, reject) => {
|
|
15
|
-
const reader = new FileReader();
|
|
16
|
-
reader.onload = () => resolve(reader.result);
|
|
17
|
-
reader.onerror = () => reject(reader.error);
|
|
18
|
-
reader.readAsDataURL(file);
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Reads the contents of a given File object as a text string.
|
|
23
|
-
*
|
|
24
|
-
* This function uses the FileReader API to read the file content and returns it as a string.
|
|
25
|
-
*
|
|
26
|
-
* @param file - The File object to be read.
|
|
27
|
-
* @returns A promise that resolves to the text content of the file as a string.
|
|
28
|
-
*
|
|
29
|
-
* @throws An error if the file could not be read.
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* // Create a file and read its content as text
|
|
33
|
-
* const myFile = new File(['Hello, world!'], 'example.txt', { type: 'text/plain' });
|
|
34
|
-
* readFileAsText(myFile).then((text) => {
|
|
35
|
-
* console.log(text); // Output: "Hello, world!"
|
|
36
|
-
* });
|
|
37
|
-
*/
|
|
38
|
-
export async function readFileAsText(file) {
|
|
39
|
-
return new Promise((resolve, reject) => {
|
|
40
|
-
const reader = new FileReader();
|
|
41
|
-
// Event handler executed when the file reading operation is completed
|
|
42
|
-
reader.onloadend = function () {
|
|
43
|
-
// Check if the file was read successfully
|
|
44
|
-
if (reader.readyState === FileReader.DONE) {
|
|
45
|
-
resolve(reader.result);
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
reject(new Error('Failed to read file as text.'));
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
// Event handler executed when an error occurs while reading the file
|
|
52
|
-
reader.onerror = function () {
|
|
53
|
-
reject(new Error(`Error reading file: ${reader.error?.message || 'Unknown error'}`));
|
|
54
|
-
};
|
|
55
|
-
// Read the file content as a text string
|
|
56
|
-
reader.readAsText(file);
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Reads the contents of a given File object as an ArrayBuffer.
|
|
61
|
-
*
|
|
62
|
-
* This function uses the FileReader API to read the file content as a binary ArrayBuffer.
|
|
63
|
-
*
|
|
64
|
-
* @param file - The File object to be read.
|
|
65
|
-
* @returns A promise that resolves to the binary content of the file as an ArrayBuffer.
|
|
66
|
-
*
|
|
67
|
-
* @throws An error if the file could not be read.
|
|
68
|
-
*
|
|
69
|
-
* @example
|
|
70
|
-
* // Create a file and read its content as an ArrayBuffer
|
|
71
|
-
* const myFile = new File(['Hello, world!'], 'example.txt', { type: 'text/plain' });
|
|
72
|
-
* readFileAsBuffer(myFile).then((buffer) => {
|
|
73
|
-
* console.log(buffer); // Output: ArrayBuffer { ... }
|
|
74
|
-
* });
|
|
75
|
-
*/
|
|
76
|
-
export async function readFileAsBuffer(file) {
|
|
77
|
-
return new Promise((resolve, reject) => {
|
|
78
|
-
const reader = new FileReader();
|
|
79
|
-
// Event handler executed when the file reading operation is completed
|
|
80
|
-
reader.onloadend = () => {
|
|
81
|
-
if (reader.readyState === FileReader.DONE) {
|
|
82
|
-
resolve(reader.result);
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
reject(new Error('Failed to read file as ArrayBuffer.'));
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
// Event handler executed when an error occurs while reading the file
|
|
89
|
-
reader.onerror = () => {
|
|
90
|
-
reject(new Error(`Error reading file as ArrayBuffer: ${reader.error?.message || 'Unknown error'}`));
|
|
91
|
-
};
|
|
92
|
-
// Read the file content as a binary ArrayBuffer
|
|
93
|
-
reader.readAsArrayBuffer(file);
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Converts a given URL to a File object by downloading the content from the URL.
|
|
98
|
-
*
|
|
99
|
-
* This function fetches the file content from the provided URL and converts it into a File object.
|
|
100
|
-
* It attempts to extract the filename and MIME type from the HTTP headers. If these are not available,
|
|
101
|
-
* it falls back to using the provided filename and MIME type or extracts the filename from the URL.
|
|
102
|
-
*
|
|
103
|
-
* @param url - The URL to download the file from.
|
|
104
|
-
* @param givenFileName - Optional. The fallback name for the file if no name can be extracted from the URL or headers.
|
|
105
|
-
* @param givenMimeType - Optional. The fallback MIME type for the file if no MIME type is available in the headers.
|
|
106
|
-
* @returns A promise that resolves to a File object containing the downloaded content.
|
|
107
|
-
*
|
|
108
|
-
* @throws An error if the download fails or the file cannot be converted.
|
|
109
|
-
*
|
|
110
|
-
* @example
|
|
111
|
-
* // Convert an image URL to a File object and log its properties
|
|
112
|
-
* readUrlAsFile('https://example.com/image.png', 'downloaded-image.png', 'image/png')
|
|
113
|
-
* .then((file) => {
|
|
114
|
-
* console.log(file.name); // Output: "downloaded-image.png"
|
|
115
|
-
* console.log(file.type); // Output: "image/png"
|
|
116
|
-
* })
|
|
117
|
-
* .catch((error) => console.error('Error converting URL to file:', error.message));
|
|
118
|
-
*/
|
|
119
|
-
export async function readUrlAsFile(url, givenFileName, givenMimeType) {
|
|
120
|
-
try {
|
|
121
|
-
const response = await fetch(url);
|
|
122
|
-
// Check if the fetch request was successful
|
|
123
|
-
if (!response.ok) {
|
|
124
|
-
throw new Error(`Failed to fetch file from URL: ${response.statusText}`);
|
|
125
|
-
}
|
|
126
|
-
// Extract filename from the Content-Disposition header if available
|
|
127
|
-
const contentDisposition = response.headers.get('Content-Disposition');
|
|
128
|
-
let fileName = "";
|
|
129
|
-
if (contentDisposition) {
|
|
130
|
-
const filenameMatch = contentDisposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/);
|
|
131
|
-
if (filenameMatch && filenameMatch[1]) {
|
|
132
|
-
fileName = filenameMatch[1].replace(/['"]/g, '');
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
// Fallback to using the URL's last segment as the filename if Content-Disposition is not available
|
|
136
|
-
if (!fileName) {
|
|
137
|
-
const urlSegments = url.split('/');
|
|
138
|
-
fileName = urlSegments[urlSegments.length - 1] || 'downloaded-file';
|
|
139
|
-
}
|
|
140
|
-
// Get MIME type from the Content-Type header, if available
|
|
141
|
-
const mimeType = response.headers.get('Content-Type') || givenMimeType || 'application/octet-stream';
|
|
142
|
-
// Read the file content as an ArrayBuffer
|
|
143
|
-
const buffer = await response.arrayBuffer();
|
|
144
|
-
// Create a File object with the buffer, filename, and MIME type
|
|
145
|
-
return new File([buffer], givenFileName || fileName, { type: mimeType });
|
|
146
|
-
}
|
|
147
|
-
catch (error) {
|
|
148
|
-
throw new Error(`Error converting URL to file: ${error.message}`);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Converts a given Data URL (Base64 or Data URL format) to a File object.
|
|
153
|
-
*
|
|
154
|
-
* @param dataUrl - The Data URL string to convert into a File object.
|
|
155
|
-
* @param fileName - Optional. The name for the created file.
|
|
156
|
-
* @param mimeType - Optional. The MIME type of the file. If not provided, it will be extracted from the Data URL.
|
|
157
|
-
* @returns A promise that resolves to a File object containing the content of the Data URL.
|
|
158
|
-
*
|
|
159
|
-
* @example
|
|
160
|
-
* // Convert a Base64 string to a File object
|
|
161
|
-
* const base64String = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...';
|
|
162
|
-
* readDataUrlAsFile(base64String, 'example.png', 'image/png')
|
|
163
|
-
* .then((file) => {
|
|
164
|
-
* console.log(file.name); // Output: "example.png"
|
|
165
|
-
* console.log(file.type); // Output: "image/png"
|
|
166
|
-
* });
|
|
167
|
-
*/
|
|
168
|
-
export async function readDataUrlAsFile(dataUrl, fileName = 'file', mimeType) {
|
|
169
|
-
try {
|
|
170
|
-
// Extract the MIME type from the Data URL if not provided
|
|
171
|
-
const mimeTypeMatch = dataUrl.match(/^data:([^;]+);/);
|
|
172
|
-
const extractedMimeType = mimeTypeMatch ? mimeTypeMatch[1] : mimeType;
|
|
173
|
-
// Convert the Base64 portion of the Data URL to a binary string
|
|
174
|
-
const binaryString = atob(dataUrl.split(',')[1]);
|
|
175
|
-
// Convert the binary string to an ArrayBuffer
|
|
176
|
-
const length = binaryString.length;
|
|
177
|
-
const buffer = new Uint8Array(length);
|
|
178
|
-
for (let i = 0; i < length; i++) {
|
|
179
|
-
buffer[i] = binaryString.charCodeAt(i);
|
|
180
|
-
}
|
|
181
|
-
// Create and return a File object
|
|
182
|
-
return new File([buffer], fileName, { type: extractedMimeType || 'application/octet-stream' });
|
|
183
|
-
}
|
|
184
|
-
catch (error) {
|
|
185
|
-
throw new Error(`Error converting Data URL to file: ${error.message}`);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* Converts a given ArrayBuffer to a File object.
|
|
190
|
-
*
|
|
191
|
-
* This function wraps an ArrayBuffer in a File object with the provided filename and MIME type.
|
|
192
|
-
*
|
|
193
|
-
* @param buffer - The ArrayBuffer containing the file data.
|
|
194
|
-
* @param fileName - The name for the created file.
|
|
195
|
-
* @param mimeType - The MIME type for the created file.
|
|
196
|
-
* @returns A promise that resolves to a File object created from the ArrayBuffer.
|
|
197
|
-
*
|
|
198
|
-
* @example
|
|
199
|
-
* // Convert an ArrayBuffer to a File object
|
|
200
|
-
* const buffer = new ArrayBuffer(8);
|
|
201
|
-
* convertBufferToFile(buffer, 'example.bin', 'application/octet-stream').then((file) => {
|
|
202
|
-
* console.log(file.name); // Output: "example.bin"
|
|
203
|
-
* console.log(file.type); // Output: "application/octet-stream"
|
|
204
|
-
* });
|
|
205
|
-
*/
|
|
206
|
-
export async function convertBufferToFile(buffer, fileName, mimeType) {
|
|
207
|
-
try {
|
|
208
|
-
// Create and return a File object from the ArrayBuffer
|
|
209
|
-
return new File([buffer], fileName, { type: mimeType });
|
|
210
|
-
}
|
|
211
|
-
catch (error) {
|
|
212
|
-
throw new Error(`Error converting ArrayBuffer to file: ${error.message}`);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Extracts the filename and extension from a given filename string.
|
|
217
|
-
*
|
|
218
|
-
* This function splits a filename into its base name and extension parts.
|
|
219
|
-
* If no extension is present, the extension will be `undefined`.
|
|
220
|
-
*
|
|
221
|
-
* @param filename - The filename string to be processed.
|
|
222
|
-
* @returns An object containing the base name and extension of the filename.
|
|
223
|
-
*
|
|
224
|
-
* @example
|
|
225
|
-
* // Extract filename and extension
|
|
226
|
-
* const { name, ext } = getFilenameExtension('example.txt');
|
|
227
|
-
* console.log(name); // Output: "example"
|
|
228
|
-
* console.log(ext); // Output: "txt"
|
|
229
|
-
*/
|
|
230
|
-
export function getFilenameExtension(filename) {
|
|
231
|
-
if (!filename)
|
|
232
|
-
return { name: '', ext: undefined };
|
|
233
|
-
const parts = filename.split('.');
|
|
234
|
-
const ext = parts.length > 1 ? parts.pop() : undefined;
|
|
235
|
-
const name = parts.join('.');
|
|
236
|
-
return { name, ext };
|
|
237
|
-
}
|
|
238
|
-
/**
|
|
239
|
-
* Converts a file size in bytes to a human-readable string.
|
|
240
|
-
*
|
|
241
|
-
* This function converts a file size in bytes to a string representation with appropriate units
|
|
242
|
-
* (e.g., B, kB, MB, GB, TB) based on the size.
|
|
243
|
-
*
|
|
244
|
-
* @param size - The file size in bytes.
|
|
245
|
-
* @returns A human-readable string representing the file size.
|
|
246
|
-
*
|
|
247
|
-
* @example
|
|
248
|
-
* const sizeString = fileSizeString(1024);
|
|
249
|
-
* console.log(sizeString); // Output: "1 kB"
|
|
250
|
-
*/
|
|
251
|
-
export function formatFileSize(bytes = 0) {
|
|
252
|
-
if (bytes < 1024) {
|
|
253
|
-
return bytes + ' Bytes';
|
|
254
|
-
}
|
|
255
|
-
else if (bytes < 1024 * 1024) {
|
|
256
|
-
return (bytes / 1024).toFixed(2) + ' KB';
|
|
257
|
-
}
|
|
258
|
-
else if (bytes < 1024 * 1024 * 1024) {
|
|
259
|
-
return (bytes / (1024 * 1024)).toFixed(2) + ' MB';
|
|
260
|
-
}
|
|
261
|
-
else {
|
|
262
|
-
return (bytes / (1024 * 1024 * 1024)).toFixed(2) + ' GB';
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
export function getExtFromFileType(type) {
|
|
266
|
-
const mimeTypes = {
|
|
267
|
-
"image/jpeg": "jpg",
|
|
268
|
-
"image/png": "png",
|
|
269
|
-
"image/gif": "gif",
|
|
270
|
-
"image/webp": "webp",
|
|
271
|
-
"image/svg+xml": "svg",
|
|
272
|
-
"video/mp4": "mp4",
|
|
273
|
-
"video/webm": "webm",
|
|
274
|
-
"video/ogg": "ogv",
|
|
275
|
-
"audio/mpeg": "mp3",
|
|
276
|
-
"audio/ogg": "ogg",
|
|
277
|
-
"audio/wav": "wav",
|
|
278
|
-
"application/pdf": "pdf",
|
|
279
|
-
"application/json": "json",
|
|
280
|
-
"application/javascript": "js",
|
|
281
|
-
"text/html": "html",
|
|
282
|
-
"text/css": "css",
|
|
283
|
-
"text/plain": "txt",
|
|
284
|
-
"application/zip": "zip",
|
|
285
|
-
"application/x-rar-compressed": "rar",
|
|
286
|
-
"application/xml": "xml"
|
|
287
|
-
};
|
|
288
|
-
return mimeTypes[type] || null;
|
|
289
|
-
}
|
|
290
|
-
export function ellipsisFileNameAtCenter(name, sizeLimit = 15, startLength = 5, endLength = 3) {
|
|
291
|
-
name = name || '';
|
|
292
|
-
if (name.length > sizeLimit) {
|
|
293
|
-
name = [
|
|
294
|
-
name.substring(0, startLength),
|
|
295
|
-
'…',
|
|
296
|
-
name.substring(name.lastIndexOf('.') - endLength, name.length)
|
|
297
|
-
].join('');
|
|
298
|
-
return name;
|
|
299
|
-
}
|
|
300
|
-
return name;
|
|
301
|
-
}
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sends a GET request to the specified URL.
|
|
3
|
-
*
|
|
4
|
-
* @param url - The URL to send the GET request to.
|
|
5
|
-
* @param headers - Optional headers to include in the request.
|
|
6
|
-
* @returns A promise that resolves to the JSON response from the server.
|
|
7
|
-
*
|
|
8
|
-
* @throws An error if the request fails or the response cannot be parsed as JSON.
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* httpGet('https://api.example.com/data')
|
|
12
|
-
* .then((response) => console.log(response))
|
|
13
|
-
* .catch((error) => console.error('Error fetching data:', error));
|
|
14
|
-
*/
|
|
15
|
-
export declare function httpGet(url: string, headers?: HeadersInit): Promise<any>;
|
|
16
|
-
/**
|
|
17
|
-
* Sends a POST request with JSON data to the specified URL.
|
|
18
|
-
*
|
|
19
|
-
* @param url - The endpoint URL to send the POST request to.
|
|
20
|
-
* @param data - The data object to be sent as the request body.
|
|
21
|
-
* @param headers - Optional headers to include in the request.
|
|
22
|
-
* @returns A promise that resolves to the JSON response from the server.
|
|
23
|
-
*
|
|
24
|
-
* @throws An error if the request fails or the response cannot be parsed as JSON.
|
|
25
|
-
*/
|
|
26
|
-
export declare function httpPost(url: string, data?: Record<string, any>, headers?: HeadersInit): Promise<any>;
|
|
27
|
-
/**
|
|
28
|
-
* Sends a PUT request with JSON data to the specified URL.
|
|
29
|
-
*
|
|
30
|
-
* @param url - The endpoint URL to send the PUT request to.
|
|
31
|
-
* @param data - The data object to be sent as the request body.
|
|
32
|
-
* @param headers - Optional headers to include in the request.
|
|
33
|
-
* @returns A promise that resolves to the JSON response from the server.
|
|
34
|
-
*
|
|
35
|
-
* @throws An error if the request fails or the response cannot be parsed as JSON.
|
|
36
|
-
*/
|
|
37
|
-
export declare function httpPut(url: string, data?: Record<string, any>, headers?: HeadersInit): Promise<any>;
|
|
38
|
-
/**
|
|
39
|
-
* Sends a PATCH request with JSON data to the specified URL.
|
|
40
|
-
*
|
|
41
|
-
* @param url - The endpoint URL to send the PATCH request to.
|
|
42
|
-
* @param data - The data object to be sent as the request body.
|
|
43
|
-
* @param headers - Optional headers to include in the request.
|
|
44
|
-
* @returns A promise that resolves to the JSON response from the server.
|
|
45
|
-
*
|
|
46
|
-
* @throws An error if the request fails or the response cannot be parsed as JSON.
|
|
47
|
-
*/
|
|
48
|
-
export declare function httpPatch(url: string, data?: Record<string, any>, headers?: HeadersInit): Promise<any>;
|
|
49
|
-
/**
|
|
50
|
-
* Sends a DELETE request to the specified URL.
|
|
51
|
-
*
|
|
52
|
-
* @param url - The endpoint URL to send the DELETE request to.
|
|
53
|
-
* @param headers - Optional headers to include in the request.
|
|
54
|
-
* @returns A promise that resolves to the JSON response from the server.
|
|
55
|
-
*
|
|
56
|
-
* @throws An error if the request fails or the response cannot be parsed as JSON.
|
|
57
|
-
*/
|
|
58
|
-
export declare function httpDelete(url: string, headers?: HeadersInit): Promise<any>;
|
|
59
|
-
/**
|
|
60
|
-
* Fetches text content from a given URL with a default timeout of 60 seconds.
|
|
61
|
-
*
|
|
62
|
-
* This function sends a GET request to the provided URL and returns the response as text.
|
|
63
|
-
* It throws an error if the request fails or the response cannot be read as text.
|
|
64
|
-
* The request will be aborted if it exceeds the default timeout of 60 seconds.
|
|
65
|
-
*
|
|
66
|
-
* @param url - The URL to fetch the text content from.
|
|
67
|
-
* @param timeout - Optional. The timeout duration in milliseconds. Default is 60 seconds (60000ms).
|
|
68
|
-
* @returns A promise that resolves to the text content of the URL.
|
|
69
|
-
*
|
|
70
|
-
* @throws An error if the fetch request fails or if the request times out.
|
|
71
|
-
*
|
|
72
|
-
* @example
|
|
73
|
-
* fetchText('https://example.com/data.txt').then((text) => {
|
|
74
|
-
* console.log(text); // Outputs the text content of the file.
|
|
75
|
-
* }).catch((error) => console.error('Failed to fetch text:', error));
|
|
76
|
-
*/
|
|
77
|
-
export declare function fetchText(url: string, timeout?: number): Promise<string>;
|
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
// Default timeout duration in milliseconds (60 seconds)
|
|
2
|
-
const DEFAULT_TIMEOUT = 60000;
|
|
3
|
-
/**
|
|
4
|
-
* Sends a GET request to the specified URL.
|
|
5
|
-
*
|
|
6
|
-
* @param url - The URL to send the GET request to.
|
|
7
|
-
* @param headers - Optional headers to include in the request.
|
|
8
|
-
* @returns A promise that resolves to the JSON response from the server.
|
|
9
|
-
*
|
|
10
|
-
* @throws An error if the request fails or the response cannot be parsed as JSON.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* httpGet('https://api.example.com/data')
|
|
14
|
-
* .then((response) => console.log(response))
|
|
15
|
-
* .catch((error) => console.error('Error fetching data:', error));
|
|
16
|
-
*/
|
|
17
|
-
export async function httpGet(url, headers = {}) {
|
|
18
|
-
return httpRequest(url, { method: 'GET', headers });
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Sends a POST request with JSON data to the specified URL.
|
|
22
|
-
*
|
|
23
|
-
* @param url - The endpoint URL to send the POST request to.
|
|
24
|
-
* @param data - The data object to be sent as the request body.
|
|
25
|
-
* @param headers - Optional headers to include in the request.
|
|
26
|
-
* @returns A promise that resolves to the JSON response from the server.
|
|
27
|
-
*
|
|
28
|
-
* @throws An error if the request fails or the response cannot be parsed as JSON.
|
|
29
|
-
*/
|
|
30
|
-
export async function httpPost(url, data = {}, headers = {}) {
|
|
31
|
-
return httpRequest(url, {
|
|
32
|
-
method: 'POST',
|
|
33
|
-
headers: { 'Content-Type': 'application/json', ...headers },
|
|
34
|
-
body: JSON.stringify(data),
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Sends a PUT request with JSON data to the specified URL.
|
|
39
|
-
*
|
|
40
|
-
* @param url - The endpoint URL to send the PUT request to.
|
|
41
|
-
* @param data - The data object to be sent as the request body.
|
|
42
|
-
* @param headers - Optional headers to include in the request.
|
|
43
|
-
* @returns A promise that resolves to the JSON response from the server.
|
|
44
|
-
*
|
|
45
|
-
* @throws An error if the request fails or the response cannot be parsed as JSON.
|
|
46
|
-
*/
|
|
47
|
-
export async function httpPut(url, data = {}, headers = {}) {
|
|
48
|
-
return httpRequest(url, {
|
|
49
|
-
method: 'PUT',
|
|
50
|
-
headers: { 'Content-Type': 'application/json', ...headers },
|
|
51
|
-
body: JSON.stringify(data),
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Sends a PATCH request with JSON data to the specified URL.
|
|
56
|
-
*
|
|
57
|
-
* @param url - The endpoint URL to send the PATCH request to.
|
|
58
|
-
* @param data - The data object to be sent as the request body.
|
|
59
|
-
* @param headers - Optional headers to include in the request.
|
|
60
|
-
* @returns A promise that resolves to the JSON response from the server.
|
|
61
|
-
*
|
|
62
|
-
* @throws An error if the request fails or the response cannot be parsed as JSON.
|
|
63
|
-
*/
|
|
64
|
-
export async function httpPatch(url, data = {}, headers = {}) {
|
|
65
|
-
return httpRequest(url, {
|
|
66
|
-
method: 'PATCH',
|
|
67
|
-
headers: { 'Content-Type': 'application/json', ...headers },
|
|
68
|
-
body: JSON.stringify(data),
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Sends a DELETE request to the specified URL.
|
|
73
|
-
*
|
|
74
|
-
* @param url - The endpoint URL to send the DELETE request to.
|
|
75
|
-
* @param headers - Optional headers to include in the request.
|
|
76
|
-
* @returns A promise that resolves to the JSON response from the server.
|
|
77
|
-
*
|
|
78
|
-
* @throws An error if the request fails or the response cannot be parsed as JSON.
|
|
79
|
-
*/
|
|
80
|
-
export async function httpDelete(url, headers = {}) {
|
|
81
|
-
return httpRequest(url, { method: 'DELETE', headers });
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Generic function to handle HTTP requests with a timeout.
|
|
85
|
-
*
|
|
86
|
-
* @param url - The URL to send the request to.
|
|
87
|
-
* @param options - The request options (method, headers, body, etc.).
|
|
88
|
-
* @param timeout - Optional timeout duration in milliseconds. Default is 60 seconds.
|
|
89
|
-
* @returns A promise that resolves to the JSON response from the server.
|
|
90
|
-
*
|
|
91
|
-
* @throws An error if the request times out or if the response cannot be parsed as JSON.
|
|
92
|
-
*/
|
|
93
|
-
async function httpRequest(url, options, timeout = DEFAULT_TIMEOUT) {
|
|
94
|
-
// Create an AbortController to handle request timeout
|
|
95
|
-
const controller = new AbortController();
|
|
96
|
-
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
97
|
-
options.signal = controller.signal;
|
|
98
|
-
try {
|
|
99
|
-
const response = await fetch(url, options);
|
|
100
|
-
// Check if the response is successful (status in the range 200–299)
|
|
101
|
-
if (!response.ok) {
|
|
102
|
-
throw new Error(`HTTP error! Status: ${response.status} ${response.statusText}`);
|
|
103
|
-
}
|
|
104
|
-
// Parse and return the response as JSON
|
|
105
|
-
return await response.json();
|
|
106
|
-
}
|
|
107
|
-
catch (error) {
|
|
108
|
-
if (error.name === 'AbortError') {
|
|
109
|
-
throw new Error(`Request to ${url} timed out after ${timeout / 1000} seconds.`);
|
|
110
|
-
}
|
|
111
|
-
throw new Error(`Failed to fetch ${url}: ${error.message}`);
|
|
112
|
-
}
|
|
113
|
-
finally {
|
|
114
|
-
clearTimeout(timeoutId); // Clear the timeout to prevent memory leaks
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Fetches text content from a given URL with a default timeout of 60 seconds.
|
|
119
|
-
*
|
|
120
|
-
* This function sends a GET request to the provided URL and returns the response as text.
|
|
121
|
-
* It throws an error if the request fails or the response cannot be read as text.
|
|
122
|
-
* The request will be aborted if it exceeds the default timeout of 60 seconds.
|
|
123
|
-
*
|
|
124
|
-
* @param url - The URL to fetch the text content from.
|
|
125
|
-
* @param timeout - Optional. The timeout duration in milliseconds. Default is 60 seconds (60000ms).
|
|
126
|
-
* @returns A promise that resolves to the text content of the URL.
|
|
127
|
-
*
|
|
128
|
-
* @throws An error if the fetch request fails or if the request times out.
|
|
129
|
-
*
|
|
130
|
-
* @example
|
|
131
|
-
* fetchText('https://example.com/data.txt').then((text) => {
|
|
132
|
-
* console.log(text); // Outputs the text content of the file.
|
|
133
|
-
* }).catch((error) => console.error('Failed to fetch text:', error));
|
|
134
|
-
*/
|
|
135
|
-
export async function fetchText(url, timeout = DEFAULT_TIMEOUT) {
|
|
136
|
-
// Create an AbortController to handle request timeout
|
|
137
|
-
const controller = new AbortController();
|
|
138
|
-
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
139
|
-
try {
|
|
140
|
-
const response = await fetch(url, { signal: controller.signal });
|
|
141
|
-
// Check if the response is successful (status in the range 200–299)
|
|
142
|
-
if (!response.ok) {
|
|
143
|
-
throw new Error(`Failed to fetch text. Status: ${response.status} ${response.statusText}`);
|
|
144
|
-
}
|
|
145
|
-
// Return the response as text
|
|
146
|
-
return await response.text();
|
|
147
|
-
}
|
|
148
|
-
catch (error) {
|
|
149
|
-
if (error.name === 'AbortError') {
|
|
150
|
-
throw new Error(`Request to ${url} timed out after ${timeout / 1000} seconds.`);
|
|
151
|
-
}
|
|
152
|
-
console.error('Error fetching text from URL:', error);
|
|
153
|
-
throw new Error(`Failed to fetch text from ${url}: ${error.message}`);
|
|
154
|
-
}
|
|
155
|
-
finally {
|
|
156
|
-
clearTimeout(timeoutId); // Clear the timeout to prevent memory leaks
|
|
157
|
-
}
|
|
158
|
-
}
|