integrate-sdk 0.5.2 → 0.5.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.
package/dist/react.js CHANGED
@@ -858,16 +858,45 @@ var require_react = __commonJS((exports, module) => {
858
858
  });
859
859
 
860
860
  // src/react/hooks.ts
861
- var import_react = __toESM(require_react(), 1);
861
+ var React = __toESM(require_react(), 1);
862
+ function getSafeFallback() {
863
+ return {
864
+ tokens: {},
865
+ headers: {},
866
+ isLoading: false,
867
+ fetch: globalThis.fetch?.bind(globalThis) || (async () => new Response),
868
+ mergeHeaders: (existingHeaders) => new Headers(existingHeaders)
869
+ };
870
+ }
871
+ function isReactHooksAvailable() {
872
+ if (!React || typeof React !== "object") {
873
+ return false;
874
+ }
875
+ if (!React.useState || !React.useEffect || !React.useMemo || !React.useCallback) {
876
+ return false;
877
+ }
878
+ if (typeof window === "undefined") {
879
+ return false;
880
+ }
881
+ if (typeof document === "undefined") {
882
+ return false;
883
+ }
884
+ return true;
885
+ }
862
886
  function useIntegrateTokens(client) {
863
- const [tokens, setTokens] = import_react.useState({});
864
- const [isLoading, setIsLoading] = import_react.useState(true);
865
- import_react.useEffect(() => {
866
- if (!client) {
867
- console.error("[useIntegrateTokens] Client parameter is required. " + "Pass your MCP client instance to this hook.");
868
- setIsLoading(false);
869
- return;
870
- }
887
+ if (!isReactHooksAvailable()) {
888
+ console.warn("[useIntegrateTokens] React hooks are not available. " + "This can happen during SSR, before React initialization, or in Suspense boundaries. " + "Returning safe fallback values.");
889
+ return getSafeFallback();
890
+ }
891
+ if (!client) {
892
+ return {
893
+ ...getSafeFallback(),
894
+ isLoading: true
895
+ };
896
+ }
897
+ const [tokens, setTokens] = React.useState({});
898
+ const [isLoading, setIsLoading] = React.useState(true);
899
+ React.useEffect(() => {
871
900
  try {
872
901
  const updateTokens = () => {
873
902
  try {
@@ -903,7 +932,7 @@ function useIntegrateTokens(client) {
903
932
  return;
904
933
  }
905
934
  }, [client]);
906
- const headers = import_react.useMemo(() => {
935
+ const headers = React.useMemo(() => {
907
936
  if (Object.keys(tokens).length === 0) {
908
937
  return {};
909
938
  }
@@ -911,7 +940,7 @@ function useIntegrateTokens(client) {
911
940
  "x-integrate-tokens": JSON.stringify(tokens)
912
941
  };
913
942
  }, [tokens]);
914
- const fetchWithHeaders = import_react.useCallback(async (input, init) => {
943
+ const fetchWithHeaders = React.useCallback(async (input, init) => {
915
944
  const mergedHeaders = new Headers(init?.headers);
916
945
  if (headers["x-integrate-tokens"]) {
917
946
  mergedHeaders.set("x-integrate-tokens", headers["x-integrate-tokens"]);
@@ -921,7 +950,7 @@ function useIntegrateTokens(client) {
921
950
  headers: mergedHeaders
922
951
  });
923
952
  }, [headers]);
924
- const mergeHeaders = import_react.useCallback((existingHeaders) => {
953
+ const mergeHeaders = React.useCallback((existingHeaders) => {
925
954
  const merged = new Headers(existingHeaders);
926
955
  if (headers["x-integrate-tokens"]) {
927
956
  merged.set("x-integrate-tokens", headers["x-integrate-tokens"]);
@@ -39,8 +39,11 @@ export interface UseIntegrateTokensResult {
39
39
  * Automatically listens for authentication events and updates when tokens change.
40
40
  * Returns tokens and formatted headers ready to pass to API requests.
41
41
  *
42
- * @param client - MCP client instance created with createMCPClient()
43
- * @returns Object with tokens, headers, and loading state
42
+ * **Note:** This hook must be called inside a React component. It will return safe
43
+ * fallback values during SSR or if the client is not ready.
44
+ *
45
+ * @param client - MCP client instance created with createMCPClient() (optional)
46
+ * @returns Object with tokens, headers, loading state, fetch function, and mergeHeaders helper
44
47
  *
45
48
  * @example
46
49
  * ```tsx
@@ -53,11 +56,11 @@ export interface UseIntegrateTokensResult {
53
56
  * });
54
57
  *
55
58
  * function ChatComponent() {
56
- * const { tokens, headers, isLoading } = useIntegrateTokens(client);
59
+ * const { fetch: fetchWithTokens, isLoading } = useIntegrateTokens(client);
57
60
  *
58
61
  * const chat = useChat({
59
62
  * api: '/api/chat',
60
- * headers, // Automatically includes x-integrate-tokens
63
+ * fetch: fetchWithTokens, // Tokens automatically included
61
64
  * });
62
65
  *
63
66
  * return <div>Chat UI here</div>;
@@ -66,21 +69,18 @@ export interface UseIntegrateTokensResult {
66
69
  *
67
70
  * @example
68
71
  * ```tsx
69
- * // Manual fetch with tokens
72
+ * // With mergeHeaders helper
70
73
  * import { createMCPClient } from 'integrate-sdk';
71
74
  *
72
75
  * const client = createMCPClient({ plugins: [...] });
73
76
  *
74
77
  * function MyComponent() {
75
- * const { headers } = useIntegrateTokens(client);
78
+ * const { mergeHeaders } = useIntegrateTokens(client);
76
79
  *
77
80
  * const fetchData = async () => {
78
81
  * const response = await fetch('/api/data', {
79
82
  * method: 'POST',
80
- * headers: {
81
- * 'Content-Type': 'application/json',
82
- * ...headers, // Includes x-integrate-tokens
83
- * },
83
+ * headers: mergeHeaders({ 'Content-Type': 'application/json' }),
84
84
  * body: JSON.stringify({ query: 'example' }),
85
85
  * });
86
86
  * return response.json();
@@ -90,5 +90,5 @@ export interface UseIntegrateTokensResult {
90
90
  * }
91
91
  * ```
92
92
  */
93
- export declare function useIntegrateTokens(client: MCPClient<any>): UseIntegrateTokensResult;
93
+ export declare function useIntegrateTokens(client?: MCPClient<any> | null): UseIntegrateTokensResult;
94
94
  //# sourceMappingURL=hooks.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../../src/react/hooks.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE9C;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAE/B;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEhC;;OAEG;IACH,SAAS,EAAE,OAAO,CAAC;IAEnB;;;OAGG;IACH,KAAK,EAAE,CAAC,KAAK,EAAE,WAAW,GAAG,GAAG,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;IAE3E;;;OAGG;IACH,YAAY,EAAE,CAAC,eAAe,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC;CAC1D;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,SAAS,CAAC,GAAG,CAAC,GACrB,wBAAwB,CA+G1B"}
1
+ {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../../src/react/hooks.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE9C;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAE/B;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEhC;;OAEG;IACH,SAAS,EAAE,OAAO,CAAC;IAEnB;;;OAGG;IACH,KAAK,EAAE,CAAC,KAAK,EAAE,WAAW,GAAG,GAAG,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;IAE3E;;;OAGG;IACH,YAAY,EAAE,CAAC,eAAe,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC;CAC1D;AA4CD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,CAAC,EAAE,SAAS,CAAC,GAAG,CAAC,GAAG,IAAI,GAC7B,wBAAwB,CA0H1B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "integrate-sdk",
3
- "version": "0.5.2",
3
+ "version": "0.5.4",
4
4
  "description": "Type-safe TypeScript SDK for MCP Client with plugin-based OAuth provider configuration",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",