vendure-plugin-capjs 0.0.1 → 1.0.1

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.
Files changed (2) hide show
  1. package/README.md +173 -1
  2. package/package.json +11 -4
package/README.md CHANGED
@@ -1 +1,173 @@
1
- # Capjs plugin
1
+ # Capjs Plugin
2
+
3
+ A [Vendure](https://www.vendure.io/) plugin that integrates [Cap.js](https://capjs.js.org/) for privacy-focused, lightweight CAPTCHA protection. This plugin provides REST endpoints for challenge creation and redemption, along with a guard decorator for protecting GraphQL mutations.
4
+
5
+ ## Features
6
+
7
+ - **Privacy-focused CAPTCHA**: Uses Cap.js proof-of-work challenges instead of tracking-based solutions
8
+ - **GraphQL protection**: Easily protect any GraphQL mutation with a decorator
9
+ - **REST endpoints**: `/cap/challenge` and `/cap/redeem` endpoints for frontend integration
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ npm install vendure-plugin-capjs @cap.js/server
15
+ ```
16
+
17
+ ## Configuration
18
+
19
+ Add the plugin to your Vendure config:
20
+
21
+ ```typescript
22
+ import { VendureConfig } from '@vendure/core';
23
+ import { CapjsPlugin } from 'vendure-plugin-capjs';
24
+
25
+ export const config: VendureConfig = {
26
+ // ... other config
27
+ plugins: [
28
+ CapjsPlugin.init({}),
29
+ // ... other plugins
30
+ ],
31
+ };
32
+ ```
33
+
34
+ ## Database Migration
35
+
36
+ This plugin adds new entities (`CapjsChallenges` and `CapjsToken`) to your database. After adding the plugin to your config, you need to generate and run a database migration:
37
+
38
+ ```bash
39
+ # Generate the migration
40
+ npm run migration:generate capjs-plugin
41
+
42
+ # Run the migration
43
+ npm run migration:run
44
+ ```
45
+
46
+ Make sure your Vendure config has migrations enabled:
47
+
48
+ ```typescript
49
+ export const config: VendureConfig = {
50
+ // ... other config
51
+ dbConnectionOptions: {
52
+ // ... other options
53
+ migrations: [/* your migrations */],
54
+ migrationsRun: false, // Set to true to run migrations automatically on startup
55
+ },
56
+ };
57
+ ```
58
+
59
+ ## Usage
60
+
61
+ ### REST Endpoints
62
+
63
+ The plugin exposes two REST endpoints:
64
+
65
+ #### Create Challenge
66
+
67
+ ```
68
+ POST /cap/challenge
69
+ ```
70
+
71
+ Returns a new CAPTCHA challenge that the client must solve.
72
+
73
+ #### Redeem Challenge
74
+
75
+ ```
76
+ POST /cap/redeem
77
+ Content-Type: application/json
78
+
79
+ {
80
+ "token": "<challenge-token>",
81
+ "solutions": [<solution-numbers>]
82
+ }
83
+ ```
84
+
85
+ Validates the solved challenge and returns a token for subsequent API calls.
86
+
87
+ ### Protecting GraphQL Mutations
88
+
89
+ Use the `@CapjsValidate()` decorator to protect any GraphQL mutation:
90
+
91
+ ```typescript
92
+ import { Mutation, Resolver } from '@nestjs/graphql';
93
+ import { CapjsValidate } from 'vendure-plugin-capjs';
94
+
95
+ @Resolver()
96
+ export class MyResolver {
97
+ @Mutation()
98
+ @CapjsValidate()
99
+ async submitForm() {
100
+ // This mutation is protected by CAPTCHA
101
+ // Will throw if x-captcha-token header is missing or invalid
102
+ }
103
+ }
104
+ ```
105
+
106
+ ### Frontend Integration
107
+
108
+ The client must:
109
+
110
+ 1. Request a challenge from `POST /cap/challenge`
111
+ 2. Solve the proof-of-work challenge using the [@cap.js/widget](https://www.npmjs.com/package/@cap.js/widget) or [@cap.js/vanilla](https://www.npmjs.com/package/@cap.js/vanilla)
112
+ 3. Submit solutions to `POST /cap/redeem` to get a validation token
113
+ 4. Include the token in the `x-captcha-token` header for protected GraphQL mutations
114
+
115
+ Example with the Cap.js widget:
116
+
117
+ ```html
118
+ <!-- Load the Cap.js widget script -->
119
+ <script src="https://cdn.jsdelivr.net/npm/@cap.js/widget"></script>
120
+
121
+ <!-- Add the widget to your form -->
122
+ <cap-widget id="cap" data-cap-api-endpoint="/cap"></cap-widget>
123
+
124
+ <script>
125
+ const widget = document.querySelector('#cap');
126
+
127
+ widget.addEventListener('solve', function (e) {
128
+ const token = e.detail.token;
129
+ // Include token in your GraphQL request headers
130
+ // headers: { 'x-captcha-token': token }
131
+ });
132
+ </script>
133
+ ```
134
+
135
+ For invisible mode (no visible widget):
136
+
137
+ ```javascript
138
+ import Cap from '@cap.js/widget';
139
+
140
+ const cap = new Cap({
141
+ apiEndpoint: '/cap/',
142
+ });
143
+
144
+ const token = await cap.solve();
145
+ // Use token in x-captcha-token header
146
+ ```
147
+
148
+ ## API Reference
149
+
150
+ ### CapjsPlugin
151
+
152
+ ```typescript
153
+ CapjsPlugin.init(options: PluginInitOptions)
154
+ ```
155
+
156
+ Initializes the plugin with the given options.
157
+
158
+ ### CapjsValidate Decorator
159
+
160
+ ```typescript
161
+ @CapjsValidate()
162
+ ```
163
+
164
+ Decorator that applies the `CapjsGuard` to a resolver method. Validates the `x-captcha-token` header and throws a `UserInputError` if validation fails.
165
+
166
+ ### Error Codes
167
+
168
+ - `MISSING_CAPTCHA`: The `x-captcha-token` header was not provided
169
+ - `INVALID_CAPTCHA`: The provided token failed validation
170
+
171
+ ## License
172
+
173
+ MIT
package/package.json CHANGED
@@ -1,7 +1,9 @@
1
1
  {
2
2
  "name": "vendure-plugin-capjs",
3
- "version": "0.0.1",
4
- "license": "GPL-3.0-or-later",
3
+ "version": "1.0.1",
4
+ "description": "A Vendure plugin that integrates Cap.js for privacy-focused, lightweight CAPTCHA protection. Provides challenge/redeem endpoints and a guard decorator for protecting GraphQL mutations.",
5
+ "license": "MIT",
6
+ "private": false,
5
7
  "main": "lib/index.js",
6
8
  "types": "lib/index.d.ts",
7
9
  "files": [
@@ -16,7 +18,6 @@
16
18
  "lint": "eslint .",
17
19
  "test": "vitest --run"
18
20
  },
19
- "private": false,
20
21
  "peerDependencies": {
21
22
  "@cap.js/server": "^4.0.5",
22
23
  "@google-cloud/pubsub": ">=4.0.0",
@@ -33,5 +34,11 @@
33
34
  "rimraf": "^5.0.5",
34
35
  "supertest": "^7.2.2",
35
36
  "typescript": "5.8.2"
36
- }
37
+ },
38
+ "keywords": [
39
+ "vendure",
40
+ "vendure-plugin",
41
+ "capjs",
42
+ "captcha"
43
+ ]
37
44
  }