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

@michaelbelgium/adonisjs-referral

v1.0.0

Published

A user referral system for AdonisJS

Downloads

8

Readme

AdonisJS referrals

A referral system for AdonisJS projects

This is a simple package that handles referrals on the back-end.

Installation

Execute the following in the root of your adonisjs project:

node ace add @michaelbelgium/adonisjs-referral

This will:

  • create a config file config/referrals.ts
  • add a migration for 2 tables in the migration folder
  • add the referral_provider to providers
  • regsiter referred_middelware as named middleware in your project
  • add the referral.link route (/referral/:code)

Next is to migrate the 2 tables:

node ace migration:run

This will add table referral_codes and referrals

Configuration

The configuration file has some options that can be edited. Each option has a comment in case you don't know what it does.

See the stub file or the config/referrals.ts file after adding the package.

Before you're starting with referrals, make sure the configuration is absolutely correct.

hasReferrals mixin

The LucidModel you've set in the userModel setting must have the hasReferrals mixin added.

For example if you have the User model set:

import { compose } from '@adonisjs/core/helpers'
import { hasReferrals } from '@michaelbelgium/adonisjs-referral'

export default class User extends compose(BaseModel, hasReferrals) {
  //...
}

This will add an afterCreate hook and a relationship to the model:

@hasOne(() => ReferralCode, { foreignKey: 'userId' })
declare referralCode: HasOne<typeof ReferralCode>

@afterCreate()
static async createReferralCode(model: ModelWithReferrals) {
  if (config.get<boolean>('referrals.referralCode.autoCreate', true)) {
    model.related('referralCode').create({})
  }
}

Referrals vs referral codes

The package does not know when you want to assign a user (the referee) to the user who shared the referral link (the referrer). It's your decision as it could be after registering, after paying a subscription, when ever.

So while the package handles saving and creating of referral codes, you must add and assign referrals yourself.

This is why, also for flexiblity, the ReferralCode and Referral model from the package can be used.

Events

The package provides 2 events you can listen to.

  • referral:code_created: emitted when a referral code is created
  • referral:visited: emitted when a referral code is visited through the referral link.

Middleware

You can use the referred middleware to only allow referred users on a route.

router.get('/only-referrals', async ({ }: HttpContext) => {
    return 'Hello referred user!'
}).middleware(middleware.referred())

The middleware basicly checks if the user has the referred cookie set. Will show unauthorized HTTP error when not having the cookie.