@odynn/awayz-flights 0.1.27 → 0.1.28

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