@trustsig/react 1.1.6 → 1.2.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 +13 -2
- package/dist/index.cjs +116 -0
- package/dist/index.d.cts +27 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +78 -0
- package/package.json +7 -4
- package/src/TrustSigProvider.tsx +0 -80
- package/src/index.ts +0 -4
- package/src/useTrustSig.ts +0 -5
- package/tests/react.test.tsx +0 -45
- package/tsup.config.ts +0 -13
package/README.md
CHANGED
|
@@ -8,11 +8,14 @@ React Context Provider and hooks for TrustSig bot protection.
|
|
|
8
8
|
npm install @trustsig/react
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
11
|
+
## Usage (Next.js App Router)
|
|
12
|
+
|
|
13
|
+
### 1. Root Layout
|
|
12
14
|
|
|
13
15
|
Wrap your application or specific routes with the provider.
|
|
14
16
|
|
|
15
17
|
```tsx
|
|
18
|
+
"use client";
|
|
16
19
|
import { TrustSigProvider } from '@trustsig/react';
|
|
17
20
|
|
|
18
21
|
export default function RootLayout({ children }) {
|
|
@@ -24,11 +27,12 @@ export default function RootLayout({ children }) {
|
|
|
24
27
|
}
|
|
25
28
|
```
|
|
26
29
|
|
|
27
|
-
|
|
30
|
+
### 2. Usage in Client Components
|
|
28
31
|
|
|
29
32
|
Use the hook inside any component nested under the provider.
|
|
30
33
|
|
|
31
34
|
```tsx
|
|
35
|
+
"use client";
|
|
32
36
|
import { useTrustSig } from '@trustsig/react';
|
|
33
37
|
|
|
34
38
|
export function LoginForm() {
|
|
@@ -52,3 +56,10 @@ export function LoginForm() {
|
|
|
52
56
|
);
|
|
53
57
|
}
|
|
54
58
|
```
|
|
59
|
+
|
|
60
|
+
## API
|
|
61
|
+
|
|
62
|
+
| Export | Type | Description |
|
|
63
|
+
| --- | --- | --- |
|
|
64
|
+
| `TrustSigProvider` | Component | The React context provider. |
|
|
65
|
+
| `useTrustSig` | Hook | Hook to get the response token and status. |
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
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/index.ts
|
|
33
|
+
var index_exports = {};
|
|
34
|
+
__export(index_exports, {
|
|
35
|
+
TrustSigProvider: () => TrustSigProvider,
|
|
36
|
+
useTrustSig: () => useTrustSig,
|
|
37
|
+
useTrustSigContext: () => useTrustSigContext
|
|
38
|
+
});
|
|
39
|
+
module.exports = __toCommonJS(index_exports);
|
|
40
|
+
|
|
41
|
+
// src/TrustSigProvider.tsx
|
|
42
|
+
var import_react = __toESM(require("react"), 1);
|
|
43
|
+
var import_client = require("@trustsig/client");
|
|
44
|
+
var TrustSigContext = (0, import_react.createContext)(null);
|
|
45
|
+
var TrustSigProvider = ({
|
|
46
|
+
siteKey,
|
|
47
|
+
endpoint,
|
|
48
|
+
wasmUrl,
|
|
49
|
+
scriptUrl,
|
|
50
|
+
interceptRequests,
|
|
51
|
+
debug,
|
|
52
|
+
nonce,
|
|
53
|
+
env,
|
|
54
|
+
customData,
|
|
55
|
+
children
|
|
56
|
+
}) => {
|
|
57
|
+
const [isLoaded, setIsLoaded] = (0, import_react.useState)(false);
|
|
58
|
+
const [error, setError] = (0, import_react.useState)(null);
|
|
59
|
+
const client = (0, import_react.useMemo)(
|
|
60
|
+
() => new import_client.TrustSigClient({
|
|
61
|
+
siteKey,
|
|
62
|
+
endpoint,
|
|
63
|
+
wasmUrl,
|
|
64
|
+
scriptUrl,
|
|
65
|
+
interceptRequests,
|
|
66
|
+
debug,
|
|
67
|
+
nonce,
|
|
68
|
+
env,
|
|
69
|
+
customData
|
|
70
|
+
}),
|
|
71
|
+
[siteKey, endpoint, wasmUrl, scriptUrl, interceptRequests, debug, nonce, env, JSON.stringify(customData)]
|
|
72
|
+
);
|
|
73
|
+
(0, import_react.useEffect)(() => {
|
|
74
|
+
let mounted = true;
|
|
75
|
+
client.load().then(() => {
|
|
76
|
+
if (mounted) {
|
|
77
|
+
setIsLoaded(true);
|
|
78
|
+
}
|
|
79
|
+
}).catch((err) => {
|
|
80
|
+
if (mounted) {
|
|
81
|
+
setError(err);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
return () => {
|
|
85
|
+
mounted = false;
|
|
86
|
+
};
|
|
87
|
+
}, [client]);
|
|
88
|
+
const value = (0, import_react.useMemo)(
|
|
89
|
+
() => ({
|
|
90
|
+
isLoaded,
|
|
91
|
+
error,
|
|
92
|
+
getResponse: () => client.getResponse(),
|
|
93
|
+
setCustomData: (data) => client.setCustomData(data)
|
|
94
|
+
}),
|
|
95
|
+
[isLoaded, error, client]
|
|
96
|
+
);
|
|
97
|
+
return import_react.default.createElement(TrustSigContext.Provider, { value }, children);
|
|
98
|
+
};
|
|
99
|
+
var useTrustSigContext = () => {
|
|
100
|
+
const context = (0, import_react.useContext)(TrustSigContext);
|
|
101
|
+
if (!context) {
|
|
102
|
+
throw new Error("USE_TRUSTSIG_CONTEXT_ERROR");
|
|
103
|
+
}
|
|
104
|
+
return context;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
// src/useTrustSig.ts
|
|
108
|
+
var useTrustSig = () => {
|
|
109
|
+
return useTrustSigContext();
|
|
110
|
+
};
|
|
111
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
112
|
+
0 && (module.exports = {
|
|
113
|
+
TrustSigProvider,
|
|
114
|
+
useTrustSig,
|
|
115
|
+
useTrustSigContext
|
|
116
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { TrustSigEnv } from '@trustsig/types';
|
|
3
|
+
|
|
4
|
+
interface TrustSigContextValue {
|
|
5
|
+
isLoaded: boolean;
|
|
6
|
+
error: Error | null;
|
|
7
|
+
getResponse: () => Promise<string | null>;
|
|
8
|
+
setCustomData: (data: Record<string, any>) => void;
|
|
9
|
+
}
|
|
10
|
+
interface TrustSigProviderProps {
|
|
11
|
+
siteKey: string;
|
|
12
|
+
endpoint?: string;
|
|
13
|
+
wasmUrl?: string;
|
|
14
|
+
scriptUrl?: string;
|
|
15
|
+
interceptRequests?: boolean;
|
|
16
|
+
debug?: boolean;
|
|
17
|
+
nonce?: string;
|
|
18
|
+
env?: TrustSigEnv;
|
|
19
|
+
customData?: Record<string, any>;
|
|
20
|
+
children: React.ReactNode;
|
|
21
|
+
}
|
|
22
|
+
declare const TrustSigProvider: React.FC<TrustSigProviderProps>;
|
|
23
|
+
declare const useTrustSigContext: () => TrustSigContextValue;
|
|
24
|
+
|
|
25
|
+
declare const useTrustSig: () => TrustSigContextValue;
|
|
26
|
+
|
|
27
|
+
export { type TrustSigContextValue, TrustSigProvider, type TrustSigProviderProps, useTrustSig, useTrustSigContext };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { TrustSigEnv } from '@trustsig/types';
|
|
3
|
+
|
|
4
|
+
interface TrustSigContextValue {
|
|
5
|
+
isLoaded: boolean;
|
|
6
|
+
error: Error | null;
|
|
7
|
+
getResponse: () => Promise<string | null>;
|
|
8
|
+
setCustomData: (data: Record<string, any>) => void;
|
|
9
|
+
}
|
|
10
|
+
interface TrustSigProviderProps {
|
|
11
|
+
siteKey: string;
|
|
12
|
+
endpoint?: string;
|
|
13
|
+
wasmUrl?: string;
|
|
14
|
+
scriptUrl?: string;
|
|
15
|
+
interceptRequests?: boolean;
|
|
16
|
+
debug?: boolean;
|
|
17
|
+
nonce?: string;
|
|
18
|
+
env?: TrustSigEnv;
|
|
19
|
+
customData?: Record<string, any>;
|
|
20
|
+
children: React.ReactNode;
|
|
21
|
+
}
|
|
22
|
+
declare const TrustSigProvider: React.FC<TrustSigProviderProps>;
|
|
23
|
+
declare const useTrustSigContext: () => TrustSigContextValue;
|
|
24
|
+
|
|
25
|
+
declare const useTrustSig: () => TrustSigContextValue;
|
|
26
|
+
|
|
27
|
+
export { type TrustSigContextValue, TrustSigProvider, type TrustSigProviderProps, useTrustSig, useTrustSigContext };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use client";
|
|
3
|
+
|
|
4
|
+
// src/TrustSigProvider.tsx
|
|
5
|
+
import React, { createContext, useContext, useEffect, useState, useMemo } from "react";
|
|
6
|
+
import { TrustSigClient } from "@trustsig/client";
|
|
7
|
+
var TrustSigContext = createContext(null);
|
|
8
|
+
var TrustSigProvider = ({
|
|
9
|
+
siteKey,
|
|
10
|
+
endpoint,
|
|
11
|
+
wasmUrl,
|
|
12
|
+
scriptUrl,
|
|
13
|
+
interceptRequests,
|
|
14
|
+
debug,
|
|
15
|
+
nonce,
|
|
16
|
+
env,
|
|
17
|
+
customData,
|
|
18
|
+
children
|
|
19
|
+
}) => {
|
|
20
|
+
const [isLoaded, setIsLoaded] = useState(false);
|
|
21
|
+
const [error, setError] = useState(null);
|
|
22
|
+
const client = useMemo(
|
|
23
|
+
() => new TrustSigClient({
|
|
24
|
+
siteKey,
|
|
25
|
+
endpoint,
|
|
26
|
+
wasmUrl,
|
|
27
|
+
scriptUrl,
|
|
28
|
+
interceptRequests,
|
|
29
|
+
debug,
|
|
30
|
+
nonce,
|
|
31
|
+
env,
|
|
32
|
+
customData
|
|
33
|
+
}),
|
|
34
|
+
[siteKey, endpoint, wasmUrl, scriptUrl, interceptRequests, debug, nonce, env, JSON.stringify(customData)]
|
|
35
|
+
);
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
let mounted = true;
|
|
38
|
+
client.load().then(() => {
|
|
39
|
+
if (mounted) {
|
|
40
|
+
setIsLoaded(true);
|
|
41
|
+
}
|
|
42
|
+
}).catch((err) => {
|
|
43
|
+
if (mounted) {
|
|
44
|
+
setError(err);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
return () => {
|
|
48
|
+
mounted = false;
|
|
49
|
+
};
|
|
50
|
+
}, [client]);
|
|
51
|
+
const value = useMemo(
|
|
52
|
+
() => ({
|
|
53
|
+
isLoaded,
|
|
54
|
+
error,
|
|
55
|
+
getResponse: () => client.getResponse(),
|
|
56
|
+
setCustomData: (data) => client.setCustomData(data)
|
|
57
|
+
}),
|
|
58
|
+
[isLoaded, error, client]
|
|
59
|
+
);
|
|
60
|
+
return React.createElement(TrustSigContext.Provider, { value }, children);
|
|
61
|
+
};
|
|
62
|
+
var useTrustSigContext = () => {
|
|
63
|
+
const context = useContext(TrustSigContext);
|
|
64
|
+
if (!context) {
|
|
65
|
+
throw new Error("USE_TRUSTSIG_CONTEXT_ERROR");
|
|
66
|
+
}
|
|
67
|
+
return context;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// src/useTrustSig.ts
|
|
71
|
+
var useTrustSig = () => {
|
|
72
|
+
return useTrustSigContext();
|
|
73
|
+
};
|
|
74
|
+
export {
|
|
75
|
+
TrustSigProvider,
|
|
76
|
+
useTrustSig,
|
|
77
|
+
useTrustSigContext
|
|
78
|
+
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trustsig/react",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
8
11
|
"exports": {
|
|
9
12
|
".": {
|
|
10
13
|
"types": "./dist/index.d.ts",
|
|
@@ -17,11 +20,11 @@
|
|
|
17
20
|
"build": "tsup"
|
|
18
21
|
},
|
|
19
22
|
"dependencies": {
|
|
20
|
-
"@trustsig/client": "^1.1.
|
|
21
|
-
"@trustsig/types": "^1.1.
|
|
23
|
+
"@trustsig/client": "^1.1.11",
|
|
24
|
+
"@trustsig/types": "^1.1.11"
|
|
22
25
|
},
|
|
23
26
|
"peerDependencies": {
|
|
24
27
|
"react": "^18.0.0 || ^19.0.0",
|
|
25
28
|
"react-dom": "^18.0.0 || ^19.0.0"
|
|
26
29
|
}
|
|
27
|
-
}
|
|
30
|
+
}
|
package/src/TrustSigProvider.tsx
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import React, { createContext, useContext, useEffect, useState, useMemo } from 'react';
|
|
2
|
-
import { TrustSigClient, ClientOptions } from '@trustsig/client';
|
|
3
|
-
|
|
4
|
-
export interface TrustSigContextValue {
|
|
5
|
-
isLoaded: boolean;
|
|
6
|
-
error: Error | null;
|
|
7
|
-
getResponse: () => Promise<string | null>;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const TrustSigContext = createContext<TrustSigContextValue | null>(null);
|
|
11
|
-
|
|
12
|
-
export interface TrustSigProviderProps extends ClientOptions {
|
|
13
|
-
children: React.ReactNode;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export const TrustSigProvider: React.FC<TrustSigProviderProps> = ({
|
|
17
|
-
siteKey,
|
|
18
|
-
endpoint,
|
|
19
|
-
wasmUrl,
|
|
20
|
-
interceptRequests,
|
|
21
|
-
debug,
|
|
22
|
-
nonce,
|
|
23
|
-
children,
|
|
24
|
-
}) => {
|
|
25
|
-
const [isLoaded, setIsLoaded] = useState(false);
|
|
26
|
-
const [error, setError] = useState<Error | null>(null);
|
|
27
|
-
|
|
28
|
-
const client = useMemo(
|
|
29
|
-
() =>
|
|
30
|
-
new TrustSigClient({
|
|
31
|
-
siteKey,
|
|
32
|
-
endpoint,
|
|
33
|
-
wasmUrl,
|
|
34
|
-
interceptRequests,
|
|
35
|
-
debug,
|
|
36
|
-
nonce,
|
|
37
|
-
}),
|
|
38
|
-
[siteKey, endpoint, wasmUrl, interceptRequests, debug, nonce]
|
|
39
|
-
);
|
|
40
|
-
|
|
41
|
-
useEffect(() => {
|
|
42
|
-
let mounted = true;
|
|
43
|
-
|
|
44
|
-
client
|
|
45
|
-
.load()
|
|
46
|
-
.then(() => {
|
|
47
|
-
if (mounted) {
|
|
48
|
-
setIsLoaded(true);
|
|
49
|
-
}
|
|
50
|
-
})
|
|
51
|
-
.catch((err) => {
|
|
52
|
-
if (mounted) {
|
|
53
|
-
setError(err);
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
return () => {
|
|
58
|
-
mounted = false;
|
|
59
|
-
};
|
|
60
|
-
}, [client]);
|
|
61
|
-
|
|
62
|
-
const value = useMemo(
|
|
63
|
-
() => ({
|
|
64
|
-
isLoaded,
|
|
65
|
-
error,
|
|
66
|
-
getResponse: () => client.getResponse(),
|
|
67
|
-
}),
|
|
68
|
-
[isLoaded, error, client]
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
return React.createElement(TrustSigContext.Provider, { value }, children);
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
export const useTrustSigContext = () => {
|
|
75
|
-
const context = useContext(TrustSigContext);
|
|
76
|
-
if (!context) {
|
|
77
|
-
throw new Error("USE_TRUSTSIG_CONTEXT_ERROR");
|
|
78
|
-
}
|
|
79
|
-
return context;
|
|
80
|
-
};
|
package/src/index.ts
DELETED
package/src/useTrustSig.ts
DELETED
package/tests/react.test.tsx
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
2
|
-
import { render, screen } from '@testing-library/react';
|
|
3
|
-
import React from 'react';
|
|
4
|
-
import { TrustSigProvider, useTrustSig } from '../src/index';
|
|
5
|
-
|
|
6
|
-
const TestComponent = () => {
|
|
7
|
-
const { isLoaded, getResponse } = useTrustSig();
|
|
8
|
-
return React.createElement(
|
|
9
|
-
'div',
|
|
10
|
-
null,
|
|
11
|
-
isLoaded ? 'Loaded' : 'Loading',
|
|
12
|
-
React.createElement('button', { onClick: getResponse }, 'Get')
|
|
13
|
-
);
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
describe('TrustSig React Provider', () => {
|
|
17
|
-
beforeEach(() => {
|
|
18
|
-
document.head.innerHTML = '';
|
|
19
|
-
delete (window as any).TrustSig;
|
|
20
|
-
|
|
21
|
-
// Prevent happy-dom from actually trying to download the remote script
|
|
22
|
-
vi.spyOn(document.head, 'appendChild').mockImplementation((node) => {
|
|
23
|
-
if (node instanceof HTMLScriptElement) {
|
|
24
|
-
setTimeout(() => {
|
|
25
|
-
if (node.onload) node.onload(new Event('load'));
|
|
26
|
-
}, 10);
|
|
27
|
-
}
|
|
28
|
-
return node;
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('renders children and provides context', async () => {
|
|
33
|
-
render(
|
|
34
|
-
React.createElement(
|
|
35
|
-
TrustSigProvider,
|
|
36
|
-
{ siteKey: 'TEST' },
|
|
37
|
-
React.createElement(TestComponent)
|
|
38
|
-
)
|
|
39
|
-
);
|
|
40
|
-
expect(screen.getByText('Loading')).toBeDefined();
|
|
41
|
-
|
|
42
|
-
const appendCall = (document.head.appendChild as any).mock.calls[0][0];
|
|
43
|
-
expect(appendCall.getAttribute('data-site-key')).toBe('TEST');
|
|
44
|
-
});
|
|
45
|
-
});
|
package/tsup.config.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { defineConfig } from 'tsup';
|
|
2
|
-
export default defineConfig({
|
|
3
|
-
entry: ['src/index.ts'],
|
|
4
|
-
format: ['cjs', 'esm'],
|
|
5
|
-
dts: true,
|
|
6
|
-
clean: true,
|
|
7
|
-
external: ['react', 'react-dom'],
|
|
8
|
-
esbuildOptions(options) {
|
|
9
|
-
options.banner = {
|
|
10
|
-
js: '"use client";',
|
|
11
|
-
};
|
|
12
|
-
},
|
|
13
|
-
});
|