@velion-la/onboarding-sdk-react-native 1.4.0-beta.4 → 1.4.0-beta.6

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.
@@ -17,6 +17,8 @@
17
17
  * - SESSION_CANCELLED rides the same channel as SESSION_DECLINED (it can land
18
18
  * mid-step too) but never triggers a GET: the backend revokes the session
19
19
  * token with the cancellation, so the SDK closes SSE and stops calling out
20
+ * - A request rejected with the 401 `VOB-40103` (token revoked by that same
21
+ * cancellation, SSE event missed) ends the flow exactly like SESSION_CANCELLED
20
22
  */
21
23
 
22
24
  import { TypedEventEmitter } from '../utils/EventEmitter';
@@ -174,6 +176,18 @@ function cancelledEventFromDto(): OnboardingSessionEvent {
174
176
  return { event_type: 'SESSION_CANCELLED', session_status: 'CANCELLED' };
175
177
  }
176
178
 
179
+ /**
180
+ * A request got the 401 `VOB-40103`: the same cancellation SSE would have
181
+ * announced, rebuilt with the reason the 401 body carries.
182
+ */
183
+ function cancelledEventFromRevokedToken(reason: string | undefined): OnboardingSessionEvent {
184
+ return {
185
+ event_type: 'SESSION_CANCELLED',
186
+ session_status: 'CANCELLED',
187
+ ...(reason !== undefined && { reason }),
188
+ };
189
+ }
190
+
177
191
  function isSessionTokenAuthSdkError(err: unknown): err is SdkError {
178
192
  return (
179
193
  err != null &&
@@ -254,11 +268,16 @@ export class VelionOnboarding {
254
268
  if (this.config.debug === true) setDebugEnabled(true);
255
269
  let api: ApiCaller;
256
270
  let constructionError: SdkError | null = null;
271
+ const hooks = {
272
+ onSessionCancelled: (reason: string | undefined) =>
273
+ this.endFlowWithRevokedToken(reason),
274
+ };
257
275
  try {
258
276
  api = createApiCaller(
259
277
  apiUrl,
260
278
  config.sessionToken,
261
279
  config.expiresAt,
280
+ hooks,
262
281
  );
263
282
  } catch (err) {
264
283
  constructionError = err as SdkError;
@@ -338,6 +357,9 @@ export class VelionOnboarding {
338
357
  try {
339
358
  await this.initialize();
340
359
  } catch (err) {
360
+ // The flow already reported how it ended (e.g. a request that found the
361
+ // session cancelled); the rejection that unwinds here is not news.
362
+ if (this.sessionEnded) return;
341
363
  this.emitError(err as SdkError);
342
364
  }
343
365
  }
@@ -941,6 +963,24 @@ export class VelionOnboarding {
941
963
  this.emitError(err);
942
964
  }
943
965
 
966
+ /**
967
+ * A request was rejected with the 401 `VOB-40103`: the backend cancelled the
968
+ * session and revoked its token, and the SSE event either never arrived or
969
+ * SSE was not open. Reported to the host exactly as SESSION_CANCELLED over
970
+ * SSE: `onSessionEvent` with the event, then `onError(SESSION_CANCELLED)` and
971
+ * the cancelled screen. Never `SESSION_EXPIRED`, so the host does not try to
972
+ * refresh a token that belongs to a closed session.
973
+ */
974
+ private endFlowWithRevokedToken(reason: string | undefined): void {
975
+ if (this.sessionEnded || this.status === 'DESTROYED') return;
976
+ const event = cancelledEventFromRevokedToken(reason);
977
+ this.showLoading(null);
978
+ this.config.onSessionEvent?.(event);
979
+ this.endFlowWithSessionCancelled(event);
980
+ // Unwind a step that swallowed the rejection instead of leaving it pending.
981
+ this.sessionDeclineHandler?.(event);
982
+ }
983
+
944
984
  /** Closes SSE and the API caller, and tears down the active step. */
945
985
  private stopTalkingToBackend(): void {
946
986
  this.api.close();
@@ -29,12 +29,32 @@ const SESSION_EXPIRED_CODE = SdkErrorCode.SESSION_EXPIRED;
29
29
  /** Backend code for an app outside the organization's enforced allowed apps. */
30
30
  const APP_ID_NOT_ALLOWED_BACKEND_CODE = 'VOB-40302';
31
31
 
32
+ /**
33
+ * Backend code of the 401 for a session token revoked because the backend
34
+ * cancelled the session (e.g. `WORKFLOW_VERSION_RETIRED`). Unlike an expired
35
+ * token, a new token does not help: the session itself is closed.
36
+ */
37
+ const SESSION_CANCELLED_BACKEND_CODE = 'VOB-40103';
38
+
39
+ /** Error body: the legacy `{message, code}` shape or RFC 7807 problem+json. */
32
40
  interface BackendErrorBody {
33
41
  message?: string;
42
+ detail?: string;
34
43
  code?: string;
44
+ reason?: string;
35
45
  details?: unknown;
36
46
  }
37
47
 
48
+ /** Hooks the orchestrator gives the API caller. */
49
+ export interface ApiCallerHooks {
50
+ /**
51
+ * Called once when a request is rejected with the 401 `VOB-40103`: the
52
+ * backend cancelled the session and revoked its token. The caller is already
53
+ * closed when it runs, and the request still rejects with `SESSION_CANCELLED`.
54
+ */
55
+ onSessionCancelled?: (reason: string | undefined) => void;
56
+ }
57
+
38
58
  function buildApiError(
39
59
  message: string,
40
60
  httpStatus?: number,
@@ -75,6 +95,7 @@ export function createApiCaller(
75
95
  rawBaseUrl: string,
76
96
  sessionToken: string,
77
97
  expiresAt?: string,
98
+ hooks: ApiCallerHooks = {},
78
99
  ) {
79
100
  if (!isSecureHttpUrl(rawBaseUrl)) {
80
101
  throw {
@@ -188,6 +209,24 @@ export function createApiCaller(
188
209
  // Body may not be JSON (e.g., a 502 from a proxy). That's fine.
189
210
  }
190
211
 
212
+ if (
213
+ response.status === 401 &&
214
+ errorBody.code === SESSION_CANCELLED_BACKEND_CODE
215
+ ) {
216
+ // Not an expired token: refreshing would only earn another 401, so
217
+ // close the caller and report the cancellation instead.
218
+ const alreadyClosed = closed;
219
+ closed = true;
220
+ if (!alreadyClosed) hooks.onSessionCancelled?.(errorBody.reason);
221
+ throw {
222
+ code: SdkErrorCode.SESSION_CANCELLED,
223
+ message: errorBody.detail ?? 'The onboarding session was cancelled.',
224
+ ...(errorBody.reason !== undefined && { cancelReason: errorBody.reason }),
225
+ cause: errorBody,
226
+ httpStatus: response.status,
227
+ } satisfies SdkError;
228
+ }
229
+
191
230
  if (errorBody.code === APP_ID_NOT_ALLOWED_BACKEND_CODE) {
192
231
  throw {
193
232
  code: SdkErrorCode.APP_NOT_ALLOWED,
@@ -201,6 +240,7 @@ export function createApiCaller(
201
240
 
202
241
  throw buildApiError(
203
242
  errorBody.message ??
243
+ errorBody.detail ??
204
244
  `Backend returned ${response.status} on ${endpoint}.`,
205
245
  response.status,
206
246
  errorBody,
@@ -300,8 +300,9 @@ export enum SdkErrorCode {
300
300
  /** Backend sent SESSION_DECLINED over SSE; session ended before completion. */
301
301
  SESSION_DECLINED = 'SESSION_DECLINED',
302
302
  /**
303
- * The backend cancelled the session (`SESSION_CANCELLED` over SSE, or
304
- * `status: CANCELLED` on `GET /sessions/:id`). Not a verdict on the person:
303
+ * The backend cancelled the session (`SESSION_CANCELLED` over SSE,
304
+ * `status: CANCELLED` on `GET /sessions/:id`, or a request rejected with the
305
+ * 401 `VOB-40103` for the token it revoked). Not a verdict on the person:
305
306
  * the session was closed on Velion's side (e.g. `WORKFLOW_VERSION_RETIRED`
306
307
  * when a new workflow version is published forcing it). The session token
307
308
  * is revoked and the SDK sends no further requests; create a new session
@@ -700,7 +701,9 @@ export interface VelionConfig extends VelionCallbacks {
700
701
  /**
701
702
  * ISO-8601 expiry timestamp of the sessionToken. If provided, the SDK may
702
703
  * check expiration before requests and emit SESSION_EXPIRED without a round-trip.
703
- * When the backend returns 401/403, the SDK emits SESSION_EXPIRED anyway.
704
+ * When the backend returns 401/403, the SDK emits SESSION_EXPIRED anyway,
705
+ * except for the 401 `VOB-40103` (session cancelled, token revoked), which
706
+ * ends the flow with SESSION_CANCELLED: a new token would not help.
704
707
  */
705
708
  expiresAt?: string;
706
709
 
@@ -1,6 +1,8 @@
1
1
  /**
2
- * Compile-time SDK version string sent on every authenticated request as
3
- * `X-Velion-SDK-Version`. Bump it together with `sdk/package.json#version`
4
- * on every release.
2
+ * SDK version sent on every authenticated request as `X-Velion-SDK-Version`
3
+ * and inside `X-Velion-Client`.
4
+ *
5
+ * GENERATED by `sdk/scripts/sync-sdk-version.js` from `sdk/package.json`:
6
+ * do not edit by hand. The build and the CI version bump regenerate it.
5
7
  */
6
- export const SDK_VERSION = '0.1.0' as const;
8
+ export const SDK_VERSION = '1.4.0-beta.6' as const;