UNPKG

better-auth

Version:

The most comprehensive authentication framework for TypeScript.

1 lines • 6.54 kB
{"version":3,"file":"schema.mjs","names":[],"sources":["../../../src/plugins/api-key/schema.ts"],"sourcesContent":["import type { BetterAuthPluginDBSchema } from \"@better-auth/core/db\";\nimport { parseJSON } from \"../../client/parser\";\n\nexport const apiKeySchema = ({\n\ttimeWindow,\n\trateLimitMax,\n}: {\n\ttimeWindow: number;\n\trateLimitMax: number;\n}) =>\n\t({\n\t\tapikey: {\n\t\t\tfields: {\n\t\t\t\t/**\n\t\t\t\t * The name of the key.\n\t\t\t\t */\n\t\t\t\tname: {\n\t\t\t\t\ttype: \"string\",\n\t\t\t\t\trequired: false,\n\t\t\t\t\tinput: false,\n\t\t\t\t},\n\t\t\t\t/**\n\t\t\t\t * Shows the first few characters of the API key\n\t\t\t\t * This allows you to show those few characters in the UI to make it easier for users to identify the API key.\n\t\t\t\t */\n\t\t\t\tstart: {\n\t\t\t\t\ttype: \"string\",\n\t\t\t\t\trequired: false,\n\t\t\t\t\tinput: false,\n\t\t\t\t},\n\t\t\t\t/**\n\t\t\t\t * The prefix of the key.\n\t\t\t\t */\n\t\t\t\tprefix: {\n\t\t\t\t\ttype: \"string\",\n\t\t\t\t\trequired: false,\n\t\t\t\t\tinput: false,\n\t\t\t\t},\n\t\t\t\t/**\n\t\t\t\t * The hashed key value.\n\t\t\t\t */\n\t\t\t\tkey: {\n\t\t\t\t\ttype: \"string\",\n\t\t\t\t\trequired: true,\n\t\t\t\t\tinput: false,\n\t\t\t\t\tindex: true,\n\t\t\t\t},\n\t\t\t\t/**\n\t\t\t\t * The user id of the user who created the key.\n\t\t\t\t */\n\t\t\t\tuserId: {\n\t\t\t\t\ttype: \"string\",\n\t\t\t\t\treferences: { model: \"user\", field: \"id\", onDelete: \"cascade\" },\n\t\t\t\t\trequired: true,\n\t\t\t\t\tinput: false,\n\t\t\t\t\tindex: true,\n\t\t\t\t},\n\t\t\t\t/**\n\t\t\t\t * The interval to refill the key in milliseconds.\n\t\t\t\t */\n\t\t\t\trefillInterval: {\n\t\t\t\t\ttype: \"number\",\n\t\t\t\t\trequired: false,\n\t\t\t\t\tinput: false,\n\t\t\t\t},\n\t\t\t\t/**\n\t\t\t\t * The amount to refill the remaining count of the key.\n\t\t\t\t */\n\t\t\t\trefillAmount: {\n\t\t\t\t\ttype: \"number\",\n\t\t\t\t\trequired: false,\n\t\t\t\t\tinput: false,\n\t\t\t\t},\n\t\t\t\t/**\n\t\t\t\t * The date and time when the key was last refilled.\n\t\t\t\t */\n\t\t\t\tlastRefillAt: {\n\t\t\t\t\ttype: \"date\",\n\t\t\t\t\trequired: false,\n\t\t\t\t\tinput: false,\n\t\t\t\t},\n\t\t\t\t/**\n\t\t\t\t * Whether the key is enabled.\n\t\t\t\t */\n\t\t\t\tenabled: {\n\t\t\t\t\ttype: \"boolean\",\n\t\t\t\t\trequired: false,\n\t\t\t\t\tinput: false,\n\t\t\t\t\tdefaultValue: true,\n\t\t\t\t},\n\t\t\t\t/**\n\t\t\t\t * Whether the key has rate limiting enabled.\n\t\t\t\t */\n\t\t\t\trateLimitEnabled: {\n\t\t\t\t\ttype: \"boolean\",\n\t\t\t\t\trequired: false,\n\t\t\t\t\tinput: false,\n\t\t\t\t\tdefaultValue: true,\n\t\t\t\t},\n\t\t\t\t/**\n\t\t\t\t * The time window in milliseconds for the rate limit.\n\t\t\t\t */\n\t\t\t\trateLimitTimeWindow: {\n\t\t\t\t\ttype: \"number\",\n\t\t\t\t\trequired: false,\n\t\t\t\t\tinput: false,\n\t\t\t\t\tdefaultValue: timeWindow,\n\t\t\t\t},\n\t\t\t\t/**\n\t\t\t\t * The maximum number of requests allowed within the `rateLimitTimeWindow`.\n\t\t\t\t */\n\t\t\t\trateLimitMax: {\n\t\t\t\t\ttype: \"number\",\n\t\t\t\t\trequired: false,\n\t\t\t\t\tinput: false,\n\t\t\t\t\tdefaultValue: rateLimitMax,\n\t\t\t\t},\n\t\t\t\t/**\n\t\t\t\t * The number of requests made within the rate limit time window\n\t\t\t\t */\n\t\t\t\trequestCount: {\n\t\t\t\t\ttype: \"number\",\n\t\t\t\t\trequired: false,\n\t\t\t\t\tinput: false,\n\t\t\t\t\tdefaultValue: 0,\n\t\t\t\t},\n\t\t\t\t/**\n\t\t\t\t * The remaining number of requests before the key is revoked.\n\t\t\t\t *\n\t\t\t\t * If this is null, then the key is not revoked.\n\t\t\t\t *\n\t\t\t\t * If `refillInterval` & `refillAmount` are provided, than this will refill accordingly.\n\t\t\t\t */\n\t\t\t\tremaining: {\n\t\t\t\t\ttype: \"number\",\n\t\t\t\t\trequired: false,\n\t\t\t\t\tinput: false,\n\t\t\t\t},\n\t\t\t\t/**\n\t\t\t\t * The date and time of the last request made to the key.\n\t\t\t\t */\n\t\t\t\tlastRequest: {\n\t\t\t\t\ttype: \"date\",\n\t\t\t\t\trequired: false,\n\t\t\t\t\tinput: false,\n\t\t\t\t},\n\t\t\t\t/**\n\t\t\t\t * The date and time when the key will expire.\n\t\t\t\t */\n\t\t\t\texpiresAt: {\n\t\t\t\t\ttype: \"date\",\n\t\t\t\t\trequired: false,\n\t\t\t\t\tinput: false,\n\t\t\t\t},\n\t\t\t\t/**\n\t\t\t\t * The date and time when the key was created.\n\t\t\t\t */\n\t\t\t\tcreatedAt: {\n\t\t\t\t\ttype: \"date\",\n\t\t\t\t\trequired: true,\n\t\t\t\t\tinput: false,\n\t\t\t\t},\n\t\t\t\t/**\n\t\t\t\t * The date and time when the key was last updated.\n\t\t\t\t */\n\t\t\t\tupdatedAt: {\n\t\t\t\t\ttype: \"date\",\n\t\t\t\t\trequired: true,\n\t\t\t\t\tinput: false,\n\t\t\t\t},\n\t\t\t\t/**\n\t\t\t\t * The permissions of the key.\n\t\t\t\t */\n\t\t\t\tpermissions: {\n\t\t\t\t\ttype: \"string\",\n\t\t\t\t\trequired: false,\n\t\t\t\t\tinput: false,\n\t\t\t\t},\n\t\t\t\t/**\n\t\t\t\t * Any additional metadata you want to store with the key.\n\t\t\t\t */\n\t\t\t\tmetadata: {\n\t\t\t\t\ttype: \"string\",\n\t\t\t\t\trequired: false,\n\t\t\t\t\tinput: true,\n\t\t\t\t\ttransform: {\n\t\t\t\t\t\tinput(value) {\n\t\t\t\t\t\t\treturn JSON.stringify(value);\n\t\t\t\t\t\t},\n\t\t\t\t\t\toutput(value) {\n\t\t\t\t\t\t\tif (!value) return null;\n\t\t\t\t\t\t\treturn parseJSON<any>(value as string);\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t}) satisfies BetterAuthPluginDBSchema;\n"],"mappings":";;;AAGA,MAAa,gBAAgB,EAC5B,YACA,oBAKC,EACA,QAAQ,EACP,QAAQ;CAIP,MAAM;EACL,MAAM;EACN,UAAU;EACV,OAAO;EACP;CAKD,OAAO;EACN,MAAM;EACN,UAAU;EACV,OAAO;EACP;CAID,QAAQ;EACP,MAAM;EACN,UAAU;EACV,OAAO;EACP;CAID,KAAK;EACJ,MAAM;EACN,UAAU;EACV,OAAO;EACP,OAAO;EACP;CAID,QAAQ;EACP,MAAM;EACN,YAAY;GAAE,OAAO;GAAQ,OAAO;GAAM,UAAU;GAAW;EAC/D,UAAU;EACV,OAAO;EACP,OAAO;EACP;CAID,gBAAgB;EACf,MAAM;EACN,UAAU;EACV,OAAO;EACP;CAID,cAAc;EACb,MAAM;EACN,UAAU;EACV,OAAO;EACP;CAID,cAAc;EACb,MAAM;EACN,UAAU;EACV,OAAO;EACP;CAID,SAAS;EACR,MAAM;EACN,UAAU;EACV,OAAO;EACP,cAAc;EACd;CAID,kBAAkB;EACjB,MAAM;EACN,UAAU;EACV,OAAO;EACP,cAAc;EACd;CAID,qBAAqB;EACpB,MAAM;EACN,UAAU;EACV,OAAO;EACP,cAAc;EACd;CAID,cAAc;EACb,MAAM;EACN,UAAU;EACV,OAAO;EACP,cAAc;EACd;CAID,cAAc;EACb,MAAM;EACN,UAAU;EACV,OAAO;EACP,cAAc;EACd;CAQD,WAAW;EACV,MAAM;EACN,UAAU;EACV,OAAO;EACP;CAID,aAAa;EACZ,MAAM;EACN,UAAU;EACV,OAAO;EACP;CAID,WAAW;EACV,MAAM;EACN,UAAU;EACV,OAAO;EACP;CAID,WAAW;EACV,MAAM;EACN,UAAU;EACV,OAAO;EACP;CAID,WAAW;EACV,MAAM;EACN,UAAU;EACV,OAAO;EACP;CAID,aAAa;EACZ,MAAM;EACN,UAAU;EACV,OAAO;EACP;CAID,UAAU;EACT,MAAM;EACN,UAAU;EACV,OAAO;EACP,WAAW;GACV,MAAM,OAAO;AACZ,WAAO,KAAK,UAAU,MAAM;;GAE7B,OAAO,OAAO;AACb,QAAI,CAAC,MAAO,QAAO;AACnB,WAAO,UAAe,MAAgB;;GAEvC;EACD;CACD,EACD,EACD"}