Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/svelte.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: sdk/svelte

on:
push:
branches: [main, 'feat/**']
paths-ignore:
- '**.md' #Do not need to run CI for markdown changes.
pull_request:
branches: [main, 'feat/**']
paths-ignore:
- '**.md'

jobs:
build-test-svelte:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- id: shared
name: Shared CI Steps
uses: ./actions/ci
with:
workspace_name: '@launchdarkly/svelte-client-sdk'
workspace_path: packages/sdk/svelte
should_build_docs: false
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"packages/sdk/react-universal/example",
"packages/sdk/vercel",
"packages/sdk/svelte",
"packages/sdk/svelte/example",
"packages/sdk/akamai-base",
"packages/sdk/akamai-base/example",
"packages/sdk/akamai-edgekv",
Expand Down
144 changes: 144 additions & 0 deletions packages/sdk/svelte/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# LaunchDarkly Svelte Client SDK

# ⛔️⛔️⛔️⛔️

> [!CAUTION]
> This library is a alpha version and should not be considered ready for production use while this message is visible.

This is the Svelte Client SDK for LaunchDarkly. It is a wrapper around the LaunchDarkly JavaScript SDK but with a Svelte-friendly API.

## Getting started

First, install the package:

```bash
# npm
npm install @launchdarkly/svelte-client-sdk

# yarn
yarn add @launchdarkly/svelte-client-sdk
```

Then, initialize the SDK with your client-side ID using the `LDProvider` component:

```svelte
<script>
import { LDProvider } from '@launchdarkly/svelte-client-sdk';
import App from './App.svelte';
</script>

// Use context relevant to your application
const context = {
kind: 'user',
key: 'user-key',
};

<LDProvider clientID="your-client-side-id" {context}>
<App />
</LDProvider>
```

Now you can use the `LDFlag` component to conditionally render content based on feature flags:

```svelte
<script>
import { LDFlag } from '@launchdarkly/svelte-client-sdk';
</script>

<LDFlag flag={'my-feature-flag'}>
<div slot="on">
<p>this will render if the feature flag is on</p>
</div>
<div slot="off">
<p>this will render if the feature flag is off</p>
</div>
</LDFlag>
```

## Advanced usage

### Changing user context

You can change the user context by using the `identify` function from the `LD` object:

```svelte
<script>
import { LD } from '@launchdarkly/svelte-client-sdk';

function handleLogin() {
LD.identify({ key: 'new-user-key' });
}
</script>

<button on:click={handleLogin}>Login</button>
```

### Getting feature flag values

#### Getting immediate flag value

If you need to get the value of a flag at time of evaluation you can use the `useFlag` function:

```svelte
<script>
import { LD } from '@launchdarkly/svelte-client-sdk';

function handleClick() {
const isFeatureFlagOn = LD.useFlag('my-feature-flag', false);
console.log(isFeatureFlagOn);
}
</script>

<button on:click={handleClick}>Check flag value</button>
```

**Note:** Please note that `useFlag` function will return the current value of the flag at the time of evaluation, which means you won't get notified if the flag value changes. This is useful for cases where you need to get the value of a flag at a specific time like a function call. If you need to get notified when the flag value changes, you should use the `LDFlag` component, the `watch` function or the `flags` object depending on your use case.

#### Watching flag value changes

If you need to get notified when a flag value changes you can use the `watch` function. The `watch` function is an instance of [Svelte Store](https://svelte.dev/docs/svelte-store), which means you can use it with the `$` store subscriber syntax or the `subscribe` method. Here is an example of how to use the `watch` function:

```svelte
<script>
import { LD } from '@launchdarkly/svelte-client-sdk';

$: flagValue = LD.watch('my-feature-flag');
</script>

<p>{$flagValue}</p>
```

#### Getting all flag values

If you need to get all flag values you can use the `flags` object. The `flags` object is an instance of [Svelte Store](https://svelte.dev/docs/svelte-store), which means you can use it with the `$` store subscriber syntax or the `subscribe` method. Here is an example of how to use the `flags` object:

```svelte
<script>
import { LD } from '@launchdarkly/svelte-client-sdk';

$: allFlags = LD.flags;
</script>

{#each Object.keys($allFlags) as flagName}
<p>{flagName}: {$allFlags[flagName]}</p>
{/each}
```

## About LaunchDarkly

- LaunchDarkly is a continuous delivery platform that provides feature flags as a service and allows developers to iterate quickly and safely. We allow you to easily flag your features and manage them from the LaunchDarkly dashboard. With LaunchDarkly, you can:
- Roll out a new feature to a subset of your users (like a group of users who opt-in to a beta tester group), gathering feedback and bug reports from real-world use cases.
- Gradually roll out a feature to an increasing percentage of users, and track the effect that the feature has on key metrics (for instance, how likely is a user to complete a purchase if they have feature A versus feature B?).
- Turn off a feature that you realize is causing performance problems in production, without needing to re-deploy, or even restart the application with a changed configuration file.
- Grant access to certain features based on user attributes, like payment plan (eg: users on the ‘gold’ plan get access to more features than users in the ‘silver’ plan).
- Disable parts of your application to facilitate maintenance, without taking everything offline.
- LaunchDarkly provides feature flag SDKs for a wide variety of languages and technologies. Read [our documentation](https://docs.launchdarkly.com/sdk) for a complete list.
- Explore LaunchDarkly
- [launchdarkly.com](https://www.launchdarkly.com/ 'LaunchDarkly Main Website') for more information
- [docs.launchdarkly.com](https://docs.launchdarkly.com/ 'LaunchDarkly Documentation') for our documentation and SDK reference guides
- [apidocs.launchdarkly.com](https://apidocs.launchdarkly.com/ 'LaunchDarkly API Documentation') for our API documentation
- [blog.launchdarkly.com](https://blog.launchdarkly.com/ 'LaunchDarkly Blog Documentation') for the latest product updates

## Credits

- Original Svelte SDK code contributed by [Robinson Marquez](https://github.com/nosnibor89)
13 changes: 13 additions & 0 deletions packages/sdk/svelte/example/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.DS_Store
node_modules
/build
/.svelte-kit
/package
.env
.env.*
!.env.example

# Ignore files for PNPM, NPM and YARN
pnpm-lock.yaml
package-lock.json
yarn.lock
31 changes: 31 additions & 0 deletions packages/sdk/svelte/example/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/** @type { import("eslint").Linter.Config } */
module.exports = {
root: true,
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:svelte/recommended',
'prettier'
],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
parserOptions: {
sourceType: 'module',
ecmaVersion: 2020,
extraFileExtensions: ['.svelte']
},
env: {
browser: true,
es2017: true,
node: true
},
overrides: [
{
files: ['*.svelte'],
parser: 'svelte-eslint-parser',
parserOptions: {
parser: '@typescript-eslint/parser'
}
}
]
};
11 changes: 11 additions & 0 deletions packages/sdk/svelte/example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.DS_Store
node_modules
/build
/dist
/.svelte-kit
/package
.env
.env.*
!.env.example
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
1 change: 1 addition & 0 deletions packages/sdk/svelte/example/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
4 changes: 4 additions & 0 deletions packages/sdk/svelte/example/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore files for PNPM, NPM and YARN
pnpm-lock.yaml
package-lock.json
yarn.lock
8 changes: 8 additions & 0 deletions packages/sdk/svelte/example/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"useTabs": true,
"singleQuote": true,
"trailingComma": "none",
"printWidth": 100,
"plugins": ["prettier-plugin-svelte"],
"overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }]
}
44 changes: 44 additions & 0 deletions packages/sdk/svelte/example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# LaunchDarkly Svelte Client SDK Example

This example project demonstrates the usage of the `@launchdarkly/svelte-client-sdk`. It showcases how to conditionally render content based on feature flags using the `LDFlag` component.

## Installing Dependencies and Setting Environment Variables

First, install the project dependencies:

```bash
# npm
npm install

# yarn
yarn install
```

Next, create a `.env` file in the root of the project and add your LaunchDarkly client-side ID and flag key. You can obtain these from any LaunchDarkly project/environment you choose.

```bash
PUBLIC_LD_CLIENT_ID=your-client-side-id
PUBLIC_LD_FLAG_KEY=your-flag-key
```

Note: The flag specified by `PUBLIC_LD_FLAG_KEY` must be a boolean flag.

## Running the Project

To run the project, use the following command:

```bash
# npm
npm run dev

# yarn
yarn dev
```

This will start the development server. Open your browser and navigate to the provided URL to see the example in action. The box will change its background color based on the value of the feature flag specified by `PUBLIC_LD_FLAG_KEY`.

### Role of `LDProvider`

The `LDProvider` component is used to initialize the LaunchDarkly client and provide the feature flag context to the rest of the application. It requires a `clientID` and a `context` object. The `context` object typically contains information about the user or environment, which LaunchDarkly uses to determine the state of feature flags.

In this example, the `LDProvider` wraps the entire application, ensuring that all child components have access to the feature flag data. The `slot="initializing"` is used to display a loading message while the flags are being fetched.
49 changes: 49 additions & 0 deletions packages/sdk/svelte/example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "ld-svelte-example",
"version": "0.0.1",
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview"
},
"exports": {
".": {
"types": "./dist/index.d.ts",
"svelte": "./dist/index.js",
"default": "./dist/index.js"
}
},
"files": [
"dist",
"!dist/**/*.test.*",
"!dist/**/*.spec.*"
],
"svelte": "./dist/index.js",
"types": "./dist/index.d.ts",
"type": "module",
"dependencies": {
"@launchdarkly/svelte-client-sdk": "workspace:^",
"esm-env": "^1.0.0",
"svelte": "^5.4.0"
},
"devDependencies": {
"@sveltejs/adapter-auto": "^3.0.0",
"@sveltejs/kit": "^2.0.0",
"@sveltejs/package": "^2.0.0",
"@sveltejs/vite-plugin-svelte": "^5.0.1",
"@types/eslint": "8.56.0",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-svelte": "^2.35.1",
"jsdom": "^24.0.0",
"prettier": "^3.1.1",
"prettier-plugin-svelte": "^3.1.2",
"svelte-check": "^3.6.0",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
"vite": "^6.0.2",
"vitest": "^2.1.8"
}
}
13 changes: 13 additions & 0 deletions packages/sdk/svelte/example/src/app.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// See https://kit.svelte.dev/docs/types#app
// for information about these interfaces
declare global {
namespace App {
// interface Error {}
// interface Locals {}
// interface PageData {}
// interface PageState {}
// interface Platform {}
}
}

export {};
12 changes: 12 additions & 0 deletions packages/sdk/svelte/example/src/app.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%sveltekit.head%
</head>
<body data-sveltekit-preload-data="hover">
<div>%sveltekit.body%</div>
</body>
</html>
7 changes: 7 additions & 0 deletions packages/sdk/svelte/example/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { describe, it, expect } from 'vitest';

describe('sum test', () => {
it('adds 1 + 2 to equal 3', () => {
expect(1 + 2).toBe(3);
});
});
Loading