@authhero/adapter-interfaces 3.9.0 → 3.11.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.
package/dist/sql.cjs ADDED
@@ -0,0 +1 @@
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});function e(e){let t=new Date(e);return Number.isNaN(t.getTime())?void 0:t.toISOString()}function t(t){if(t!=null){if(typeof t==`number`)return e(t);if(typeof t==`string`)return t===``?void 0:/^-?\d+(\.\d+)?$/.test(t)?e(parseFloat(t)):t}}function n(e,n=new Date(0).toISOString()){return t(e)??n}function r(e){if(!e||e===``)return null;let t=new Date(e);return isNaN(t.getTime())?null:t.getTime()}function i(){return new Date().toISOString()}function a(e,r,i=[]){let a={};for(let n of i)a[n.replace(/_ts$/,``)]=t(e[n]);for(let t of r)a[t.replace(/_ts$/,``)]=n(e[t]);return a}function o(e){return e===void 0?void 0:JSON.stringify(e)}function s(e,t,n={...e}){for(let r of t)e[r]!==void 0&&(n[r]=JSON.stringify(e[r]));return n}function c(e,t,n=e){for(let r of t)e[r]!==void 0&&(n[r]=+!!e[r])}function l(e){return Object.fromEntries(Object.entries(e).filter(([,e])=>e!=null))}function u(e){if(typeof e!=`object`||!e)return e;if(Array.isArray(e))return e.map(e=>typeof e==`object`&&e?u(e):e);let t=Object.getPrototypeOf(e);return t!==Object.prototype&&t!==null?e:Object.fromEntries(Object.entries(e).filter(([,e])=>e!==null).map(([e,t])=>[e,typeof t==`object`?u(t):t]))}function d(e){let t=Number(e);if(!Number.isSafeInteger(t))throw RangeError(`COUNT result is not a safe integer: ${String(e)}`);return t}exports.booleanToInt=c,exports.convertDatesToAdapter=a,exports.dbDateToIso=t,exports.dbDateToIsoRequired=n,exports.getCountAsInt=d,exports.isoToDbDate=r,exports.nowIso=i,exports.removeNullProperties=u,exports.removeUndefinedAndNull=l,exports.stringifyIfDefined=o,exports.stringifyProperties=s;
package/dist/sql.d.ts ADDED
@@ -0,0 +1,114 @@
1
+ /**
2
+ * Date conversion utilities shared by the SQL adapters (kysely, drizzle).
3
+ *
4
+ * Databases may hold timestamps in either of two formats during migration:
5
+ * - Old format: varchar(35) ISO 8601 strings (e.g., "2024-01-15T10:30:00.000Z")
6
+ * - New format: bigint Unix timestamps in milliseconds (e.g., 1705315800000),
7
+ * stored in `_ts`-suffixed columns
8
+ *
9
+ * The adapter interface always uses ISO strings, so adapters convert:
10
+ * - On READ: detect the stored format and convert to an ISO string
11
+ * - On WRITE: write as bigint timestamp (new format)
12
+ *
13
+ * This allows zero-downtime migration where:
14
+ * 1. Deploy code that reads both formats, writes new format
15
+ * 2. Run migration to convert existing data
16
+ * 3. (Optional) Remove old format support after migration completes
17
+ */
18
+ /**
19
+ * Type for a date field that could be either format from the database
20
+ */
21
+ type DbDateField = string | number | null | undefined;
22
+ /**
23
+ * Convert a database date field (either format) to an ISO string for the
24
+ * adapter interface. Returns undefined if the value is null/undefined/empty.
25
+ */
26
+ declare function dbDateToIso(value: DbDateField): string | undefined;
27
+ /**
28
+ * Convert a database date field to ISO string, with a required fallback.
29
+ * Use this for non-nullable date fields like created_at.
30
+ */
31
+ declare function dbDateToIsoRequired(value: DbDateField, fallback?: string): string;
32
+ /**
33
+ * Convert an ISO string from the adapter interface to a bigint timestamp
34
+ * for writing to the database.
35
+ */
36
+ declare function isoToDbDate(isoString: string | null | undefined): number | null;
37
+ /**
38
+ * Get current timestamp as ISO string (for adapter interface)
39
+ */
40
+ declare function nowIso(): string;
41
+ /**
42
+ * Convert date fields from DB format to adapter format (ISO strings).
43
+ * Strips the _ts suffix from column names (e.g., created_at_ts -> created_at).
44
+ * @param row - The database row object
45
+ * @param requiredColumns - Columns that must have a value (will use fallback if null)
46
+ * @param optionalColumns - Columns that can be undefined
47
+ * @returns Object with converted date fields (with _ts suffix stripped)
48
+ */
49
+ declare function convertDatesToAdapter<T extends Record<string, DbDateField>, R extends Extract<keyof T, string>, O extends Extract<keyof T, string> = never>(row: T, requiredColumns: R[], optionalColumns?: O[]): Record<string, string | undefined>;
50
+
51
+ /**
52
+ * Row/entity transformation helpers shared by the SQL adapters (kysely,
53
+ * drizzle). They convert between the adapter interface's rich entity shapes
54
+ * (nested objects, booleans, optional fields) and the flat SQL row shapes
55
+ * (JSON strings, 0/1 integers, no undefined values).
56
+ */
57
+ /**
58
+ * Stringify a value to JSON if it's defined, otherwise return undefined.
59
+ * This is useful for converting objects to JSON strings for database storage.
60
+ */
61
+ declare function stringifyIfDefined<T>(value: T | undefined): string | undefined;
62
+ /**
63
+ * Stringify multiple properties of an object to JSON strings.
64
+ * Only properties that are defined will be stringified.
65
+ *
66
+ * @param obj - The source object containing properties to stringify
67
+ * @param properties - Array of property names to stringify
68
+ * @param target - The target object to write stringified values to (defaults to a new object based on obj)
69
+ * @returns The target object with stringified properties
70
+ *
71
+ * @example
72
+ * const source = { flags: { enabled: true }, sessions: { timeout: 300 }, name: "Test" };
73
+ * const result = stringifyProperties(source, ['flags', 'sessions']);
74
+ * // result = { flags: '{"enabled":true}', sessions: '{"timeout":300}', name: "Test" }
75
+ */
76
+ type StringifiedProperties<T, K extends keyof T> = {
77
+ [P in keyof T]: P extends K ? undefined extends T[P] ? string | undefined : string : T[P];
78
+ };
79
+ declare function stringifyProperties<T extends Record<string, unknown>, K extends keyof T & string>(obj: T, properties: K[]): StringifiedProperties<T, K>;
80
+ declare function stringifyProperties<T extends Record<string, unknown>, U extends Record<string, unknown>>(obj: T, properties: (keyof T & string)[], target: U): U;
81
+ /**
82
+ * Convert boolean properties to integers (1 for true, 0 for false).
83
+ * Only properties that are defined will be converted.
84
+ *
85
+ * @param source - The source object containing boolean properties to convert
86
+ * @param properties - Array of property names to convert
87
+ * @param target - The target object to write integer values to (defaults to source)
88
+ *
89
+ * @example
90
+ * const source = { enabled: true, active: false, name: "Test" };
91
+ * const result = {};
92
+ * booleanToInt(source, ['enabled', 'active'], result);
93
+ * // result = { enabled: 1, active: 0 }
94
+ */
95
+ declare function booleanToInt<T extends Record<string, unknown>>(source: Partial<T>, properties: (keyof T & string)[], target?: Record<string, unknown>): void;
96
+ /**
97
+ * Remove undefined and null properties from an object.
98
+ * This keeps the SQL payload clean by only including defined values.
99
+ */
100
+ declare function removeUndefinedAndNull<T extends Record<string, unknown>>(obj: T): Partial<T>;
101
+ /**
102
+ * Remove null properties from an object recursively. Used when mapping SQL
103
+ * rows (where missing values are NULL) back to adapter entities (where they
104
+ * are absent).
105
+ */
106
+ declare function removeNullProperties<T>(obj: unknown): T;
107
+ /**
108
+ * Get a COUNT(*) result as an integer regardless of how the DB driver
109
+ * returns it (string, number, or bigint).
110
+ */
111
+ declare function getCountAsInt(count: string | number | bigint): number;
112
+
113
+ export { booleanToInt, convertDatesToAdapter, dbDateToIso, dbDateToIsoRequired, getCountAsInt, isoToDbDate, nowIso, removeNullProperties, removeUndefinedAndNull, stringifyIfDefined, stringifyProperties };
114
+ export type { DbDateField };
package/dist/sql.mjs ADDED
@@ -0,0 +1,56 @@
1
+ //#region src/sql/dates.ts
2
+ function e(e) {
3
+ let t = new Date(e);
4
+ return Number.isNaN(t.getTime()) ? void 0 : t.toISOString();
5
+ }
6
+ function t(t) {
7
+ if (t != null) {
8
+ if (typeof t == "number") return e(t);
9
+ if (typeof t == "string") return t === "" ? void 0 : /^-?\d+(\.\d+)?$/.test(t) ? e(parseFloat(t)) : t;
10
+ }
11
+ }
12
+ function n(e, n = (/* @__PURE__ */ new Date(0)).toISOString()) {
13
+ return t(e) ?? n;
14
+ }
15
+ function r(e) {
16
+ if (!e || e === "") return null;
17
+ let t = new Date(e);
18
+ return isNaN(t.getTime()) ? null : t.getTime();
19
+ }
20
+ function i() {
21
+ return (/* @__PURE__ */ new Date()).toISOString();
22
+ }
23
+ function a(e, r, i = []) {
24
+ let a = {};
25
+ for (let n of i) a[n.replace(/_ts$/, "")] = t(e[n]);
26
+ for (let t of r) a[t.replace(/_ts$/, "")] = n(e[t]);
27
+ return a;
28
+ }
29
+ //#endregion
30
+ //#region src/sql/transform.ts
31
+ function o(e) {
32
+ return e === void 0 ? void 0 : JSON.stringify(e);
33
+ }
34
+ function s(e, t, n = { ...e }) {
35
+ for (let r of t) e[r] !== void 0 && (n[r] = JSON.stringify(e[r]));
36
+ return n;
37
+ }
38
+ function c(e, t, n = e) {
39
+ for (let r of t) e[r] !== void 0 && (n[r] = +!!e[r]);
40
+ }
41
+ function l(e) {
42
+ return Object.fromEntries(Object.entries(e).filter(([, e]) => e != null));
43
+ }
44
+ function u(e) {
45
+ if (typeof e != "object" || !e) return e;
46
+ if (Array.isArray(e)) return e.map((e) => typeof e == "object" && e ? u(e) : e);
47
+ let t = Object.getPrototypeOf(e);
48
+ return t !== Object.prototype && t !== null ? e : Object.fromEntries(Object.entries(e).filter(([, e]) => e !== null).map(([e, t]) => [e, typeof t == "object" ? u(t) : t]));
49
+ }
50
+ function d(e) {
51
+ let t = Number(e);
52
+ if (!Number.isSafeInteger(t)) throw RangeError(`COUNT result is not a safe integer: ${String(e)}`);
53
+ return t;
54
+ }
55
+ //#endregion
56
+ export { c as booleanToInt, a as convertDatesToAdapter, t as dbDateToIso, n as dbDateToIsoRequired, d as getCountAsInt, r as isoToDbDate, i as nowIso, u as removeNullProperties, l as removeUndefinedAndNull, o as stringifyIfDefined, s as stringifyProperties };