@vllnt/convex-tokens 0.1.0-canary.4f0e428

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 (76) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +127 -0
  3. package/dist/client/index.d.ts +116 -0
  4. package/dist/client/index.d.ts.map +1 -0
  5. package/dist/client/index.js +92 -0
  6. package/dist/client/index.js.map +1 -0
  7. package/dist/client/types.d.ts +59 -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 +71 -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 +121 -0
  24. package/dist/component/_generated/server.d.ts.map +1 -0
  25. package/dist/component/_generated/server.js +78 -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 +10 -0
  32. package/dist/component/crons.d.ts.map +1 -0
  33. package/dist/component/crons.js +13 -0
  34. package/dist/component/crons.js.map +1 -0
  35. package/dist/component/mutations.d.ts +49 -0
  36. package/dist/component/mutations.d.ts.map +1 -0
  37. package/dist/component/mutations.js +141 -0
  38. package/dist/component/mutations.js.map +1 -0
  39. package/dist/component/queries.d.ts +51 -0
  40. package/dist/component/queries.d.ts.map +1 -0
  41. package/dist/component/queries.js +84 -0
  42. package/dist/component/queries.js.map +1 -0
  43. package/dist/component/schema.d.ts +30 -0
  44. package/dist/component/schema.d.ts.map +1 -0
  45. package/dist/component/schema.js +23 -0
  46. package/dist/component/schema.js.map +1 -0
  47. package/dist/component/validators.d.ts +45 -0
  48. package/dist/component/validators.d.ts.map +1 -0
  49. package/dist/component/validators.js +24 -0
  50. package/dist/component/validators.js.map +1 -0
  51. package/dist/react/index.d.ts +35 -0
  52. package/dist/react/index.d.ts.map +1 -0
  53. package/dist/react/index.js +30 -0
  54. package/dist/react/index.js.map +1 -0
  55. package/dist/shared.d.ts +60 -0
  56. package/dist/shared.d.ts.map +1 -0
  57. package/dist/shared.js +69 -0
  58. package/dist/shared.js.map +1 -0
  59. package/package.json +112 -0
  60. package/src/client/index.ts +220 -0
  61. package/src/client/types.ts +60 -0
  62. package/src/component/_generated/api.ts +56 -0
  63. package/src/component/_generated/component.ts +93 -0
  64. package/src/component/_generated/dataModel.ts +60 -0
  65. package/src/component/_generated/server.ts +156 -0
  66. package/src/component/convex.config.ts +9 -0
  67. package/src/component/crons.ts +19 -0
  68. package/src/component/mutations.ts +158 -0
  69. package/src/component/queries.ts +90 -0
  70. package/src/component/schema.ts +23 -0
  71. package/src/component/validators.ts +28 -0
  72. package/src/react/index.test.tsx +86 -0
  73. package/src/react/index.tsx +41 -0
  74. package/src/shared.ts +98 -0
  75. package/src/test.ts +12 -0
  76. package/src/unit.test.ts +48 -0
@@ -0,0 +1,220 @@
1
+ import type {
2
+ FunctionArgs,
3
+ FunctionReference,
4
+ FunctionReturnType,
5
+ } from "convex/server";
6
+ import type {
7
+ MintResult,
8
+ TokenMetadata,
9
+ TokensOptions,
10
+ ValidateResult,
11
+ } from "./types.js";
12
+ import {
13
+ DEFAULT_HASH_ALGO,
14
+ DEFAULT_MAX_TTL_MS,
15
+ DEFAULT_SCOPE,
16
+ DEFAULT_TOKEN_BYTES,
17
+ DEFAULT_TTL_MS,
18
+ generateToken,
19
+ type HashAlgo,
20
+ hashToken,
21
+ } from "../shared.js";
22
+
23
+ /**
24
+ * The token component's function references, as exposed on the host via
25
+ * `components.tokens`. The component computes `expiresAt` itself from `ttlMs` —
26
+ * the client never sends a timestamp — and reads the current time server-side in
27
+ * `validate`, so neither expiry nor validity can be spoofed by a client clock.
28
+ */
29
+ export interface TokensComponent {
30
+ mutations: {
31
+ mint: FunctionReference<
32
+ "mutation",
33
+ "internal",
34
+ {
35
+ tokenHash: string;
36
+ scope: string;
37
+ resourceRef?: string;
38
+ ttlMs: number;
39
+ defaultTtlMs: number;
40
+ maxTtlMs: number;
41
+ },
42
+ string
43
+ >;
44
+ revoke: FunctionReference<
45
+ "mutation",
46
+ "internal",
47
+ { tokenHash: string; scope: string },
48
+ boolean
49
+ >;
50
+ prune: FunctionReference<"mutation", "internal", { before: number }, number>;
51
+ };
52
+ queries: {
53
+ validate: FunctionReference<
54
+ "query",
55
+ "internal",
56
+ { tokenHash: string; scope: string },
57
+ ValidateResult
58
+ >;
59
+ list: FunctionReference<
60
+ "query",
61
+ "internal",
62
+ { scope: string; resourceRef?: string; limit?: number },
63
+ TokenMetadata[]
64
+ >;
65
+ getMetadata: FunctionReference<
66
+ "query",
67
+ "internal",
68
+ { id: string },
69
+ TokenMetadata | null
70
+ >;
71
+ };
72
+ }
73
+
74
+ interface RunQueryCtx {
75
+ runQuery<Q extends FunctionReference<"query", "internal">>(
76
+ reference: Q,
77
+ args: FunctionArgs<Q>,
78
+ ): Promise<FunctionReturnType<Q>>;
79
+ }
80
+
81
+ interface RunMutationCtx {
82
+ runMutation<M extends FunctionReference<"mutation", "internal">>(
83
+ reference: M,
84
+ args: FunctionArgs<M>,
85
+ ): Promise<FunctionReturnType<M>>;
86
+ }
87
+
88
+ /** Options for {@link Tokens.mint}. */
89
+ export interface MintOptions {
90
+ /** Namespace for the token. Defaults to the client's `defaultScope`. */
91
+ scope?: string;
92
+ /** Opaque host-owned reference to carry on the token. */
93
+ resourceRef?: string;
94
+ /**
95
+ * Requested lifetime in ms. The component clamps it into
96
+ * `[MIN_TTL_MS, maxTtlMs]` server-side; a non-finite value falls back to
97
+ * `defaultTtlMs`. The client never computes the expiry timestamp.
98
+ */
99
+ ttlMs?: number;
100
+ }
101
+
102
+ /** Options for {@link Tokens.list}. */
103
+ export interface ListOptions {
104
+ /** Namespace to list within. Defaults to the client's `defaultScope`. */
105
+ scope?: string;
106
+ /** Restrict to a single opaque host reference. */
107
+ resourceRef?: string;
108
+ /** Max rows to return. Default 100 (component-side). */
109
+ limit?: number;
110
+ }
111
+
112
+ /**
113
+ * Consumer-facing client for the hashed-secret token registry. The host owns
114
+ * meaning and auth; it passes an opaque `resourceRef` and an optional `scope`.
115
+ * The raw token is generated and hashed here — the component only ever sees the
116
+ * hash, never the secret. Time is sourced server-side: the client never supplies
117
+ * `now` or an `expiresAt`.
118
+ */
119
+ export class Tokens {
120
+ private readonly defaultScope: string;
121
+ private readonly defaultTtlMs: number;
122
+ private readonly maxTtlMs: number;
123
+ private readonly tokenBytes: number;
124
+ private readonly hashAlgo: HashAlgo;
125
+
126
+ constructor(
127
+ private readonly component: TokensComponent,
128
+ options: TokensOptions = {},
129
+ ) {
130
+ this.defaultScope = options.defaultScope ?? DEFAULT_SCOPE;
131
+ this.defaultTtlMs = options.defaultTtlMs ?? DEFAULT_TTL_MS;
132
+ this.maxTtlMs = options.maxTtlMs ?? DEFAULT_MAX_TTL_MS;
133
+ this.tokenBytes = options.tokenBytes ?? DEFAULT_TOKEN_BYTES;
134
+ this.hashAlgo = options.hashAlgo ?? DEFAULT_HASH_ALGO;
135
+ }
136
+
137
+ private scopeOf(scope: string | undefined): string {
138
+ return scope ?? this.defaultScope;
139
+ }
140
+
141
+ /**
142
+ * Mint a new token. Returns the raw secret ONCE (it is never recoverable
143
+ * afterwards) alongside the component-side id. The requested `ttlMs` is sent
144
+ * to the component, which clamps it and computes `expiresAt` from its own
145
+ * clock.
146
+ */
147
+ async mint(ctx: RunMutationCtx, opts: MintOptions = {}): Promise<MintResult> {
148
+ const raw = generateToken(this.tokenBytes);
149
+ const tokenHash = await hashToken(raw, this.hashAlgo);
150
+ const id = await ctx.runMutation(this.component.mutations.mint, {
151
+ tokenHash,
152
+ scope: this.scopeOf(opts.scope),
153
+ resourceRef: opts.resourceRef,
154
+ ttlMs: opts.ttlMs ?? this.defaultTtlMs,
155
+ defaultTtlMs: this.defaultTtlMs,
156
+ maxTtlMs: this.maxTtlMs,
157
+ });
158
+ return { token: raw, id };
159
+ }
160
+
161
+ /**
162
+ * Validate `rawToken`. Hashes the secret and checks scope, revocation, and
163
+ * expiry. Expiry is evaluated against the component's server clock — there is
164
+ * no client-supplied time to override it.
165
+ */
166
+ async validate(
167
+ ctx: RunQueryCtx,
168
+ rawToken: string,
169
+ scope?: string,
170
+ ): Promise<ValidateResult> {
171
+ const tokenHash = await hashToken(rawToken, this.hashAlgo);
172
+ return ctx.runQuery(this.component.queries.validate, {
173
+ tokenHash,
174
+ scope: this.scopeOf(scope),
175
+ });
176
+ }
177
+
178
+ /** Revoke `rawToken`. Returns `true` if a matching token was revoked. */
179
+ async revoke(
180
+ ctx: RunMutationCtx,
181
+ rawToken: string,
182
+ scope?: string,
183
+ ): Promise<boolean> {
184
+ const tokenHash = await hashToken(rawToken, this.hashAlgo);
185
+ return ctx.runMutation(this.component.mutations.revoke, {
186
+ tokenHash,
187
+ scope: this.scopeOf(scope),
188
+ });
189
+ }
190
+
191
+ /**
192
+ * List token METADATA (never the hash) for a management surface, filtered by
193
+ * scope and optional `resourceRef`. Safe to expose to a future admin UI.
194
+ */
195
+ list(ctx: RunQueryCtx, opts: ListOptions = {}): Promise<TokenMetadata[]> {
196
+ return ctx.runQuery(this.component.queries.list, {
197
+ scope: this.scopeOf(opts.scope),
198
+ resourceRef: opts.resourceRef,
199
+ limit: opts.limit,
200
+ });
201
+ }
202
+
203
+ /**
204
+ * Fetch the safe METADATA of one token by its component id (the `id` from
205
+ * {@link Tokens.mint}). Returns `null` if it no longer exists. Never returns
206
+ * the hash.
207
+ */
208
+ getMetadata(ctx: RunQueryCtx, id: string): Promise<TokenMetadata | null> {
209
+ return ctx.runQuery(this.component.queries.getMetadata, { id });
210
+ }
211
+
212
+ /** Delete every token that expired before `before` (default now). Returns the count. */
213
+ prune(ctx: RunMutationCtx, before?: number): Promise<number> {
214
+ return ctx.runMutation(this.component.mutations.prune, {
215
+ before: before ?? Date.now(),
216
+ });
217
+ }
218
+ }
219
+
220
+ export type { MintResult, TokenMetadata, TokensOptions, ValidateResult };
@@ -0,0 +1,60 @@
1
+ /** Public TypeScript surface for the tokens client. */
2
+
3
+ import type { HashAlgo } from "../shared.js";
4
+
5
+ /** Result of minting a token. `token` is the raw secret — shown ONCE, never stored. */
6
+ export interface MintResult {
7
+ /** The raw token. Hand it to the caller now; the component keeps only its hash. */
8
+ token: string;
9
+ /** The component-side id of the minted token record (use it with `getMetadata`). */
10
+ id: string;
11
+ }
12
+
13
+ /**
14
+ * Result of validating a raw token.
15
+ *
16
+ * Discriminated union on `valid`:
17
+ * - `{ valid: true, resourceRef? }` — the token is live; `resourceRef` is the
18
+ * opaque host-owned reference the token was minted with, if any.
19
+ * - `{ valid: false }` — the token is unknown, wrong-scope, revoked, or expired.
20
+ * The failed branch structurally cannot carry `resourceRef`.
21
+ */
22
+ export type ValidateResult =
23
+ | { valid: true; resourceRef?: string }
24
+ | { valid: false };
25
+
26
+ /**
27
+ * Safe, leak-free metadata for one token — what a management UI may display.
28
+ * NEVER includes `tokenHash`.
29
+ */
30
+ export interface TokenMetadata {
31
+ /** The component-side id of the token record. */
32
+ id: string;
33
+ /** The token's namespace. */
34
+ scope: string;
35
+ /** The opaque host-owned reference, if the token carries one. */
36
+ resourceRef?: string;
37
+ /** Whether the token has been revoked. */
38
+ revoked: boolean;
39
+ /** Expiry timestamp (ms since epoch). */
40
+ expiresAt: number;
41
+ /** Creation timestamp (ms since epoch). */
42
+ createdAt: number;
43
+ }
44
+
45
+ /** Construction options for the {@link Tokens} client. */
46
+ export interface TokensOptions {
47
+ /** Namespace applied when a call omits `scope`. Default `"global"`. */
48
+ defaultScope?: string;
49
+ /** Lifetime applied when `mint` omits `ttlMs`, in ms. Default 24h. */
50
+ defaultTtlMs?: number;
51
+ /** Ceiling a requested TTL is clamped to, in ms. Default 30d. */
52
+ maxTtlMs?: number;
53
+ /** Entropy of a generated raw token, in bytes. Default 24. */
54
+ tokenBytes?: number;
55
+ /**
56
+ * Digest algorithm for hashing tokens. Fixed per mount — changing it
57
+ * invalidates every existing hash. Default `"SHA-256"`.
58
+ */
59
+ hashAlgo?: HashAlgo;
60
+ }
@@ -0,0 +1,56 @@
1
+ /* eslint-disable */
2
+ /**
3
+ * Generated `api` utility.
4
+ *
5
+ * THIS CODE IS AUTOMATICALLY GENERATED.
6
+ *
7
+ * To regenerate, run `npx convex dev`.
8
+ * @module
9
+ */
10
+
11
+ import type * as crons from "../crons.js";
12
+ import type * as mutations from "../mutations.js";
13
+ import type * as queries from "../queries.js";
14
+ import type * as validators from "../validators.js";
15
+
16
+ import type {
17
+ ApiFromModules,
18
+ FilterApi,
19
+ FunctionReference,
20
+ } from "convex/server";
21
+ import { anyApi, componentsGeneric } from "convex/server";
22
+
23
+ const fullApi: ApiFromModules<{
24
+ crons: typeof crons;
25
+ mutations: typeof mutations;
26
+ queries: typeof queries;
27
+ validators: typeof validators;
28
+ }> = anyApi as any;
29
+
30
+ /**
31
+ * A utility for referencing Convex functions in your app's public API.
32
+ *
33
+ * Usage:
34
+ * ```js
35
+ * const myFunctionReference = api.myModule.myFunction;
36
+ * ```
37
+ */
38
+ export const api: FilterApi<
39
+ typeof fullApi,
40
+ FunctionReference<any, "public">
41
+ > = anyApi as any;
42
+
43
+ /**
44
+ * A utility for referencing Convex functions in your app's internal API.
45
+ *
46
+ * Usage:
47
+ * ```js
48
+ * const myFunctionReference = internal.myModule.myFunction;
49
+ * ```
50
+ */
51
+ export const internal: FilterApi<
52
+ typeof fullApi,
53
+ FunctionReference<any, "internal">
54
+ > = anyApi as any;
55
+
56
+ export const components = componentsGeneric() as unknown as {};
@@ -0,0 +1,93 @@
1
+ /* eslint-disable */
2
+ /**
3
+ * Generated `ComponentApi` utility.
4
+ *
5
+ * THIS CODE IS AUTOMATICALLY GENERATED.
6
+ *
7
+ * To regenerate, run `npx convex dev`.
8
+ * @module
9
+ */
10
+
11
+ import type { FunctionReference } from "convex/server";
12
+
13
+ /**
14
+ * A utility for referencing a Convex component's exposed API.
15
+ *
16
+ * Useful when expecting a parameter like `components.myComponent`.
17
+ * Usage:
18
+ * ```ts
19
+ * async function myFunction(ctx: QueryCtx, component: ComponentApi) {
20
+ * return ctx.runQuery(component.someFile.someQuery, { ...args });
21
+ * }
22
+ * ```
23
+ */
24
+ export type ComponentApi<Name extends string | undefined = string | undefined> =
25
+ {
26
+ mutations: {
27
+ mint: FunctionReference<
28
+ "mutation",
29
+ "internal",
30
+ {
31
+ defaultTtlMs: number;
32
+ maxTtlMs: number;
33
+ resourceRef?: string;
34
+ scope: string;
35
+ tokenHash: string;
36
+ ttlMs: number;
37
+ },
38
+ string,
39
+ Name
40
+ >;
41
+ prune: FunctionReference<
42
+ "mutation",
43
+ "internal",
44
+ { before: number },
45
+ number,
46
+ Name
47
+ >;
48
+ revoke: FunctionReference<
49
+ "mutation",
50
+ "internal",
51
+ { scope: string; tokenHash: string },
52
+ boolean,
53
+ Name
54
+ >;
55
+ };
56
+ queries: {
57
+ getMetadata: FunctionReference<
58
+ "query",
59
+ "internal",
60
+ { id: string },
61
+ {
62
+ createdAt: number;
63
+ expiresAt: number;
64
+ id: string;
65
+ resourceRef?: string;
66
+ revoked: boolean;
67
+ scope: string;
68
+ } | null,
69
+ Name
70
+ >;
71
+ list: FunctionReference<
72
+ "query",
73
+ "internal",
74
+ { limit?: number; resourceRef?: string; scope: string },
75
+ Array<{
76
+ createdAt: number;
77
+ expiresAt: number;
78
+ id: string;
79
+ resourceRef?: string;
80
+ revoked: boolean;
81
+ scope: string;
82
+ }>,
83
+ Name
84
+ >;
85
+ validate: FunctionReference<
86
+ "query",
87
+ "internal",
88
+ { scope: string; tokenHash: string },
89
+ { resourceRef?: string; valid: boolean },
90
+ Name
91
+ >;
92
+ };
93
+ };
@@ -0,0 +1,60 @@
1
+ /* eslint-disable */
2
+ /**
3
+ * Generated data model types.
4
+ *
5
+ * THIS CODE IS AUTOMATICALLY GENERATED.
6
+ *
7
+ * To regenerate, run `npx convex dev`.
8
+ * @module
9
+ */
10
+
11
+ import type {
12
+ DataModelFromSchemaDefinition,
13
+ DocumentByName,
14
+ TableNamesInDataModel,
15
+ SystemTableNames,
16
+ } from "convex/server";
17
+ import type { GenericId } from "convex/values";
18
+ import schema from "../schema.js";
19
+
20
+ /**
21
+ * The names of all of your Convex tables.
22
+ */
23
+ export type TableNames = TableNamesInDataModel<DataModel>;
24
+
25
+ /**
26
+ * The type of a document stored in Convex.
27
+ *
28
+ * @typeParam TableName - A string literal type of the table name (like "users").
29
+ */
30
+ export type Doc<TableName extends TableNames> = DocumentByName<
31
+ DataModel,
32
+ TableName
33
+ >;
34
+
35
+ /**
36
+ * An identifier for a document in Convex.
37
+ *
38
+ * Convex documents are uniquely identified by their `Id`, which is accessible
39
+ * on the `_id` field. To learn more, see [Document IDs](https://docs.convex.dev/using/document-ids).
40
+ *
41
+ * Documents can be loaded using `db.get(tableName, id)` in query and mutation functions.
42
+ *
43
+ * IDs are just strings at runtime, but this type can be used to distinguish them from other
44
+ * strings when type checking.
45
+ *
46
+ * @typeParam TableName - A string literal type of the table name (like "users").
47
+ */
48
+ export type Id<TableName extends TableNames | SystemTableNames> =
49
+ GenericId<TableName>;
50
+
51
+ /**
52
+ * A type describing your Convex data model.
53
+ *
54
+ * This type includes information about what tables you have, the type of
55
+ * documents stored in those tables, and the indexes defined on them.
56
+ *
57
+ * This type is used to parameterize methods like `queryGeneric` and
58
+ * `mutationGeneric` to make them type-safe.
59
+ */
60
+ export type DataModel = DataModelFromSchemaDefinition<typeof schema>;
@@ -0,0 +1,156 @@
1
+ /* eslint-disable */
2
+ /**
3
+ * Generated utilities for implementing server-side Convex query and mutation functions.
4
+ *
5
+ * THIS CODE IS AUTOMATICALLY GENERATED.
6
+ *
7
+ * To regenerate, run `npx convex dev`.
8
+ * @module
9
+ */
10
+
11
+ import type {
12
+ ActionBuilder,
13
+ HttpActionBuilder,
14
+ MutationBuilder,
15
+ QueryBuilder,
16
+ GenericActionCtx,
17
+ GenericMutationCtx,
18
+ GenericQueryCtx,
19
+ GenericDatabaseReader,
20
+ GenericDatabaseWriter,
21
+ } from "convex/server";
22
+ import {
23
+ actionGeneric,
24
+ httpActionGeneric,
25
+ queryGeneric,
26
+ mutationGeneric,
27
+ internalActionGeneric,
28
+ internalMutationGeneric,
29
+ internalQueryGeneric,
30
+ } from "convex/server";
31
+ import type { DataModel } from "./dataModel.js";
32
+
33
+ /**
34
+ * Define a query in this Convex app's public API.
35
+ *
36
+ * This function will be allowed to read your Convex database and will be accessible from the client.
37
+ *
38
+ * @param func - The query function. It receives a {@link QueryCtx} as its first argument.
39
+ * @returns The wrapped query. Include this as an `export` to name it and make it accessible.
40
+ */
41
+ export const query: QueryBuilder<DataModel, "public"> = queryGeneric;
42
+
43
+ /**
44
+ * Define a query that is only accessible from other Convex functions (but not from the client).
45
+ *
46
+ * This function will be allowed to read from your Convex database. It will not be accessible from the client.
47
+ *
48
+ * @param func - The query function. It receives a {@link QueryCtx} as its first argument.
49
+ * @returns The wrapped query. Include this as an `export` to name it and make it accessible.
50
+ */
51
+ export const internalQuery: QueryBuilder<DataModel, "internal"> =
52
+ internalQueryGeneric;
53
+
54
+ /**
55
+ * Define a mutation in this Convex app's public API.
56
+ *
57
+ * This function will be allowed to modify your Convex database and will be accessible from the client.
58
+ *
59
+ * @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
60
+ * @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
61
+ */
62
+ export const mutation: MutationBuilder<DataModel, "public"> = mutationGeneric;
63
+
64
+ /**
65
+ * Define a mutation that is only accessible from other Convex functions (but not from the client).
66
+ *
67
+ * This function will be allowed to modify your Convex database. It will not be accessible from the client.
68
+ *
69
+ * @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
70
+ * @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
71
+ */
72
+ export const internalMutation: MutationBuilder<DataModel, "internal"> =
73
+ internalMutationGeneric;
74
+
75
+ /**
76
+ * Define an action in this Convex app's public API.
77
+ *
78
+ * An action is a function which can execute any JavaScript code, including non-deterministic
79
+ * code and code with side-effects, like calling third-party services.
80
+ * They can be run in Convex's JavaScript environment or in Node.js using the "use node" directive.
81
+ * They can interact with the database indirectly by calling queries and mutations using the {@link ActionCtx}.
82
+ *
83
+ * @param func - The action. It receives an {@link ActionCtx} as its first argument.
84
+ * @returns The wrapped action. Include this as an `export` to name it and make it accessible.
85
+ */
86
+ export const action: ActionBuilder<DataModel, "public"> = actionGeneric;
87
+
88
+ /**
89
+ * Define an action that is only accessible from other Convex functions (but not from the client).
90
+ *
91
+ * @param func - The function. It receives an {@link ActionCtx} as its first argument.
92
+ * @returns The wrapped function. Include this as an `export` to name it and make it accessible.
93
+ */
94
+ export const internalAction: ActionBuilder<DataModel, "internal"> =
95
+ internalActionGeneric;
96
+
97
+ /**
98
+ * Define an HTTP action.
99
+ *
100
+ * The wrapped function will be used to respond to HTTP requests received
101
+ * by a Convex deployment if the requests matches the path and method where
102
+ * this action is routed. Be sure to route your httpAction in `convex/http.js`.
103
+ *
104
+ * @param func - The function. It receives an {@link ActionCtx} as its first argument
105
+ * and a Fetch API `Request` object as its second.
106
+ * @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
107
+ */
108
+ export const httpAction: HttpActionBuilder = httpActionGeneric;
109
+
110
+ /**
111
+ * A set of services for use within Convex query functions.
112
+ *
113
+ * The query context is passed as the first argument to any Convex query
114
+ * function run on the server.
115
+ *
116
+ * If you're using code generation, use the `QueryCtx` type in `convex/_generated/server.d.ts` instead.
117
+ */
118
+ export type QueryCtx = GenericQueryCtx<DataModel>;
119
+
120
+ /**
121
+ * A set of services for use within Convex mutation functions.
122
+ *
123
+ * The mutation context is passed as the first argument to any Convex mutation
124
+ * function run on the server.
125
+ *
126
+ * If you're using code generation, use the `MutationCtx` type in `convex/_generated/server.d.ts` instead.
127
+ */
128
+ export type MutationCtx = GenericMutationCtx<DataModel>;
129
+
130
+ /**
131
+ * A set of services for use within Convex action functions.
132
+ *
133
+ * The action context is passed as the first argument to any Convex action
134
+ * function run on the server.
135
+ */
136
+ export type ActionCtx = GenericActionCtx<DataModel>;
137
+
138
+ /**
139
+ * An interface to read from the database within Convex query functions.
140
+ *
141
+ * The two entry points are {@link DatabaseReader.get}, which fetches a single
142
+ * document by its {@link Id}, or {@link DatabaseReader.query}, which starts
143
+ * building a query.
144
+ */
145
+ export type DatabaseReader = GenericDatabaseReader<DataModel>;
146
+
147
+ /**
148
+ * An interface to read from and write to the database within Convex mutation
149
+ * functions.
150
+ *
151
+ * Convex guarantees that all writes within a single mutation are
152
+ * executed atomically, so you never have to worry about partial writes leaving
153
+ * your data in an inconsistent state. See [the Convex Guide](https://docs.convex.dev/understanding/convex-fundamentals/functions#atomicity-and-optimistic-concurrency-control)
154
+ * for the guarantees Convex provides your functions.
155
+ */
156
+ export type DatabaseWriter = GenericDatabaseWriter<DataModel>;
@@ -0,0 +1,9 @@
1
+ import { defineComponent } from "convex/server";
2
+
3
+ const component = defineComponent("tokens");
4
+
5
+ // Nest official child components here, e.g.:
6
+ // import sharded from "@convex-dev/sharded-counter/convex.config";
7
+ // component.use(sharded);
8
+
9
+ export default component;
@@ -0,0 +1,19 @@
1
+ import { cronJobs } from "convex/server";
2
+ import { internal } from "./_generated/api";
3
+
4
+ /**
5
+ * Internal cron that sweeps expired and stale-revoked tokens daily. The cadence
6
+ * is fixed at the component layer (Convex crons are static); a mount that needs
7
+ * a different sweep frequency can also call `prune` from its own schedule. The
8
+ * sweep is bounded + self-rescheduling, so a daily tick safely drains any
9
+ * backlog. See `pruneExpired` in `mutations.ts`.
10
+ */
11
+ const crons = cronJobs();
12
+
13
+ crons.interval(
14
+ "tokens: prune expired and stale-revoked",
15
+ { hours: 24 },
16
+ internal.mutations.pruneExpired,
17
+ );
18
+
19
+ export default crons;