@tagsamurai/fats-api-services 1.0.0-alpha.302 → 1.0.0-alpha.303
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/api-services.es.js +38 -7
- package/api-services.system.js +38 -7
- package/package.json +1 -1
- package/src/utils/getImageURL.util.d.ts +1 -1
package/api-services.es.js
CHANGED
|
@@ -42,19 +42,50 @@ const queryParamsStringfy = (data) => {
|
|
|
42
42
|
});
|
|
43
43
|
return assignedData;
|
|
44
44
|
};
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const filePath = name.startsWith("http") ? name : `${BASE_URL}/utility/v2/files/${name.replace(/^\/+/, "")}`;
|
|
45
|
+
const buildImageUrl = (name, width, height) => {
|
|
46
|
+
const BASE_URL = new URL(getBaseURL("APP_API")).origin;
|
|
47
|
+
let url = name.startsWith("http") ? name : `${BASE_URL}/file-storage/api/file/${name.replace(/^\/+/, "")}`;
|
|
49
48
|
if (width || height) {
|
|
50
49
|
const params = new URLSearchParams();
|
|
51
50
|
if (width) {
|
|
52
51
|
params.set("width", width.toString());
|
|
53
|
-
params.set("height", height
|
|
52
|
+
params.set("height", (height == null ? void 0 : height.toString()) || width.toString());
|
|
53
|
+
}
|
|
54
|
+
url += `?${params.toString()}`;
|
|
55
|
+
}
|
|
56
|
+
return url;
|
|
57
|
+
};
|
|
58
|
+
const getAuthToken = () => {
|
|
59
|
+
const user = JSON.parse(localStorage.getItem("user") ?? "{}");
|
|
60
|
+
return user.jwt ?? user.token ?? "";
|
|
61
|
+
};
|
|
62
|
+
const fetchImageBlob = async (url, token) => {
|
|
63
|
+
const res = await fetch(url, {
|
|
64
|
+
headers: {
|
|
65
|
+
Authorization: `Bearer ${token}`
|
|
54
66
|
}
|
|
55
|
-
|
|
67
|
+
});
|
|
68
|
+
if (!res.ok) {
|
|
69
|
+
throw new Error(`Image fetch failed: ${res.status} ${res.statusText}`);
|
|
70
|
+
}
|
|
71
|
+
return res.blob();
|
|
72
|
+
};
|
|
73
|
+
const blobToDataURI = (blob) => new Promise((resolve, reject) => {
|
|
74
|
+
const reader = new FileReader();
|
|
75
|
+
reader.onloadend = () => resolve(reader.result);
|
|
76
|
+
reader.onerror = reject;
|
|
77
|
+
reader.readAsDataURL(blob);
|
|
78
|
+
});
|
|
79
|
+
const getImageURL = async (name, width, height) => {
|
|
80
|
+
if (!name) return;
|
|
81
|
+
try {
|
|
82
|
+
const url = buildImageUrl(name, width, height);
|
|
83
|
+
const token = getAuthToken();
|
|
84
|
+
const blob = await fetchImageBlob(url, token);
|
|
85
|
+
return blobToDataURI(blob);
|
|
86
|
+
} catch (err) {
|
|
87
|
+
return void 0;
|
|
56
88
|
}
|
|
57
|
-
return filePath;
|
|
58
89
|
};
|
|
59
90
|
const getAssetsFile = async (file, type = "excel") => {
|
|
60
91
|
const response = await fetch(
|
package/api-services.system.js
CHANGED
|
@@ -49,19 +49,50 @@ System.register(["axios"], function(exports, module) {
|
|
|
49
49
|
});
|
|
50
50
|
return assignedData;
|
|
51
51
|
});
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const filePath = name.startsWith("http") ? name : `${BASE_URL}/utility/v2/files/${name.replace(/^\/+/, "")}`;
|
|
52
|
+
const buildImageUrl = (name, width, height) => {
|
|
53
|
+
const BASE_URL = new URL(getBaseURL("APP_API")).origin;
|
|
54
|
+
let url = name.startsWith("http") ? name : `${BASE_URL}/file-storage/api/file/${name.replace(/^\/+/, "")}`;
|
|
56
55
|
if (width || height) {
|
|
57
56
|
const params = new URLSearchParams();
|
|
58
57
|
if (width) {
|
|
59
58
|
params.set("width", width.toString());
|
|
60
|
-
params.set("height", height
|
|
59
|
+
params.set("height", (height == null ? void 0 : height.toString()) || width.toString());
|
|
60
|
+
}
|
|
61
|
+
url += `?${params.toString()}`;
|
|
62
|
+
}
|
|
63
|
+
return url;
|
|
64
|
+
};
|
|
65
|
+
const getAuthToken = () => {
|
|
66
|
+
const user = JSON.parse(localStorage.getItem("user") ?? "{}");
|
|
67
|
+
return user.jwt ?? user.token ?? "";
|
|
68
|
+
};
|
|
69
|
+
const fetchImageBlob = async (url, token) => {
|
|
70
|
+
const res = await fetch(url, {
|
|
71
|
+
headers: {
|
|
72
|
+
Authorization: `Bearer ${token}`
|
|
61
73
|
}
|
|
62
|
-
|
|
74
|
+
});
|
|
75
|
+
if (!res.ok) {
|
|
76
|
+
throw new Error(`Image fetch failed: ${res.status} ${res.statusText}`);
|
|
77
|
+
}
|
|
78
|
+
return res.blob();
|
|
79
|
+
};
|
|
80
|
+
const blobToDataURI = (blob) => new Promise((resolve, reject) => {
|
|
81
|
+
const reader = new FileReader();
|
|
82
|
+
reader.onloadend = () => resolve(reader.result);
|
|
83
|
+
reader.onerror = reject;
|
|
84
|
+
reader.readAsDataURL(blob);
|
|
85
|
+
});
|
|
86
|
+
const getImageURL = exports("getImageURL", async (name, width, height) => {
|
|
87
|
+
if (!name) return;
|
|
88
|
+
try {
|
|
89
|
+
const url = buildImageUrl(name, width, height);
|
|
90
|
+
const token = getAuthToken();
|
|
91
|
+
const blob = await fetchImageBlob(url, token);
|
|
92
|
+
return blobToDataURI(blob);
|
|
93
|
+
} catch (err) {
|
|
94
|
+
return void 0;
|
|
63
95
|
}
|
|
64
|
-
return filePath;
|
|
65
96
|
});
|
|
66
97
|
const getAssetsFile = exports("getAssetsFile", async (file, type = "excel") => {
|
|
67
98
|
const response = await fetch(
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const getImageURL: (name?: string | null, width?: number, height?: number) => string | undefined
|
|
1
|
+
export declare const getImageURL: (name?: string | null, width?: number, height?: number) => Promise<string | undefined>;
|