npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lambdacurry/medusa-plugin-webhooks

v0.0.2

Published

Medusa Markethaus Webhooks

Downloads

66

Readme

Medusa Webhooks Plugin

This plugin adds webhook functionality to your Medusa e-commerce server, enabling you to send real-time notifications to external services whenever specific events occur in your system. By integrating this plugin, you can easily manage webhooks directly from the Medusa admin panel, ensuring that your application stays connected and responsive to changes.

Features

  • Create and Manage Webhooks: Easily create, update, and delete webhooks through a user-friendly interface in the Medusa admin panel.
  • Custom Event Subscriptions: Support for custom event types, allowing you to trigger webhooks for specific events that are important to your business.
  • Seamless Integration: Fully integrates with Medusa's event bus system, making it easy to extend and customize.

Installation

To install the Medusa Webhooks Plugin in your Medusa project, follow these steps:

  1. Add the plugin package to your Medusa project:

    yarn add @lambdacurry/medusa-plugin-webhooks
  2. Open your medusa-config.js file and add the plugin to the plugins array with the desired configuration:

    const plugins = [
      // other plugins
      {
        resolve: "@lambdacurry/medusa-plugin-webhooks",
        options: {
          enableUI: true, // Enables the UI components for managing webhooks in the admin panel
          customSubscriptions: [
            "customer.referred",
            "wishlist.item_added",
            "wishlist.item_removed",
          ],
        },
      },
      // other plugins
    ];

Configuration

To configure the Webhooks Plugin:

  1. Enable the UI: By setting enableUI: true, the plugin will include UI components in the Medusa admin panel, allowing for easy management of webhooks.

  2. Custom Event Subscriptions: The customSubscriptions array allows you to define custom events that your application can subscribe to. These events will trigger the webhooks you've set up.

    Example custom events:

    • customer.referred: Triggered when a customer refers a friend or family member to the store.
    • wishlist.item_added: Triggered when a customer adds an item to their wishlist.
    • wishlist.item_removed: Triggered when a customer removes an item from their wishlist.

Example: Implementing a Custom Subscriber

To handle custom events in your Medusa server, you can create a custom subscriber. Here's an example implementation of a custom subscriber for the wishlist.item_added event:

    import { type SubscriberArgs, type SubscriberConfig } from '@medusajs/medusa';
    import WishlistService from '../services/wishlist';
    import WebhookService from '@lambdacurry/medusa-plugin-webhooks/src/services/webhook';
    import { Logger } from '@tanstack/react-query';

    export const config: SubscriberConfig = {
      event: [WishlistService.Events.ITEM_ADDED, WishlistService.Events.ITEM_REMOVED],
      context: {
        subscriberId: 'wishlist-webhooks',
      },
    };

    export default async function handleWishlistWebhooks({ eventName, container, data }: SubscriberArgs) {
      const webhookService = container.resolve<WebhookService>('webhookService');
      const logger = container.resolve<Logger>('logger');
      const { itemId } = data as {
        itemId: string;
      };

      const webhooks = await webhookService.list({
        event_type: eventName,
        active: true,
      });

      if (webhooks.length === 0) {
        logger.warn(`No webhooks found for event ${eventName}`);
        return;
      }

      const wishlistService = container.resolve<WishlistService>('wishlistService');

      const wishlistItem = await wishlistService.retrieveItem(
        { id: itemId }
      );

      const results = await webhookService.sendWebhooksEvents(webhooks, wishlistItem);

      results.forEach((result) => {
        logger.log(`Webhook ${result.value.event_type} -> ${result.value.target_url} ${result.value.result}`);
      });
    }

By following these steps, your Medusa server will be equipped to handle real-time notifications via webhooks, enhancing the interactivity and responsiveness of your e-commerce platform.