@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
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# Form handlers and payment webhooks
|
|
2
|
+
|
|
3
|
+
Both patterns below pass the browser's visitor IDs to the server, then call Proofhorn with the secret key. No provider SDK is included; plug in your own.
|
|
4
|
+
|
|
5
|
+
## Form handler (server action)
|
|
6
|
+
|
|
7
|
+
Client side, attach the IDs when the form is submitted:
|
|
8
|
+
|
|
9
|
+
```tsx
|
|
10
|
+
'use client';
|
|
11
|
+
import { getVisitor } from '@proofhorn/next';
|
|
12
|
+
import { enrol } from './actions';
|
|
13
|
+
|
|
14
|
+
export function EnrolForm() {
|
|
15
|
+
async function submit(form: FormData) {
|
|
16
|
+
const { visitorId, sessionId, referralCode } = getVisitor();
|
|
17
|
+
form.set('visitorId', visitorId ?? '');
|
|
18
|
+
form.set('sessionId', sessionId ?? '');
|
|
19
|
+
form.set('referralCode', referralCode ?? '');
|
|
20
|
+
await enrol(form);
|
|
21
|
+
}
|
|
22
|
+
return <form action={submit}>…</form>;
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Server side:
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
'use server';
|
|
30
|
+
import { ProofhornError, recordActivity } from '@proofhorn/next/server';
|
|
31
|
+
|
|
32
|
+
export async function enrol(form: FormData) {
|
|
33
|
+
const text = (name: string) => String(form.get(name) ?? '').trim() || undefined;
|
|
34
|
+
// Save the submission in your own system first; its ID is the natural key for the activity.
|
|
35
|
+
const submissionId = await saveSubmission(form);
|
|
36
|
+
try {
|
|
37
|
+
await recordActivity({
|
|
38
|
+
source: 'form',
|
|
39
|
+
externalId: submissionId,
|
|
40
|
+
type: 'lead',
|
|
41
|
+
occurredAt: new Date().toISOString(),
|
|
42
|
+
item: { key: 'course-101', type: 'course', title: 'Data Analytics Bootcamp' },
|
|
43
|
+
actor: { firstName: text('firstName'), city: text('city') },
|
|
44
|
+
publicDisplay: false, // count the conversion without showing a lead publicly
|
|
45
|
+
visitorId: text('visitorId'),
|
|
46
|
+
sessionId: text('sessionId'),
|
|
47
|
+
referralCode: text('referralCode'),
|
|
48
|
+
});
|
|
49
|
+
} catch (error) {
|
|
50
|
+
if (error instanceof ProofhornError && error.retryable) queueForRetry(submissionId);
|
|
51
|
+
else throw error;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Payment webhook (route handler)
|
|
57
|
+
|
|
58
|
+
Verify the provider's signature with the provider's own method, then record one activity per item with `{paymentId}:{itemKey}` as the external ID. Retrying returns `duplicate: true`, so a redelivered webhook is harmless.
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { recordActivity, requestReview, ProofhornError } from '@proofhorn/next/server';
|
|
62
|
+
|
|
63
|
+
export async function POST(request: Request) {
|
|
64
|
+
const raw = await request.text();
|
|
65
|
+
if (!verifyProviderSignature(raw, request.headers)) return new Response('Bad signature', { status: 401 });
|
|
66
|
+
const payment = JSON.parse(raw);
|
|
67
|
+
// The browser passed these to your checkout, and you stored them in the payment's metadata/notes.
|
|
68
|
+
const { visitorId, sessionId, referralCode, firstName, city, email } = payment.notes;
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
await recordActivity({
|
|
72
|
+
source: 'razorpay',
|
|
73
|
+
externalId: `${payment.id}:course-101`,
|
|
74
|
+
type: 'purchase',
|
|
75
|
+
occurredAt: new Date(payment.created_at * 1000).toISOString(),
|
|
76
|
+
item: { key: 'course-101', type: 'course', title: 'Data Analytics Bootcamp' },
|
|
77
|
+
actor: { firstName, city, countryCode: 'IN' },
|
|
78
|
+
customerRef: payment.customer_id,
|
|
79
|
+
value: { amountMinor: payment.amount, currency: payment.currency },
|
|
80
|
+
visitorId,
|
|
81
|
+
sessionId,
|
|
82
|
+
referralCode,
|
|
83
|
+
});
|
|
84
|
+
await requestReview({
|
|
85
|
+
source: 'razorpay',
|
|
86
|
+
externalId: `${payment.id}:course-101`,
|
|
87
|
+
itemKey: 'course-101',
|
|
88
|
+
customerRef: payment.customer_id,
|
|
89
|
+
email,
|
|
90
|
+
firstName,
|
|
91
|
+
});
|
|
92
|
+
} catch (error) {
|
|
93
|
+
// A 5xx makes the provider redeliver, which is what a retryable failure needs.
|
|
94
|
+
if (error instanceof ProofhornError && error.retryable) return new Response('Retry', { status: 503 });
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
97
|
+
return new Response(null, { status: 204 });
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
For a refund, call `createServerClient(...).cancelActivity('razorpay', externalId)`.
|
|
102
|
+
|
|
103
|
+
## Webhooks from Proofhorn
|
|
104
|
+
|
|
105
|
+
Proofhorn signs its webhooks (reward issued, review published, site disconnected). Read the raw body and verify before parsing:
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
import { verifyWebhook } from '@proofhorn/next/server';
|
|
109
|
+
|
|
110
|
+
export async function POST(request: Request) {
|
|
111
|
+
const raw = await request.text();
|
|
112
|
+
const ok = await verifyWebhook(raw, request.headers.get('Proofhorn-Signature'), process.env.PROOFHORN_WEBHOOK_SECRET!);
|
|
113
|
+
if (!ok) return new Response('Invalid signature', { status: 401 });
|
|
114
|
+
const event = JSON.parse(raw);
|
|
115
|
+
// Delivery is at least once: dedupe on event.id before acting.
|
|
116
|
+
return new Response(null, { status: 204 });
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Privacy
|
|
121
|
+
|
|
122
|
+
`eraseCustomer({ customerRef })` or `eraseCustomer({ email })` removes everything held on a person. The core runs it asynchronously.
|
package/package.json
CHANGED
|
@@ -1,6 +1,72 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@proofhorn/next",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Proofhorn reviews, social proof, A/B testing and referrals for Next.js.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Khushrav Ventures",
|
|
7
|
+
"homepage": "https://proofhorn.khushrav.com",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"proofhorn",
|
|
10
|
+
"nextjs",
|
|
11
|
+
"next",
|
|
12
|
+
"react",
|
|
13
|
+
"reviews",
|
|
14
|
+
"social-proof",
|
|
15
|
+
"ab-testing",
|
|
16
|
+
"referrals"
|
|
17
|
+
],
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"type": "module",
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./server": {
|
|
31
|
+
"types": "./dist/server/index.d.ts",
|
|
32
|
+
"default": "./dist/server/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./package.json": "./package.json"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"assets",
|
|
39
|
+
"docs",
|
|
40
|
+
"README.md",
|
|
41
|
+
"LICENSE"
|
|
42
|
+
],
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=20"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "node -e \"fs.rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.build.json && node scripts/copy-runtime.mjs",
|
|
48
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
49
|
+
"test": "vitest run",
|
|
50
|
+
"size": "node scripts/check-size.mjs",
|
|
51
|
+
"verify:next": "node scripts/verify-next.mjs",
|
|
52
|
+
"mock": "node mock/server.mjs",
|
|
53
|
+
"sync:runtime": "node scripts/sync-runtime.mjs",
|
|
54
|
+
"prepublishOnly": "npm run typecheck && npm test && npm run build && npm run size"
|
|
55
|
+
},
|
|
56
|
+
"peerDependencies": {
|
|
57
|
+
"next": "^15.0.0 || ^16.0.0",
|
|
58
|
+
"react": "^18.2.0 || ^19.0.0",
|
|
59
|
+
"react-dom": "^18.2.0 || ^19.0.0"
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@types/node": "^24.0.0",
|
|
63
|
+
"@types/react": "^19.0.0",
|
|
64
|
+
"@types/react-dom": "^19.0.0",
|
|
65
|
+
"jsdom": "^27.0.0",
|
|
66
|
+
"next": "^16.4.3",
|
|
67
|
+
"react": "^19.3.1",
|
|
68
|
+
"react-dom": "^19.3.1",
|
|
69
|
+
"typescript": "^5.9.0",
|
|
70
|
+
"vitest": "^5.0.5"
|
|
71
|
+
}
|
|
72
|
+
}
|