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

@fireact.dev/saas-cloud-functions

v1.0.6

Published

Firebase Cloud Functions for SaaS applications, providing Stripe integration, subscription management, and automated billing workflows. Essential backend functionality for @fireact.dev/saas applications.

Downloads

58

Readme

@fireact.dev/saas-cloud-functions

Cloud Functions for fireact.dev SaaS applications.

Prerequisites

  • Firebase project with Blaze (pay-as-you-go) plan
  • Stripe account for payment processing
  • Firebase CLI installed (npm install -g firebase-tools)

Installation

Go to your functions folder.

npm install @fireact.dev/saas-cloud-functions

Install the required peer dependencies:

npm install firebase-admin firebase-functions stripe

Setup

1. Initialize Firebase Functions

If you haven't already, initialize Firebase Functions in your project:

firebase init functions

Select TypeScript when prompted.

2. Create saasConfig.json

Create src/saasConfig.json in your functions directory:

{
    "stripe": {
        "secret_api_key": "your_stripe_secret_key",
        "end_point_secret": "your_stripe_webhook_endpoint_secret"
    },
    "emulators": {
        "enabled": false,
        "useTestKeys": false
    },
    "plans": [
        {
            "id": "free",
            "titleKey": "plans.free.title",
            "popular": false,
            "priceIds": [
                "your_stripe_price_id"
            ],
            "currency": "$",
            "price": 0,
            "frequency": "month",
            "descriptionKeys": [
                "plans.free.feature1",
                "plans.free.feature2",
                "plans.free.feature3"
            ],
            "free": true,
            "legacy": false
        }
    ],
    "permissions": {
        "access": {
            "label": "Access",
            "default": true,
            "admin": false
        },
        "admin": {
            "label": "Administrator",
            "default": false,
            "admin": true
        }
    }
}

3. Update index.ts

Create or update your functions' src/index.ts:

import { initializeApp } from 'firebase-admin/app';

// Initialize Firebase Admin
initializeApp();

import type { Plan, Permission } from '@fireact.dev/saas-cloud-functions';
import configFile from './saasConfig.json';

declare global {
    var saasConfig: {
        stripe: {
            secret_api_key: string;
            end_point_secret: string;
        };
        emulators: {
            enabled: boolean;
            useTestKeys: boolean;
        };
        plans: Plan[];
        permissions: Record<string, Permission>;
    };
}

// Initialize global config
global.saasConfig = configFile;

// Import cloud functions after config is initialized
import {
  createSubscription,
  createInvite,
  getSubscriptionUsers,
  acceptInvite,
  rejectInvite,
  revokeInvite,
  removeUser,
  updateUserPermissions,
  stripeWebhook,
  changeSubscriptionPlan,
  cancelSubscription,
  getPaymentMethods,
  createSetupIntent,
  setDefaultPaymentMethod,
  deletePaymentMethod,
  updateBillingDetails,
  getBillingDetails,
  transferSubscriptionOwnership
} from '@fireact.dev/saas-cloud-functions';

// Export cloud functions
export {
  createSubscription,
  createInvite,
  getSubscriptionUsers,
  acceptInvite,
  rejectInvite,
  revokeInvite,
  removeUser,
  updateUserPermissions,
  stripeWebhook,
  changeSubscriptionPlan,
  cancelSubscription,
  getPaymentMethods,
  createSetupIntent,
  setDefaultPaymentMethod,
  deletePaymentMethod,
  updateBillingDetails,
  getBillingDetails,
  transferSubscriptionOwnership
}

Rewrite tsconfig.json as the example below.

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017",
    "resolveJsonModule": true,
    "esModuleInterop": true
  },
  "compileOnSave": true,
  "include": [
    "src"
  ]
}

4. Stripe Setup

  1. Create a Stripe account at https://stripe.com
  2. Get your secret API key from the Stripe Dashboard
  3. Set up webhook endpoint:
    • Go to Stripe Dashboard > Developers > Webhooks
    • Add endpoint: https://your-region-your-project.cloudfunctions.net/stripeWebhook
    • Select events to listen for:
      • customer.subscription.created
      • customer.subscription.updated
      • customer.subscription.deleted
      • invoice.created
      • invoice.paid
      • invoice.overdue
      • invoice.payment_failed
      • invoice.updated
    • Copy the signing secret to use as end_point_secret in saasConfig.json

5. Deploy to Firebase

  1. Build your functions:
npm run build
  1. Deploy to Firebase:
firebase deploy --only functions

Available Functions

Subscription Management

  • createSubscription - Create a new subscription
  • changeSubscriptionPlan - Change subscription plan
  • cancelSubscription - Cancel subscription
  • transferSubscriptionOwnership - Transfer subscription ownership

User Management

  • createInvite - Create user invitation
  • acceptInvite - Accept invitation
  • rejectInvite - Reject invitation
  • revokeInvite - Revoke invitation
  • removeUser - Remove user from subscription
  • updateUserPermissions - Update user permissions
  • getSubscriptionUsers - Get subscription users

Payment Management

  • getPaymentMethods - Get payment methods
  • createSetupIntent - Create Stripe setup intent
  • setDefaultPaymentMethod - Set default payment method
  • deletePaymentMethod - Delete payment method
  • updateBillingDetails - Update billing details
  • getBillingDetails - Get billing details

Webhooks

  • stripeWebhook - Handle Stripe webhook events

Testing with Firebase Emulators

  1. Update saasConfig.json:
{
    "emulators": {
        "enabled": true,
        "useTestKeys": true
    }
}
  1. Start emulators:
firebase emulators:start

Troubleshooting

  • Deploy error: functions predeploy error: Command terminated with non-zero exit code 2 To resolve this error, remove "npm --prefix \"$RESOURCE_DIR\" run lint", from the firebase.json file.

License

MIT