@proofhorn/next 0.0.0-stage → 0.3.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 +21 -0
- package/README.md +97 -2
- package/assets/proofhorn-logo-dark.png +0 -0
- package/assets/proofhorn-logo.png +0 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +5 -0
- package/dist/provider.d.ts +18 -0
- package/dist/provider.js +40 -0
- package/dist/runtime/load.d.ts +4 -0
- package/dist/runtime/load.js +19 -0
- package/dist/runtime/types.d.ts +33 -0
- package/dist/runtime/types.js +1 -0
- package/dist/runtime/vendor/LICENSE +21 -0
- package/dist/runtime/vendor/proofhorn-runtime.js +903 -0
- package/dist/server/client.d.ts +53 -0
- package/dist/server/client.js +98 -0
- package/dist/server/components.d.ts +55 -0
- package/dist/server/components.js +56 -0
- package/dist/server/env.d.ts +6 -0
- package/dist/server/env.js +24 -0
- package/dist/server/index.d.ts +20 -0
- package/dist/server/index.js +26 -0
- package/dist/server/reads.d.ts +7 -0
- package/dist/server/reads.js +21 -0
- package/dist/server/webhook.d.ts +7 -0
- package/dist/server/webhook.js +41 -0
- package/dist/shared/http.d.ts +38 -0
- package/dist/shared/http.js +81 -0
- package/dist/shared/public-client.d.ts +32 -0
- package/dist/shared/public-client.js +78 -0
- package/dist/shared/types.d.ts +323 -0
- package/dist/shared/types.js +17 -0
- package/dist/star-rating.d.ts +6 -0
- package/dist/star-rating.js +48 -0
- package/dist/visitor.d.ts +8 -0
- package/dist/visitor.js +19 -0
- package/docs/csp.md +22 -0
- package/docs/quick-start.md +104 -0
- package/docs/server-examples.md +122 -0
- package/package.json +70 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Khushrav
|
|
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,3 +1,98 @@
|
|
|
1
|
-
|
|
1
|
+
<p>
|
|
2
|
+
<picture>
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://unpkg.com/@proofhorn/next/assets/proofhorn-logo-dark.png">
|
|
4
|
+
<img src="https://unpkg.com/@proofhorn/next/assets/proofhorn-logo.png" alt="Proofhorn" width="300">
|
|
5
|
+
</picture>
|
|
6
|
+
</p>
|
|
2
7
|
|
|
3
|
-
|
|
8
|
+
# @proofhorn/next
|
|
9
|
+
|
|
10
|
+
Reviews, social proof, A/B testing and referrals for Next.js sites, from [Proofhorn](https://proofhorn.khushrav.com). One package, two entries:
|
|
11
|
+
|
|
12
|
+
| Entry | Runs in | Needs |
|
|
13
|
+
| --- | --- | --- |
|
|
14
|
+
| `@proofhorn/next` | Browser (client components) | `NEXT_PUBLIC_PROOFHORN_KEY` (`pk_live_…` or `pk_test_…`) |
|
|
15
|
+
| `@proofhorn/next/server` | Server only. Guarded with `server-only`, so importing it from a client component fails the build | `PROOFHORN_SECRET_KEY` (`sk_live_…` or `sk_test_…`) |
|
|
16
|
+
|
|
17
|
+
Test keys act on the same site in test mode. Test rows are never shown to live visitors.
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install @proofhorn/next
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Needs Next.js 15 or 16 and React 18.2 or 19. Put both keys from the Proofhorn dashboard in `.env.local`:
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
NEXT_PUBLIC_PROOFHORN_KEY=pk_test_...
|
|
29
|
+
PROOFHORN_SECRET_KEY=sk_test_...
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
Load the widgets once, in `app/layout.tsx`:
|
|
35
|
+
|
|
36
|
+
```tsx
|
|
37
|
+
import { ProofhornProvider, ProofWidgets } from '@proofhorn/next';
|
|
38
|
+
|
|
39
|
+
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
|
40
|
+
return (
|
|
41
|
+
<html lang="en">
|
|
42
|
+
<body>
|
|
43
|
+
<ProofhornProvider publicKey={process.env.NEXT_PUBLIC_PROOFHORN_KEY!}>
|
|
44
|
+
{children}
|
|
45
|
+
<ProofWidgets placement="all" />
|
|
46
|
+
</ProofhornProvider>
|
|
47
|
+
</body>
|
|
48
|
+
</html>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Record something real that happened, from a route handler, server action or webhook:
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { recordActivity } from '@proofhorn/next/server';
|
|
57
|
+
|
|
58
|
+
await recordActivity({
|
|
59
|
+
source: 'checkout',
|
|
60
|
+
externalId: order.id, // the same ID on a retry is not counted twice
|
|
61
|
+
type: 'purchase',
|
|
62
|
+
occurredAt: new Date().toISOString(),
|
|
63
|
+
item: { key: 'course-101', type: 'course', title: 'Course 101' },
|
|
64
|
+
actor: { firstName: 'Asha', city: 'Pune' },
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
More:
|
|
69
|
+
|
|
70
|
+
- [Quick start](docs/quick-start.md)
|
|
71
|
+
- [Form handlers and payment webhooks](docs/server-examples.md)
|
|
72
|
+
- [Content Security Policy](docs/csp.md)
|
|
73
|
+
|
|
74
|
+
## Rules the package keeps for you
|
|
75
|
+
|
|
76
|
+
- The secret key never reaches a browser. The public client refuses an `sk_` key, the server client refuses a `pk_` key, and the server entry cannot be bundled for the client.
|
|
77
|
+
- Reads never break a page. Server reads give up within 2 seconds and render nothing. Widgets do nothing when the API is unreachable.
|
|
78
|
+
- Writes throw a `ProofhornError` with `retryable`, so a webhook can answer with an error and be retried.
|
|
79
|
+
- `recordActivity` takes only the fields in the API contract. Its types reject a surname, email or phone, and the runtime check rejects them again before anything is sent.
|
|
80
|
+
- Reviews marked `incentivized` always carry a visible label.
|
|
81
|
+
- No plan checks in the package and nothing plan-related in the browser. Activities are never refused for plan reasons; a `ProofhornPlanLimitError` from the server entry (402 `plan_limit`, on create calls only) carries `upgradeUrl`, and `getSummary()` returns this month's usage for your own admin screens and logs (`plan.activitiesLimit` is `null` when there is no limit).
|
|
82
|
+
- Nothing is invented. An empty feed shows nothing, and an item with no reviews shows no stars.
|
|
83
|
+
|
|
84
|
+
## Scripts
|
|
85
|
+
|
|
86
|
+
| Command | Does |
|
|
87
|
+
| --- | --- |
|
|
88
|
+
| `npm test` | Unit tests and the core repo’s conformance suite, against a local mock of `/v1` |
|
|
89
|
+
| `npm run typecheck` | Includes the `recordActivity` type tests |
|
|
90
|
+
| `npm run size` | Measures the browser entry against the 50KB gzipped budget |
|
|
91
|
+
| `npm run verify:next` | Builds the sample app on Next 15 and 16 and proves the server entry is absent from browser output |
|
|
92
|
+
| `npm run mock` | Runs the local `/v1` mock on port 4010 for the sample app |
|
|
93
|
+
|
|
94
|
+
## Status
|
|
95
|
+
|
|
96
|
+
The widget runtime is the shared `@proofhorn/runtime` 1.0.0 from the core repo, vendored in `src/runtime/vendor/` until it is published to npm. It is the same code the Shopify theme extension and WordPress plugin ship.
|
|
97
|
+
|
|
98
|
+
MIT.
|
|
Binary file
|
|
Binary file
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { ProofhornProvider, ProofWidgets, type ProofhornProviderProps, type ProofWidgetsProps } from './provider.js';
|
|
2
|
+
export { StarRating, type StarRatingProps } from './star-rating.js';
|
|
3
|
+
export { getVisitor, consent } from './visitor.js';
|
|
4
|
+
export { createPublicClient, type PublicClient, type PublicClientOptions } from './shared/public-client.js';
|
|
5
|
+
export { ProofhornError } from './shared/http.js';
|
|
6
|
+
export type { Visitor } from './runtime/types.js';
|
|
7
|
+
export type * from './shared/types.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { ProofhornProvider, ProofWidgets } from './provider.js';
|
|
2
|
+
export { StarRating } from './star-rating.js';
|
|
3
|
+
export { getVisitor, consent } from './visitor.js';
|
|
4
|
+
export { createPublicClient } from './shared/public-client.js';
|
|
5
|
+
export { ProofhornError } from './shared/http.js';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import type { RuntimeOptions } from './runtime/types.js';
|
|
3
|
+
import type { Placement } from './shared/types.js';
|
|
4
|
+
export declare const ProofhornContext: import("react").Context<RuntimeOptions | null>;
|
|
5
|
+
export type ProofhornProviderProps = {
|
|
6
|
+
/** `NEXT_PUBLIC_PROOFHORN_KEY`. A key without the `pk_` prefix is refused and nothing loads. */
|
|
7
|
+
publicKey: string;
|
|
8
|
+
apiBase?: string;
|
|
9
|
+
children?: ReactNode;
|
|
10
|
+
};
|
|
11
|
+
/** Renders no DOM of its own, and loads the runtime only after hydration. */
|
|
12
|
+
export declare function ProofhornProvider({ publicKey, apiBase, children }: ProofhornProviderProps): import("react").JSX.Element;
|
|
13
|
+
export type ProofWidgetsProps = {
|
|
14
|
+
placement?: Placement;
|
|
15
|
+
itemKey?: string;
|
|
16
|
+
};
|
|
17
|
+
/** Popups are fixed-position, so this renders nothing into the page and cannot shift the layout. */
|
|
18
|
+
export declare function ProofWidgets({ placement, itemKey }: ProofWidgetsProps): null;
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { createContext, useContext, useEffect, useMemo } from 'react';
|
|
4
|
+
import { usePathname } from 'next/navigation.js';
|
|
5
|
+
import { loadRuntime } from './runtime/load.js';
|
|
6
|
+
export const ProofhornContext = createContext(null);
|
|
7
|
+
/** Renders no DOM of its own, and loads the runtime only after hydration. */
|
|
8
|
+
export function ProofhornProvider({ publicKey, apiBase, children }) {
|
|
9
|
+
const value = useMemo(() => {
|
|
10
|
+
if (publicKey?.startsWith('pk_'))
|
|
11
|
+
return { publicKey, apiBase };
|
|
12
|
+
console.error('[proofhorn] publicKey must be a pk_live_ or pk_test_ key. Proofhorn is disabled.');
|
|
13
|
+
return null;
|
|
14
|
+
}, [publicKey, apiBase]);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
if (value)
|
|
17
|
+
void loadRuntime(value);
|
|
18
|
+
}, [value]);
|
|
19
|
+
return _jsx(ProofhornContext.Provider, { value: value, children: children });
|
|
20
|
+
}
|
|
21
|
+
/** Popups are fixed-position, so this renders nothing into the page and cannot shift the layout. */
|
|
22
|
+
export function ProofWidgets({ placement = 'other', itemKey }) {
|
|
23
|
+
const context = useContext(ProofhornContext);
|
|
24
|
+
const pathname = usePathname();
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
if (!context)
|
|
27
|
+
return;
|
|
28
|
+
let unmount;
|
|
29
|
+
let active = true;
|
|
30
|
+
void loadRuntime(context).then((runtime) => {
|
|
31
|
+
if (active)
|
|
32
|
+
unmount = runtime?.mount({ placement, itemKey });
|
|
33
|
+
});
|
|
34
|
+
return () => {
|
|
35
|
+
active = false;
|
|
36
|
+
unmount?.();
|
|
37
|
+
};
|
|
38
|
+
}, [context, pathname, placement, itemKey]);
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { ProofhornRuntime, RuntimeOptions } from './types.js';
|
|
2
|
+
/** Starts the runtime once per page load. Resolves to null if it cannot load, and the page carries on without it. */
|
|
3
|
+
export declare function loadRuntime(options: RuntimeOptions): Promise<ProofhornRuntime | null>;
|
|
4
|
+
export declare function setPendingConsent(granted: boolean): void;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
let runtime;
|
|
2
|
+
let pendingConsent;
|
|
3
|
+
/** Starts the runtime once per page load. Resolves to null if it cannot load, and the page carries on without it. */
|
|
4
|
+
export function loadRuntime(options) {
|
|
5
|
+
// The shared runtime is vendored until @proofhorn/runtime is on npm (see vendor/README.md).
|
|
6
|
+
runtime ??= import('./vendor/proofhorn-runtime.js')
|
|
7
|
+
.then(({ createRuntime }) => {
|
|
8
|
+
const instance = createRuntime(options);
|
|
9
|
+
window.Proofhorn = instance;
|
|
10
|
+
if (pendingConsent !== undefined)
|
|
11
|
+
instance.consent(pendingConsent);
|
|
12
|
+
return instance;
|
|
13
|
+
})
|
|
14
|
+
.catch(() => null);
|
|
15
|
+
return runtime;
|
|
16
|
+
}
|
|
17
|
+
export function setPendingConsent(granted) {
|
|
18
|
+
pendingConsent = granted;
|
|
19
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Placement } from '../shared/types.js';
|
|
2
|
+
export type Visitor = {
|
|
3
|
+
visitorId: string | null;
|
|
4
|
+
sessionId: string | null;
|
|
5
|
+
referralCode: string | null;
|
|
6
|
+
};
|
|
7
|
+
export type RuntimeOptions = {
|
|
8
|
+
publicKey: string;
|
|
9
|
+
apiBase?: string | undefined;
|
|
10
|
+
};
|
|
11
|
+
export type MountOptions = {
|
|
12
|
+
placement: Placement;
|
|
13
|
+
itemKey?: string | undefined;
|
|
14
|
+
};
|
|
15
|
+
/** The surface of the shared widget runtime (`@proofhorn/runtime`) that the SDK uses. */
|
|
16
|
+
export interface ProofhornRuntime {
|
|
17
|
+
version: string;
|
|
18
|
+
getVisitor(): Visitor;
|
|
19
|
+
consent(granted: boolean): void;
|
|
20
|
+
/** Shows the widgets for this page, filling any `[data-proofhorn-widget]` elements. Returns a function that removes them. */
|
|
21
|
+
mount(options: MountOptions): () => void;
|
|
22
|
+
track(type: string, data?: {
|
|
23
|
+
variantId?: string;
|
|
24
|
+
itemKey?: string;
|
|
25
|
+
name?: string;
|
|
26
|
+
}): void;
|
|
27
|
+
onAddToCart(itemKey?: string): void;
|
|
28
|
+
}
|
|
29
|
+
declare global {
|
|
30
|
+
interface Window {
|
|
31
|
+
Proofhorn?: ProofhornRuntime;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Khushrav Ventures
|
|
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.
|