@trustsig/react 1.2.7 → 2.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 TrustSig
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @trustsig/react
2
2
 
3
- React Context Provider and hooks for TrustSig bot protection.
3
+ React Context Provider and hooks for TrustSig bot protection. React 18 or 19.
4
4
 
5
5
  ## Installation
6
6
 
@@ -8,10 +8,9 @@ React Context Provider and hooks for TrustSig bot protection.
8
8
  npm install @trustsig/react
9
9
  ```
10
10
 
11
- ## Usage (Next.js App Router)
12
-
13
- ### 1. Root Layout
11
+ ## Usage
14
12
 
13
+ ### 1. Setup Provider
15
14
  Wrap your application or specific routes with the provider.
16
15
 
17
16
  ```tsx
@@ -20,46 +19,63 @@ import { TrustSigProvider } from '@trustsig/react';
20
19
 
21
20
  export default function RootLayout({ children }) {
22
21
  return (
23
- <TrustSigProvider siteKey="YOUR_PUBLIC_KEY" interceptRequests={true}>
22
+ <TrustSigProvider siteKey="pk_live_..." autoScan>
24
23
  {children}
25
24
  </TrustSigProvider>
26
25
  );
27
26
  }
28
27
  ```
29
28
 
30
- ### 2. Usage in Client Components
31
-
32
- Use the hook inside any component nested under the provider.
29
+ ### 2. Using the Hook
30
+ Use `useTrustSig` to access the analysis methods.
33
31
 
34
32
  ```tsx
35
33
  "use client";
36
34
  import { useTrustSig } from '@trustsig/react';
37
35
 
38
- export function LoginForm() {
39
- const { getResponse } = useTrustSig();
36
+ export function ActionForm() {
37
+ const { getResponse, isLoaded, error } = useTrustSig();
40
38
 
41
39
  const handleSubmit = async (e) => {
42
40
  e.preventDefault();
43
- const token = await getResponse();
44
-
45
- await fetch('/api/login', {
46
- method: 'POST',
47
- headers: { 'X-TrustSig-Response': token || '' },
48
- body: JSON.stringify({ email: "user@example.com" })
49
- });
41
+ const response = await getResponse(); // { request_id, token } | null
42
+
43
+ if (response?.token) {
44
+ await fetch('/api/secure-action', {
45
+ method: 'POST',
46
+ headers: {
47
+ 'Content-Type': 'application/json',
48
+ 'X-TrustSig-Response': response.token,
49
+ },
50
+ body: JSON.stringify({ data: '...' }),
51
+ });
52
+ }
50
53
  };
51
54
 
52
55
  return (
53
- <form onSubmit={handleSubmit}>
54
- <button type="submit">Login</button>
55
- </form>
56
+ <button onClick={handleSubmit} disabled={!isLoaded}>
57
+ Submit Secure Action
58
+ </button>
56
59
  );
57
60
  }
58
61
  ```
59
62
 
63
+ `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
65
+ trust boundary: always verify the token with `@trustsig/server`.
66
+
60
67
  ## API
61
68
 
62
69
  | Export | Type | Description |
63
70
  | --- | --- | --- |
64
- | `TrustSigProvider` | Component | The React context provider. |
65
- | `useTrustSig` | Hook | Hook to get the response token and status. |
71
+ | `isLoaded` | `boolean` | True once the TrustSig script has loaded. |
72
+ | `error` | `Error \| null` | Set if script loading failed or timed out (`SCRIPT_LOAD_FAIL` / `SCRIPT_LOAD_TIMEOUT`). |
73
+ | `getResponse()` | `Promise<TrustSigResponse \| null>` | Current analysis result. |
74
+ | `scan()` | `Promise<TrustSigResponse \| null>` | Manually trigger a new analysis scan. |
75
+ | `setCustomData(data)` | `void` | Update custom metadata for subsequent scans. |
76
+
77
+ ### Provider props
78
+
79
+ `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.
package/dist/index.cjs CHANGED
@@ -42,6 +42,17 @@ module.exports = __toCommonJS(index_exports);
42
42
  var import_react = __toESM(require("react"), 1);
43
43
  var import_client = require("@trustsig/client");
44
44
  var TrustSigContext = (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
+ }
45
56
  var TrustSigProvider = ({
46
57
  siteKey,
47
58
  scriptUrl,
@@ -55,6 +66,7 @@ var TrustSigProvider = ({
55
66
  }) => {
56
67
  const [isLoaded, setIsLoaded] = (0, import_react.useState)(false);
57
68
  const [error, setError] = (0, import_react.useState)(null);
69
+ const initialCustomData = (0, import_react.useRef)(customData).current;
58
70
  const client = (0, import_react.useMemo)(
59
71
  () => new import_client.TrustSigClient({
60
72
  siteKey,
@@ -64,30 +76,33 @@ var TrustSigProvider = ({
64
76
  debug,
65
77
  nonce,
66
78
  env,
67
- customData
79
+ customData: initialCustomData
68
80
  }),
69
- [siteKey, scriptUrl, interceptRequests, autoScan, debug, nonce, env, JSON.stringify(customData)]
81
+ [siteKey, scriptUrl, interceptRequests, autoScan, debug, nonce, env, initialCustomData]
70
82
  );
71
83
  (0, import_react.useEffect)(() => {
72
84
  let mounted = true;
73
85
  client.load().then(() => {
74
- if (mounted) {
75
- setIsLoaded(true);
76
- }
86
+ if (mounted) setIsLoaded(true);
77
87
  }).catch((err) => {
78
- if (mounted) {
79
- setError(err);
80
- }
88
+ if (mounted) setError(err);
81
89
  });
82
90
  return () => {
83
91
  mounted = false;
84
92
  };
85
93
  }, [client]);
94
+ const customDataKey = customData ? stableStringify(customData) : "";
95
+ (0, import_react.useEffect)(() => {
96
+ if (customData) {
97
+ client.setCustomData(customData);
98
+ }
99
+ }, [client, customDataKey]);
86
100
  const value = (0, import_react.useMemo)(
87
101
  () => ({
88
102
  isLoaded,
89
103
  error,
90
104
  getResponse: () => client.getResponse(),
105
+ scan: () => client.scan(),
91
106
  setCustomData: (data) => client.setCustomData(data)
92
107
  }),
93
108
  [isLoaded, error, client]
@@ -97,7 +112,7 @@ var TrustSigProvider = ({
97
112
  var useTrustSigContext = () => {
98
113
  const context = (0, import_react.useContext)(TrustSigContext);
99
114
  if (!context) {
100
- throw new Error("USE_TRUSTSIG_CONTEXT_ERROR");
115
+ throw new Error("useTrustSig must be used within a <TrustSigProvider>");
101
116
  }
102
117
  return context;
103
118
  };
package/dist/index.d.cts CHANGED
@@ -1,11 +1,12 @@
1
1
  import React from 'react';
2
- import { TrustSigEnv } from '@trustsig/types';
2
+ import { TrustSigResponse, TrustSigEnv } from '@trustsig/types';
3
3
 
4
4
  interface TrustSigContextValue {
5
5
  isLoaded: boolean;
6
6
  error: Error | null;
7
- getResponse: () => Promise<string | null>;
8
- setCustomData: (data: Record<string, any>) => void;
7
+ getResponse: () => Promise<TrustSigResponse | null>;
8
+ scan: () => Promise<TrustSigResponse | null>;
9
+ setCustomData: (data: Record<string, unknown>) => void;
9
10
  }
10
11
  interface TrustSigProviderProps {
11
12
  siteKey: string;
@@ -15,7 +16,7 @@ interface TrustSigProviderProps {
15
16
  debug?: boolean;
16
17
  nonce?: string;
17
18
  env?: TrustSigEnv;
18
- customData?: Record<string, any>;
19
+ customData?: Record<string, unknown>;
19
20
  children: React.ReactNode;
20
21
  }
21
22
  declare const TrustSigProvider: React.FC<TrustSigProviderProps>;
package/dist/index.d.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  import React from 'react';
2
- import { TrustSigEnv } from '@trustsig/types';
2
+ import { TrustSigResponse, TrustSigEnv } from '@trustsig/types';
3
3
 
4
4
  interface TrustSigContextValue {
5
5
  isLoaded: boolean;
6
6
  error: Error | null;
7
- getResponse: () => Promise<string | null>;
8
- setCustomData: (data: Record<string, any>) => void;
7
+ getResponse: () => Promise<TrustSigResponse | null>;
8
+ scan: () => Promise<TrustSigResponse | null>;
9
+ setCustomData: (data: Record<string, unknown>) => void;
9
10
  }
10
11
  interface TrustSigProviderProps {
11
12
  siteKey: string;
@@ -15,7 +16,7 @@ interface TrustSigProviderProps {
15
16
  debug?: boolean;
16
17
  nonce?: string;
17
18
  env?: TrustSigEnv;
18
- customData?: Record<string, any>;
19
+ customData?: Record<string, unknown>;
19
20
  children: React.ReactNode;
20
21
  }
21
22
  declare const TrustSigProvider: React.FC<TrustSigProviderProps>;
package/dist/index.js CHANGED
@@ -2,9 +2,20 @@
2
2
  "use client";
3
3
 
4
4
  // src/TrustSigProvider.tsx
5
- import React, { createContext, useContext, useEffect, useState, useMemo } from "react";
5
+ import React, { createContext, useContext, useEffect, useRef, useState, useMemo } from "react";
6
6
  import { TrustSigClient } from "@trustsig/client";
7
7
  var TrustSigContext = createContext(null);
8
+ function stableStringify(value) {
9
+ return JSON.stringify(value, (_key, val) => {
10
+ if (val && typeof val === "object" && !Array.isArray(val)) {
11
+ return Object.keys(val).sort().reduce((acc, k) => {
12
+ acc[k] = val[k];
13
+ return acc;
14
+ }, {});
15
+ }
16
+ return val;
17
+ });
18
+ }
8
19
  var TrustSigProvider = ({
9
20
  siteKey,
10
21
  scriptUrl,
@@ -18,6 +29,7 @@ var TrustSigProvider = ({
18
29
  }) => {
19
30
  const [isLoaded, setIsLoaded] = useState(false);
20
31
  const [error, setError] = useState(null);
32
+ const initialCustomData = useRef(customData).current;
21
33
  const client = useMemo(
22
34
  () => new TrustSigClient({
23
35
  siteKey,
@@ -27,30 +39,33 @@ var TrustSigProvider = ({
27
39
  debug,
28
40
  nonce,
29
41
  env,
30
- customData
42
+ customData: initialCustomData
31
43
  }),
32
- [siteKey, scriptUrl, interceptRequests, autoScan, debug, nonce, env, JSON.stringify(customData)]
44
+ [siteKey, scriptUrl, interceptRequests, autoScan, debug, nonce, env, initialCustomData]
33
45
  );
34
46
  useEffect(() => {
35
47
  let mounted = true;
36
48
  client.load().then(() => {
37
- if (mounted) {
38
- setIsLoaded(true);
39
- }
49
+ if (mounted) setIsLoaded(true);
40
50
  }).catch((err) => {
41
- if (mounted) {
42
- setError(err);
43
- }
51
+ if (mounted) setError(err);
44
52
  });
45
53
  return () => {
46
54
  mounted = false;
47
55
  };
48
56
  }, [client]);
57
+ const customDataKey = customData ? stableStringify(customData) : "";
58
+ useEffect(() => {
59
+ if (customData) {
60
+ client.setCustomData(customData);
61
+ }
62
+ }, [client, customDataKey]);
49
63
  const value = useMemo(
50
64
  () => ({
51
65
  isLoaded,
52
66
  error,
53
67
  getResponse: () => client.getResponse(),
68
+ scan: () => client.scan(),
54
69
  setCustomData: (data) => client.setCustomData(data)
55
70
  }),
56
71
  [isLoaded, error, client]
@@ -60,7 +75,7 @@ var TrustSigProvider = ({
60
75
  var useTrustSigContext = () => {
61
76
  const context = useContext(TrustSigContext);
62
77
  if (!context) {
63
- throw new Error("USE_TRUSTSIG_CONTEXT_ERROR");
78
+ throw new Error("useTrustSig must be used within a <TrustSigProvider>");
64
79
  }
65
80
  return context;
66
81
  };
package/package.json CHANGED
@@ -1,10 +1,12 @@
1
1
  {
2
2
  "name": "@trustsig/react",
3
- "version": "1.2.7",
3
+ "version": "2.0.0",
4
+ "description": "React Context Provider and hooks for TrustSig bot protection",
4
5
  "type": "module",
5
6
  "main": "./dist/index.cjs",
6
7
  "module": "./dist/index.js",
7
8
  "types": "./dist/index.d.ts",
9
+ "sideEffects": false,
8
10
  "files": [
9
11
  "dist"
10
12
  ],
@@ -17,14 +19,40 @@
17
19
  "./package.json": "./package.json"
18
20
  },
19
21
  "scripts": {
20
- "build": "tsup"
22
+ "build": "tsup",
23
+ "prepublishOnly": "tsup"
24
+ },
25
+ "keywords": [
26
+ "trustsig",
27
+ "security",
28
+ "bot-protection",
29
+ "react",
30
+ "nextjs",
31
+ "hooks"
32
+ ],
33
+ "author": "TrustSig (https://trustsig.eu)",
34
+ "license": "MIT",
35
+ "homepage": "https://trustsig.eu",
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "git+https://github.com/trustsig-eu/trustsigjs.git",
39
+ "directory": "packages/react"
40
+ },
41
+ "bugs": {
42
+ "url": "https://github.com/trustsig-eu/trustsigjs/issues"
43
+ },
44
+ "engines": {
45
+ "node": ">=18"
21
46
  },
22
47
  "dependencies": {
23
- "@trustsig/client": "^1.1.11",
24
- "@trustsig/types": "^1.1.11"
48
+ "@trustsig/client": "^2.0.0",
49
+ "@trustsig/types": "^2.0.0"
25
50
  },
26
51
  "peerDependencies": {
27
52
  "react": "^18.0.0 || ^19.0.0",
28
53
  "react-dom": "^18.0.0 || ^19.0.0"
54
+ },
55
+ "publishConfig": {
56
+ "access": "public"
29
57
  }
30
58
  }