@odynn/awayz-flights 0.2.8 → 0.2.9

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.
@@ -1,77 +1,78 @@
1
- import { useState as N, useRef as K } from "react";
2
- import { useMutation as L } from "@tanstack/react-query";
3
- import { ECabinClass as Q, EFlightType as J, EFlightPaymentType as A } from "../../types/enums.js";
4
- import { FlightService as k } from "../../services/flights/FlightsService.js";
5
- import { mapFlightDetailsToResults as V, groupFlightResults as G, buildFlightSearchQuery as X } from "../../utils/flightUtils.js";
1
+ import { useState as D, useRef as J } from "react";
2
+ import { useMutation as M } from "@tanstack/react-query";
3
+ import { ECabinClass as Q, EFlightType as $, EFlightPaymentType as N } from "../../types/enums.js";
4
+ import { FlightService as A } from "../../services/flights/FlightsService.js";
5
+ import { mapFlightDetailsToResults as X, groupFlightResults as Z, buildFlightSearchQuery as ee } from "../../utils/flightUtils.js";
6
6
  import "../../arrayExtensions-DWKenJCh.js";
7
- import { c as W } from "../../objectUtils-CyIaVmog.js";
8
- import Z from "../../stores/useFlightStore.js";
9
- import { useAwayzContext as ee, getBaseUrl as te } from "@odynn/awayz-core";
10
- async function ne(o, t) {
11
- const r = o.getReader();
12
- let n;
13
- for (; !(n = await r.read()).done; )
14
- t(n.value);
7
+ import { c as K } from "../../objectUtils-CyIaVmog.js";
8
+ import te from "../../stores/useFlightStore.js";
9
+ import { useAwayzContext as ne, awayzClient as q, getBaseUrl as re } from "@odynn/awayz-core";
10
+ var I = /* @__PURE__ */ ((n) => (n.CHECK_USER = "CHECK_USER", n.USER_REGIONS = "USER_REGIONS", n.CONVERT_CURRENCY = "CONVERT_CURRENCY", n.CLIENT_COMPONENT_KEY = "CLIENT_COMPONENT_KEY", n.SEARCH_LIMIT = "SEARCH_LIMIT", n))(I || {});
11
+ async function oe(n, t) {
12
+ const o = n.getReader();
13
+ let r;
14
+ for (; !(r = await o.read()).done; )
15
+ t(r.value);
15
16
  }
16
- function re(o) {
17
- let t, r, n, s = !1;
17
+ function se(n) {
18
+ let t, o, r, s = !1;
18
19
  return function(c) {
19
- t === void 0 ? (t = c, r = 0, n = -1) : t = se(t, c);
20
- const i = t.length;
20
+ t === void 0 ? (t = c, o = 0, r = -1) : t = ie(t, c);
21
+ const u = t.length;
21
22
  let a = 0;
22
- for (; r < i; ) {
23
- s && (t[r] === 10 && (a = ++r), s = !1);
23
+ for (; o < u; ) {
24
+ s && (t[o] === 10 && (a = ++o), s = !1);
24
25
  let l = -1;
25
- for (; r < i && l === -1; ++r)
26
- switch (t[r]) {
26
+ for (; o < u && l === -1; ++o)
27
+ switch (t[o]) {
27
28
  case 58:
28
- n === -1 && (n = r - a);
29
+ r === -1 && (r = o - a);
29
30
  break;
30
31
  case 13:
31
32
  s = !0;
32
33
  case 10:
33
- l = r;
34
+ l = o;
34
35
  break;
35
36
  }
36
37
  if (l === -1)
37
38
  break;
38
- o(t.subarray(a, l), n), a = r, n = -1;
39
+ n(t.subarray(a, l), r), a = o, r = -1;
39
40
  }
40
- a === i ? t = void 0 : a !== 0 && (t = t.subarray(a), r -= a);
41
+ a === u ? t = void 0 : a !== 0 && (t = t.subarray(a), o -= a);
41
42
  };
42
43
  }
43
- function oe(o, t, r) {
44
- let n = U();
44
+ function ae(n, t, o) {
45
+ let r = W();
45
46
  const s = new TextDecoder();
46
- return function(c, i) {
47
+ return function(c, u) {
47
48
  if (c.length === 0)
48
- r == null || r(n), n = U();
49
- else if (i > 0) {
50
- const a = s.decode(c.subarray(0, i)), l = i + (c[i + 1] === 32 ? 2 : 1), b = s.decode(c.subarray(l));
49
+ o == null || o(r), r = W();
50
+ else if (u > 0) {
51
+ const a = s.decode(c.subarray(0, u)), l = u + (c[u + 1] === 32 ? 2 : 1), b = s.decode(c.subarray(l));
51
52
  switch (a) {
52
53
  case "data":
53
- n.data = n.data ? n.data + `
54
+ r.data = r.data ? r.data + `
54
55
  ` + b : b;
55
56
  break;
56
57
  case "event":
57
- n.event = b;
58
+ r.event = b;
58
59
  break;
59
60
  case "id":
60
- o(n.id = b);
61
+ n(r.id = b);
61
62
  break;
62
63
  case "retry":
63
64
  const g = parseInt(b, 10);
64
- isNaN(g) || t(n.retry = g);
65
+ isNaN(g) || t(r.retry = g);
65
66
  break;
66
67
  }
67
68
  }
68
69
  };
69
70
  }
70
- function se(o, t) {
71
- const r = new Uint8Array(o.length + t.length);
72
- return r.set(o), r.set(t, o.length), r;
71
+ function ie(n, t) {
72
+ const o = new Uint8Array(n.length + t.length);
73
+ return o.set(n), o.set(t, n.length), o;
73
74
  }
74
- function U() {
75
+ function W() {
75
76
  return {
76
77
  data: "",
77
78
  event: "",
@@ -79,166 +80,172 @@ function U() {
79
80
  retry: void 0
80
81
  };
81
82
  }
82
- var ae = function(o, t) {
83
- var r = {};
84
- for (var n in o) Object.prototype.hasOwnProperty.call(o, n) && t.indexOf(n) < 0 && (r[n] = o[n]);
85
- if (o != null && typeof Object.getOwnPropertySymbols == "function")
86
- for (var s = 0, n = Object.getOwnPropertySymbols(o); s < n.length; s++)
87
- t.indexOf(n[s]) < 0 && Object.prototype.propertyIsEnumerable.call(o, n[s]) && (r[n[s]] = o[n[s]]);
88
- return r;
83
+ var ue = function(n, t) {
84
+ var o = {};
85
+ for (var r in n) Object.prototype.hasOwnProperty.call(n, r) && t.indexOf(r) < 0 && (o[r] = n[r]);
86
+ if (n != null && typeof Object.getOwnPropertySymbols == "function")
87
+ for (var s = 0, r = Object.getOwnPropertySymbols(n); s < r.length; s++)
88
+ t.indexOf(r[s]) < 0 && Object.prototype.propertyIsEnumerable.call(n, r[s]) && (o[r[s]] = n[r[s]]);
89
+ return o;
89
90
  };
90
- const x = "text/event-stream", ie = 1e3, z = "last-event-id";
91
- function ue(o, t) {
92
- var { signal: r, headers: n, onopen: s, onmessage: v, onclose: c, onerror: i, openWhenHidden: a, fetch: l } = t, b = ae(t, ["signal", "headers", "onopen", "onmessage", "onclose", "onerror", "openWhenHidden", "fetch"]);
93
- return new Promise((g, D) => {
94
- const p = Object.assign({}, n);
95
- p.accept || (p.accept = x);
91
+ const j = "text/event-stream", ce = 1e3, z = "last-event-id";
92
+ function de(n, t) {
93
+ var { signal: o, headers: r, onopen: s, onmessage: w, onclose: c, onerror: u, openWhenHidden: a, fetch: l } = t, b = ue(t, ["signal", "headers", "onopen", "onmessage", "onclose", "onerror", "openWhenHidden", "fetch"]);
94
+ return new Promise((g, L) => {
95
+ const p = Object.assign({}, r);
96
+ p.accept || (p.accept = j);
96
97
  let m;
97
- function w() {
98
+ function E() {
98
99
  m.abort(), document.hidden || T();
99
100
  }
100
- a || document.addEventListener("visibilitychange", w);
101
- let F = ie, E = 0;
101
+ a || document.addEventListener("visibilitychange", E);
102
+ let _ = ce, R = 0;
102
103
  function S() {
103
- document.removeEventListener("visibilitychange", w), window.clearTimeout(E), m.abort();
104
+ document.removeEventListener("visibilitychange", E), window.clearTimeout(R), m.abort();
104
105
  }
105
- r == null || r.addEventListener("abort", () => {
106
+ o == null || o.addEventListener("abort", () => {
106
107
  S(), g();
107
108
  });
108
- const _ = l ?? window.fetch, q = s ?? ce;
109
+ const H = l ?? window.fetch, P = s ?? le;
109
110
  async function T() {
110
- var C;
111
+ var v;
111
112
  m = new AbortController();
112
113
  try {
113
- const O = await _(o, Object.assign(Object.assign({}, b), { headers: p, signal: m.signal }));
114
- await q(O), await ne(O.body, re(oe((h) => {
114
+ const C = await H(n, Object.assign(Object.assign({}, b), { headers: p, signal: m.signal }));
115
+ await P(C), await oe(C.body, se(ae((h) => {
115
116
  h ? p[z] = h : delete p[z];
116
117
  }, (h) => {
117
- F = h;
118
- }, v))), c == null || c(), S(), g();
119
- } catch (O) {
118
+ _ = h;
119
+ }, w))), c == null || c(), S(), g();
120
+ } catch (C) {
120
121
  if (!m.signal.aborted)
121
122
  try {
122
- const h = (C = i == null ? void 0 : i(O)) !== null && C !== void 0 ? C : F;
123
- window.clearTimeout(E), E = window.setTimeout(T, h);
123
+ const h = (v = u == null ? void 0 : u(C)) !== null && v !== void 0 ? v : _;
124
+ window.clearTimeout(R), R = window.setTimeout(T, h);
124
125
  } catch (h) {
125
- S(), D(h);
126
+ S(), L(h);
126
127
  }
127
128
  }
128
129
  }
129
130
  T();
130
131
  });
131
132
  }
132
- function ce(o) {
133
- const t = o.headers.get("content-type");
134
- if (!(t != null && t.startsWith(x)))
135
- throw new Error(`Expected content-type to be ${x}, Actual: ${t}`);
133
+ function le(n) {
134
+ const t = n.headers.get("content-type");
135
+ if (!(t != null && t.startsWith(j)))
136
+ throw new Error(`Expected content-type to be ${j}, Actual: ${t}`);
136
137
  }
137
- const B = 1, Y = Q.ECONOMY;
138
- let j = new AbortController();
139
- const de = 3 * 60 * 1e3, $ = (o) => {
140
- if (!o.origin || !o.destination)
138
+ const V = 1, B = Q.ECONOMY;
139
+ let U = new AbortController();
140
+ const fe = 3 * 60 * 1e3, G = (n) => {
141
+ if (!n.origin || !n.destination)
141
142
  throw new Error("Origin and destination are required.");
142
- if (!o.departureDate)
143
+ if (!n.departureDate)
143
144
  throw new Error("Departure date is required.");
144
- if (o.flightType === J.ROUND_TRIP && !o.returnDate)
145
+ if (n.flightType === $.ROUND_TRIP && !n.returnDate)
145
146
  throw new Error("Return date is required for round-trip flights.");
146
- }, Ee = () => {
147
- const { token: o, config: t } = ee(), [r, n] = N(""), [s, v] = N([]), [c, i] = N(!1), a = K(null), {
147
+ }, Se = () => {
148
+ const { token: n, config: t } = ne(), [o, r] = D(""), [s, w] = D([]), [c, u] = D(!1), a = J(null), {
148
149
  inboundResults: l,
149
150
  outboundResults: b,
150
151
  setInboundResults: g,
151
- setOutboundResults: D,
152
+ setOutboundResults: L,
152
153
  partialOfferRequestId: p,
153
154
  setPartialOfferRequestId: m
154
- } = Z(), w = ({
155
+ } = te(), E = ({
155
156
  data: e,
156
157
  inbound: f
157
158
  }) => {
158
- const d = V(e);
159
- return (f ? g : D)(G(d)), d;
159
+ const d = X(e);
160
+ return (f ? g : L)(Z(d)), d;
160
161
  }, {
161
- mutate: F,
162
- status: E,
162
+ mutate: _,
163
+ status: R,
163
164
  error: S
164
- } = L({
165
+ } = M({
165
166
  mutationFn: async (e) => {
166
- $(e);
167
+ G(e);
167
168
  const d = {
168
169
  passengers: Array.from(
169
- { length: e.passengers ?? B },
170
+ { length: e.passengers ?? V },
170
171
  () => ({ type: "adult" })
171
172
  ),
172
173
  origin: e.origin || "",
173
174
  destination: e.destination || "",
174
- cabin_class: e.cabinClass || Y
175
+ cabin_class: e.cabinClass || B
175
176
  };
176
- if (e.flightType === J.ONE_WAY) {
177
- const u = await k.cashOnlySearch({
177
+ if (e.flightType === $.ONE_WAY) {
178
+ const i = await A.cashOnlySearch({
178
179
  ...d,
179
180
  departure_date: e.departureDate
180
181
  });
181
182
  return {
182
- success: u.success,
183
- data: u.data
183
+ success: i.success,
184
+ data: i.data
184
185
  };
185
186
  } else {
186
- const u = await k.roundTripOutboundSearch({
187
+ const i = await A.roundTripOutboundSearch({
187
188
  ...d,
188
189
  departure_date: e.departureDate,
189
190
  return_date: e.returnDate
190
191
  });
191
192
  return {
192
- success: u.success,
193
- data: u.data.offers,
194
- partialOfferRequestId: u.data.id
193
+ success: i.success,
194
+ data: i.data.offers,
195
+ partialOfferRequestId: i.data.id
195
196
  };
196
197
  }
197
198
  },
198
199
  onSuccess: (e) => {
199
- const { success: f, data: d, partialOfferRequestId: u } = e;
200
+ const { success: f, data: d, partialOfferRequestId: i } = e;
200
201
  if (!f) {
201
202
  console.log("oh lol, it didn't work");
202
203
  return;
203
204
  }
204
- w({ data: d }), u && m(u);
205
+ E({ data: d }), i && m(i);
205
206
  },
206
207
  onError: (e) => {
207
208
  console.error("Error in outbound flight search:", e);
209
+ },
210
+ onSettled: () => {
211
+ q.invalidateQueries({ queryKey: [I.SEARCH_LIMIT] });
208
212
  }
209
213
  }), {
210
- mutate: _,
211
- status: q,
214
+ mutate: H,
215
+ status: P,
212
216
  error: T
213
- } = L({
217
+ } = M({
214
218
  mutationFn: async (e) => {
215
219
  if (!p)
216
220
  throw new Error("No partialOfferRequestId found for inbound search.");
217
- return n(e), await k.roundTripInboundSearch({
221
+ return r(e), await A.roundTripInboundSearch({
218
222
  outboundOfferId: e,
219
223
  outboundOfferRequestId: p
220
224
  });
221
225
  },
222
226
  onSuccess: (e) => {
223
227
  const { success: f, data: d } = e;
224
- f && w({
228
+ f && E({
225
229
  data: d.offers,
226
230
  inbound: !0
227
231
  });
228
232
  },
229
233
  onError: (e) => {
230
234
  console.error("Error in inbound flight search:", e);
235
+ },
236
+ onSettled: () => {
237
+ q.invalidateQueries({ queryKey: [I.SEARCH_LIMIT] });
231
238
  }
232
239
  }), {
233
- mutate: C,
234
- status: O,
240
+ mutate: v,
241
+ status: C,
235
242
  error: h
236
- } = L({
243
+ } = M({
237
244
  mutationFn: async (e) => {
238
245
  if (!p)
239
246
  throw new Error("No partialOfferRequestId found for round-trip fares.");
240
- return await k.getRoundTripFares({
241
- outboundOfferId: r,
247
+ return await A.getRoundTripFares({
248
+ outboundOfferId: o,
242
249
  outboundOfferRequestId: p,
243
250
  inboundOfferId: e
244
251
  });
@@ -246,60 +253,63 @@ const de = 3 * 60 * 1e3, $ = (o) => {
246
253
  onSuccess: (e) => {
247
254
  const { success: f, data: d } = e;
248
255
  if (!f) return;
249
- const u = d.offers;
250
- v(W(u));
256
+ const i = d.offers;
257
+ w(K(i));
251
258
  },
252
259
  onError: (e) => {
253
260
  console.error("Error in round-trip fare search:", e);
261
+ },
262
+ onSettled: () => {
263
+ q.invalidateQueries({ queryKey: [I.SEARCH_LIMIT] });
254
264
  }
255
- }), H = async (e) => {
256
- var M;
257
- i(!0);
258
- const f = e.paymentType === A.CASH_AND_MILES ? ['"cash"', '"points"'] : ['"points"'], d = X({
265
+ }), x = async (e) => {
266
+ var Y;
267
+ u(!0);
268
+ const f = e.paymentType === N.CASH_AND_MILES ? ['"cash"', '"points"'] : ['"points"'], d = ee({
259
269
  originCode: e.origin,
260
270
  destinationCode: e.destination,
261
271
  departureDate: e.departureDate,
262
- cabinClass: e.cabinClass || Y,
263
- passengerCount: e.passengers || B,
272
+ cabinClass: e.cabinClass || B,
273
+ passengerCount: e.passengers || V,
264
274
  paymentType: f
265
275
  });
266
- let u = [], P = 0;
267
- j = new AbortController(), a.current = setTimeout(() => {
268
- R();
269
- }, (((M = t == null ? void 0 : t.flightsConfig) == null ? void 0 : M.streamTimeoutTotalSeconds) || de) * 1e3), ue(te() + d, {
276
+ let i = [], k = 0;
277
+ U = new AbortController(), a.current = setTimeout(() => {
278
+ O();
279
+ }, (((Y = t == null ? void 0 : t.flightsConfig) == null ? void 0 : Y.streamTimeoutTotalSeconds) || fe) * 1e3), de(re() + d, {
270
280
  headers: {
271
281
  "Content-Type": "application/json",
272
- Authorization: o || "",
282
+ Authorization: n || "",
273
283
  "client-id": (t == null ? void 0 : t.clientId) || ""
274
284
  },
275
285
  openWhenHidden: !0,
276
- signal: j.signal,
277
- onmessage(I) {
286
+ signal: U.signal,
287
+ onmessage(F) {
278
288
  try {
279
- const y = W(JSON.parse(I.data));
289
+ const y = K(JSON.parse(F.data));
280
290
  if (y.success === !1)
281
291
  return;
282
292
  if (y.close === !0) {
283
- R();
293
+ O();
284
294
  return;
285
295
  }
286
- y.flights && (P = 0, u = [...u, ...y.flights], w({
287
- data: u
288
- })), y.ping && (P++, P >= t.flightsConfig.streamTimeoutPingCount && R());
296
+ y.flights && (k = 0, i = [...i, ...y.flights], E({
297
+ data: i
298
+ })), y.ping && (k++, k >= t.flightsConfig.streamTimeoutPingCount && O());
289
299
  } catch (y) {
290
- console.error("Error parsing message:", y), R();
300
+ console.error("Error parsing message:", y), O();
291
301
  }
292
302
  },
293
- onerror(I) {
294
- throw console.error("EventSource error:", I), R(), I;
303
+ onerror(F) {
304
+ throw console.error("EventSource error:", F), O(), F;
295
305
  },
296
306
  onclose() {
297
- i(!1);
307
+ O();
298
308
  },
299
309
  keepalive: !1
300
310
  });
301
- }, R = () => {
302
- j.abort(), a.current && (clearTimeout(a.current), a.current = null), i(!1);
311
+ }, O = () => {
312
+ U.abort(), a.current && (clearTimeout(a.current), a.current = null), u(!1), q.invalidateQueries({ queryKey: [I.SEARCH_LIMIT] });
303
313
  };
304
314
  return {
305
315
  /**
@@ -307,13 +317,13 @@ const de = 3 * 60 * 1e3, $ = (o) => {
307
317
  * @type {boolean}
308
318
  * @default false
309
319
  */
310
- loading: E === "pending" || q === "pending" || c,
320
+ loading: R === "pending" || P === "pending" || c,
311
321
  /**
312
322
  * Loading status for fares
313
323
  * @type {boolean}
314
324
  * @default false
315
325
  */
316
- loadingFares: O === "pending",
326
+ loadingFares: C === "pending",
317
327
  outboundError: S,
318
328
  inboundError: T,
319
329
  roundTripFaresError: h,
@@ -329,16 +339,16 @@ const de = 3 * 60 * 1e3, $ = (o) => {
329
339
  * @param {ISearchFlightArgs} args - The search arguments
330
340
  */
331
341
  searchOutboundFlights: async (e) => {
332
- switch ($(e), e.paymentType) {
342
+ switch (G(e), e.paymentType) {
333
343
  default:
334
- case A.CASH_AND_MILES:
335
- H(e);
344
+ case N.CASH_AND_MILES:
345
+ x(e);
336
346
  break;
337
- case A.CASH:
338
- F(e);
347
+ case N.CASH:
348
+ _(e);
339
349
  break;
340
- case A.MILES:
341
- H(e);
350
+ case N.MILES:
351
+ x(e);
342
352
  break;
343
353
  }
344
354
  },
@@ -348,16 +358,16 @@ const de = 3 * 60 * 1e3, $ = (o) => {
348
358
  *
349
359
  * @param {string} outboundOfferId - The ID of the outbound offer
350
360
  */
351
- searchInboundFlights: _,
361
+ searchInboundFlights: H,
352
362
  /**
353
363
  * Used to get round-trip fares after selecting both outbound and inbound flights
354
364
  * @type {function}
355
365
  *
356
366
  * @param {string} inboundOfferId - The ID of the inbound offer
357
367
  */
358
- getRoundTripFares: C
368
+ getRoundTripFares: v
359
369
  };
360
370
  };
361
371
  export {
362
- Ee as useFlightSearch
372
+ Se as useFlightSearch
363
373
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@odynn/awayz-flights",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"
@@ -68,7 +68,7 @@
68
68
  },
69
69
  "dependencies": {
70
70
  "@microsoft/fetch-event-source": "^2.0.1",
71
- "@odynn/awayz-core": "^0.2.0",
71
+ "@odynn/awayz-core": "^0.2.17",
72
72
  "@tanstack/react-query": "^5.67.1"
73
73
  }
74
74
  }