@revolugo/common 7.0.1 → 7.1.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.
Files changed (35) hide show
  1. package/package.json +2 -2
  2. package/src/cancellation-policies.test.ts +8 -2
  3. package/src/cancellation-policies.ts +57 -4
  4. package/src/currencies/types.ts +1 -1
  5. package/src/currencies/utils.ts +52 -52
  6. package/src/schemas/hotel-room-offer.ts +18 -13
  7. package/src/schemas/hotel-room.ts +1 -4
  8. package/src/types/calendar.ts +2 -2
  9. package/src/types/elements/amenity.ts +10 -10
  10. package/src/types/elements/booking-policy.ts +7 -7
  11. package/src/types/elements/booking.ts +15 -15
  12. package/src/types/elements/contact-person.ts +7 -0
  13. package/src/types/elements/elements-events.ts +2 -0
  14. package/src/types/elements/event-metadata.ts +6 -6
  15. package/src/types/elements/hotel-offer-list.ts +1 -1
  16. package/src/types/elements/hotel-offer-request.ts +21 -21
  17. package/src/types/elements/hotel-offer.ts +15 -15
  18. package/src/types/elements/hotel-offers-filters.ts +1 -1
  19. package/src/types/elements/hotel-room-offer-request.ts +22 -22
  20. package/src/types/elements/hotel-room-offer.ts +15 -15
  21. package/src/types/elements/hotel-room.ts +6 -6
  22. package/src/types/elements/hotel-rooming-list.ts +8 -8
  23. package/src/types/elements/hotel.ts +6 -6
  24. package/src/types/elements/meta-polling.ts +6 -6
  25. package/src/types/event.ts +1 -1
  26. package/src/types/hotel-contract.ts +1 -1
  27. package/src/types/hotel-room-stock.ts +1 -1
  28. package/src/types/pagination.ts +2 -2
  29. package/src/utils/create-composite-key.ts +2 -2
  30. package/src/utils/debounce.ts +1 -1
  31. package/src/utils/generate-dummy-hotel-images.ts +7 -5
  32. package/src/utils/get-sanitized-room-count.ts +1 -1
  33. package/src/utils/images.ts +5 -0
  34. package/src/utils/parse-children.ts +1 -1
  35. package/src/utils/poller.ts +56 -55
@@ -45,13 +45,14 @@ export class Poller<V extends IPollerResponse> {
45
45
  minCallCount: 1,
46
46
  }
47
47
 
48
- private pollings: Record<string, string> = {}
49
48
  private events: TPollerEvents<V> = {
50
49
  complete: () => undefined,
51
50
  data: () => undefined,
52
51
  error: () => undefined,
53
52
  }
54
53
 
54
+ private pollings: Record<string, string> = {}
55
+
55
56
  static getInstance<R extends IPollerResponse>(): Poller<R> {
56
57
  if (!Poller.instance) {
57
58
  Poller.instance = new Poller<R>() as Poller<IPollerResponse>
@@ -60,6 +61,13 @@ export class Poller<V extends IPollerResponse> {
60
61
  return Poller.instance as Poller<R>
61
62
  }
62
63
 
64
+ on(
65
+ eventName: TPollerEventName,
66
+ eventCallback: TPollerEventCallback<V>,
67
+ ): void {
68
+ this.events[eventName] = eventCallback
69
+ }
70
+
63
71
  poll(request: TRequest<V>, options: TOptions<V>): Poller<V> {
64
72
  const buildedOptions = this.buildPollerOptions(options)
65
73
  this.storeCurrentPolling(buildedOptions)
@@ -75,13 +83,6 @@ export class Poller<V extends IPollerResponse> {
75
83
  return this
76
84
  }
77
85
 
78
- on(
79
- eventName: TPollerEventName,
80
- eventCallback: TPollerEventCallback<V>,
81
- ): void {
82
- this.events[eventName] = eventCallback
83
- }
84
-
85
86
  public stop(type: string): void {
86
87
  if (type && this.pollings[type]) {
87
88
  // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
@@ -89,6 +90,25 @@ export class Poller<V extends IPollerResponse> {
89
90
  }
90
91
  }
91
92
 
93
+ private buildPollerOptions(options: TOptions<V>): TOptions<V> {
94
+ const compactedOptions = Object.entries(options).reduce<
95
+ Record<string, unknown>
96
+ >((acc, [key, value]) => {
97
+ if (value !== undefined) {
98
+ const tKey = key as keyof TOptions<V>
99
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
100
+ acc[tKey] = value as any
101
+ }
102
+
103
+ return acc
104
+ }, {}) as TOptions<V>
105
+
106
+ return {
107
+ ...this.defaultOptions,
108
+ ...compactedOptions,
109
+ }
110
+ }
111
+
92
112
  private dispatch(
93
113
  eventName: TPollerEventName,
94
114
  payload?: TPollerEventCallbackArg<V>,
@@ -96,6 +116,27 @@ export class Poller<V extends IPollerResponse> {
96
116
  this.events[eventName](payload)
97
117
  }
98
118
 
119
+ private isActivePoller(options: TOptions<V>): boolean {
120
+ if (options.type) {
121
+ return (
122
+ !!this.pollings[options.type] &&
123
+ this.pollings[options.type] === options.uuid
124
+ )
125
+ }
126
+
127
+ return true
128
+ }
129
+
130
+ private isInProgress(result: V): boolean {
131
+ return (result?.meta?.status ?? '') !== 'COMPLETE'
132
+ }
133
+
134
+ private onComplete(result: V, options: TOptions<V>) {
135
+ this.dispatch('data', result)
136
+ this.dispatch('complete', result)
137
+ this.removeCurrentPolling(options)
138
+ }
139
+
99
140
  private async onRequest(
100
141
  result: V,
101
142
  request: TRequest<V>,
@@ -148,53 +189,6 @@ export class Poller<V extends IPollerResponse> {
148
189
  }
149
190
  }
150
191
 
151
- private onComplete(result: V, options: TOptions<V>) {
152
- this.dispatch('data', result)
153
- this.dispatch('complete', result)
154
- this.removeCurrentPolling(options)
155
- }
156
-
157
- private isInProgress(result: V): boolean {
158
- return (result?.meta?.status ?? '') !== 'COMPLETE'
159
- }
160
-
161
- private buildPollerOptions(options: TOptions<V>): TOptions<V> {
162
- const compactedOptions = Object.entries(options).reduce<
163
- Record<string, unknown>
164
- >((acc, [key, value]) => {
165
- if (value !== undefined) {
166
- const tKey = key as keyof TOptions<V>
167
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
168
- acc[tKey] = value as any
169
- }
170
-
171
- return acc
172
- }, {}) as TOptions<V>
173
-
174
- return {
175
- ...this.defaultOptions,
176
- ...compactedOptions,
177
- }
178
- }
179
-
180
- private storeCurrentPolling(options: TOptions<V>): void {
181
- if (options.type && !options.uuid) {
182
- options.uuid = uuidv4()
183
- this.pollings[options.type] = options.uuid
184
- }
185
- }
186
-
187
- private isActivePoller(options: TOptions<V>): boolean {
188
- if (options.type) {
189
- return (
190
- !!this.pollings[options.type] &&
191
- this.pollings[options.type] === options.uuid
192
- )
193
- }
194
-
195
- return true
196
- }
197
-
198
192
  private pause(options: TOptions<V>): Promise<void> {
199
193
  return new Promise(resolve => {
200
194
  setTimeout(resolve, options.interval)
@@ -207,6 +201,13 @@ export class Poller<V extends IPollerResponse> {
207
201
  delete this.pollings[options.type]
208
202
  }
209
203
  }
204
+
205
+ private storeCurrentPolling(options: TOptions<V>): void {
206
+ if (options.type && !options.uuid) {
207
+ options.uuid = uuidv4()
208
+ this.pollings[options.type] = options.uuid
209
+ }
210
+ }
210
211
  }
211
212
 
212
213
  export type PollerReturn<R extends IPollerResponse = IPollerResponse> =