@trustsig/react 2.2.0 → 2.4.0

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 CHANGED
@@ -1,6 +1,7 @@
1
1
  # @trustsig/react
2
2
 
3
- React Context Provider and hooks for TrustSig bot protection. React 18 or 19.
3
+ React context provider and hooks for TrustSig bot protection. Supports React
4
+ 18 and 19.
4
5
 
5
6
  ## Installation
6
7
 
@@ -10,8 +11,7 @@ npm install @trustsig/react
10
11
 
11
12
  ## Usage
12
13
 
13
- ### 1. Setup Provider
14
- Wrap your application or specific routes with the provider.
14
+ Wrap your application or specific routes with the provider:
15
15
 
16
16
  ```tsx
17
17
  "use client";
@@ -26,8 +26,7 @@ export default function RootLayout({ children }) {
26
26
  }
27
27
  ```
28
28
 
29
- ### 2. Using the Hook
30
- Use `useTrustSig` to access the analysis methods.
29
+ Access the analysis methods with `useTrustSig`:
31
30
 
32
31
  ```tsx
33
32
  "use client";
@@ -61,7 +60,7 @@ export function ActionForm() {
61
60
  ```
62
61
 
63
62
  `getResponse()` resolves to `{ request_id, token }` or `null` (script failed
64
- to load, timed out, or no verdict yet inspect `error`). The server is the
63
+ to load, timed out, or no verdict yet; inspect `error`). The server is the
65
64
  trust boundary: always verify the token with `@trustsig/server`.
66
65
 
67
66
  ## API
@@ -77,5 +76,53 @@ trust boundary: always verify the token with `@trustsig/server`.
77
76
  ### Provider props
78
77
 
79
78
  `siteKey` (required), `scriptUrl`, `interceptRequests`, `autoScan`, `debug`,
80
- `nonce`, `env`, `customData`. Changing `customData` updates the running script
81
- in place it does **not** re-inject the script or reset state.
79
+ `nonce`, `env`, `customData`. Changing `customData` updates the running
80
+ script in place; it does not re-inject the script or reset state.
81
+
82
+ ## TrustSig Pro
83
+
84
+ `TrustSigProProvider` and `useTrustSigPro` drive TrustSig Pro (account-level
85
+ fraud intelligence). They are separate, opt-in exports; the Vanilla
86
+ `TrustSigProvider` / `useTrustSig` above are unchanged. Each provider uses
87
+ its own React context, but the underlying edge script build is shared and
88
+ refuses to initialize twice, so do not mount both providers on the same
89
+ page; whichever script loads first wins. Import from the main entry or from
90
+ `@trustsig/react/pro`.
91
+
92
+ The provider takes a project publishable key (`pk_...` / `PK_...`) instead
93
+ of a `siteKey`. Everything else matches the Vanilla provider.
94
+
95
+ ```tsx
96
+ "use client";
97
+ import { TrustSigProProvider, useTrustSigPro } from '@trustsig/react';
98
+
99
+ export default function RootLayout({ children }) {
100
+ return (
101
+ <TrustSigProProvider publishableKey="PK_live_..." autoScan>
102
+ {children}
103
+ </TrustSigProProvider>
104
+ );
105
+ }
106
+
107
+ function LoginForm() {
108
+ const { getResponse, isLoaded } = useTrustSigPro();
109
+
110
+ const handleSubmit = async (e) => {
111
+ e.preventDefault();
112
+ const res = await getResponse(); // { request_id, token } | null
113
+ // Send res.token to your backend; verify and bind it with @trustsig/server.
114
+ };
115
+
116
+ return <button onClick={handleSubmit} disabled={!isLoaded}>Login</button>;
117
+ }
118
+ ```
119
+
120
+ `useTrustSigPro()` exposes the same shape as `useTrustSig()`: `isLoaded`,
121
+ `error`, `getResponse()`, `scan()`, `setCustomData()`. The returned token
122
+ feeds `TrustSigPro` on the server (`@trustsig/server`), whose verdicts use
123
+ the `ALLOW | REVIEW | BLOCK` decision set.
124
+
125
+ ### Provider props
126
+
127
+ `publishableKey` (required), `scriptUrl`, `interceptRequests`, `autoScan`,
128
+ `debug`, `nonce`, `env`, `customData`.
@@ -0,0 +1,91 @@
1
+ "use client";
2
+
3
+ // src/TrustSigProProvider.tsx
4
+ import React, { createContext, useContext, useEffect, useRef, useState, useMemo } from "react";
5
+ import { TrustSigProClient } from "@trustsig/client";
6
+ var TrustSigProContext = createContext(null);
7
+ function stableStringify(value) {
8
+ return JSON.stringify(value, (_key, val) => {
9
+ if (val && typeof val === "object" && !Array.isArray(val)) {
10
+ return Object.keys(val).sort().reduce((acc, k) => {
11
+ acc[k] = val[k];
12
+ return acc;
13
+ }, {});
14
+ }
15
+ return val;
16
+ });
17
+ }
18
+ var TrustSigProProvider = ({
19
+ publishableKey,
20
+ scriptUrl,
21
+ interceptRequests,
22
+ autoScan,
23
+ debug,
24
+ nonce,
25
+ env,
26
+ customData,
27
+ children
28
+ }) => {
29
+ const [isLoaded, setIsLoaded] = useState(false);
30
+ const [error, setError] = useState(null);
31
+ const initialCustomData = useRef(customData).current;
32
+ const client = useMemo(
33
+ () => new TrustSigProClient({
34
+ publishableKey,
35
+ scriptUrl,
36
+ interceptRequests,
37
+ autoScan,
38
+ debug,
39
+ nonce,
40
+ env,
41
+ customData: initialCustomData
42
+ }),
43
+ [publishableKey, scriptUrl, interceptRequests, autoScan, debug, nonce, env, initialCustomData]
44
+ );
45
+ useEffect(() => {
46
+ let mounted = true;
47
+ client.load().then(() => {
48
+ if (mounted) setIsLoaded(true);
49
+ }).catch((err) => {
50
+ if (mounted) setError(err);
51
+ });
52
+ return () => {
53
+ mounted = false;
54
+ };
55
+ }, [client]);
56
+ const customDataKey = customData ? stableStringify(customData) : "";
57
+ useEffect(() => {
58
+ if (customData) {
59
+ client.setCustomData(customData);
60
+ }
61
+ }, [client, customDataKey]);
62
+ const value = useMemo(
63
+ () => ({
64
+ isLoaded,
65
+ error,
66
+ getResponse: () => client.getResponse(),
67
+ scan: () => client.scan(),
68
+ setCustomData: (data) => client.setCustomData(data)
69
+ }),
70
+ [isLoaded, error, client]
71
+ );
72
+ return React.createElement(TrustSigProContext.Provider, { value }, children);
73
+ };
74
+ var useTrustSigProContext = () => {
75
+ const context = useContext(TrustSigProContext);
76
+ if (!context) {
77
+ throw new Error("useTrustSigPro must be used within a <TrustSigProProvider>");
78
+ }
79
+ return context;
80
+ };
81
+
82
+ // src/useTrustSigPro.ts
83
+ var useTrustSigPro = () => {
84
+ return useTrustSigProContext();
85
+ };
86
+
87
+ export {
88
+ TrustSigProProvider,
89
+ useTrustSigProContext,
90
+ useTrustSigPro
91
+ };
package/dist/index.cjs CHANGED
@@ -32,9 +32,12 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
32
32
  // src/index.ts
33
33
  var index_exports = {};
34
34
  __export(index_exports, {
35
+ TrustSigProProvider: () => TrustSigProProvider,
35
36
  TrustSigProvider: () => TrustSigProvider,
36
37
  useTrustSig: () => useTrustSig,
37
- useTrustSigContext: () => useTrustSigContext
38
+ useTrustSigContext: () => useTrustSigContext,
39
+ useTrustSigPro: () => useTrustSigPro,
40
+ useTrustSigProContext: () => useTrustSigProContext
38
41
  });
39
42
  module.exports = __toCommonJS(index_exports);
40
43
 
@@ -121,9 +124,96 @@ var useTrustSigContext = () => {
121
124
  var useTrustSig = () => {
122
125
  return useTrustSigContext();
123
126
  };
127
+
128
+ // src/TrustSigProProvider.tsx
129
+ var import_react2 = __toESM(require("react"), 1);
130
+ var import_client2 = require("@trustsig/client");
131
+ var TrustSigProContext = (0, import_react2.createContext)(null);
132
+ function stableStringify2(value) {
133
+ return JSON.stringify(value, (_key, val) => {
134
+ if (val && typeof val === "object" && !Array.isArray(val)) {
135
+ return Object.keys(val).sort().reduce((acc, k) => {
136
+ acc[k] = val[k];
137
+ return acc;
138
+ }, {});
139
+ }
140
+ return val;
141
+ });
142
+ }
143
+ var TrustSigProProvider = ({
144
+ publishableKey,
145
+ scriptUrl,
146
+ interceptRequests,
147
+ autoScan,
148
+ debug,
149
+ nonce,
150
+ env,
151
+ customData,
152
+ children
153
+ }) => {
154
+ const [isLoaded, setIsLoaded] = (0, import_react2.useState)(false);
155
+ const [error, setError] = (0, import_react2.useState)(null);
156
+ const initialCustomData = (0, import_react2.useRef)(customData).current;
157
+ const client = (0, import_react2.useMemo)(
158
+ () => new import_client2.TrustSigProClient({
159
+ publishableKey,
160
+ scriptUrl,
161
+ interceptRequests,
162
+ autoScan,
163
+ debug,
164
+ nonce,
165
+ env,
166
+ customData: initialCustomData
167
+ }),
168
+ [publishableKey, scriptUrl, interceptRequests, autoScan, debug, nonce, env, initialCustomData]
169
+ );
170
+ (0, import_react2.useEffect)(() => {
171
+ let mounted = true;
172
+ client.load().then(() => {
173
+ if (mounted) setIsLoaded(true);
174
+ }).catch((err) => {
175
+ if (mounted) setError(err);
176
+ });
177
+ return () => {
178
+ mounted = false;
179
+ };
180
+ }, [client]);
181
+ const customDataKey = customData ? stableStringify2(customData) : "";
182
+ (0, import_react2.useEffect)(() => {
183
+ if (customData) {
184
+ client.setCustomData(customData);
185
+ }
186
+ }, [client, customDataKey]);
187
+ const value = (0, import_react2.useMemo)(
188
+ () => ({
189
+ isLoaded,
190
+ error,
191
+ getResponse: () => client.getResponse(),
192
+ scan: () => client.scan(),
193
+ setCustomData: (data) => client.setCustomData(data)
194
+ }),
195
+ [isLoaded, error, client]
196
+ );
197
+ return import_react2.default.createElement(TrustSigProContext.Provider, { value }, children);
198
+ };
199
+ var useTrustSigProContext = () => {
200
+ const context = (0, import_react2.useContext)(TrustSigProContext);
201
+ if (!context) {
202
+ throw new Error("useTrustSigPro must be used within a <TrustSigProProvider>");
203
+ }
204
+ return context;
205
+ };
206
+
207
+ // src/useTrustSigPro.ts
208
+ var useTrustSigPro = () => {
209
+ return useTrustSigProContext();
210
+ };
124
211
  // Annotate the CommonJS export names for ESM import in node:
125
212
  0 && (module.exports = {
213
+ TrustSigProProvider,
126
214
  TrustSigProvider,
127
215
  useTrustSig,
128
- useTrustSigContext
216
+ useTrustSigContext,
217
+ useTrustSigPro,
218
+ useTrustSigProContext
129
219
  });
package/dist/index.d.cts CHANGED
@@ -1,5 +1,7 @@
1
1
  import React from 'react';
2
2
  import { TrustSigResponse, TrustSigEnv } from '@trustsig/types';
3
+ export { TrustSigProContextValue, TrustSigProProvider, TrustSigProProviderProps, useTrustSigPro, useTrustSigProContext } from './pro.cjs';
4
+ import '@trustsig/client';
3
5
 
4
6
  interface TrustSigContextValue {
5
7
  isLoaded: boolean;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import React from 'react';
2
2
  import { TrustSigResponse, TrustSigEnv } from '@trustsig/types';
3
+ export { TrustSigProContextValue, TrustSigProProvider, TrustSigProProviderProps, useTrustSigPro, useTrustSigProContext } from './pro.js';
4
+ import '@trustsig/client';
3
5
 
4
6
  interface TrustSigContextValue {
5
7
  isLoaded: boolean;
package/dist/index.js CHANGED
@@ -1,5 +1,10 @@
1
1
  "use client";
2
2
  "use client";
3
+ import {
4
+ TrustSigProProvider,
5
+ useTrustSigPro,
6
+ useTrustSigProContext
7
+ } from "./chunk-SIOYXKET.js";
3
8
 
4
9
  // src/TrustSigProvider.tsx
5
10
  import React, { createContext, useContext, useEffect, useRef, useState, useMemo } from "react";
@@ -85,7 +90,10 @@ var useTrustSig = () => {
85
90
  return useTrustSigContext();
86
91
  };
87
92
  export {
93
+ TrustSigProProvider,
88
94
  TrustSigProvider,
89
95
  useTrustSig,
90
- useTrustSigContext
96
+ useTrustSigContext,
97
+ useTrustSigPro,
98
+ useTrustSigProContext
91
99
  };
package/dist/pro.cjs ADDED
@@ -0,0 +1,129 @@
1
+ "use client";
2
+ "use strict";
3
+ "use client";
4
+ var __create = Object.create;
5
+ var __defProp = Object.defineProperty;
6
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getProtoOf = Object.getPrototypeOf;
9
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
10
+ var __export = (target, all) => {
11
+ for (var name in all)
12
+ __defProp(target, name, { get: all[name], enumerable: true });
13
+ };
14
+ var __copyProps = (to, from, except, desc) => {
15
+ if (from && typeof from === "object" || typeof from === "function") {
16
+ for (let key of __getOwnPropNames(from))
17
+ if (!__hasOwnProp.call(to, key) && key !== except)
18
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
19
+ }
20
+ return to;
21
+ };
22
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
23
+ // If the importer is in node compatibility mode or this is not an ESM
24
+ // file that has been converted to a CommonJS file using a Babel-
25
+ // compatible transform (i.e. "__esModule" has not been set), then set
26
+ // "default" to the CommonJS "module.exports" for node compatibility.
27
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
28
+ mod
29
+ ));
30
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
31
+
32
+ // src/pro.ts
33
+ var pro_exports = {};
34
+ __export(pro_exports, {
35
+ TrustSigProProvider: () => TrustSigProProvider,
36
+ useTrustSigPro: () => useTrustSigPro,
37
+ useTrustSigProContext: () => useTrustSigProContext
38
+ });
39
+ module.exports = __toCommonJS(pro_exports);
40
+
41
+ // src/TrustSigProProvider.tsx
42
+ var import_react = __toESM(require("react"), 1);
43
+ var import_client = require("@trustsig/client");
44
+ var TrustSigProContext = (0, import_react.createContext)(null);
45
+ function stableStringify(value) {
46
+ return JSON.stringify(value, (_key, val) => {
47
+ if (val && typeof val === "object" && !Array.isArray(val)) {
48
+ return Object.keys(val).sort().reduce((acc, k) => {
49
+ acc[k] = val[k];
50
+ return acc;
51
+ }, {});
52
+ }
53
+ return val;
54
+ });
55
+ }
56
+ var TrustSigProProvider = ({
57
+ publishableKey,
58
+ scriptUrl,
59
+ interceptRequests,
60
+ autoScan,
61
+ debug,
62
+ nonce,
63
+ env,
64
+ customData,
65
+ children
66
+ }) => {
67
+ const [isLoaded, setIsLoaded] = (0, import_react.useState)(false);
68
+ const [error, setError] = (0, import_react.useState)(null);
69
+ const initialCustomData = (0, import_react.useRef)(customData).current;
70
+ const client = (0, import_react.useMemo)(
71
+ () => new import_client.TrustSigProClient({
72
+ publishableKey,
73
+ scriptUrl,
74
+ interceptRequests,
75
+ autoScan,
76
+ debug,
77
+ nonce,
78
+ env,
79
+ customData: initialCustomData
80
+ }),
81
+ [publishableKey, scriptUrl, interceptRequests, autoScan, debug, nonce, env, initialCustomData]
82
+ );
83
+ (0, import_react.useEffect)(() => {
84
+ let mounted = true;
85
+ client.load().then(() => {
86
+ if (mounted) setIsLoaded(true);
87
+ }).catch((err) => {
88
+ if (mounted) setError(err);
89
+ });
90
+ return () => {
91
+ mounted = false;
92
+ };
93
+ }, [client]);
94
+ const customDataKey = customData ? stableStringify(customData) : "";
95
+ (0, import_react.useEffect)(() => {
96
+ if (customData) {
97
+ client.setCustomData(customData);
98
+ }
99
+ }, [client, customDataKey]);
100
+ const value = (0, import_react.useMemo)(
101
+ () => ({
102
+ isLoaded,
103
+ error,
104
+ getResponse: () => client.getResponse(),
105
+ scan: () => client.scan(),
106
+ setCustomData: (data) => client.setCustomData(data)
107
+ }),
108
+ [isLoaded, error, client]
109
+ );
110
+ return import_react.default.createElement(TrustSigProContext.Provider, { value }, children);
111
+ };
112
+ var useTrustSigProContext = () => {
113
+ const context = (0, import_react.useContext)(TrustSigProContext);
114
+ if (!context) {
115
+ throw new Error("useTrustSigPro must be used within a <TrustSigProProvider>");
116
+ }
117
+ return context;
118
+ };
119
+
120
+ // src/useTrustSigPro.ts
121
+ var useTrustSigPro = () => {
122
+ return useTrustSigProContext();
123
+ };
124
+ // Annotate the CommonJS export names for ESM import in node:
125
+ 0 && (module.exports = {
126
+ TrustSigProProvider,
127
+ useTrustSigPro,
128
+ useTrustSigProContext
129
+ });
package/dist/pro.d.cts ADDED
@@ -0,0 +1,34 @@
1
+ import React from 'react';
2
+ import { ProClientResponse } from '@trustsig/client';
3
+ import { TrustSigEnv } from '@trustsig/types';
4
+
5
+ interface TrustSigProContextValue {
6
+ isLoaded: boolean;
7
+ error: Error | null;
8
+ getResponse: () => Promise<ProClientResponse | null>;
9
+ scan: () => Promise<ProClientResponse | null>;
10
+ setCustomData: (data: Record<string, unknown>) => void;
11
+ }
12
+ interface TrustSigProProviderProps {
13
+ publishableKey: string;
14
+ scriptUrl?: string;
15
+ interceptRequests?: boolean;
16
+ autoScan?: boolean;
17
+ debug?: boolean;
18
+ nonce?: string;
19
+ env?: TrustSigEnv;
20
+ customData?: Record<string, unknown>;
21
+ children: React.ReactNode;
22
+ }
23
+ /**
24
+ * React provider for **TrustSig Pro**. Mirrors {@link TrustSigProvider} but
25
+ * drives the Pro edge loader (publishable key, `epro` script, `REVIEW`-aware
26
+ * verdicts) and exposes its state through a separate context, so a single app
27
+ * can mount both editions without collision.
28
+ */
29
+ declare const TrustSigProProvider: React.FC<TrustSigProProviderProps>;
30
+ declare const useTrustSigProContext: () => TrustSigProContextValue;
31
+
32
+ declare const useTrustSigPro: () => TrustSigProContextValue;
33
+
34
+ export { type TrustSigProContextValue, TrustSigProProvider, type TrustSigProProviderProps, useTrustSigPro, useTrustSigProContext };
package/dist/pro.d.ts ADDED
@@ -0,0 +1,34 @@
1
+ import React from 'react';
2
+ import { ProClientResponse } from '@trustsig/client';
3
+ import { TrustSigEnv } from '@trustsig/types';
4
+
5
+ interface TrustSigProContextValue {
6
+ isLoaded: boolean;
7
+ error: Error | null;
8
+ getResponse: () => Promise<ProClientResponse | null>;
9
+ scan: () => Promise<ProClientResponse | null>;
10
+ setCustomData: (data: Record<string, unknown>) => void;
11
+ }
12
+ interface TrustSigProProviderProps {
13
+ publishableKey: string;
14
+ scriptUrl?: string;
15
+ interceptRequests?: boolean;
16
+ autoScan?: boolean;
17
+ debug?: boolean;
18
+ nonce?: string;
19
+ env?: TrustSigEnv;
20
+ customData?: Record<string, unknown>;
21
+ children: React.ReactNode;
22
+ }
23
+ /**
24
+ * React provider for **TrustSig Pro**. Mirrors {@link TrustSigProvider} but
25
+ * drives the Pro edge loader (publishable key, `epro` script, `REVIEW`-aware
26
+ * verdicts) and exposes its state through a separate context, so a single app
27
+ * can mount both editions without collision.
28
+ */
29
+ declare const TrustSigProProvider: React.FC<TrustSigProProviderProps>;
30
+ declare const useTrustSigProContext: () => TrustSigProContextValue;
31
+
32
+ declare const useTrustSigPro: () => TrustSigProContextValue;
33
+
34
+ export { type TrustSigProContextValue, TrustSigProProvider, type TrustSigProProviderProps, useTrustSigPro, useTrustSigProContext };
package/dist/pro.js ADDED
@@ -0,0 +1,12 @@
1
+ "use client";
2
+ "use client";
3
+ import {
4
+ TrustSigProProvider,
5
+ useTrustSigPro,
6
+ useTrustSigProContext
7
+ } from "./chunk-SIOYXKET.js";
8
+ export {
9
+ TrustSigProProvider,
10
+ useTrustSigPro,
11
+ useTrustSigProContext
12
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trustsig/react",
3
- "version": "2.2.0",
3
+ "version": "2.4.0",
4
4
  "description": "React Context Provider and hooks for TrustSig bot protection",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -16,6 +16,11 @@
16
16
  "import": "./dist/index.js",
17
17
  "require": "./dist/index.cjs"
18
18
  },
19
+ "./pro": {
20
+ "types": "./dist/pro.d.ts",
21
+ "import": "./dist/pro.js",
22
+ "require": "./dist/pro.cjs"
23
+ },
19
24
  "./package.json": "./package.json"
20
25
  },
21
26
  "scripts": {
@@ -45,8 +50,8 @@
45
50
  "node": ">=18"
46
51
  },
47
52
  "dependencies": {
48
- "@trustsig/client": "^2.2.0",
49
- "@trustsig/types": "^2.2.0"
53
+ "@trustsig/client": "^2.4.0",
54
+ "@trustsig/types": "^2.4.0"
50
55
  },
51
56
  "peerDependencies": {
52
57
  "react": "^18.0.0 || ^19.0.0",