timezest 1.1.2 → 1.1.4
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/LICENSE +8 -8
- package/README.md +31 -9
- package/{config → dist/config}/config.d.ts +3 -0
- package/{config → dist/config}/config.js +5 -2
- package/dist/config/config.js.map +1 -0
- package/dist/index.d.ts +99 -0
- package/{index.js → dist/index.js} +27 -24
- package/dist/index.js.map +1 -0
- package/{utils → dist/utils}/makePaginatedRequest.d.ts +3 -2
- package/{utils → dist/utils}/makePaginatedRequest.js +3 -2
- package/dist/utils/makePaginatedRequest.js.map +1 -0
- package/{utils → dist/utils}/makeRequest.js +30 -7
- package/dist/utils/makeRequest.js.map +1 -0
- package/dist/utils/tqlFilter.d.ts +260 -0
- package/dist/utils/tqlFilter.js +439 -0
- package/dist/utils/tqlFilter.js.map +1 -0
- package/package.json +1 -1
- package/config/config.js.map +0 -1
- package/index.d.ts +0 -97
- package/index.js.map +0 -1
- package/utils/makePaginatedRequest.js.map +0 -1
- package/utils/makeRequest.js.map +0 -1
- /package/{constants → dist/constants}/endpoints.d.ts +0 -0
- /package/{constants → dist/constants}/endpoints.js +0 -0
- /package/{constants → dist/constants}/endpoints.js.map +0 -0
- /package/{entities → dist/entities}/entities.d.ts +0 -0
- /package/{entities → dist/entities}/entities.js +0 -0
- /package/{entities → dist/entities}/entities.js.map +0 -0
- /package/{entities → dist/entities}/schemas.d.ts +0 -0
- /package/{entities → dist/entities}/schemas.js +0 -0
- /package/{entities → dist/entities}/schemas.js.map +0 -0
- /package/{utils → dist/utils}/handleError.d.ts +0 -0
- /package/{utils → dist/utils}/handleError.js +0 -0
- /package/{utils → dist/utils}/handleError.js.map +0 -0
- /package/{utils → dist/utils}/logger.d.ts +0 -0
- /package/{utils → dist/utils}/logger.js +0 -0
- /package/{utils → dist/utils}/logger.js.map +0 -0
- /package/{utils → dist/utils}/makeRequest.d.ts +0 -0
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TimeZest Query Language (TQL) Filter Builder
|
|
3
|
+
*
|
|
4
|
+
* Provides a user-friendly, fluent API for constructing valid TQL filters.
|
|
5
|
+
* Based on the TimeZest TQL documentation: https://developer.timezest.com/tql/
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* // Type-safe filtering with autocomplete (recommended)
|
|
10
|
+
* const filter = TQL.forSchedulingRequests()
|
|
11
|
+
* .filter('status').eq('scheduled');
|
|
12
|
+
*
|
|
13
|
+
* // Type-safe chaining with AND/OR
|
|
14
|
+
* const filter = TQL.forSchedulingRequests()
|
|
15
|
+
* .filter('end_user_email').like('@example.com')
|
|
16
|
+
* .and('status').eq('scheduled');
|
|
17
|
+
*
|
|
18
|
+
* // Using with API - no toString() needed!
|
|
19
|
+
* const requests = await timeZest.getSchedulingRequests(
|
|
20
|
+
* TQL.forSchedulingRequests().filter('status').eq('scheduled')
|
|
21
|
+
* );
|
|
22
|
+
*
|
|
23
|
+
* // Flexible filtering (no type checking, still works)
|
|
24
|
+
* const filter = TQL.filter('scheduling_request.status').eq('scheduled');
|
|
25
|
+
*
|
|
26
|
+
* // Call toString() explicitly if you need the string
|
|
27
|
+
* const filterString = TQL.forAgents().filter('name').like('John').toString();
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
import type { Agent, Resource, Team, AppointmentType, SchedulingRequest } from '../entities/entities';
|
|
31
|
+
/**
|
|
32
|
+
* Union of all valid TQL attribute paths.
|
|
33
|
+
* This is automatically inferred from entity types when using the type-safe helpers.
|
|
34
|
+
*/
|
|
35
|
+
export type TQLAttribute = `agent.${keyof Agent & string}` | `resource.${keyof Resource & string}` | `team.${keyof Team & string}` | `appointment_type.${keyof AppointmentType & string}` | `scheduling_request.${keyof SchedulingRequest & string}`;
|
|
36
|
+
/**
|
|
37
|
+
* Represents a TQL filter being constructed.
|
|
38
|
+
*/
|
|
39
|
+
export declare class TQLFilter<TAttribute extends string = string> {
|
|
40
|
+
private predicates;
|
|
41
|
+
private logicalOperators;
|
|
42
|
+
private entityPrefix;
|
|
43
|
+
private currentAttribute;
|
|
44
|
+
private currentOperator;
|
|
45
|
+
private currentValue;
|
|
46
|
+
/**
|
|
47
|
+
* Creates a new TQL filter starting with the given attribute.
|
|
48
|
+
* @param attribute - The attribute to filter on (e.g., 'scheduling_request.status')
|
|
49
|
+
* @param entityPrefix - Optional prefix for this entity type (e.g., 'scheduling_request')
|
|
50
|
+
*/
|
|
51
|
+
constructor(attribute: TAttribute, entityPrefix?: string);
|
|
52
|
+
/**
|
|
53
|
+
* Adds a completed predicate and starts a new one.
|
|
54
|
+
*/
|
|
55
|
+
private finalizePredicate;
|
|
56
|
+
/**
|
|
57
|
+
* Sets the operator and value for comparison operations.
|
|
58
|
+
*/
|
|
59
|
+
private setComparison;
|
|
60
|
+
/**
|
|
61
|
+
* Equals operator (EQ)
|
|
62
|
+
* @param value - The value to compare against
|
|
63
|
+
*/
|
|
64
|
+
eq(value: string | number): this;
|
|
65
|
+
/**
|
|
66
|
+
* Not equals operator (NOT_EQ)
|
|
67
|
+
* @param value - The value to compare against
|
|
68
|
+
*/
|
|
69
|
+
notEq(value: string | number): this;
|
|
70
|
+
/**
|
|
71
|
+
* Like operator (contains pattern matching)
|
|
72
|
+
* @param value - The pattern to match against
|
|
73
|
+
*/
|
|
74
|
+
like(value: string): this;
|
|
75
|
+
/**
|
|
76
|
+
* Not like operator (does not contain pattern)
|
|
77
|
+
* @param value - The pattern to exclude
|
|
78
|
+
*/
|
|
79
|
+
notLike(value: string): this;
|
|
80
|
+
/**
|
|
81
|
+
* In operator (value is in the provided array)
|
|
82
|
+
* @param values - Array of values to match against
|
|
83
|
+
*/
|
|
84
|
+
in(values: string[] | number[]): this;
|
|
85
|
+
/**
|
|
86
|
+
* Not in operator (value is not in the provided array)
|
|
87
|
+
* @param values - Array of values to exclude
|
|
88
|
+
*/
|
|
89
|
+
notIn(values: string[] | number[]): this;
|
|
90
|
+
/**
|
|
91
|
+
* Greater than operator (GT)
|
|
92
|
+
* @param value - The value to compare against
|
|
93
|
+
*/
|
|
94
|
+
gt(value: number): this;
|
|
95
|
+
/**
|
|
96
|
+
* Greater than or equal operator (GTE)
|
|
97
|
+
* @param value - The value to compare against
|
|
98
|
+
*/
|
|
99
|
+
gte(value: number): this;
|
|
100
|
+
/**
|
|
101
|
+
* Less than operator (LT)
|
|
102
|
+
* @param value - The value to compare against
|
|
103
|
+
*/
|
|
104
|
+
lt(value: number): this;
|
|
105
|
+
/**
|
|
106
|
+
* Less than or equal operator (LTE)
|
|
107
|
+
* @param value - The value to compare against
|
|
108
|
+
*/
|
|
109
|
+
lte(value: number): this;
|
|
110
|
+
/**
|
|
111
|
+
* Adds an AND logical operator and starts a new predicate with the given attribute.
|
|
112
|
+
* If this filter was created with an entity prefix and you pass just a property name,
|
|
113
|
+
* it will automatically prepend the entity prefix.
|
|
114
|
+
* @param attribute - The attribute for the next predicate (full path or property name if using entity prefix)
|
|
115
|
+
*/
|
|
116
|
+
and(attribute: TAttribute | string): this;
|
|
117
|
+
/**
|
|
118
|
+
* Adds an OR logical operator and starts a new predicate with the given attribute.
|
|
119
|
+
* If this filter was created with an entity prefix and you pass just a property name,
|
|
120
|
+
* it will automatically prepend the entity prefix.
|
|
121
|
+
* @param attribute - The attribute for the next predicate (full path or property name if using entity prefix)
|
|
122
|
+
*/
|
|
123
|
+
or(attribute: TAttribute | string): this;
|
|
124
|
+
/**
|
|
125
|
+
* Formats a value for TQL output.
|
|
126
|
+
* Handles arrays, strings with special characters, and numbers.
|
|
127
|
+
*/
|
|
128
|
+
private formatValue;
|
|
129
|
+
/**
|
|
130
|
+
* Formats a single value for TQL output.
|
|
131
|
+
*/
|
|
132
|
+
private formatSingleValue;
|
|
133
|
+
/**
|
|
134
|
+
* Replaces spaces with tildes outside of quoted strings.
|
|
135
|
+
* This is needed for URL encoding while preserving spaces inside quoted values.
|
|
136
|
+
* Handles escaped quotes properly (e.g., "value with \"quotes\"").
|
|
137
|
+
* @param str - The string to process
|
|
138
|
+
* @returns The string with spaces outside quotes replaced by tildes
|
|
139
|
+
*/
|
|
140
|
+
private replaceSpacesOutsideQuotes;
|
|
141
|
+
/**
|
|
142
|
+
* Converts the filter to a TQL string suitable for API requests.
|
|
143
|
+
* In URLs, spaces are replaced with tildes (~).
|
|
144
|
+
* @param urlEncode - If true, replaces spaces with ~ for URL encoding (default: true)
|
|
145
|
+
* @returns The TQL filter string
|
|
146
|
+
*/
|
|
147
|
+
toString(urlEncode?: boolean): string;
|
|
148
|
+
/**
|
|
149
|
+
* Returns the filter as a plain string with spaces (not URL encoded).
|
|
150
|
+
* Useful for debugging or when you need the human-readable format.
|
|
151
|
+
*/
|
|
152
|
+
toHumanReadableString(): string;
|
|
153
|
+
/**
|
|
154
|
+
* Returns the string representation when converted to a primitive.
|
|
155
|
+
* This allows TQLFilter to be used directly without calling toString().
|
|
156
|
+
*/
|
|
157
|
+
valueOf(): string;
|
|
158
|
+
/**
|
|
159
|
+
* Custom primitive conversion for string contexts.
|
|
160
|
+
* Allows TQLFilter to be used directly in places expecting strings.
|
|
161
|
+
*/
|
|
162
|
+
[Symbol.toPrimitive](hint: 'string' | 'number' | 'default'): string;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Normalizes a filter value to a string.
|
|
166
|
+
* Converts TQLFilter instances to strings, leaves strings as-is, and returns null for null.
|
|
167
|
+
* @param filter - The filter value (TQLFilter, string, or null)
|
|
168
|
+
* @returns The normalized filter string or null
|
|
169
|
+
*/
|
|
170
|
+
export declare function normalizeFilter(filter: TQLFilter | string | null): string | null;
|
|
171
|
+
/**
|
|
172
|
+
* Helper class for building type-safe filters for specific entity types.
|
|
173
|
+
* Provides autocomplete and type checking for entity properties.
|
|
174
|
+
*/
|
|
175
|
+
declare class TypedTQLFilterBuilder<T extends Record<string, any>> {
|
|
176
|
+
private prefix;
|
|
177
|
+
constructor(prefix: string);
|
|
178
|
+
/**
|
|
179
|
+
* Starts a filter with a type-safe attribute from the entity.
|
|
180
|
+
* @param attribute - A valid property key from the entity type
|
|
181
|
+
* @returns A TQLFilter instance for method chaining (with entity prefix stored)
|
|
182
|
+
*/
|
|
183
|
+
filter<K extends keyof T>(attribute: K): TQLFilter<`${string}.${string & K}`>;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* TQL Filter Builder
|
|
187
|
+
* Provides a user-friendly, fluent API for constructing TimeZest Query Language filters.
|
|
188
|
+
*/
|
|
189
|
+
export declare class TQL {
|
|
190
|
+
/**
|
|
191
|
+
* Starts building a TQL filter with the given attribute.
|
|
192
|
+
* Use this for flexibility - no type checking on the attribute.
|
|
193
|
+
* For type-safe filtering, use the endpoint-specific helpers like `TQL.forAgents()`.
|
|
194
|
+
*
|
|
195
|
+
* @param attribute - The attribute to filter on (e.g., 'scheduling_request.status')
|
|
196
|
+
* @returns A TQLFilter instance for method chaining
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* ```typescript
|
|
200
|
+
* TQL.filter('scheduling_request.status').eq('scheduled')
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
static filter(attribute: string): TQLFilter<string>;
|
|
204
|
+
/**
|
|
205
|
+
* Creates a type-safe filter builder for Agent entities.
|
|
206
|
+
* Provides autocomplete and type checking for Agent attributes.
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```typescript
|
|
210
|
+
* TQL.forAgents().filter('name').like('John')
|
|
211
|
+
* TQL.forAgents().filter('email').eq('user@example.com')
|
|
212
|
+
* ```
|
|
213
|
+
*/
|
|
214
|
+
static forAgents(): TypedTQLFilterBuilder<Agent>;
|
|
215
|
+
/**
|
|
216
|
+
* Creates a type-safe filter builder for Resource entities.
|
|
217
|
+
* Provides autocomplete and type checking for Resource attributes.
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* ```typescript
|
|
221
|
+
* TQL.forResources().filter('name').like('Room')
|
|
222
|
+
* TQL.forResources().filter('schedulable').eq(true)
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
225
|
+
static forResources(): TypedTQLFilterBuilder<Resource>;
|
|
226
|
+
/**
|
|
227
|
+
* Creates a type-safe filter builder for Team entities.
|
|
228
|
+
* Provides autocomplete and type checking for Team attributes.
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```typescript
|
|
232
|
+
* TQL.forTeams().filter('internal_name').eq('Tier1')
|
|
233
|
+
* TQL.forTeams().filter('team_type').eq('support')
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
236
|
+
static forTeams(): TypedTQLFilterBuilder<Team>;
|
|
237
|
+
/**
|
|
238
|
+
* Creates a type-safe filter builder for AppointmentType entities.
|
|
239
|
+
* Provides autocomplete and type checking for AppointmentType attributes.
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* ```typescript
|
|
243
|
+
* TQL.forAppointmentTypes().filter('internal_name').eq('consultation')
|
|
244
|
+
* TQL.forAppointmentTypes().filter('duration_mins').gte(30)
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
static forAppointmentTypes(): TypedTQLFilterBuilder<AppointmentType>;
|
|
248
|
+
/**
|
|
249
|
+
* Creates a type-safe filter builder for SchedulingRequest entities.
|
|
250
|
+
* Provides autocomplete and type checking for SchedulingRequest attributes.
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```typescript
|
|
254
|
+
* TQL.forSchedulingRequests().filter('status').eq('scheduled')
|
|
255
|
+
* TQL.forSchedulingRequests().filter('end_user_email').like('@example.com')
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
static forSchedulingRequests(): TypedTQLFilterBuilder<SchedulingRequest>;
|
|
259
|
+
}
|
|
260
|
+
export default TQL;
|
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* TimeZest Query Language (TQL) Filter Builder
|
|
4
|
+
*
|
|
5
|
+
* Provides a user-friendly, fluent API for constructing valid TQL filters.
|
|
6
|
+
* Based on the TimeZest TQL documentation: https://developer.timezest.com/tql/
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* // Type-safe filtering with autocomplete (recommended)
|
|
11
|
+
* const filter = TQL.forSchedulingRequests()
|
|
12
|
+
* .filter('status').eq('scheduled');
|
|
13
|
+
*
|
|
14
|
+
* // Type-safe chaining with AND/OR
|
|
15
|
+
* const filter = TQL.forSchedulingRequests()
|
|
16
|
+
* .filter('end_user_email').like('@example.com')
|
|
17
|
+
* .and('status').eq('scheduled');
|
|
18
|
+
*
|
|
19
|
+
* // Using with API - no toString() needed!
|
|
20
|
+
* const requests = await timeZest.getSchedulingRequests(
|
|
21
|
+
* TQL.forSchedulingRequests().filter('status').eq('scheduled')
|
|
22
|
+
* );
|
|
23
|
+
*
|
|
24
|
+
* // Flexible filtering (no type checking, still works)
|
|
25
|
+
* const filter = TQL.filter('scheduling_request.status').eq('scheduled');
|
|
26
|
+
*
|
|
27
|
+
* // Call toString() explicitly if you need the string
|
|
28
|
+
* const filterString = TQL.forAgents().filter('name').like('John').toString();
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
+
exports.TQL = exports.TQLFilter = void 0;
|
|
33
|
+
exports.normalizeFilter = normalizeFilter;
|
|
34
|
+
/**
|
|
35
|
+
* Represents a TQL filter being constructed.
|
|
36
|
+
*/
|
|
37
|
+
class TQLFilter {
|
|
38
|
+
/**
|
|
39
|
+
* Creates a new TQL filter starting with the given attribute.
|
|
40
|
+
* @param attribute - The attribute to filter on (e.g., 'scheduling_request.status')
|
|
41
|
+
* @param entityPrefix - Optional prefix for this entity type (e.g., 'scheduling_request')
|
|
42
|
+
*/
|
|
43
|
+
constructor(attribute, entityPrefix) {
|
|
44
|
+
this.predicates = [];
|
|
45
|
+
this.logicalOperators = [];
|
|
46
|
+
this.entityPrefix = null;
|
|
47
|
+
this.currentOperator = null;
|
|
48
|
+
this.currentValue = null;
|
|
49
|
+
this.currentAttribute = attribute;
|
|
50
|
+
this.entityPrefix = entityPrefix || null;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Adds a completed predicate and starts a new one.
|
|
54
|
+
*/
|
|
55
|
+
finalizePredicate() {
|
|
56
|
+
if (this.currentAttribute && this.currentOperator !== null && this.currentValue !== null) {
|
|
57
|
+
this.predicates.push({
|
|
58
|
+
attribute: this.currentAttribute,
|
|
59
|
+
operator: this.currentOperator,
|
|
60
|
+
value: this.currentValue,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
this.currentAttribute = '';
|
|
64
|
+
this.currentOperator = null;
|
|
65
|
+
this.currentValue = null;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Sets the operator and value for comparison operations.
|
|
69
|
+
*/
|
|
70
|
+
setComparison(operator, value) {
|
|
71
|
+
if (!this.currentAttribute) {
|
|
72
|
+
throw new Error('Must call filter() or and() or or() before using comparison operators');
|
|
73
|
+
}
|
|
74
|
+
this.currentOperator = operator;
|
|
75
|
+
this.currentValue = value;
|
|
76
|
+
this.finalizePredicate();
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
// String comparison operators
|
|
80
|
+
/**
|
|
81
|
+
* Equals operator (EQ)
|
|
82
|
+
* @param value - The value to compare against
|
|
83
|
+
*/
|
|
84
|
+
eq(value) {
|
|
85
|
+
return this.setComparison('EQ', value);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Not equals operator (NOT_EQ)
|
|
89
|
+
* @param value - The value to compare against
|
|
90
|
+
*/
|
|
91
|
+
notEq(value) {
|
|
92
|
+
return this.setComparison('NOT_EQ', value);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Like operator (contains pattern matching)
|
|
96
|
+
* @param value - The pattern to match against
|
|
97
|
+
*/
|
|
98
|
+
like(value) {
|
|
99
|
+
return this.setComparison('LIKE', value);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Not like operator (does not contain pattern)
|
|
103
|
+
* @param value - The pattern to exclude
|
|
104
|
+
*/
|
|
105
|
+
notLike(value) {
|
|
106
|
+
return this.setComparison('NOT_LIKE', value);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* In operator (value is in the provided array)
|
|
110
|
+
* @param values - Array of values to match against
|
|
111
|
+
*/
|
|
112
|
+
in(values) {
|
|
113
|
+
if (!Array.isArray(values) || values.length === 0) {
|
|
114
|
+
throw new Error('IN operator requires a non-empty array');
|
|
115
|
+
}
|
|
116
|
+
return this.setComparison('IN', values);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Not in operator (value is not in the provided array)
|
|
120
|
+
* @param values - Array of values to exclude
|
|
121
|
+
*/
|
|
122
|
+
notIn(values) {
|
|
123
|
+
if (!Array.isArray(values) || values.length === 0) {
|
|
124
|
+
throw new Error('NOT_IN operator requires a non-empty array');
|
|
125
|
+
}
|
|
126
|
+
return this.setComparison('NOT_IN', values);
|
|
127
|
+
}
|
|
128
|
+
// Numeric/timestamp comparison operators
|
|
129
|
+
/**
|
|
130
|
+
* Greater than operator (GT)
|
|
131
|
+
* @param value - The value to compare against
|
|
132
|
+
*/
|
|
133
|
+
gt(value) {
|
|
134
|
+
return this.setComparison('GT', value);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Greater than or equal operator (GTE)
|
|
138
|
+
* @param value - The value to compare against
|
|
139
|
+
*/
|
|
140
|
+
gte(value) {
|
|
141
|
+
return this.setComparison('GTE', value);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Less than operator (LT)
|
|
145
|
+
* @param value - The value to compare against
|
|
146
|
+
*/
|
|
147
|
+
lt(value) {
|
|
148
|
+
return this.setComparison('LT', value);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Less than or equal operator (LTE)
|
|
152
|
+
* @param value - The value to compare against
|
|
153
|
+
*/
|
|
154
|
+
lte(value) {
|
|
155
|
+
return this.setComparison('LTE', value);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Adds an AND logical operator and starts a new predicate with the given attribute.
|
|
159
|
+
* If this filter was created with an entity prefix and you pass just a property name,
|
|
160
|
+
* it will automatically prepend the entity prefix.
|
|
161
|
+
* @param attribute - The attribute for the next predicate (full path or property name if using entity prefix)
|
|
162
|
+
*/
|
|
163
|
+
and(attribute) {
|
|
164
|
+
if (this.predicates.length === 0) {
|
|
165
|
+
throw new Error('Must have at least one predicate before using AND');
|
|
166
|
+
}
|
|
167
|
+
this.logicalOperators.push('AND');
|
|
168
|
+
// If we have an entity prefix and the attribute doesn't contain a dot, prepend the prefix
|
|
169
|
+
if (this.entityPrefix && !attribute.includes('.')) {
|
|
170
|
+
this.currentAttribute = `${this.entityPrefix}.${attribute}`;
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
this.currentAttribute = attribute;
|
|
174
|
+
}
|
|
175
|
+
return this;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Adds an OR logical operator and starts a new predicate with the given attribute.
|
|
179
|
+
* If this filter was created with an entity prefix and you pass just a property name,
|
|
180
|
+
* it will automatically prepend the entity prefix.
|
|
181
|
+
* @param attribute - The attribute for the next predicate (full path or property name if using entity prefix)
|
|
182
|
+
*/
|
|
183
|
+
or(attribute) {
|
|
184
|
+
if (this.predicates.length === 0) {
|
|
185
|
+
throw new Error('Must have at least one predicate before using OR');
|
|
186
|
+
}
|
|
187
|
+
this.logicalOperators.push('OR');
|
|
188
|
+
// If we have an entity prefix and the attribute doesn't contain a dot, prepend the prefix
|
|
189
|
+
if (this.entityPrefix && !attribute.includes('.')) {
|
|
190
|
+
this.currentAttribute = `${this.entityPrefix}.${attribute}`;
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
this.currentAttribute = attribute;
|
|
194
|
+
}
|
|
195
|
+
return this;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Formats a value for TQL output.
|
|
199
|
+
* Handles arrays, strings with special characters, and numbers.
|
|
200
|
+
*/
|
|
201
|
+
formatValue(value) {
|
|
202
|
+
if (Array.isArray(value)) {
|
|
203
|
+
// Arrays are comma-separated (no spaces) according to TQL spec
|
|
204
|
+
return value.map(v => this.formatSingleValue(v)).join(',');
|
|
205
|
+
}
|
|
206
|
+
return this.formatSingleValue(value);
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Formats a single value for TQL output.
|
|
210
|
+
*/
|
|
211
|
+
formatSingleValue(value) {
|
|
212
|
+
if (typeof value === 'number') {
|
|
213
|
+
return String(value);
|
|
214
|
+
}
|
|
215
|
+
// Strings containing spaces, commas, or tildes should be enclosed in quotes
|
|
216
|
+
if (/[\s,~"]/.test(value) || value === '') {
|
|
217
|
+
// Escape quotes in the value
|
|
218
|
+
const escaped = value.replace(/"/g, '\\"');
|
|
219
|
+
return `"${escaped}"`;
|
|
220
|
+
}
|
|
221
|
+
return value;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Replaces spaces with tildes outside of quoted strings.
|
|
225
|
+
* This is needed for URL encoding while preserving spaces inside quoted values.
|
|
226
|
+
* Handles escaped quotes properly (e.g., "value with \"quotes\"").
|
|
227
|
+
* @param str - The string to process
|
|
228
|
+
* @returns The string with spaces outside quotes replaced by tildes
|
|
229
|
+
*/
|
|
230
|
+
replaceSpacesOutsideQuotes(str) {
|
|
231
|
+
let result = '';
|
|
232
|
+
let insideQuotes = false;
|
|
233
|
+
let i = 0;
|
|
234
|
+
while (i < str.length) {
|
|
235
|
+
const char = str[i];
|
|
236
|
+
if (char === '\\' && insideQuotes && i + 1 < str.length) {
|
|
237
|
+
// Handle escaped characters (like \")
|
|
238
|
+
result += char + str[i + 1];
|
|
239
|
+
i += 2;
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
if (char === '"') {
|
|
243
|
+
// Toggle quote state
|
|
244
|
+
insideQuotes = !insideQuotes;
|
|
245
|
+
result += char;
|
|
246
|
+
}
|
|
247
|
+
else if (char === ' ' || char === '\t' || char === '\n') {
|
|
248
|
+
// Only replace whitespace if we're NOT inside quotes
|
|
249
|
+
result += insideQuotes ? char : '~';
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
result += char;
|
|
253
|
+
}
|
|
254
|
+
i++;
|
|
255
|
+
}
|
|
256
|
+
return result;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Converts the filter to a TQL string suitable for API requests.
|
|
260
|
+
* In URLs, spaces are replaced with tildes (~).
|
|
261
|
+
* @param urlEncode - If true, replaces spaces with ~ for URL encoding (default: true)
|
|
262
|
+
* @returns The TQL filter string
|
|
263
|
+
*/
|
|
264
|
+
toString(urlEncode = true) {
|
|
265
|
+
// Finalize any pending predicate
|
|
266
|
+
if (this.currentAttribute && this.currentOperator !== null && this.currentValue !== null) {
|
|
267
|
+
this.finalizePredicate();
|
|
268
|
+
}
|
|
269
|
+
if (this.predicates.length === 0) {
|
|
270
|
+
throw new Error('Filter must have at least one predicate');
|
|
271
|
+
}
|
|
272
|
+
// Build the filter string
|
|
273
|
+
// Format: predicate1 [operator] predicate2 [operator] predicate3 ...
|
|
274
|
+
const parts = [];
|
|
275
|
+
for (let i = 0; i < this.predicates.length; i++) {
|
|
276
|
+
const predicate = this.predicates[i];
|
|
277
|
+
const predicateStr = `${predicate.attribute} ${predicate.operator} ${this.formatValue(predicate.value)}`;
|
|
278
|
+
parts.push(predicateStr);
|
|
279
|
+
// Add logical operator AFTER this predicate if it's not the last one
|
|
280
|
+
// logicalOperators[i] contains the operator between predicates[i] and predicates[i+1]
|
|
281
|
+
if (i < this.predicates.length - 1 && this.logicalOperators[i]) {
|
|
282
|
+
parts.push(this.logicalOperators[i]);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
let result = parts.join(' ');
|
|
286
|
+
// Replace spaces with ~ for URL encoding if requested
|
|
287
|
+
// Only replace spaces outside of quoted strings to preserve spaces in values
|
|
288
|
+
if (urlEncode) {
|
|
289
|
+
result = this.replaceSpacesOutsideQuotes(result);
|
|
290
|
+
}
|
|
291
|
+
return result;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Returns the filter as a plain string with spaces (not URL encoded).
|
|
295
|
+
* Useful for debugging or when you need the human-readable format.
|
|
296
|
+
*/
|
|
297
|
+
toHumanReadableString() {
|
|
298
|
+
return this.toString(false);
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Returns the string representation when converted to a primitive.
|
|
302
|
+
* This allows TQLFilter to be used directly without calling toString().
|
|
303
|
+
*/
|
|
304
|
+
valueOf() {
|
|
305
|
+
return this.toString();
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Custom primitive conversion for string contexts.
|
|
309
|
+
* Allows TQLFilter to be used directly in places expecting strings.
|
|
310
|
+
*/
|
|
311
|
+
[Symbol.toPrimitive](hint) {
|
|
312
|
+
return this.toString();
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
exports.TQLFilter = TQLFilter;
|
|
316
|
+
/**
|
|
317
|
+
* Normalizes a filter value to a string.
|
|
318
|
+
* Converts TQLFilter instances to strings, leaves strings as-is, and returns null for null.
|
|
319
|
+
* @param filter - The filter value (TQLFilter, string, or null)
|
|
320
|
+
* @returns The normalized filter string or null
|
|
321
|
+
*/
|
|
322
|
+
function normalizeFilter(filter) {
|
|
323
|
+
if (filter === null) {
|
|
324
|
+
return null;
|
|
325
|
+
}
|
|
326
|
+
if (typeof filter === 'string') {
|
|
327
|
+
return filter;
|
|
328
|
+
}
|
|
329
|
+
// filter is a TQLFilter instance
|
|
330
|
+
return filter.toString();
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Helper class for building type-safe filters for specific entity types.
|
|
334
|
+
* Provides autocomplete and type checking for entity properties.
|
|
335
|
+
*/
|
|
336
|
+
class TypedTQLFilterBuilder {
|
|
337
|
+
constructor(prefix) {
|
|
338
|
+
this.prefix = prefix;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Starts a filter with a type-safe attribute from the entity.
|
|
342
|
+
* @param attribute - A valid property key from the entity type
|
|
343
|
+
* @returns A TQLFilter instance for method chaining (with entity prefix stored)
|
|
344
|
+
*/
|
|
345
|
+
filter(attribute) {
|
|
346
|
+
const fullPath = `${this.prefix}.${String(attribute)}`;
|
|
347
|
+
return new TQLFilter(fullPath, this.prefix);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* TQL Filter Builder
|
|
352
|
+
* Provides a user-friendly, fluent API for constructing TimeZest Query Language filters.
|
|
353
|
+
*/
|
|
354
|
+
class TQL {
|
|
355
|
+
/**
|
|
356
|
+
* Starts building a TQL filter with the given attribute.
|
|
357
|
+
* Use this for flexibility - no type checking on the attribute.
|
|
358
|
+
* For type-safe filtering, use the endpoint-specific helpers like `TQL.forAgents()`.
|
|
359
|
+
*
|
|
360
|
+
* @param attribute - The attribute to filter on (e.g., 'scheduling_request.status')
|
|
361
|
+
* @returns A TQLFilter instance for method chaining
|
|
362
|
+
*
|
|
363
|
+
* @example
|
|
364
|
+
* ```typescript
|
|
365
|
+
* TQL.filter('scheduling_request.status').eq('scheduled')
|
|
366
|
+
* ```
|
|
367
|
+
*/
|
|
368
|
+
static filter(attribute) {
|
|
369
|
+
return new TQLFilter(attribute);
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Creates a type-safe filter builder for Agent entities.
|
|
373
|
+
* Provides autocomplete and type checking for Agent attributes.
|
|
374
|
+
*
|
|
375
|
+
* @example
|
|
376
|
+
* ```typescript
|
|
377
|
+
* TQL.forAgents().filter('name').like('John')
|
|
378
|
+
* TQL.forAgents().filter('email').eq('user@example.com')
|
|
379
|
+
* ```
|
|
380
|
+
*/
|
|
381
|
+
static forAgents() {
|
|
382
|
+
return new TypedTQLFilterBuilder('agent');
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Creates a type-safe filter builder for Resource entities.
|
|
386
|
+
* Provides autocomplete and type checking for Resource attributes.
|
|
387
|
+
*
|
|
388
|
+
* @example
|
|
389
|
+
* ```typescript
|
|
390
|
+
* TQL.forResources().filter('name').like('Room')
|
|
391
|
+
* TQL.forResources().filter('schedulable').eq(true)
|
|
392
|
+
* ```
|
|
393
|
+
*/
|
|
394
|
+
static forResources() {
|
|
395
|
+
return new TypedTQLFilterBuilder('resource');
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Creates a type-safe filter builder for Team entities.
|
|
399
|
+
* Provides autocomplete and type checking for Team attributes.
|
|
400
|
+
*
|
|
401
|
+
* @example
|
|
402
|
+
* ```typescript
|
|
403
|
+
* TQL.forTeams().filter('internal_name').eq('Tier1')
|
|
404
|
+
* TQL.forTeams().filter('team_type').eq('support')
|
|
405
|
+
* ```
|
|
406
|
+
*/
|
|
407
|
+
static forTeams() {
|
|
408
|
+
return new TypedTQLFilterBuilder('team');
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Creates a type-safe filter builder for AppointmentType entities.
|
|
412
|
+
* Provides autocomplete and type checking for AppointmentType attributes.
|
|
413
|
+
*
|
|
414
|
+
* @example
|
|
415
|
+
* ```typescript
|
|
416
|
+
* TQL.forAppointmentTypes().filter('internal_name').eq('consultation')
|
|
417
|
+
* TQL.forAppointmentTypes().filter('duration_mins').gte(30)
|
|
418
|
+
* ```
|
|
419
|
+
*/
|
|
420
|
+
static forAppointmentTypes() {
|
|
421
|
+
return new TypedTQLFilterBuilder('appointment_type');
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Creates a type-safe filter builder for SchedulingRequest entities.
|
|
425
|
+
* Provides autocomplete and type checking for SchedulingRequest attributes.
|
|
426
|
+
*
|
|
427
|
+
* @example
|
|
428
|
+
* ```typescript
|
|
429
|
+
* TQL.forSchedulingRequests().filter('status').eq('scheduled')
|
|
430
|
+
* TQL.forSchedulingRequests().filter('end_user_email').like('@example.com')
|
|
431
|
+
* ```
|
|
432
|
+
*/
|
|
433
|
+
static forSchedulingRequests() {
|
|
434
|
+
return new TypedTQLFilterBuilder('scheduling_request');
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
exports.TQL = TQL;
|
|
438
|
+
exports.default = TQL;
|
|
439
|
+
//# sourceMappingURL=tqlFilter.js.map
|