Skip to content

Commit

Permalink
Make actionTransformer and stateTransformer optional
Browse files Browse the repository at this point in the history
  • Loading branch information
onurtemizkan committed Oct 4, 2024
1 parent cf1b89f commit c8482b9
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions packages/vue/src/pinia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ type PiniaPlugin = (context: {

type SentryPiniaPluginOptions = {
attachPiniaState?: boolean;
actionTransformer: (action: any) => any;
stateTransformer: (state: any) => any;
actionTransformer?: (action: any) => any;
stateTransformer?: (state: any) => any;
};

export const createSentryPiniaPlugin: (options?: SentryPiniaPluginOptions) => PiniaPlugin = (
Expand All @@ -24,7 +24,7 @@ export const createSentryPiniaPlugin: (options?: SentryPiniaPluginOptions) => Pi
},
) => {
const plugin: PiniaPlugin = ({ store }) => {
options.attachPiniaState &&
options.attachPiniaState !== false &&
getGlobalScope().addEventProcessor((event, hint) => {
try {
// Get current timestamp in hh:mm:ss
Expand All @@ -47,18 +47,20 @@ export const createSentryPiniaPlugin: (options?: SentryPiniaPluginOptions) => Pi

store.$onAction(context => {
context.after(() => {
const transformedAction = options.actionTransformer(context.name);
const transformedActionName = options.actionTransformer
? options.actionTransformer(context.name) || ''
: context.name;

if (typeof transformedAction !== 'undefined' && transformedAction !== null) {
if (typeof transformedActionName !== 'undefined' && transformedActionName !== null) {
addBreadcrumb({
category: 'action',
message: transformedAction,
message: transformedActionName,
level: 'info',
});
}

/* Set latest state to scope */
const transformedState = options.stateTransformer(store.$state);
const transformedState = options.stateTransformer ? options.stateTransformer(store.$state) : store.$state;
const scope = getCurrentScope();

if (typeof transformedState !== 'undefined' && transformedState !== null) {
Expand Down

0 comments on commit c8482b9

Please sign in to comment.