0.10.14 • Published 8 months ago

@flowglad/nextjs v0.10.14

Weekly downloads
-
License
-
Repository
-
Last release
8 months ago

@flowglad/nextjs

A Next.js integration package for FlowGlad, providing utilities and components for both client and server-side functionality.

Installation

npm install @flowglad/nextjs
# or
yarn add @flowglad/nextjs
# or
pnpm add @flowglad/nextjs

Requirements

  • React 18 or 19
  • Next.js 14 or 15

Usage

Client-side Components

Import client-side components and utilities directly from the package:

import { Component } from '@flowglad/nextjs';

Server-side Code

For server-side code, use the dedicated server import path:

import { serverFunction } from '@flowglad/nextjs/server';

This separation ensures proper code splitting and prevents server-only code from being included in client bundles.

Features

  • App Router Support
  • Pages Router Support
  • Type-safe route handlers
  • Server-side utilities
  • Client-side components
  • React Context for billing and subscription management

API Reference

React Context

The package provides a React context for managing billing and subscription state. Here's how to use it:

// In your app's root layout or page
import { FlowgladProvider } from '@flowglad/nextjs';

export default function RootLayout({ children }) {
  return (
    <FlowgladProvider
      serverRoute="/api/flowglad" // Your Flowglad API route
      loadBilling={true} // Set to true to load billing data
      theme={{
        mode: 'system', // 'light' | 'dark' | 'system'
        // Optional theme customization
      }}
    >
      {children}
    </FlowgladProvider>
  );
}

// In your components
import { useBilling } from '@flowglad/nextjs';

function BillingComponent() {
  const { 
    customer,
    subscriptions,
    paymentMethods,
    createCheckoutSession,
    cancelSubscription,
    loaded,
    errors 
  } = useBilling();

  if (!loaded) {
    return <div>Loading...</div>;
  }

  if (errors) {
    return <div>Error: {errors[0].message}</div>;
  }

  return (
    <div>
      <h2>Current Subscriptions</h2>
      {subscriptions?.map(sub => (
        <div key={sub.id}>
          {sub.name} - {sub.status}
          <button onClick={() => cancelSubscription({ subscriptionId: sub.id })}>
            Cancel
          </button>
        </div>
      ))}
      
      <button onClick={() => createCheckoutSession({
        successUrl: 'https://your-app.com/success',
        cancelUrl: 'https://your-app.com/cancel',
        priceId: 'price_123'
      })}>
        Subscribe
      </button>
    </div>
  );
}

Route Handlers

App Router

import { createAppRouterRouteHandler } from '@flowglad/nextjs/server';
import { FlowgladServer } from '@flowglad/nextjs/server';

// Create your FlowgladServer instance
const flowgladServer = new FlowgladServer();

// Create the route handler
const handler = createAppRouterRouteHandler(flowgladServer);

// Export the handler for the HTTP method you want to support
export const GET = handler;
export const POST = handler;
// etc...

Pages Router

import { createPagesRouterRouteHandler } from '@flowglad/nextjs/server';
import { FlowgladServer } from '@flowglad/nextjs/server';

// Create your FlowgladServer instance
const flowgladServer = new FlowgladServer();

// Create the route handler
const handler = createPagesRouterRouteHandler(flowgladServer);

// Export the handler as the default export
export default handler;

The route handlers will automatically:

  • Parse the request path from the URL
  • Handle query parameters (normalized for Pages Router)
  • Process request bodies for non-GET requests

License

This project is licensed under the MIT License - see the LICENSE file for details.

0.10.14

8 months ago

0.10.13

8 months ago

0.10.12

8 months ago

0.10.11

8 months ago

0.10.10

8 months ago

0.10.9

8 months ago

0.10.8

9 months ago

0.10.7

9 months ago

0.10.6

10 months ago

0.10.5

10 months ago

0.10.4

10 months ago

0.10.3

10 months ago

0.10.2

10 months ago

0.10.1

10 months ago

0.10.0

10 months ago

0.9.1

10 months ago

0.9.0

10 months ago

0.8.13

10 months ago

0.8.12

10 months ago

0.8.11

10 months ago

0.8.10

10 months ago

0.8.9

10 months ago

0.8.8

10 months ago

0.8.7

10 months ago

0.8.6

10 months ago

0.8.5

10 months ago

0.8.4

10 months ago

0.8.3

10 months ago

0.8.2

10 months ago

0.8.1

10 months ago

0.8.0

11 months ago

0.7.0

11 months ago

0.6.0

11 months ago

0.5.0

11 months ago

0.4.22

11 months ago

0.4.21

11 months ago

0.4.20

11 months ago

0.4.19

11 months ago

0.4.18

11 months ago

0.4.17

11 months ago

0.4.16

11 months ago

0.4.15

11 months ago

0.4.14

11 months ago

0.4.13

11 months ago

0.4.12

11 months ago

0.4.11

11 months ago

0.4.10

11 months ago

0.4.9

11 months ago

0.4.8

11 months ago

0.4.7

11 months ago

0.4.6

11 months ago

0.4.5

11 months ago

0.4.4

11 months ago

0.4.3

11 months ago

0.4.2

11 months ago

0.4.1

11 months ago

0.4.0

11 months ago

0.2.4

11 months ago

0.2.3

11 months ago

0.2.1

11 months ago

0.2.0

11 months ago

0.0.1

1 year ago