@scryme/chat 0.0.1 → 2.13.4

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,4 +1,6 @@
1
1
  import { AxiosRequestConfig, AxiosError } from 'axios';
2
+ export declare const setGlobalToken: (token: string | null) => void;
3
+ export declare const getGlobalToken: () => string | null;
2
4
  export declare const AXIOS_INSTANCE: import("axios").AxiosInstance;
3
5
  export declare const customInstance: <T>(config: AxiosRequestConfig, options?: AxiosRequestConfig) => Promise<T>;
4
6
  export type ErrorType<Error> = AxiosError<Error>;
@@ -37,12 +37,30 @@ var getBaseURL = function () {
37
37
  }
38
38
  return url.replace(/\/$/, '') + '/api';
39
39
  };
40
+ var globalToken = null;
41
+ export var setGlobalToken = function (token) {
42
+ globalToken = token;
43
+ };
44
+ export var getGlobalToken = function () { return globalToken; };
40
45
  export var AXIOS_INSTANCE = axios.create({
41
46
  baseURL: getBaseURL(),
42
47
  timeout: 10000,
43
48
  withCredentials: true,
44
49
  });
45
50
  AXIOS_INSTANCE.interceptors.request.use(function (config) {
51
+ if (!config.headers) {
52
+ config.headers = {};
53
+ }
54
+ // Check if an Authorization header is already present (case-insensitive)
55
+ var hasAuth = config.headers.Authorization ||
56
+ config.headers.authorization ||
57
+ config.headers['Authorization'] ||
58
+ config.headers['authorization'];
59
+ // 1. Apply global token if available and no Auth header is set yet
60
+ if (globalToken && !hasAuth) {
61
+ config.headers.Authorization = "Bearer ".concat(globalToken);
62
+ }
63
+ // 2. Check for browser-based session/bearer tokens
46
64
  if (typeof window !== 'undefined') {
47
65
  var getCookie = function (name) {
48
66
  var _a;
@@ -78,7 +96,12 @@ AXIOS_INSTANCE.interceptors.request.use(function (config) {
78
96
  window.localStorage.setItem('better-auth.session-token', token);
79
97
  }
80
98
  }
81
- if (token) {
99
+ // Re-check for Authorization header before applying browser token
100
+ var hasAuthNow = config.headers.Authorization ||
101
+ config.headers.authorization ||
102
+ config.headers['Authorization'] ||
103
+ config.headers['authorization'];
104
+ if (token && !hasAuthNow) {
82
105
  config.headers.Authorization = "Bearer ".concat(token);
83
106
  }
84
107
  }