core-services-sdk 1.3.62 → 1.3.64
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/.claude/settings.local.json +22 -0
- package/package.json +1 -1
- package/src/core/normalize-phone-number.js +144 -25
- package/src/postgresql/apply-filter.js +141 -15
- package/tests/core/normalize-phone-number.unit.test.js +45 -0
- package/tests/postgresql/apply-filter-snake-case.integration.test.js +549 -0
- package/tests/postgresql/apply-filter.integration.test.js +56 -325
- package/tests/postgresql/paginate.integration.test.js +2 -2
- package/tests/postgresql/validate-schema.integration.test.js +2 -2
- package/types/core/normalize-phone-number.d.ts +97 -27
- package/types/postgresql/apply-filter.d.ts +231 -0
- package/types/postgresql/index.d.ts +1 -0
|
@@ -1,25 +1,82 @@
|
|
|
1
|
-
/** Resolve libphonenumber regardless of interop shape */
|
|
2
|
-
export function getLib(): any
|
|
3
|
-
export function phoneUtil(): any
|
|
4
1
|
/**
|
|
5
|
-
*
|
|
6
|
-
* @
|
|
7
|
-
* @
|
|
8
|
-
* @
|
|
2
|
+
* @typedef {Object} NormalizedPhone
|
|
3
|
+
* @property {string} e164
|
|
4
|
+
* @property {number} type
|
|
5
|
+
* @property {string} national
|
|
6
|
+
* @property {number} countryCode
|
|
7
|
+
* @property {string} nationalClean
|
|
8
|
+
* @property {string} international
|
|
9
|
+
* @property {string} countryCodeE164
|
|
10
|
+
* @property {string} internationalClean
|
|
11
|
+
* @property {string | undefined} regionCode
|
|
9
12
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
/**
|
|
14
|
+
* normalize-phone-number.js
|
|
15
|
+
*
|
|
16
|
+
* Utilities for parsing, validating, and normalizing phone numbers
|
|
17
|
+
* using google-libphonenumber.
|
|
18
|
+
*
|
|
19
|
+
* Supports both ESM and CJS interop builds.
|
|
20
|
+
* All normalization outputs are canonical and safe for persistence,
|
|
21
|
+
* comparison, indexing, and login identifiers.
|
|
22
|
+
*/
|
|
23
|
+
/**
|
|
24
|
+
* Resolve google-libphonenumber exports regardless of ESM / CJS shape.
|
|
25
|
+
*
|
|
26
|
+
* Some builds expose:
|
|
27
|
+
* - raw.PhoneNumberUtil
|
|
28
|
+
* Others expose:
|
|
29
|
+
* - raw.default.PhoneNumberUtil
|
|
30
|
+
*
|
|
31
|
+
* This helper guarantees a consistent API.
|
|
32
|
+
*
|
|
33
|
+
* @returns {{
|
|
34
|
+
* PhoneNumberUtil: any,
|
|
35
|
+
* PhoneNumberFormat: any
|
|
36
|
+
* }}
|
|
37
|
+
* @throws {Error} If required exports are missing
|
|
38
|
+
*/
|
|
39
|
+
export function getLib(): {
|
|
40
|
+
PhoneNumberUtil: any
|
|
41
|
+
PhoneNumberFormat: any
|
|
16
42
|
}
|
|
17
43
|
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
44
|
+
* Lazy singleton accessor for PhoneNumberUtil.
|
|
45
|
+
*
|
|
46
|
+
* Ensures:
|
|
47
|
+
* - Single instance per process
|
|
48
|
+
* - No eager initialization cost
|
|
49
|
+
*
|
|
50
|
+
* @returns {any} PhoneNumberUtil instance
|
|
51
|
+
*/
|
|
52
|
+
export function phoneUtil(): any
|
|
53
|
+
/**
|
|
54
|
+
* Normalize and validate an international phone number.
|
|
55
|
+
*
|
|
56
|
+
* Input MUST start with '+'.
|
|
57
|
+
*
|
|
58
|
+
* @param {string} input International phone number (E.164-like)
|
|
59
|
+
* @returns {NormalizedPhone}
|
|
60
|
+
* @throws {Error} If the phone number is invalid
|
|
61
|
+
*/
|
|
62
|
+
export function normalizePhoneOrThrowIntl(input: string): NormalizedPhone
|
|
63
|
+
/**
|
|
64
|
+
* Normalize and validate a national phone number using a region hint.
|
|
65
|
+
*
|
|
66
|
+
* Example:
|
|
67
|
+
* input: "0523444444"
|
|
68
|
+
* defaultRegion: "IL"
|
|
69
|
+
*
|
|
70
|
+
* @param {string} input National phone number
|
|
71
|
+
* @param {string} defaultRegion ISO 3166-1 alpha-2 country code
|
|
72
|
+
* @returns {{
|
|
73
|
+
* e164: string,
|
|
74
|
+
* national: string,
|
|
75
|
+
* international: string,
|
|
76
|
+
* regionCode: string | undefined,
|
|
77
|
+
* type: number
|
|
78
|
+
* }}
|
|
79
|
+
* @throws {Error} If the phone number is invalid
|
|
23
80
|
*/
|
|
24
81
|
export function normalizePhoneOrThrowWithRegion(
|
|
25
82
|
input: string,
|
|
@@ -32,23 +89,36 @@ export function normalizePhoneOrThrowWithRegion(
|
|
|
32
89
|
type: number
|
|
33
90
|
}
|
|
34
91
|
/**
|
|
35
|
-
* Smart normalization
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
92
|
+
* Smart normalization entry point.
|
|
93
|
+
*
|
|
94
|
+
* Behavior:
|
|
95
|
+
* - If input starts with '+', parses as international
|
|
96
|
+
* - Otherwise requires defaultRegion and parses as national
|
|
97
|
+
*
|
|
98
|
+
* This is the recommended function for login, signup, and verification flows.
|
|
99
|
+
*
|
|
100
|
+
* @param {string} input Phone number (international or national)
|
|
101
|
+
* @param {{
|
|
102
|
+
* defaultRegion?: string
|
|
103
|
+
* }} [opts]
|
|
104
|
+
*
|
|
105
|
+
* @returns {NormalizedPhone}
|
|
106
|
+
* @throws {Error} If invalid or defaultRegion is missing
|
|
42
107
|
*/
|
|
43
108
|
export function normalizePhoneOrThrow(
|
|
44
109
|
input: string,
|
|
45
110
|
opts?: {
|
|
46
111
|
defaultRegion?: string
|
|
47
112
|
},
|
|
48
|
-
):
|
|
113
|
+
): NormalizedPhone
|
|
114
|
+
export type NormalizedPhone = {
|
|
49
115
|
e164: string
|
|
116
|
+
type: number
|
|
50
117
|
national: string
|
|
118
|
+
countryCode: number
|
|
119
|
+
nationalClean: string
|
|
51
120
|
international: string
|
|
121
|
+
countryCodeE164: string
|
|
122
|
+
internationalClean: string
|
|
52
123
|
regionCode: string | undefined
|
|
53
|
-
type: number
|
|
54
124
|
}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builds a Knex query with MongoDB-style filter operators.
|
|
3
|
+
* Pure utility function that can be used across repositories.
|
|
4
|
+
*
|
|
5
|
+
* This function applies various filter operators to build SQL WHERE clauses. All column names
|
|
6
|
+
* are automatically qualified with the provided table name. Filter keys are used as-is without
|
|
7
|
+
* any case conversion. If you need automatic camelCase to snake_case conversion, use
|
|
8
|
+
* {@link applyFilterSnakeCase} instead.
|
|
9
|
+
*
|
|
10
|
+
* **Supported Operators:**
|
|
11
|
+
* - `eq` - Equality (default for simple values)
|
|
12
|
+
* - `ne` / `neq` - Not equal
|
|
13
|
+
* - `in` - Array membership (or pass array directly)
|
|
14
|
+
* - `nin` - Not in array
|
|
15
|
+
* - `gt` - Greater than
|
|
16
|
+
* - `gte` - Greater than or equal
|
|
17
|
+
* - `lt` - Less than
|
|
18
|
+
* - `lte` - Less than or equal
|
|
19
|
+
* - `like` - Case-sensitive pattern matching (SQL LIKE)
|
|
20
|
+
* - `ilike` - Case-insensitive pattern matching (PostgreSQL ILIKE)
|
|
21
|
+
* - `isNull` - Check if field is NULL
|
|
22
|
+
* - `isNotNull` - Check if field is NOT NULL
|
|
23
|
+
*
|
|
24
|
+
* **Key Features:**
|
|
25
|
+
* - Filter keys are used as-is (no automatic case conversion)
|
|
26
|
+
* - Qualified column names (table.column format)
|
|
27
|
+
* - Multiple operators can be applied to the same field
|
|
28
|
+
* - Unknown operators are silently ignored
|
|
29
|
+
* - Arrays are automatically converted to IN clauses
|
|
30
|
+
*
|
|
31
|
+
* **Note:** This function does NOT convert filter keys. If your database uses snake_case columns
|
|
32
|
+
* but your filter keys are in camelCase, use {@link applyFilterSnakeCase} instead, or ensure
|
|
33
|
+
* your filter keys already match your database column names.
|
|
34
|
+
*
|
|
35
|
+
* @param {Object} params - Function parameters
|
|
36
|
+
* @param {import('knex').Knex.QueryBuilder} params.query - Knex query builder instance to apply filters to
|
|
37
|
+
* @param {Object<string, *>} params.filter - Filter object with keys matching database column names (used as-is)
|
|
38
|
+
* Filter values can be:
|
|
39
|
+
* - Simple values (string, number, boolean) → treated as equality
|
|
40
|
+
* - Arrays → treated as IN operator
|
|
41
|
+
* - Objects with operator keys → apply specific operators
|
|
42
|
+
* @param {string} params.tableName - Table name used to qualify column names (e.g., "assets" → "assets.column_name")
|
|
43
|
+
* @returns {import('knex').Knex.QueryBuilder} Modified query builder with applied WHERE clauses
|
|
44
|
+
*
|
|
45
|
+
* @throws {TypeError} If query is not a valid Knex QueryBuilder instance
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* // Simple equality - converts to WHERE assets.status = 'active'
|
|
49
|
+
* const query = db('assets').select('*')
|
|
50
|
+
* applyFilter({ query, filter: { status: 'active' }, tableName: 'assets' })
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* // Not equal - converts to WHERE assets.status != 'deleted'
|
|
54
|
+
* applyFilter({ query, filter: { status: { ne: 'deleted' } }, tableName: 'assets' })
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* // Array/IN operator - converts to WHERE assets.status IN ('active', 'pending')
|
|
58
|
+
* applyFilter({ query, filter: { status: ['active', 'pending'] }, tableName: 'assets' })
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* // Explicit IN operator
|
|
62
|
+
* applyFilter({ query, filter: { status: { in: ['active', 'pending'] } }, tableName: 'assets' })
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* // Not in array - converts to WHERE assets.status NOT IN ('deleted', 'archived')
|
|
66
|
+
* applyFilter({ query, filter: { status: { nin: ['deleted', 'archived'] } }, tableName: 'assets' })
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* // Range operators - converts to WHERE assets.price >= 100 AND assets.price <= 200
|
|
70
|
+
* applyFilter({ query, filter: { price: { gte: 100, lte: 200 } }, tableName: 'assets' })
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* // Pattern matching (case-sensitive) - converts to WHERE assets.name LIKE '%invoice%'
|
|
74
|
+
* applyFilter({ query, filter: { name: { like: '%invoice%' } }, tableName: 'assets' })
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* // Pattern matching (case-insensitive) - converts to WHERE assets.name ILIKE '%invoice%'
|
|
78
|
+
* applyFilter({ query, filter: { name: { ilike: '%invoice%' } }, tableName: 'assets' })
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* // Null checks - converts to WHERE assets.deleted_at IS NULL
|
|
82
|
+
* applyFilter({ query, filter: { deletedAt: { isNull: true } }, tableName: 'assets' })
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* // Multiple filters - converts to WHERE assets.status = 'active' AND assets.type = 'invoice' AND assets.price >= 100
|
|
86
|
+
* applyFilter({
|
|
87
|
+
* query,
|
|
88
|
+
* filter: {
|
|
89
|
+
* status: 'active',
|
|
90
|
+
* type: 'invoice',
|
|
91
|
+
* price: { gte: 100 }
|
|
92
|
+
* },
|
|
93
|
+
* tableName: 'assets'
|
|
94
|
+
* })
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* // Filter keys are used as-is - ensure they match your database column names
|
|
98
|
+
* applyFilter({ query, filter: { deleted_at: { isNull: true } }, tableName: 'assets' })
|
|
99
|
+
* // SQL: WHERE assets.deleted_at IS NULL
|
|
100
|
+
* // Note: If you need camelCase conversion, use applyFilterSnakeCase instead
|
|
101
|
+
*/
|
|
102
|
+
export function applyFilter({
|
|
103
|
+
query,
|
|
104
|
+
filter,
|
|
105
|
+
tableName,
|
|
106
|
+
}: {
|
|
107
|
+
query: import('knex').Knex.QueryBuilder
|
|
108
|
+
filter: {
|
|
109
|
+
[x: string]: any
|
|
110
|
+
}
|
|
111
|
+
tableName: string
|
|
112
|
+
}): import('knex').Knex.QueryBuilder
|
|
113
|
+
/**
|
|
114
|
+
* Applies MongoDB-style filter operators to a Knex query with automatic camelCase to snake_case conversion.
|
|
115
|
+
*
|
|
116
|
+
* This function is a convenience wrapper around {@link applyFilter} that automatically converts
|
|
117
|
+
* all filter keys from camelCase to snake_case before applying the filters. This is useful when
|
|
118
|
+
* your application code uses camelCase naming conventions but your database columns use snake_case.
|
|
119
|
+
*
|
|
120
|
+
* The function first converts all filter object keys using `toSnakeCase`, then applies the filters
|
|
121
|
+
* using the standard `applyFilter` function. This ensures that filter keys like `userId` are
|
|
122
|
+
* converted to `user_id` before being used in SQL queries.
|
|
123
|
+
*
|
|
124
|
+
* **Key Features:**
|
|
125
|
+
* - Automatic camelCase to snake_case key conversion
|
|
126
|
+
* - Same operator support as `applyFilter`
|
|
127
|
+
* - Qualified column names (table.column format)
|
|
128
|
+
* - Multiple operators can be applied to the same field
|
|
129
|
+
* - Arrays are automatically converted to IN clauses
|
|
130
|
+
*
|
|
131
|
+
* **Supported Operators:**
|
|
132
|
+
* Same as {@link applyFilter}:
|
|
133
|
+
* - `eq` - Equality (default for simple values)
|
|
134
|
+
* - `ne` / `neq` - Not equal
|
|
135
|
+
* - `in` - Array membership (or pass array directly)
|
|
136
|
+
* - `nin` - Not in array
|
|
137
|
+
* - `gt` - Greater than
|
|
138
|
+
* - `gte` - Greater than or equal
|
|
139
|
+
* - `lt` - Less than
|
|
140
|
+
* - `lte` - Less than or equal
|
|
141
|
+
* - `like` - Case-sensitive pattern matching (SQL LIKE)
|
|
142
|
+
* - `ilike` - Case-insensitive pattern matching (PostgreSQL ILIKE)
|
|
143
|
+
* - `isNull` - Check if field is NULL
|
|
144
|
+
* - `isNotNull` - Check if field is NOT NULL
|
|
145
|
+
*
|
|
146
|
+
* @param {Object} params - Function parameters
|
|
147
|
+
* @param {import('knex').Knex.QueryBuilder} params.query - Knex query builder instance to apply filters to
|
|
148
|
+
* @param {Object<string, *>} params.filter - Filter object with camelCase keys (will be converted to snake_case)
|
|
149
|
+
* Filter values can be:
|
|
150
|
+
* - Simple values (string, number, boolean) → treated as equality
|
|
151
|
+
* - Arrays → treated as IN operator
|
|
152
|
+
* - Objects with operator keys → apply specific operators
|
|
153
|
+
* @param {string} params.tableName - Table name used to qualify column names (e.g., "assets" → "assets.column_name")
|
|
154
|
+
* @returns {import('knex').Knex.QueryBuilder} Modified query builder with applied WHERE clauses (using snake_case column names)
|
|
155
|
+
*
|
|
156
|
+
* @throws {TypeError} If query is not a valid Knex QueryBuilder instance
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* // Simple equality with camelCase key - converts to WHERE assets.user_id = 1
|
|
160
|
+
* const query = db('assets').select('*')
|
|
161
|
+
* applyFilterSnakeCase({ query, filter: { userId: 1 }, tableName: 'assets' })
|
|
162
|
+
* // SQL: WHERE assets.user_id = 1
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* // Not equal with camelCase key - converts to WHERE assets.status != 'deleted'
|
|
166
|
+
* applyFilterSnakeCase({
|
|
167
|
+
* query,
|
|
168
|
+
* filter: { status: { ne: 'deleted' } },
|
|
169
|
+
* tableName: 'assets'
|
|
170
|
+
* })
|
|
171
|
+
* // SQL: WHERE assets.status != 'deleted'
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* // Array/IN operator with camelCase key - converts to WHERE assets.status IN ('active', 'pending')
|
|
175
|
+
* applyFilterSnakeCase({
|
|
176
|
+
* query,
|
|
177
|
+
* filter: { status: ['active', 'pending'] },
|
|
178
|
+
* tableName: 'assets'
|
|
179
|
+
* })
|
|
180
|
+
* // SQL: WHERE assets.status IN ('active', 'pending')
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* // Range operators with camelCase keys - converts to WHERE assets.price >= 100 AND assets.price <= 200
|
|
184
|
+
* applyFilterSnakeCase({
|
|
185
|
+
* query,
|
|
186
|
+
* filter: { price: { gte: 100, lte: 200 } },
|
|
187
|
+
* tableName: 'assets'
|
|
188
|
+
* })
|
|
189
|
+
* // SQL: WHERE assets.price >= 100 AND assets.price <= 200
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* // Null checks with camelCase key - converts to WHERE assets.deleted_at IS NULL
|
|
193
|
+
* applyFilterSnakeCase({
|
|
194
|
+
* query,
|
|
195
|
+
* filter: { deletedAt: { isNull: true } },
|
|
196
|
+
* tableName: 'assets'
|
|
197
|
+
* })
|
|
198
|
+
* // SQL: WHERE assets.deleted_at IS NULL
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* // Multiple filters with camelCase keys
|
|
202
|
+
* applyFilterSnakeCase({
|
|
203
|
+
* query,
|
|
204
|
+
* filter: {
|
|
205
|
+
* userId: 123, // Converts to user_id
|
|
206
|
+
* createdAt: { gte: '2024-01-01' }, // Converts to created_at
|
|
207
|
+
* status: 'active'
|
|
208
|
+
* },
|
|
209
|
+
* tableName: 'assets'
|
|
210
|
+
* })
|
|
211
|
+
* // SQL: WHERE assets.user_id = 123 AND assets.created_at >= '2024-01-01' AND assets.status = 'active'
|
|
212
|
+
*
|
|
213
|
+
* @see {@link applyFilter} For the base function without key conversion
|
|
214
|
+
* @see {@link toSnakeCase} For details on the key conversion process
|
|
215
|
+
*/
|
|
216
|
+
export function applyFilterSnakeCase({
|
|
217
|
+
query,
|
|
218
|
+
filter,
|
|
219
|
+
tableName,
|
|
220
|
+
}: {
|
|
221
|
+
query: import('knex').Knex.QueryBuilder
|
|
222
|
+
filter: {
|
|
223
|
+
[x: string]: any
|
|
224
|
+
}
|
|
225
|
+
tableName: string
|
|
226
|
+
}): import('knex').Knex.QueryBuilder
|
|
227
|
+
/**
|
|
228
|
+
* Type definition for filter operator functions.
|
|
229
|
+
* Each operator function applies a WHERE condition to a Knex query builder.
|
|
230
|
+
*/
|
|
231
|
+
export type OperatorFunction = Function
|