better-auth-vercel-geolocation 1.1.0 → 1.1.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/client.d.cts +24 -2
- package/dist/client.d.cts.map +1 -1
- package/dist/client.d.ts +24 -2
- package/dist/client.d.ts.map +1 -1
- package/dist/index.d.cts +24 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +24 -2
- package/dist/index.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/client.d.cts
CHANGED
|
@@ -2,7 +2,28 @@ import * as better_auth from 'better-auth';
|
|
|
2
2
|
import { Geo } from '@vercel/functions';
|
|
3
3
|
|
|
4
4
|
type GeolocationField = keyof Geo;
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Configuration for which geolocation fields to store.
|
|
7
|
+
* Each field can be set to `true` to include it in the session.
|
|
8
|
+
*/
|
|
9
|
+
interface GeolocationFieldConfig {
|
|
10
|
+
/** The city that the request originated from. */
|
|
11
|
+
city?: boolean;
|
|
12
|
+
/** The country that the request originated from. */
|
|
13
|
+
country?: boolean;
|
|
14
|
+
/** The region part of the ISO 3166-2 code of the client IP. */
|
|
15
|
+
countryRegion?: boolean;
|
|
16
|
+
/** The flag emoji for the country the request originated from. */
|
|
17
|
+
flag?: boolean;
|
|
18
|
+
/** The latitude of the client. */
|
|
19
|
+
latitude?: boolean;
|
|
20
|
+
/** The longitude of the client. */
|
|
21
|
+
longitude?: boolean;
|
|
22
|
+
/** The postal code of the client. */
|
|
23
|
+
postalCode?: boolean;
|
|
24
|
+
/** The Vercel Edge Network region that received the request. */
|
|
25
|
+
region?: boolean;
|
|
26
|
+
}
|
|
6
27
|
declare const DEFAULT_FIELDS: {
|
|
7
28
|
readonly city: true;
|
|
8
29
|
readonly country: true;
|
|
@@ -23,9 +44,10 @@ type InferGeoSession<T extends GeolocationFieldConfig> = {
|
|
|
23
44
|
interface GeolocationPluginOptions<T extends GeolocationFieldConfig = GeolocationFieldConfig> {
|
|
24
45
|
/**
|
|
25
46
|
* Which geolocation fields to store on the session.
|
|
47
|
+
* All available fields: city, country, countryRegion, flag, latitude, longitude, postalCode, region.
|
|
26
48
|
* @default { city: true, country: true, countryRegion: true, flag: true }
|
|
27
49
|
*/
|
|
28
|
-
fields?: T;
|
|
50
|
+
fields?: T & GeolocationFieldConfig;
|
|
29
51
|
/**
|
|
30
52
|
* Whether to update session geolocation data when the session is refreshed.
|
|
31
53
|
* @default true
|
package/dist/client.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.cts","sources":["../src/types.ts","../src/geolocation.ts","../src/client.ts"],"sourcesContent":["import type { Geo } from '@vercel/functions';\n\nexport type GeolocationField = keyof Geo;\n\nexport
|
|
1
|
+
{"version":3,"file":"client.d.cts","sources":["../src/types.ts","../src/geolocation.ts","../src/client.ts"],"sourcesContent":["import type { Geo } from '@vercel/functions';\n\nexport type GeolocationField = keyof Geo;\n\n/**\n * Configuration for which geolocation fields to store.\n * Each field can be set to `true` to include it in the session.\n */\nexport interface GeolocationFieldConfig {\n /** The city that the request originated from. */\n city?: boolean;\n /** The country that the request originated from. */\n country?: boolean;\n /** The region part of the ISO 3166-2 code of the client IP. */\n countryRegion?: boolean;\n /** The flag emoji for the country the request originated from. */\n flag?: boolean;\n /** The latitude of the client. */\n latitude?: boolean;\n /** The longitude of the client. */\n longitude?: boolean;\n /** The postal code of the client. */\n postalCode?: boolean;\n /** The Vercel Edge Network region that received the request. */\n region?: boolean;\n}\n\nexport const DEFAULT_FIELDS = {\n city: true,\n country: true,\n countryRegion: true,\n flag: true,\n} as const satisfies GeolocationFieldConfig;\n\nexport type DefaultGeolocationFields = typeof DEFAULT_FIELDS;\n\nexport type GeoSchemaFields<T extends GeolocationFieldConfig> = {\n [K in keyof T & GeolocationField as T[K] extends true ? K : never]: {\n type: 'string';\n required: false;\n input: false;\n };\n};\n\nexport type InferGeoSession<T extends GeolocationFieldConfig> = {\n [K in keyof T & GeolocationField as T[K] extends true\n ? K\n : never]?: string | null;\n};\n\nexport interface GeolocationPluginOptions<\n T extends GeolocationFieldConfig = GeolocationFieldConfig,\n> {\n /**\n * Which geolocation fields to store on the session.\n * All available fields: city, country, countryRegion, flag, latitude, longitude, postalCode, region.\n * @default { city: true, country: true, countryRegion: true, flag: true }\n */\n fields?: T & GeolocationFieldConfig;\n /**\n * Whether to update session geolocation data when the session is refreshed.\n * @default true\n */\n updateOnRefresh?: boolean;\n}\n","import type { BetterAuthPlugin } from 'better-auth';\n\nimport { geolocation as extractGeo } from '@vercel/functions';\n\nimport {\n DEFAULT_FIELDS,\n type DefaultGeolocationFields,\n type GeolocationField,\n type GeolocationFieldConfig,\n type GeolocationPluginOptions,\n type GeoSchemaFields,\n type InferGeoSession,\n} from './types';\n\nexport const geolocation = <\n const T extends GeolocationFieldConfig = DefaultGeolocationFields,\n>(\n options?: GeolocationPluginOptions<T>,\n) => {\n const updateOnRefresh = options?.updateOnRefresh ?? true;\n const fieldConfig = (options?.fields ?? DEFAULT_FIELDS) as T;\n\n const activeFields = (Object.keys(fieldConfig) as GeolocationField[]).filter(\n (field) => fieldConfig[field] === true,\n );\n\n const schemaFields = Object.fromEntries(\n activeFields.map((field) => [\n field,\n {\n type: 'string',\n required: false,\n input: false,\n },\n ]),\n ) as GeoSchemaFields<T>;\n\n function applyGeo(\n session: Record<string, unknown>,\n request: Request | undefined,\n ) {\n if (!request) return;\n\n const geo = extractGeo(request);\n for (const field of activeFields) {\n session[field] = geo[field];\n }\n return { data: session };\n }\n\n return {\n id: 'vercel-geolocation',\n schema: {\n session: { fields: schemaFields },\n },\n $Infer: {\n Session: {\n session: {} as InferGeoSession<T>,\n },\n },\n init() {\n return {\n options: {\n databaseHooks: {\n session: {\n create: {\n before: async (session, ctx) => {\n return applyGeo(session, ctx?.request);\n },\n },\n update: {\n before: async (session, ctx) => {\n if (!updateOnRefresh) return;\n return applyGeo(session, ctx?.request);\n },\n },\n },\n },\n },\n };\n },\n } satisfies BetterAuthPlugin;\n};","import type { BetterAuthClientPlugin } from 'better-auth/client';\n\nimport type { geolocation } from './geolocation';\nimport type {\n DefaultGeolocationFields,\n GeolocationFieldConfig,\n} from './types';\n\nexport const geolocationClient = <\n const T extends GeolocationFieldConfig = DefaultGeolocationFields,\n>() => {\n return {\n id: 'vercel-geolocation',\n $InferServerPlugin: {} as ReturnType<typeof geolocation<T>>,\n } satisfies BetterAuthClientPlugin;\n};\n"],"names":[],"mappings":";;;AACO,KAAA,gBAAA,SAAA,GAAA;AACP;AACA;AACA;AACA;AACO,UAAA,sBAAA;AACP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,cAAA,cAAA;AACP;AACA;AACA;AACA;AACA;AACO,KAAA,wBAAA,UAAA,cAAA;AACA,KAAA,eAAA,WAAA,sBAAA;AACP,oBAAA,gBAAA;AACA;AACA;AACA;AACA;AACA;AACO,KAAA,eAAA,WAAA,sBAAA;AACP,oBAAA,gBAAA;AACA;AACO,UAAA,wBAAA,WAAA,sBAAA,GAAA,sBAAA;AACP;AACA;AACA;AACA;AACA;AACA,iBAAA,sBAAA;AACA;AACA;AACA;AACA;AACA;AACA;;ACpDO,cAAA,WAAA,mBAAA,sBAAA,GAAA,wBAAA,YAAA,wBAAA;AACP;AACA;AACA;AACA,oBAAA,eAAA;AACA;AACA;AACA;AACA;AACA,qBAAA,eAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,uCAAA,IAAA;AACA,uCAAA,IAAA;AACA;AACA,uCAAA,IAAA;AACA;AACA;AACA;AACA,4BAAA,MAAA,wBAA0D,WAAqB,CAAA,sBAAA,YAAA,OAAA;AAC/E,kCAAA,MAAA;AACA;AACA;AACA;AACA,0CAAA,OAAA;AACA;AACA,uCAAA,IAAA;AACA,uCAAA,IAAA;AACA;AACA,uCAAA,IAAA;AACA;AACA;AACA;AACA,6BAAA,MAAA,wBAA2D,WAAqB,CAAA,sBAAA,YAAA,OAAA;AAChF,kCAAA,MAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AC/CO,cAAA,iBAAA,mBAAA,sBAAA,GAAA,wBAAA;AACP;AACA,wBAAA,UAAA,QAAA,WAAA;AACA;;;;"}
|
package/dist/client.d.ts
CHANGED
|
@@ -2,7 +2,28 @@ import * as better_auth from 'better-auth';
|
|
|
2
2
|
import { Geo } from '@vercel/functions';
|
|
3
3
|
|
|
4
4
|
type GeolocationField = keyof Geo;
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Configuration for which geolocation fields to store.
|
|
7
|
+
* Each field can be set to `true` to include it in the session.
|
|
8
|
+
*/
|
|
9
|
+
interface GeolocationFieldConfig {
|
|
10
|
+
/** The city that the request originated from. */
|
|
11
|
+
city?: boolean;
|
|
12
|
+
/** The country that the request originated from. */
|
|
13
|
+
country?: boolean;
|
|
14
|
+
/** The region part of the ISO 3166-2 code of the client IP. */
|
|
15
|
+
countryRegion?: boolean;
|
|
16
|
+
/** The flag emoji for the country the request originated from. */
|
|
17
|
+
flag?: boolean;
|
|
18
|
+
/** The latitude of the client. */
|
|
19
|
+
latitude?: boolean;
|
|
20
|
+
/** The longitude of the client. */
|
|
21
|
+
longitude?: boolean;
|
|
22
|
+
/** The postal code of the client. */
|
|
23
|
+
postalCode?: boolean;
|
|
24
|
+
/** The Vercel Edge Network region that received the request. */
|
|
25
|
+
region?: boolean;
|
|
26
|
+
}
|
|
6
27
|
declare const DEFAULT_FIELDS: {
|
|
7
28
|
readonly city: true;
|
|
8
29
|
readonly country: true;
|
|
@@ -23,9 +44,10 @@ type InferGeoSession<T extends GeolocationFieldConfig> = {
|
|
|
23
44
|
interface GeolocationPluginOptions<T extends GeolocationFieldConfig = GeolocationFieldConfig> {
|
|
24
45
|
/**
|
|
25
46
|
* Which geolocation fields to store on the session.
|
|
47
|
+
* All available fields: city, country, countryRegion, flag, latitude, longitude, postalCode, region.
|
|
26
48
|
* @default { city: true, country: true, countryRegion: true, flag: true }
|
|
27
49
|
*/
|
|
28
|
-
fields?: T;
|
|
50
|
+
fields?: T & GeolocationFieldConfig;
|
|
29
51
|
/**
|
|
30
52
|
* Whether to update session geolocation data when the session is refreshed.
|
|
31
53
|
* @default true
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sources":["../src/types.ts","../src/geolocation.ts","../src/client.ts"],"sourcesContent":["import type { Geo } from '@vercel/functions';\n\nexport type GeolocationField = keyof Geo;\n\nexport
|
|
1
|
+
{"version":3,"file":"client.d.ts","sources":["../src/types.ts","../src/geolocation.ts","../src/client.ts"],"sourcesContent":["import type { Geo } from '@vercel/functions';\n\nexport type GeolocationField = keyof Geo;\n\n/**\n * Configuration for which geolocation fields to store.\n * Each field can be set to `true` to include it in the session.\n */\nexport interface GeolocationFieldConfig {\n /** The city that the request originated from. */\n city?: boolean;\n /** The country that the request originated from. */\n country?: boolean;\n /** The region part of the ISO 3166-2 code of the client IP. */\n countryRegion?: boolean;\n /** The flag emoji for the country the request originated from. */\n flag?: boolean;\n /** The latitude of the client. */\n latitude?: boolean;\n /** The longitude of the client. */\n longitude?: boolean;\n /** The postal code of the client. */\n postalCode?: boolean;\n /** The Vercel Edge Network region that received the request. */\n region?: boolean;\n}\n\nexport const DEFAULT_FIELDS = {\n city: true,\n country: true,\n countryRegion: true,\n flag: true,\n} as const satisfies GeolocationFieldConfig;\n\nexport type DefaultGeolocationFields = typeof DEFAULT_FIELDS;\n\nexport type GeoSchemaFields<T extends GeolocationFieldConfig> = {\n [K in keyof T & GeolocationField as T[K] extends true ? K : never]: {\n type: 'string';\n required: false;\n input: false;\n };\n};\n\nexport type InferGeoSession<T extends GeolocationFieldConfig> = {\n [K in keyof T & GeolocationField as T[K] extends true\n ? K\n : never]?: string | null;\n};\n\nexport interface GeolocationPluginOptions<\n T extends GeolocationFieldConfig = GeolocationFieldConfig,\n> {\n /**\n * Which geolocation fields to store on the session.\n * All available fields: city, country, countryRegion, flag, latitude, longitude, postalCode, region.\n * @default { city: true, country: true, countryRegion: true, flag: true }\n */\n fields?: T & GeolocationFieldConfig;\n /**\n * Whether to update session geolocation data when the session is refreshed.\n * @default true\n */\n updateOnRefresh?: boolean;\n}\n","import type { BetterAuthPlugin } from 'better-auth';\n\nimport { geolocation as extractGeo } from '@vercel/functions';\n\nimport {\n DEFAULT_FIELDS,\n type DefaultGeolocationFields,\n type GeolocationField,\n type GeolocationFieldConfig,\n type GeolocationPluginOptions,\n type GeoSchemaFields,\n type InferGeoSession,\n} from './types';\n\nexport const geolocation = <\n const T extends GeolocationFieldConfig = DefaultGeolocationFields,\n>(\n options?: GeolocationPluginOptions<T>,\n) => {\n const updateOnRefresh = options?.updateOnRefresh ?? true;\n const fieldConfig = (options?.fields ?? DEFAULT_FIELDS) as T;\n\n const activeFields = (Object.keys(fieldConfig) as GeolocationField[]).filter(\n (field) => fieldConfig[field] === true,\n );\n\n const schemaFields = Object.fromEntries(\n activeFields.map((field) => [\n field,\n {\n type: 'string',\n required: false,\n input: false,\n },\n ]),\n ) as GeoSchemaFields<T>;\n\n function applyGeo(\n session: Record<string, unknown>,\n request: Request | undefined,\n ) {\n if (!request) return;\n\n const geo = extractGeo(request);\n for (const field of activeFields) {\n session[field] = geo[field];\n }\n return { data: session };\n }\n\n return {\n id: 'vercel-geolocation',\n schema: {\n session: { fields: schemaFields },\n },\n $Infer: {\n Session: {\n session: {} as InferGeoSession<T>,\n },\n },\n init() {\n return {\n options: {\n databaseHooks: {\n session: {\n create: {\n before: async (session, ctx) => {\n return applyGeo(session, ctx?.request);\n },\n },\n update: {\n before: async (session, ctx) => {\n if (!updateOnRefresh) return;\n return applyGeo(session, ctx?.request);\n },\n },\n },\n },\n },\n };\n },\n } satisfies BetterAuthPlugin;\n};","import type { BetterAuthClientPlugin } from 'better-auth/client';\n\nimport type { geolocation } from './geolocation';\nimport type {\n DefaultGeolocationFields,\n GeolocationFieldConfig,\n} from './types';\n\nexport const geolocationClient = <\n const T extends GeolocationFieldConfig = DefaultGeolocationFields,\n>() => {\n return {\n id: 'vercel-geolocation',\n $InferServerPlugin: {} as ReturnType<typeof geolocation<T>>,\n } satisfies BetterAuthClientPlugin;\n};\n"],"names":[],"mappings":";;;AACO,KAAA,gBAAA,SAAA,GAAA;AACP;AACA;AACA;AACA;AACO,UAAA,sBAAA;AACP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,cAAA,cAAA;AACP;AACA;AACA;AACA;AACA;AACO,KAAA,wBAAA,UAAA,cAAA;AACA,KAAA,eAAA,WAAA,sBAAA;AACP,oBAAA,gBAAA;AACA;AACA;AACA;AACA;AACA;AACO,KAAA,eAAA,WAAA,sBAAA;AACP,oBAAA,gBAAA;AACA;AACO,UAAA,wBAAA,WAAA,sBAAA,GAAA,sBAAA;AACP;AACA;AACA;AACA;AACA;AACA,iBAAA,sBAAA;AACA;AACA;AACA;AACA;AACA;AACA;;ACpDO,cAAA,WAAA,mBAAA,sBAAA,GAAA,wBAAA,YAAA,wBAAA;AACP;AACA;AACA;AACA,oBAAA,eAAA;AACA;AACA;AACA;AACA;AACA,qBAAA,eAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,uCAAA,IAAA;AACA,uCAAA,IAAA;AACA;AACA,uCAAA,IAAA;AACA;AACA;AACA;AACA,4BAAA,MAAA,wBAA0D,WAAqB,CAAA,sBAAA,YAAA,OAAA;AAC/E,kCAAA,MAAA;AACA;AACA;AACA;AACA,0CAAA,OAAA;AACA;AACA,uCAAA,IAAA;AACA,uCAAA,IAAA;AACA;AACA,uCAAA,IAAA;AACA;AACA;AACA;AACA,6BAAA,MAAA,wBAA2D,WAAqB,CAAA,sBAAA,YAAA,OAAA;AAChF,kCAAA,MAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AC/CO,cAAA,iBAAA,mBAAA,sBAAA,GAAA,wBAAA;AACP;AACA,wBAAA,UAAA,QAAA,WAAA;AACA;;;;"}
|
package/dist/index.d.cts
CHANGED
|
@@ -2,7 +2,28 @@ import * as better_auth from 'better-auth';
|
|
|
2
2
|
import { Geo } from '@vercel/functions';
|
|
3
3
|
|
|
4
4
|
type GeolocationField = keyof Geo;
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Configuration for which geolocation fields to store.
|
|
7
|
+
* Each field can be set to `true` to include it in the session.
|
|
8
|
+
*/
|
|
9
|
+
interface GeolocationFieldConfig {
|
|
10
|
+
/** The city that the request originated from. */
|
|
11
|
+
city?: boolean;
|
|
12
|
+
/** The country that the request originated from. */
|
|
13
|
+
country?: boolean;
|
|
14
|
+
/** The region part of the ISO 3166-2 code of the client IP. */
|
|
15
|
+
countryRegion?: boolean;
|
|
16
|
+
/** The flag emoji for the country the request originated from. */
|
|
17
|
+
flag?: boolean;
|
|
18
|
+
/** The latitude of the client. */
|
|
19
|
+
latitude?: boolean;
|
|
20
|
+
/** The longitude of the client. */
|
|
21
|
+
longitude?: boolean;
|
|
22
|
+
/** The postal code of the client. */
|
|
23
|
+
postalCode?: boolean;
|
|
24
|
+
/** The Vercel Edge Network region that received the request. */
|
|
25
|
+
region?: boolean;
|
|
26
|
+
}
|
|
6
27
|
declare const DEFAULT_FIELDS: {
|
|
7
28
|
readonly city: true;
|
|
8
29
|
readonly country: true;
|
|
@@ -23,9 +44,10 @@ type InferGeoSession<T extends GeolocationFieldConfig> = {
|
|
|
23
44
|
interface GeolocationPluginOptions<T extends GeolocationFieldConfig = GeolocationFieldConfig> {
|
|
24
45
|
/**
|
|
25
46
|
* Which geolocation fields to store on the session.
|
|
47
|
+
* All available fields: city, country, countryRegion, flag, latitude, longitude, postalCode, region.
|
|
26
48
|
* @default { city: true, country: true, countryRegion: true, flag: true }
|
|
27
49
|
*/
|
|
28
|
-
fields?: T;
|
|
50
|
+
fields?: T & GeolocationFieldConfig;
|
|
29
51
|
/**
|
|
30
52
|
* Whether to update session geolocation data when the session is refreshed.
|
|
31
53
|
* @default true
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","sources":["../src/types.ts","../src/geolocation.ts"],"sourcesContent":["import type { Geo } from '@vercel/functions';\n\nexport type GeolocationField = keyof Geo;\n\nexport
|
|
1
|
+
{"version":3,"file":"index.d.cts","sources":["../src/types.ts","../src/geolocation.ts"],"sourcesContent":["import type { Geo } from '@vercel/functions';\n\nexport type GeolocationField = keyof Geo;\n\n/**\n * Configuration for which geolocation fields to store.\n * Each field can be set to `true` to include it in the session.\n */\nexport interface GeolocationFieldConfig {\n /** The city that the request originated from. */\n city?: boolean;\n /** The country that the request originated from. */\n country?: boolean;\n /** The region part of the ISO 3166-2 code of the client IP. */\n countryRegion?: boolean;\n /** The flag emoji for the country the request originated from. */\n flag?: boolean;\n /** The latitude of the client. */\n latitude?: boolean;\n /** The longitude of the client. */\n longitude?: boolean;\n /** The postal code of the client. */\n postalCode?: boolean;\n /** The Vercel Edge Network region that received the request. */\n region?: boolean;\n}\n\nexport const DEFAULT_FIELDS = {\n city: true,\n country: true,\n countryRegion: true,\n flag: true,\n} as const satisfies GeolocationFieldConfig;\n\nexport type DefaultGeolocationFields = typeof DEFAULT_FIELDS;\n\nexport type GeoSchemaFields<T extends GeolocationFieldConfig> = {\n [K in keyof T & GeolocationField as T[K] extends true ? K : never]: {\n type: 'string';\n required: false;\n input: false;\n };\n};\n\nexport type InferGeoSession<T extends GeolocationFieldConfig> = {\n [K in keyof T & GeolocationField as T[K] extends true\n ? K\n : never]?: string | null;\n};\n\nexport interface GeolocationPluginOptions<\n T extends GeolocationFieldConfig = GeolocationFieldConfig,\n> {\n /**\n * Which geolocation fields to store on the session.\n * All available fields: city, country, countryRegion, flag, latitude, longitude, postalCode, region.\n * @default { city: true, country: true, countryRegion: true, flag: true }\n */\n fields?: T & GeolocationFieldConfig;\n /**\n * Whether to update session geolocation data when the session is refreshed.\n * @default true\n */\n updateOnRefresh?: boolean;\n}\n","import type { BetterAuthPlugin } from 'better-auth';\n\nimport { geolocation as extractGeo } from '@vercel/functions';\n\nimport {\n DEFAULT_FIELDS,\n type DefaultGeolocationFields,\n type GeolocationField,\n type GeolocationFieldConfig,\n type GeolocationPluginOptions,\n type GeoSchemaFields,\n type InferGeoSession,\n} from './types';\n\nexport const geolocation = <\n const T extends GeolocationFieldConfig = DefaultGeolocationFields,\n>(\n options?: GeolocationPluginOptions<T>,\n) => {\n const updateOnRefresh = options?.updateOnRefresh ?? true;\n const fieldConfig = (options?.fields ?? DEFAULT_FIELDS) as T;\n\n const activeFields = (Object.keys(fieldConfig) as GeolocationField[]).filter(\n (field) => fieldConfig[field] === true,\n );\n\n const schemaFields = Object.fromEntries(\n activeFields.map((field) => [\n field,\n {\n type: 'string',\n required: false,\n input: false,\n },\n ]),\n ) as GeoSchemaFields<T>;\n\n function applyGeo(\n session: Record<string, unknown>,\n request: Request | undefined,\n ) {\n if (!request) return;\n\n const geo = extractGeo(request);\n for (const field of activeFields) {\n session[field] = geo[field];\n }\n return { data: session };\n }\n\n return {\n id: 'vercel-geolocation',\n schema: {\n session: { fields: schemaFields },\n },\n $Infer: {\n Session: {\n session: {} as InferGeoSession<T>,\n },\n },\n init() {\n return {\n options: {\n databaseHooks: {\n session: {\n create: {\n before: async (session, ctx) => {\n return applyGeo(session, ctx?.request);\n },\n },\n update: {\n before: async (session, ctx) => {\n if (!updateOnRefresh) return;\n return applyGeo(session, ctx?.request);\n },\n },\n },\n },\n },\n };\n },\n } satisfies BetterAuthPlugin;\n};"],"names":[],"mappings":";;;AACO,KAAA,gBAAA,SAAA,GAAA;AACP;AACA;AACA;AACA;AACO,UAAA,sBAAA;AACP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,cAAA,cAAA;AACP;AACA;AACA;AACA;AACA;AACO,KAAA,wBAAA,UAAA,cAAA;AACA,KAAA,eAAA,WAAA,sBAAA;AACP,oBAAA,gBAAA;AACA;AACA;AACA;AACA;AACA;AACO,KAAA,eAAA,WAAA,sBAAA;AACP,oBAAA,gBAAA;AACA;AACO,UAAA,wBAAA,WAAA,sBAAA,GAAA,sBAAA;AACP;AACA;AACA;AACA;AACA;AACA,iBAAA,sBAAA;AACA;AACA;AACA;AACA;AACA;AACA;;ACpDO,cAAA,WAAA,mBAAA,sBAAA,GAAA,wBAAA,YAAA,wBAAA;AACP;AACA;AACA;AACA,oBAAA,eAAA;AACA;AACA;AACA;AACA;AACA,qBAAA,eAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,uCAAA,IAAA;AACA,uCAAA,IAAA;AACA;AACA,uCAAA,IAAA;AACA;AACA;AACA;AACA,4BAAA,MAAA,wBAA0D,WAAqB,CAAA,sBAAA,YAAA,OAAA;AAC/E,kCAAA,MAAA;AACA;AACA;AACA;AACA,0CAAA,OAAA;AACA;AACA,uCAAA,IAAA;AACA,uCAAA,IAAA;AACA;AACA,uCAAA,IAAA;AACA;AACA;AACA;AACA,6BAAA,MAAA,wBAA2D,WAAqB,CAAA,sBAAA,YAAA,OAAA;AAChF,kCAAA,MAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,28 @@ import * as better_auth from 'better-auth';
|
|
|
2
2
|
import { Geo } from '@vercel/functions';
|
|
3
3
|
|
|
4
4
|
type GeolocationField = keyof Geo;
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Configuration for which geolocation fields to store.
|
|
7
|
+
* Each field can be set to `true` to include it in the session.
|
|
8
|
+
*/
|
|
9
|
+
interface GeolocationFieldConfig {
|
|
10
|
+
/** The city that the request originated from. */
|
|
11
|
+
city?: boolean;
|
|
12
|
+
/** The country that the request originated from. */
|
|
13
|
+
country?: boolean;
|
|
14
|
+
/** The region part of the ISO 3166-2 code of the client IP. */
|
|
15
|
+
countryRegion?: boolean;
|
|
16
|
+
/** The flag emoji for the country the request originated from. */
|
|
17
|
+
flag?: boolean;
|
|
18
|
+
/** The latitude of the client. */
|
|
19
|
+
latitude?: boolean;
|
|
20
|
+
/** The longitude of the client. */
|
|
21
|
+
longitude?: boolean;
|
|
22
|
+
/** The postal code of the client. */
|
|
23
|
+
postalCode?: boolean;
|
|
24
|
+
/** The Vercel Edge Network region that received the request. */
|
|
25
|
+
region?: boolean;
|
|
26
|
+
}
|
|
6
27
|
declare const DEFAULT_FIELDS: {
|
|
7
28
|
readonly city: true;
|
|
8
29
|
readonly country: true;
|
|
@@ -23,9 +44,10 @@ type InferGeoSession<T extends GeolocationFieldConfig> = {
|
|
|
23
44
|
interface GeolocationPluginOptions<T extends GeolocationFieldConfig = GeolocationFieldConfig> {
|
|
24
45
|
/**
|
|
25
46
|
* Which geolocation fields to store on the session.
|
|
47
|
+
* All available fields: city, country, countryRegion, flag, latitude, longitude, postalCode, region.
|
|
26
48
|
* @default { city: true, country: true, countryRegion: true, flag: true }
|
|
27
49
|
*/
|
|
28
|
-
fields?: T;
|
|
50
|
+
fields?: T & GeolocationFieldConfig;
|
|
29
51
|
/**
|
|
30
52
|
* Whether to update session geolocation data when the session is refreshed.
|
|
31
53
|
* @default true
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sources":["../src/types.ts","../src/geolocation.ts"],"sourcesContent":["import type { Geo } from '@vercel/functions';\n\nexport type GeolocationField = keyof Geo;\n\nexport
|
|
1
|
+
{"version":3,"file":"index.d.ts","sources":["../src/types.ts","../src/geolocation.ts"],"sourcesContent":["import type { Geo } from '@vercel/functions';\n\nexport type GeolocationField = keyof Geo;\n\n/**\n * Configuration for which geolocation fields to store.\n * Each field can be set to `true` to include it in the session.\n */\nexport interface GeolocationFieldConfig {\n /** The city that the request originated from. */\n city?: boolean;\n /** The country that the request originated from. */\n country?: boolean;\n /** The region part of the ISO 3166-2 code of the client IP. */\n countryRegion?: boolean;\n /** The flag emoji for the country the request originated from. */\n flag?: boolean;\n /** The latitude of the client. */\n latitude?: boolean;\n /** The longitude of the client. */\n longitude?: boolean;\n /** The postal code of the client. */\n postalCode?: boolean;\n /** The Vercel Edge Network region that received the request. */\n region?: boolean;\n}\n\nexport const DEFAULT_FIELDS = {\n city: true,\n country: true,\n countryRegion: true,\n flag: true,\n} as const satisfies GeolocationFieldConfig;\n\nexport type DefaultGeolocationFields = typeof DEFAULT_FIELDS;\n\nexport type GeoSchemaFields<T extends GeolocationFieldConfig> = {\n [K in keyof T & GeolocationField as T[K] extends true ? K : never]: {\n type: 'string';\n required: false;\n input: false;\n };\n};\n\nexport type InferGeoSession<T extends GeolocationFieldConfig> = {\n [K in keyof T & GeolocationField as T[K] extends true\n ? K\n : never]?: string | null;\n};\n\nexport interface GeolocationPluginOptions<\n T extends GeolocationFieldConfig = GeolocationFieldConfig,\n> {\n /**\n * Which geolocation fields to store on the session.\n * All available fields: city, country, countryRegion, flag, latitude, longitude, postalCode, region.\n * @default { city: true, country: true, countryRegion: true, flag: true }\n */\n fields?: T & GeolocationFieldConfig;\n /**\n * Whether to update session geolocation data when the session is refreshed.\n * @default true\n */\n updateOnRefresh?: boolean;\n}\n","import type { BetterAuthPlugin } from 'better-auth';\n\nimport { geolocation as extractGeo } from '@vercel/functions';\n\nimport {\n DEFAULT_FIELDS,\n type DefaultGeolocationFields,\n type GeolocationField,\n type GeolocationFieldConfig,\n type GeolocationPluginOptions,\n type GeoSchemaFields,\n type InferGeoSession,\n} from './types';\n\nexport const geolocation = <\n const T extends GeolocationFieldConfig = DefaultGeolocationFields,\n>(\n options?: GeolocationPluginOptions<T>,\n) => {\n const updateOnRefresh = options?.updateOnRefresh ?? true;\n const fieldConfig = (options?.fields ?? DEFAULT_FIELDS) as T;\n\n const activeFields = (Object.keys(fieldConfig) as GeolocationField[]).filter(\n (field) => fieldConfig[field] === true,\n );\n\n const schemaFields = Object.fromEntries(\n activeFields.map((field) => [\n field,\n {\n type: 'string',\n required: false,\n input: false,\n },\n ]),\n ) as GeoSchemaFields<T>;\n\n function applyGeo(\n session: Record<string, unknown>,\n request: Request | undefined,\n ) {\n if (!request) return;\n\n const geo = extractGeo(request);\n for (const field of activeFields) {\n session[field] = geo[field];\n }\n return { data: session };\n }\n\n return {\n id: 'vercel-geolocation',\n schema: {\n session: { fields: schemaFields },\n },\n $Infer: {\n Session: {\n session: {} as InferGeoSession<T>,\n },\n },\n init() {\n return {\n options: {\n databaseHooks: {\n session: {\n create: {\n before: async (session, ctx) => {\n return applyGeo(session, ctx?.request);\n },\n },\n update: {\n before: async (session, ctx) => {\n if (!updateOnRefresh) return;\n return applyGeo(session, ctx?.request);\n },\n },\n },\n },\n },\n };\n },\n } satisfies BetterAuthPlugin;\n};"],"names":[],"mappings":";;;AACO,KAAA,gBAAA,SAAA,GAAA;AACP;AACA;AACA;AACA;AACO,UAAA,sBAAA;AACP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,cAAA,cAAA;AACP;AACA;AACA;AACA;AACA;AACO,KAAA,wBAAA,UAAA,cAAA;AACA,KAAA,eAAA,WAAA,sBAAA;AACP,oBAAA,gBAAA;AACA;AACA;AACA;AACA;AACA;AACO,KAAA,eAAA,WAAA,sBAAA;AACP,oBAAA,gBAAA;AACA;AACO,UAAA,wBAAA,WAAA,sBAAA,GAAA,sBAAA;AACP;AACA;AACA;AACA;AACA;AACA,iBAAA,sBAAA;AACA;AACA;AACA;AACA;AACA;AACA;;ACpDO,cAAA,WAAA,mBAAA,sBAAA,GAAA,wBAAA,YAAA,wBAAA;AACP;AACA;AACA;AACA,oBAAA,eAAA;AACA;AACA;AACA;AACA;AACA,qBAAA,eAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,uCAAA,IAAA;AACA,uCAAA,IAAA;AACA;AACA,uCAAA,IAAA;AACA;AACA;AACA;AACA,4BAAA,MAAA,wBAA0D,WAAqB,CAAA,sBAAA,YAAA,OAAA;AAC/E,kCAAA,MAAA;AACA;AACA;AACA;AACA,0CAAA,OAAA;AACA;AACA,uCAAA,IAAA;AACA,uCAAA,IAAA;AACA;AACA,uCAAA,IAAA;AACA;AACA;AACA;AACA,6BAAA,MAAA,wBAA2D,WAAqB,CAAA,sBAAA,YAAA,OAAA;AAChF,kCAAA,MAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "better-auth-vercel-geolocation",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "A better-auth plugin that enriches sessions with Vercel edge geolocation data.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://github.com/weepaho3/better-auth-vercel-geolocation",
|