@putkoff/abstract-securefiles 0.0.34 → 0.0.43
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/dist/FileHandlers/imports.d.ts +1 -1
- package/dist/file_utils/src/cookieFetch.d.ts +14 -3
- package/dist/index.cjs +35 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +35 -4
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/FileHandlers/imports.ts +1 -1
- package/src/file_utils/src/cookieFetch.ts +35 -4
- package/src/file_utils/src/upload_utils/src/upload_files.ts +1 -1
|
@@ -2,9 +2,37 @@
|
|
|
2
2
|
* Cookie-authenticated API helpers.
|
|
3
3
|
* All requests include credentials: 'include' so the browser sends the
|
|
4
4
|
* session cookie (auth_session) automatically. No JWT tokens are used.
|
|
5
|
+
*
|
|
6
|
+
* API base URL resolution order (first truthy value wins):
|
|
7
|
+
* 1. window.__SECURE_FILES_API_BASE__ – runtime override (any bundler)
|
|
8
|
+
* 2. VITE_SECURE_FILES_API_BASE – Vite env var
|
|
9
|
+
* 3. REACT_APP_SECURE_FILES_API_BASE – Create React App env var
|
|
10
|
+
* 4. /api/secure-files – same-origin default
|
|
11
|
+
*
|
|
12
|
+
* Subdomain API example (set one of the above to the full origin + path):
|
|
13
|
+
* VITE_SECURE_FILES_API_BASE=https://api.example.com/secure-files
|
|
5
14
|
*/
|
|
6
15
|
|
|
7
|
-
|
|
16
|
+
function resolveApiBase(): string {
|
|
17
|
+
// 1. Runtime window config — works regardless of bundler
|
|
18
|
+
if (typeof window !== "undefined" && (window as any).__SECURE_FILES_API_BASE__) {
|
|
19
|
+
return String((window as any).__SECURE_FILES_API_BASE__).replace(/\/+$/, "");
|
|
20
|
+
}
|
|
21
|
+
// 2. Vite (import.meta.env is replaced at build time; guard with try for non-Vite builds)
|
|
22
|
+
try {
|
|
23
|
+
const v = (import.meta as any).env?.VITE_SECURE_FILES_API_BASE;
|
|
24
|
+
if (v) return String(v).replace(/\/+$/, "");
|
|
25
|
+
} catch { /* not a Vite build */ }
|
|
26
|
+
// 3. Create React App / plain Node
|
|
27
|
+
try {
|
|
28
|
+
const v = (process as any).env?.REACT_APP_SECURE_FILES_API_BASE;
|
|
29
|
+
if (v) return String(v).replace(/\/+$/, "");
|
|
30
|
+
} catch { /* process not defined */ }
|
|
31
|
+
// 4. Same-origin default
|
|
32
|
+
return "/api/secure-files";
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const API_BASE = resolveApiBase();
|
|
8
36
|
|
|
9
37
|
function apiUrl(path: string): string {
|
|
10
38
|
const p = path.startsWith("/") ? path : `/${path}`;
|
|
@@ -41,8 +69,8 @@ export async function cookieFetch<T = unknown>(
|
|
|
41
69
|
|
|
42
70
|
/**
|
|
43
71
|
* Check whether the current session is valid.
|
|
44
|
-
* Calls /userInfo which requires authentication; returns true if the user
|
|
45
|
-
* logged in, false otherwise (no exception thrown).
|
|
72
|
+
* Calls /userInfo which requires authentication; returns true if the user
|
|
73
|
+
* is logged in, false otherwise (no exception thrown).
|
|
46
74
|
*/
|
|
47
75
|
export async function checkAuth(): Promise<boolean> {
|
|
48
76
|
try {
|
|
@@ -58,7 +86,7 @@ export async function checkAuth(): Promise<boolean> {
|
|
|
58
86
|
|
|
59
87
|
/**
|
|
60
88
|
* Upload a single file using multipart/form-data with session-cookie auth.
|
|
61
|
-
* @param file
|
|
89
|
+
* @param file The File object to upload.
|
|
62
90
|
* @param relPath Optional relative path within the user's directory.
|
|
63
91
|
*/
|
|
64
92
|
export async function cookieUpload(file: File, relPath = ""): Promise<unknown> {
|
|
@@ -76,3 +104,6 @@ export async function cookieUpload(file: File, relPath = ""): Promise<unknown> {
|
|
|
76
104
|
if (!res.ok) throw new Error(`Upload failed: HTTP ${res.status}`);
|
|
77
105
|
return res.json();
|
|
78
106
|
}
|
|
107
|
+
|
|
108
|
+
/** The resolved API base URL, exported for debugging / display purposes. */
|
|
109
|
+
export const SECURE_FILES_API_BASE = API_BASE;
|