| 55 | export const createSentryPiniaPlugin: ( |
| 56 | userOptions?: Partial<SentryPiniaPluginOptions>, |
| 57 | ) => PiniaPlugin = userOptions => { |
| 58 | const options: SentryPiniaPluginOptions = { ...DEFAULT_PINIA_PLUGIN_OPTIONS, ...userOptions }; |
| 59 | |
| 60 | const plugin: PiniaPlugin = ({ store, pinia }) => { |
| 61 | options.attachPiniaState !== false && |
| 62 | getGlobalScope().addEventProcessor((event, hint) => { |
| 63 | try { |
| 64 | // Get current timestamp in hh:mm:ss |
| 65 | const timestamp = new Date().toTimeString().split(' ')[0]; |
| 66 | const filename = `pinia_state_all_stores_${timestamp}.json`; |
| 67 | |
| 68 | // event processor runs for each pinia store - attachment should only be added once per event |
| 69 | const hasExistingPiniaStateAttachment = hint.attachments?.some(attachment => |
| 70 | attachment.filename.startsWith('pinia_state_all_stores_'), |
| 71 | ); |
| 72 | |
| 73 | if (!hasExistingPiniaStateAttachment) { |
| 74 | hint.attachments = [ |
| 75 | ...(hint.attachments || []), |
| 76 | { |
| 77 | filename, |
| 78 | data: JSON.stringify(getAllStoreStates(pinia, options.stateTransformer)), |
| 79 | }, |
| 80 | ]; |
| 81 | } |
| 82 | } catch { |
| 83 | // empty |
| 84 | } |
| 85 | |
| 86 | return event; |
| 87 | }); |
| 88 | |
| 89 | store.$onAction(context => { |
| 90 | context.after(() => { |
| 91 | const transformedActionName = options.actionTransformer(context.name); |
| 92 | |
| 93 | if ( |
| 94 | typeof transformedActionName !== 'undefined' && |
| 95 | transformedActionName !== null && |
| 96 | options.addBreadcrumbs !== false |
| 97 | ) { |
| 98 | addBreadcrumb({ |
| 99 | category: 'pinia.action', |
| 100 | message: `Store: ${store.$id} | Action: ${transformedActionName}`, |
| 101 | level: 'info', |
| 102 | }); |
| 103 | } |
| 104 | |
| 105 | /* Set latest state of all stores to scope */ |
| 106 | const allStates = getAllStoreStates(pinia, options.stateTransformer); |
| 107 | const scope = getCurrentScope(); |
| 108 | const currentState = scope.getScopeData().contexts.state; |
| 109 | |
| 110 | if (typeof allStates !== 'undefined' && allStates !== null) { |
| 111 | const client = getClient(); |
| 112 | const options = client?.getOptions(); |
| 113 | const normalizationDepth = options?.normalizeDepth || 3; // default state normalization depth to 3 |
| 114 | const piniaStateContext = { type: 'pinia', value: allStates }; |