terrier-engine 4.14.0 → 4.14.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/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "files": [
5
5
  "*"
6
6
  ],
7
- "version": "4.14.0",
7
+ "version": "4.14.1",
8
8
  "repository": {
9
9
  "type": "git",
10
10
  "url": "https://github.com/Terrier-Tech/terrier-engine"
@@ -11,8 +11,14 @@ export type SubscriptionOptions = {
11
11
  keepAlive?: boolean
12
12
  }
13
13
 
14
+ /** Options specific to Subscribers that use GET HTTP requests */
15
+ export type GetSubscriberOptions = {
16
+ /** If true, keys in the params object will be snakified before being sent to the server */
17
+ snakifyKeys?: boolean
18
+ }
19
+
14
20
  /** Parameters to send with the subscription request */
15
- export type SubscriptionParams = Record<string, string>
21
+ export type SubscriptionParams = Record<string, unknown>
16
22
 
17
23
  export type SubscriptionEventHandlers<TResult> = {
18
24
  onResult: resultListener<TResult>[]
@@ -206,7 +212,7 @@ export class PollingSubscriber<TResult, TParams extends SubscriptionParams> exte
206
212
  public url: string,
207
213
  public params: TParams,
208
214
  public interval: number | duration.Duration,
209
- public options: SubscriptionOptions | undefined = undefined
215
+ public options: (SubscriptionOptions & GetSubscriberOptions) | undefined = undefined
210
216
  ) {
211
217
  super()
212
218
  }
@@ -247,7 +253,8 @@ export class PollingSubscriber<TResult, TParams extends SubscriptionParams> exte
247
253
  }
248
254
 
249
255
  protected request(): Promise<((ApiResponse & SubscriptionEvent<TResult>) | { events: SubscriptionEvent<TResult>[] })> {
250
- return Api.get<((ApiResponse & SubscriptionEvent<TResult>) | { events: SubscriptionEvent<TResult>[] })>(this.url, this.params)
256
+ const params = Api.objectToQueryParams(this.params, this.options?.snakifyKeys)
257
+ return Api.get<((ApiResponse & SubscriptionEvent<TResult>) | { events: SubscriptionEvent<TResult>[] })>(this.url, params)
251
258
  }
252
259
 
253
260
  protected handleEvent(event: SubscriptionEvent<TResult>): boolean {
@@ -341,7 +348,7 @@ export class StreamingSubscriber<TResult, TParams extends SubscriptionParams> ex
341
348
  constructor(
342
349
  public url: string,
343
350
  public params: TParams,
344
- public options: SubscriptionOptions | undefined = undefined,
351
+ public options: (SubscriptionOptions & GetSubscriberOptions) | undefined = undefined,
345
352
  ) {
346
353
  super()
347
354
  }
@@ -367,8 +374,9 @@ export class StreamingSubscriber<TResult, TParams extends SubscriptionParams> ex
367
374
  }
368
375
 
369
376
  protected subscribeImpl(): void {
370
- const paramsString = new URLSearchParams(this.params).toString()
371
- const streamer = new Streamer(`${this.url}?${paramsString}`, this.options ?? {})
377
+ const params = Api.objectToQueryParams(this.params, this.options?.snakifyKeys)
378
+ const fullUrl = new URLSearchParams(params).toString()
379
+ const streamer = new Streamer(fullUrl, this.options ?? {})
372
380
 
373
381
  this.eventHandlers.onResult.forEach(handler => streamer.on<ResultEvent<TResult>>('_result', this.wrapHandler(handler)))
374
382
  this.eventHandlers.onError.forEach(handler => streamer.on<ErrorEvent>('_error', this.wrapHandler(handler)))
package/terrier/api.ts CHANGED
@@ -2,6 +2,8 @@ import {Logger} from "tuff-core/logging"
2
2
  import {QueryParams} from "tuff-core/urls"
3
3
  import {LogEntry} from "./logging"
4
4
  import {ErrorEvent} from "./api-subscriber"
5
+ import Strings from "tuff-core/strings";
6
+ import dayjs from "dayjs";
5
7
 
6
8
  const log = new Logger('Api')
7
9
 
@@ -147,6 +149,28 @@ async function post<ResponseType>(url: string, body: Record<string, unknown> | F
147
149
  return await request<ResponseType & ApiResponse>(url, config)
148
150
  }
149
151
 
152
+ ////////////////////////////////////////////////////////////////////////////////
153
+ // Query Params
154
+ ////////////////////////////////////////////////////////////////////////////////
155
+
156
+ /**
157
+ * Transforms the given object to a QueryParams compatible object.
158
+ */
159
+ function objectToQueryParams(object: Record<string, unknown>, snakifyKeys: boolean = false): Record<string, string> {
160
+ const raw: Record<string, string> = {}
161
+ for (const [key, value] of Object.entries(object)) {
162
+ const paramKey = snakifyKeys ? Strings.splitWords(key).map(w => w.toLowerCase()).join("_") : key
163
+
164
+ if (dayjs.isDayjs(value)) {
165
+ raw[paramKey] = value.format()
166
+ } else if (value == undefined) {
167
+ raw[paramKey] = ""
168
+ } else {
169
+ raw[paramKey] = value?.toString()
170
+ }
171
+ }
172
+ return raw
173
+ }
150
174
 
151
175
  ////////////////////////////////////////////////////////////////////////////////
152
176
  // Event Streams
@@ -248,5 +272,6 @@ const Api = {
248
272
  post,
249
273
  stream,
250
274
  addRequestDecorator,
275
+ objectToQueryParams,
251
276
  }
252
277
  export default Api