@vllnt/convex-invitations 0.1.0-canary.01d468e

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 (69) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +139 -0
  3. package/dist/client/index.d.ts +196 -0
  4. package/dist/client/index.d.ts.map +1 -0
  5. package/dist/client/index.js +181 -0
  6. package/dist/client/index.js.map +1 -0
  7. package/dist/client/types.d.ts +93 -0
  8. package/dist/client/types.d.ts.map +1 -0
  9. package/dist/client/types.js +3 -0
  10. package/dist/client/types.js.map +1 -0
  11. package/dist/component/_generated/api.d.ts +40 -0
  12. package/dist/component/_generated/api.d.ts.map +1 -0
  13. package/dist/component/_generated/api.js +31 -0
  14. package/dist/component/_generated/api.js.map +1 -0
  15. package/dist/component/_generated/component.d.ts +148 -0
  16. package/dist/component/_generated/component.d.ts.map +1 -0
  17. package/dist/component/_generated/component.js +11 -0
  18. package/dist/component/_generated/component.js.map +1 -0
  19. package/dist/component/_generated/dataModel.d.ts +46 -0
  20. package/dist/component/_generated/dataModel.d.ts.map +1 -0
  21. package/dist/component/_generated/dataModel.js +11 -0
  22. package/dist/component/_generated/dataModel.js.map +1 -0
  23. package/dist/component/_generated/server.d.ts +133 -0
  24. package/dist/component/_generated/server.d.ts.map +1 -0
  25. package/dist/component/_generated/server.js +80 -0
  26. package/dist/component/_generated/server.js.map +1 -0
  27. package/dist/component/convex.config.d.ts +3 -0
  28. package/dist/component/convex.config.d.ts.map +1 -0
  29. package/dist/component/convex.config.js +7 -0
  30. package/dist/component/convex.config.js.map +1 -0
  31. package/dist/component/crons.d.ts +19 -0
  32. package/dist/component/crons.d.ts.map +1 -0
  33. package/dist/component/crons.js +22 -0
  34. package/dist/component/crons.js.map +1 -0
  35. package/dist/component/mutations.d.ts +118 -0
  36. package/dist/component/mutations.d.ts.map +1 -0
  37. package/dist/component/mutations.js +272 -0
  38. package/dist/component/mutations.js.map +1 -0
  39. package/dist/component/queries.d.ts +98 -0
  40. package/dist/component/queries.d.ts.map +1 -0
  41. package/dist/component/queries.js +93 -0
  42. package/dist/component/queries.js.map +1 -0
  43. package/dist/component/schema.d.ts +56 -0
  44. package/dist/component/schema.d.ts.map +1 -0
  45. package/dist/component/schema.js +44 -0
  46. package/dist/component/schema.js.map +1 -0
  47. package/dist/component/validators.d.ts +49 -0
  48. package/dist/component/validators.d.ts.map +1 -0
  49. package/dist/component/validators.js +37 -0
  50. package/dist/component/validators.js.map +1 -0
  51. package/dist/shared.d.ts +33 -0
  52. package/dist/shared.d.ts.map +1 -0
  53. package/dist/shared.js +40 -0
  54. package/dist/shared.js.map +1 -0
  55. package/package.json +101 -0
  56. package/src/client/index.ts +357 -0
  57. package/src/client/types.ts +98 -0
  58. package/src/component/_generated/api.ts +56 -0
  59. package/src/component/_generated/component.ts +179 -0
  60. package/src/component/_generated/dataModel.ts +60 -0
  61. package/src/component/_generated/server.ts +169 -0
  62. package/src/component/convex.config.ts +9 -0
  63. package/src/component/crons.ts +26 -0
  64. package/src/component/mutations.ts +307 -0
  65. package/src/component/queries.ts +113 -0
  66. package/src/component/schema.ts +44 -0
  67. package/src/component/validators.ts +44 -0
  68. package/src/shared.ts +48 -0
  69. package/src/test.ts +12 -0
@@ -0,0 +1,44 @@
1
+ import { defineSchema, defineTable } from "convex/server";
2
+ import { v } from "convex/values";
3
+ import { invitationState, jsonValue } from "./validators";
4
+
5
+ /**
6
+ * Sandboxed table — the invitation's own concern. A `token` is a
7
+ * component-minted single-use secret that names one invitation; the host hands
8
+ * it to the invitee (out of band — email, link) and the invitee redeems it via
9
+ * `accept`. `resourceRef` is the opaque host resource the invite grants access
10
+ * to (an org, a team, a workspace); `role`/`payload` are opaque host data
11
+ * carried through to the host on accept. `state` tracks the lifecycle and
12
+ * `expiresAt` the TTL.
13
+ *
14
+ * Indexes serve token redemption/lookup (`by_token`), the pending-per-resource
15
+ * listing (`by_resource_state`), the TTL expiry sweep over open invites
16
+ * (`by_state_expires`), and the terminal retention sweep
17
+ * (`by_state_created`, oldest-first).
18
+ *
19
+ * The component holds no membership and no real secret store at this minimal
20
+ * stage — it models the invite self-contained. A later version would delegate
21
+ * the hashed/revocable token to `@vllnt/convex-tokens` and write the granted
22
+ * membership via `@vllnt/convex-memberships`; today the `token` lives here and
23
+ * acceptance returns the grant to the host to apply.
24
+ */
25
+ export default defineSchema({
26
+ invitations: defineTable({
27
+ token: v.string(),
28
+ resourceRef: v.string(),
29
+ role: v.optional(jsonValue),
30
+ inviterRef: v.optional(v.string()),
31
+ inviteeRef: v.optional(v.string()),
32
+ payload: v.optional(jsonValue),
33
+ state: invitationState,
34
+ createdAt: v.number(),
35
+ expiresAt: v.number(),
36
+ acceptedAt: v.optional(v.number()),
37
+ acceptedBy: v.optional(v.string()),
38
+ revokedAt: v.optional(v.number()),
39
+ })
40
+ .index("by_token", ["token"])
41
+ .index("by_resource_state", ["resourceRef", "state", "createdAt"])
42
+ .index("by_state_expires", ["state", "expiresAt"])
43
+ .index("by_state_created", ["state", "createdAt"]),
44
+ });
@@ -0,0 +1,44 @@
1
+ import { v } from "convex/values";
2
+
3
+ /**
4
+ * Opaque host-owned data stored on an invitation — its `role` and its
5
+ * `payload`. The component never inspects either; both are last-resort
6
+ * arbitrary data, aliased here rather than left bare in function signatures.
7
+ * The host narrows them at the {@link Invitations} client boundary via the
8
+ * optional `roleValidator` / `payloadValidator` parsers.
9
+ *
10
+ * This is the single documented `v.any()` escape hatch in the component; the
11
+ * lint rule `convex-rules/no-bare-v-any` is satisfied by routing every arbitrary
12
+ * host payload through this alias instead of a bare `v.any()`.
13
+ */
14
+ export const jsonValue = v.any();
15
+
16
+ /** The four lifecycle states an invitation moves through. */
17
+ export const invitationState = v.union(
18
+ v.literal("pending"),
19
+ v.literal("accepted"),
20
+ v.literal("revoked"),
21
+ v.literal("expired"),
22
+ );
23
+
24
+ /**
25
+ * Public projection of an invitation returned by {@link getByToken} and
26
+ * {@link listPending}. `resourceRef`, `inviterRef`, `inviteeRef`, `role`, and
27
+ * `payload` are opaque host data — the component stores and returns them without
28
+ * interpreting them. `token` is the single-use secret the host hands to the
29
+ * invitee; the host gates who may read it.
30
+ */
31
+ export const invitationView = v.object({
32
+ token: v.string(),
33
+ resourceRef: v.string(),
34
+ role: v.optional(jsonValue),
35
+ inviterRef: v.optional(v.string()),
36
+ inviteeRef: v.optional(v.string()),
37
+ payload: v.optional(jsonValue),
38
+ state: invitationState,
39
+ createdAt: v.number(),
40
+ expiresAt: v.number(),
41
+ acceptedAt: v.optional(v.number()),
42
+ acceptedBy: v.optional(v.string()),
43
+ revokedAt: v.optional(v.number()),
44
+ });
package/src/shared.ts ADDED
@@ -0,0 +1,48 @@
1
+ /** Shared constants used by both `client/` and `component/`. */
2
+
3
+ export const COMPONENT_NAME = "invitations";
4
+
5
+ /**
6
+ * The invitation lifecycle states. `pending` is the freshly-issued, still-open
7
+ * invite; it transitions exactly once into a terminal state — `accepted`
8
+ * (consumed, single-use), `revoked` (cancelled by the host), or `expired`
9
+ * (its TTL elapsed). Terminal states are final — a transition out of one is
10
+ * rejected.
11
+ */
12
+ export const INVITATION_STATES = [
13
+ "pending",
14
+ "accepted",
15
+ "revoked",
16
+ "expired",
17
+ ] as const;
18
+
19
+ /** A single invitation lifecycle state. */
20
+ export type InvitationState = (typeof INVITATION_STATES)[number];
21
+
22
+ /** The three terminal states — once reached, an invitation never transitions again. */
23
+ export const TERMINAL_STATES: ReadonlySet<InvitationState> = new Set([
24
+ "accepted",
25
+ "revoked",
26
+ "expired",
27
+ ]);
28
+
29
+ /**
30
+ * Default time-to-live (ms) for a freshly-issued invitation: 7 days. A
31
+ * `pending` invite whose `expiresAt` is in the past can no longer be accepted
32
+ * and is swept to `expired` by the prune cron. A host that wants a different
33
+ * window passes `ttlMs` to the {@link Invitations} client (per call or as the
34
+ * construction default), or an absolute `expiresAt` to `issue`.
35
+ */
36
+ export const DEFAULT_TTL_MS = 604_800_000;
37
+
38
+ /**
39
+ * Default retention (ms) for terminal invitations before the prune cron sweeps
40
+ * them: 30 days. Bounds unbounded growth of the `invitations` table while
41
+ * leaving a generous window for the host to audit accepted/revoked invites. A
42
+ * host that wants a different window drives `prune` from its own scheduler with
43
+ * an explicit `before` cutoff.
44
+ */
45
+ export const DEFAULT_RETENTION_MS = 2_592_000_000;
46
+
47
+ /** Default page size for a `prune` pass before the sweep self-reschedules. */
48
+ export const DEFAULT_PRUNE_BATCH = 200;
package/src/test.ts ADDED
@@ -0,0 +1,12 @@
1
+ import type { TestConvex } from "convex-test";
2
+ import schema from "./component/schema";
3
+
4
+ const modules = import.meta.glob("./component/**/*.ts");
5
+
6
+ /**
7
+ * Register this component with a `convex-test` instance so consuming apps can
8
+ * test integration: `import { register } from "@vllnt/convex-invitations/test"`.
9
+ */
10
+ export function register(t: TestConvex<typeof schema>, name = "invitations"): void {
11
+ t.registerComponent(name, schema, modules);
12
+ }