@trustsig/react 1.2.8 → 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
 
@@ -19,10 +19,7 @@ import { TrustSigProvider } from '@trustsig/react';
19
19
 
20
20
  export default function RootLayout({ children }) {
21
21
  return (
22
- <TrustSigProvider
23
- siteKey="pk_live_..."
24
- autoScan={true}
25
- >
22
+ <TrustSigProvider siteKey="pk_live_..." autoScan>
26
23
  {children}
27
24
  </TrustSigProvider>
28
25
  );
@@ -37,22 +34,20 @@ Use `useTrustSig` to access the analysis methods.
37
34
  import { useTrustSig } from '@trustsig/react';
38
35
 
39
36
  export function ActionForm() {
40
- const { getResponse, scan, isLoaded } = useTrustSig();
37
+ const { getResponse, isLoaded, error } = useTrustSig();
41
38
 
42
39
  const handleSubmit = async (e) => {
43
40
  e.preventDefault();
44
-
45
- // Get the current analysis response
46
- const response = await getResponse();
47
-
41
+ const response = await getResponse(); // { request_id, token } | null
42
+
48
43
  if (response?.token) {
49
44
  await fetch('/api/secure-action', {
50
45
  method: 'POST',
51
- headers: {
46
+ headers: {
52
47
  'Content-Type': 'application/json',
53
- 'X-TrustSig-Response': response.token
48
+ 'X-TrustSig-Response': response.token,
54
49
  },
55
- body: JSON.stringify({ data: "..." })
50
+ body: JSON.stringify({ data: '...' }),
56
51
  });
57
52
  }
58
53
  };
@@ -65,12 +60,22 @@ export function ActionForm() {
65
60
  }
66
61
  ```
67
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
+
68
67
  ## API
69
68
 
70
69
  | Export | Type | Description |
71
70
  | --- | --- | --- |
72
- | `isLoaded` | `boolean` | True when the TrustSig script has loaded. |
73
- | `getResponse()` | `Promise<TrustSigResponse>` | Returns the current analysis result. |
74
- | `scan()` | `Promise<TrustSigResponse>` | Manually triggers a new analysis scan. |
75
- | `setCustomData(data)` | `void` | Updates custom metadata for subsequent scans. |
76
- | `error` | `Error \| null` | Error object if script loading failed. |
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,25 +76,27 @@ 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,
@@ -98,7 +112,7 @@ var TrustSigProvider = ({
98
112
  var useTrustSigContext = () => {
99
113
  const context = (0, import_react.useContext)(TrustSigContext);
100
114
  if (!context) {
101
- throw new Error("USE_TRUSTSIG_CONTEXT_ERROR");
115
+ throw new Error("useTrustSig must be used within a <TrustSigProvider>");
102
116
  }
103
117
  return context;
104
118
  };
package/dist/index.d.cts CHANGED
@@ -6,7 +6,7 @@ interface TrustSigContextValue {
6
6
  error: Error | null;
7
7
  getResponse: () => Promise<TrustSigResponse | null>;
8
8
  scan: () => Promise<TrustSigResponse | null>;
9
- setCustomData: (data: Record<string, any>) => void;
9
+ setCustomData: (data: Record<string, unknown>) => void;
10
10
  }
11
11
  interface TrustSigProviderProps {
12
12
  siteKey: string;
@@ -16,7 +16,7 @@ interface TrustSigProviderProps {
16
16
  debug?: boolean;
17
17
  nonce?: string;
18
18
  env?: TrustSigEnv;
19
- customData?: Record<string, any>;
19
+ customData?: Record<string, unknown>;
20
20
  children: React.ReactNode;
21
21
  }
22
22
  declare const TrustSigProvider: React.FC<TrustSigProviderProps>;
package/dist/index.d.ts CHANGED
@@ -6,7 +6,7 @@ interface TrustSigContextValue {
6
6
  error: Error | null;
7
7
  getResponse: () => Promise<TrustSigResponse | null>;
8
8
  scan: () => Promise<TrustSigResponse | null>;
9
- setCustomData: (data: Record<string, any>) => void;
9
+ setCustomData: (data: Record<string, unknown>) => void;
10
10
  }
11
11
  interface TrustSigProviderProps {
12
12
  siteKey: string;
@@ -16,7 +16,7 @@ interface TrustSigProviderProps {
16
16
  debug?: boolean;
17
17
  nonce?: string;
18
18
  env?: TrustSigEnv;
19
- customData?: Record<string, any>;
19
+ customData?: Record<string, unknown>;
20
20
  children: React.ReactNode;
21
21
  }
22
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,25 +39,27 @@ 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,
@@ -61,7 +75,7 @@ var TrustSigProvider = ({
61
75
  var useTrustSigContext = () => {
62
76
  const context = useContext(TrustSigContext);
63
77
  if (!context) {
64
- throw new Error("USE_TRUSTSIG_CONTEXT_ERROR");
78
+ throw new Error("useTrustSig must be used within a <TrustSigProvider>");
65
79
  }
66
80
  return context;
67
81
  };
package/package.json CHANGED
@@ -1,10 +1,12 @@
1
1
  {
2
2
  "name": "@trustsig/react",
3
- "version": "1.2.8",
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.2.8",
24
- "@trustsig/types": "^1.2.8"
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
  }