@tagsamurai/fats-api-services 1.0.3-alpha.33 → 1.0.3-alpha.35

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.
@@ -3,45 +3,6 @@ const __vite_import_meta_env__ = { "BASE_URL": "/", "DEV": false, "MODE": "produ
3
3
  const getBaseURL = (env = "APP_API") => {
4
4
  return __vite_import_meta_env__["VITE_" + env];
5
5
  };
6
- const createAxiosInstance = (config = {}, useDifferentHeaders = false) => {
7
- const { env = "APP_API", prefix = "", headers = {}, ...restConfig } = config;
8
- const baseURL = `${getBaseURL(env)}${prefix}`;
9
- const instance = axios.create({
10
- ...restConfig,
11
- baseURL,
12
- headers: useDifferentHeaders ? headers : {
13
- "Content-Type": "application/json",
14
- ...headers
15
- }
16
- });
17
- instance.interceptors.request.use((request) => {
18
- const user = JSON.parse(localStorage.getItem("user") ?? "{}");
19
- const jwt = user.jwt ?? user.token ?? "";
20
- request.headers["Authorization"] = `Bearer ${jwt}`;
21
- return request;
22
- });
23
- return instance;
24
- };
25
- const queryParamsStringfy = (data) => {
26
- if (!data || typeof data === "string") {
27
- return;
28
- }
29
- const assignedData = {};
30
- Object.keys(data).forEach((item) => {
31
- if (Array.isArray(data[item])) {
32
- if (data[item].length > 0) {
33
- Object.assign(assignedData, {
34
- [item]: JSON.stringify(data[item])
35
- });
36
- }
37
- } else if (data[item] !== void 0) {
38
- Object.assign(assignedData, {
39
- [item]: data[item]
40
- });
41
- }
42
- });
43
- return assignedData;
44
- };
45
6
  const buildFileURL = (name, width, height) => {
46
7
  const BASE_URL = new URL(getBaseURL("APP_API")).origin;
47
8
  let url = name.startsWith("http") ? name : `${BASE_URL}/file-storage/api/file/${name.replace(/^\/+/, "")}`;
@@ -73,6 +34,22 @@ const fetchBlobFile = async (url, token) => {
73
34
  type: res.headers.get("Content-Type") || "image/webp"
74
35
  });
75
36
  };
37
+ const downloadFile = async (fileUrl, fileName) => {
38
+ const token = getAuthToken();
39
+ const blob = await fetchBlobFile(fileUrl, token);
40
+ const blobUrl = URL.createObjectURL(blob);
41
+ if (/^(image|application\/pdf)/i.test(blob.type)) {
42
+ window.open(blobUrl, "_blank");
43
+ } else {
44
+ const a = document.createElement("a");
45
+ a.href = blobUrl;
46
+ a.download = fileName;
47
+ document.body.appendChild(a);
48
+ a.click();
49
+ a.remove();
50
+ }
51
+ window.URL.revokeObjectURL(blobUrl);
52
+ };
76
53
  const createBlobURL = async (rawFileUrl) => {
77
54
  try {
78
55
  const token = getAuthToken();
@@ -88,6 +65,45 @@ const getImageURL = async (name, width, height, returnURLOnly) => {
88
65
  if (returnURLOnly) return url;
89
66
  return createBlobURL(url);
90
67
  };
68
+ const createAxiosInstance = (config = {}, useDifferentHeaders = false) => {
69
+ const { env = "APP_API", prefix = "", headers = {}, ...restConfig } = config;
70
+ const baseURL = `${getBaseURL(env)}${prefix}`;
71
+ const instance = axios.create({
72
+ ...restConfig,
73
+ baseURL,
74
+ headers: useDifferentHeaders ? headers : {
75
+ "Content-Type": "application/json",
76
+ ...headers
77
+ }
78
+ });
79
+ instance.interceptors.request.use((request) => {
80
+ const user = JSON.parse(localStorage.getItem("user") ?? "{}");
81
+ const jwt = user.jwt ?? user.token ?? "";
82
+ request.headers["Authorization"] = `Bearer ${jwt}`;
83
+ return request;
84
+ });
85
+ return instance;
86
+ };
87
+ const queryParamsStringfy = (data) => {
88
+ if (!data || typeof data === "string") {
89
+ return;
90
+ }
91
+ const assignedData = {};
92
+ Object.keys(data).forEach((item) => {
93
+ if (Array.isArray(data[item])) {
94
+ if (data[item].length > 0) {
95
+ Object.assign(assignedData, {
96
+ [item]: JSON.stringify(data[item])
97
+ });
98
+ }
99
+ } else if (data[item] !== void 0) {
100
+ Object.assign(assignedData, {
101
+ [item]: data[item]
102
+ });
103
+ }
104
+ });
105
+ return assignedData;
106
+ };
91
107
  const getAssetsFile = async (file, type = "excel") => {
92
108
  const response = await fetch(
93
109
  `${getBaseURL("APP_ASSETS_URL")}/${type}/${file}`
@@ -2734,6 +2750,8 @@ export {
2734
2750
  TransferServicesGo as TransferServices,
2735
2751
  UserServices,
2736
2752
  buildFileURL,
2753
+ downloadFile,
2754
+ fetchBlobFile,
2737
2755
  getAssetsFile,
2738
2756
  getBaseURL,
2739
2757
  getImageURL,
@@ -10,45 +10,6 @@ System.register(["axios"], function(exports, module) {
10
10
  const getBaseURL = exports("getBaseURL", (env = "APP_API") => {
11
11
  return __vite_import_meta_env__["VITE_" + env];
12
12
  });
13
- const createAxiosInstance = (config = {}, useDifferentHeaders = false) => {
14
- const { env = "APP_API", prefix = "", headers = {}, ...restConfig } = config;
15
- const baseURL = `${getBaseURL(env)}${prefix}`;
16
- const instance = axios.create({
17
- ...restConfig,
18
- baseURL,
19
- headers: useDifferentHeaders ? headers : {
20
- "Content-Type": "application/json",
21
- ...headers
22
- }
23
- });
24
- instance.interceptors.request.use((request) => {
25
- const user = JSON.parse(localStorage.getItem("user") ?? "{}");
26
- const jwt = user.jwt ?? user.token ?? "";
27
- request.headers["Authorization"] = `Bearer ${jwt}`;
28
- return request;
29
- });
30
- return instance;
31
- };
32
- const queryParamsStringfy = exports("queryParamsStringfy", (data) => {
33
- if (!data || typeof data === "string") {
34
- return;
35
- }
36
- const assignedData = {};
37
- Object.keys(data).forEach((item) => {
38
- if (Array.isArray(data[item])) {
39
- if (data[item].length > 0) {
40
- Object.assign(assignedData, {
41
- [item]: JSON.stringify(data[item])
42
- });
43
- }
44
- } else if (data[item] !== void 0) {
45
- Object.assign(assignedData, {
46
- [item]: data[item]
47
- });
48
- }
49
- });
50
- return assignedData;
51
- });
52
13
  const buildFileURL = exports("buildFileURL", (name, width, height) => {
53
14
  const BASE_URL = new URL(getBaseURL("APP_API")).origin;
54
15
  let url = name.startsWith("http") ? name : `${BASE_URL}/file-storage/api/file/${name.replace(/^\/+/, "")}`;
@@ -66,7 +27,7 @@ System.register(["axios"], function(exports, module) {
66
27
  const user = JSON.parse(localStorage.getItem("user") ?? "{}");
67
28
  return user.jwt ?? user.token ?? "";
68
29
  };
69
- const fetchBlobFile = async (url, token) => {
30
+ const fetchBlobFile = exports("fetchBlobFile", async (url, token) => {
70
31
  const res = await fetch(url, {
71
32
  headers: {
72
33
  Authorization: `Bearer ${token}`
@@ -79,7 +40,23 @@ System.register(["axios"], function(exports, module) {
79
40
  return new Blob([arrayBuffer], {
80
41
  type: res.headers.get("Content-Type") || "image/webp"
81
42
  });
82
- };
43
+ });
44
+ const downloadFile = exports("downloadFile", async (fileUrl, fileName) => {
45
+ const token = getAuthToken();
46
+ const blob = await fetchBlobFile(fileUrl, token);
47
+ const blobUrl = URL.createObjectURL(blob);
48
+ if (/^(image|application\/pdf)/i.test(blob.type)) {
49
+ window.open(blobUrl, "_blank");
50
+ } else {
51
+ const a = document.createElement("a");
52
+ a.href = blobUrl;
53
+ a.download = fileName;
54
+ document.body.appendChild(a);
55
+ a.click();
56
+ a.remove();
57
+ }
58
+ window.URL.revokeObjectURL(blobUrl);
59
+ });
83
60
  const createBlobURL = async (rawFileUrl) => {
84
61
  try {
85
62
  const token = getAuthToken();
@@ -95,6 +72,45 @@ System.register(["axios"], function(exports, module) {
95
72
  if (returnURLOnly) return url;
96
73
  return createBlobURL(url);
97
74
  });
75
+ const createAxiosInstance = (config = {}, useDifferentHeaders = false) => {
76
+ const { env = "APP_API", prefix = "", headers = {}, ...restConfig } = config;
77
+ const baseURL = `${getBaseURL(env)}${prefix}`;
78
+ const instance = axios.create({
79
+ ...restConfig,
80
+ baseURL,
81
+ headers: useDifferentHeaders ? headers : {
82
+ "Content-Type": "application/json",
83
+ ...headers
84
+ }
85
+ });
86
+ instance.interceptors.request.use((request) => {
87
+ const user = JSON.parse(localStorage.getItem("user") ?? "{}");
88
+ const jwt = user.jwt ?? user.token ?? "";
89
+ request.headers["Authorization"] = `Bearer ${jwt}`;
90
+ return request;
91
+ });
92
+ return instance;
93
+ };
94
+ const queryParamsStringfy = exports("queryParamsStringfy", (data) => {
95
+ if (!data || typeof data === "string") {
96
+ return;
97
+ }
98
+ const assignedData = {};
99
+ Object.keys(data).forEach((item) => {
100
+ if (Array.isArray(data[item])) {
101
+ if (data[item].length > 0) {
102
+ Object.assign(assignedData, {
103
+ [item]: JSON.stringify(data[item])
104
+ });
105
+ }
106
+ } else if (data[item] !== void 0) {
107
+ Object.assign(assignedData, {
108
+ [item]: data[item]
109
+ });
110
+ }
111
+ });
112
+ return assignedData;
113
+ });
98
114
  const getAssetsFile = exports("getAssetsFile", async (file, type = "excel") => {
99
115
  const response = await fetch(
100
116
  `${getBaseURL("APP_ASSETS_URL")}/${type}/${file}`
package/main.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- export { getImageURL as getImageURL, getBaseURL, getAssetsFile, queryParamsStringfy, buildFileURL, } from './src/utils';
1
+ export * from './src/utils/getImageURL.util';
2
+ export { getBaseURL, getAssetsFile, queryParamsStringfy } from './src/utils';
2
3
  export { default as LogServices } from './src/services/log.service';
3
4
  export { default as AssetServices } from './src/services/asset.service';
4
5
  export { default as MissingServices } from './src/services/missing.service';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tagsamurai/fats-api-services",
3
- "version": "1.0.3-alpha.33",
3
+ "version": "1.0.3-alpha.35",
4
4
  "author": "developer.tagsamurai",
5
5
  "description": "Fixed Asset Tag Samurai Services Library",
6
6
  "module": "./api-services.es.js",
@@ -5,5 +5,5 @@ export declare const fetchBlobFile: (url: string, token: string) => Promise<Blob
5
5
  * @param fileUrl Full URL of the file to download
6
6
  * @returns
7
7
  */
8
- export declare const downloadFile: (fileUrl: string) => Promise<string>;
8
+ export declare const downloadFile: (fileUrl: string, fileName: string) => Promise<void>;
9
9
  export declare const getImageURL: (name?: string | null, width?: number, height?: number, returnURLOnly?: boolean) => Promise<string | undefined>;