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

@planship/react

v0.3.2

Published

Welcome to `@planship/react`, the client-side SDK for [React](https://react.dev/) that enables entitlements, metering, plan packaging, and customer/subscription management in your React and [Next.js](https://nextjs.org/) apps powered by [Planship](https:/

Downloads

2

Readme

planship-react

Welcome to @planship/react, the client-side SDK for React that enables entitlements, metering, plan packaging, and customer/subscription management in your React and Next.js apps powered by Planship. This SDK is built on top of the @planship/fetch JavaScript library and it uses the React Context API.

A complete working example of a Next.js app integrated with Planship can be found at https://github.com/planship/planship-nextjs-example

The basics

The Planship React SDK implements two context providers:

  • Planship provider initialized with withPlanshipProvider and accessible via the usePlanship hook. This provider exposes an instance of the Planship API client class.

  • PlanshipCustomer provider initialized with withPlanshipCustomerProvider and accessible via the usePlanshipCustomer hook. This provider exposes an instance of the Planship Customer API client class initialized for a specific customer, as well as their entitlements that are continously updated via a WebSocket connection.

Installation

Install @planship/react with npm, yarn or pnpm:

npm install @planship/react
# or
yarn add @planship/react
# or
pnpm add @planship/react

Planship context provider

The Planship context provider is designed to be initialized at the very top of your layout where the Planship customer ID might not be known (E.g. outside of your authed layout).

With @planship/react added to your project, initialize PlanshipProvider using withPlanshipProvider, and wrap your components with it.

import { withPlanshipProvider } from '@planship/react'

function App() {
  const PlanshipProvider = withPlanshipProvider(
    {
      slug: 'clicker-demo', // your Planship product slug
      getAccessToken: getAccessToken, // function that returns a valid Planship token
    }
  )

  return (
    <PlanshipProvider>
      <Page />
    </PlanshipProvider>
  )
}

Then, consume the Planship context in any of the nested components using the usePlanship hook:

import { usePlanship } from '@planship/react'

export default function YourComponent({ children }) {
  const { planshipApiClient } = usePlanship()

  // Hook that provides the current user
  const user = useCurrentUser()

  // React state for Planship entitlements
  const [entitlements, setEntitlements ] = useState(() => ({}))

  // useEffect hook to retrieve entitlements from Planship via an API call
  useEffect(() => {
    async function fetchEntitlements(customerId: string) {
      planshipApiClient.getEntitlements(user.id)
      .then((e) => setEntitlements(e))
    }
    fetchEntitlements(user.id)
  }, [user])

  return (
    // Render some content using Planship customer entitlements
  )
}

PlanshipCustomer context provider

The PlanshipCustomer context provider is designed to be initialized within a context where the Planship customer ID (typically your current user) is known, and it makes consuming customer level data like entitlements much easier.

With @planship/react added to your project, initialize PlanshipCustomerProvider using withPlanshipCustomerProvider, and wrap your components with it.

import { withPlanshipCustomerProvider } from '@planship/react'

function App() {
  const user = getCurrentUser() //
  const PlanshipCustomerProvider = withPlanshipCustomerProvider(
    {
      slug: 'clicker-demo',           // Planship product slug
      customerId: user.id,            // Planship customer ID
      getAccessToken: getAccessToken, // function that returns a valid Planship token
    }
  )

  return (
    <PlanshipCustomerProvider>
      <Page />
    </PlanshipCustomerProvider>
  )
}

Then, consume the PlanshipCustomer context in any of the nested components:

import { usePlanshipCustomer } from '@planship/react'

export default function YourComponent({ children }) {
  const { entitlements } = usePlanshipCustomer()

  return (
    // Render some content using Planship customer entitlements
  )
}

To fetch additional data from Planship, use an instance of the Planship Customer API client exposed by the PlanshipCustomer context. The example below shows how a list of customer subscriptions can be fetched from Planship.

import { usePlanshipCustomer } from '@planship/react'

export default function SubscriptionInfoComponent({ children }) {
  const { planshipCustomerApiClient } = usePlanshipCustomer()

  // React state for Planship subscriptions
  const [subscriptions, setSubscriptions ] = useState(() => [])

  // useEffect hook to retrieve subscriptions via the Planship API
  useEffect(() => {
    async function fetchSubscriptions() {
      // No customer ID is required in PlanshipCustomer API client calls
      planshipCustomerApiClient.listSubscriptions().then((s) => setSubscriptions(s))
    }

    fetchSubscriptions()
  }, [planshipCustomerApiClient])

  return (
    // Render a list of Planship customer subscriptions
  )
}

Typed entitlements with PlanshipCustomer context provider

When working with the entitlements dictionary returned by the usePlanshipCustomer hook, it can be useful to wrap it in an object with getters for individual levers.

import { usePlanshipCustomer, EntitlementsBase } from '@planship/react'

class Entitlements extends EntitlementsBase {
  get apiCallsPerMonth(): number {
    return this.entitlementsDict?.['api-calls-per-month'].valueOf()
  }

  get advancedAnalytics(): boolean {
    return this.entitlementsDict?.['advanced-analytics']
  }
}

export default function YourComponent({ children }) {
  const { entitlements } = usePlanshipCustomer<Entitlements>(Entitlements)

  // entitlements object is an instance of the Entitlements class
  // with apiCallsPerMonth and advancedAnalytics properties

  if (advancedAnalytics.advancedAnalytics) {
    // do something if advancedAnalytics are enabled
  }
}

This is especially advantageous in IDEs like VS Code where it enables autocomplete for entitlements.

Using Planship providers in a Next.js app

By default, all components in a Next.js app are React Server Components. React Server Components don't allow for consuming React context directly, so Planship context providers cannot by used as is. To address this, simply wrap them in your own Client Component as outlined in this official guide.

'use client'

import { withPlanshipCustomerProvider } from '@planship/react'

export default function PlanshipProvider({
  children,
}: {
  children: React.ReactNode
}) {
  const PlanshipCustomerProvider = withPlanshipCustomerProvider(
    {
      slug: 'clicker-demo',           // Planship product slug
      getAccessToken: getAccessToken, // function that returns a Planship access token
    },
  )

  // current user context provided by CurrentUserProvider
  const currentUser = useCurrentUser()

  return (
    <PlanshipCustomerProvider customerId={currentUser.id}>
      {children}
    </PlanshipCustomerProvider>
  )
}

Then, you can use the new PlanshipCustomerProvider component in your Next.js app, E.g. in your root layout.

export default async function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html>
      <body>
        <CurrentUserProvider>
          <PlanshipProvider>
            {children}
          </PlanshipProvider>
        </CurrentUserProvider>
      </body>
    </html>
  );
}

This pattern can be explored in more detail in our Next.js example app at https://github.com/planship/planship-nextjs-example

Links