@kawaiininja/fetch 1.0.1 → 1.0.3

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.
@@ -1,3 +1,4 @@
1
+ import { SecureStoragePlugin } from "capacitor-secure-storage-plugin";
1
2
  import { useCallback, useEffect, useMemo, useRef, useState } from "react";
2
3
  import { INTERNAL_HEADER } from "../context/ApiContext";
3
4
  import { useApiConfig } from "./useApiConfig";
@@ -31,21 +32,58 @@ export const useFetch = (endpoint, baseOptions = {}) => {
31
32
  }
32
33
  }, []);
33
34
  const performFetch = async (url, method, token, body, headers, rest) => {
34
- const authToken = typeof window !== "undefined" ? localStorage.getItem("token") : null;
35
- const sessionId = typeof window !== "undefined" ? localStorage.getItem("session_id") : null;
35
+ // 🌍 PLATFORM DETECTION
36
+ // We check if we are running natively (iOS/Android) or on the Web
37
+ const platform = typeof window !== "undefined"
38
+ ? window.Capacitor?.getPlatform() || "web"
39
+ : "web";
40
+ const isNative = platform === "ios" || platform === "android";
41
+ // 🎯 TARGET CHECK (Prevent Credential Leakage)
42
+ // Only attach sensitive headers if sending to our own API
43
+ const isInternal = url.startsWith("/") ||
44
+ (apiUrl("") && url.startsWith(apiUrl(""))) ||
45
+ false;
36
46
  const headersConfig = {
37
47
  ...(optionsRef.current.headers || {}),
38
48
  ...(headers || {}),
39
49
  ...INTERNAL_HEADER,
40
- "X-CSRF-Token": token,
41
- ...(authToken ? { Authorization: `Bearer ${authToken}` } : {}),
42
- ...(sessionId ? { "X-Session-ID": sessionId } : {}),
43
50
  };
51
+ // 🔒 SECURITY STRATEGY: NATIVE (MOBILE)
52
+ if (isNative && isInternal) {
53
+ // Mobile relies on manual headers ("Active Courier")
54
+ // We DO NOT use CSRF tokens on mobile (Cookies generally don't work reliably here)
55
+ // 🛡️ S-RANK UPGRADE: Use Secure Storage (Async) instead of LocalStorage
56
+ try {
57
+ const { value: authToken } = await SecureStoragePlugin.get({
58
+ key: "token",
59
+ }).catch(() => ({ value: null }));
60
+ const { value: sessionId } = await SecureStoragePlugin.get({
61
+ key: "session_id",
62
+ }).catch(() => ({ value: null }));
63
+ if (authToken)
64
+ headersConfig["Authorization"] = `Bearer ${authToken}`;
65
+ if (sessionId)
66
+ headersConfig["X-Session-ID"] = sessionId;
67
+ }
68
+ catch (err) {
69
+ // Determine if we should log this - might be noisy if keys just don't exist yet
70
+ }
71
+ }
72
+ // 🔒 SECURITY STRATEGY: WEB (BROWSER)
73
+ if (!isNative && isInternal) {
74
+ // Web relies on Cookies ("Passive Courier") for Auth
75
+ // BUT we MUST attach the CSRF Token to prevent attacks
76
+ if (token)
77
+ headersConfig["X-CSRF-Token"] = token;
78
+ // On Web, we DO NOT attach 'Authorization' or 'Session-ID' from localStorage
79
+ // This minimizes XSS risk. The HttpOnly cookie handles it.
80
+ }
44
81
  return fetch(url, {
45
82
  ...optionsRef.current,
46
83
  ...rest,
47
84
  method,
48
85
  signal: abortRef.current.signal,
86
+ // 🍪 Web: "include" sends cookies. Mobile: ignored/useless but harmless.
49
87
  credentials: "include",
50
88
  headers: headersConfig,
51
89
  body,
package/package.json CHANGED
@@ -1,40 +1,46 @@
1
- {
2
- "name": "@kawaiininja/fetch",
3
- "version": "1.0.1",
4
- "description": "Core fetch utility for Onyx Framework",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
7
- "module": "dist/index.js",
8
- "type": "module",
9
- "exports": {
10
- ".": "./dist/index.js"
11
- },
12
- "files": [
13
- "dist",
14
- "README.md"
15
- ],
16
- "scripts": {
17
- "build": "tsc"
18
- },
19
- "keywords": [
20
- "react",
21
- "fetch",
22
- "onyx",
23
- "kawaiininja",
24
- "http"
25
- ],
26
- "author": "Vinay (4kawaiininja)",
27
- "license": "MIT",
28
- "peerDependencies": {
29
- "react": "^18.0.0 || ^19.0.0",
30
- "react-dom": "^18.0.0 || ^19.0.0"
31
- },
32
- "publishConfig": {
33
- "access": "public"
34
- },
35
- "devDependencies": {
36
- "@types/react": "^19.2.7",
37
- "@types/react-dom": "^19.2.3",
38
- "typescript": "^5.7.0"
39
- }
40
- }
1
+ {
2
+ "name": "@kawaiininja/fetch",
3
+ "version": "1.0.3",
4
+ "description": "Core fetch utility for Onyx Framework",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "module": "dist/index.js",
8
+ "type": "module",
9
+ "exports": {
10
+ ".": "./dist/index.js"
11
+ },
12
+ "files": [
13
+ "dist",
14
+ "README.md"
15
+ ],
16
+ "scripts": {
17
+ "build": "tsc"
18
+ },
19
+ "keywords": [
20
+ "react",
21
+ "fetch",
22
+ "onyx",
23
+ "kawaiininja",
24
+ "http"
25
+ ],
26
+ "author": "Vinay (4kawaiininja)",
27
+ "license": "MIT",
28
+ "peerDependencies": {
29
+ "react": "^18.0.0 || ^19.0.0",
30
+ "react-dom": "^18.0.0 || ^19.0.0"
31
+ },
32
+ "publishConfig": {
33
+ "access": "public"
34
+ },
35
+ "devDependencies": {
36
+ "@types/react": "^19.2.7",
37
+ "@types/react-dom": "^19.2.3",
38
+ "typescript": "^5.7.0"
39
+ },
40
+ "dependencies": {
41
+ "@capacitor-community/security-provider": "^7.0.0",
42
+ "@capacitor-community/text-to-speech": "^6.1.0",
43
+ "@capacitor/core": "^8.0.1",
44
+ "capacitor-secure-storage-plugin": "^0.13.0"
45
+ }
46
+ }