@viasoftbr/shared-ui 0.0.7-1 → 0.0.7-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.
package/dist/index.js CHANGED
@@ -2872,19 +2872,24 @@ function formatAxiosError(err) {
2872
2872
  return err instanceof Error ? err : new Error(String(err));
2873
2873
  }
2874
2874
  var fetchApi = {
2875
- getJson: async (path, params, headers) => {
2875
+ getJson: async (path, params, headersOrOptions) => {
2876
2876
  try {
2877
- const response = await (path.includes("/auth") ? authApi : api).get(path.replace("/auth", ""), { params, headers });
2877
+ const requestOptions = normalizeRequestOptions(params, headersOrOptions);
2878
+ const response = await (path.includes("/auth") ? authApi : api).get(path.replace("/auth", ""), {
2879
+ params: withTokenQueryParams(requestOptions),
2880
+ headers: requestOptions.headers
2881
+ });
2878
2882
  return response.data;
2879
2883
  } catch (e10) {
2880
2884
  throw formatAxiosError(e10);
2881
2885
  }
2882
2886
  },
2883
- getText: async (path, params, headers) => {
2887
+ getText: async (path, params, headersOrOptions) => {
2884
2888
  try {
2889
+ const requestOptions = normalizeRequestOptions(params, headersOrOptions);
2885
2890
  const response = await (path.includes("/auth") ? authApi : api).get(path.replace("/auth", ""), {
2886
- params,
2887
- headers,
2891
+ params: withTokenQueryParams(requestOptions),
2892
+ headers: requestOptions.headers,
2888
2893
  responseType: "text"
2889
2894
  });
2890
2895
  return response.data;
@@ -2892,65 +2897,104 @@ var fetchApi = {
2892
2897
  throw formatAxiosError(e10);
2893
2898
  }
2894
2899
  },
2895
- getVoid: async (path, params, headers) => {
2900
+ getVoid: async (path, params, headersOrOptions) => {
2896
2901
  try {
2897
- await (path.includes("/auth") ? authApi : api).get(path.replace("/auth", ""), { params, headers });
2902
+ const requestOptions = normalizeRequestOptions(params, headersOrOptions);
2903
+ await (path.includes("/auth") ? authApi : api).get(path.replace("/auth", ""), {
2904
+ params: withTokenQueryParams(requestOptions),
2905
+ headers: requestOptions.headers
2906
+ });
2898
2907
  } catch (e10) {
2899
2908
  throw formatAxiosError(e10);
2900
2909
  }
2901
2910
  },
2902
- postJson: async (path, payload, headers) => {
2911
+ postJson: async (path, payload, headersOrOptions) => {
2903
2912
  try {
2904
- const response = await (path.includes("/auth") ? authApi : api).post(path.replace("/auth", ""), payload, { headers });
2913
+ const requestOptions = normalizeRequestOptions(void 0, headersOrOptions);
2914
+ const response = await (path.includes("/auth") ? authApi : api).post(path.replace("/auth", ""), payload, {
2915
+ headers: requestOptions.headers,
2916
+ params: withTokenQueryParams(requestOptions)
2917
+ });
2905
2918
  return response.data;
2906
2919
  } catch (e10) {
2907
2920
  throw formatAxiosError(e10);
2908
2921
  }
2909
2922
  },
2910
- patchJson: async (path, payload, headers) => {
2923
+ patchJson: async (path, payload, headersOrOptions) => {
2911
2924
  try {
2912
- const response = await (path.includes("/auth") ? authApi : api).patch(path.replace("/auth", ""), payload, { headers });
2925
+ const requestOptions = normalizeRequestOptions(void 0, headersOrOptions);
2926
+ const response = await (path.includes("/auth") ? authApi : api).patch(path.replace("/auth", ""), payload, {
2927
+ headers: requestOptions.headers,
2928
+ params: withTokenQueryParams(requestOptions)
2929
+ });
2913
2930
  return response.data;
2914
2931
  } catch (e10) {
2915
2932
  throw formatAxiosError(e10);
2916
2933
  }
2917
2934
  },
2918
- putJson: async (path, payload, headers) => {
2935
+ putJson: async (path, payload, headersOrOptions) => {
2919
2936
  try {
2920
- const response = await (path.includes("/auth") ? authApi : api).put(path.replace("/auth", ""), payload, { headers });
2937
+ const requestOptions = normalizeRequestOptions(void 0, headersOrOptions);
2938
+ const response = await (path.includes("/auth") ? authApi : api).put(path.replace("/auth", ""), payload, {
2939
+ headers: requestOptions.headers,
2940
+ params: withTokenQueryParams(requestOptions)
2941
+ });
2921
2942
  return response.data;
2922
2943
  } catch (e10) {
2923
2944
  throw formatAxiosError(e10);
2924
2945
  }
2925
2946
  },
2926
- deleteJson: async (path, headers) => {
2947
+ deleteJson: async (path, headersOrOptions) => {
2927
2948
  try {
2928
- const response = await (path.includes("/auth") ? authApi : api).delete(path.replace("/auth", ""), { headers });
2949
+ const requestOptions = normalizeRequestOptions(void 0, headersOrOptions);
2950
+ const response = await (path.includes("/auth") ? authApi : api).delete(path.replace("/auth", ""), {
2951
+ headers: requestOptions.headers,
2952
+ params: withTokenQueryParams(requestOptions)
2953
+ });
2929
2954
  return response.data;
2930
2955
  } catch (e10) {
2931
2956
  throw formatAxiosError(e10);
2932
2957
  }
2933
2958
  },
2934
- deleteVoid: async (path, headers) => {
2959
+ deleteVoid: async (path, headersOrOptions) => {
2935
2960
  try {
2936
- await (path.includes("/auth") ? authApi : api).delete(path.replace("/auth", ""), { headers });
2961
+ const requestOptions = normalizeRequestOptions(void 0, headersOrOptions);
2962
+ await (path.includes("/auth") ? authApi : api).delete(path.replace("/auth", ""), {
2963
+ headers: requestOptions.headers,
2964
+ params: withTokenQueryParams(requestOptions)
2965
+ });
2937
2966
  } catch (e10) {
2938
2967
  throw formatAxiosError(e10);
2939
2968
  }
2940
2969
  }
2941
2970
  };
2942
- function buildWsUrl(path = "/") {
2971
+ function buildWsUrl(path = "/", options = {}) {
2943
2972
  try {
2944
2973
  const origin2 = new URL(apiOrigin);
2945
2974
  origin2.protocol = origin2.protocol === "https:" ? "wss:" : "ws:";
2946
2975
  if (!path.startsWith("/"))
2947
2976
  path = "/" + path;
2948
2977
  origin2.pathname = path;
2949
- return origin2.toString();
2978
+ return withAccessTokenQuery(origin2.toString(), {
2979
+ headers: options.headers,
2980
+ params: options.query,
2981
+ token: options.token ?? options.bearerToken,
2982
+ tokenQueryParam: options.tokenQueryParam ?? options.bearerQueryParam,
2983
+ includeStoredToken: options.includeStoredToken
2984
+ });
2950
2985
  } catch (e10) {
2951
2986
  const proto = apiOrigin.startsWith("https") ? "wss" : "ws";
2952
2987
  const host = apiOrigin.replace(/^https?:\/\//, "").replace(/\/$/, "");
2953
- return `${proto}://${host}${path.startsWith("/") ? path : "/" + path}`;
2988
+ return withAccessTokenQuery(
2989
+ `${proto}://${host}${path.startsWith("/") ? path : "/" + path}`,
2990
+ {
2991
+ headers: options.headers,
2992
+ params: options.query,
2993
+ token: options.token ?? options.bearerToken,
2994
+ tokenQueryParam: options.tokenQueryParam ?? options.bearerQueryParam,
2995
+ includeStoredToken: options.includeStoredToken
2996
+ }
2997
+ );
2954
2998
  }
2955
2999
  }
2956
3000
  function extractBearerToken(headers, explicitToken) {
@@ -2964,17 +3008,41 @@ function extractBearerToken(headers, explicitToken) {
2964
3008
  const match = authHeader.match(/^Bearer\s+(.+)$/i);
2965
3009
  return match ? match[1] : null;
2966
3010
  }
2967
- function appendWsQueryParam(url, key, value) {
3011
+ function resolveAuthToken(options = {}) {
3012
+ const explicitToken = options.token ?? options.bearerToken ?? null;
3013
+ const headerToken = extractBearerToken(options.headers, explicitToken);
3014
+ if (headerToken)
3015
+ return headerToken;
3016
+ if (options.includeStoredToken === false)
3017
+ return null;
3018
+ return getAccessToken();
3019
+ }
3020
+ function mergeQueryParams(params, key, value) {
3021
+ const nextParams = { ...params ?? {} };
3022
+ if (key && value)
3023
+ nextParams[key] = value;
3024
+ return Object.keys(nextParams).length > 0 ? nextParams : void 0;
3025
+ }
3026
+ function appendQueryParams(url, params) {
3027
+ if (!params || Object.keys(params).length === 0)
3028
+ return url;
2968
3029
  try {
2969
3030
  const parsed = new URL(
2970
3031
  url,
2971
3032
  typeof window !== "undefined" ? window.location.href : void 0
2972
3033
  );
2973
- parsed.searchParams.set(key, value);
3034
+ for (const [key, value] of Object.entries(params)) {
3035
+ if (value === void 0 || value === null)
3036
+ continue;
3037
+ parsed.searchParams.set(key, String(value));
3038
+ }
2974
3039
  return parsed.toString();
2975
3040
  } catch {
3041
+ const search = Object.entries(params).filter(([, value]) => value !== void 0 && value !== null).map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`).join("&");
3042
+ if (!search)
3043
+ return url;
2976
3044
  const separator = url.includes("?") ? "&" : "?";
2977
- return `${url}${separator}${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
3045
+ return `${url}${separator}${search}`;
2978
3046
  }
2979
3047
  }
2980
3048
  function normalizeProtocols(protocols) {
@@ -2982,6 +3050,39 @@ function normalizeProtocols(protocols) {
2982
3050
  return void 0;
2983
3051
  return Array.isArray(protocols) ? protocols.filter(Boolean) : [protocols];
2984
3052
  }
3053
+ function normalizeRequestOptions(params, headersOrOptions) {
3054
+ const maybeOptions = headersOrOptions;
3055
+ const hasOptionShape = Boolean(
3056
+ maybeOptions && ("headers" in maybeOptions || "params" in maybeOptions || "sendTokenInQuery" in maybeOptions || "token" in maybeOptions || "tokenQueryParam" in maybeOptions || "includeStoredToken" in maybeOptions)
3057
+ );
3058
+ if (hasOptionShape) {
3059
+ return {
3060
+ ...maybeOptions,
3061
+ params: { ...params ?? {}, ...maybeOptions?.params ?? {} }
3062
+ };
3063
+ }
3064
+ return {
3065
+ params,
3066
+ headers: headersOrOptions
3067
+ };
3068
+ }
3069
+ function withTokenQueryParams(options) {
3070
+ if (!options.sendTokenInQuery)
3071
+ return options.params;
3072
+ const token = resolveAuthToken(options);
3073
+ return mergeQueryParams(
3074
+ options.params,
3075
+ options.tokenQueryParam ?? "access_token",
3076
+ token
3077
+ );
3078
+ }
3079
+ function withAccessTokenQuery(url, options = {}) {
3080
+ const token = resolveAuthToken(options);
3081
+ return appendQueryParams(
3082
+ url,
3083
+ mergeQueryParams(options.params, options.tokenQueryParam ?? "access_token", token)
3084
+ );
3085
+ }
2985
3086
  async function encodeFfurl(settings) {
2986
3087
  const { ffurl } = await fetchApi.postJson("/auth/protocols/ffurl/encode", settings);
2987
3088
  return ffurl;
@@ -2991,9 +3092,14 @@ async function decodeFfurl(ffurl) {
2991
3092
  return settings;
2992
3093
  }
2993
3094
  function subscribeToWebsocket(url, onMessage, options = {}) {
2994
- const bearerToken = extractBearerToken(options.headers, options.bearerToken);
2995
- const bearerStrategy = options.bearerStrategy ?? "query";
2996
- let resolvedUrl = url;
3095
+ const bearerToken = resolveAuthToken({
3096
+ headers: options.headers,
3097
+ token: options.token,
3098
+ bearerToken: options.bearerToken,
3099
+ includeStoredToken: options.includeStoredToken
3100
+ });
3101
+ const bearerStrategy = options.tokenTransport ?? options.bearerStrategy ?? "query";
3102
+ let resolvedUrl = appendQueryParams(url, options.query);
2997
3103
  const protocols = normalizeProtocols(options.protocols) ?? [];
2998
3104
  if (options.headers && Object.keys(options.headers).length > 0) {
2999
3105
  console.warn(
@@ -3004,11 +3110,11 @@ function subscribeToWebsocket(url, onMessage, options = {}) {
3004
3110
  if (bearerStrategy === "protocol") {
3005
3111
  protocols.push("bearer", bearerToken);
3006
3112
  } else {
3007
- resolvedUrl = appendWsQueryParam(
3008
- url,
3009
- options.bearerQueryParam ?? "access_token",
3010
- bearerToken
3011
- );
3113
+ resolvedUrl = withAccessTokenQuery(resolvedUrl, {
3114
+ token: bearerToken,
3115
+ tokenQueryParam: options.tokenQueryParam ?? options.bearerQueryParam,
3116
+ includeStoredToken: false
3117
+ });
3012
3118
  }
3013
3119
  }
3014
3120
  const socket = protocols.length > 0 ? new WebSocket(resolvedUrl, protocols) : new WebSocket(resolvedUrl);
@@ -41353,7 +41459,8 @@ export {
41353
41459
  useAuth,
41354
41460
  useAvailableSubservices,
41355
41461
  useSettings,
41356
- userService
41462
+ userService,
41463
+ withAccessTokenQuery
41357
41464
  };
41358
41465
  /*! Bundled license information:
41359
41466
 
@@ -1,5 +1,12 @@
1
1
  import { ProtocolSettings } from '../types/protocol';
2
- import { WebSocketSubscribeOptions } from '../types/websocket';
2
+ import { AuthTokenQueryOptions, WebSocketSubscribeOptions } from '../types/websocket';
3
+ type QueryParamValue = string | number | boolean | null | undefined;
4
+ type QueryParams = Record<string, QueryParamValue>;
5
+ interface RequestOptions extends AuthTokenQueryOptions {
6
+ headers?: Record<string, string>;
7
+ params?: QueryParams;
8
+ sendTokenInQuery?: boolean;
9
+ }
3
10
  export declare const api: import("axios").AxiosInstance;
4
11
  export declare const authApi: import("axios").AxiosInstance;
5
12
  export declare function getAccessToken(): string | null;
@@ -8,16 +15,18 @@ export declare function getRefreshToken(): string | null;
8
15
  export declare function setRefreshToken(token: string | null): void;
9
16
  export declare function clearTokens(): void;
10
17
  export declare const fetchApi: {
11
- getJson: <T>(path: string, params?: Record<string, any>, headers?: Record<string, string>) => Promise<T>;
12
- getText: (path: string, params?: Record<string, any>, headers?: Record<string, string>) => Promise<string>;
13
- getVoid: (path: string, params?: Record<string, any>, headers?: Record<string, string>) => Promise<void>;
14
- postJson: <T>(path: string, payload?: any, headers?: Record<string, string>) => Promise<T>;
15
- patchJson: <T>(path: string, payload?: any, headers?: Record<string, string>) => Promise<T>;
16
- putJson: <T>(path: string, payload?: any, headers?: Record<string, string>) => Promise<T>;
17
- deleteJson: <T>(path: string, headers?: Record<string, string>) => Promise<T>;
18
- deleteVoid: (path: string, headers?: Record<string, string>) => Promise<void>;
18
+ getJson: <T>(path: string, params?: QueryParams, headersOrOptions?: Record<string, string> | RequestOptions) => Promise<T>;
19
+ getText: (path: string, params?: QueryParams, headersOrOptions?: Record<string, string> | RequestOptions) => Promise<string>;
20
+ getVoid: (path: string, params?: QueryParams, headersOrOptions?: Record<string, string> | RequestOptions) => Promise<void>;
21
+ postJson: <T>(path: string, payload?: any, headersOrOptions?: Record<string, string> | RequestOptions) => Promise<T>;
22
+ patchJson: <T>(path: string, payload?: any, headersOrOptions?: Record<string, string> | RequestOptions) => Promise<T>;
23
+ putJson: <T>(path: string, payload?: any, headersOrOptions?: Record<string, string> | RequestOptions) => Promise<T>;
24
+ deleteJson: <T>(path: string, headersOrOptions?: Record<string, string> | RequestOptions) => Promise<T>;
25
+ deleteVoid: (path: string, headersOrOptions?: Record<string, string> | RequestOptions) => Promise<void>;
19
26
  };
20
- export declare function buildWsUrl(path?: string): string;
27
+ export declare function buildWsUrl(path?: string, options?: Omit<WebSocketSubscribeOptions, 'protocols'>): string;
28
+ export declare function withAccessTokenQuery(url: string, options?: Pick<RequestOptions, 'headers' | 'params' | 'token' | 'tokenQueryParam' | 'includeStoredToken'>): string;
21
29
  export declare function encodeFfurl(settings: ProtocolSettings): Promise<string>;
22
30
  export declare function decodeFfurl(ffurl: string): Promise<ProtocolSettings>;
23
31
  export declare function subscribeToWebsocket(url: string, onMessage: (data: any) => void, options?: WebSocketSubscribeOptions): () => void;
32
+ export {};
package/dist/services.cjs CHANGED
@@ -44,7 +44,8 @@ __export(services_exports, {
44
44
  setAccessToken: () => setAccessToken,
45
45
  setRefreshToken: () => setRefreshToken,
46
46
  subscribeToWebsocket: () => subscribeToWebsocket,
47
- userService: () => userService
47
+ userService: () => userService,
48
+ withAccessTokenQuery: () => withAccessTokenQuery
48
49
  });
49
50
  module.exports = __toCommonJS(services_exports);
50
51
 
@@ -2806,19 +2807,24 @@ function formatAxiosError(err) {
2806
2807
  return err instanceof Error ? err : new Error(String(err));
2807
2808
  }
2808
2809
  var fetchApi = {
2809
- getJson: async (path, params, headers) => {
2810
+ getJson: async (path, params, headersOrOptions) => {
2810
2811
  try {
2811
- const response = await (path.includes("/auth") ? authApi : api).get(path.replace("/auth", ""), { params, headers });
2812
+ const requestOptions = normalizeRequestOptions(params, headersOrOptions);
2813
+ const response = await (path.includes("/auth") ? authApi : api).get(path.replace("/auth", ""), {
2814
+ params: withTokenQueryParams(requestOptions),
2815
+ headers: requestOptions.headers
2816
+ });
2812
2817
  return response.data;
2813
2818
  } catch (e) {
2814
2819
  throw formatAxiosError(e);
2815
2820
  }
2816
2821
  },
2817
- getText: async (path, params, headers) => {
2822
+ getText: async (path, params, headersOrOptions) => {
2818
2823
  try {
2824
+ const requestOptions = normalizeRequestOptions(params, headersOrOptions);
2819
2825
  const response = await (path.includes("/auth") ? authApi : api).get(path.replace("/auth", ""), {
2820
- params,
2821
- headers,
2826
+ params: withTokenQueryParams(requestOptions),
2827
+ headers: requestOptions.headers,
2822
2828
  responseType: "text"
2823
2829
  });
2824
2830
  return response.data;
@@ -2826,65 +2832,104 @@ var fetchApi = {
2826
2832
  throw formatAxiosError(e);
2827
2833
  }
2828
2834
  },
2829
- getVoid: async (path, params, headers) => {
2835
+ getVoid: async (path, params, headersOrOptions) => {
2830
2836
  try {
2831
- await (path.includes("/auth") ? authApi : api).get(path.replace("/auth", ""), { params, headers });
2837
+ const requestOptions = normalizeRequestOptions(params, headersOrOptions);
2838
+ await (path.includes("/auth") ? authApi : api).get(path.replace("/auth", ""), {
2839
+ params: withTokenQueryParams(requestOptions),
2840
+ headers: requestOptions.headers
2841
+ });
2832
2842
  } catch (e) {
2833
2843
  throw formatAxiosError(e);
2834
2844
  }
2835
2845
  },
2836
- postJson: async (path, payload, headers) => {
2846
+ postJson: async (path, payload, headersOrOptions) => {
2837
2847
  try {
2838
- const response = await (path.includes("/auth") ? authApi : api).post(path.replace("/auth", ""), payload, { headers });
2848
+ const requestOptions = normalizeRequestOptions(void 0, headersOrOptions);
2849
+ const response = await (path.includes("/auth") ? authApi : api).post(path.replace("/auth", ""), payload, {
2850
+ headers: requestOptions.headers,
2851
+ params: withTokenQueryParams(requestOptions)
2852
+ });
2839
2853
  return response.data;
2840
2854
  } catch (e) {
2841
2855
  throw formatAxiosError(e);
2842
2856
  }
2843
2857
  },
2844
- patchJson: async (path, payload, headers) => {
2858
+ patchJson: async (path, payload, headersOrOptions) => {
2845
2859
  try {
2846
- const response = await (path.includes("/auth") ? authApi : api).patch(path.replace("/auth", ""), payload, { headers });
2860
+ const requestOptions = normalizeRequestOptions(void 0, headersOrOptions);
2861
+ const response = await (path.includes("/auth") ? authApi : api).patch(path.replace("/auth", ""), payload, {
2862
+ headers: requestOptions.headers,
2863
+ params: withTokenQueryParams(requestOptions)
2864
+ });
2847
2865
  return response.data;
2848
2866
  } catch (e) {
2849
2867
  throw formatAxiosError(e);
2850
2868
  }
2851
2869
  },
2852
- putJson: async (path, payload, headers) => {
2870
+ putJson: async (path, payload, headersOrOptions) => {
2853
2871
  try {
2854
- const response = await (path.includes("/auth") ? authApi : api).put(path.replace("/auth", ""), payload, { headers });
2872
+ const requestOptions = normalizeRequestOptions(void 0, headersOrOptions);
2873
+ const response = await (path.includes("/auth") ? authApi : api).put(path.replace("/auth", ""), payload, {
2874
+ headers: requestOptions.headers,
2875
+ params: withTokenQueryParams(requestOptions)
2876
+ });
2855
2877
  return response.data;
2856
2878
  } catch (e) {
2857
2879
  throw formatAxiosError(e);
2858
2880
  }
2859
2881
  },
2860
- deleteJson: async (path, headers) => {
2882
+ deleteJson: async (path, headersOrOptions) => {
2861
2883
  try {
2862
- const response = await (path.includes("/auth") ? authApi : api).delete(path.replace("/auth", ""), { headers });
2884
+ const requestOptions = normalizeRequestOptions(void 0, headersOrOptions);
2885
+ const response = await (path.includes("/auth") ? authApi : api).delete(path.replace("/auth", ""), {
2886
+ headers: requestOptions.headers,
2887
+ params: withTokenQueryParams(requestOptions)
2888
+ });
2863
2889
  return response.data;
2864
2890
  } catch (e) {
2865
2891
  throw formatAxiosError(e);
2866
2892
  }
2867
2893
  },
2868
- deleteVoid: async (path, headers) => {
2894
+ deleteVoid: async (path, headersOrOptions) => {
2869
2895
  try {
2870
- await (path.includes("/auth") ? authApi : api).delete(path.replace("/auth", ""), { headers });
2896
+ const requestOptions = normalizeRequestOptions(void 0, headersOrOptions);
2897
+ await (path.includes("/auth") ? authApi : api).delete(path.replace("/auth", ""), {
2898
+ headers: requestOptions.headers,
2899
+ params: withTokenQueryParams(requestOptions)
2900
+ });
2871
2901
  } catch (e) {
2872
2902
  throw formatAxiosError(e);
2873
2903
  }
2874
2904
  }
2875
2905
  };
2876
- function buildWsUrl(path = "/") {
2906
+ function buildWsUrl(path = "/", options = {}) {
2877
2907
  try {
2878
2908
  const origin2 = new URL(apiOrigin);
2879
2909
  origin2.protocol = origin2.protocol === "https:" ? "wss:" : "ws:";
2880
2910
  if (!path.startsWith("/"))
2881
2911
  path = "/" + path;
2882
2912
  origin2.pathname = path;
2883
- return origin2.toString();
2913
+ return withAccessTokenQuery(origin2.toString(), {
2914
+ headers: options.headers,
2915
+ params: options.query,
2916
+ token: options.token ?? options.bearerToken,
2917
+ tokenQueryParam: options.tokenQueryParam ?? options.bearerQueryParam,
2918
+ includeStoredToken: options.includeStoredToken
2919
+ });
2884
2920
  } catch (e) {
2885
2921
  const proto = apiOrigin.startsWith("https") ? "wss" : "ws";
2886
2922
  const host = apiOrigin.replace(/^https?:\/\//, "").replace(/\/$/, "");
2887
- return `${proto}://${host}${path.startsWith("/") ? path : "/" + path}`;
2923
+ return withAccessTokenQuery(
2924
+ `${proto}://${host}${path.startsWith("/") ? path : "/" + path}`,
2925
+ {
2926
+ headers: options.headers,
2927
+ params: options.query,
2928
+ token: options.token ?? options.bearerToken,
2929
+ tokenQueryParam: options.tokenQueryParam ?? options.bearerQueryParam,
2930
+ includeStoredToken: options.includeStoredToken
2931
+ }
2932
+ );
2888
2933
  }
2889
2934
  }
2890
2935
  function extractBearerToken(headers, explicitToken) {
@@ -2898,17 +2943,41 @@ function extractBearerToken(headers, explicitToken) {
2898
2943
  const match = authHeader.match(/^Bearer\s+(.+)$/i);
2899
2944
  return match ? match[1] : null;
2900
2945
  }
2901
- function appendWsQueryParam(url, key, value) {
2946
+ function resolveAuthToken(options = {}) {
2947
+ const explicitToken = options.token ?? options.bearerToken ?? null;
2948
+ const headerToken = extractBearerToken(options.headers, explicitToken);
2949
+ if (headerToken)
2950
+ return headerToken;
2951
+ if (options.includeStoredToken === false)
2952
+ return null;
2953
+ return getAccessToken();
2954
+ }
2955
+ function mergeQueryParams(params, key, value) {
2956
+ const nextParams = { ...params ?? {} };
2957
+ if (key && value)
2958
+ nextParams[key] = value;
2959
+ return Object.keys(nextParams).length > 0 ? nextParams : void 0;
2960
+ }
2961
+ function appendQueryParams(url, params) {
2962
+ if (!params || Object.keys(params).length === 0)
2963
+ return url;
2902
2964
  try {
2903
2965
  const parsed = new URL(
2904
2966
  url,
2905
2967
  typeof window !== "undefined" ? window.location.href : void 0
2906
2968
  );
2907
- parsed.searchParams.set(key, value);
2969
+ for (const [key, value] of Object.entries(params)) {
2970
+ if (value === void 0 || value === null)
2971
+ continue;
2972
+ parsed.searchParams.set(key, String(value));
2973
+ }
2908
2974
  return parsed.toString();
2909
2975
  } catch {
2976
+ const search = Object.entries(params).filter(([, value]) => value !== void 0 && value !== null).map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`).join("&");
2977
+ if (!search)
2978
+ return url;
2910
2979
  const separator = url.includes("?") ? "&" : "?";
2911
- return `${url}${separator}${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
2980
+ return `${url}${separator}${search}`;
2912
2981
  }
2913
2982
  }
2914
2983
  function normalizeProtocols(protocols) {
@@ -2916,6 +2985,39 @@ function normalizeProtocols(protocols) {
2916
2985
  return void 0;
2917
2986
  return Array.isArray(protocols) ? protocols.filter(Boolean) : [protocols];
2918
2987
  }
2988
+ function normalizeRequestOptions(params, headersOrOptions) {
2989
+ const maybeOptions = headersOrOptions;
2990
+ const hasOptionShape = Boolean(
2991
+ maybeOptions && ("headers" in maybeOptions || "params" in maybeOptions || "sendTokenInQuery" in maybeOptions || "token" in maybeOptions || "tokenQueryParam" in maybeOptions || "includeStoredToken" in maybeOptions)
2992
+ );
2993
+ if (hasOptionShape) {
2994
+ return {
2995
+ ...maybeOptions,
2996
+ params: { ...params ?? {}, ...maybeOptions?.params ?? {} }
2997
+ };
2998
+ }
2999
+ return {
3000
+ params,
3001
+ headers: headersOrOptions
3002
+ };
3003
+ }
3004
+ function withTokenQueryParams(options) {
3005
+ if (!options.sendTokenInQuery)
3006
+ return options.params;
3007
+ const token = resolveAuthToken(options);
3008
+ return mergeQueryParams(
3009
+ options.params,
3010
+ options.tokenQueryParam ?? "access_token",
3011
+ token
3012
+ );
3013
+ }
3014
+ function withAccessTokenQuery(url, options = {}) {
3015
+ const token = resolveAuthToken(options);
3016
+ return appendQueryParams(
3017
+ url,
3018
+ mergeQueryParams(options.params, options.tokenQueryParam ?? "access_token", token)
3019
+ );
3020
+ }
2919
3021
  async function encodeFfurl(settings) {
2920
3022
  const { ffurl } = await fetchApi.postJson("/auth/protocols/ffurl/encode", settings);
2921
3023
  return ffurl;
@@ -2925,9 +3027,14 @@ async function decodeFfurl(ffurl) {
2925
3027
  return settings;
2926
3028
  }
2927
3029
  function subscribeToWebsocket(url, onMessage, options = {}) {
2928
- const bearerToken = extractBearerToken(options.headers, options.bearerToken);
2929
- const bearerStrategy = options.bearerStrategy ?? "query";
2930
- let resolvedUrl = url;
3030
+ const bearerToken = resolveAuthToken({
3031
+ headers: options.headers,
3032
+ token: options.token,
3033
+ bearerToken: options.bearerToken,
3034
+ includeStoredToken: options.includeStoredToken
3035
+ });
3036
+ const bearerStrategy = options.tokenTransport ?? options.bearerStrategy ?? "query";
3037
+ let resolvedUrl = appendQueryParams(url, options.query);
2931
3038
  const protocols = normalizeProtocols(options.protocols) ?? [];
2932
3039
  if (options.headers && Object.keys(options.headers).length > 0) {
2933
3040
  console.warn(
@@ -2938,11 +3045,11 @@ function subscribeToWebsocket(url, onMessage, options = {}) {
2938
3045
  if (bearerStrategy === "protocol") {
2939
3046
  protocols.push("bearer", bearerToken);
2940
3047
  } else {
2941
- resolvedUrl = appendWsQueryParam(
2942
- url,
2943
- options.bearerQueryParam ?? "access_token",
2944
- bearerToken
2945
- );
3048
+ resolvedUrl = withAccessTokenQuery(resolvedUrl, {
3049
+ token: bearerToken,
3050
+ tokenQueryParam: options.tokenQueryParam ?? options.bearerQueryParam,
3051
+ includeStoredToken: false
3052
+ });
2946
3053
  }
2947
3054
  }
2948
3055
  const socket = protocols.length > 0 ? new WebSocket(resolvedUrl, protocols) : new WebSocket(resolvedUrl);