@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.
- package/LICENSE +21 -0
- package/README.md +139 -0
- package/dist/client/index.d.ts +196 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +181 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/types.d.ts +93 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +3 -0
- package/dist/client/types.js.map +1 -0
- package/dist/component/_generated/api.d.ts +40 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +148 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +133 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +80 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +7 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/crons.d.ts +19 -0
- package/dist/component/crons.d.ts.map +1 -0
- package/dist/component/crons.js +22 -0
- package/dist/component/crons.js.map +1 -0
- package/dist/component/mutations.d.ts +118 -0
- package/dist/component/mutations.d.ts.map +1 -0
- package/dist/component/mutations.js +272 -0
- package/dist/component/mutations.js.map +1 -0
- package/dist/component/queries.d.ts +98 -0
- package/dist/component/queries.d.ts.map +1 -0
- package/dist/component/queries.js +93 -0
- package/dist/component/queries.js.map +1 -0
- package/dist/component/schema.d.ts +56 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +44 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/validators.d.ts +49 -0
- package/dist/component/validators.d.ts.map +1 -0
- package/dist/component/validators.js +37 -0
- package/dist/component/validators.js.map +1 -0
- package/dist/shared.d.ts +33 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +40 -0
- package/dist/shared.js.map +1 -0
- package/package.json +101 -0
- package/src/client/index.ts +357 -0
- package/src/client/types.ts +98 -0
- package/src/component/_generated/api.ts +56 -0
- package/src/component/_generated/component.ts +179 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +169 -0
- package/src/component/convex.config.ts +9 -0
- package/src/component/crons.ts +26 -0
- package/src/component/mutations.ts +307 -0
- package/src/component/queries.ts +113 -0
- package/src/component/schema.ts +44 -0
- package/src/component/validators.ts +44 -0
- package/src/shared.ts +48 -0
- package/src/test.ts +12 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 bntvllnt
|
|
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
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
<!-- Badges -->
|
|
2
|
+
[](https://www.convex.dev/components)
|
|
3
|
+
[](https://www.npmjs.com/package/@vllnt/convex-invitations)
|
|
4
|
+
[](https://github.com/vllnt/convex-invitations/actions/workflows/ci.yml)
|
|
5
|
+
[](./LICENSE)
|
|
6
|
+
|
|
7
|
+
# @vllnt/convex-invitations
|
|
8
|
+
|
|
9
|
+
The invite → accept → expire flow, as a Convex component.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
const invites = new Invitations(components.invitations);
|
|
13
|
+
const { token } = await invites.issue(ctx, orgId, { role: "member" }); // host delivers token
|
|
14
|
+
const grant = await invites.accept(ctx, token, userId); // single-use; host applies grant
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
A host mutation issues a single-use, expiring invitation to an opaque resource and
|
|
18
|
+
gets back a token to deliver out of band; the invitee redeems it with `accept`,
|
|
19
|
+
which consumes the invite and returns the grant the host applies. Domain-neutral: a
|
|
20
|
+
game guild invite, a SaaS workspace invite, a publication contributor invite.
|
|
21
|
+
|
|
22
|
+
## Features
|
|
23
|
+
|
|
24
|
+
- **Issue-and-deliver** — `issue` inserts a `pending` invite, returns a single-use `token` + `expiresAt`; the host delivers it (never sends email).
|
|
25
|
+
- **Single-use accept** — `accept` consumes the invite and returns the grant (`resourceRef` + opaque `role`/`payload`) the host applies.
|
|
26
|
+
- **Terminal states are final** — `accepted`/`revoked`/`expired` are terminal; a replayed link can never double-grant.
|
|
27
|
+
- **TTL + expiry** — every invite carries an `expiresAt`; accepting past-TTL is rejected; a daily cron and `peek` sweep stale invites.
|
|
28
|
+
- **Revoke** — `revoke` cancels a still-`pending` invite; a revoke racing an accept yields exactly one winner.
|
|
29
|
+
- **Poll or subscribe** — `getByToken`, `listPending`, and `listByResourceState` page invites reactively in Convex.
|
|
30
|
+
- **Typed, opaque host data** — `Invitations<TRole, TPayload>` with optional `roleValidator`/`payloadValidator` at the boundary; server-sourced time.
|
|
31
|
+
- **Bounded prune + cron, mount-safe** — a daily cron expires + prunes in idempotent batches; correct under multiple named mounts.
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pnpm add @vllnt/convex-invitations
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Peer dependency: `convex@^1.41.0`.
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
// convex/convex.config.ts
|
|
45
|
+
import { defineApp } from "convex/server";
|
|
46
|
+
import invitations from "@vllnt/convex-invitations/convex.config";
|
|
47
|
+
|
|
48
|
+
const app = defineApp();
|
|
49
|
+
app.use(invitations);
|
|
50
|
+
export default app;
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
// convex/invites.ts — host owns auth, the resource meaning, and delivery.
|
|
55
|
+
import { components } from "./_generated/api";
|
|
56
|
+
import { mutation } from "./_generated/server";
|
|
57
|
+
import { v } from "convex/values";
|
|
58
|
+
import { Invitations } from "@vllnt/convex-invitations";
|
|
59
|
+
|
|
60
|
+
const invites = new Invitations<"admin" | "member", never>(components.invitations, {
|
|
61
|
+
ttlMs: 1000 * 60 * 60 * 24 * 3, // 3-day links
|
|
62
|
+
roleValidator: v.union(v.literal("admin"), v.literal("member")).parse,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// Issue: record the invite, return the token to deliver.
|
|
66
|
+
export const invite = mutation({
|
|
67
|
+
args: { orgId: v.string(), role: v.union(v.literal("admin"), v.literal("member")) },
|
|
68
|
+
handler: async (ctx, { orgId, role }) => {
|
|
69
|
+
const { token } = await invites.issue(ctx, orgId, { role }); // host gates the caller first
|
|
70
|
+
return { token }; // host emails `https://app/join?token=${token}`
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// Accept: consume the invite, apply the grant (write your own membership).
|
|
75
|
+
export const join = mutation({
|
|
76
|
+
args: { token: v.string() },
|
|
77
|
+
handler: async (ctx, { token }) => {
|
|
78
|
+
const me = await currentUser(ctx); // host resolves identity
|
|
79
|
+
const grant = await invites.accept(ctx, token, me);
|
|
80
|
+
await ctx.db.insert("memberships", { orgId: grant.resourceRef, userId: me, role: grant.role });
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Client options: `new Invitations(component, { ttlMs?, generateToken?, roleValidator?, payloadValidator? })`.
|
|
86
|
+
|
|
87
|
+
## API Reference
|
|
88
|
+
|
|
89
|
+
| Method | Kind | Result |
|
|
90
|
+
|--------|------|--------|
|
|
91
|
+
| `issue(ctx, resourceRef, opts?)` | mutation | `{ token, expiresAt }` (`opts`: `{ role?; inviterRef?; inviteeRef?; payload?; ttlMs?; expiresAt? }`) |
|
|
92
|
+
| `accept(ctx, token, acceptedBy)` | mutation | `InvitationGrant` (`{ resourceRef; role?; payload? }`) |
|
|
93
|
+
| `revoke(ctx, token)` | mutation | `null` |
|
|
94
|
+
| `peek(ctx, token)` | mutation | `InvitationView \| null` (read-time TTL enforcement) |
|
|
95
|
+
| `getByToken(ctx, token)` | query | `InvitationView \| null` (pure reactive read) |
|
|
96
|
+
| `listPending(ctx, resourceRef, paginationOpts)` | query | `PaginationResult<InvitationView>` |
|
|
97
|
+
| `listByResourceState(ctx, resourceRef, state, paginationOpts)` | query | `PaginationResult<InvitationView>` |
|
|
98
|
+
| `prune(ctx, opts?)` | mutation | `number` (invites touched in the first bounded pass) |
|
|
99
|
+
|
|
100
|
+
Full reference: [docs/API.md](docs/API.md).
|
|
101
|
+
|
|
102
|
+
## React
|
|
103
|
+
|
|
104
|
+
Backend-only — no `./react` entry. An invite-management surface is an ordinary
|
|
105
|
+
reactive `useQuery` over the host's own re-exported `getByToken` / `listPending`
|
|
106
|
+
refs; the token is delivered by the host, never revealed in a client component.
|
|
107
|
+
|
|
108
|
+
## Security
|
|
109
|
+
|
|
110
|
+
- **Auth-agnostic** — the host authenticates the caller, decides who may issue/accept/revoke, and owns token delivery; tables are sandboxed.
|
|
111
|
+
- **Single-use + server-sourced time** — terminal states are final (no double-grant); TTL is enforced on read; timestamps come from `Date.now()`, never the caller.
|
|
112
|
+
- **Opaque host data** — `resourceRef`/`role`/`payload` are opaque, narrowed only by the optional host validators at the client boundary.
|
|
113
|
+
|
|
114
|
+
See [docs/API.md](docs/API.md).
|
|
115
|
+
|
|
116
|
+
## Testing
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
pnpm test # single run
|
|
120
|
+
pnpm test:coverage # enforced 100% on covered files
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Tests run against the real component runtime via `convex-test` (`@edge-runtime/vm`), not mocks.
|
|
124
|
+
|
|
125
|
+
## Contributing
|
|
126
|
+
|
|
127
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
128
|
+
|
|
129
|
+
## Author
|
|
130
|
+
|
|
131
|
+
Built by [bntvllnt](https://github.com/bntvllnt) · [bntvllnt.com](https://bntvllnt.com) · [X @bntvllnt](https://x.com/bntvllnt)
|
|
132
|
+
|
|
133
|
+
Part of the [@vllnt](https://github.com/vllnt) Convex component fleet — [vllnt.com](https://vllnt.com)
|
|
134
|
+
|
|
135
|
+
If this is useful, [sponsor the work](https://github.com/sponsors/bntvllnt).
|
|
136
|
+
|
|
137
|
+
## License
|
|
138
|
+
|
|
139
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import type { FunctionArgs, FunctionReference, FunctionReturnType, PaginationOptions, PaginationResult } from "convex/server";
|
|
2
|
+
import type { InvitationGrant, InvitationState, InvitationView, InvitationsOptions, IssueOptions, Parser } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* The component's raw invitation view, before the client narrows opaque host
|
|
5
|
+
* data. `role` and `payload` are `unknown` here; the {@link Invitations} client
|
|
6
|
+
* runs the host validators over them at its typed boundary.
|
|
7
|
+
*/
|
|
8
|
+
type RawView = {
|
|
9
|
+
token: string;
|
|
10
|
+
resourceRef: string;
|
|
11
|
+
role?: unknown;
|
|
12
|
+
inviterRef?: string;
|
|
13
|
+
inviteeRef?: string;
|
|
14
|
+
payload?: unknown;
|
|
15
|
+
state: InvitationState;
|
|
16
|
+
createdAt: number;
|
|
17
|
+
expiresAt: number;
|
|
18
|
+
acceptedAt?: number;
|
|
19
|
+
acceptedBy?: string;
|
|
20
|
+
revokedAt?: number;
|
|
21
|
+
};
|
|
22
|
+
/** The component's raw accept grant, before the client narrows opaque host data. */
|
|
23
|
+
type RawGrant = {
|
|
24
|
+
resourceRef: string;
|
|
25
|
+
role?: unknown;
|
|
26
|
+
payload?: unknown;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* The invitations component's function references, as exposed on the host via
|
|
30
|
+
* `components.invitations`. The host's stored `role`/`payload` are opaque here
|
|
31
|
+
* (`unknown`); the {@link Invitations} client narrows them at its own typed
|
|
32
|
+
* boundary.
|
|
33
|
+
*/
|
|
34
|
+
export interface InvitationsComponent {
|
|
35
|
+
mutations: {
|
|
36
|
+
issue: FunctionReference<"mutation", "internal", {
|
|
37
|
+
token: string;
|
|
38
|
+
resourceRef: string;
|
|
39
|
+
role?: unknown;
|
|
40
|
+
inviterRef?: string;
|
|
41
|
+
inviteeRef?: string;
|
|
42
|
+
payload?: unknown;
|
|
43
|
+
expiresAt: number;
|
|
44
|
+
}, {
|
|
45
|
+
token: string;
|
|
46
|
+
expiresAt: number;
|
|
47
|
+
}>;
|
|
48
|
+
accept: FunctionReference<"mutation", "internal", {
|
|
49
|
+
token: string;
|
|
50
|
+
acceptedBy: string;
|
|
51
|
+
}, RawGrant>;
|
|
52
|
+
revoke: FunctionReference<"mutation", "internal", {
|
|
53
|
+
token: string;
|
|
54
|
+
}, null>;
|
|
55
|
+
peek: FunctionReference<"mutation", "internal", {
|
|
56
|
+
token: string;
|
|
57
|
+
}, RawView | null>;
|
|
58
|
+
prune: FunctionReference<"mutation", "internal", {
|
|
59
|
+
before?: number;
|
|
60
|
+
retentionBefore?: number;
|
|
61
|
+
batch: number;
|
|
62
|
+
}, number>;
|
|
63
|
+
};
|
|
64
|
+
queries: {
|
|
65
|
+
getByToken: FunctionReference<"query", "internal", {
|
|
66
|
+
token: string;
|
|
67
|
+
}, RawView | null>;
|
|
68
|
+
listPending: FunctionReference<"query", "internal", {
|
|
69
|
+
resourceRef: string;
|
|
70
|
+
paginationOpts: PaginationOptions;
|
|
71
|
+
}, PaginationResult<RawView>>;
|
|
72
|
+
listByResourceState: FunctionReference<"query", "internal", {
|
|
73
|
+
resourceRef: string;
|
|
74
|
+
state: InvitationState;
|
|
75
|
+
paginationOpts: PaginationOptions;
|
|
76
|
+
}, PaginationResult<RawView>>;
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
interface RunQueryCtx {
|
|
80
|
+
runQuery<Q extends FunctionReference<"query", "internal">>(reference: Q, args: FunctionArgs<Q>): Promise<FunctionReturnType<Q>>;
|
|
81
|
+
}
|
|
82
|
+
interface RunMutationCtx {
|
|
83
|
+
runMutation<M extends FunctionReference<"mutation", "internal">>(reference: M, args: FunctionArgs<M>): Promise<FunctionReturnType<M>>;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Consumer-facing client for the invitation flow — issue → accept → expire. A
|
|
87
|
+
* host mutation `issue`s an invitation to an opaque `resourceRef` (an org, a
|
|
88
|
+
* team, a workspace) and gets back a single-use `token` to hand to the invitee
|
|
89
|
+
* out of band (an email, a link); the invitee redeems it with `accept`, which
|
|
90
|
+
* consumes the invite (single-use) and returns the grant the host applies. A
|
|
91
|
+
* still-open invite can be `revoke`d, and an unaccepted invite `expire`s when
|
|
92
|
+
* its TTL elapses (enforced on read and by a daily cron). The host owns meaning
|
|
93
|
+
* and auth — it resolves identity, decides who may issue/accept, and passes
|
|
94
|
+
* opaque `resourceRef`/`inviterRef`/`inviteeRef` refs plus arbitrary
|
|
95
|
+
* `role`/`payload` data the component stores without inspecting. Pass
|
|
96
|
+
* `roleValidator` / `payloadValidator` to narrow that opaque data to
|
|
97
|
+
* `TRole` / `TPayload` at the boundary — there is no unchecked cast.
|
|
98
|
+
*
|
|
99
|
+
* At this minimal stage the invite (including its token) is modelled
|
|
100
|
+
* self-contained; a later version would delegate the hashed/revocable token to
|
|
101
|
+
* `@vllnt/convex-tokens` and write the granted membership via
|
|
102
|
+
* `@vllnt/convex-memberships`, with `accept` composing the two.
|
|
103
|
+
*
|
|
104
|
+
* @typeParam TRole - The host's role type to grant on accept (defaults to `unknown`).
|
|
105
|
+
* @typeParam TPayload - The host's opaque invite payload type (defaults to `unknown`).
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```ts
|
|
109
|
+
* const invites = new Invitations(components.invitations, {
|
|
110
|
+
* ttlMs: 1000 * 60 * 60 * 24 * 3, // 3-day links
|
|
111
|
+
* roleValidator: v.union(v.literal("admin"), v.literal("member")).parse,
|
|
112
|
+
* });
|
|
113
|
+
* const { token } = await invites.issue(ctx, "org_42", { role: "member", inviterRef: me });
|
|
114
|
+
* // ... invitee later redeems the link:
|
|
115
|
+
* const grant = await invites.accept(ctx, token, invitee);
|
|
116
|
+
* // host applies grant.resourceRef + grant.role (writes a membership)
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
export declare class Invitations<TRole = unknown, TPayload = unknown> {
|
|
120
|
+
private readonly component;
|
|
121
|
+
private readonly ttlMs;
|
|
122
|
+
private readonly generateToken;
|
|
123
|
+
private readonly roleValidator;
|
|
124
|
+
private readonly payloadValidator;
|
|
125
|
+
constructor(component: InvitationsComponent, options?: InvitationsOptions<TRole, TPayload>);
|
|
126
|
+
/** Narrow an opaque value through a host parser; pass `undefined` and unset parsers through. */
|
|
127
|
+
private parse;
|
|
128
|
+
/** Project a raw component view into the typed, validated client view. */
|
|
129
|
+
private view;
|
|
130
|
+
/** Project a raw accept grant into the typed, validated grant. */
|
|
131
|
+
private grant;
|
|
132
|
+
/**
|
|
133
|
+
* Issue an invitation to `resourceRef` and return the single-use `token` (to
|
|
134
|
+
* hand to the invitee out of band) plus its absolute `expiresAt`. The token is
|
|
135
|
+
* minted by `generateToken` (a UUID by default). Expiry resolves to
|
|
136
|
+
* `opts.expiresAt` if given, else `now + (opts.ttlMs ?? the client default)`.
|
|
137
|
+
* `role`/`payload` are validated against the host validators before storage.
|
|
138
|
+
*/
|
|
139
|
+
issue(ctx: RunMutationCtx, resourceRef: string, opts?: IssueOptions<TRole, TPayload>): Promise<{
|
|
140
|
+
token: string;
|
|
141
|
+
expiresAt: number;
|
|
142
|
+
}>;
|
|
143
|
+
/**
|
|
144
|
+
* Redeem an invitation by its `token` for the opaque `acceptedBy` subject —
|
|
145
|
+
* the single-use accept. Consumes the invite (transitions it to `accepted`)
|
|
146
|
+
* and returns the grant the host applies (`resourceRef` + narrowed
|
|
147
|
+
* `role`/`payload`). Rejects a missing, expired, or already-terminal invite
|
|
148
|
+
* (the component throws a coded `ConvexError`); two racing accepts yield
|
|
149
|
+
* exactly one winner.
|
|
150
|
+
*/
|
|
151
|
+
accept(ctx: RunMutationCtx, token: string, acceptedBy: string): Promise<InvitationGrant<TRole, TPayload>>;
|
|
152
|
+
/** Revoke a still-`pending` invitation by its `token`. Rejects an already-terminal invite. */
|
|
153
|
+
revoke(ctx: RunMutationCtx, token: string): Promise<null>;
|
|
154
|
+
/**
|
|
155
|
+
* The current view of one invitation by its `token`, or `null` if no such
|
|
156
|
+
* invite is held — with read-time TTL enforcement: a `pending` invite found
|
|
157
|
+
* past its `expiresAt` is flipped to `expired` and returned as such. Runs as a
|
|
158
|
+
* mutation (it may write that one transition); use {@link getByToken} for a
|
|
159
|
+
* pure reactive read.
|
|
160
|
+
*/
|
|
161
|
+
peek(ctx: RunMutationCtx, token: string): Promise<InvitationView<TRole, TPayload> | null>;
|
|
162
|
+
/**
|
|
163
|
+
* Sweep stale `pending` invites to `expired` and delete terminal invites past
|
|
164
|
+
* retention, in bounded batches oldest-first. `before` (the TTL cutoff) and
|
|
165
|
+
* `retentionBefore` (the deletion cutoff) default to the server clock; `batch`
|
|
166
|
+
* caps each pass and the sweep self-reschedules until the tail is clean.
|
|
167
|
+
* Returns the count touched in the first pass. The built-in daily cron drives
|
|
168
|
+
* this automatically.
|
|
169
|
+
*/
|
|
170
|
+
prune(ctx: RunMutationCtx, opts?: {
|
|
171
|
+
before?: number;
|
|
172
|
+
retentionBefore?: number;
|
|
173
|
+
batch?: number;
|
|
174
|
+
}): Promise<number>;
|
|
175
|
+
/**
|
|
176
|
+
* The current view of one invitation by its `token`, or `null` — a pure
|
|
177
|
+
* reactive read (no write). Reports the stored `state` as-is, so a past-TTL
|
|
178
|
+
* invite may still read `pending` until a `peek`/`accept`/cron sweep flips it;
|
|
179
|
+
* compare `expiresAt` against the clock, or use {@link peek} for read-time TTL.
|
|
180
|
+
*/
|
|
181
|
+
getByToken(ctx: RunQueryCtx, token: string): Promise<InvitationView<TRole, TPayload> | null>;
|
|
182
|
+
/**
|
|
183
|
+
* Page the still-`pending` invitations for one `resourceRef`, oldest first.
|
|
184
|
+
* Returns the standard Convex pagination envelope with each row narrowed to
|
|
185
|
+
* the typed view.
|
|
186
|
+
*/
|
|
187
|
+
listPending(ctx: RunQueryCtx, resourceRef: string, paginationOpts: PaginationOptions): Promise<PaginationResult<InvitationView<TRole, TPayload>>>;
|
|
188
|
+
/**
|
|
189
|
+
* Page invitations for one `resourceRef` in a given `state`, oldest first —
|
|
190
|
+
* for an accepted/revoked/expired audit surface. Returns the standard Convex
|
|
191
|
+
* pagination envelope with each row narrowed to the typed view.
|
|
192
|
+
*/
|
|
193
|
+
listByResourceState(ctx: RunQueryCtx, resourceRef: string, state: InvitationState, paginationOpts: PaginationOptions): Promise<PaginationResult<InvitationView<TRole, TPayload>>>;
|
|
194
|
+
}
|
|
195
|
+
export type { InvitationGrant, InvitationState, InvitationView, InvitationsOptions, IssueOptions, Parser, };
|
|
196
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,EACjB,gBAAgB,EACjB,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EACV,eAAe,EACf,eAAe,EACf,cAAc,EACd,kBAAkB,EAClB,YAAY,EACZ,MAAM,EACP,MAAM,YAAY,CAAC;AAGpB;;;;GAIG;AACH,KAAK,OAAO,GAAG;IACb,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,KAAK,EAAE,eAAe,CAAC;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,oFAAoF;AACpF,KAAK,QAAQ,GAAG;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,OAAO,CAAC;IAAC,OAAO,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAE3E;;;;;GAKG;AACH,MAAM,WAAW,oBAAoB;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AM5D;IAED;;;;OAIG;IACG,mBAAmB,CACvB,GAAG,EAAE,WAAW,EAChB,WAAW,EAAE,MAAM,EACnB,KAAK,EAAE,eAAe,EACtB,cAAc,EAAE,iBAAiB,GAChC,OAAO,CAAC,gBAAgB,CAAC,cAAc,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,CAM5D;CACF;AAED,YAAY,EACV,eAAe,EACf,eAAe,EACf,cAAc,EACd,kBAAkB,EAClB,YAAY,EACZ,MAAM,GACP,CAAC"}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { DEFAULT_PRUNE_BATCH, DEFAULT_TTL_MS } from "../shared.js";
|
|
2
|
+
/**
|
|
3
|
+
* Consumer-facing client for the invitation flow — issue → accept → expire. A
|
|
4
|
+
* host mutation `issue`s an invitation to an opaque `resourceRef` (an org, a
|
|
5
|
+
* team, a workspace) and gets back a single-use `token` to hand to the invitee
|
|
6
|
+
* out of band (an email, a link); the invitee redeems it with `accept`, which
|
|
7
|
+
* consumes the invite (single-use) and returns the grant the host applies. A
|
|
8
|
+
* still-open invite can be `revoke`d, and an unaccepted invite `expire`s when
|
|
9
|
+
* its TTL elapses (enforced on read and by a daily cron). The host owns meaning
|
|
10
|
+
* and auth — it resolves identity, decides who may issue/accept, and passes
|
|
11
|
+
* opaque `resourceRef`/`inviterRef`/`inviteeRef` refs plus arbitrary
|
|
12
|
+
* `role`/`payload` data the component stores without inspecting. Pass
|
|
13
|
+
* `roleValidator` / `payloadValidator` to narrow that opaque data to
|
|
14
|
+
* `TRole` / `TPayload` at the boundary — there is no unchecked cast.
|
|
15
|
+
*
|
|
16
|
+
* At this minimal stage the invite (including its token) is modelled
|
|
17
|
+
* self-contained; a later version would delegate the hashed/revocable token to
|
|
18
|
+
* `@vllnt/convex-tokens` and write the granted membership via
|
|
19
|
+
* `@vllnt/convex-memberships`, with `accept` composing the two.
|
|
20
|
+
*
|
|
21
|
+
* @typeParam TRole - The host's role type to grant on accept (defaults to `unknown`).
|
|
22
|
+
* @typeParam TPayload - The host's opaque invite payload type (defaults to `unknown`).
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const invites = new Invitations(components.invitations, {
|
|
27
|
+
* ttlMs: 1000 * 60 * 60 * 24 * 3, // 3-day links
|
|
28
|
+
* roleValidator: v.union(v.literal("admin"), v.literal("member")).parse,
|
|
29
|
+
* });
|
|
30
|
+
* const { token } = await invites.issue(ctx, "org_42", { role: "member", inviterRef: me });
|
|
31
|
+
* // ... invitee later redeems the link:
|
|
32
|
+
* const grant = await invites.accept(ctx, token, invitee);
|
|
33
|
+
* // host applies grant.resourceRef + grant.role (writes a membership)
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export class Invitations {
|
|
37
|
+
component;
|
|
38
|
+
ttlMs;
|
|
39
|
+
generateToken;
|
|
40
|
+
roleValidator;
|
|
41
|
+
payloadValidator;
|
|
42
|
+
constructor(component, options = {}) {
|
|
43
|
+
this.component = component;
|
|
44
|
+
this.ttlMs = options.ttlMs ?? DEFAULT_TTL_MS;
|
|
45
|
+
this.generateToken = options.generateToken ?? (() => crypto.randomUUID());
|
|
46
|
+
this.roleValidator = options.roleValidator;
|
|
47
|
+
this.payloadValidator = options.payloadValidator;
|
|
48
|
+
}
|
|
49
|
+
/** Narrow an opaque value through a host parser; pass `undefined` and unset parsers through. */
|
|
50
|
+
parse(value, parser) {
|
|
51
|
+
if (value === undefined) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
if (parser === undefined) {
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
return parser(value);
|
|
58
|
+
}
|
|
59
|
+
/** Project a raw component view into the typed, validated client view. */
|
|
60
|
+
view(raw) {
|
|
61
|
+
return {
|
|
62
|
+
token: raw.token,
|
|
63
|
+
resourceRef: raw.resourceRef,
|
|
64
|
+
role: this.parse(raw.role, this.roleValidator),
|
|
65
|
+
inviterRef: raw.inviterRef,
|
|
66
|
+
inviteeRef: raw.inviteeRef,
|
|
67
|
+
payload: this.parse(raw.payload, this.payloadValidator),
|
|
68
|
+
state: raw.state,
|
|
69
|
+
createdAt: raw.createdAt,
|
|
70
|
+
expiresAt: raw.expiresAt,
|
|
71
|
+
acceptedAt: raw.acceptedAt,
|
|
72
|
+
acceptedBy: raw.acceptedBy,
|
|
73
|
+
revokedAt: raw.revokedAt,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/** Project a raw accept grant into the typed, validated grant. */
|
|
77
|
+
grant(raw) {
|
|
78
|
+
return {
|
|
79
|
+
resourceRef: raw.resourceRef,
|
|
80
|
+
role: this.parse(raw.role, this.roleValidator),
|
|
81
|
+
payload: this.parse(raw.payload, this.payloadValidator),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Issue an invitation to `resourceRef` and return the single-use `token` (to
|
|
86
|
+
* hand to the invitee out of band) plus its absolute `expiresAt`. The token is
|
|
87
|
+
* minted by `generateToken` (a UUID by default). Expiry resolves to
|
|
88
|
+
* `opts.expiresAt` if given, else `now + (opts.ttlMs ?? the client default)`.
|
|
89
|
+
* `role`/`payload` are validated against the host validators before storage.
|
|
90
|
+
*/
|
|
91
|
+
async issue(ctx, resourceRef, opts = {}) {
|
|
92
|
+
const token = this.generateToken();
|
|
93
|
+
const expiresAt = opts.expiresAt ?? Date.now() + (opts.ttlMs ?? this.ttlMs);
|
|
94
|
+
return ctx.runMutation(this.component.mutations.issue, {
|
|
95
|
+
token,
|
|
96
|
+
resourceRef,
|
|
97
|
+
role: this.parse(opts.role, this.roleValidator),
|
|
98
|
+
inviterRef: opts.inviterRef,
|
|
99
|
+
inviteeRef: opts.inviteeRef,
|
|
100
|
+
payload: this.parse(opts.payload, this.payloadValidator),
|
|
101
|
+
expiresAt,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Redeem an invitation by its `token` for the opaque `acceptedBy` subject —
|
|
106
|
+
* the single-use accept. Consumes the invite (transitions it to `accepted`)
|
|
107
|
+
* and returns the grant the host applies (`resourceRef` + narrowed
|
|
108
|
+
* `role`/`payload`). Rejects a missing, expired, or already-terminal invite
|
|
109
|
+
* (the component throws a coded `ConvexError`); two racing accepts yield
|
|
110
|
+
* exactly one winner.
|
|
111
|
+
*/
|
|
112
|
+
async accept(ctx, token, acceptedBy) {
|
|
113
|
+
const raw = await ctx.runMutation(this.component.mutations.accept, {
|
|
114
|
+
token,
|
|
115
|
+
acceptedBy,
|
|
116
|
+
});
|
|
117
|
+
return this.grant(raw);
|
|
118
|
+
}
|
|
119
|
+
/** Revoke a still-`pending` invitation by its `token`. Rejects an already-terminal invite. */
|
|
120
|
+
revoke(ctx, token) {
|
|
121
|
+
return ctx.runMutation(this.component.mutations.revoke, { token });
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* The current view of one invitation by its `token`, or `null` if no such
|
|
125
|
+
* invite is held — with read-time TTL enforcement: a `pending` invite found
|
|
126
|
+
* past its `expiresAt` is flipped to `expired` and returned as such. Runs as a
|
|
127
|
+
* mutation (it may write that one transition); use {@link getByToken} for a
|
|
128
|
+
* pure reactive read.
|
|
129
|
+
*/
|
|
130
|
+
async peek(ctx, token) {
|
|
131
|
+
const raw = await ctx.runMutation(this.component.mutations.peek, { token });
|
|
132
|
+
return raw === null ? null : this.view(raw);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Sweep stale `pending` invites to `expired` and delete terminal invites past
|
|
136
|
+
* retention, in bounded batches oldest-first. `before` (the TTL cutoff) and
|
|
137
|
+
* `retentionBefore` (the deletion cutoff) default to the server clock; `batch`
|
|
138
|
+
* caps each pass and the sweep self-reschedules until the tail is clean.
|
|
139
|
+
* Returns the count touched in the first pass. The built-in daily cron drives
|
|
140
|
+
* this automatically.
|
|
141
|
+
*/
|
|
142
|
+
prune(ctx, opts = {}) {
|
|
143
|
+
return ctx.runMutation(this.component.mutations.prune, {
|
|
144
|
+
before: opts.before,
|
|
145
|
+
retentionBefore: opts.retentionBefore,
|
|
146
|
+
batch: opts.batch ?? DEFAULT_PRUNE_BATCH,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* The current view of one invitation by its `token`, or `null` — a pure
|
|
151
|
+
* reactive read (no write). Reports the stored `state` as-is, so a past-TTL
|
|
152
|
+
* invite may still read `pending` until a `peek`/`accept`/cron sweep flips it;
|
|
153
|
+
* compare `expiresAt` against the clock, or use {@link peek} for read-time TTL.
|
|
154
|
+
*/
|
|
155
|
+
async getByToken(ctx, token) {
|
|
156
|
+
const raw = await ctx.runQuery(this.component.queries.getByToken, { token });
|
|
157
|
+
return raw === null ? null : this.view(raw);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Page the still-`pending` invitations for one `resourceRef`, oldest first.
|
|
161
|
+
* Returns the standard Convex pagination envelope with each row narrowed to
|
|
162
|
+
* the typed view.
|
|
163
|
+
*/
|
|
164
|
+
async listPending(ctx, resourceRef, paginationOpts) {
|
|
165
|
+
const result = await ctx.runQuery(this.component.queries.listPending, {
|
|
166
|
+
resourceRef,
|
|
167
|
+
paginationOpts,
|
|
168
|
+
});
|
|
169
|
+
return { ...result, page: result.page.map((raw) => this.view(raw)) };
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Page invitations for one `resourceRef` in a given `state`, oldest first —
|
|
173
|
+
* for an accepted/revoked/expired audit surface. Returns the standard Convex
|
|
174
|
+
* pagination envelope with each row narrowed to the typed view.
|
|
175
|
+
*/
|
|
176
|
+
async listByResourceState(ctx, resourceRef, state, paginationOpts) {
|
|
177
|
+
const result = await ctx.runQuery(this.component.queries.listByResourceState, { resourceRef, state, paginationOpts });
|
|
178
|
+
return { ...result, page: result.page.map((raw) => this.view(raw)) };
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,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,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QAC7E,OAAO,GAAG,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC9C,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,WAAW,CACf,GAAgB,EAChB,WAAmB,EACnB,cAAiC;QAEjC,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,WAAW,EAAE;YACpE,WAAW;YACX,cAAc;SACf,CAAC,CAAC;QACH,OAAO,EAAE,GAAG,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;IACvE,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,mBAAmB,CACvB,GAAgB,EAChB,WAAmB,EACnB,KAAsB,EACtB,cAAiC;QAEjC,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,QAAQ,CAC/B,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,mBAAmB,EAC1C,EAAE,WAAW,EAAE,KAAK,EAAE,cAAc,EAAE,CACvC,CAAC;QACF,OAAO,EAAE,GAAG,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;IACvE,CAAC;CACF"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/** Public TypeScript surface for the invitations client. */
|
|
2
|
+
/** The four lifecycle states an invitation moves through. */
|
|
3
|
+
export type InvitationState = "pending" | "accepted" | "revoked" | "expired";
|
|
4
|
+
/**
|
|
5
|
+
* Validates and narrows an opaque stored value to a host type `T` at the client
|
|
6
|
+
* boundary. Receives the raw value the component returned (`unknown`) and MUST
|
|
7
|
+
* return a typed `T` or throw. A `convex/values` validator's `.parse` (or a Zod
|
|
8
|
+
* `.parse`) fits directly; omit it to keep the value unvalidated.
|
|
9
|
+
*
|
|
10
|
+
* @typeParam T - The host's stored type (invitation `role` or `payload`).
|
|
11
|
+
*/
|
|
12
|
+
export type Parser<T> = (value: unknown) => T;
|
|
13
|
+
/** The public view of one invitation returned by {@link Invitations.getByToken}. */
|
|
14
|
+
export interface InvitationView<TRole = unknown, TPayload = unknown> {
|
|
15
|
+
/** The single-use secret naming this invitation. */
|
|
16
|
+
token: string;
|
|
17
|
+
/** The opaque host resource the invite grants access to (an org/team/workspace). */
|
|
18
|
+
resourceRef: string;
|
|
19
|
+
/** The opaque host role to grant on accept (narrowed if a `roleValidator` is set). */
|
|
20
|
+
role?: TRole;
|
|
21
|
+
/** The opaque host subject who issued the invite. */
|
|
22
|
+
inviterRef?: string;
|
|
23
|
+
/** The opaque host subject the invite is addressed to (open link when omitted). */
|
|
24
|
+
inviteeRef?: string;
|
|
25
|
+
/** Arbitrary opaque host data carried through to accept (narrowed if a `payloadValidator` is set). */
|
|
26
|
+
payload?: TPayload;
|
|
27
|
+
/** The current lifecycle state. */
|
|
28
|
+
state: InvitationState;
|
|
29
|
+
/** Absolute ms timestamp the invite was issued. */
|
|
30
|
+
createdAt: number;
|
|
31
|
+
/** Absolute ms timestamp the invite stops being acceptable. */
|
|
32
|
+
expiresAt: number;
|
|
33
|
+
/** Absolute ms timestamp of acceptance, present once `accepted`. */
|
|
34
|
+
acceptedAt?: number;
|
|
35
|
+
/** The opaque host subject who accepted, present once `accepted`. */
|
|
36
|
+
acceptedBy?: string;
|
|
37
|
+
/** Absolute ms timestamp of revocation, present once `revoked`. */
|
|
38
|
+
revokedAt?: number;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The grant {@link Invitations.accept} returns — what the host applies (writes a
|
|
42
|
+
* membership, grants a role) once an invite is consumed. `role`/`payload` are
|
|
43
|
+
* narrowed by the host validators when set.
|
|
44
|
+
*/
|
|
45
|
+
export interface InvitationGrant<TRole = unknown, TPayload = unknown> {
|
|
46
|
+
/** The opaque host resource the accepted invite grants access to. */
|
|
47
|
+
resourceRef: string;
|
|
48
|
+
/** The opaque host role to grant (narrowed if a `roleValidator` is set). */
|
|
49
|
+
role?: TRole;
|
|
50
|
+
/** Arbitrary opaque host data carried from `issue` (narrowed if a `payloadValidator` is set). */
|
|
51
|
+
payload?: TPayload;
|
|
52
|
+
}
|
|
53
|
+
/** Per-call options for {@link Invitations.issue}. */
|
|
54
|
+
export interface IssueOptions<TRole, TPayload> {
|
|
55
|
+
/** The opaque host role to grant on accept (validated against `roleValidator` before storage). */
|
|
56
|
+
role?: TRole;
|
|
57
|
+
/** The opaque host subject issuing the invite. */
|
|
58
|
+
inviterRef?: string;
|
|
59
|
+
/** The opaque host subject the invite is addressed to; omit for an open link. */
|
|
60
|
+
inviteeRef?: string;
|
|
61
|
+
/** Arbitrary opaque host data carried through to accept (validated against `payloadValidator`). */
|
|
62
|
+
payload?: TPayload;
|
|
63
|
+
/** Override the client's default TTL for this invite (ms from issue). Ignored when `expiresAt` is set. */
|
|
64
|
+
ttlMs?: number;
|
|
65
|
+
/** An absolute expiry (ms epoch). Takes precedence over `ttlMs` and the default TTL. */
|
|
66
|
+
expiresAt?: number;
|
|
67
|
+
}
|
|
68
|
+
/** Construction options for the {@link Invitations} client. */
|
|
69
|
+
export interface InvitationsOptions<TRole, TPayload> {
|
|
70
|
+
/**
|
|
71
|
+
* Default time-to-live (ms from issue) applied when an `issue` call passes
|
|
72
|
+
* neither `ttlMs` nor an absolute `expiresAt`. Defaults to 7 days.
|
|
73
|
+
*/
|
|
74
|
+
ttlMs?: number;
|
|
75
|
+
/**
|
|
76
|
+
* Mints the single-use token for a new invite. Defaults to `crypto.randomUUID`.
|
|
77
|
+
* Supply a host generator for a different shape (a longer secret, a prefix).
|
|
78
|
+
*/
|
|
79
|
+
generateToken?: () => string;
|
|
80
|
+
/**
|
|
81
|
+
* Validates/narrows a stored `role` to `TRole` at the boundary — applied to the
|
|
82
|
+
* `role` passed into `issue` (before storage) and the `role` returned by
|
|
83
|
+
* `getByToken`/`accept`. Throws on a mismatch. Omit to leave roles unvalidated.
|
|
84
|
+
*/
|
|
85
|
+
roleValidator?: Parser<TRole>;
|
|
86
|
+
/**
|
|
87
|
+
* Validates/narrows a stored `payload` to `TPayload` at the boundary — applied
|
|
88
|
+
* to the `payload` passed into `issue` and returned by `getByToken`/`accept`.
|
|
89
|
+
* Throws on a mismatch. Omit to leave payloads unvalidated.
|
|
90
|
+
*/
|
|
91
|
+
payloadValidator?: Parser<TPayload>;
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/client/types.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAE5D,6DAA6D;AAC7D,MAAM,MAAM,eAAe,GAAG,SAAS,GAAG,UAAU,GAAG,SAAS,GAAG,SAAS,CAAC;AAE7E;;;;;;;GAOG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,OAAO,KAAK,CAAC,CAAC;AAE9C,oFAAoF;AACpF,MAAM,WAAW,cAAc,CAAC,KAAK,GAAG,OAAO,EAAE,QAAQ,GAAG,OAAO;IACjE,oDAAoD;IACpD,KAAK,EAAE,MAAM,CAAC;IACd,oFAAoF;IACpF,WAAW,EAAE,MAAM,CAAC;IACpB,sFAAsF;IACtF,IAAI,CAAC,EAAE,KAAK,CAAC;IACb,qDAAqD;IACrD,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,mFAAmF;IACnF,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,sGAAsG;IACtG,OAAO,CAAC,EAAE,QAAQ,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