@replanejs/svelte 0.7.2 → 0.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +232 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
# @replanejs/svelte
|
|
2
|
+
|
|
3
|
+
Svelte SDK for [Replane](https://github.com/replane-dev/replane-javascript) - feature flags and remote configuration with reactive stores.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @replanejs/svelte
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @replanejs/svelte
|
|
11
|
+
# or
|
|
12
|
+
yarn add @replanejs/svelte
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Requirements
|
|
16
|
+
|
|
17
|
+
- Svelte 4.0.0 or higher (Svelte 5 supported)
|
|
18
|
+
- Node.js 18.0.0 or higher
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
```svelte
|
|
23
|
+
<script>
|
|
24
|
+
import { ReplaneProvider, useConfig } from '@replanejs/svelte';
|
|
25
|
+
import { createReplaneClient } from '@replanejs/sdk';
|
|
26
|
+
|
|
27
|
+
const client = await createReplaneClient({
|
|
28
|
+
baseUrl: 'https://your-replane-server.com',
|
|
29
|
+
sdkKey: 'your-sdk-key',
|
|
30
|
+
});
|
|
31
|
+
</script>
|
|
32
|
+
|
|
33
|
+
<ReplaneProvider {client}>
|
|
34
|
+
<MyComponent />
|
|
35
|
+
</ReplaneProvider>
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```svelte
|
|
39
|
+
<!-- MyComponent.svelte -->
|
|
40
|
+
<script>
|
|
41
|
+
import { useConfig } from '@replanejs/svelte';
|
|
42
|
+
|
|
43
|
+
const isFeatureEnabled = useConfig<boolean>('feature-flag-name');
|
|
44
|
+
</script>
|
|
45
|
+
|
|
46
|
+
{#if $isFeatureEnabled}
|
|
47
|
+
<p>Feature is enabled!</p>
|
|
48
|
+
{:else}
|
|
49
|
+
<p>Feature is disabled</p>
|
|
50
|
+
{/if}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## API
|
|
54
|
+
|
|
55
|
+
### ReplaneProvider
|
|
56
|
+
|
|
57
|
+
Provider component that makes the Replane client available to your component tree. Use this when you have a pre-created client.
|
|
58
|
+
|
|
59
|
+
```svelte
|
|
60
|
+
<script>
|
|
61
|
+
import { ReplaneProvider } from '@replanejs/svelte';
|
|
62
|
+
import { createReplaneClient } from '@replanejs/sdk';
|
|
63
|
+
|
|
64
|
+
const client = await createReplaneClient({
|
|
65
|
+
baseUrl: 'https://your-replane-server.com',
|
|
66
|
+
sdkKey: 'your-sdk-key',
|
|
67
|
+
});
|
|
68
|
+
</script>
|
|
69
|
+
|
|
70
|
+
<ReplaneProvider {client}>
|
|
71
|
+
<App />
|
|
72
|
+
</ReplaneProvider>
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### ReplaneProviderAsync
|
|
76
|
+
|
|
77
|
+
Provider component that creates and manages the client internally. Handles loading and error states.
|
|
78
|
+
|
|
79
|
+
```svelte
|
|
80
|
+
<script>
|
|
81
|
+
import { ReplaneProviderAsync } from '@replanejs/svelte';
|
|
82
|
+
|
|
83
|
+
const options = {
|
|
84
|
+
baseUrl: 'https://your-replane-server.com',
|
|
85
|
+
sdkKey: 'your-sdk-key',
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
function handleError(error) {
|
|
89
|
+
console.error('Failed to initialize Replane:', error);
|
|
90
|
+
}
|
|
91
|
+
</script>
|
|
92
|
+
|
|
93
|
+
<ReplaneProviderAsync {options} onError={handleError}>
|
|
94
|
+
<App />
|
|
95
|
+
|
|
96
|
+
{#snippet loader()}
|
|
97
|
+
<p>Loading configuration...</p>
|
|
98
|
+
{/snippet}
|
|
99
|
+
</ReplaneProviderAsync>
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### useConfig
|
|
103
|
+
|
|
104
|
+
Create a reactive store for a specific config value. The store automatically updates when the config value changes on the server.
|
|
105
|
+
|
|
106
|
+
Must be called during component initialization (in the script section, not in event handlers).
|
|
107
|
+
|
|
108
|
+
```svelte
|
|
109
|
+
<script>
|
|
110
|
+
import { useConfig } from '@replanejs/svelte';
|
|
111
|
+
|
|
112
|
+
// Basic usage - returns a Svelte readable store
|
|
113
|
+
const featureEnabled = useConfig<boolean>('featureEnabled');
|
|
114
|
+
|
|
115
|
+
// With evaluation context
|
|
116
|
+
const greeting = useConfig<string>('greeting', {
|
|
117
|
+
context: { userId: '123', isPremium: true }
|
|
118
|
+
});
|
|
119
|
+
</script>
|
|
120
|
+
|
|
121
|
+
{#if $featureEnabled}
|
|
122
|
+
<p>{$greeting}</p>
|
|
123
|
+
{/if}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### useReplane
|
|
127
|
+
|
|
128
|
+
Get direct access to the Replane client from context.
|
|
129
|
+
|
|
130
|
+
```svelte
|
|
131
|
+
<script>
|
|
132
|
+
import { useReplane } from '@replanejs/svelte';
|
|
133
|
+
|
|
134
|
+
const { client } = useReplane();
|
|
135
|
+
|
|
136
|
+
function handleClick() {
|
|
137
|
+
// Access client methods directly
|
|
138
|
+
const value = client.get('some-config');
|
|
139
|
+
console.log(value);
|
|
140
|
+
}
|
|
141
|
+
</script>
|
|
142
|
+
|
|
143
|
+
<button onclick={handleClick}>Get Config</button>
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### createConfigStore
|
|
147
|
+
|
|
148
|
+
Create a reactive store for a config value using a pre-existing client. Useful when you have direct access to a client and don't want to use the context-based approach.
|
|
149
|
+
|
|
150
|
+
```svelte
|
|
151
|
+
<script>
|
|
152
|
+
import { createConfigStore } from '@replanejs/svelte';
|
|
153
|
+
import { client } from './replane-client';
|
|
154
|
+
|
|
155
|
+
const featureEnabled = createConfigStore<boolean>(client, 'featureEnabled');
|
|
156
|
+
|
|
157
|
+
// With context
|
|
158
|
+
const userGreeting = createConfigStore<string>(client, 'greeting', {
|
|
159
|
+
context: { userId: '123' }
|
|
160
|
+
});
|
|
161
|
+
</script>
|
|
162
|
+
|
|
163
|
+
{#if $featureEnabled}
|
|
164
|
+
<p>{$userGreeting}</p>
|
|
165
|
+
{/if}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## TypeScript
|
|
169
|
+
|
|
170
|
+
The SDK is fully typed. You can provide type parameters to get type-safe configuration values:
|
|
171
|
+
|
|
172
|
+
```svelte
|
|
173
|
+
<script lang="ts">
|
|
174
|
+
import { useConfig, useReplane } from '@replanejs/svelte';
|
|
175
|
+
|
|
176
|
+
interface MyConfigs {
|
|
177
|
+
theme: 'light' | 'dark';
|
|
178
|
+
maxItems: number;
|
|
179
|
+
features: {
|
|
180
|
+
analytics: boolean;
|
|
181
|
+
notifications: boolean;
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Type-safe stores
|
|
186
|
+
const theme = useConfig<MyConfigs['theme']>('theme');
|
|
187
|
+
const maxItems = useConfig<MyConfigs['maxItems']>('maxItems');
|
|
188
|
+
|
|
189
|
+
// Typed client access
|
|
190
|
+
const { client } = useReplane<MyConfigs>();
|
|
191
|
+
</script>
|
|
192
|
+
|
|
193
|
+
<p>Theme: {$theme}, Max items: {$maxItems}</p>
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## Context Utilities
|
|
197
|
+
|
|
198
|
+
For advanced use cases, you can directly interact with the Svelte context:
|
|
199
|
+
|
|
200
|
+
```svelte
|
|
201
|
+
<script>
|
|
202
|
+
import { setReplaneContext, getReplaneContext, hasReplaneContext } from '@replanejs/svelte';
|
|
203
|
+
|
|
204
|
+
// Check if context exists
|
|
205
|
+
if (hasReplaneContext()) {
|
|
206
|
+
const { client } = getReplaneContext();
|
|
207
|
+
// Use client...
|
|
208
|
+
}
|
|
209
|
+
</script>
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## Realtime Updates
|
|
213
|
+
|
|
214
|
+
All stores created with `useConfig` or `createConfigStore` automatically subscribe to realtime updates via Server-Sent Events (SSE). When a config value changes on the server, the store updates and your component re-renders automatically.
|
|
215
|
+
|
|
216
|
+
```svelte
|
|
217
|
+
<script>
|
|
218
|
+
import { useConfig } from '@replanejs/svelte';
|
|
219
|
+
|
|
220
|
+
// This store will automatically update when 'maintenance-mode' changes
|
|
221
|
+
const maintenanceMode = useConfig<boolean>('maintenance-mode');
|
|
222
|
+
</script>
|
|
223
|
+
|
|
224
|
+
{#if $maintenanceMode}
|
|
225
|
+
<MaintenanceBanner />
|
|
226
|
+
{/if}
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## License
|
|
230
|
+
|
|
231
|
+
MIT
|
|
232
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@replanejs/svelte",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.4",
|
|
4
4
|
"description": "Svelte SDK for Replane - feature flags and remote configuration",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"svelte": "./dist/index.js",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"svelte": ">=4.0.0"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@replanejs/sdk": "^0.7.
|
|
42
|
+
"@replanejs/sdk": "^0.7.4"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@sveltejs/package": "^2.3.10",
|