@better-auth/api-key 1.5.5 → 1.5.7-beta.1

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/dist/index.d.mts CHANGED
@@ -1,3 +1,3 @@
1
- import { n as ApiKeyConfigurationOptions, r as ApiKeyOptions, t as ApiKey } from "./types-CCe5L05Y.mjs";
2
- import { i as API_KEY_ERROR_CODES, n as apiKey, r as defaultKeyHasher, t as API_KEY_TABLE_NAME } from "./index-CP8iCAEj.mjs";
1
+ import { n as ApiKeyConfigurationOptions, r as ApiKeyOptions, t as ApiKey } from "./types-BR70O3Q3.mjs";
2
+ import { i as API_KEY_ERROR_CODES, n as apiKey, r as defaultKeyHasher, t as API_KEY_TABLE_NAME } from "./index-WXH3i1Uh.mjs";
3
3
  export { API_KEY_ERROR_CODES, API_KEY_TABLE_NAME, ApiKey, ApiKeyConfigurationOptions, ApiKeyOptions, apiKey, defaultKeyHasher };
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as API_KEY_ERROR_CODES } from "./error-codes-vwTWW2ez.mjs";
1
+ import { t as API_KEY_ERROR_CODES } from "./error-codes-C8MQDPqn.mjs";
2
2
  import { createAuthEndpoint, createAuthMiddleware } from "@better-auth/core/api";
3
3
  import { base64Url } from "@better-auth/utils/base64";
4
4
  import { createHash } from "@better-auth/utils/hash";
@@ -9,13 +9,11 @@ import { mergeSchema } from "better-auth/db";
9
9
  import { APIError as APIError$1 } from "@better-auth/core/error";
10
10
  import { generateId } from "@better-auth/core/utils/id";
11
11
  import { safeJSONParse } from "@better-auth/core/utils/json";
12
- import * as z$1 from "zod";
12
+ import * as z from "zod";
13
13
  import { isDevelopment, isTest } from "@better-auth/core/env";
14
14
  import { isValidIP, normalizeIP } from "@better-auth/core/utils/ip";
15
- import * as z from "zod/v4";
16
15
  import { role } from "better-auth/plugins/access";
17
16
  import { parseJSON } from "better-auth/client";
18
-
19
17
  //#region src/adapter.ts
20
18
  /**
21
19
  * Parses double-stringified metadata synchronously without updating the database.
@@ -478,7 +476,6 @@ async function listApiKeys$1(ctx, referenceId, opts, paginationOpts) {
478
476
  total
479
477
  };
480
478
  }
481
-
482
479
  //#endregion
483
480
  //#region src/org-authorization.ts
484
481
  /**
@@ -486,8 +483,9 @@ async function listApiKeys$1(ctx, referenceId, opts, paginationOpts) {
486
483
  * Returns null if the organization plugin is not installed.
487
484
  */
488
485
  function getOrgOptions(ctx) {
489
- if (ctx.context.orgOptions) return ctx.context.orgOptions;
490
- const orgPlugin = ctx.context.getPlugin?.("organization");
486
+ const context = ctx.context;
487
+ if ("orgOptions" in context && context.orgOptions) return context.orgOptions;
488
+ const orgPlugin = context.getPlugin?.("organization");
491
489
  if (orgPlugin && "options" in orgPlugin) return orgPlugin.options;
492
490
  return null;
493
491
  }
@@ -549,7 +547,6 @@ async function checkPermission(ctx, role, organizationId, action, orgOptions) {
549
547
  return false;
550
548
  }
551
549
  }
552
-
553
550
  //#endregion
554
551
  //#region src/utils.ts
555
552
  const getDate = (span, unit = "ms") => {
@@ -573,24 +570,23 @@ function getIp(req, options) {
573
570
  if (isTest() || isDevelopment()) return LOCALHOST_IP;
574
571
  return null;
575
572
  }
576
-
577
573
  //#endregion
578
574
  //#region src/routes/create-api-key.ts
579
- const createApiKeyBodySchema = z$1.object({
580
- configId: z$1.string().meta({ description: "The configuration ID to use for the API key. If not provided, the default configuration will be used." }).optional(),
581
- name: z$1.string().meta({ description: "Name of the Api Key" }).optional(),
582
- expiresIn: z$1.number().meta({ description: "Expiration time of the Api Key in seconds" }).min(1).optional().nullable().default(null),
583
- prefix: z$1.string().meta({ description: "Prefix of the Api Key" }).regex(/^[a-zA-Z0-9_-]+$/, { message: "Invalid prefix format, must be alphanumeric and contain only underscores and hyphens." }).optional(),
584
- remaining: z$1.number().meta({ description: "Remaining number of requests. Server side only" }).min(0).optional().nullable().default(null),
585
- metadata: z$1.any().optional(),
586
- refillAmount: z$1.number().meta({ description: "Amount to refill the remaining count of the Api Key. server-only. Eg: 100" }).min(1).optional(),
587
- refillInterval: z$1.number().meta({ description: "Interval to refill the Api Key in milliseconds. server-only. Eg: 1000" }).optional(),
588
- rateLimitTimeWindow: z$1.number().meta({ description: "The duration in milliseconds where each request is counted. Once the `maxRequests` is reached, the request will be rejected until the `timeWindow` has passed, at which point the `timeWindow` will be reset. server-only. Eg: 1000" }).optional(),
589
- rateLimitMax: z$1.number().meta({ description: "Maximum amount of requests allowed within a window. Once the `maxRequests` is reached, the request will be rejected until the `timeWindow` has passed, at which point the `timeWindow` will be reset. server-only. Eg: 100" }).optional(),
590
- rateLimitEnabled: z$1.boolean().meta({ description: "Whether the key has rate limiting enabled. server-only. Eg: true" }).optional(),
591
- permissions: z$1.record(z$1.string(), z$1.array(z$1.string())).meta({ description: "Permissions of the Api Key." }).optional(),
592
- userId: z$1.coerce.string().meta({ description: "User Id of the user that the Api Key belongs to. server-only. Eg: \"user-id\"" }).optional(),
593
- organizationId: z$1.coerce.string().meta({ description: "Organization Id of the organization that the Api Key belongs to. Eg: 'org-id'" }).optional()
575
+ const createApiKeyBodySchema = z.object({
576
+ configId: z.string().meta({ description: "The configuration ID to use for the API key. If not provided, the default configuration will be used." }).optional(),
577
+ name: z.string().meta({ description: "Name of the Api Key" }).optional(),
578
+ expiresIn: z.number().meta({ description: "Expiration time of the Api Key in seconds" }).min(1).optional().nullable().default(null),
579
+ prefix: z.string().meta({ description: "Prefix of the Api Key" }).regex(/^[a-zA-Z0-9_-]+$/, { message: "Invalid prefix format, must be alphanumeric and contain only underscores and hyphens." }).optional(),
580
+ remaining: z.number().meta({ description: "Remaining number of requests. Server side only" }).min(0).optional().nullable().default(null),
581
+ metadata: z.any().optional(),
582
+ refillAmount: z.number().meta({ description: "Amount to refill the remaining count of the Api Key. server-only. Eg: 100" }).min(1).optional(),
583
+ refillInterval: z.number().meta({ description: "Interval to refill the Api Key in milliseconds. server-only. Eg: 1000" }).optional(),
584
+ rateLimitTimeWindow: z.number().meta({ description: "The duration in milliseconds where each request is counted. Once the `maxRequests` is reached, the request will be rejected until the `timeWindow` has passed, at which point the `timeWindow` will be reset. server-only. Eg: 1000" }).optional(),
585
+ rateLimitMax: z.number().meta({ description: "Maximum amount of requests allowed within a window. Once the `maxRequests` is reached, the request will be rejected until the `timeWindow` has passed, at which point the `timeWindow` will be reset. server-only. Eg: 100" }).optional(),
586
+ rateLimitEnabled: z.boolean().meta({ description: "Whether the key has rate limiting enabled. server-only. Eg: true" }).optional(),
587
+ permissions: z.record(z.string(), z.array(z.string())).meta({ description: "Permissions of the Api Key." }).optional(),
588
+ userId: z.coerce.string().meta({ description: "User Id of the user that the Api Key belongs to. server-only. Eg: \"user-id\"" }).optional(),
589
+ organizationId: z.coerce.string().meta({ description: "Organization Id of the organization that the Api Key belongs to. Eg: 'org-id'" }).optional()
594
590
  });
595
591
  function createApiKey({ defaultKeyGenerator, configurations, schema, deleteAllExpiredApiKeys }) {
596
592
  return createAuthEndpoint("/api-key/create", {
@@ -839,7 +835,7 @@ function createApiKey({ defaultKeyGenerator, configurations, schema, deleteAllEx
839
835
  });
840
836
  await setApiKey(ctx, apiKey, opts);
841
837
  } else if (opts.storage === "secondary-storage") {
842
- const id = ctx.context.generateId({ model: API_KEY_TABLE_NAME }) || generateId();
838
+ const id = ctx.context.generateId({ model: "apikey" }) || generateId();
843
839
  apiKey = {
844
840
  ...data,
845
841
  id
@@ -857,7 +853,6 @@ function createApiKey({ defaultKeyGenerator, configurations, schema, deleteAllEx
857
853
  });
858
854
  });
859
855
  }
860
-
861
856
  //#endregion
862
857
  //#region src/routes/delete-all-expired-api-keys.ts
863
858
  function deleteAllExpiredApiKeysEndpoint({ deleteAllExpiredApiKeys }) {
@@ -877,12 +872,11 @@ function deleteAllExpiredApiKeysEndpoint({ deleteAllExpiredApiKeys }) {
877
872
  });
878
873
  });
879
874
  }
880
-
881
875
  //#endregion
882
876
  //#region src/routes/delete-api-key.ts
883
- const deleteApiKeyBodySchema = z$1.object({
884
- configId: z$1.string().meta({ description: "The configuration ID to use for the API key lookup. If not provided, the default configuration will be used." }).optional(),
885
- keyId: z$1.string().meta({ description: "The id of the Api Key" })
877
+ const deleteApiKeyBodySchema = z.object({
878
+ configId: z.string().meta({ description: "The configuration ID to use for the API key lookup. If not provided, the default configuration will be used." }).optional(),
879
+ keyId: z.string().meta({ description: "The id of the Api Key" })
886
880
  });
887
881
  function deleteApiKey({ configurations, schema, deleteAllExpiredApiKeys }) {
888
882
  return createAuthEndpoint("/api-key/delete", {
@@ -948,12 +942,11 @@ function deleteApiKey({ configurations, schema, deleteAllExpiredApiKeys }) {
948
942
  return ctx.json({ success: true });
949
943
  });
950
944
  }
951
-
952
945
  //#endregion
953
946
  //#region src/routes/get-api-key.ts
954
- const getApiKeyQuerySchema = z$1.object({
955
- configId: z$1.string().meta({ description: "The configuration ID to use for the API key lookup. If not provided, the default configuration will be used." }).optional(),
956
- id: z$1.string().meta({ description: "The id of the Api Key" })
947
+ const getApiKeyQuerySchema = z.object({
948
+ configId: z.string().meta({ description: "The configuration ID to use for the API key lookup. If not provided, the default configuration will be used." }).optional(),
949
+ id: z.string().meta({ description: "The id of the Api Key" })
957
950
  });
958
951
  function getApiKey({ configurations, schema, deleteAllExpiredApiKeys }) {
959
952
  return createAuthEndpoint("/api-key/get", {
@@ -1101,7 +1094,6 @@ function getApiKey({ configurations, schema, deleteAllExpiredApiKeys }) {
1101
1094
  });
1102
1095
  });
1103
1096
  }
1104
-
1105
1097
  //#endregion
1106
1098
  //#region src/routes/list-api-keys.ts
1107
1099
  /**
@@ -1337,24 +1329,23 @@ function listApiKeys({ configurations, schema, deleteAllExpiredApiKeys }) {
1337
1329
  });
1338
1330
  });
1339
1331
  }
1340
-
1341
1332
  //#endregion
1342
1333
  //#region src/routes/update-api-key.ts
1343
- const updateApiKeyBodySchema = z$1.object({
1344
- configId: z$1.string().meta({ description: "The configuration ID to use for the API key lookup. If not provided, the default configuration will be used." }).optional(),
1345
- keyId: z$1.string().meta({ description: "The id of the Api Key" }),
1346
- userId: z$1.coerce.string().meta({ description: "The id of the user which the api key belongs to. server-only. Eg: \"some-user-id\"" }).optional(),
1347
- name: z$1.string().meta({ description: "The name of the key" }).optional(),
1348
- enabled: z$1.boolean().meta({ description: "Whether the Api Key is enabled or not" }).optional(),
1349
- remaining: z$1.number().meta({ description: "The number of remaining requests" }).min(1).optional(),
1350
- refillAmount: z$1.number().meta({ description: "The refill amount" }).optional(),
1351
- refillInterval: z$1.number().meta({ description: "The refill interval" }).optional(),
1352
- metadata: z$1.any().optional(),
1353
- expiresIn: z$1.number().meta({ description: "Expiration time of the Api Key in seconds" }).min(1).optional().nullable(),
1354
- rateLimitEnabled: z$1.boolean().meta({ description: "Whether the key has rate limiting enabled." }).optional(),
1355
- rateLimitTimeWindow: z$1.number().meta({ description: "The duration in milliseconds where each request is counted. server-only. Eg: 1000" }).optional(),
1356
- rateLimitMax: z$1.number().meta({ description: "Maximum amount of requests allowed within a window. Once the `maxRequests` is reached, the request will be rejected until the `timeWindow` has passed, at which point the `timeWindow` will be reset. server-only. Eg: 100" }).optional(),
1357
- permissions: z$1.record(z$1.string(), z$1.array(z$1.string())).meta({ description: "Update the permissions on the API Key. server-only." }).optional().nullable()
1334
+ const updateApiKeyBodySchema = z.object({
1335
+ configId: z.string().meta({ description: "The configuration ID to use for the API key lookup. If not provided, the default configuration will be used." }).optional(),
1336
+ keyId: z.string().meta({ description: "The id of the Api Key" }),
1337
+ userId: z.coerce.string().meta({ description: "The id of the user which the api key belongs to. server-only. Eg: \"some-user-id\"" }).optional(),
1338
+ name: z.string().meta({ description: "The name of the key" }).optional(),
1339
+ enabled: z.boolean().meta({ description: "Whether the Api Key is enabled or not" }).optional(),
1340
+ remaining: z.number().meta({ description: "The number of remaining requests" }).min(1).optional(),
1341
+ refillAmount: z.number().meta({ description: "The refill amount" }).optional(),
1342
+ refillInterval: z.number().meta({ description: "The refill interval" }).optional(),
1343
+ metadata: z.any().optional(),
1344
+ expiresIn: z.number().meta({ description: "Expiration time of the Api Key in seconds" }).min(1).optional().nullable(),
1345
+ rateLimitEnabled: z.boolean().meta({ description: "Whether the key has rate limiting enabled." }).optional(),
1346
+ rateLimitTimeWindow: z.number().meta({ description: "The duration in milliseconds where each request is counted. server-only. Eg: 1000" }).optional(),
1347
+ rateLimitMax: z.number().meta({ description: "Maximum amount of requests allowed within a window. Once the `maxRequests` is reached, the request will be rejected until the `timeWindow` has passed, at which point the `timeWindow` will be reset. server-only. Eg: 100" }).optional(),
1348
+ permissions: z.record(z.string(), z.array(z.string())).meta({ description: "Update the permissions on the API Key. server-only." }).optional().nullable()
1358
1349
  });
1359
1350
  function updateApiKey({ configurations, schema, deleteAllExpiredApiKeys }) {
1360
1351
  return createAuthEndpoint("/api-key/update", {
@@ -1577,7 +1568,6 @@ function updateApiKey({ configurations, schema, deleteAllExpiredApiKeys }) {
1577
1568
  });
1578
1569
  });
1579
1570
  }
1580
-
1581
1571
  //#endregion
1582
1572
  //#region src/rate-limit.ts
1583
1573
  /**
@@ -1646,7 +1636,6 @@ function isRateLimited(apiKey, opts) {
1646
1636
  }
1647
1637
  };
1648
1638
  }
1649
-
1650
1639
  //#endregion
1651
1640
  //#region src/routes/verify-api-key.ts
1652
1641
  async function validateApiKey({ hashedKey, ctx, opts, schema, permissions }) {
@@ -1783,10 +1772,10 @@ async function validateApiKey({ hashedKey, ctx, opts, schema, permissions }) {
1783
1772
  }
1784
1773
  return newApiKey;
1785
1774
  }
1786
- const verifyApiKeyBodySchema = z$1.object({
1787
- configId: z$1.string().meta({ description: "The configuration ID to use for verification. If not provided, the default configuration will be used." }).optional(),
1788
- key: z$1.string().meta({ description: "The key to verify" }),
1789
- permissions: z$1.record(z$1.string(), z$1.array(z$1.string())).meta({ description: "The permissions to verify." }).optional()
1775
+ const verifyApiKeyBodySchema = z.object({
1776
+ configId: z.string().meta({ description: "The configuration ID to use for verification. If not provided, the default configuration will be used." }).optional(),
1777
+ key: z.string().meta({ description: "The key to verify" }),
1778
+ permissions: z.record(z.string(), z.array(z.string())).meta({ description: "The permissions to verify." }).optional()
1790
1779
  });
1791
1780
  function verifyApiKey({ configurations, schema, deleteAllExpiredApiKeys }) {
1792
1781
  return createAuthEndpoint({
@@ -1859,7 +1848,6 @@ function verifyApiKey({ configurations, schema, deleteAllExpiredApiKeys }) {
1859
1848
  });
1860
1849
  });
1861
1850
  }
1862
-
1863
1851
  //#endregion
1864
1852
  //#region src/routes/index.ts
1865
1853
  function resolveConfiguration(authContext, configurations, configId) {
@@ -1951,7 +1939,6 @@ function createApiKeyRoutes({ defaultKeyGenerator, configurations, schema }) {
1951
1939
  deleteAllExpiredApiKeys: deleteAllExpiredApiKeysEndpoint({ deleteAllExpiredApiKeys })
1952
1940
  };
1953
1941
  }
1954
-
1955
1942
  //#endregion
1956
1943
  //#region src/schema.ts
1957
1944
  const apiKeySchema = ({ defaultRateLimitMax, defaultTimeWindow }) => ({ apikey: { fields: {
@@ -2079,7 +2066,6 @@ const apiKeySchema = ({ defaultRateLimitMax, defaultTimeWindow }) => ({ apikey:
2079
2066
  }
2080
2067
  }
2081
2068
  } } });
2082
-
2083
2069
  //#endregion
2084
2070
  //#region src/index.ts
2085
2071
  const defaultKeyHasher = async (key) => {
@@ -2222,11 +2208,10 @@ function apiKey(_configurations, _options) {
2222
2208
  listApiKeys: routes.listApiKeys,
2223
2209
  deleteAllExpiredApiKeys: routes.deleteAllExpiredApiKeys
2224
2210
  },
2225
- schema,
2226
- configurations
2211
+ schema
2227
2212
  };
2228
2213
  }
2229
-
2230
2214
  //#endregion
2231
2215
  export { API_KEY_ERROR_CODES, API_KEY_TABLE_NAME, apiKey, defaultKeyHasher };
2216
+
2232
2217
  //# sourceMappingURL=index.mjs.map