@tagsamurai/fats-api-services 1.0.0-alpha.302 → 1.0.0-alpha.304

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.
@@ -42,19 +42,48 @@ const queryParamsStringfy = (data) => {
42
42
  });
43
43
  return assignedData;
44
44
  };
45
- const getImageURL = (name, width, height) => {
46
- if (!name) return;
47
- const BASE_URL = getBaseURL("APP_API");
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 ? height == null ? void 0 : height.toString() : width.toString());
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
- return `${filePath}?${params.toString()}`;
67
+ });
68
+ if (!res.ok) {
69
+ throw new Error(`Image fetch failed: ${res.status} ${res.statusText}`);
70
+ }
71
+ const arrayBuffer = await res.arrayBuffer();
72
+ return new Blob([arrayBuffer], {
73
+ type: res.headers.get("Content-Type") || "image/webp"
74
+ });
75
+ };
76
+ const getImageURL = async (name, width, height, returnURLOnly) => {
77
+ if (!name) return;
78
+ const url = buildImageUrl(name, width, height);
79
+ if (returnURLOnly) return url;
80
+ try {
81
+ const token = getAuthToken();
82
+ const blob = await fetchImageBlob(url, token);
83
+ return URL.createObjectURL(blob);
84
+ } catch (err) {
85
+ return void 0;
56
86
  }
57
- return filePath;
58
87
  };
59
88
  const getAssetsFile = async (file, type = "excel") => {
60
89
  const response = await fetch(
@@ -49,19 +49,48 @@ System.register(["axios"], function(exports, module) {
49
49
  });
50
50
  return assignedData;
51
51
  });
52
- const getImageURL = exports("getImageURL", (name, width, height) => {
53
- if (!name) return;
54
- const BASE_URL = getBaseURL("APP_API");
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 ? height == null ? void 0 : height.toString() : width.toString());
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
- return `${filePath}?${params.toString()}`;
74
+ });
75
+ if (!res.ok) {
76
+ throw new Error(`Image fetch failed: ${res.status} ${res.statusText}`);
77
+ }
78
+ const arrayBuffer = await res.arrayBuffer();
79
+ return new Blob([arrayBuffer], {
80
+ type: res.headers.get("Content-Type") || "image/webp"
81
+ });
82
+ };
83
+ const getImageURL = exports("getImageURL", async (name, width, height, returnURLOnly) => {
84
+ if (!name) return;
85
+ const url = buildImageUrl(name, width, height);
86
+ if (returnURLOnly) return url;
87
+ try {
88
+ const token = getAuthToken();
89
+ const blob = await fetchImageBlob(url, token);
90
+ return URL.createObjectURL(blob);
91
+ } catch (err) {
92
+ return void 0;
63
93
  }
64
- return filePath;
65
94
  });
66
95
  const getAssetsFile = exports("getAssetsFile", async (file, type = "excel") => {
67
96
  const response = await fetch(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tagsamurai/fats-api-services",
3
- "version": "1.0.0-alpha.302",
3
+ "version": "1.0.0-alpha.304",
4
4
  "author": "developer.tagsamurai",
5
5
  "description": "Fixed Asset Tag Samurai Services Library",
6
6
  "module": "./api-services.es.js",
@@ -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, returnURLOnly?: boolean) => Promise<string | undefined>;