Skip to content

Commit de13740

Browse files
abarker-launchdarklynosnibor89robinson-mdRobinson Marquez
authored
feat: add svelte example project and documentation (#852)
The contributed work was already reviewed in the original PR (#722) which was merged into this temporary holding branch to: - Standardize the README files to be consistent with our other documentation - Add the build/test github action workflow for the Svelte SDK Only the changes made in commit 79b42ae need to be reviewed. --------- Co-authored-by: Robinson Marquez <[email protected]> Co-authored-by: Robinson Marquez <[email protected]> Co-authored-by: Robinson Marquez <[email protected]>
1 parent c985daa commit de13740

File tree

22 files changed

+466
-7
lines changed

22 files changed

+466
-7
lines changed

.github/workflows/svelte.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: sdk/svelte
2+
3+
on:
4+
push:
5+
branches: [main, 'feat/**']
6+
paths-ignore:
7+
- '**.md' #Do not need to run CI for markdown changes.
8+
pull_request:
9+
branches: [main, 'feat/**']
10+
paths-ignore:
11+
- '**.md'
12+
13+
jobs:
14+
build-test-svelte:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
- uses: actions/setup-node@v4
19+
- id: shared
20+
name: Shared CI Steps
21+
uses: ./actions/ci
22+
with:
23+
workspace_name: '@launchdarkly/svelte-client-sdk'
24+
workspace_path: packages/sdk/svelte
25+
should_build_docs: false

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"packages/sdk/react-universal/example",
1818
"packages/sdk/vercel",
1919
"packages/sdk/svelte",
20+
"packages/sdk/svelte/example",
2021
"packages/sdk/akamai-base",
2122
"packages/sdk/akamai-base/example",
2223
"packages/sdk/akamai-edgekv",

packages/sdk/svelte/README.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# LaunchDarkly Svelte Client SDK
2+
3+
# ⛔️⛔️⛔️⛔️
4+
5+
> [!CAUTION]
6+
> This library is a alpha version and should not be considered ready for production use while this message is visible.
7+
8+
This is the Svelte Client SDK for LaunchDarkly. It is a wrapper around the LaunchDarkly JavaScript SDK but with a Svelte-friendly API.
9+
10+
## Getting started
11+
12+
First, install the package:
13+
14+
```bash
15+
# npm
16+
npm install @launchdarkly/svelte-client-sdk
17+
18+
# yarn
19+
yarn add @launchdarkly/svelte-client-sdk
20+
```
21+
22+
Then, initialize the SDK with your client-side ID using the `LDProvider` component:
23+
24+
```svelte
25+
<script>
26+
import { LDProvider } from '@launchdarkly/svelte-client-sdk';
27+
import App from './App.svelte';
28+
</script>
29+
30+
// Use context relevant to your application
31+
const context = {
32+
kind: 'user',
33+
key: 'user-key',
34+
};
35+
36+
<LDProvider clientID="your-client-side-id" {context}>
37+
<App />
38+
</LDProvider>
39+
```
40+
41+
Now you can use the `LDFlag` component to conditionally render content based on feature flags:
42+
43+
```svelte
44+
<script>
45+
import { LDFlag } from '@launchdarkly/svelte-client-sdk';
46+
</script>
47+
48+
<LDFlag flag={'my-feature-flag'}>
49+
<div slot="on">
50+
<p>this will render if the feature flag is on</p>
51+
</div>
52+
<div slot="off">
53+
<p>this will render if the feature flag is off</p>
54+
</div>
55+
</LDFlag>
56+
```
57+
58+
## Advanced usage
59+
60+
### Changing user context
61+
62+
You can change the user context by using the `identify` function from the `LD` object:
63+
64+
```svelte
65+
<script>
66+
import { LD } from '@launchdarkly/svelte-client-sdk';
67+
68+
function handleLogin() {
69+
LD.identify({ key: 'new-user-key' });
70+
}
71+
</script>
72+
73+
<button on:click={handleLogin}>Login</button>
74+
```
75+
76+
### Getting feature flag values
77+
78+
#### Getting immediate flag value
79+
80+
If you need to get the value of a flag at time of evaluation you can use the `useFlag` function:
81+
82+
```svelte
83+
<script>
84+
import { LD } from '@launchdarkly/svelte-client-sdk';
85+
86+
function handleClick() {
87+
const isFeatureFlagOn = LD.useFlag('my-feature-flag', false);
88+
console.log(isFeatureFlagOn);
89+
}
90+
</script>
91+
92+
<button on:click={handleClick}>Check flag value</button>
93+
```
94+
95+
**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.
96+
97+
#### Watching flag value changes
98+
99+
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:
100+
101+
```svelte
102+
<script>
103+
import { LD } from '@launchdarkly/svelte-client-sdk';
104+
105+
$: flagValue = LD.watch('my-feature-flag');
106+
</script>
107+
108+
<p>{$flagValue}</p>
109+
```
110+
111+
#### Getting all flag values
112+
113+
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:
114+
115+
```svelte
116+
<script>
117+
import { LD } from '@launchdarkly/svelte-client-sdk';
118+
119+
$: allFlags = LD.flags;
120+
</script>
121+
122+
{#each Object.keys($allFlags) as flagName}
123+
<p>{flagName}: {$allFlags[flagName]}</p>
124+
{/each}
125+
```
126+
127+
## About LaunchDarkly
128+
129+
- 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:
130+
- 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.
131+
- 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?).
132+
- 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.
133+
- 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).
134+
- Disable parts of your application to facilitate maintenance, without taking everything offline.
135+
- 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.
136+
- Explore LaunchDarkly
137+
- [launchdarkly.com](https://www.launchdarkly.com/ 'LaunchDarkly Main Website') for more information
138+
- [docs.launchdarkly.com](https://docs.launchdarkly.com/ 'LaunchDarkly Documentation') for our documentation and SDK reference guides
139+
- [apidocs.launchdarkly.com](https://apidocs.launchdarkly.com/ 'LaunchDarkly API Documentation') for our API documentation
140+
- [blog.launchdarkly.com](https://blog.launchdarkly.com/ 'LaunchDarkly Blog Documentation') for the latest product updates
141+
142+
## Credits
143+
144+
- Original Svelte SDK code contributed by [Robinson Marquez](https://github.com/nosnibor89)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.DS_Store
2+
node_modules
3+
/build
4+
/.svelte-kit
5+
/package
6+
.env
7+
.env.*
8+
!.env.example
9+
10+
# Ignore files for PNPM, NPM and YARN
11+
pnpm-lock.yaml
12+
package-lock.json
13+
yarn.lock
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/** @type { import("eslint").Linter.Config } */
2+
module.exports = {
3+
root: true,
4+
extends: [
5+
'eslint:recommended',
6+
'plugin:@typescript-eslint/recommended',
7+
'plugin:svelte/recommended',
8+
'prettier'
9+
],
10+
parser: '@typescript-eslint/parser',
11+
plugins: ['@typescript-eslint'],
12+
parserOptions: {
13+
sourceType: 'module',
14+
ecmaVersion: 2020,
15+
extraFileExtensions: ['.svelte']
16+
},
17+
env: {
18+
browser: true,
19+
es2017: true,
20+
node: true
21+
},
22+
overrides: [
23+
{
24+
files: ['*.svelte'],
25+
parser: 'svelte-eslint-parser',
26+
parserOptions: {
27+
parser: '@typescript-eslint/parser'
28+
}
29+
}
30+
]
31+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.DS_Store
2+
node_modules
3+
/build
4+
/dist
5+
/.svelte-kit
6+
/package
7+
.env
8+
.env.*
9+
!.env.example
10+
vite.config.js.timestamp-*
11+
vite.config.ts.timestamp-*

packages/sdk/svelte/example/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
engine-strict=true
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Ignore files for PNPM, NPM and YARN
2+
pnpm-lock.yaml
3+
package-lock.json
4+
yarn.lock
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"useTabs": true,
3+
"singleQuote": true,
4+
"trailingComma": "none",
5+
"printWidth": 100,
6+
"plugins": ["prettier-plugin-svelte"],
7+
"overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }]
8+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# LaunchDarkly Svelte Client SDK Example
2+
3+
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.
4+
5+
## Installing Dependencies and Setting Environment Variables
6+
7+
First, install the project dependencies:
8+
9+
```bash
10+
# npm
11+
npm install
12+
13+
# yarn
14+
yarn install
15+
```
16+
17+
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.
18+
19+
```bash
20+
PUBLIC_LD_CLIENT_ID=your-client-side-id
21+
PUBLIC_LD_FLAG_KEY=your-flag-key
22+
```
23+
24+
Note: The flag specified by `PUBLIC_LD_FLAG_KEY` must be a boolean flag.
25+
26+
## Running the Project
27+
28+
To run the project, use the following command:
29+
30+
```bash
31+
# npm
32+
npm run dev
33+
34+
# yarn
35+
yarn dev
36+
```
37+
38+
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`.
39+
40+
### Role of `LDProvider`
41+
42+
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.
43+
44+
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.

0 commit comments

Comments
 (0)