@gaganref/convex-api-keys 0.1.0

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 (102) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +419 -0
  3. package/dist/client/_generated/_ignore.d.ts +1 -0
  4. package/dist/client/_generated/_ignore.d.ts.map +1 -0
  5. package/dist/client/_generated/_ignore.js +3 -0
  6. package/dist/client/_generated/_ignore.js.map +1 -0
  7. package/dist/client/crypto.d.ts +4 -0
  8. package/dist/client/crypto.d.ts.map +1 -0
  9. package/dist/client/crypto.js +48 -0
  10. package/dist/client/crypto.js.map +1 -0
  11. package/dist/client/errors.d.ts +32 -0
  12. package/dist/client/errors.d.ts.map +1 -0
  13. package/dist/client/errors.js +43 -0
  14. package/dist/client/errors.js.map +1 -0
  15. package/dist/client/index.d.ts +7 -0
  16. package/dist/client/index.d.ts.map +1 -0
  17. package/dist/client/index.js +4 -0
  18. package/dist/client/index.js.map +1 -0
  19. package/dist/client/operations.d.ts +240 -0
  20. package/dist/client/operations.d.ts.map +1 -0
  21. package/dist/client/operations.js +700 -0
  22. package/dist/client/operations.js.map +1 -0
  23. package/dist/client/options.d.ts +79 -0
  24. package/dist/client/options.d.ts.map +1 -0
  25. package/dist/client/options.js +51 -0
  26. package/dist/client/options.js.map +1 -0
  27. package/dist/client/types.d.ts +269 -0
  28. package/dist/client/types.d.ts.map +1 -0
  29. package/dist/client/types.js +24 -0
  30. package/dist/client/types.js.map +1 -0
  31. package/dist/component/_generated/api.d.ts +40 -0
  32. package/dist/component/_generated/api.d.ts.map +1 -0
  33. package/dist/component/_generated/api.js +31 -0
  34. package/dist/component/_generated/api.js.map +1 -0
  35. package/dist/component/_generated/component.d.ts +253 -0
  36. package/dist/component/_generated/component.d.ts.map +1 -0
  37. package/dist/component/_generated/component.js +11 -0
  38. package/dist/component/_generated/component.js.map +1 -0
  39. package/dist/component/_generated/dataModel.d.ts +46 -0
  40. package/dist/component/_generated/dataModel.d.ts.map +1 -0
  41. package/dist/component/_generated/dataModel.js +11 -0
  42. package/dist/component/_generated/dataModel.js.map +1 -0
  43. package/dist/component/_generated/server.d.ts +121 -0
  44. package/dist/component/_generated/server.d.ts.map +1 -0
  45. package/dist/component/_generated/server.js +78 -0
  46. package/dist/component/_generated/server.js.map +1 -0
  47. package/dist/component/cleanup.d.ts +29 -0
  48. package/dist/component/cleanup.d.ts.map +1 -0
  49. package/dist/component/cleanup.js +70 -0
  50. package/dist/component/cleanup.js.map +1 -0
  51. package/dist/component/convex.config.d.ts +3 -0
  52. package/dist/component/convex.config.d.ts.map +1 -0
  53. package/dist/component/convex.config.js +3 -0
  54. package/dist/component/convex.config.js.map +1 -0
  55. package/dist/component/crons.d.ts +3 -0
  56. package/dist/component/crons.d.ts.map +1 -0
  57. package/dist/component/crons.js +7 -0
  58. package/dist/component/crons.js.map +1 -0
  59. package/dist/component/lib.d.ts +323 -0
  60. package/dist/component/lib.d.ts.map +1 -0
  61. package/dist/component/lib.js +659 -0
  62. package/dist/component/lib.js.map +1 -0
  63. package/dist/component/schema.d.ts +82 -0
  64. package/dist/component/schema.d.ts.map +1 -0
  65. package/dist/component/schema.js +38 -0
  66. package/dist/component/schema.js.map +1 -0
  67. package/dist/component/sweep.d.ts +27 -0
  68. package/dist/component/sweep.d.ts.map +1 -0
  69. package/dist/component/sweep.js +94 -0
  70. package/dist/component/sweep.js.map +1 -0
  71. package/dist/shared.d.ts +11 -0
  72. package/dist/shared.d.ts.map +1 -0
  73. package/dist/shared.js +11 -0
  74. package/dist/shared.js.map +1 -0
  75. package/package.json +116 -0
  76. package/src/client/__tests__/contracts.test.ts +109 -0
  77. package/src/client/__tests__/errors.test.ts +133 -0
  78. package/src/client/__tests__/hooks.test.ts +154 -0
  79. package/src/client/__tests__/operations.test.ts +742 -0
  80. package/src/client/__tests__/setup.test.ts +31 -0
  81. package/src/client/_generated/_ignore.ts +1 -0
  82. package/src/client/crypto.ts +64 -0
  83. package/src/client/errors.ts +67 -0
  84. package/src/client/index.ts +44 -0
  85. package/src/client/operations.ts +881 -0
  86. package/src/client/options.ts +146 -0
  87. package/src/client/types.ts +313 -0
  88. package/src/component/__tests__/cleanup.test.ts +472 -0
  89. package/src/component/__tests__/lib.test.ts +676 -0
  90. package/src/component/__tests__/setup.test.ts +11 -0
  91. package/src/component/_generated/api.ts +56 -0
  92. package/src/component/_generated/component.ts +300 -0
  93. package/src/component/_generated/dataModel.ts +60 -0
  94. package/src/component/_generated/server.ts +156 -0
  95. package/src/component/cleanup.ts +85 -0
  96. package/src/component/convex.config.ts +3 -0
  97. package/src/component/crons.ts +20 -0
  98. package/src/component/lib.ts +843 -0
  99. package/src/component/schema.ts +49 -0
  100. package/src/component/sweep.ts +117 -0
  101. package/src/shared.ts +18 -0
  102. package/src/test.ts +18 -0
@@ -0,0 +1,31 @@
1
+ /// <reference types="vite/client" />
2
+ import { test } from "vitest";
3
+ import { convexTest } from "convex-test";
4
+ import {
5
+ defineSchema,
6
+ type GenericSchema,
7
+ type SchemaDefinition,
8
+ componentsGeneric,
9
+ } from "convex/server";
10
+ import type { ComponentApi } from "../../component/_generated/component.js";
11
+ import componentSchema from "../../component/schema.js";
12
+ import { register } from "../../test.js";
13
+
14
+ export const modules = import.meta.glob("../**/*.*s");
15
+ export const componentModules = import.meta.glob("../../component/**/*.ts");
16
+
17
+ export { componentSchema };
18
+
19
+ export const components = componentsGeneric() as unknown as {
20
+ apiKeys: ComponentApi;
21
+ };
22
+
23
+ export function initConvexTest<
24
+ Schema extends SchemaDefinition<GenericSchema, boolean>,
25
+ >(schema?: Schema) {
26
+ const t = convexTest(schema ?? defineSchema({}), modules);
27
+ register(t);
28
+ return t;
29
+ }
30
+
31
+ test("setup", () => {});
@@ -0,0 +1 @@
1
+ // This is only here so convex-test can detect a _generated folder
@@ -0,0 +1,64 @@
1
+ import { runtimeUnavailableError } from "./errors.js";
2
+
3
+ const textEncoder = new TextEncoder();
4
+
5
+ export function generateToken(prefix: string, keyLengthBytes: number): string {
6
+ const bytes = randomBytes(keyLengthBytes);
7
+ return `${prefix}${base64UrlEncode(bytes)}`;
8
+ }
9
+
10
+ export async function sha256Base64Url(value: string): Promise<string> {
11
+ const hash = await getSubtleOrThrow().digest(
12
+ "SHA-256",
13
+ textEncoder.encode(value),
14
+ );
15
+ return base64UrlEncode(new Uint8Array(hash));
16
+ }
17
+
18
+ export function tokenLast4(token: string) {
19
+ return token.slice(-4);
20
+ }
21
+
22
+ function randomBytes(length: number) {
23
+ const crypto = getCryptoOrThrow();
24
+ const bytes = new Uint8Array(length);
25
+ crypto.getRandomValues(bytes);
26
+ return bytes;
27
+ }
28
+
29
+ function getSubtleOrThrow() {
30
+ const subtle = getCryptoOrThrow().subtle;
31
+ if (!subtle) {
32
+ throw runtimeUnavailableError(
33
+ "Web Crypto API subtle crypto is not available",
34
+ );
35
+ }
36
+ return subtle;
37
+ }
38
+
39
+ function getCryptoOrThrow() {
40
+ const crypto = globalThis.crypto;
41
+ if (!crypto) {
42
+ throw runtimeUnavailableError(
43
+ "Web Crypto API is not available in this runtime",
44
+ );
45
+ }
46
+ return crypto;
47
+ }
48
+
49
+ function base64UrlEncode(bytes: Uint8Array) {
50
+ let binary = "";
51
+ for (const byte of bytes) {
52
+ binary += String.fromCharCode(byte);
53
+ }
54
+ if (typeof globalThis.btoa !== "function") {
55
+ throw runtimeUnavailableError(
56
+ "Base64 encoding is not available in this runtime",
57
+ );
58
+ }
59
+ return globalThis
60
+ .btoa(binary)
61
+ .replace(/\+/g, "-")
62
+ .replace(/\//g, "_")
63
+ .replace(/=+$/g, "");
64
+ }
@@ -0,0 +1,67 @@
1
+ export type ApiKeysClientErrorCode =
2
+ // Init-time — bad config passed to createApiKeys()
3
+ | "INVALID_OPTIONS"
4
+ // Call-time — programmer mistakes at call site
5
+ | "TOKEN_REQUIRED"
6
+ // Infrastructure
7
+ | "RUNTIME_UNAVAILABLE"
8
+ | "OPERATION_FAILED";
9
+
10
+ /**
11
+ * Structured runtime error thrown by client-side helpers.
12
+ *
13
+ * Chains the underlying error via `cause`, making it visible in
14
+ * stack traces and debuggers.
15
+ *
16
+ * ```ts
17
+ * try {
18
+ * await apiKeys.create(ctx, args);
19
+ * } catch (error) {
20
+ * if (isApiKeysClientError(error)) {
21
+ * console.error(error.code, error.message);
22
+ * if (error.cause) console.error("Caused by:", error.cause);
23
+ * }
24
+ * }
25
+ * ```
26
+ */
27
+ export class ApiKeysClientError extends Error {
28
+ readonly name = "ApiKeysClientError";
29
+ readonly code: ApiKeysClientErrorCode;
30
+ readonly cause?: unknown;
31
+
32
+ constructor(code: ApiKeysClientErrorCode, message: string, cause?: unknown) {
33
+ super(message);
34
+ this.code = code;
35
+ this.cause = cause;
36
+ }
37
+ }
38
+
39
+ /**
40
+ * Type guard for structured API keys client errors.
41
+ */
42
+ export function isApiKeysClientError(
43
+ error: unknown,
44
+ ): error is ApiKeysClientError {
45
+ return error instanceof ApiKeysClientError;
46
+ }
47
+
48
+ export function optionsError(message: string) {
49
+ return new ApiKeysClientError(
50
+ "INVALID_OPTIONS",
51
+ `api-keys options: ${message}`,
52
+ );
53
+ }
54
+
55
+ export function tokenRequiredError() {
56
+ return new ApiKeysClientError(
57
+ "TOKEN_REQUIRED",
58
+ "api-keys: token must not be empty",
59
+ );
60
+ }
61
+
62
+ export function runtimeUnavailableError(message: string) {
63
+ return new ApiKeysClientError(
64
+ "RUNTIME_UNAVAILABLE",
65
+ `api-keys runtime: ${message}`,
66
+ );
67
+ }
@@ -0,0 +1,44 @@
1
+ export { ApiKeys, ApiKeys as default } from "./operations.js";
2
+
3
+ export { ApiKeysClientError, isApiKeysClientError } from "./errors.js";
4
+ export type { ApiKeysClientErrorCode } from "./errors.js";
5
+
6
+ export type { ApiKeysOptions } from "./options.js";
7
+
8
+ export { onInvalidateHookPayloadValidator } from "./types.js";
9
+
10
+ export type {
11
+ ApiKeyId,
12
+ ApiKeyEventMetadata,
13
+ ApiKeyMetadata,
14
+ ApiKeyToken,
15
+ ApiKeysTypeOptions,
16
+ CleanupExpiredArgs,
17
+ CleanupExpiredResult,
18
+ CreateArgs,
19
+ CreateResult,
20
+ GetKeyArgs,
21
+ GetKeyResult,
22
+ InvalidateArgs,
23
+ InvalidateAllArgs,
24
+ InvalidateAllResult,
25
+ InvalidateResult,
26
+ ListEventsArgs,
27
+ ListEventsResult,
28
+ ListKeyEventsArgs,
29
+ ListKeyEventsResult,
30
+ ListKeysArgs,
31
+ ListKeysResult,
32
+ PaginationOptions,
33
+ OnInvalidateHookPayload,
34
+ RefreshArgs,
35
+ RefreshResult,
36
+ RunMutationCtx,
37
+ RunQueryCtx,
38
+ TouchArgs,
39
+ TouchResult,
40
+ UpdateArgs,
41
+ UpdateResult,
42
+ ValidateArgs,
43
+ ValidateResult,
44
+ } from "./types.js";