nuxt-launch-darkly

A Nuxt module for interacting with the Launch Darkly SDK

Usage no npm install needed!

<script type="module">
  import nuxtLaunchDarkly from 'https://cdn.skypack.dev/nuxt-launch-darkly';
</script>

README

nuxt-launch-darkly

A Nuxt module for interacting with the Launch Darkly SDK

Features

Setup

  1. Install nuxt-launch-darkly
npm install nuxt-launch-darkly or # yarn add nuxt-launch-darkly
  1. Add it as a buildModule in nuxt.config.ts and configure it with your Launch Darkly server-side SDK key. The config should go in privateRuntimeConfig since the Launch Darkly SDK Key needs to be kept private.
export default defineNuxtConfig({
  buildModules: ['nuxt-launch-darkly'],
  privateRuntimeConfig: {
    launchDarkly: {
      addPlugin: true, // optional, default false
      sdkKey: process.env.LD_SDK_KEY // required, your LD SDK key
    }
  }
})

Usage

🧩 Composables

<script setup async>
  const USER = {
    key: 'UNIQUE_USER_ID',
    email: 'user@domain.com' // optional
  }
  const FLAG_KEY = 'my-feature-flag'

  const { getAllVariations, getVariationByKey } = useLaunchDarkly()

  // get all variations for the provided user
  const allFlags = await getAllVariations(USER)
  // get a specified variation for the provided user
  const singleFlag = await getVariationByKey(USER,   FLAG_KEY)
</script>

🔌 Plugin

<script setup async>
  const USER = {
    key: 'UNIQUE_USER_ID',
    email: 'user@domain.com' // optional
  }
  const FLAG_KEY = 'my-feature-flag'

  const { $launchDarkly } = useNuxtApp()

  // get all variations for the provided user
  const allFlags = await $launchDarkly.getAllVariations(USER)
  // get a specified variation for the provided user
  const singleFlag = await $launchDarkly.getVariationByKey(USER, FLAG_KEY)
</script>

🌀 REST Endpoint

This module exposes the REST endpoint that is used by the composables and the plugin internally. This could be useful if you wanted to get all the flags on page load and save them to the store for example.

Get all variants
GET /api/launch-darkly?key=xxx-xxx&email=user@domain.com
Get single variant
GET /api/launch-darkly/{variant-key}?key=xxx-xxx&email=user@domain.com

Params:

key: string
email?: string // optional

Development

  • Create a .env file in the playground directory and add a variable called LD_SDK_KEY with the value of a Launch Darkly server-side SDK Key.
  • Run npm run prepare to generate type stubs.
  • Use npm run play to start playground in development mode.