@@ -13,31 +13,42 @@ type PiniaPlugin = (context: {
1313} ) => void ;
1414
1515type 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 ;
2020} ;
2121
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+ } ;
3328
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+ } ;
3745
38- return states ;
39- } ;
46+ export const createSentryPiniaPlugin : (
47+ userOptions ?: Partial < SentryPiniaPluginOptions > ,
48+ ) => PiniaPlugin = userOptions => {
49+ const options : SentryPiniaPluginOptions = { ...DEFAULT_PINIA_PLUGIN_OPTIONS , ...userOptions } ;
4050
51+ const plugin : PiniaPlugin = ( { store, pinia } ) => {
4152 options . attachPiniaState !== false &&
4253 getGlobalScope ( ) . addEventProcessor ( ( event , hint ) => {
4354 try {
@@ -55,7 +66,7 @@ export const createSentryPiniaPlugin: (options?: SentryPiniaPluginOptions) => Pi
5566 ...( hint . attachments || [ ] ) ,
5667 {
5768 filename,
58- data : JSON . stringify ( getAllStoreStates ( ) ) ,
69+ data : JSON . stringify ( getAllStoreStates ( pinia , options . stateTransformer ) ) ,
5970 } ,
6071 ] ;
6172 }
@@ -68,9 +79,7 @@ export const createSentryPiniaPlugin: (options?: SentryPiniaPluginOptions) => Pi
6879
6980 store . $onAction ( context => {
7081 context . after ( ( ) => {
71- const transformedActionName = options . actionTransformer
72- ? options . actionTransformer ( context . name )
73- : context . name ;
82+ const transformedActionName = options . actionTransformer ( context . name ) ;
7483
7584 if (
7685 typeof transformedActionName !== 'undefined' &&
@@ -85,16 +94,15 @@ export const createSentryPiniaPlugin: (options?: SentryPiniaPluginOptions) => Pi
8594 }
8695
8796 /* 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 ) ;
9098 const scope = getCurrentScope ( ) ;
9199 const currentState = scope . getScopeData ( ) . contexts . state ;
92100
93- if ( typeof transformedState !== 'undefined' && transformedState !== null ) {
101+ if ( typeof allStates !== 'undefined' && allStates !== null ) {
94102 const client = getClient ( ) ;
95103 const options = client ?. getOptions ( ) ;
96104 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 } ;
98106
99107 const newState = {
100108 ...( currentState || { } ) ,
0 commit comments