@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
|
|
2
|
-
import { useMutation as
|
|
3
|
-
import { ECabinClass as
|
|
4
|
-
import { FlightService as
|
|
5
|
-
import { parseFlightDetails as
|
|
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
|
|
8
|
-
import
|
|
9
|
-
import { useAwayzContext as
|
|
10
|
-
async function
|
|
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
|
|
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 =
|
|
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
|
|
44
|
-
let n =
|
|
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 =
|
|
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),
|
|
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
|
-
` +
|
|
54
|
+
` + g : g;
|
|
55
55
|
break;
|
|
56
56
|
case "event":
|
|
57
|
-
n.event =
|
|
57
|
+
n.event = g;
|
|
58
58
|
break;
|
|
59
59
|
case "id":
|
|
60
|
-
o(n.id =
|
|
60
|
+
o(n.id = g);
|
|
61
61
|
break;
|
|
62
62
|
case "retry":
|
|
63
|
-
const
|
|
64
|
-
isNaN(
|
|
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
|
|
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
|
|
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
|
|
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
|
|
91
|
-
function
|
|
92
|
-
var { signal: r, headers: n, onopen: s, onmessage: v, onclose: c, onerror: u, openWhenHidden: a, fetch: d } = t,
|
|
93
|
-
return new Promise((
|
|
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 =
|
|
95
|
+
p.accept || (p.accept = x);
|
|
96
96
|
let m;
|
|
97
|
-
function
|
|
98
|
-
m.abort(), document.hidden ||
|
|
97
|
+
function w() {
|
|
98
|
+
m.abort(), document.hidden || T();
|
|
99
99
|
}
|
|
100
|
-
a || document.addEventListener("visibilitychange",
|
|
101
|
-
let
|
|
100
|
+
a || document.addEventListener("visibilitychange", w);
|
|
101
|
+
let F = ie, E = 0;
|
|
102
102
|
function S() {
|
|
103
|
-
document.removeEventListener("visibilitychange",
|
|
103
|
+
document.removeEventListener("visibilitychange", w), window.clearTimeout(E), m.abort();
|
|
104
104
|
}
|
|
105
105
|
r == null || r.addEventListener("abort", () => {
|
|
106
|
-
S(),
|
|
106
|
+
S(), b();
|
|
107
107
|
});
|
|
108
|
-
const
|
|
109
|
-
async function
|
|
110
|
-
var
|
|
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
|
|
114
|
-
await
|
|
115
|
-
h ? p[
|
|
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
|
-
|
|
118
|
-
}, v))), c == null || c(), S(),
|
|
119
|
-
} catch (
|
|
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 = (
|
|
123
|
-
window.clearTimeout(E), E = window.setTimeout(
|
|
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(),
|
|
125
|
+
S(), k(h);
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
|
-
|
|
129
|
+
T();
|
|
130
130
|
});
|
|
131
131
|
}
|
|
132
|
-
function
|
|
132
|
+
function ce(o) {
|
|
133
133
|
const t = o.headers.get("content-type");
|
|
134
|
-
if (!(t != null && t.startsWith(
|
|
135
|
-
throw new Error(`Expected content-type to be ${
|
|
134
|
+
if (!(t != null && t.startsWith(x)))
|
|
135
|
+
throw new Error(`Expected content-type to be ${x}, Actual: ${t}`);
|
|
136
136
|
}
|
|
137
|
-
const
|
|
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 ===
|
|
144
|
+
if (o.flightType === J.ROUND_TRIP && !o.returnDate)
|
|
143
145
|
throw new Error("Return date is required for round-trip flights.");
|
|
144
|
-
},
|
|
145
|
-
const { token: o, config: t } =
|
|
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:
|
|
148
|
-
setInboundResults:
|
|
149
|
-
setOutboundResults:
|
|
149
|
+
outboundResults: g,
|
|
150
|
+
setInboundResults: b,
|
|
151
|
+
setOutboundResults: k,
|
|
150
152
|
partialOfferRequestId: p,
|
|
151
153
|
setPartialOfferRequestId: m
|
|
152
|
-
} =
|
|
154
|
+
} = Z(), w = ({
|
|
153
155
|
data: e,
|
|
154
156
|
previousResults: l,
|
|
155
157
|
inbound: f
|
|
156
158
|
}) => {
|
|
157
|
-
const i =
|
|
158
|
-
return (f ?
|
|
159
|
+
const i = V(e, l);
|
|
160
|
+
return (f ? b : k)(G(i)), i;
|
|
159
161
|
}, {
|
|
160
|
-
mutate:
|
|
162
|
+
mutate: F,
|
|
161
163
|
status: E,
|
|
162
164
|
error: S
|
|
163
|
-
} =
|
|
165
|
+
} = L({
|
|
164
166
|
mutationFn: async (e) => {
|
|
165
|
-
|
|
167
|
+
$(e);
|
|
166
168
|
const f = {
|
|
167
169
|
passengers: Array.from(
|
|
168
|
-
{ length: e.passengers ??
|
|
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 ||
|
|
175
|
+
cabin_class: e.cabinClass || Y
|
|
174
176
|
};
|
|
175
|
-
if (e.flightType ===
|
|
176
|
-
const i = await
|
|
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
|
|
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
|
-
|
|
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:
|
|
212
|
-
error:
|
|
213
|
-
} =
|
|
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
|
|
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 &&
|
|
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:
|
|
234
|
-
status:
|
|
235
|
+
mutate: C,
|
|
236
|
+
status: O,
|
|
235
237
|
error: h
|
|
236
|
-
} =
|
|
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
|
|
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(
|
|
252
|
+
v(W(i));
|
|
251
253
|
},
|
|
252
254
|
onError: (e) => {
|
|
253
255
|
console.error("Error in round-trip fare search:", e);
|
|
254
256
|
}
|
|
255
|
-
}),
|
|
257
|
+
}), H = async (e) => {
|
|
258
|
+
var M;
|
|
256
259
|
u(!0);
|
|
257
|
-
const l = e.paymentType ===
|
|
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 ||
|
|
262
|
-
passengerCount: e.passengers ||
|
|
264
|
+
cabinClass: e.cabinClass || Y,
|
|
265
|
+
passengerCount: e.passengers || B,
|
|
263
266
|
paymentType: l
|
|
264
267
|
});
|
|
265
|
-
let i = [];
|
|
266
|
-
a.current = setTimeout(() => {
|
|
267
|
-
|
|
268
|
-
},
|
|
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:
|
|
276
|
-
onmessage(
|
|
278
|
+
signal: j.signal,
|
|
279
|
+
onmessage(A) {
|
|
277
280
|
try {
|
|
278
|
-
const
|
|
279
|
-
if (
|
|
280
|
-
C();
|
|
281
|
+
const y = W(JSON.parse(A.data));
|
|
282
|
+
if (y.success === !1)
|
|
281
283
|
return;
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
C();
|
|
284
|
+
if (y.close === !0) {
|
|
285
|
+
R();
|
|
285
286
|
return;
|
|
286
287
|
}
|
|
287
|
-
|
|
288
|
-
data:
|
|
288
|
+
y.flights && (I = 0, i = w({
|
|
289
|
+
data: y.flights,
|
|
289
290
|
previousResults: i
|
|
290
|
-
}));
|
|
291
|
-
} catch (
|
|
292
|
-
console.error("Error parsing message:",
|
|
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(
|
|
296
|
-
throw console.error("EventSource error:",
|
|
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
|
-
},
|
|
304
|
-
|
|
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" ||
|
|
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:
|
|
318
|
+
loadingFares: O === "pending",
|
|
319
319
|
outboundError: S,
|
|
320
|
-
inboundError:
|
|
320
|
+
inboundError: T,
|
|
321
321
|
roundTripFaresError: h,
|
|
322
322
|
// Results
|
|
323
|
-
outboundResults:
|
|
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 (
|
|
334
|
+
switch ($(e), e.paymentType) {
|
|
335
335
|
default:
|
|
336
|
-
case
|
|
337
|
-
|
|
336
|
+
case D.CASH_AND_MILES:
|
|
337
|
+
H(e);
|
|
338
338
|
break;
|
|
339
|
-
case
|
|
340
|
-
|
|
339
|
+
case D.CASH:
|
|
340
|
+
F(e);
|
|
341
341
|
break;
|
|
342
|
-
case
|
|
343
|
-
|
|
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:
|
|
360
|
+
getRoundTripFares: C
|
|
361
361
|
};
|
|
362
362
|
};
|
|
363
363
|
export {
|
|
364
|
-
|
|
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.
|
|
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.
|
|
71
|
+
"@odynn/awayz-auth": "^0.1.36",
|
|
72
72
|
"@tanstack/react-query": "^5.67.1"
|
|
73
73
|
}
|
|
74
74
|
}
|