@lookiero/checkout 0.6.0-beta.40 → 0.6.0-beta.42

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,13 +1,10 @@
1
- import { matchRoutes as reactRouterMatchRoutes } from "@remix-run/router";
2
1
  import { BrowserClient, Hub } from "@sentry/browser";
3
2
  import { ErrorBoundary } from "@sentry/react";
4
3
  import { ReactNativeTransportOptions } from "@sentry/react-native/dist/js/options";
5
4
  import { Transport } from "@sentry/types";
6
- import { Integrations, WrapComponent, WrapUseRoutes } from "./types";
5
+ import { wrapUseRoutes } from "./reactRouterV6Instrumentation/reactRouterV6Instrumentation";
6
+ import { Integrations, WrapComponent } from "./types";
7
7
  declare const transport: (options: ReactNativeTransportOptions) => Transport;
8
- declare const matchRoutes: typeof reactRouterMatchRoutes;
9
- export { matchRoutes };
10
8
  declare const integrations: Integrations;
11
9
  declare const wrapComponent: WrapComponent;
12
- declare const wrapUseRoutes: WrapUseRoutes;
13
10
  export { BrowserClient as Client, Hub, ErrorBoundary, transport, integrations, wrapComponent, wrapUseRoutes };
@@ -1,24 +1,17 @@
1
- import { matchRoutes as reactRouterMatchRoutes } from "@remix-run/router";
2
1
  import { BrowserClient, Hub, makeFetchTransport, defaultIntegrations } from "@sentry/browser";
3
2
  import { ErrorBoundary } from "@sentry/react";
4
- import { reactRouterV6Instrumentation } from "@sentry/react";
5
- import { wrapUseRoutes as reactRouterV6WrapUseRoutes } from "@sentry/react";
6
3
  import { BrowserTracing } from "@sentry/tracing";
7
4
  import { useEffect } from "react";
8
- import { createRoutesFromChildren, useLocation, useNavigationType } from "react-router-native";
9
- import { stripFirstSlugFromLocation } from "./location";
5
+ import { useLocation, useNavigationType, matchRoutes } from "react-router-native";
6
+ import { reactRouterV6Instrumentation, wrapUseRoutes, } from "./reactRouterV6Instrumentation/reactRouterV6Instrumentation";
10
7
  const transport = (options, nativeFetch) => makeFetchTransport(options, nativeFetch);
11
- // Remove the location's first path ("/checkout"). Otherwise it won't match the proper route.
12
- const matchRoutes = (routes, locationArg) => reactRouterMatchRoutes(routes, stripFirstSlugFromLocation(locationArg));
13
- export { matchRoutes };
14
8
  /**
15
9
  * BrowserTracing must be initialized before the BrowserClient
16
10
  * so we instantiate it before the integrations function is called
17
11
  */
18
12
  const browserTracing = new BrowserTracing({
19
- routingInstrumentation: reactRouterV6Instrumentation(useEffect, useLocation, useNavigationType, createRoutesFromChildren, matchRoutes),
13
+ routingInstrumentation: reactRouterV6Instrumentation(useEffect, useLocation, useNavigationType, matchRoutes),
20
14
  });
21
15
  const integrations = () => [...defaultIntegrations, browserTracing];
22
16
  const wrapComponent = (Component) => Component;
23
- const wrapUseRoutes = reactRouterV6WrapUseRoutes;
24
17
  export { BrowserClient as Client, Hub, ErrorBoundary, transport, integrations, wrapComponent, wrapUseRoutes };
@@ -5,14 +5,7 @@ import { NATIVE } from "@sentry/react-native/dist/js/wrapper";
5
5
  import { useEffect } from "react";
6
6
  import { matchRoutes, useLocation } from "react-router-native";
7
7
  import { ReactRouterV6Instrumentation } from "./reactRouterV6Instrumentation/reactRouterV6Instrumentation.native";
8
- const transport = (options, nativeFetch) => {
9
- console.log("NATIVE.isNativeTransportAvailable()", NATIVE.isNativeTransportAvailable(), options, nativeFetch);
10
- console.log(makeReactNativeTransport(options));
11
- console.log(makeFetchTransport(options, nativeFetch));
12
- return NATIVE.isNativeTransportAvailable()
13
- ? makeReactNativeTransport(options)
14
- : makeFetchTransport(options, nativeFetch);
15
- };
8
+ const transport = (options, nativeFetch) => NATIVE.isNativeTransportAvailable() ? makeReactNativeTransport(options) : makeFetchTransport(options, nativeFetch);
16
9
  const routingInstrumentation = new ReactRouterV6Instrumentation(useEffect, useLocation, matchRoutes);
17
10
  // ReactNativeTracing's instantiation must be delayed so we wrap it in a function
18
11
  // that's going to be called when the Client is instantiated
@@ -73,12 +73,23 @@ const sentryLogger = ({ publicKey, environment, release, project }) => {
73
73
  });
74
74
  return new Hub(client);
75
75
  };
76
+ /**
77
+ * BrowserClient must be initialized before wrapUseRoutes is used.
78
+ *
79
+ * ReactNativeClient cannot be initialized until actual component is rendered.
80
+ * Its initialization will trigger the native sdk setup with the provided settings,
81
+ * but by the time an event will be sent to Sentry, the host project (UAF)
82
+ * will have overrided its configuration (dsn, etc) after initializing it
83
+ * so we have to initialize it on the component mount.
84
+ */
85
+ if (Platform.OS === "web") {
86
+ currentHub = initHub();
87
+ }
76
88
  const onMount = (customerId) => {
77
- if (!currentHub) {
78
- currentHub = initHub();
79
- currentHub.setUser({ id: customerId });
80
- currentHub.startSession();
81
- }
89
+ // currentHub won't be initialized in native until this time
90
+ currentHub = currentHub ?? initHub();
91
+ currentHub.setUser({ id: customerId });
92
+ currentHub.startSession();
82
93
  if (!globalHub) {
83
94
  globalHub = makeMain(currentHub);
84
95
  }
@@ -87,7 +98,6 @@ const sentryLogger = ({ publicKey, environment, release, project }) => {
87
98
  if (currentHub) {
88
99
  currentHub.setUser(null);
89
100
  currentHub.endSession();
90
- currentHub = null;
91
101
  }
92
102
  if (globalHub) {
93
103
  makeMain(globalHub);
@@ -0,0 +1,5 @@
1
+ import { Transaction, TransactionContext } from "@sentry/types";
2
+ import { MatchRoutes, UseEffect, UseLocation, UseNavigationType, WrapUseRoutes } from "../types";
3
+ declare const reactRouterV6Instrumentation: (useEffect: UseEffect, useLocation: UseLocation, useNavigationType: UseNavigationType, matchRoutes: MatchRoutes) => (customStartTransaction: (context: TransactionContext) => Transaction | undefined, startTransactionOnPageLoad?: boolean, startTransactionOnLocationChange?: boolean) => void;
4
+ declare const wrapUseRoutes: WrapUseRoutes;
5
+ export { reactRouterV6Instrumentation, wrapUseRoutes };
@@ -0,0 +1,103 @@
1
+ import { WINDOW } from "@sentry/browser";
2
+ import { logger } from "@sentry/utils";
3
+ import React from "react";
4
+ import { stripFirstSlugFromLocation } from "../location";
5
+ import { normalizedNameAndMatchForLocation } from "./normalizedNameAndMatchForLocation";
6
+ /**
7
+ * "@sentry/react"'s reactRouterV6Instrumentation has a bug and we have to patch its implementation.
8
+ * (regarding location and matchRoutes)
9
+ */
10
+ let activeTransaction;
11
+ /* eslint-disable @typescript-eslint/naming-convention */
12
+ let _useEffect;
13
+ let _useLocation;
14
+ let _useNavigationType;
15
+ let _matchRoutes;
16
+ let _customStartTransaction;
17
+ let _startTransactionOnLocationChange;
18
+ /* eslint-enable @typescript-eslint/naming-convention */
19
+ const SENTRY_TAGS = {
20
+ ["routing.instrumentation"]: "react-router-v6",
21
+ };
22
+ const reactRouterV6Instrumentation = (useEffect, useLocation, useNavigationType, matchRoutes) => {
23
+ return (customStartTransaction, startTransactionOnPageLoad = true, startTransactionOnLocationChange = true) => {
24
+ const initPathName = WINDOW && WINDOW.location && WINDOW.location.pathname;
25
+ if (startTransactionOnPageLoad && initPathName) {
26
+ activeTransaction = customStartTransaction({
27
+ name: initPathName,
28
+ op: "pageload",
29
+ tags: SENTRY_TAGS,
30
+ metadata: {
31
+ source: "url",
32
+ },
33
+ });
34
+ }
35
+ _useEffect = useEffect;
36
+ _useLocation = useLocation;
37
+ _useNavigationType = useNavigationType;
38
+ _matchRoutes = matchRoutes;
39
+ _customStartTransaction = customStartTransaction;
40
+ _startTransactionOnLocationChange = startTransactionOnLocationChange;
41
+ };
42
+ };
43
+ const updatePageloadTransaction = (location, routes) => {
44
+ const normalizedLocation = stripFirstSlugFromLocation(location);
45
+ const branches = _matchRoutes(routes, normalizedLocation);
46
+ if (activeTransaction && branches) {
47
+ const [name, match] = normalizedNameAndMatchForLocation({ routes, location: normalizedLocation, branches });
48
+ const source = match ? "route" : "url";
49
+ activeTransaction.setName(match?.route.path ?? name, source);
50
+ }
51
+ };
52
+ const handleNavigation = (location, routes, navigationType) => {
53
+ const normalizedLocation = stripFirstSlugFromLocation(location);
54
+ const branches = _matchRoutes(routes, normalizedLocation);
55
+ if (_startTransactionOnLocationChange && (navigationType === "PUSH" || navigationType === "POP") && branches) {
56
+ if (activeTransaction) {
57
+ activeTransaction.finish();
58
+ }
59
+ const [name, match] = normalizedNameAndMatchForLocation({ routes, location: normalizedLocation, branches });
60
+ const source = match ? "route" : "url";
61
+ activeTransaction = _customStartTransaction({
62
+ name: match?.route.path ?? name,
63
+ op: "navigation",
64
+ tags: SENTRY_TAGS,
65
+ metadata: {
66
+ source,
67
+ },
68
+ });
69
+ }
70
+ };
71
+ const wrapUseRoutes = (origUseRoutes) => {
72
+ if (!_useEffect || !_useLocation || !_useNavigationType || !_matchRoutes || !_customStartTransaction) {
73
+ __DEBUG_BUILD__ &&
74
+ logger.warn("reactRouterV6Instrumentation was unable to wrap `useRoutes` because of one or more missing parameters.");
75
+ return origUseRoutes;
76
+ }
77
+ let isMountRenderPass = true;
78
+ // eslint-disable-next-line react/display-name
79
+ return (routes, locationArg) => {
80
+ const SentryRoutes = () => {
81
+ const Routes = origUseRoutes(routes, locationArg);
82
+ const location = _useLocation();
83
+ const navigationType = _useNavigationType();
84
+ // A value with stable identity to either pick `locationArg` if available or `location` if not
85
+ const stableLocationParam = typeof locationArg === "string" || (locationArg && locationArg.pathname)
86
+ ? locationArg
87
+ : location;
88
+ _useEffect(() => {
89
+ const normalizedLocation = typeof stableLocationParam === "string" ? { pathname: stableLocationParam } : stableLocationParam;
90
+ if (isMountRenderPass) {
91
+ updatePageloadTransaction(normalizedLocation, routes);
92
+ isMountRenderPass = false;
93
+ }
94
+ else {
95
+ handleNavigation(normalizedLocation, routes, navigationType);
96
+ }
97
+ }, [navigationType, stableLocationParam]);
98
+ return Routes;
99
+ };
100
+ return React.createElement(SentryRoutes, null);
101
+ };
102
+ };
103
+ export { reactRouterV6Instrumentation, wrapUseRoutes };
@@ -7,6 +7,8 @@ interface Location {
7
7
  type UseEffect = typeof useEffect;
8
8
  type UseLocation = typeof useLocation;
9
9
  type MatchRoutes = typeof matchRoutes;
10
+ type NavigationType = "PUSH" | "REPLACE" | "POP";
11
+ type UseNavigationType = () => NavigationType;
10
12
  type UseRoutes = (routes: RouteObject[], locationArg?: Partial<Location> | string) => React.ReactElement | null;
11
13
  interface WrapUseRoutes {
12
14
  (useRoutes: UseRoutes): UseRoutes;
@@ -17,4 +19,4 @@ interface WrapComponent {
17
19
  interface Integrations {
18
20
  (): Integration[];
19
21
  }
20
- export type { Location, Integrations, UseEffect, UseLocation, MatchRoutes, UseRoutes, WrapUseRoutes, WrapComponent };
22
+ export type { Location, Integrations, UseEffect, UseLocation, NavigationType, UseNavigationType, MatchRoutes, UseRoutes, WrapUseRoutes, WrapComponent, };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lookiero/checkout",
3
- "version": "0.6.0-beta.40",
3
+ "version": "0.6.0-beta.42",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [