|
| 1 | +import { addBreadcrumb, getClient, getCurrentScope, getGlobalScope } from '@sentry/core'; |
| 2 | +import { addNonEnumerableProperty } from '@sentry/utils'; |
| 3 | + |
| 4 | +// Inline PiniaPlugin type |
| 5 | +type PiniaPlugin = (context: { |
| 6 | + store: { |
| 7 | + $id: string; |
| 8 | + $state: unknown; |
| 9 | + $onAction: (callback: (context: { name: string; after: (callback: () => void) => void }) => void) => void; |
| 10 | + }; |
| 11 | +}) => void; |
| 12 | + |
| 13 | +type SentryPiniaPluginOptions = { |
| 14 | + attachPiniaState?: boolean; |
| 15 | + addBreadcrumbs?: boolean; |
| 16 | + actionTransformer?: (action: any) => any; |
| 17 | + stateTransformer?: (state: any) => any; |
| 18 | +}; |
| 19 | + |
| 20 | +export const createSentryPiniaPlugin: (options?: SentryPiniaPluginOptions) => PiniaPlugin = ( |
| 21 | + options: SentryPiniaPluginOptions = { |
| 22 | + attachPiniaState: true, |
| 23 | + addBreadcrumbs: true, |
| 24 | + actionTransformer: action => action, |
| 25 | + stateTransformer: state => state, |
| 26 | + }, |
| 27 | +) => { |
| 28 | + const plugin: PiniaPlugin = ({ store }) => { |
| 29 | + options.attachPiniaState !== false && |
| 30 | + getGlobalScope().addEventProcessor((event, hint) => { |
| 31 | + try { |
| 32 | + // Get current timestamp in hh:mm:ss |
| 33 | + const timestamp = new Date().toTimeString().split(' ')[0]; |
| 34 | + const filename = `pinia_state_${store.$id}_${timestamp}.json`; |
| 35 | + |
| 36 | + hint.attachments = [ |
| 37 | + ...(hint.attachments || []), |
| 38 | + { |
| 39 | + filename, |
| 40 | + data: JSON.stringify(store.$state), |
| 41 | + }, |
| 42 | + ]; |
| 43 | + } catch (_) { |
| 44 | + // empty |
| 45 | + } |
| 46 | + |
| 47 | + return event; |
| 48 | + }); |
| 49 | + |
| 50 | + store.$onAction(context => { |
| 51 | + context.after(() => { |
| 52 | + const transformedActionName = options.actionTransformer |
| 53 | + ? options.actionTransformer(context.name) |
| 54 | + : context.name; |
| 55 | + |
| 56 | + if ( |
| 57 | + typeof transformedActionName !== 'undefined' && |
| 58 | + transformedActionName !== null && |
| 59 | + options.addBreadcrumbs !== false |
| 60 | + ) { |
| 61 | + addBreadcrumb({ |
| 62 | + category: 'action', |
| 63 | + message: transformedActionName, |
| 64 | + level: 'info', |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + /* Set latest state to scope */ |
| 69 | + const transformedState = options.stateTransformer ? options.stateTransformer(store.$state) : store.$state; |
| 70 | + const scope = getCurrentScope(); |
| 71 | + const currentState = scope.getScopeData().contexts.state; |
| 72 | + |
| 73 | + if (typeof transformedState !== 'undefined' && transformedState !== null) { |
| 74 | + const client = getClient(); |
| 75 | + const options = client && client.getOptions(); |
| 76 | + const normalizationDepth = (options && options.normalizeDepth) || 3; // default state normalization depth to 3 |
| 77 | + const piniaStateContext = { type: 'pinia', value: transformedState }; |
| 78 | + |
| 79 | + const newState = { |
| 80 | + ...(currentState || {}), |
| 81 | + state: piniaStateContext, |
| 82 | + }; |
| 83 | + |
| 84 | + addNonEnumerableProperty( |
| 85 | + newState, |
| 86 | + '__sentry_override_normalization_depth__', |
| 87 | + 3 + // 3 layers for `state.value.transformedState |
| 88 | + normalizationDepth, // rest for the actual state |
| 89 | + ); |
| 90 | + |
| 91 | + scope.setContext('state', newState); |
| 92 | + } else { |
| 93 | + scope.setContext('state', { |
| 94 | + ...(currentState || {}), |
| 95 | + state: { type: 'pinia', value: 'undefined' }, |
| 96 | + }); |
| 97 | + } |
| 98 | + }); |
| 99 | + }); |
| 100 | + }; |
| 101 | + |
| 102 | + return plugin; |
| 103 | +}; |
0 commit comments