@better-auth/api-key 1.5.0 → 1.5.1-beta.2

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/types.d.mts CHANGED
@@ -1,358 +1,2 @@
1
- import { t as apiKeySchema } from "./schema-D2f7--fy.mjs";
2
- import { Statements } from "better-auth/plugins/access";
3
- import { Awaitable, GenericEndpointContext, HookEndpointContext, LiteralString } from "@better-auth/core";
4
- import { InferOptionSchema } from "better-auth/types";
5
-
6
- //#region src/types.d.ts
7
- interface ApiKeyOptions {
8
- schema?: InferOptionSchema<ReturnType<typeof apiKeySchema>> | undefined;
9
- }
10
- interface ApiKeyConfigurationOptions {
11
- /**
12
- * The name for this set of API key configurations. Must be unique across different configurations.
13
- */
14
- configId?: LiteralString | undefined;
15
- /**
16
- * The header name to check for API key
17
- * @default "x-api-key"
18
- */
19
- apiKeyHeaders?: (string | string[]) | undefined;
20
- /**
21
- * Disable hashing of the API key.
22
- *
23
- * ⚠️ Security Warning: It's strongly recommended to not disable hashing.
24
- * Storing API keys in plaintext makes them vulnerable to database breaches, potentially exposing all your users' API keys.
25
- *
26
- * @default false
27
- */
28
- disableKeyHashing?: boolean | undefined;
29
- /**
30
- * The function to get the API key from the context
31
- */
32
- customAPIKeyGetter?: ((ctx: HookEndpointContext) => string | null) | undefined;
33
- /**
34
- * A custom function to validate the API key
35
- */
36
- customAPIKeyValidator?: ((options: {
37
- ctx: GenericEndpointContext;
38
- key: string;
39
- }) => Awaitable<boolean>) | undefined;
40
- /**
41
- * custom key generation function
42
- */
43
- customKeyGenerator?: (options: {
44
- /**
45
- * The length of the API key to generate
46
- */
47
- length: number;
48
- /**
49
- * The prefix of the API key to generate
50
- */
51
- prefix: string | undefined;
52
- }) => Awaitable<string>;
53
- /**
54
- * The configuration for storing the starting characters of the API key in the database.
55
- *
56
- * Useful if you want to display the starting characters of an API key in the UI.
57
- */
58
- startingCharactersConfig?: {
59
- /**
60
- * Whether to store the starting characters in the database. If false, we will set `start` to `null`.
61
- *
62
- * @default true
63
- */
64
- shouldStore?: boolean;
65
- /**
66
- * The length of the starting characters to store in the database.
67
- *
68
- * This includes the prefix length.
69
- *
70
- * @default 6
71
- */
72
- charactersLength?: number;
73
- } | undefined;
74
- /**
75
- * The length of the API key. Longer is better. Default is 64. (Doesn't include the prefix length)
76
- * @default 64
77
- */
78
- defaultKeyLength?: number | undefined;
79
- /**
80
- * The prefix of the API key.
81
- *
82
- * Note: We recommend you append an underscore to the prefix to make the prefix more identifiable. (eg `hello_`)
83
- */
84
- defaultPrefix?: string | undefined;
85
- /**
86
- * The maximum length of the prefix.
87
- *
88
- * @default 32
89
- */
90
- maximumPrefixLength?: number | undefined;
91
- /**
92
- * Whether to require a name for the API key.
93
- *
94
- * @default false
95
- */
96
- requireName?: boolean | undefined;
97
- /**
98
- * The minimum length of the prefix.
99
- *
100
- * @default 1
101
- */
102
- minimumPrefixLength?: number | undefined;
103
- /**
104
- * The maximum length of the name.
105
- *
106
- * @default 32
107
- */
108
- maximumNameLength?: number | undefined;
109
- /**
110
- * The minimum length of the name.
111
- *
112
- * @default 1
113
- */
114
- minimumNameLength?: number | undefined;
115
- /**
116
- * Whether to enable metadata for an API key.
117
- *
118
- * @default false
119
- */
120
- enableMetadata?: boolean | undefined;
121
- /**
122
- * Customize the key expiration.
123
- */
124
- keyExpiration?: {
125
- /**
126
- * The default expires time in milliseconds.
127
- *
128
- * If `null`, then there will be no expiration time.
129
- *
130
- * @default null
131
- */
132
- defaultExpiresIn?: number | null;
133
- /**
134
- * Whether to disable the expires time passed from the client.
135
- *
136
- * If `true`, the expires time will be based on the default values.
137
- *
138
- * @default false
139
- */
140
- disableCustomExpiresTime?: boolean;
141
- /**
142
- * The minimum expiresIn value allowed to be set from the client. in days.
143
- *
144
- * @default 1
145
- */
146
- minExpiresIn?: number;
147
- /**
148
- * The maximum expiresIn value allowed to be set from the client. in days.
149
- *
150
- * @default 365
151
- */
152
- maxExpiresIn?: number;
153
- } | undefined;
154
- /**
155
- * Default rate limiting options.
156
- */
157
- rateLimit?: {
158
- /**
159
- * Whether to enable rate limiting.
160
- *
161
- * @default true
162
- */
163
- enabled?: boolean;
164
- /**
165
- * The duration in milliseconds where each request is counted.
166
- *
167
- * Once the `maxRequests` is reached, the request will be rejected until the `timeWindow` has passed, at which point the `timeWindow` will be reset.
168
- *
169
- * @default 1000 * 60 * 60 * 24 // 1 day
170
- */
171
- timeWindow?: number;
172
- /**
173
- * Maximum amount of requests allowed within a window
174
- *
175
- * Once the `maxRequests` is reached, the request will be rejected until the `timeWindow` has passed, at which point the `timeWindow` will be reset.
176
- *
177
- * @default 10 // 10 requests per day
178
- */
179
- maxRequests?: number;
180
- } | undefined;
181
- /**
182
- * An API Key can represent a valid session, so we automatically mock a session for the user if we find a valid API key in the request headers.
183
- *
184
- * ⚠︎ This is not recommended for production use, as it can lead to security issues.
185
- * @default false
186
- */
187
- enableSessionForAPIKeys?: boolean | undefined;
188
- /**
189
- * Permissions for the API key.
190
- */
191
- permissions?: {
192
- /**
193
- * The default permissions for the API key.
194
- */
195
- defaultPermissions?: Statements | ((referenceId: string, ctx: GenericEndpointContext) => Awaitable<Statements>);
196
- } | undefined;
197
- /**
198
- * Storage backend for API keys.
199
- *
200
- * - `"database"`: Store API keys in the database adapter (default)
201
- * - `"secondary-storage"`: Store API keys in the configured secondary storage (e.g., Redis)
202
- *
203
- * @default "database"
204
- */
205
- storage?: "database" | "secondary-storage" | undefined;
206
- /**
207
- * When `storage` is `"secondary-storage"`, enable fallback to database if key is not found in secondary storage.
208
- *
209
- * Useful for gradual migration from database to secondary storage.
210
- *
211
- * @default false
212
- */
213
- fallbackToDatabase?: boolean | undefined;
214
- /**
215
- * Custom storage methods for API keys.
216
- *
217
- * If provided, these methods will be used instead of `ctx.context.secondaryStorage`.
218
- * Custom methods take precedence over global secondary storage.
219
- *
220
- * Useful when you want to use a different storage backend specifically for API keys,
221
- * or when you need custom logic for storage operations.
222
- */
223
- customStorage?: {
224
- /**
225
- * Get a value from storage
226
- */
227
- get: (key: string) => Awaitable<unknown>;
228
- /**
229
- * Set a value in storage
230
- */
231
- set: (key: string, value: string, ttl?: number | undefined) => Awaitable<void | null | unknown>;
232
- /**
233
- * Delete a value from storage
234
- */
235
- delete: (key: string) => Awaitable<void | null | string>;
236
- } | undefined;
237
- /**
238
- * Defer non-critical updates (rate limiting counters, timestamps, remaining count)
239
- * to run after the response is sent using the global `advanced.backgroundTasks` handler.
240
- *
241
- * Requires `advanced.backgroundTasks.handler` to be configured in the main auth options.
242
- *
243
- * ⚠️ Warning: Enabling this introduces eventual consistency where the response
244
- * returns optimistic data before the database is updated. If the deferred update
245
- * fails, the database will have stale values. Only enable if your application
246
- * can tolerate this trade-off for improved latency.
247
- *
248
- * @default false
249
- */
250
- deferUpdates?: boolean | undefined;
251
- /**
252
- * What the API key references. This determines ownership over the API key.
253
- *
254
- * @default "user"
255
- */
256
- references?: "user" | "organization" | undefined;
257
- }
258
- type ApiKey = {
259
- /**
260
- * ID
261
- */
262
- id: string;
263
- /**
264
- * The configuration ID this key belongs to.
265
- * Use this to look up the configuration to determine the reference type (user vs organization).
266
- */
267
- configId: string;
268
- /**
269
- * The name of the key
270
- */
271
- name: string | null;
272
- /**
273
- * Shows the first few characters of the API key, including the prefix.
274
- * This allows you to show those few characters in the UI to make it easier for users to identify the API key.
275
- */
276
- start: string | null;
277
- /**
278
- * The API Key prefix. Stored as plain text.
279
- */
280
- prefix: string | null;
281
- /**
282
- * The hashed API key value
283
- */
284
- key: string;
285
- /**
286
- * The ID of the entity that owns this key (userId or organizationId based on config's `references` setting)
287
- */
288
- referenceId: string;
289
- /**
290
- * The interval in milliseconds between refills of the `remaining` count
291
- *
292
- * @example 3600000 // refill every hour (3600000ms = 1h)
293
- */
294
- refillInterval: number | null;
295
- /**
296
- * The amount to refill
297
- */
298
- refillAmount: number | null;
299
- /**
300
- * The last refill date
301
- */
302
- lastRefillAt: Date | null;
303
- /**
304
- * Sets if key is enabled or disabled
305
- *
306
- * @default true
307
- */
308
- enabled: boolean;
309
- /**
310
- * Whether the key has rate limiting enabled.
311
- */
312
- rateLimitEnabled: boolean;
313
- /**
314
- * The duration in milliseconds
315
- */
316
- rateLimitTimeWindow: number | null;
317
- /**
318
- * Maximum amount of requests allowed within a window
319
- */
320
- rateLimitMax: number | null;
321
- /**
322
- * The number of requests made within the rate limit time window
323
- */
324
- requestCount: number;
325
- /**
326
- * Remaining requests (every time API key is used this should updated and should be updated on refill as well)
327
- */
328
- remaining: number | null;
329
- /**
330
- * When last request occurred
331
- */
332
- lastRequest: Date | null;
333
- /**
334
- * Expiry date of a key
335
- */
336
- expiresAt: Date | null;
337
- /**
338
- * created at
339
- */
340
- createdAt: Date;
341
- /**
342
- * updated at
343
- */
344
- updatedAt: Date;
345
- /**
346
- * Extra metadata about the apiKey
347
- */
348
- metadata: Record<string, any> | null;
349
- /**
350
- * Permissions for the API key
351
- */
352
- permissions?: ({
353
- [key: string]: string[];
354
- } | null) | undefined;
355
- };
356
- //#endregion
357
- export { ApiKey, ApiKeyConfigurationOptions, ApiKeyOptions };
358
- //# sourceMappingURL=types.d.mts.map
1
+ import { n as ApiKeyConfigurationOptions, r as ApiKeyOptions, t as ApiKey } from "./types-CCe5L05Y.mjs";
2
+ export { ApiKey, ApiKeyConfigurationOptions, ApiKeyOptions };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@better-auth/api-key",
3
- "version": "1.5.0",
3
+ "version": "1.5.1-beta.2",
4
4
  "description": "API Key plugin for Better Auth.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -59,14 +59,14 @@
59
59
  "zod": "^4.3.6"
60
60
  },
61
61
  "devDependencies": {
62
- "tsdown": "^0.20.3",
63
- "@better-auth/core": "1.5.0",
64
- "better-auth": "1.5.0"
62
+ "tsdown": "0.21.0-beta.2",
63
+ "@better-auth/core": "1.5.1-beta.2",
64
+ "better-auth": "1.5.1-beta.2"
65
65
  },
66
66
  "peerDependencies": {
67
67
  "@better-auth/utils": "0.3.1",
68
- "@better-auth/core": "1.5.0",
69
- "better-auth": "1.5.0"
68
+ "@better-auth/core": "1.5.1-beta.2",
69
+ "better-auth": "1.5.1-beta.2"
70
70
  },
71
71
  "scripts": {
72
72
  "build": "tsdown",
@@ -1,50 +0,0 @@
1
- import { ApiKeyConfigurationOptions } from "./types.mjs";
2
- import * as better_auth0 from "better-auth";
3
- import "zod";
4
- import "better-call";
5
- import { Awaitable } from "@better-auth/core";
6
-
7
- //#region src/routes/index.d.ts
8
- type PredefinedApiKeyOptions = ApiKeyConfigurationOptions & Required<Pick<ApiKeyConfigurationOptions, "apiKeyHeaders" | "defaultKeyLength" | "keyExpiration" | "rateLimit" | "maximumPrefixLength" | "minimumPrefixLength" | "maximumNameLength" | "disableKeyHashing" | "minimumNameLength" | "requireName" | "enableMetadata" | "enableSessionForAPIKeys" | "startingCharactersConfig" | "storage" | "fallbackToDatabase" | "deferUpdates">> & {
9
- keyExpiration: Required<NonNullable<ApiKeyConfigurationOptions["keyExpiration"]>>;
10
- startingCharactersConfig: Required<NonNullable<ApiKeyConfigurationOptions["startingCharactersConfig"]>>;
11
- rateLimit: Required<NonNullable<ApiKeyConfigurationOptions["rateLimit"]>>;
12
- };
13
- //#endregion
14
- //#region src/error-codes.d.ts
15
- declare const API_KEY_ERROR_CODES: {
16
- INVALID_METADATA_TYPE: better_auth0.RawError<"INVALID_METADATA_TYPE">;
17
- REFILL_AMOUNT_AND_INTERVAL_REQUIRED: better_auth0.RawError<"REFILL_AMOUNT_AND_INTERVAL_REQUIRED">;
18
- REFILL_INTERVAL_AND_AMOUNT_REQUIRED: better_auth0.RawError<"REFILL_INTERVAL_AND_AMOUNT_REQUIRED">;
19
- USER_BANNED: better_auth0.RawError<"USER_BANNED">;
20
- UNAUTHORIZED_SESSION: better_auth0.RawError<"UNAUTHORIZED_SESSION">;
21
- KEY_NOT_FOUND: better_auth0.RawError<"KEY_NOT_FOUND">;
22
- KEY_DISABLED: better_auth0.RawError<"KEY_DISABLED">;
23
- KEY_EXPIRED: better_auth0.RawError<"KEY_EXPIRED">;
24
- USAGE_EXCEEDED: better_auth0.RawError<"USAGE_EXCEEDED">;
25
- KEY_NOT_RECOVERABLE: better_auth0.RawError<"KEY_NOT_RECOVERABLE">;
26
- EXPIRES_IN_IS_TOO_SMALL: better_auth0.RawError<"EXPIRES_IN_IS_TOO_SMALL">;
27
- EXPIRES_IN_IS_TOO_LARGE: better_auth0.RawError<"EXPIRES_IN_IS_TOO_LARGE">;
28
- INVALID_REMAINING: better_auth0.RawError<"INVALID_REMAINING">;
29
- INVALID_PREFIX_LENGTH: better_auth0.RawError<"INVALID_PREFIX_LENGTH">;
30
- INVALID_NAME_LENGTH: better_auth0.RawError<"INVALID_NAME_LENGTH">;
31
- METADATA_DISABLED: better_auth0.RawError<"METADATA_DISABLED">;
32
- RATE_LIMIT_EXCEEDED: better_auth0.RawError<"RATE_LIMIT_EXCEEDED">;
33
- NO_VALUES_TO_UPDATE: better_auth0.RawError<"NO_VALUES_TO_UPDATE">;
34
- KEY_DISABLED_EXPIRATION: better_auth0.RawError<"KEY_DISABLED_EXPIRATION">;
35
- INVALID_API_KEY: better_auth0.RawError<"INVALID_API_KEY">;
36
- INVALID_USER_ID_FROM_API_KEY: better_auth0.RawError<"INVALID_USER_ID_FROM_API_KEY">;
37
- INVALID_REFERENCE_ID_FROM_API_KEY: better_auth0.RawError<"INVALID_REFERENCE_ID_FROM_API_KEY">;
38
- INVALID_API_KEY_GETTER_RETURN_TYPE: better_auth0.RawError<"INVALID_API_KEY_GETTER_RETURN_TYPE">;
39
- SERVER_ONLY_PROPERTY: better_auth0.RawError<"SERVER_ONLY_PROPERTY">;
40
- FAILED_TO_UPDATE_API_KEY: better_auth0.RawError<"FAILED_TO_UPDATE_API_KEY">;
41
- NAME_REQUIRED: better_auth0.RawError<"NAME_REQUIRED">;
42
- ORGANIZATION_ID_REQUIRED: better_auth0.RawError<"ORGANIZATION_ID_REQUIRED">;
43
- USER_NOT_MEMBER_OF_ORGANIZATION: better_auth0.RawError<"USER_NOT_MEMBER_OF_ORGANIZATION">;
44
- INSUFFICIENT_API_KEY_PERMISSIONS: better_auth0.RawError<"INSUFFICIENT_API_KEY_PERMISSIONS">;
45
- NO_DEFAULT_API_KEY_CONFIGURATION_FOUND: better_auth0.RawError<"NO_DEFAULT_API_KEY_CONFIGURATION_FOUND">;
46
- ORGANIZATION_PLUGIN_REQUIRED: better_auth0.RawError<"ORGANIZATION_PLUGIN_REQUIRED">;
47
- };
48
- //#endregion
49
- export { PredefinedApiKeyOptions as n, API_KEY_ERROR_CODES as t };
50
- //# sourceMappingURL=error-codes-xCoIeQ-k.d.mts.map
@@ -1,201 +0,0 @@
1
- import * as better_auth0 from "better-auth";
2
-
3
- //#region src/schema.d.ts
4
- declare const apiKeySchema: ({
5
- defaultRateLimitMax,
6
- defaultTimeWindow
7
- }: {
8
- defaultTimeWindow: number;
9
- defaultRateLimitMax: number;
10
- }) => {
11
- apikey: {
12
- fields: {
13
- configId: {
14
- type: "string";
15
- required: true;
16
- defaultValue: string;
17
- input: false;
18
- index: true;
19
- };
20
- /**
21
- * The name of the key.
22
- */
23
- name: {
24
- type: "string";
25
- required: false;
26
- input: false;
27
- };
28
- /**
29
- * Shows the first few characters of the API key
30
- * This allows you to show those few characters in the UI to make it easier for users to identify the API key.
31
- */
32
- start: {
33
- type: "string";
34
- required: false;
35
- input: false;
36
- };
37
- /**
38
- * The ID of the entity that owns this key (userId or organizationId based on config's `references` setting).
39
- */
40
- referenceId: {
41
- type: "string";
42
- required: true;
43
- input: false;
44
- index: true;
45
- };
46
- /**
47
- * The prefix of the key.
48
- */
49
- prefix: {
50
- type: "string";
51
- required: false;
52
- input: false;
53
- };
54
- /**
55
- * The hashed key value.
56
- */
57
- key: {
58
- type: "string";
59
- required: true;
60
- input: false;
61
- index: true;
62
- };
63
- /**
64
- * The interval to refill the key in milliseconds.
65
- */
66
- refillInterval: {
67
- type: "number";
68
- required: false;
69
- input: false;
70
- };
71
- /**
72
- * The amount to refill the remaining count of the key.
73
- */
74
- refillAmount: {
75
- type: "number";
76
- required: false;
77
- input: false;
78
- };
79
- /**
80
- * The date and time when the key was last refilled.
81
- */
82
- lastRefillAt: {
83
- type: "date";
84
- required: false;
85
- input: false;
86
- };
87
- /**
88
- * Whether the key is enabled.
89
- */
90
- enabled: {
91
- type: "boolean";
92
- required: false;
93
- input: false;
94
- defaultValue: true;
95
- };
96
- /**
97
- * Whether the key has rate limiting enabled.
98
- */
99
- rateLimitEnabled: {
100
- type: "boolean";
101
- required: false;
102
- input: false;
103
- defaultValue: true;
104
- };
105
- /**
106
- * The time window in milliseconds for the rate limit.
107
- */
108
- rateLimitTimeWindow: {
109
- type: "number";
110
- required: false;
111
- input: false;
112
- defaultValue: number;
113
- };
114
- /**
115
- * The maximum number of requests allowed within the `rateLimitTimeWindow`.
116
- */
117
- rateLimitMax: {
118
- type: "number";
119
- required: false;
120
- input: false;
121
- defaultValue: number;
122
- };
123
- /**
124
- * The number of requests made within the rate limit time window
125
- */
126
- requestCount: {
127
- type: "number";
128
- required: false;
129
- input: false;
130
- defaultValue: number;
131
- };
132
- /**
133
- * The remaining number of requests before the key is revoked.
134
- *
135
- * If this is null, then the key is not revoked.
136
- *
137
- * If `refillInterval` & `refillAmount` are provided, than this will refill accordingly.
138
- */
139
- remaining: {
140
- type: "number";
141
- required: false;
142
- input: false;
143
- };
144
- /**
145
- * The date and time of the last request made to the key.
146
- */
147
- lastRequest: {
148
- type: "date";
149
- required: false;
150
- input: false;
151
- };
152
- /**
153
- * The date and time when the key will expire.
154
- */
155
- expiresAt: {
156
- type: "date";
157
- required: false;
158
- input: false;
159
- };
160
- /**
161
- * The date and time when the key was created.
162
- */
163
- createdAt: {
164
- type: "date";
165
- required: true;
166
- input: false;
167
- };
168
- /**
169
- * The date and time when the key was last updated.
170
- */
171
- updatedAt: {
172
- type: "date";
173
- required: true;
174
- input: false;
175
- };
176
- /**
177
- * The permissions of the key.
178
- */
179
- permissions: {
180
- type: "string";
181
- required: false;
182
- input: false;
183
- };
184
- /**
185
- * Any additional metadata you want to store with the key.
186
- */
187
- metadata: {
188
- type: "string";
189
- required: false;
190
- input: true;
191
- transform: {
192
- input(value: better_auth0.DBPrimitive): string;
193
- output(value: better_auth0.DBPrimitive): any;
194
- };
195
- };
196
- };
197
- };
198
- };
199
- //#endregion
200
- export { apiKeySchema as t };
201
- //# sourceMappingURL=schema-D2f7--fy.d.mts.map