@@ -13,31 +13,42 @@ type PiniaPlugin = (context: {
13
13
} ) => void ;
14
14
15
15
type SentryPiniaPluginOptions = {
16
- attachPiniaState ? : boolean ;
17
- addBreadcrumbs ? : boolean ;
18
- actionTransformer ? : ( action : string ) => any ;
19
- stateTransformer ? : ( state : Record < string , unknown > ) => any ;
16
+ attachPiniaState : boolean ;
17
+ addBreadcrumbs : boolean ;
18
+ actionTransformer : ( action : string ) => any ;
19
+ stateTransformer : ( state : Record < string , unknown > ) => any ;
20
20
} ;
21
21
22
- export const createSentryPiniaPlugin : ( options ?: SentryPiniaPluginOptions ) => PiniaPlugin = (
23
- options : SentryPiniaPluginOptions = {
24
- attachPiniaState : true ,
25
- addBreadcrumbs : true ,
26
- actionTransformer : action => action ,
27
- stateTransformer : state => state ,
28
- } ,
29
- ) => {
30
- const plugin : PiniaPlugin = ( { store, pinia } ) => {
31
- const getAllStoreStates = ( ) : Record < string , unknown > => {
32
- const states : Record < string , unknown > = { } ;
22
+ const DEFAULT_PINIA_PLUGIN_OPTIONS : SentryPiniaPluginOptions = {
23
+ attachPiniaState : true ,
24
+ addBreadcrumbs : true ,
25
+ actionTransformer : action => action ,
26
+ stateTransformer : state => state ,
27
+ } ;
33
28
34
- Object . keys ( pinia . state . value ) . forEach ( storeId => {
35
- states [ storeId ] = pinia . state . value [ storeId ] ;
36
- } ) ;
29
+ const getAllStoreStates = (
30
+ pinia : { state : Ref < Record < string , StateTree > > } ,
31
+ stateTransformer ?: SentryPiniaPluginOptions [ 'stateTransformer' ] ,
32
+ ) : Record < string , unknown > => {
33
+ const states : Record < string , unknown > = { } ;
34
+
35
+ try {
36
+ Object . keys ( pinia . state . value ) . forEach ( storeId => {
37
+ states [ storeId ] = pinia . state . value [ storeId ] ;
38
+ } ) ;
39
+
40
+ return stateTransformer ? stateTransformer ( states ) : states ;
41
+ } catch {
42
+ return states ;
43
+ }
44
+ } ;
37
45
38
- return states ;
39
- } ;
46
+ export const createSentryPiniaPlugin : (
47
+ userOptions ?: Partial < SentryPiniaPluginOptions > ,
48
+ ) => PiniaPlugin = userOptions => {
49
+ const options : SentryPiniaPluginOptions = { ...DEFAULT_PINIA_PLUGIN_OPTIONS , ...userOptions } ;
40
50
51
+ const plugin : PiniaPlugin = ( { store, pinia } ) => {
41
52
options . attachPiniaState !== false &&
42
53
getGlobalScope ( ) . addEventProcessor ( ( event , hint ) => {
43
54
try {
@@ -55,7 +66,7 @@ export const createSentryPiniaPlugin: (options?: SentryPiniaPluginOptions) => Pi
55
66
...( hint . attachments || [ ] ) ,
56
67
{
57
68
filename,
58
- data : JSON . stringify ( getAllStoreStates ( ) ) ,
69
+ data : JSON . stringify ( getAllStoreStates ( pinia , options . stateTransformer ) ) ,
59
70
} ,
60
71
] ;
61
72
}
@@ -68,9 +79,7 @@ export const createSentryPiniaPlugin: (options?: SentryPiniaPluginOptions) => Pi
68
79
69
80
store . $onAction ( context => {
70
81
context . after ( ( ) => {
71
- const transformedActionName = options . actionTransformer
72
- ? options . actionTransformer ( context . name )
73
- : context . name ;
82
+ const transformedActionName = options . actionTransformer ( context . name ) ;
74
83
75
84
if (
76
85
typeof transformedActionName !== 'undefined' &&
@@ -85,16 +94,15 @@ export const createSentryPiniaPlugin: (options?: SentryPiniaPluginOptions) => Pi
85
94
}
86
95
87
96
/* Set latest state of all stores to scope */
88
- const allStates = getAllStoreStates ( ) ;
89
- const transformedState = options . stateTransformer ? options . stateTransformer ( allStates ) : allStates ;
97
+ const allStates = getAllStoreStates ( pinia , options . stateTransformer ) ;
90
98
const scope = getCurrentScope ( ) ;
91
99
const currentState = scope . getScopeData ( ) . contexts . state ;
92
100
93
- if ( typeof transformedState !== 'undefined' && transformedState !== null ) {
101
+ if ( typeof allStates !== 'undefined' && allStates !== null ) {
94
102
const client = getClient ( ) ;
95
103
const options = client ?. getOptions ( ) ;
96
104
const normalizationDepth = options ?. normalizeDepth || 3 ; // default state normalization depth to 3
97
- const piniaStateContext = { type : 'pinia' , value : transformedState } ;
105
+ const piniaStateContext = { type : 'pinia' , value : allStates } ;
98
106
99
107
const newState = {
100
108
...( currentState || { } ) ,
0 commit comments