@viasoftbr/shared-ui 0.0.7-4 → 0.0.7-6

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,24 +2872,22 @@ 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, headersOrOptions) => {
2875
+ getJson: async (path, params, headers) => {
2876
2876
  try {
2877
- const requestOptions = normalizeRequestOptions(params, headersOrOptions);
2878
2877
  const response = await (path.includes("/auth") ? authApi : api).get(path.replace("/auth", ""), {
2879
- params: withTokenQueryParams(requestOptions),
2880
- headers: requestOptions.headers
2878
+ params,
2879
+ headers
2881
2880
  });
2882
2881
  return response.data;
2883
2882
  } catch (e10) {
2884
2883
  throw formatAxiosError(e10);
2885
2884
  }
2886
2885
  },
2887
- getText: async (path, params, headersOrOptions) => {
2886
+ getText: async (path, params, headers) => {
2888
2887
  try {
2889
- const requestOptions = normalizeRequestOptions(params, headersOrOptions);
2890
2888
  const response = await (path.includes("/auth") ? authApi : api).get(path.replace("/auth", ""), {
2891
- params: withTokenQueryParams(requestOptions),
2892
- headers: requestOptions.headers,
2889
+ params,
2890
+ headers,
2893
2891
  responseType: "text"
2894
2892
  });
2895
2893
  return response.data;
@@ -2897,106 +2895,61 @@ var fetchApi = {
2897
2895
  throw formatAxiosError(e10);
2898
2896
  }
2899
2897
  },
2900
- getVoid: async (path, params, headersOrOptions) => {
2898
+ getVoid: async (path, params, headers) => {
2901
2899
  try {
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
- });
2900
+ await (path.includes("/auth") ? authApi : api).get(path.replace("/auth", ""), { params, headers });
2907
2901
  } catch (e10) {
2908
2902
  throw formatAxiosError(e10);
2909
2903
  }
2910
2904
  },
2911
- postJson: async (path, payload, headersOrOptions) => {
2905
+ postJson: async (path, payload, headers) => {
2912
2906
  try {
2913
- const requestOptions = normalizeRequestOptions(void 0, headersOrOptions);
2914
2907
  const response = await (path.includes("/auth") ? authApi : api).post(path.replace("/auth", ""), payload, {
2915
- headers: requestOptions.headers,
2916
- params: withTokenQueryParams(requestOptions)
2908
+ headers
2917
2909
  });
2918
2910
  return response.data;
2919
2911
  } catch (e10) {
2920
2912
  throw formatAxiosError(e10);
2921
2913
  }
2922
2914
  },
2923
- patchJson: async (path, payload, headersOrOptions) => {
2915
+ patchJson: async (path, payload, headers) => {
2924
2916
  try {
2925
- const requestOptions = normalizeRequestOptions(void 0, headersOrOptions);
2926
2917
  const response = await (path.includes("/auth") ? authApi : api).patch(path.replace("/auth", ""), payload, {
2927
- headers: requestOptions.headers,
2928
- params: withTokenQueryParams(requestOptions)
2918
+ headers
2929
2919
  });
2930
2920
  return response.data;
2931
2921
  } catch (e10) {
2932
2922
  throw formatAxiosError(e10);
2933
2923
  }
2934
2924
  },
2935
- putJson: async (path, payload, headersOrOptions) => {
2925
+ putJson: async (path, payload, headers) => {
2936
2926
  try {
2937
- const requestOptions = normalizeRequestOptions(void 0, headersOrOptions);
2938
2927
  const response = await (path.includes("/auth") ? authApi : api).put(path.replace("/auth", ""), payload, {
2939
- headers: requestOptions.headers,
2940
- params: withTokenQueryParams(requestOptions)
2928
+ headers
2941
2929
  });
2942
2930
  return response.data;
2943
2931
  } catch (e10) {
2944
2932
  throw formatAxiosError(e10);
2945
2933
  }
2946
2934
  },
2947
- deleteJson: async (path, headersOrOptions) => {
2935
+ deleteJson: async (path, headers) => {
2948
2936
  try {
2949
- const requestOptions = normalizeRequestOptions(void 0, headersOrOptions);
2950
2937
  const response = await (path.includes("/auth") ? authApi : api).delete(path.replace("/auth", ""), {
2951
- headers: requestOptions.headers,
2952
- params: withTokenQueryParams(requestOptions)
2938
+ headers
2953
2939
  });
2954
2940
  return response.data;
2955
2941
  } catch (e10) {
2956
2942
  throw formatAxiosError(e10);
2957
2943
  }
2958
2944
  },
2959
- deleteVoid: async (path, headersOrOptions) => {
2945
+ deleteVoid: async (path, headers) => {
2960
2946
  try {
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
- });
2947
+ await (path.includes("/auth") ? authApi : api).delete(path.replace("/auth", ""), { headers });
2966
2948
  } catch (e10) {
2967
2949
  throw formatAxiosError(e10);
2968
2950
  }
2969
2951
  }
2970
2952
  };
2971
- function buildWsUrl(path = "/", options = {}) {
2972
- try {
2973
- const origin2 = new URL(apiOrigin);
2974
- origin2.protocol = origin2.protocol === "https:" ? "wss:" : "ws:";
2975
- if (!path.startsWith("/"))
2976
- path = "/" + path;
2977
- origin2.pathname = path;
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
- });
2985
- } catch (e10) {
2986
- const proto = apiOrigin.startsWith("https") ? "wss" : "ws";
2987
- const host = apiOrigin.replace(/^https?:\/\//, "").replace(/\/$/, "");
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
- );
2998
- }
2999
- }
3000
2953
  function extractBearerToken(headers, explicitToken) {
3001
2954
  if (explicitToken)
3002
2955
  return explicitToken;
@@ -3008,7 +2961,7 @@ function extractBearerToken(headers, explicitToken) {
3008
2961
  const match = authHeader.match(/^Bearer\s+(.+)$/i);
3009
2962
  return match ? match[1] : null;
3010
2963
  }
3011
- function resolveAuthToken(options = {}) {
2964
+ function resolveWebSocketToken(options = {}) {
3012
2965
  const explicitToken = options.token ?? options.bearerToken ?? null;
3013
2966
  const headerToken = extractBearerToken(options.headers, explicitToken);
3014
2967
  if (headerToken)
@@ -3017,12 +2970,6 @@ function resolveAuthToken(options = {}) {
3017
2970
  return null;
3018
2971
  return getAccessToken();
3019
2972
  }
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
2973
  function appendQueryParams(url, params) {
3027
2974
  if (!params || Object.keys(params).length === 0)
3028
2975
  return url;
@@ -3045,43 +2992,32 @@ function appendQueryParams(url, params) {
3045
2992
  return `${url}${separator}${search}`;
3046
2993
  }
3047
2994
  }
2995
+ function withWebSocketTokenQuery(url, options) {
2996
+ const token = resolveWebSocketToken(options);
2997
+ const params = { ...options.query ?? {} };
2998
+ if (token) {
2999
+ params[options.tokenQueryParam ?? options.bearerQueryParam ?? "token"] = token;
3000
+ }
3001
+ return appendQueryParams(url, params);
3002
+ }
3048
3003
  function normalizeProtocols(protocols) {
3049
3004
  if (!protocols)
3050
3005
  return void 0;
3051
3006
  return Array.isArray(protocols) ? protocols.filter(Boolean) : [protocols];
3052
3007
  }
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
- };
3008
+ function buildWsUrl(path = "/", options = {}) {
3009
+ try {
3010
+ const origin2 = new URL(apiOrigin);
3011
+ origin2.protocol = origin2.protocol === "https:" ? "wss:" : "ws:";
3012
+ if (!path.startsWith("/"))
3013
+ path = "/" + path;
3014
+ origin2.pathname = path;
3015
+ return withWebSocketTokenQuery(origin2.toString(), options);
3016
+ } catch (e10) {
3017
+ const proto = apiOrigin.startsWith("https") ? "wss" : "ws";
3018
+ const host = apiOrigin.replace(/^https?:\/\//, "").replace(/\/$/, "");
3019
+ return withWebSocketTokenQuery(`${proto}://${host}${path.startsWith("/") ? path : "/" + path}`, options);
3063
3020
  }
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
3021
  }
3086
3022
  async function encodeFfurl(settings) {
3087
3023
  const { ffurl } = await fetchApi.postJson("/auth/protocols/ffurl/encode", settings);
@@ -3092,30 +3028,18 @@ async function decodeFfurl(ffurl) {
3092
3028
  return settings;
3093
3029
  }
3094
3030
  function subscribeToWebsocket(url, onMessage, options = {}) {
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);
3031
+ const tokenTransport = options.tokenTransport ?? options.bearerStrategy ?? "query";
3103
3032
  const protocols = normalizeProtocols(options.protocols) ?? [];
3104
3033
  if (options.headers && Object.keys(options.headers).length > 0) {
3105
3034
  console.warn(
3106
3035
  "WebSocket connections in browsers do not support custom HTTP headers. shared-ui will derive bearer auth from Authorization and forward it using query string or subprotocol."
3107
3036
  );
3108
3037
  }
3109
- if (bearerToken) {
3110
- if (bearerStrategy === "protocol") {
3111
- protocols.push("bearer", bearerToken);
3112
- } else {
3113
- resolvedUrl = withAccessTokenQuery(resolvedUrl, {
3114
- token: bearerToken,
3115
- tokenQueryParam: options.tokenQueryParam ?? options.bearerQueryParam,
3116
- includeStoredToken: false
3117
- });
3118
- }
3038
+ let resolvedUrl = tokenTransport === "query" ? withWebSocketTokenQuery(url, options) : appendQueryParams(url, options.query);
3039
+ if (tokenTransport === "protocol") {
3040
+ const token = resolveWebSocketToken(options);
3041
+ if (token)
3042
+ protocols.push("bearer", token);
3119
3043
  }
3120
3044
  const socket = protocols.length > 0 ? new WebSocket(resolvedUrl, protocols) : new WebSocket(resolvedUrl);
3121
3045
  socket.onmessage = (event) => {
@@ -41459,8 +41383,7 @@ export {
41459
41383
  useAuth,
41460
41384
  useAvailableSubservices,
41461
41385
  useSettings,
41462
- userService,
41463
- withAccessTokenQuery
41386
+ userService
41464
41387
  };
41465
41388
  /*! Bundled license information:
41466
41389
 
@@ -1,12 +1,5 @@
1
1
  import { ProtocolSettings } from '../types/protocol';
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
- }
2
+ import { WebSocketSubscribeOptions } from '../types/websocket';
10
3
  export declare const api: import("axios").AxiosInstance;
11
4
  export declare const authApi: import("axios").AxiosInstance;
12
5
  export declare function getAccessToken(): string | null;
@@ -15,18 +8,16 @@ export declare function getRefreshToken(): string | null;
15
8
  export declare function setRefreshToken(token: string | null): void;
16
9
  export declare function clearTokens(): void;
17
10
  export declare const fetchApi: {
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>;
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>;
26
19
  };
27
20
  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;
29
21
  export declare function encodeFfurl(settings: ProtocolSettings): Promise<string>;
30
22
  export declare function decodeFfurl(ffurl: string): Promise<ProtocolSettings>;
31
23
  export declare function subscribeToWebsocket(url: string, onMessage: (data: any) => void, options?: WebSocketSubscribeOptions): () => void;
32
- export {};
package/dist/services.cjs CHANGED
@@ -44,8 +44,7 @@ __export(services_exports, {
44
44
  setAccessToken: () => setAccessToken,
45
45
  setRefreshToken: () => setRefreshToken,
46
46
  subscribeToWebsocket: () => subscribeToWebsocket,
47
- userService: () => userService,
48
- withAccessTokenQuery: () => withAccessTokenQuery
47
+ userService: () => userService
49
48
  });
50
49
  module.exports = __toCommonJS(services_exports);
51
50
 
@@ -2807,24 +2806,22 @@ function formatAxiosError(err) {
2807
2806
  return err instanceof Error ? err : new Error(String(err));
2808
2807
  }
2809
2808
  var fetchApi = {
2810
- getJson: async (path, params, headersOrOptions) => {
2809
+ getJson: async (path, params, headers) => {
2811
2810
  try {
2812
- const requestOptions = normalizeRequestOptions(params, headersOrOptions);
2813
2811
  const response = await (path.includes("/auth") ? authApi : api).get(path.replace("/auth", ""), {
2814
- params: withTokenQueryParams(requestOptions),
2815
- headers: requestOptions.headers
2812
+ params,
2813
+ headers
2816
2814
  });
2817
2815
  return response.data;
2818
2816
  } catch (e) {
2819
2817
  throw formatAxiosError(e);
2820
2818
  }
2821
2819
  },
2822
- getText: async (path, params, headersOrOptions) => {
2820
+ getText: async (path, params, headers) => {
2823
2821
  try {
2824
- const requestOptions = normalizeRequestOptions(params, headersOrOptions);
2825
2822
  const response = await (path.includes("/auth") ? authApi : api).get(path.replace("/auth", ""), {
2826
- params: withTokenQueryParams(requestOptions),
2827
- headers: requestOptions.headers,
2823
+ params,
2824
+ headers,
2828
2825
  responseType: "text"
2829
2826
  });
2830
2827
  return response.data;
@@ -2832,106 +2829,61 @@ var fetchApi = {
2832
2829
  throw formatAxiosError(e);
2833
2830
  }
2834
2831
  },
2835
- getVoid: async (path, params, headersOrOptions) => {
2832
+ getVoid: async (path, params, headers) => {
2836
2833
  try {
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
- });
2834
+ await (path.includes("/auth") ? authApi : api).get(path.replace("/auth", ""), { params, headers });
2842
2835
  } catch (e) {
2843
2836
  throw formatAxiosError(e);
2844
2837
  }
2845
2838
  },
2846
- postJson: async (path, payload, headersOrOptions) => {
2839
+ postJson: async (path, payload, headers) => {
2847
2840
  try {
2848
- const requestOptions = normalizeRequestOptions(void 0, headersOrOptions);
2849
2841
  const response = await (path.includes("/auth") ? authApi : api).post(path.replace("/auth", ""), payload, {
2850
- headers: requestOptions.headers,
2851
- params: withTokenQueryParams(requestOptions)
2842
+ headers
2852
2843
  });
2853
2844
  return response.data;
2854
2845
  } catch (e) {
2855
2846
  throw formatAxiosError(e);
2856
2847
  }
2857
2848
  },
2858
- patchJson: async (path, payload, headersOrOptions) => {
2849
+ patchJson: async (path, payload, headers) => {
2859
2850
  try {
2860
- const requestOptions = normalizeRequestOptions(void 0, headersOrOptions);
2861
2851
  const response = await (path.includes("/auth") ? authApi : api).patch(path.replace("/auth", ""), payload, {
2862
- headers: requestOptions.headers,
2863
- params: withTokenQueryParams(requestOptions)
2852
+ headers
2864
2853
  });
2865
2854
  return response.data;
2866
2855
  } catch (e) {
2867
2856
  throw formatAxiosError(e);
2868
2857
  }
2869
2858
  },
2870
- putJson: async (path, payload, headersOrOptions) => {
2859
+ putJson: async (path, payload, headers) => {
2871
2860
  try {
2872
- const requestOptions = normalizeRequestOptions(void 0, headersOrOptions);
2873
2861
  const response = await (path.includes("/auth") ? authApi : api).put(path.replace("/auth", ""), payload, {
2874
- headers: requestOptions.headers,
2875
- params: withTokenQueryParams(requestOptions)
2862
+ headers
2876
2863
  });
2877
2864
  return response.data;
2878
2865
  } catch (e) {
2879
2866
  throw formatAxiosError(e);
2880
2867
  }
2881
2868
  },
2882
- deleteJson: async (path, headersOrOptions) => {
2869
+ deleteJson: async (path, headers) => {
2883
2870
  try {
2884
- const requestOptions = normalizeRequestOptions(void 0, headersOrOptions);
2885
2871
  const response = await (path.includes("/auth") ? authApi : api).delete(path.replace("/auth", ""), {
2886
- headers: requestOptions.headers,
2887
- params: withTokenQueryParams(requestOptions)
2872
+ headers
2888
2873
  });
2889
2874
  return response.data;
2890
2875
  } catch (e) {
2891
2876
  throw formatAxiosError(e);
2892
2877
  }
2893
2878
  },
2894
- deleteVoid: async (path, headersOrOptions) => {
2879
+ deleteVoid: async (path, headers) => {
2895
2880
  try {
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
- });
2881
+ await (path.includes("/auth") ? authApi : api).delete(path.replace("/auth", ""), { headers });
2901
2882
  } catch (e) {
2902
2883
  throw formatAxiosError(e);
2903
2884
  }
2904
2885
  }
2905
2886
  };
2906
- function buildWsUrl(path = "/", options = {}) {
2907
- try {
2908
- const origin2 = new URL(apiOrigin);
2909
- origin2.protocol = origin2.protocol === "https:" ? "wss:" : "ws:";
2910
- if (!path.startsWith("/"))
2911
- path = "/" + path;
2912
- origin2.pathname = path;
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
- });
2920
- } catch (e) {
2921
- const proto = apiOrigin.startsWith("https") ? "wss" : "ws";
2922
- const host = apiOrigin.replace(/^https?:\/\//, "").replace(/\/$/, "");
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
- );
2933
- }
2934
- }
2935
2887
  function extractBearerToken(headers, explicitToken) {
2936
2888
  if (explicitToken)
2937
2889
  return explicitToken;
@@ -2943,7 +2895,7 @@ function extractBearerToken(headers, explicitToken) {
2943
2895
  const match = authHeader.match(/^Bearer\s+(.+)$/i);
2944
2896
  return match ? match[1] : null;
2945
2897
  }
2946
- function resolveAuthToken(options = {}) {
2898
+ function resolveWebSocketToken(options = {}) {
2947
2899
  const explicitToken = options.token ?? options.bearerToken ?? null;
2948
2900
  const headerToken = extractBearerToken(options.headers, explicitToken);
2949
2901
  if (headerToken)
@@ -2952,12 +2904,6 @@ function resolveAuthToken(options = {}) {
2952
2904
  return null;
2953
2905
  return getAccessToken();
2954
2906
  }
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
2907
  function appendQueryParams(url, params) {
2962
2908
  if (!params || Object.keys(params).length === 0)
2963
2909
  return url;
@@ -2980,43 +2926,32 @@ function appendQueryParams(url, params) {
2980
2926
  return `${url}${separator}${search}`;
2981
2927
  }
2982
2928
  }
2929
+ function withWebSocketTokenQuery(url, options) {
2930
+ const token = resolveWebSocketToken(options);
2931
+ const params = { ...options.query ?? {} };
2932
+ if (token) {
2933
+ params[options.tokenQueryParam ?? options.bearerQueryParam ?? "token"] = token;
2934
+ }
2935
+ return appendQueryParams(url, params);
2936
+ }
2983
2937
  function normalizeProtocols(protocols) {
2984
2938
  if (!protocols)
2985
2939
  return void 0;
2986
2940
  return Array.isArray(protocols) ? protocols.filter(Boolean) : [protocols];
2987
2941
  }
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
- };
2942
+ function buildWsUrl(path = "/", options = {}) {
2943
+ try {
2944
+ const origin2 = new URL(apiOrigin);
2945
+ origin2.protocol = origin2.protocol === "https:" ? "wss:" : "ws:";
2946
+ if (!path.startsWith("/"))
2947
+ path = "/" + path;
2948
+ origin2.pathname = path;
2949
+ return withWebSocketTokenQuery(origin2.toString(), options);
2950
+ } catch (e) {
2951
+ const proto = apiOrigin.startsWith("https") ? "wss" : "ws";
2952
+ const host = apiOrigin.replace(/^https?:\/\//, "").replace(/\/$/, "");
2953
+ return withWebSocketTokenQuery(`${proto}://${host}${path.startsWith("/") ? path : "/" + path}`, options);
2998
2954
  }
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
2955
  }
3021
2956
  async function encodeFfurl(settings) {
3022
2957
  const { ffurl } = await fetchApi.postJson("/auth/protocols/ffurl/encode", settings);
@@ -3027,30 +2962,18 @@ async function decodeFfurl(ffurl) {
3027
2962
  return settings;
3028
2963
  }
3029
2964
  function subscribeToWebsocket(url, onMessage, options = {}) {
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);
2965
+ const tokenTransport = options.tokenTransport ?? options.bearerStrategy ?? "query";
3038
2966
  const protocols = normalizeProtocols(options.protocols) ?? [];
3039
2967
  if (options.headers && Object.keys(options.headers).length > 0) {
3040
2968
  console.warn(
3041
2969
  "WebSocket connections in browsers do not support custom HTTP headers. shared-ui will derive bearer auth from Authorization and forward it using query string or subprotocol."
3042
2970
  );
3043
2971
  }
3044
- if (bearerToken) {
3045
- if (bearerStrategy === "protocol") {
3046
- protocols.push("bearer", bearerToken);
3047
- } else {
3048
- resolvedUrl = withAccessTokenQuery(resolvedUrl, {
3049
- token: bearerToken,
3050
- tokenQueryParam: options.tokenQueryParam ?? options.bearerQueryParam,
3051
- includeStoredToken: false
3052
- });
3053
- }
2972
+ let resolvedUrl = tokenTransport === "query" ? withWebSocketTokenQuery(url, options) : appendQueryParams(url, options.query);
2973
+ if (tokenTransport === "protocol") {
2974
+ const token = resolveWebSocketToken(options);
2975
+ if (token)
2976
+ protocols.push("bearer", token);
3054
2977
  }
3055
2978
  const socket = protocols.length > 0 ? new WebSocket(resolvedUrl, protocols) : new WebSocket(resolvedUrl);
3056
2979
  socket.onmessage = (event) => {