@vllnt/convex-api-keys 0.2.0 → 0.2.1-canary.1629092
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 -190
- package/README.md +47 -133
- package/dist/client/index.d.ts.map +1 -1
- package/dist/component/_generated/api.d.ts +2 -0
- package/dist/component/_generated/api.d.ts.map +1 -1
- package/dist/component/_generated/api.js.map +1 -1
- package/dist/component/_generated/component.d.ts +2 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/_generated/server.d.ts +12 -0
- package/dist/component/_generated/server.d.ts.map +1 -1
- package/dist/component/_generated/server.js +2 -0
- package/dist/component/_generated/server.js.map +1 -1
- package/dist/component/convex.config.d.ts +1 -1
- package/dist/component/convex.config.d.ts.map +1 -1
- package/dist/component/mutations.d.ts +11 -11
- package/dist/component/mutations.js +16 -16
- package/dist/component/mutations.js.map +1 -1
- package/dist/component/queries.d.ts +2 -2
- package/dist/component/queries.js +1 -1
- package/dist/component/queries.js.map +1 -1
- package/dist/component/schema.d.ts +13 -13
- package/dist/component/schema.d.ts.map +1 -1
- package/dist/shared.d.ts +2 -2
- package/package.json +30 -18
- package/src/component/_generated/api.ts +2 -0
- package/src/component/_generated/component.ts +3 -6
- package/src/component/_generated/server.ts +13 -0
- package/src/component/mutations.ts +16 -16
- package/src/component/queries.ts +1 -1
|
@@ -168,7 +168,7 @@ export const validate = mutation({
|
|
|
168
168
|
}
|
|
169
169
|
|
|
170
170
|
if (matchedKey.expiresAt && matchedKey.expiresAt <= now) {
|
|
171
|
-
await ctx.db.patch(matchedKey._id, { status: "expired" });
|
|
171
|
+
await ctx.db.patch("apiKeys", matchedKey._id, { status: "expired" });
|
|
172
172
|
log.info("key.expired", { keyId: matchedKey._id });
|
|
173
173
|
return { valid: false as const, reason: "expired" };
|
|
174
174
|
}
|
|
@@ -178,7 +178,7 @@ export const validate = mutation({
|
|
|
178
178
|
matchedKey.gracePeriodEnd &&
|
|
179
179
|
matchedKey.gracePeriodEnd <= now
|
|
180
180
|
) {
|
|
181
|
-
await ctx.db.patch(matchedKey._id, { status: "expired" });
|
|
181
|
+
await ctx.db.patch("apiKeys", matchedKey._id, { status: "expired" });
|
|
182
182
|
log.info("key.expired", { keyId: matchedKey._id, reason: "grace_period_ended" });
|
|
183
183
|
return { valid: false as const, reason: "expired" };
|
|
184
184
|
}
|
|
@@ -186,7 +186,7 @@ export const validate = mutation({
|
|
|
186
186
|
let newRemaining = matchedKey.remaining;
|
|
187
187
|
if (matchedKey.remaining !== undefined) {
|
|
188
188
|
if (matchedKey.remaining <= 0) {
|
|
189
|
-
await ctx.db.patch(matchedKey._id, { status: "exhausted" });
|
|
189
|
+
await ctx.db.patch("apiKeys", matchedKey._id, { status: "exhausted" });
|
|
190
190
|
log.info("key.exhausted", { keyId: matchedKey._id });
|
|
191
191
|
return { valid: false as const, reason: "exhausted" };
|
|
192
192
|
}
|
|
@@ -207,7 +207,7 @@ export const validate = mutation({
|
|
|
207
207
|
patch.lastUsedAt = now;
|
|
208
208
|
}
|
|
209
209
|
if (Object.keys(patch).length > 0) {
|
|
210
|
-
await ctx.db.patch(matchedKey._id, patch);
|
|
210
|
+
await ctx.db.patch("apiKeys", matchedKey._id, patch);
|
|
211
211
|
}
|
|
212
212
|
if (newRemaining === 0) {
|
|
213
213
|
log.info("key.exhausted", { keyId: matchedKey._id });
|
|
@@ -238,7 +238,7 @@ export const revoke = mutation({
|
|
|
238
238
|
},
|
|
239
239
|
returns: v.null(),
|
|
240
240
|
handler: async (ctx, { keyId, ownerId }) => {
|
|
241
|
-
const key = await ctx.db.get(keyId);
|
|
241
|
+
const key = await ctx.db.get("apiKeys", keyId);
|
|
242
242
|
if (!key) {
|
|
243
243
|
throw new Error("key not found");
|
|
244
244
|
}
|
|
@@ -248,7 +248,7 @@ export const revoke = mutation({
|
|
|
248
248
|
if (key.status === "revoked") {
|
|
249
249
|
return null;
|
|
250
250
|
}
|
|
251
|
-
await ctx.db.patch(keyId, { status: "revoked", revokedAt: Date.now() });
|
|
251
|
+
await ctx.db.patch("apiKeys", keyId, { status: "revoked", revokedAt: Date.now() });
|
|
252
252
|
log.info("key.revoked", { keyId, ownerId });
|
|
253
253
|
return null;
|
|
254
254
|
},
|
|
@@ -279,7 +279,7 @@ export const revokeByTag = mutation({
|
|
|
279
279
|
const now = Date.now();
|
|
280
280
|
for (const key of allKeys) {
|
|
281
281
|
if (key.tags.includes(tag)) {
|
|
282
|
-
await ctx.db.patch(key._id, { status: "revoked", revokedAt: now });
|
|
282
|
+
await ctx.db.patch("apiKeys", key._id, { status: "revoked", revokedAt: now });
|
|
283
283
|
revokedCount++;
|
|
284
284
|
}
|
|
285
285
|
}
|
|
@@ -303,7 +303,7 @@ export const rotate = mutation({
|
|
|
303
303
|
oldKeyExpiresAt: v.number(),
|
|
304
304
|
}),
|
|
305
305
|
handler: async (ctx, args) => {
|
|
306
|
-
const oldKey = await ctx.db.get(args.keyId);
|
|
306
|
+
const oldKey = await ctx.db.get("apiKeys", args.keyId);
|
|
307
307
|
if (!oldKey) {
|
|
308
308
|
throw new Error("key not found");
|
|
309
309
|
}
|
|
@@ -324,7 +324,7 @@ export const rotate = mutation({
|
|
|
324
324
|
const now = Date.now();
|
|
325
325
|
const gracePeriodEnd = now + gracePeriodMs;
|
|
326
326
|
|
|
327
|
-
await ctx.db.patch(args.keyId, {
|
|
327
|
+
await ctx.db.patch("apiKeys", args.keyId, {
|
|
328
328
|
status: "rotating",
|
|
329
329
|
gracePeriodEnd,
|
|
330
330
|
});
|
|
@@ -374,7 +374,7 @@ export const update = mutation({
|
|
|
374
374
|
},
|
|
375
375
|
returns: v.null(),
|
|
376
376
|
handler: async (ctx, { keyId, ownerId, ...updates }) => {
|
|
377
|
-
const key = await ctx.db.get(keyId);
|
|
377
|
+
const key = await ctx.db.get("apiKeys", keyId);
|
|
378
378
|
if (!key) {
|
|
379
379
|
throw new Error("key not found");
|
|
380
380
|
}
|
|
@@ -396,7 +396,7 @@ export const update = mutation({
|
|
|
396
396
|
if (updates.metadata !== undefined) patch.metadata = updates.metadata;
|
|
397
397
|
|
|
398
398
|
if (Object.keys(patch).length > 0) {
|
|
399
|
-
await ctx.db.patch(keyId, patch);
|
|
399
|
+
await ctx.db.patch("apiKeys", keyId, patch);
|
|
400
400
|
log.info("key.updated", { keyId, ownerId, fields: Object.keys(patch) });
|
|
401
401
|
}
|
|
402
402
|
|
|
@@ -411,7 +411,7 @@ export const disable = mutation({
|
|
|
411
411
|
},
|
|
412
412
|
returns: v.null(),
|
|
413
413
|
handler: async (ctx, { keyId, ownerId }) => {
|
|
414
|
-
const key = await ctx.db.get(keyId);
|
|
414
|
+
const key = await ctx.db.get("apiKeys", keyId);
|
|
415
415
|
if (!key) {
|
|
416
416
|
throw new Error("key not found");
|
|
417
417
|
}
|
|
@@ -424,7 +424,7 @@ export const disable = mutation({
|
|
|
424
424
|
if (key.status !== "active") {
|
|
425
425
|
throw new Error("can only disable active keys");
|
|
426
426
|
}
|
|
427
|
-
await ctx.db.patch(keyId, { status: "disabled" });
|
|
427
|
+
await ctx.db.patch("apiKeys", keyId, { status: "disabled" });
|
|
428
428
|
log.info("key.disabled", { keyId, ownerId });
|
|
429
429
|
return null;
|
|
430
430
|
},
|
|
@@ -437,7 +437,7 @@ export const enable = mutation({
|
|
|
437
437
|
},
|
|
438
438
|
returns: v.null(),
|
|
439
439
|
handler: async (ctx, { keyId, ownerId }) => {
|
|
440
|
-
const key = await ctx.db.get(keyId);
|
|
440
|
+
const key = await ctx.db.get("apiKeys", keyId);
|
|
441
441
|
if (!key) {
|
|
442
442
|
throw new Error("key not found");
|
|
443
443
|
}
|
|
@@ -450,7 +450,7 @@ export const enable = mutation({
|
|
|
450
450
|
if (key.status !== "disabled") {
|
|
451
451
|
throw new Error("can only enable disabled keys");
|
|
452
452
|
}
|
|
453
|
-
await ctx.db.patch(keyId, { status: "active" });
|
|
453
|
+
await ctx.db.patch("apiKeys", keyId, { status: "active" });
|
|
454
454
|
log.info("key.enabled", { keyId, ownerId });
|
|
455
455
|
return null;
|
|
456
456
|
},
|
|
@@ -476,7 +476,7 @@ export const configure = mutation({
|
|
|
476
476
|
: {};
|
|
477
477
|
|
|
478
478
|
if (existing) {
|
|
479
|
-
await ctx.db.patch(existing._id, args);
|
|
479
|
+
await ctx.db.patch("config", existing._id, args);
|
|
480
480
|
} else {
|
|
481
481
|
await ctx.db.insert("config", args);
|
|
482
482
|
}
|
package/src/component/queries.ts
CHANGED
|
@@ -109,7 +109,7 @@ export const getUsage = query({
|
|
|
109
109
|
remaining: v.optional(v.number()),
|
|
110
110
|
}),
|
|
111
111
|
handler: async (ctx, { keyId, ownerId }) => {
|
|
112
|
-
const key = await ctx.db.get(keyId);
|
|
112
|
+
const key = await ctx.db.get("apiKeys", keyId);
|
|
113
113
|
if (!key) {
|
|
114
114
|
throw new Error("key not found");
|
|
115
115
|
}
|