@viasoftbr/shared-ui 0.0.6 → 0.0.7-2

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.
@@ -25628,17 +25628,6 @@ function buildWsUrl(path = "/") {
25628
25628
  return `${proto}://${host}${path.startsWith("/") ? path : "/" + path}`;
25629
25629
  }
25630
25630
  }
25631
- function extractBearerToken(headers, explicitToken) {
25632
- if (explicitToken)
25633
- return explicitToken;
25634
- if (!headers)
25635
- return null;
25636
- const authHeader = headers.Authorization || headers.authorization;
25637
- if (!authHeader)
25638
- return null;
25639
- const match = authHeader.match(/^Bearer\s+(.+)$/i);
25640
- return match ? match[1] : null;
25641
- }
25642
25631
  function appendWsQueryParam(url, key, value) {
25643
25632
  try {
25644
25633
  const parsed = new URL(
@@ -25661,24 +25650,26 @@ async function decodeFfurl(ffurl) {
25661
25650
  const { settings } = await fetchApi.postJson("/auth/protocols/ffurl/decode", { ffurl });
25662
25651
  return settings;
25663
25652
  }
25653
+ function resolveWsBearerToken(options) {
25654
+ if (options.bearerToken)
25655
+ return options.bearerToken;
25656
+ if (options.autoAuth)
25657
+ return getAccessToken();
25658
+ return null;
25659
+ }
25664
25660
  function subscribeToWebsocket(url, onMessage, options = {}) {
25665
- const bearerToken = extractBearerToken(options.headers, options.bearerToken);
25666
- const bearerStrategy = options.bearerStrategy ?? "query";
25667
- let resolvedUrl = url;
25661
+ const token = resolveWsBearerToken(options);
25662
+ const strategy = options.bearerStrategy ?? "query";
25668
25663
  const protocols = normalizeProtocols(options.protocols) ?? [];
25669
- if (options.headers && Object.keys(options.headers).length > 0) {
25670
- console.warn(
25671
- "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."
25672
- );
25673
- }
25674
- if (bearerToken) {
25675
- if (bearerStrategy === "protocol") {
25676
- protocols.push("bearer", bearerToken);
25664
+ let resolvedUrl = url;
25665
+ if (token) {
25666
+ if (strategy === "protocol") {
25667
+ protocols.push("bearer", token);
25677
25668
  } else {
25678
25669
  resolvedUrl = appendWsQueryParam(
25679
25670
  url,
25680
25671
  options.bearerQueryParam ?? "access_token",
25681
- bearerToken
25672
+ token
25682
25673
  );
25683
25674
  }
25684
25675
  }
@@ -25688,7 +25679,7 @@ function subscribeToWebsocket(url, onMessage, options = {}) {
25688
25679
  const data = JSON.parse(event.data);
25689
25680
  onMessage(data);
25690
25681
  } catch (e10) {
25691
- console.error("Parse error", e10);
25682
+ console.error("WebSocket parse error", e10);
25692
25683
  }
25693
25684
  };
25694
25685
  socket.onerror = (err) => console.error("WebSocket error", err);
@@ -25571,17 +25571,6 @@ function buildWsUrl(path = "/") {
25571
25571
  return `${proto}://${host}${path.startsWith("/") ? path : "/" + path}`;
25572
25572
  }
25573
25573
  }
25574
- function extractBearerToken(headers, explicitToken) {
25575
- if (explicitToken)
25576
- return explicitToken;
25577
- if (!headers)
25578
- return null;
25579
- const authHeader = headers.Authorization || headers.authorization;
25580
- if (!authHeader)
25581
- return null;
25582
- const match = authHeader.match(/^Bearer\s+(.+)$/i);
25583
- return match ? match[1] : null;
25584
- }
25585
25574
  function appendWsQueryParam(url, key, value) {
25586
25575
  try {
25587
25576
  const parsed = new URL(
@@ -25604,24 +25593,26 @@ async function decodeFfurl(ffurl) {
25604
25593
  const { settings } = await fetchApi.postJson("/auth/protocols/ffurl/decode", { ffurl });
25605
25594
  return settings;
25606
25595
  }
25596
+ function resolveWsBearerToken(options) {
25597
+ if (options.bearerToken)
25598
+ return options.bearerToken;
25599
+ if (options.autoAuth)
25600
+ return getAccessToken();
25601
+ return null;
25602
+ }
25607
25603
  function subscribeToWebsocket(url, onMessage, options = {}) {
25608
- const bearerToken = extractBearerToken(options.headers, options.bearerToken);
25609
- const bearerStrategy = options.bearerStrategy ?? "query";
25610
- let resolvedUrl = url;
25604
+ const token = resolveWsBearerToken(options);
25605
+ const strategy = options.bearerStrategy ?? "query";
25611
25606
  const protocols = normalizeProtocols(options.protocols) ?? [];
25612
- if (options.headers && Object.keys(options.headers).length > 0) {
25613
- console.warn(
25614
- "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."
25615
- );
25616
- }
25617
- if (bearerToken) {
25618
- if (bearerStrategy === "protocol") {
25619
- protocols.push("bearer", bearerToken);
25607
+ let resolvedUrl = url;
25608
+ if (token) {
25609
+ if (strategy === "protocol") {
25610
+ protocols.push("bearer", token);
25620
25611
  } else {
25621
25612
  resolvedUrl = appendWsQueryParam(
25622
25613
  url,
25623
25614
  options.bearerQueryParam ?? "access_token",
25624
- bearerToken
25615
+ token
25625
25616
  );
25626
25617
  }
25627
25618
  }
@@ -25631,7 +25622,7 @@ function subscribeToWebsocket(url, onMessage, options = {}) {
25631
25622
  const data = JSON.parse(event.data);
25632
25623
  onMessage(data);
25633
25624
  } catch (e10) {
25634
- console.error("Parse error", e10);
25625
+ console.error("WebSocket parse error", e10);
25635
25626
  }
25636
25627
  };
25637
25628
  socket.onerror = (err) => console.error("WebSocket error", err);
package/dist/index.cjs CHANGED
@@ -3040,17 +3040,6 @@ function buildWsUrl(path = "/") {
3040
3040
  return `${proto}://${host}${path.startsWith("/") ? path : "/" + path}`;
3041
3041
  }
3042
3042
  }
3043
- function extractBearerToken(headers, explicitToken) {
3044
- if (explicitToken)
3045
- return explicitToken;
3046
- if (!headers)
3047
- return null;
3048
- const authHeader = headers.Authorization || headers.authorization;
3049
- if (!authHeader)
3050
- return null;
3051
- const match = authHeader.match(/^Bearer\s+(.+)$/i);
3052
- return match ? match[1] : null;
3053
- }
3054
3043
  function appendWsQueryParam(url, key, value) {
3055
3044
  try {
3056
3045
  const parsed = new URL(
@@ -3077,24 +3066,26 @@ async function decodeFfurl(ffurl) {
3077
3066
  const { settings } = await fetchApi.postJson("/auth/protocols/ffurl/decode", { ffurl });
3078
3067
  return settings;
3079
3068
  }
3069
+ function resolveWsBearerToken(options) {
3070
+ if (options.bearerToken)
3071
+ return options.bearerToken;
3072
+ if (options.autoAuth)
3073
+ return getAccessToken();
3074
+ return null;
3075
+ }
3080
3076
  function subscribeToWebsocket(url, onMessage, options = {}) {
3081
- const bearerToken = extractBearerToken(options.headers, options.bearerToken);
3082
- const bearerStrategy = options.bearerStrategy ?? "query";
3083
- let resolvedUrl = url;
3077
+ const token = resolveWsBearerToken(options);
3078
+ const strategy = options.bearerStrategy ?? "query";
3084
3079
  const protocols = normalizeProtocols(options.protocols) ?? [];
3085
- if (options.headers && Object.keys(options.headers).length > 0) {
3086
- console.warn(
3087
- "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."
3088
- );
3089
- }
3090
- if (bearerToken) {
3091
- if (bearerStrategy === "protocol") {
3092
- protocols.push("bearer", bearerToken);
3080
+ let resolvedUrl = url;
3081
+ if (token) {
3082
+ if (strategy === "protocol") {
3083
+ protocols.push("bearer", token);
3093
3084
  } else {
3094
3085
  resolvedUrl = appendWsQueryParam(
3095
3086
  url,
3096
3087
  options.bearerQueryParam ?? "access_token",
3097
- bearerToken
3088
+ token
3098
3089
  );
3099
3090
  }
3100
3091
  }
@@ -3104,7 +3095,7 @@ function subscribeToWebsocket(url, onMessage, options = {}) {
3104
3095
  const data = JSON.parse(event.data);
3105
3096
  onMessage(data);
3106
3097
  } catch (e10) {
3107
- console.error("Parse error", e10);
3098
+ console.error("WebSocket parse error", e10);
3108
3099
  }
3109
3100
  };
3110
3101
  socket.onerror = (err) => console.error("WebSocket error", err);
package/dist/index.js CHANGED
@@ -2953,17 +2953,6 @@ function buildWsUrl(path = "/") {
2953
2953
  return `${proto}://${host}${path.startsWith("/") ? path : "/" + path}`;
2954
2954
  }
2955
2955
  }
2956
- function extractBearerToken(headers, explicitToken) {
2957
- if (explicitToken)
2958
- return explicitToken;
2959
- if (!headers)
2960
- return null;
2961
- const authHeader = headers.Authorization || headers.authorization;
2962
- if (!authHeader)
2963
- return null;
2964
- const match = authHeader.match(/^Bearer\s+(.+)$/i);
2965
- return match ? match[1] : null;
2966
- }
2967
2956
  function appendWsQueryParam(url, key, value) {
2968
2957
  try {
2969
2958
  const parsed = new URL(
@@ -2990,24 +2979,26 @@ async function decodeFfurl(ffurl) {
2990
2979
  const { settings } = await fetchApi.postJson("/auth/protocols/ffurl/decode", { ffurl });
2991
2980
  return settings;
2992
2981
  }
2982
+ function resolveWsBearerToken(options) {
2983
+ if (options.bearerToken)
2984
+ return options.bearerToken;
2985
+ if (options.autoAuth)
2986
+ return getAccessToken();
2987
+ return null;
2988
+ }
2993
2989
  function subscribeToWebsocket(url, onMessage, options = {}) {
2994
- const bearerToken = extractBearerToken(options.headers, options.bearerToken);
2995
- const bearerStrategy = options.bearerStrategy ?? "query";
2996
- let resolvedUrl = url;
2990
+ const token = resolveWsBearerToken(options);
2991
+ const strategy = options.bearerStrategy ?? "query";
2997
2992
  const protocols = normalizeProtocols(options.protocols) ?? [];
2998
- if (options.headers && Object.keys(options.headers).length > 0) {
2999
- console.warn(
3000
- "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."
3001
- );
3002
- }
3003
- if (bearerToken) {
3004
- if (bearerStrategy === "protocol") {
3005
- protocols.push("bearer", bearerToken);
2993
+ let resolvedUrl = url;
2994
+ if (token) {
2995
+ if (strategy === "protocol") {
2996
+ protocols.push("bearer", token);
3006
2997
  } else {
3007
2998
  resolvedUrl = appendWsQueryParam(
3008
2999
  url,
3009
3000
  options.bearerQueryParam ?? "access_token",
3010
- bearerToken
3001
+ token
3011
3002
  );
3012
3003
  }
3013
3004
  }
@@ -3017,7 +3008,7 @@ function subscribeToWebsocket(url, onMessage, options = {}) {
3017
3008
  const data = JSON.parse(event.data);
3018
3009
  onMessage(data);
3019
3010
  } catch (e10) {
3020
- console.error("Parse error", e10);
3011
+ console.error("WebSocket parse error", e10);
3021
3012
  }
3022
3013
  };
3023
3014
  socket.onerror = (err) => console.error("WebSocket error", err);
@@ -20,4 +20,27 @@ export declare const fetchApi: {
20
20
  export declare function buildWsUrl(path?: string): string;
21
21
  export declare function encodeFfurl(settings: ProtocolSettings): Promise<string>;
22
22
  export declare function decodeFfurl(ffurl: string): Promise<ProtocolSettings>;
23
+ /**
24
+ * Abre uma conexão WebSocket com suporte a autenticação Bearer.
25
+ *
26
+ * @param url - URL WebSocket de destino (ws:// ou wss://)
27
+ * @param onMessage - Callback chamado com o payload já parseado (JSON)
28
+ * @param options - Opções de autenticação e sub-protocolos
29
+ * @returns - Função de cleanup que fecha o socket
30
+ *
31
+ * @example
32
+ * // Token explícito
33
+ * const close = subscribeToWebsocket(url, handler, { bearerToken: myJwt });
34
+ *
35
+ * @example
36
+ * // Token automático (usa getAccessToken do storage)
37
+ * const close = subscribeToWebsocket(url, handler, { autoAuth: true });
38
+ *
39
+ * @example
40
+ * // Token via sub-protocolo Sec-WebSocket-Protocol
41
+ * const close = subscribeToWebsocket(url, handler, {
42
+ * bearerToken: myJwt,
43
+ * bearerStrategy: 'protocol',
44
+ * });
45
+ */
23
46
  export declare function subscribeToWebsocket(url: string, onMessage: (data: any) => void, options?: WebSocketSubscribeOptions): () => void;
package/dist/services.cjs CHANGED
@@ -2887,17 +2887,6 @@ function buildWsUrl(path = "/") {
2887
2887
  return `${proto}://${host}${path.startsWith("/") ? path : "/" + path}`;
2888
2888
  }
2889
2889
  }
2890
- function extractBearerToken(headers, explicitToken) {
2891
- if (explicitToken)
2892
- return explicitToken;
2893
- if (!headers)
2894
- return null;
2895
- const authHeader = headers.Authorization || headers.authorization;
2896
- if (!authHeader)
2897
- return null;
2898
- const match = authHeader.match(/^Bearer\s+(.+)$/i);
2899
- return match ? match[1] : null;
2900
- }
2901
2890
  function appendWsQueryParam(url, key, value) {
2902
2891
  try {
2903
2892
  const parsed = new URL(
@@ -2924,24 +2913,26 @@ async function decodeFfurl(ffurl) {
2924
2913
  const { settings } = await fetchApi.postJson("/auth/protocols/ffurl/decode", { ffurl });
2925
2914
  return settings;
2926
2915
  }
2916
+ function resolveWsBearerToken(options) {
2917
+ if (options.bearerToken)
2918
+ return options.bearerToken;
2919
+ if (options.autoAuth)
2920
+ return getAccessToken();
2921
+ return null;
2922
+ }
2927
2923
  function subscribeToWebsocket(url, onMessage, options = {}) {
2928
- const bearerToken = extractBearerToken(options.headers, options.bearerToken);
2929
- const bearerStrategy = options.bearerStrategy ?? "query";
2930
- let resolvedUrl = url;
2924
+ const token = resolveWsBearerToken(options);
2925
+ const strategy = options.bearerStrategy ?? "query";
2931
2926
  const protocols = normalizeProtocols(options.protocols) ?? [];
2932
- if (options.headers && Object.keys(options.headers).length > 0) {
2933
- console.warn(
2934
- "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."
2935
- );
2936
- }
2937
- if (bearerToken) {
2938
- if (bearerStrategy === "protocol") {
2939
- protocols.push("bearer", bearerToken);
2927
+ let resolvedUrl = url;
2928
+ if (token) {
2929
+ if (strategy === "protocol") {
2930
+ protocols.push("bearer", token);
2940
2931
  } else {
2941
2932
  resolvedUrl = appendWsQueryParam(
2942
2933
  url,
2943
2934
  options.bearerQueryParam ?? "access_token",
2944
- bearerToken
2935
+ token
2945
2936
  );
2946
2937
  }
2947
2938
  }
@@ -2951,7 +2942,7 @@ function subscribeToWebsocket(url, onMessage, options = {}) {
2951
2942
  const data = JSON.parse(event.data);
2952
2943
  onMessage(data);
2953
2944
  } catch (e) {
2954
- console.error("Parse error", e);
2945
+ console.error("WebSocket parse error", e);
2955
2946
  }
2956
2947
  };
2957
2948
  socket.onerror = (err) => console.error("WebSocket error", err);
package/dist/services.js CHANGED
@@ -2847,17 +2847,6 @@ function buildWsUrl(path = "/") {
2847
2847
  return `${proto}://${host}${path.startsWith("/") ? path : "/" + path}`;
2848
2848
  }
2849
2849
  }
2850
- function extractBearerToken(headers, explicitToken) {
2851
- if (explicitToken)
2852
- return explicitToken;
2853
- if (!headers)
2854
- return null;
2855
- const authHeader = headers.Authorization || headers.authorization;
2856
- if (!authHeader)
2857
- return null;
2858
- const match = authHeader.match(/^Bearer\s+(.+)$/i);
2859
- return match ? match[1] : null;
2860
- }
2861
2850
  function appendWsQueryParam(url, key, value) {
2862
2851
  try {
2863
2852
  const parsed = new URL(
@@ -2884,24 +2873,26 @@ async function decodeFfurl(ffurl) {
2884
2873
  const { settings } = await fetchApi.postJson("/auth/protocols/ffurl/decode", { ffurl });
2885
2874
  return settings;
2886
2875
  }
2876
+ function resolveWsBearerToken(options) {
2877
+ if (options.bearerToken)
2878
+ return options.bearerToken;
2879
+ if (options.autoAuth)
2880
+ return getAccessToken();
2881
+ return null;
2882
+ }
2887
2883
  function subscribeToWebsocket(url, onMessage, options = {}) {
2888
- const bearerToken = extractBearerToken(options.headers, options.bearerToken);
2889
- const bearerStrategy = options.bearerStrategy ?? "query";
2890
- let resolvedUrl = url;
2884
+ const token = resolveWsBearerToken(options);
2885
+ const strategy = options.bearerStrategy ?? "query";
2891
2886
  const protocols = normalizeProtocols(options.protocols) ?? [];
2892
- if (options.headers && Object.keys(options.headers).length > 0) {
2893
- console.warn(
2894
- "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."
2895
- );
2896
- }
2897
- if (bearerToken) {
2898
- if (bearerStrategy === "protocol") {
2899
- protocols.push("bearer", bearerToken);
2887
+ let resolvedUrl = url;
2888
+ if (token) {
2889
+ if (strategy === "protocol") {
2890
+ protocols.push("bearer", token);
2900
2891
  } else {
2901
2892
  resolvedUrl = appendWsQueryParam(
2902
2893
  url,
2903
2894
  options.bearerQueryParam ?? "access_token",
2904
- bearerToken
2895
+ token
2905
2896
  );
2906
2897
  }
2907
2898
  }
@@ -2911,7 +2902,7 @@ function subscribeToWebsocket(url, onMessage, options = {}) {
2911
2902
  const data = JSON.parse(event.data);
2912
2903
  onMessage(data);
2913
2904
  } catch (e) {
2914
- console.error("Parse error", e);
2905
+ console.error("WebSocket parse error", e);
2915
2906
  }
2916
2907
  };
2917
2908
  socket.onerror = (err) => console.error("WebSocket error", err);
@@ -26,9 +26,30 @@ export interface WebSocketData {
26
26
  system: SystemInfo;
27
27
  }
28
28
  export interface WebSocketSubscribeOptions {
29
- headers?: Record<string, string>;
30
- protocols?: string | string[];
29
+ /**
30
+ * Token JWT Bearer explícito. Se fornecido, tem prioridade sobre autoAuth.
31
+ */
31
32
  bearerToken?: string | null;
33
+ /**
34
+ * Quando `true`, usa automaticamente o accessToken armazenado (localStorage)
35
+ * caso `bearerToken` não tenha sido fornecido.
36
+ * @default false
37
+ */
38
+ autoAuth?: boolean;
39
+ /**
40
+ * Define como o token é enviado ao servidor.
41
+ * - `'query'` → parâmetro de query string (padrão, ex.: `?access_token=...`)
42
+ * - `'protocol'` → sub-protocolo WebSocket (`Sec-WebSocket-Protocol`)
43
+ * @default 'query'
44
+ */
32
45
  bearerStrategy?: 'query' | 'protocol';
46
+ /**
47
+ * Nome do parâmetro de query usado na estratégia `'query'`.
48
+ * @default 'access_token'
49
+ */
33
50
  bearerQueryParam?: string;
51
+ /**
52
+ * Sub-protocolos WebSocket adicionais.
53
+ */
54
+ protocols?: string | string[];
34
55
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@viasoftbr/shared-ui",
3
- "version": "0.0.6",
3
+ "version": "0.0.7-2",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/types/index.d.ts",
6
6
  "exports": {