@odynn/awayz-core 0.2.9 → 0.2.11

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,53 +1,57 @@
1
- import { A as N, E as r, a as i, b as F, d as x } from "../../AccountService-CKQ3VzVx.js";
2
- import { useState as m, useCallback as U, useEffect as w } from "react";
3
- import { EAuthFlow as k } from "../../types/EAuthFlow.js";
4
- import { awayzClient as p } from "../../configs/awayzClient.js";
5
- const u = (t, a) => {
6
- var f, E;
7
- let c = "", d = a;
8
- typeof a == "string" ? c = a : a instanceof N ? (c = a.message, d = {
1
+ import { A as _, E as n, a as o, b as q, d as L } from "../../AccountService-CKQ3VzVx.js";
2
+ import { useState as m, useCallback as R, useEffect as w } from "react";
3
+ import { EAuthFlow as S } from "../../types/EAuthFlow.js";
4
+ import { awayzClient as U } from "../../configs/awayzClient.js";
5
+ const v = (t, a) => {
6
+ var f, d;
7
+ let c = "", E = a;
8
+ typeof a == "string" ? c = a : a instanceof _ ? (c = a.message, E = {
9
9
  message: a.message,
10
10
  cause: a.cause,
11
11
  status: a.status,
12
12
  data: (f = a == null ? void 0 : a.response) == null ? void 0 : f.data
13
- }) : c = a.message, (E = window.parent) == null || E.postMessage(
13
+ }) : c = a.message, (d = window.parent) == null || d.postMessage(
14
14
  {
15
15
  type: "auth_error",
16
- details: { message: c, action: t, info: d }
16
+ details: { message: c, action: t, info: E }
17
17
  },
18
18
  "*"
19
19
  );
20
- }, V = (t) => {
21
- const [a, c] = m(!0), [d, f] = m(!1), [E, S] = m(), [R, T] = m(), [O, _] = m([]), h = U(
20
+ }, J = (t) => {
21
+ const [a, c] = m(!0), [E, f] = m(!1), [d, T] = m(), [A, k] = m(), [O, K] = m([]), y = R(
22
22
  async (s) => {
23
- t != null && t.onSuccess && (t == null || t.onSuccess(s)), t != null && t.onSuccessAsync && await (t == null ? void 0 : t.onSuccessAsync(s)), c(!1), f(!0), S(s.token), T(s.user);
23
+ t != null && t.onSuccess && (t == null || t.onSuccess(s)), t != null && t.onSuccessAsync && await (t == null ? void 0 : t.onSuccessAsync(s)), c(!1), f(!0), T(s.token), k(s.user);
24
24
  },
25
25
  [t]
26
- ), y = async (s) => {
26
+ ), i = R(
27
+ async (s, e) => {
28
+ if (t.authFlow !== S.POST_MESSAGE)
29
+ return v(e ?? "exchange_token", s);
30
+ t != null && t.onError && (t == null || t.onError(s)), t != null && t.onErrorAsync && await (t == null ? void 0 : t.onErrorAsync(s)), c(!1);
31
+ },
32
+ [t]
33
+ ), h = async (s) => {
27
34
  try {
28
- const { success: e, data: n, message: o } = await i.authenticateWithToken(s);
35
+ const { success: e, data: r, message: u } = await o.authenticateWithToken(s);
29
36
  if (!e) {
30
- u("exchange_token", o);
37
+ i(u);
31
38
  return;
32
39
  }
33
- if (!(n != null && n.token)) {
34
- u(
35
- "exchange_token",
36
- "no awayz token returned after exchanging id token"
37
- );
40
+ if (!(r != null && r.token)) {
41
+ i("no awayz token returned after exchanging id token");
38
42
  return;
39
43
  }
40
- localStorage.setItem(r.TOKEN, n.token), await h(n);
41
- const { data: g, ...l } = await i.getRequestLimit();
42
- if (!l.success) {
43
- console.error({ reqLimitError: l });
44
+ localStorage.setItem(n.TOKEN, r.token), await y(r);
45
+ const { data: l, ...g } = await o.getRequestLimit();
46
+ if (!g.success) {
47
+ console.error({ reqLimitError: g });
44
48
  return;
45
49
  }
46
- g == null || g.forEach(
47
- (A) => window.parent.postMessage(
50
+ l == null || l.forEach(
51
+ (F) => window.parent.postMessage(
48
52
  {
49
53
  type: "request_limit",
50
- limit: A
54
+ limit: F
51
55
  },
52
56
  "*"
53
57
  )
@@ -57,64 +61,68 @@ const u = (t, a) => {
57
61
  } finally {
58
62
  c(!1);
59
63
  }
60
- }, K = async () => {
64
+ }, I = async () => {
61
65
  try {
62
- const { success: s, data: e } = await p.ensureQueryData({
63
- queryKey: [F.CHECK_USER],
64
- queryFn: i.checkUser
66
+ const { success: s, data: e } = await U.ensureQueryData({
67
+ queryKey: [q.CHECK_USER],
68
+ queryFn: o.checkUser
65
69
  });
66
70
  if (!s) {
67
- u("exchange_token", e);
71
+ i(e);
68
72
  return;
69
73
  }
70
- await h({
74
+ await y({
71
75
  user: e,
72
- token: localStorage.getItem(r.TOKEN)
76
+ token: localStorage.getItem(n.TOKEN)
73
77
  });
74
78
  } catch (s) {
75
- u("exchange_token", s);
79
+ i(s);
76
80
  } finally {
77
81
  c(!1);
78
82
  }
79
83
  };
80
84
  w(() => {
81
- if (t.authFlow !== k.OAUTH) return;
82
- const s = localStorage.getItem(r.TOKEN), e = localStorage.getItem(r.REFRESH_TOKEN);
83
- s && e ? K() : c(!1);
85
+ if (t.authFlow !== S.STANDARD) return;
86
+ const s = localStorage.getItem(n.TOKEN), e = localStorage.getItem(n.REFRESH_TOKEN);
87
+ s && e ? I() : c(!1);
84
88
  }, []);
85
- const I = async () => {
86
- const s = await i.getRegions();
87
- _(s);
89
+ const N = async () => {
90
+ const s = await o.getRegions();
91
+ K(s);
88
92
  };
89
93
  return w(() => {
90
- I();
94
+ N();
91
95
  }, []), w(() => {
92
- if (t.authFlow !== k.QUERY) return;
93
- const e = new URLSearchParams(window.location.search).get(r.TOKEN);
94
- e && y(e);
96
+ if (t.authFlow !== S.QUERY) return;
97
+ const e = new URLSearchParams(window.location.search).get(n.TOKEN);
98
+ e && h(e);
95
99
  }, []), w(() => {
96
- if (t.authFlow !== k.POST_MESSAGE) return;
100
+ if (t.authFlow !== S.POST_MESSAGE) return;
97
101
  const s = async (e) => {
98
- var g;
102
+ var l;
99
103
  if (e.data.type !== "auth") return;
100
- const n = (g = e.data) == null ? void 0 : g.idToken, o = e.data.refreshToken;
101
- if (!n) {
102
- const l = `received message ${JSON.stringify(
104
+ const r = (l = e.data) == null ? void 0 : l.idToken, u = e.data.refreshToken;
105
+ if (!r) {
106
+ const g = `received message ${JSON.stringify(
103
107
  e.data
104
108
  )}
105
109
  no idToken in body`;
106
- u("exchange_token", l);
110
+ i(g);
107
111
  return;
108
112
  }
109
- if (!o) {
110
- const l = `received message: ${JSON.stringify(
113
+ if (!u) {
114
+ const g = `received message: ${JSON.stringify(
111
115
  e.data
112
116
  )}
113
117
  no refreshToken in body`;
114
- u("refresh_token", l);
118
+ i(
119
+ g,
120
+ "refresh_token"
121
+ /* REFRESH */
122
+ );
115
123
  return;
116
124
  }
117
- localStorage.setItem(r.REFRESH_TOKEN, o), await y(n);
125
+ localStorage.setItem(n.REFRESH_TOKEN, u), await h(r);
118
126
  };
119
127
  return window.addEventListener("message", s), window.parent && window.parent.postMessage(
120
128
  {
@@ -127,50 +135,50 @@ const u = (t, a) => {
127
135
  };
128
136
  }, []), {
129
137
  isAuthenticating: a,
130
- isAuthenticated: d,
131
- token: E,
132
- user: R,
138
+ isAuthenticated: E,
139
+ token: d,
140
+ user: A,
133
141
  supportedRegions: O,
134
142
  signIn: async (s) => {
135
- var n;
136
- const e = await i.login(s);
143
+ var r;
144
+ const e = await o.login(s);
137
145
  if (!e.success)
138
- u("exchange_token", e.message);
146
+ i(e.message);
139
147
  else {
140
- const o = (n = e.data) == null ? void 0 : n.refreshToken;
141
- localStorage.setItem(r.REFRESH_TOKEN, o), await h(e.data);
148
+ const u = (r = e.data) == null ? void 0 : r.refreshToken;
149
+ localStorage.setItem(n.REFRESH_TOKEN, u), await y(e.data);
142
150
  }
143
151
  return e;
144
152
  },
145
153
  signUp: async (s) => {
146
- var n;
147
- const e = await i.signUp(s);
154
+ var r;
155
+ const e = await o.signUp(s);
148
156
  if (!e.success)
149
- u("exchange_token", e.message);
157
+ i(e.message);
150
158
  else {
151
- const o = (n = e.data) == null ? void 0 : n.refreshToken;
152
- localStorage.setItem(r.REFRESH_TOKEN, o), await h(e.data);
159
+ const u = (r = e.data) == null ? void 0 : r.refreshToken;
160
+ localStorage.setItem(n.REFRESH_TOKEN, u), await y(e.data);
153
161
  }
154
162
  return e;
155
163
  },
156
164
  signOut: () => {
157
- localStorage.removeItem(r.TOKEN), localStorage.removeItem(r.REFRESH_TOKEN), f(!1), S(void 0), T(void 0);
165
+ localStorage.removeItem(n.TOKEN), localStorage.removeItem(n.REFRESH_TOKEN), f(!1), T(void 0), k(void 0);
158
166
  },
159
- resendVerificationEmail: async (s) => (await i.resendVerificationEmail(s)).success,
167
+ resendVerificationEmail: async (s) => (await o.resendVerificationEmail(s)).success,
160
168
  setTokens: (s, e) => {
161
- localStorage.setItem(r.TOKEN, s), localStorage.setItem(r.REFRESH_TOKEN, e), y(s);
169
+ localStorage.setItem(n.TOKEN, s), localStorage.setItem(n.REFRESH_TOKEN, e), h(s);
162
170
  },
163
171
  getRequestLimits: async () => {
164
- const { data: s, ...e } = await i.getRequestLimit();
172
+ const { data: s, ...e } = await o.getRequestLimit();
165
173
  if (!e.success) {
166
174
  console.error({ reqLimitError: e });
167
175
  return;
168
176
  }
169
- return x(s);
177
+ return L(s);
170
178
  }
171
179
  };
172
180
  };
173
181
  export {
174
- u as authFailurePostMessage,
175
- V as useAwayzAuth
182
+ v as authFailurePostMessage,
183
+ J as useAwayzAuth
176
184
  };
@@ -21,6 +21,9 @@ export interface IAwayzConfig {
21
21
  * @default false
22
22
  */
23
23
  externalInventory?: boolean;
24
+ /**
25
+ * Config for the flights stream
26
+ */
24
27
  flightsConfig?: {
25
28
  /**
26
29
  * The maximum number of ping events before the flight search times out
@@ -33,4 +36,12 @@ export interface IAwayzConfig {
33
36
  */
34
37
  streamTimeoutTotalSeconds?: number;
35
38
  };
39
+ /**
40
+ * Gets called when the authentication flow failed for any reason
41
+ */
42
+ onAuthFailure?: () => void;
43
+ /**
44
+ * Gets called when the authentication flow succeeded
45
+ */
46
+ onAuthSuccess?: () => void;
36
47
  }
@@ -6,12 +6,16 @@ interface IEmbeddedAuthOptionsBase {
6
6
  includeDebugLogs?: boolean;
7
7
  }
8
8
  interface IEmbeddedAuthWithSyncSuccess extends IEmbeddedAuthOptionsBase {
9
- onSuccess: (authData: IAuthData) => void;
9
+ onSuccess?: (authData: IAuthData) => void;
10
10
  onSuccessAsync?: never;
11
+ onError?: (error: any) => void;
12
+ onErrorAsync?: never;
11
13
  }
12
14
  interface IEmbeddedAuthWithAsyncSuccess extends IEmbeddedAuthOptionsBase {
13
15
  onSuccess?: never;
14
16
  onSuccessAsync: (authData: IAuthData) => Promise<void>;
17
+ onError?: never;
18
+ onErrorAsync: (error: any) => Promise<void>;
15
19
  }
16
20
  export type TEmbeddedAuthOptions = IEmbeddedAuthWithSyncSuccess | IEmbeddedAuthWithAsyncSuccess;
17
21
  export {};
@@ -1,34 +1,34 @@
1
- import { jsx as r } from "react/jsx-runtime";
1
+ import { jsx as t } from "react/jsx-runtime";
2
2
  import { useEffect as i } from "react";
3
3
  import { AwayzContext as m } from "../context/AwayzContext.js";
4
4
  import { E as e } from "../AccountService-CKQ3VzVx.js";
5
- import { useAwayzAuth as l } from "../hooks/useAwayzAuth/useAwayzAuth.js";
6
- import { setBaseUrl as s } from "../configs/baseUrl.js";
5
+ import { useAwayzAuth as s } from "../hooks/useAwayzAuth/useAwayzAuth.js";
6
+ import { setBaseUrl as l } from "../configs/baseUrl.js";
7
7
  import { QueryClientProvider as u } from "@tanstack/react-query";
8
8
  import { awayzClient as d } from "../configs/awayzClient.js";
9
9
  import '../assets/_styles.css';/* empty css */
10
10
  import { defaultAwayzConfig as n } from "../configs/defaultAwayzConfig.js";
11
- const C = ({ children: o, config: t }) => {
12
- const a = l({
13
- authFlow: t.authFlow,
14
- onSuccess: () => {
15
- },
16
- trustedOrigins: t.trustedOrigins
11
+ const C = ({ children: o, config: r }) => {
12
+ const a = s({
13
+ authFlow: r.authFlow,
14
+ onSuccess: r.onAuthSuccess,
15
+ onError: r.onAuthFailure,
16
+ trustedOrigins: r.trustedOrigins
17
17
  });
18
18
  return i(() => {
19
- if (!t.clientId) {
19
+ if (!r.clientId) {
20
20
  console.error("You need to provide a client ID to the AwayzProvider");
21
21
  return;
22
22
  }
23
- localStorage.setItem(e.CLIENT_ID, t.clientId), localStorage.setItem(e.AUTH_FLOW, t.authFlow), s(t.testMode ?? !1);
24
- }, [t]), /* @__PURE__ */ r(u, { client: d, children: /* @__PURE__ */ r(
23
+ localStorage.setItem(e.CLIENT_ID, r.clientId), localStorage.setItem(e.AUTH_FLOW, r.authFlow), l(r.testMode ?? !1);
24
+ }, [r]), /* @__PURE__ */ t(u, { client: d, children: /* @__PURE__ */ t(
25
25
  m.Provider,
26
26
  {
27
27
  value: {
28
28
  ...a,
29
29
  config: {
30
30
  ...n,
31
- ...t
31
+ ...r
32
32
  }
33
33
  },
34
34
  children: o
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@odynn/awayz-core",
3
- "version": "0.2.9",
3
+ "version": "0.2.11",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"