@trustsig/react 1.1.5 → 1.1.11

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
@@ -8,11 +8,14 @@ React Context Provider and hooks for TrustSig bot protection.
8
8
  npm install @trustsig/react
9
9
  ```
10
10
 
11
- ## Setup
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
- ## Usage
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,109 @@
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
+ interceptRequests,
50
+ debug,
51
+ nonce,
52
+ children
53
+ }) => {
54
+ const [isLoaded, setIsLoaded] = (0, import_react.useState)(false);
55
+ const [error, setError] = (0, import_react.useState)(null);
56
+ const client = (0, import_react.useMemo)(
57
+ () => new import_client.TrustSigClient({
58
+ siteKey,
59
+ endpoint,
60
+ wasmUrl,
61
+ interceptRequests,
62
+ debug,
63
+ nonce
64
+ }),
65
+ [siteKey, endpoint, wasmUrl, interceptRequests, debug, nonce]
66
+ );
67
+ (0, import_react.useEffect)(() => {
68
+ let mounted = true;
69
+ client.load().then(() => {
70
+ if (mounted) {
71
+ setIsLoaded(true);
72
+ }
73
+ }).catch((err) => {
74
+ if (mounted) {
75
+ setError(err);
76
+ }
77
+ });
78
+ return () => {
79
+ mounted = false;
80
+ };
81
+ }, [client]);
82
+ const value = (0, import_react.useMemo)(
83
+ () => ({
84
+ isLoaded,
85
+ error,
86
+ getResponse: () => client.getResponse()
87
+ }),
88
+ [isLoaded, error, client]
89
+ );
90
+ return import_react.default.createElement(TrustSigContext.Provider, { value }, children);
91
+ };
92
+ var useTrustSigContext = () => {
93
+ const context = (0, import_react.useContext)(TrustSigContext);
94
+ if (!context) {
95
+ throw new Error("USE_TRUSTSIG_CONTEXT_ERROR");
96
+ }
97
+ return context;
98
+ };
99
+
100
+ // src/useTrustSig.ts
101
+ var useTrustSig = () => {
102
+ return useTrustSigContext();
103
+ };
104
+ // Annotate the CommonJS export names for ESM import in node:
105
+ 0 && (module.exports = {
106
+ TrustSigProvider,
107
+ useTrustSig,
108
+ useTrustSigContext
109
+ });
@@ -0,0 +1,22 @@
1
+ import React from 'react';
2
+
3
+ interface TrustSigContextValue {
4
+ isLoaded: boolean;
5
+ error: Error | null;
6
+ getResponse: () => Promise<string | null>;
7
+ }
8
+ interface TrustSigProviderProps {
9
+ siteKey: string;
10
+ endpoint?: string;
11
+ wasmUrl?: string;
12
+ interceptRequests?: boolean;
13
+ debug?: boolean;
14
+ nonce?: string;
15
+ children: React.ReactNode;
16
+ }
17
+ declare const TrustSigProvider: React.FC<TrustSigProviderProps>;
18
+ declare const useTrustSigContext: () => TrustSigContextValue;
19
+
20
+ declare const useTrustSig: () => TrustSigContextValue;
21
+
22
+ export { type TrustSigContextValue, TrustSigProvider, type TrustSigProviderProps, useTrustSig, useTrustSigContext };
@@ -0,0 +1,22 @@
1
+ import React from 'react';
2
+
3
+ interface TrustSigContextValue {
4
+ isLoaded: boolean;
5
+ error: Error | null;
6
+ getResponse: () => Promise<string | null>;
7
+ }
8
+ interface TrustSigProviderProps {
9
+ siteKey: string;
10
+ endpoint?: string;
11
+ wasmUrl?: string;
12
+ interceptRequests?: boolean;
13
+ debug?: boolean;
14
+ nonce?: string;
15
+ children: React.ReactNode;
16
+ }
17
+ declare const TrustSigProvider: React.FC<TrustSigProviderProps>;
18
+ declare const useTrustSigContext: () => TrustSigContextValue;
19
+
20
+ declare const useTrustSig: () => TrustSigContextValue;
21
+
22
+ export { type TrustSigContextValue, TrustSigProvider, type TrustSigProviderProps, useTrustSig, useTrustSigContext };
package/dist/index.js ADDED
@@ -0,0 +1,71 @@
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
+ interceptRequests,
13
+ debug,
14
+ nonce,
15
+ children
16
+ }) => {
17
+ const [isLoaded, setIsLoaded] = useState(false);
18
+ const [error, setError] = useState(null);
19
+ const client = useMemo(
20
+ () => new TrustSigClient({
21
+ siteKey,
22
+ endpoint,
23
+ wasmUrl,
24
+ interceptRequests,
25
+ debug,
26
+ nonce
27
+ }),
28
+ [siteKey, endpoint, wasmUrl, interceptRequests, debug, nonce]
29
+ );
30
+ useEffect(() => {
31
+ let mounted = true;
32
+ client.load().then(() => {
33
+ if (mounted) {
34
+ setIsLoaded(true);
35
+ }
36
+ }).catch((err) => {
37
+ if (mounted) {
38
+ setError(err);
39
+ }
40
+ });
41
+ return () => {
42
+ mounted = false;
43
+ };
44
+ }, [client]);
45
+ const value = useMemo(
46
+ () => ({
47
+ isLoaded,
48
+ error,
49
+ getResponse: () => client.getResponse()
50
+ }),
51
+ [isLoaded, error, client]
52
+ );
53
+ return React.createElement(TrustSigContext.Provider, { value }, children);
54
+ };
55
+ var useTrustSigContext = () => {
56
+ const context = useContext(TrustSigContext);
57
+ if (!context) {
58
+ throw new Error("USE_TRUSTSIG_CONTEXT_ERROR");
59
+ }
60
+ return context;
61
+ };
62
+
63
+ // src/useTrustSig.ts
64
+ var useTrustSig = () => {
65
+ return useTrustSigContext();
66
+ };
67
+ export {
68
+ TrustSigProvider,
69
+ useTrustSig,
70
+ useTrustSigContext
71
+ };
package/package.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
2
  "name": "@trustsig/react",
3
- "version": "1.1.5",
3
+ "version": "1.1.11",
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.5",
21
- "@trustsig/types": "^1.1.5"
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
+ }
@@ -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
@@ -1,2 +0,0 @@
1
- export * from './TrustSigProvider';
2
- export * from './useTrustSig';
@@ -1,5 +0,0 @@
1
- import { useTrustSigContext } from './TrustSigProvider';
2
-
3
- export const useTrustSig = () => {
4
- return useTrustSigContext();
5
- };
@@ -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,2 +0,0 @@
1
- import { defineConfig } from 'tsup';
2
- export default defineConfig({ entry: ['src/index.ts'], format: ['cjs', 'esm'], dts: true, clean: true, external: ['react', 'react-dom'] });