@unifold/react-provider 0.1.1

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/README.md ADDED
@@ -0,0 +1,108 @@
1
+ # @unifold/react-provider
2
+
3
+ Minimal React provider for Unifold SDKs. This package provides only the essential `publishableKey` context and QueryClient setup that is shared across all Unifold React-based SDKs.
4
+
5
+ ## Purpose
6
+
7
+ This package is designed to be the **universal, minimal provider** for:
8
+ - `@unifold/connect-react` - Full UI SDK with modal components
9
+ - `@unifold/headless-react` - Headless SDK with hooks only (coming soon)
10
+ - Any future React-based Unifold SDKs
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ npm install @unifold/react-provider
16
+ # or
17
+ pnpm add @unifold/react-provider
18
+ ```
19
+
20
+ > **Note**: This package is typically installed automatically as a dependency of `@unifold/connect-react` or `@unifold/headless-react`. You don't need to install it directly unless you're building a custom SDK.
21
+
22
+ ## Usage
23
+
24
+ ### Basic Setup
25
+
26
+ ```tsx
27
+ import { UnifoldProvider } from '@unifold/react-provider';
28
+
29
+ function App() {
30
+ return (
31
+ <UnifoldProvider publishableKey="pk_test_...">
32
+ <YourApp />
33
+ </UnifoldProvider>
34
+ );
35
+ }
36
+ ```
37
+
38
+ ### Using the Context
39
+
40
+ ```tsx
41
+ import { useUnifold } from '@unifold/react-provider';
42
+
43
+ function MyComponent() {
44
+ const { publishableKey } = useUnifold();
45
+
46
+ return <div>Connected with key: {publishableKey}</div>;
47
+ }
48
+ ```
49
+
50
+ ## API
51
+
52
+ ### UnifoldProvider
53
+
54
+ The main provider component that wraps your application.
55
+
56
+ **Props:**
57
+
58
+ - `publishableKey` (required): Your Unifold API publishable key
59
+ - `children` (required): Your React components
60
+
61
+ ### useUnifold
62
+
63
+ Hook to access the Unifold context.
64
+
65
+ **Returns:**
66
+
67
+ - `publishableKey`: Your API key
68
+
69
+ ## Features
70
+
71
+ - ✅ **QueryClient Setup**: Automatically configures React Query for optimal performance
72
+ - ✅ **API Key Validation**: Validates publishable keys on mount
73
+ - ✅ **SSR Support**: Safe to use with server-side rendering
74
+ - ✅ **TypeScript**: Full type definitions included
75
+ - ✅ **Ultra-Lightweight**: Minimal bundle size (~2 KB)
76
+ - ✅ **Zero Configuration**: Only requires `publishableKey`
77
+
78
+ ## Design Philosophy
79
+
80
+ This package intentionally stays minimal and only handles:
81
+ 1. `publishableKey` management
82
+ 2. React Query setup
83
+ 3. SSR-safe context
84
+
85
+ All SDK-specific configuration (apiUrl, defaultConfig, etc.) is handled by the higher-level packages (`@unifold/connect-react`, `@unifold/headless-react`) that wrap this provider.
86
+
87
+ ## Architecture
88
+
89
+ This package sits at the base of the Unifold React ecosystem:
90
+
91
+ ```
92
+ @unifold/react-provider (this package)
93
+ └─ publishableKey + QueryClient only
94
+
95
+ @unifold/connect-react
96
+ ├─ Wraps: @unifold/react-provider
97
+ ├─ Adds: apiUrl, appName, defaultConfig
98
+ └─ Provides: UI components, modal flows
99
+
100
+ @unifold/headless-react (coming soon)
101
+ ├─ Wraps: @unifold/react-provider
102
+ ├─ Adds: apiUrl, custom config
103
+ └─ Provides: Headless hooks, custom UI
104
+ ```
105
+
106
+ ## License
107
+
108
+ MIT
@@ -0,0 +1,16 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import React from 'react';
3
+
4
+ interface UnifoldConfig {
5
+ publishableKey: string;
6
+ }
7
+ interface UnifoldProviderProps {
8
+ children: React.ReactNode;
9
+ publishableKey: string;
10
+ }
11
+ declare function UnifoldProvider({ children, publishableKey, }: UnifoldProviderProps): react_jsx_runtime.JSX.Element;
12
+ declare function useUnifold(): {
13
+ publishableKey: string;
14
+ };
15
+
16
+ export { type UnifoldConfig, UnifoldProvider, useUnifold };
@@ -0,0 +1,16 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import React from 'react';
3
+
4
+ interface UnifoldConfig {
5
+ publishableKey: string;
6
+ }
7
+ interface UnifoldProviderProps {
8
+ children: React.ReactNode;
9
+ publishableKey: string;
10
+ }
11
+ declare function UnifoldProvider({ children, publishableKey, }: UnifoldProviderProps): react_jsx_runtime.JSX.Element;
12
+ declare function useUnifold(): {
13
+ publishableKey: string;
14
+ };
15
+
16
+ export { type UnifoldConfig, UnifoldProvider, useUnifold };
package/dist/index.js ADDED
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ UnifoldProvider: () => UnifoldProvider,
24
+ useUnifold: () => useUnifold
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+
28
+ // src/provider.tsx
29
+ var import_react = require("react");
30
+ var import_react_query = require("@tanstack/react-query");
31
+ var import_jsx_runtime = require("react/jsx-runtime");
32
+ var UnifoldContext = (0, import_react.createContext)(null);
33
+ var createQueryClient = () => new import_react_query.QueryClient({
34
+ defaultOptions: {
35
+ queries: {
36
+ staleTime: 6e4,
37
+ retry: 1
38
+ }
39
+ }
40
+ });
41
+ function UnifoldProvider({
42
+ children,
43
+ publishableKey
44
+ }) {
45
+ const [queryClient] = (0, import_react.useState)(() => createQueryClient());
46
+ (0, import_react.useEffect)(() => {
47
+ if (!publishableKey || publishableKey.trim() === "") {
48
+ throw new Error(
49
+ "Unifold: publishableKey is required. Please provide a valid publishable key."
50
+ );
51
+ }
52
+ if (!publishableKey.startsWith("pk_test_") && !publishableKey.startsWith("pk_live_")) {
53
+ console.warn(
54
+ 'Unifold: publishableKey should start with "pk_test_" or "pk_live_". Please ensure you are using a valid publishable key.'
55
+ );
56
+ }
57
+ }, [publishableKey]);
58
+ const prevPublishableKeyRef = (0, import_react.useRef)(publishableKey);
59
+ (0, import_react.useEffect)(() => {
60
+ if (prevPublishableKeyRef.current !== publishableKey) {
61
+ console.log(
62
+ "Unifold: Reinitializing with new publishable key:",
63
+ publishableKey
64
+ );
65
+ prevPublishableKeyRef.current = publishableKey;
66
+ }
67
+ }, [publishableKey]);
68
+ const contextValue = (0, import_react.useMemo)(
69
+ () => ({
70
+ publishableKey
71
+ }),
72
+ [publishableKey]
73
+ );
74
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_query.QueryClientProvider, { client: queryClient, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(UnifoldContext.Provider, { value: contextValue, children }) });
75
+ }
76
+ function useUnifold() {
77
+ const context = (0, import_react.useContext)(UnifoldContext);
78
+ if (typeof window === "undefined") {
79
+ return {
80
+ publishableKey: ""
81
+ };
82
+ }
83
+ if (!context) {
84
+ throw new Error("useUnifold must be used within UnifoldProvider");
85
+ }
86
+ return {
87
+ publishableKey: context.publishableKey
88
+ };
89
+ }
90
+ // Annotate the CommonJS export names for ESM import in node:
91
+ 0 && (module.exports = {
92
+ UnifoldProvider,
93
+ useUnifold
94
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,73 @@
1
+ // src/provider.tsx
2
+ import {
3
+ createContext,
4
+ useContext,
5
+ useState,
6
+ useEffect,
7
+ useMemo,
8
+ useRef
9
+ } from "react";
10
+ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
11
+ import { jsx } from "react/jsx-runtime";
12
+ var UnifoldContext = createContext(null);
13
+ var createQueryClient = () => new QueryClient({
14
+ defaultOptions: {
15
+ queries: {
16
+ staleTime: 6e4,
17
+ retry: 1
18
+ }
19
+ }
20
+ });
21
+ function UnifoldProvider({
22
+ children,
23
+ publishableKey
24
+ }) {
25
+ const [queryClient] = useState(() => createQueryClient());
26
+ useEffect(() => {
27
+ if (!publishableKey || publishableKey.trim() === "") {
28
+ throw new Error(
29
+ "Unifold: publishableKey is required. Please provide a valid publishable key."
30
+ );
31
+ }
32
+ if (!publishableKey.startsWith("pk_test_") && !publishableKey.startsWith("pk_live_")) {
33
+ console.warn(
34
+ 'Unifold: publishableKey should start with "pk_test_" or "pk_live_". Please ensure you are using a valid publishable key.'
35
+ );
36
+ }
37
+ }, [publishableKey]);
38
+ const prevPublishableKeyRef = useRef(publishableKey);
39
+ useEffect(() => {
40
+ if (prevPublishableKeyRef.current !== publishableKey) {
41
+ console.log(
42
+ "Unifold: Reinitializing with new publishable key:",
43
+ publishableKey
44
+ );
45
+ prevPublishableKeyRef.current = publishableKey;
46
+ }
47
+ }, [publishableKey]);
48
+ const contextValue = useMemo(
49
+ () => ({
50
+ publishableKey
51
+ }),
52
+ [publishableKey]
53
+ );
54
+ return /* @__PURE__ */ jsx(QueryClientProvider, { client: queryClient, children: /* @__PURE__ */ jsx(UnifoldContext.Provider, { value: contextValue, children }) });
55
+ }
56
+ function useUnifold() {
57
+ const context = useContext(UnifoldContext);
58
+ if (typeof window === "undefined") {
59
+ return {
60
+ publishableKey: ""
61
+ };
62
+ }
63
+ if (!context) {
64
+ throw new Error("useUnifold must be used within UnifoldProvider");
65
+ }
66
+ return {
67
+ publishableKey: context.publishableKey
68
+ };
69
+ }
70
+ export {
71
+ UnifoldProvider,
72
+ useUnifold
73
+ };
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@unifold/react-provider",
3
+ "version": "0.1.1",
4
+ "description": "Unifold React Provider - Core provider and context for Unifold React SDKs",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "peerDependencies": {
19
+ "react": "^18.2.0",
20
+ "react-dom": "^18.2.0"
21
+ },
22
+ "dependencies": {
23
+ "@tanstack/react-query": "^5.62.15"
24
+ },
25
+ "devDependencies": {
26
+ "@types/react": "^18.2.0",
27
+ "@types/react-dom": "^18.2.0",
28
+ "tsup": "^8.0.0",
29
+ "typescript": "^5.0.0"
30
+ },
31
+ "keywords": [
32
+ "unifold",
33
+ "react",
34
+ "provider",
35
+ "context",
36
+ "crypto",
37
+ "web3"
38
+ ],
39
+ "author": "Unifold",
40
+ "license": "MIT",
41
+ "scripts": {
42
+ "build": "tsup",
43
+ "dev": "tsup --watch",
44
+ "clean": "rm -rf dist",
45
+ "lint": "eslint \"src/**/*.ts*\"",
46
+ "type-check": "tsc --noEmit"
47
+ }
48
+ }