@vllnt/convex-reactions 0.1.0 → 0.1.1-canary.fbe719b
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 +105 -42
- package/dist/component/mutations.d.ts +6 -1
- package/dist/component/mutations.d.ts.map +1 -1
- package/dist/component/mutations.js +29 -4
- package/dist/component/mutations.js.map +1 -1
- package/dist/component/queries.js +2 -2
- package/dist/component/queries.js.map +1 -1
- package/package.json +24 -14
- package/src/component/mutations.ts +34 -4
- package/src/component/queries.ts +3 -3
- package/src/test.ts +3 -2
package/README.md
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
<!-- Badges -->
|
|
2
|
+
|
|
2
3
|
[](https://www.convex.dev/components)
|
|
3
4
|
[](https://www.npmjs.com/package/@vllnt/convex-reactions)
|
|
4
5
|
[](https://github.com/vllnt/convex-reactions/actions/workflows/ci.yml)
|
|
@@ -11,35 +12,53 @@ Reactions, votes, and likes on any resource, as a Convex component.
|
|
|
11
12
|
```ts
|
|
12
13
|
const reactions = new Reactions(components.reactions);
|
|
13
14
|
await reactions.react(ctx, authorRef, resourceRef, "up"); // toggle the edge in one transaction
|
|
14
|
-
await reactions.counts(ctx, resourceRef);
|
|
15
|
+
await reactions.counts(ctx, resourceRef); // [{ kind, count }], reactive
|
|
15
16
|
await reactions.hasReacted(ctx, authorRef, resourceRef, "up");
|
|
16
17
|
```
|
|
17
18
|
|
|
18
|
-
A reaction is the opaque edge `(authorRef, resourceRef, kind)`. `react` toggles
|
|
19
|
-
remove if present); `counts` tallies a resource per kind;
|
|
20
|
-
|
|
21
|
-
correct under
|
|
19
|
+
A reaction is the opaque edge `(authorRef, resourceRef, kind)`. `react` toggles
|
|
20
|
+
it (add if absent, remove if present); `counts` tallies a resource per kind;
|
|
21
|
+
`reactors` pages who reacted. One edge per subject per kind per resource is
|
|
22
|
+
enforced in the mutation transaction, so toggles and counts stay correct under
|
|
23
|
+
concurrency.
|
|
22
24
|
|
|
23
25
|
## Features
|
|
24
26
|
|
|
25
|
-
- **Toggle** — `react(authorRef, resourceRef, kind)` adds the edge if absent and
|
|
26
|
-
|
|
27
|
-
- **
|
|
28
|
-
|
|
29
|
-
- **
|
|
30
|
-
|
|
31
|
-
- **
|
|
32
|
-
|
|
27
|
+
- **Toggle** — `react(authorRef, resourceRef, kind)` adds the edge if absent and
|
|
28
|
+
removes it if present in one transaction, returning `{ reacted, action }`.
|
|
29
|
+
- **Idempotent remove** — `unreact(authorRef, resourceRef, kind)` deletes the
|
|
30
|
+
edge; removing an absent one is a safe no-op (`false`).
|
|
31
|
+
- **Count by kind** — `counts(resourceRef)` returns `[{ kind, count }, ...]`
|
|
32
|
+
sorted by kind; no reactions returns `[]`.
|
|
33
|
+
- **List reactors** — `reactors(resourceRef, kind, paginationOpts)` pages who
|
|
34
|
+
reacted with one kind, oldest first. `numItems` must be an integer from 1
|
|
35
|
+
through 1000 and `maximumRowsRead` is capped at 1000, including reactive
|
|
36
|
+
cursor ranges.
|
|
37
|
+
- **Per-subject state** — `hasReacted` and `myReactions` give the host the
|
|
38
|
+
subject's own reaction state for rendering controls.
|
|
39
|
+
- **Configurable vocabulary** — `allowedKinds` pins the reaction set
|
|
40
|
+
(`["up", "down"]`, a fixed emoji list); an unknown kind is rejected at the
|
|
41
|
+
boundary. Omit for freeform.
|
|
42
|
+
- **Server-sourced time** — `createdAt` is stamped from the server clock; a
|
|
43
|
+
caller can't supply a timestamp.
|
|
44
|
+
- **Mount-safe** — correct under multiple named `app.use` mounts; each instance
|
|
45
|
+
is an isolated sandbox.
|
|
33
46
|
|
|
34
47
|
## Installation
|
|
35
48
|
|
|
36
49
|
```bash
|
|
37
|
-
pnpm add @vllnt/convex-reactions
|
|
50
|
+
pnpm add @vllnt/convex-reactions convex@^1.45.0
|
|
38
51
|
```
|
|
39
52
|
|
|
40
|
-
Peer dependency: `convex@^1.45.0`.
|
|
53
|
+
Peer dependency: `convex@^1.45.0`. The command installs the latest stable
|
|
54
|
+
release; use `@vllnt/convex-reactions@canary` for the newest pre-release build
|
|
55
|
+
from `main`, and pin a resolved canary version for reproducible installs.
|
|
56
|
+
|
|
57
|
+
## Quick start
|
|
41
58
|
|
|
42
|
-
|
|
59
|
+
After mounting, run `pnpm exec convex dev` to generate `components.reactions`.
|
|
60
|
+
The example exports internal functions for trusted server callers. Public
|
|
61
|
+
wrappers must resolve identity and authorize resource access.
|
|
43
62
|
|
|
44
63
|
```ts
|
|
45
64
|
// convex/convex.config.ts
|
|
@@ -54,8 +73,7 @@ export default app;
|
|
|
54
73
|
```ts
|
|
55
74
|
// convex/reactions.ts — host owns auth; pass opaque refs in.
|
|
56
75
|
import { components } from "./_generated/api";
|
|
57
|
-
import {
|
|
58
|
-
import { paginationOptsValidator } from "convex/server";
|
|
76
|
+
import { internalMutation, internalQuery } from "./_generated/server";
|
|
59
77
|
import { v } from "convex/values";
|
|
60
78
|
import { Reactions } from "@vllnt/convex-reactions";
|
|
61
79
|
|
|
@@ -63,15 +81,13 @@ const reactions = new Reactions(components.reactions, {
|
|
|
63
81
|
allowedKinds: ["up", "down"], // pin the vocabulary (optional)
|
|
64
82
|
});
|
|
65
83
|
|
|
66
|
-
export const vote =
|
|
67
|
-
args: {
|
|
68
|
-
handler:
|
|
69
|
-
|
|
70
|
-
return reactions.react(ctx, userId, postId, kind);
|
|
71
|
-
},
|
|
84
|
+
export const vote = internalMutation({
|
|
85
|
+
args: { authorRef: v.string(), resourceRef: v.string(), kind: v.string() },
|
|
86
|
+
handler: (ctx, { authorRef, resourceRef, kind }) =>
|
|
87
|
+
reactions.react(ctx, authorRef, resourceRef, kind),
|
|
72
88
|
});
|
|
73
89
|
|
|
74
|
-
export const tally =
|
|
90
|
+
export const tally = internalQuery({
|
|
75
91
|
args: { postId: v.string() },
|
|
76
92
|
handler: (ctx, { postId }) => reactions.counts(ctx, postId),
|
|
77
93
|
});
|
|
@@ -79,28 +95,48 @@ export const tally = query({
|
|
|
79
95
|
|
|
80
96
|
## API Reference
|
|
81
97
|
|
|
82
|
-
| Method
|
|
83
|
-
|
|
84
|
-
| `react(ctx, authorRef, resourceRef, kind)`
|
|
85
|
-
| `unreact(ctx, authorRef, resourceRef, kind)`
|
|
86
|
-
| `counts(ctx, resourceRef)`
|
|
87
|
-
| `hasReacted(ctx, authorRef, resourceRef, kind)`
|
|
88
|
-
| `myReactions(ctx, authorRef, resourceRef)`
|
|
89
|
-
| `reactors(ctx, resourceRef, kind, paginationOpts)` | query
|
|
90
|
-
|
|
91
|
-
Invalid numeric pagination options throw `
|
|
98
|
+
| Method | Kind | Result |
|
|
99
|
+
| -------------------------------------------------- | -------- | ----------------------------------------------------------- |
|
|
100
|
+
| `react(ctx, authorRef, resourceRef, kind)` | mutation | `{ reacted, action }` (`action`: `"added" \| "removed"`) |
|
|
101
|
+
| `unreact(ctx, authorRef, resourceRef, kind)` | mutation | `boolean` (true if an edge was removed) |
|
|
102
|
+
| `counts(ctx, resourceRef)` | query | `{ kind, count }[]` (sorted by kind) |
|
|
103
|
+
| `hasReacted(ctx, authorRef, resourceRef, kind)` | query | `boolean` |
|
|
104
|
+
| `myReactions(ctx, authorRef, resourceRef)` | query | `string[]` (kinds the subject placed) |
|
|
105
|
+
| `reactors(ctx, resourceRef, kind, paginationOpts)` | query | `PaginationResult<ReactionView>`; bounded to 1000 rows read |
|
|
106
|
+
|
|
107
|
+
Invalid numeric pagination options throw coded `ConvexError`
|
|
108
|
+
`INVALID_PAGE_SIZE`; empty `authorRef`/`resourceRef` in `react`/`unreact` throw
|
|
109
|
+
`INVALID_REF` and an empty `kind` throws `INVALID_KIND`. Disallowed kinds in
|
|
110
|
+
`react`/`unreact` throw a plain client-side `Error`, not a component error code;
|
|
111
|
+
query methods do not apply the allowlist.
|
|
92
112
|
|
|
93
113
|
Full reference: [docs/API.md](docs/API.md).
|
|
94
114
|
|
|
95
|
-
##
|
|
115
|
+
## Options and limitations
|
|
116
|
+
|
|
117
|
+
- `allowedKinds?: readonly string[]` is the sole constructor option. Omit it for
|
|
118
|
+
freeform kinds; an empty list rejects every write kind.
|
|
119
|
+
- `counts` scans every edge on a resource and groups by kind; it is not a
|
|
120
|
+
constant-time aggregate and is subject to Convex read limits. `myReactions`
|
|
121
|
+
also collects its matching edges.
|
|
122
|
+
- `react` is a toggle, not an idempotent retry operation: calling twice removes
|
|
123
|
+
the edge. Use `unreact` to ensure absence.
|
|
124
|
+
- Mount another isolated instance with `app.use(reactions, { name: "votes" })`
|
|
125
|
+
and `new Reactions(components.votes)`.
|
|
96
126
|
|
|
97
|
-
|
|
127
|
+
## Integrations
|
|
128
|
+
|
|
129
|
+
Backend-only — no `./react` entry. A reaction tally or reactor list is an
|
|
130
|
+
ordinary reactive `useQuery` over the host's own re-exported `counts` /
|
|
131
|
+
`reactors` refs.
|
|
98
132
|
|
|
99
133
|
## Security
|
|
100
134
|
|
|
101
135
|
- Auth-agnostic — the host resolves identity and decides who may react.
|
|
102
|
-
- Tables sandboxed — reached only through the exported functions; never touches
|
|
103
|
-
|
|
136
|
+
- Tables sandboxed — reached only through the exported functions; never touches
|
|
137
|
+
host or sibling tables.
|
|
138
|
+
- Uniqueness is transactional + time is server-sourced; refs and `kind` stay
|
|
139
|
+
opaque to the component.
|
|
104
140
|
|
|
105
141
|
See [docs/API.md](docs/API.md).
|
|
106
142
|
|
|
@@ -111,7 +147,32 @@ pnpm test # single run
|
|
|
111
147
|
pnpm test:coverage # enforced 100% on covered files
|
|
112
148
|
```
|
|
113
149
|
|
|
114
|
-
Tests run against the real component runtime via `convex-test`
|
|
150
|
+
Tests run against the real component runtime via `convex-test`
|
|
151
|
+
(`@edge-runtime/vm`), not mocks.
|
|
152
|
+
|
|
153
|
+
### Testing your app
|
|
154
|
+
|
|
155
|
+
`@vllnt/convex-reactions/test` exports `register(t, name = "reactions")` to
|
|
156
|
+
mount the component in a `convex-test` instance. It needs `convex-test` and
|
|
157
|
+
`vitest` (with Vite's `import.meta.glob`) in the consuming app:
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
/// <reference types="vite/client" />
|
|
161
|
+
import { convexTest } from "convex-test";
|
|
162
|
+
import { register } from "@vllnt/convex-reactions/test";
|
|
163
|
+
import schema from "./schema";
|
|
164
|
+
|
|
165
|
+
const t = convexTest(schema, import.meta.glob("./**/*.ts"));
|
|
166
|
+
register(t);
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Documentation
|
|
170
|
+
|
|
171
|
+
`llms.txt` is the maintained discovery index. `pnpm generate:llms` checks its
|
|
172
|
+
heading and local links without generating a source bundle.
|
|
173
|
+
|
|
174
|
+
[API reference](docs/API.md) · [LLM index](llms.txt) ·
|
|
175
|
+
[Security policy](SECURITY.md)
|
|
115
176
|
|
|
116
177
|
## Contributing
|
|
117
178
|
|
|
@@ -119,9 +180,11 @@ See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
|
119
180
|
|
|
120
181
|
## Author
|
|
121
182
|
|
|
122
|
-
Built by [bntvllnt](https://github.com/bntvllnt) ·
|
|
183
|
+
Built by [bntvllnt](https://github.com/bntvllnt) ·
|
|
184
|
+
[bntvllnt.com](https://bntvllnt.com) · [X @bntvllnt](https://x.com/bntvllnt)
|
|
123
185
|
|
|
124
|
-
Part of the [@vllnt](https://github.com/vllnt) Convex component fleet —
|
|
186
|
+
Part of the [@vllnt](https://github.com/vllnt) Convex component fleet —
|
|
187
|
+
[vllnt.com](https://vllnt.com)
|
|
125
188
|
|
|
126
189
|
If this is useful, [sponsor the work](https://github.com/sponsors/bntvllnt).
|
|
127
190
|
|
|
@@ -9,6 +9,10 @@
|
|
|
9
9
|
* `createdAt` is stamped from the server clock (`Date.now()` inside the handler
|
|
10
10
|
* — never caller-supplied). `kind` vocabulary and any allowlist are the host's
|
|
11
11
|
* concern, enforced at the client boundary before this call.
|
|
12
|
+
*
|
|
13
|
+
* @throws `ConvexError({ code: "INVALID_REF" })` when `authorRef` or `resourceRef`
|
|
14
|
+
* is empty.
|
|
15
|
+
* @throws `ConvexError({ code: "INVALID_KIND" })` when `kind` is empty.
|
|
12
16
|
*/
|
|
13
17
|
export declare const react: import("convex/server").RegisteredMutation<"public", {
|
|
14
18
|
authorRef: string;
|
|
@@ -25,7 +29,8 @@ export declare const react: import("convex/server").RegisteredMutation<"public",
|
|
|
25
29
|
* Remove a subject's `(authorRef, resourceRef, kind)` reaction edge. Idempotent:
|
|
26
30
|
* removing an edge that does not exist is a no-op that returns `false`, so a
|
|
27
31
|
* duplicate or replayed `unreact` is safe. Returns `true` only when an edge was
|
|
28
|
-
* actually deleted.
|
|
32
|
+
* actually deleted. Throws the same `INVALID_REF` / `INVALID_KIND` errors as
|
|
33
|
+
* {@link react} for empty arguments.
|
|
29
34
|
*/
|
|
30
35
|
export declare const unreact: import("convex/server").RegisteredMutation<"public", {
|
|
31
36
|
authorRef: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mutations.d.ts","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"mutations.d.ts","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"AA2BA;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,KAAK;;;;;;YAqBqB,SAAS;;;YASZ,OAAO;GAEzC,CAAC;AAEH;;;;;;GAMG;AACH,eAAO,MAAM,OAAO;;;;oBAwBlB,CAAC"}
|
|
@@ -1,6 +1,24 @@
|
|
|
1
|
-
import { v } from "convex/values";
|
|
2
|
-
import { mutation } from "./_generated/server";
|
|
3
|
-
import { reactResult } from "./validators";
|
|
1
|
+
import { ConvexError, v } from "convex/values";
|
|
2
|
+
import { mutation } from "./_generated/server.js";
|
|
3
|
+
import { reactResult } from "./validators.js";
|
|
4
|
+
/**
|
|
5
|
+
* Reject empty refs and kind. An empty ref or kind is never a valid edge key and
|
|
6
|
+
* would silently create rows no real subject, resource, or kind can address.
|
|
7
|
+
*/
|
|
8
|
+
function assertEdgeKey(args) {
|
|
9
|
+
if (args.authorRef === "" || args.resourceRef === "") {
|
|
10
|
+
throw new ConvexError({
|
|
11
|
+
code: "INVALID_REF",
|
|
12
|
+
message: "authorRef and resourceRef must be non-empty",
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
if (args.kind === "") {
|
|
16
|
+
throw new ConvexError({
|
|
17
|
+
code: "INVALID_KIND",
|
|
18
|
+
message: "kind must be non-empty",
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
4
22
|
/**
|
|
5
23
|
* Toggle a reaction edge in one transaction. If the subject has no
|
|
6
24
|
* `(authorRef, resourceRef, kind)` edge it is inserted (`action: "added"`,
|
|
@@ -12,6 +30,10 @@ import { reactResult } from "./validators";
|
|
|
12
30
|
* `createdAt` is stamped from the server clock (`Date.now()` inside the handler
|
|
13
31
|
* — never caller-supplied). `kind` vocabulary and any allowlist are the host's
|
|
14
32
|
* concern, enforced at the client boundary before this call.
|
|
33
|
+
*
|
|
34
|
+
* @throws `ConvexError({ code: "INVALID_REF" })` when `authorRef` or `resourceRef`
|
|
35
|
+
* is empty.
|
|
36
|
+
* @throws `ConvexError({ code: "INVALID_KIND" })` when `kind` is empty.
|
|
15
37
|
*/
|
|
16
38
|
export const react = mutation({
|
|
17
39
|
args: {
|
|
@@ -21,6 +43,7 @@ export const react = mutation({
|
|
|
21
43
|
},
|
|
22
44
|
returns: reactResult,
|
|
23
45
|
handler: async (ctx, args) => {
|
|
46
|
+
assertEdgeKey(args);
|
|
24
47
|
const existing = await ctx.db
|
|
25
48
|
.query("reactions")
|
|
26
49
|
.withIndex("by_author_resource_kind", (q) => q
|
|
@@ -45,7 +68,8 @@ export const react = mutation({
|
|
|
45
68
|
* Remove a subject's `(authorRef, resourceRef, kind)` reaction edge. Idempotent:
|
|
46
69
|
* removing an edge that does not exist is a no-op that returns `false`, so a
|
|
47
70
|
* duplicate or replayed `unreact` is safe. Returns `true` only when an edge was
|
|
48
|
-
* actually deleted.
|
|
71
|
+
* actually deleted. Throws the same `INVALID_REF` / `INVALID_KIND` errors as
|
|
72
|
+
* {@link react} for empty arguments.
|
|
49
73
|
*/
|
|
50
74
|
export const unreact = mutation({
|
|
51
75
|
args: {
|
|
@@ -55,6 +79,7 @@ export const unreact = mutation({
|
|
|
55
79
|
},
|
|
56
80
|
returns: v.boolean(),
|
|
57
81
|
handler: async (ctx, args) => {
|
|
82
|
+
assertEdgeKey(args);
|
|
58
83
|
const existing = await ctx.db
|
|
59
84
|
.query("reactions")
|
|
60
85
|
.withIndex("by_author_resource_kind", (q) => q
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mutations.js","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"mutations.js","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAE9C;;;GAGG;AACH,SAAS,aAAa,CAAC,IAItB;IACC,IAAI,IAAI,CAAC,SAAS,KAAK,EAAE,IAAI,IAAI,CAAC,WAAW,KAAK,EAAE,EAAE,CAAC;QACrD,MAAM,IAAI,WAAW,CAAC;YACpB,IAAI,EAAE,aAAa;YACnB,OAAO,EAAE,6CAA6C;SACvD,CAAC,CAAC;IACL,CAAC;IACD,IAAI,IAAI,CAAC,IAAI,KAAK,EAAE,EAAE,CAAC;QACrB,MAAM,IAAI,WAAW,CAAC;YACpB,IAAI,EAAE,cAAc;YACpB,OAAO,EAAE,wBAAwB;SAClC,CAAC,CAAC;IACL,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,QAAQ,CAAC;IAC5B,IAAI,EAAE;QAC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J,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;QACrB,WAAW,EAAE,CAAC,CAAC,MAAM,EAAE;QACvB,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;KACjB;IACD,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE;IACpB,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QAC3B,aAAa,CAAC,IAAI,CAAC,CAAC;QACpB,MAAM,QAAQ,GAAG,MAAM,GAAG,CAAC,EAAE;aAC1B,KAAK,CAAC,WAAW,CAAC;aAClB,SAAS,CAAC,yBAAyB,EAAE,CAAC,CAAC,EAAE,EAAE,CAC1C,CAAC;aACE,EAAE,CAAC,WAAW,EAAE,IAAI,CAAC,SAAS,CAAC;aAC/B,EAAE,CAAC,aAAa,EAAE,IAAI,CAAC,WAAW,CAAC;aACnC,EAAE,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,CAAC,CACzB;aACA,MAAM,EAAE,CAAC;QACZ,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;YACtB,OAAO,KAAK,CAAC;QACf,CAAC;QACD,MAAM,GAAG,CAAC,EAAE,CAAC,MAAM,CAAC,WAAW,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC;QAC/C,OAAO,IAAI,CAAC;IACd,CAAC;CACF,CAAC,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ConvexError, v } from "convex/values";
|
|
2
2
|
import { paginationOptsValidator } from "convex/server";
|
|
3
|
-
import { query } from "./_generated/server";
|
|
4
|
-
import { kindCount, reactionView } from "./validators";
|
|
3
|
+
import { query } from "./_generated/server.js";
|
|
4
|
+
import { kindCount, reactionView } from "./validators.js";
|
|
5
5
|
const MAX_PAGE_SIZE = 1000;
|
|
6
6
|
function boundedPaginationOptions(paginationOpts) {
|
|
7
7
|
const { numItems, maximumRowsRead } = paginationOpts;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queries.js","sourceRoot":"","sources":["../../src/component/queries.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AAExD,OAAO,EAAE,KAAK,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"queries.js","sourceRoot":"","sources":["../../src/component/queries.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AAExD,OAAO,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAG1D,MAAM,aAAa,GAAG,IAAI,CAAC;AAE3B,SAAS,wBAAwB,CAC/B,cAAiC;IAEjC,MAAM,EAAE,QAAQ,EAAE,eAAe,EAAE,GAAG,cAAc,CAAC;IACrD,IACE,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC;QAC1B,CAAC,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC;QAC3B,QAAQ,GAAG,CAAC;QACZ,QAAQ,GAAG,aAAa,EACxB,CAAC;QACD,MAAM,IAAI,WAAW,CAAC;YACpB,IAAI,EAAE,mBAAmB;YACzB,OAAO,EAAE,4DAA4D,aAAa,EAAE;SACrF,CAAC,CAAC;IACL,CAAC;IACD,IACE,eAAe,KAAK,SAAS;QAC7B,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,eAAe,CAAC;YAChC,CAAC,MAAM,CAAC,SAAS,CAAC,eAAe,CAAC;YAClC,eAAe,GAAG,CAAC,CAAC,EACtB,CAAC;QACD,MAAM,IAAI,WAAW,CAAC;YACpB,IAAI,EAAE,mBAAmB;YACzB,OAAO,EAAE,kEAAkE;SAC5E,CAAC,CAAC;IACL,CAAC;IACD,OAAO;QACL,GAAG,cAAc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c,CAAC,CAAC;QACrE,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,EAAE;aACxB,KAAK,CAAC,WAAW,CAAC;aAClB,SAAS,CAAC,kBAAkB,EAAE,CAAC,CAAC,EAAE,EAAE,CACnC,CAAC,CAAC,EAAE,CAAC,aAAa,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC,EAAE,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,CAAC,CAC5D;aACA,KAAK,CAAC,KAAK,CAAC;aACZ,QAAQ,CAAC,cAAc,CAAC,CAAC;QAC5B,OAAO,EAAE,GAAG,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;IACpD,CAAC;CACF,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vllnt/convex-reactions",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1-canary.fbe719b",
|
|
4
4
|
"description": "Reactions, votes, and likes on any resource — toggle, count by kind, and list reactors as a Convex component",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"packageManager": "pnpm@
|
|
7
|
+
"packageManager": "pnpm@10.34.8",
|
|
8
8
|
"files": [
|
|
9
9
|
"dist",
|
|
10
10
|
"src"
|
|
@@ -37,17 +37,14 @@
|
|
|
37
37
|
"build": "tsc --project ./tsconfig.build.json",
|
|
38
38
|
"build:codegen": "pnpm codegen && pnpm build",
|
|
39
39
|
"build:clean": "rm -rf dist *.tsbuildinfo && pnpm build:codegen",
|
|
40
|
-
"typecheck": "tsc --noEmit",
|
|
41
|
-
"typecheck:ci": "tsc --noEmit --project tsconfig.ci.json",
|
|
42
|
-
"lint": "pnpm check:agents && eslint .",
|
|
40
|
+
"typecheck": "tsc --noEmit --incremental --tsBuildInfoFile node_modules/.cache/tsc/typecheck.tsbuildinfo",
|
|
41
|
+
"typecheck:ci": "tsc --noEmit --incremental --tsBuildInfoFile node_modules/.cache/tsc/typecheck-ci.tsbuildinfo --project tsconfig.ci.json",
|
|
42
|
+
"lint": "pnpm check:agents && eslint --cache --cache-location node_modules/.cache/eslint/ .",
|
|
43
43
|
"test": "vitest run --passWithNoTests",
|
|
44
44
|
"test:watch": "vitest --typecheck --clearScreen false",
|
|
45
45
|
"test:coverage": "vitest run --coverage --coverage.reporter=text",
|
|
46
46
|
"generate:llms": "node scripts/generate-llms.mjs",
|
|
47
47
|
"preversion": "pnpm install --frozen-lockfile && pnpm build && pnpm test:coverage && pnpm typecheck && pnpm generate:llms",
|
|
48
|
-
"prepublishOnly": "npm whoami || npm login",
|
|
49
|
-
"alpha": "npm version prerelease --preid alpha && npm publish --tag alpha && git push --follow-tags",
|
|
50
|
-
"release": "npm version patch && npm publish && git push --follow-tags",
|
|
51
48
|
"check:agents": "node -e \"const f=require('node:fs');if(!f.existsSync('AGENTS.md')||f.existsSync('CLAUDE.md')||f.existsSync('.claude'))process.exit(1)\"",
|
|
52
49
|
"codegen": "pnpm convex codegen --component-dir ./src/component"
|
|
53
50
|
},
|
|
@@ -57,16 +54,29 @@
|
|
|
57
54
|
"devDependencies": {
|
|
58
55
|
"@edge-runtime/vm": "^5.0.0",
|
|
59
56
|
"@typescript/native": "npm:typescript@7.0.2",
|
|
60
|
-
"@vitest/coverage-v8": "^
|
|
57
|
+
"@vitest/coverage-v8": "^5.0.5",
|
|
61
58
|
"@vllnt/eslint-config": "2.0.0",
|
|
62
59
|
"@vllnt/typescript": "2.0.0",
|
|
63
|
-
"convex": "^1.
|
|
64
|
-
"convex-test": "^0.0.
|
|
60
|
+
"convex": "^1.48.0",
|
|
61
|
+
"convex-test": "^0.0.65",
|
|
65
62
|
"eslint": "^9.39.5",
|
|
66
|
-
"prettier": "^3.9.
|
|
63
|
+
"prettier": "^3.9.10",
|
|
67
64
|
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
68
|
-
"vite": "^8.
|
|
69
|
-
"vitest": "^
|
|
65
|
+
"vite": "^8.3.5",
|
|
66
|
+
"vitest": "^5.0.5"
|
|
67
|
+
},
|
|
68
|
+
"pnpm": {
|
|
69
|
+
"overrides": {
|
|
70
|
+
"deepmerge-ts@<8.0.0": "^8.0.2",
|
|
71
|
+
"handlebars@<4.7.10": "^4.7.10"
|
|
72
|
+
},
|
|
73
|
+
"minimumReleaseAge": 10080,
|
|
74
|
+
"minimumReleaseAgeExclude": [
|
|
75
|
+
"@vllnt/*"
|
|
76
|
+
],
|
|
77
|
+
"ignoredBuiltDependencies": [
|
|
78
|
+
"esbuild"
|
|
79
|
+
]
|
|
70
80
|
},
|
|
71
81
|
"homepage": "https://github.com/vllnt/convex-reactions#readme",
|
|
72
82
|
"bugs": {
|
|
@@ -1,6 +1,29 @@
|
|
|
1
|
-
import { v } from "convex/values";
|
|
2
|
-
import { mutation } from "./_generated/server";
|
|
3
|
-
import { reactResult } from "./validators";
|
|
1
|
+
import { ConvexError, v } from "convex/values";
|
|
2
|
+
import { mutation } from "./_generated/server.js";
|
|
3
|
+
import { reactResult } from "./validators.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Reject empty refs and kind. An empty ref or kind is never a valid edge key and
|
|
7
|
+
* would silently create rows no real subject, resource, or kind can address.
|
|
8
|
+
*/
|
|
9
|
+
function assertEdgeKey(args: {
|
|
10
|
+
authorRef: string;
|
|
11
|
+
resourceRef: string;
|
|
12
|
+
kind: string;
|
|
13
|
+
}): void {
|
|
14
|
+
if (args.authorRef === "" || args.resourceRef === "") {
|
|
15
|
+
throw new ConvexError({
|
|
16
|
+
code: "INVALID_REF",
|
|
17
|
+
message: "authorRef and resourceRef must be non-empty",
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
if (args.kind === "") {
|
|
21
|
+
throw new ConvexError({
|
|
22
|
+
code: "INVALID_KIND",
|
|
23
|
+
message: "kind must be non-empty",
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
4
27
|
|
|
5
28
|
/**
|
|
6
29
|
* Toggle a reaction edge in one transaction. If the subject has no
|
|
@@ -13,6 +36,10 @@ import { reactResult } from "./validators";
|
|
|
13
36
|
* `createdAt` is stamped from the server clock (`Date.now()` inside the handler
|
|
14
37
|
* — never caller-supplied). `kind` vocabulary and any allowlist are the host's
|
|
15
38
|
* concern, enforced at the client boundary before this call.
|
|
39
|
+
*
|
|
40
|
+
* @throws `ConvexError({ code: "INVALID_REF" })` when `authorRef` or `resourceRef`
|
|
41
|
+
* is empty.
|
|
42
|
+
* @throws `ConvexError({ code: "INVALID_KIND" })` when `kind` is empty.
|
|
16
43
|
*/
|
|
17
44
|
export const react = mutation({
|
|
18
45
|
args: {
|
|
@@ -22,6 +49,7 @@ export const react = mutation({
|
|
|
22
49
|
},
|
|
23
50
|
returns: reactResult,
|
|
24
51
|
handler: async (ctx, args) => {
|
|
52
|
+
assertEdgeKey(args);
|
|
25
53
|
const existing = await ctx.db
|
|
26
54
|
.query("reactions")
|
|
27
55
|
.withIndex("by_author_resource_kind", (q) =>
|
|
@@ -51,7 +79,8 @@ export const react = mutation({
|
|
|
51
79
|
* Remove a subject's `(authorRef, resourceRef, kind)` reaction edge. Idempotent:
|
|
52
80
|
* removing an edge that does not exist is a no-op that returns `false`, so a
|
|
53
81
|
* duplicate or replayed `unreact` is safe. Returns `true` only when an edge was
|
|
54
|
-
* actually deleted.
|
|
82
|
+
* actually deleted. Throws the same `INVALID_REF` / `INVALID_KIND` errors as
|
|
83
|
+
* {@link react} for empty arguments.
|
|
55
84
|
*/
|
|
56
85
|
export const unreact = mutation({
|
|
57
86
|
args: {
|
|
@@ -61,6 +90,7 @@ export const unreact = mutation({
|
|
|
61
90
|
},
|
|
62
91
|
returns: v.boolean(),
|
|
63
92
|
handler: async (ctx, args) => {
|
|
93
|
+
assertEdgeKey(args);
|
|
64
94
|
const existing = await ctx.db
|
|
65
95
|
.query("reactions")
|
|
66
96
|
.withIndex("by_author_resource_kind", (q) =>
|
package/src/component/queries.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { ConvexError, v } from "convex/values";
|
|
2
2
|
import { paginationOptsValidator } from "convex/server";
|
|
3
3
|
import type { PaginationOptions } from "convex/server";
|
|
4
|
-
import { query } from "./_generated/server";
|
|
5
|
-
import { kindCount, reactionView } from "./validators";
|
|
6
|
-
import type { Doc } from "./_generated/dataModel";
|
|
4
|
+
import { query } from "./_generated/server.js";
|
|
5
|
+
import { kindCount, reactionView } from "./validators.js";
|
|
6
|
+
import type { Doc } from "./_generated/dataModel.js";
|
|
7
7
|
|
|
8
8
|
const MAX_PAGE_SIZE = 1000;
|
|
9
9
|
|
package/src/test.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
/// <reference types="vite/client" />
|
|
1
2
|
import type { TestConvex } from "convex-test";
|
|
2
|
-
import schema from "./component/schema";
|
|
3
|
-
import { COMPONENT_NAME } from "./shared";
|
|
3
|
+
import schema from "./component/schema.js";
|
|
4
|
+
import { COMPONENT_NAME } from "./shared.js";
|
|
4
5
|
|
|
5
6
|
const modules = import.meta.glob("./component/**/*.ts");
|
|
6
7
|
|