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

lemonsqueezy-webhooks

v0.1.2

Published

lemon-squeezy webhooks types and an utility functions to handle webhooks in Node.js

Downloads

267

Readme

Install

npm i lemonsqueezy-webhooks

Usage

This package exposes the lemon-squeezy webhooks types and an utility functions to handle webhooks in Node.js

nodejsWebHookHandler

Checks the signature of the request body and parses it to a WebhookPayload type.

It also adds a top level event_name field to make Typescript discriminated unions work inside onData.

Usage in Node.js

import { nodejsWebHookHandler } from 'lemonsqueezy-webhooks'

const secret = process.env.LEMON_SQUEEZY_WEBHOOK_SECRET

// ... Express app setup

app.post('/webhooks', async (req, res) => {
    await nodejsWebHookHandler({
        async onData(payload) {
            console.log(payload)
            // payload.event_name allows TypeScript to infer the type of payload.data
            if (payload.event_name === 'order_created') {
                // payload.data is an Order
                console.log(payload.data.attributes.status)
            }
        },
        req,
        res,
        secret,
    })
})

Usage in Next.js (with Node runtime)

You can also see the source code in the Next.js app example in this repo for a full example.

// api/webhook.ts
import type { NextApiResponse, NextApiRequest } from 'next'
import { nodejsWebHookHandler } from 'lemonsqueezy-webhooks'

export const config = {
    api: {
        // important! otherwise the body signature check will fail
        bodyParser: false,
    },
}

const secret = process.env.SECRET

if (!secret) {
    throw new Error('SECRET is not set')
}

export default async function handler(
    req: NextApiRequest,
    res: NextApiResponse,
) {
    await nodejsWebHookHandler({
        async onData(payload) {
            console.log(payload)
            if (payload.event_name === 'order_created') {
                // payload.data is an Order
                console.log(payload.data.attributes.status)
            }
        },
        req,
        res,
        secret,
    })
}

Usage in Next.js (with Web Streams)

You can also use the whatwgWebhooksHandler function to handle webhooks in Next.js routes that export a POST and GET handler.

// api/webhook.ts

import { whatwgWebhooksHandler } from 'lemonsqueezy-webhooks'

const secret = process.env.SECRET

if (!secret) {
    throw new Error('SECRET is not set')
}

export const POST = (request: Request) => {
    return whatwgWebhooksHandler({
        async onData(payload) {
            console.log(payload)
            if (payload.event_name === 'order_created') {
                // payload.data is an Order
                console.log(payload.data.attributes.status)
            }
        },
        request,
        secret,
    })
}

Exported types:

  • WebhookPayload, the lemonsqueezy json body of a webhook
  • Order, the payload.data type for the events
    • order_created
    • order_updated
    • order_deleted
  • Subscription, the payload.data type for the events
    • subscription_created
    • subscription_cancelled
    • subscription_resumed
    • subscription_expired
    • subscription_paused
    • subscription_unpaused
  • SubscriptionInvoice, the payload.data type for the events
    • subscription_payment_success
    • subscription_payment_failed
    • subscription_payment_recovered
  • LicenseKey, the payload.data type for the events
    • license_key_created

Exported functions

  • nodejsWebHookHandler, it handles webhooks signature check and parsing. It also adds a top level event_name field to the payload to make Typescript discriminated unions work and infer the payload.data type under if blocks inside onData.

Sponsors

Notaku

Notaku


Licensed under MIT.