@savvagent/sveltekit 1.0.0
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/LICENSE +21 -0
- package/README.md +80 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +43 -0
- package/dist/index.mjs +19 -0
- package/dist/server.d.mts +116 -0
- package/dist/server.d.ts +116 -0
- package/dist/server.js +89 -0
- package/dist/server.mjs +57 -0
- package/package.json +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Savvagent, LLC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# @savvagent/sveltekit
|
|
2
|
+
|
|
3
|
+
SvelteKit SDK for Savvagent with server-side load functions and client-side stores.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @savvagent/sveltekit
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
### Server-Side (Load Functions)
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
// src/hooks.server.ts
|
|
17
|
+
import { initSvelteKitServer } from '@savvagent/sveltekit/server';
|
|
18
|
+
|
|
19
|
+
initSvelteKitServer({
|
|
20
|
+
apiKey: process.env.SAVVAGENT_API_KEY!,
|
|
21
|
+
});
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
// +page.server.ts
|
|
26
|
+
import { isEnabled } from '@savvagent/sveltekit/server';
|
|
27
|
+
|
|
28
|
+
export async function load({ cookies }) {
|
|
29
|
+
const enabled = await isEnabled('new-feature', {
|
|
30
|
+
user_id: cookies.get('user_id'),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
return { enabled };
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Client-Side (Stores)
|
|
38
|
+
|
|
39
|
+
```svelte
|
|
40
|
+
<!-- +layout.svelte -->
|
|
41
|
+
<script>
|
|
42
|
+
import { initSavvagent } from '@savvagent/sveltekit';
|
|
43
|
+
|
|
44
|
+
initSavvagent({
|
|
45
|
+
apiKey: import.meta.env.VITE_SAVVAGENT_API_KEY,
|
|
46
|
+
});
|
|
47
|
+
</script>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
```svelte
|
|
51
|
+
<!-- +page.svelte -->
|
|
52
|
+
<script>
|
|
53
|
+
import { createFlag } from '@savvagent/sveltekit';
|
|
54
|
+
|
|
55
|
+
const isEnabled = createFlag('client-feature');
|
|
56
|
+
</script>
|
|
57
|
+
|
|
58
|
+
{#if $isEnabled}
|
|
59
|
+
<NewFeature />
|
|
60
|
+
{/if}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## API Reference
|
|
64
|
+
|
|
65
|
+
### Server-Side (`@savvagent/sveltekit/server`)
|
|
66
|
+
|
|
67
|
+
- `initSvelteKitServer(config)` - Initialize server client
|
|
68
|
+
- `isEnabled(flagKey, context?)` - Check if flag is enabled
|
|
69
|
+
- `evaluate(flagKey, context?)` - Get detailed result
|
|
70
|
+
- `evaluateForEvent(event, flagKey, context?)` - Evaluate with event context
|
|
71
|
+
- `getEventContext(event, overrides?)` - Extract context from event
|
|
72
|
+
- `trackError(flagKey, error, context?)` - Track errors
|
|
73
|
+
|
|
74
|
+
### Client-Side
|
|
75
|
+
|
|
76
|
+
All stores and functions from `@savvagent/svelte` are available.
|
|
77
|
+
|
|
78
|
+
## License
|
|
79
|
+
|
|
80
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { FlagStoreOptions, FlagStoreValue, createFlag, createFlagStore, createUserIdStore, getSavvagent, initSavvagent, trackError as trackErrorClient } from '@savvagent/svelte';
|
|
2
|
+
export { ApiTypes, ErrorEvent, EvaluationEvent, FlagClient, FlagClientConfig, FlagContext, FlagEvaluationResult, FlagUpdateEvent, components } from '@savvagent/sdk';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { FlagStoreOptions, FlagStoreValue, createFlag, createFlagStore, createUserIdStore, getSavvagent, initSavvagent, trackError as trackErrorClient } from '@savvagent/svelte';
|
|
2
|
+
export { ApiTypes, ErrorEvent, EvaluationEvent, FlagClient, FlagClientConfig, FlagContext, FlagEvaluationResult, FlagUpdateEvent, components } from '@savvagent/sdk';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
FlagClient: () => import_sdk.FlagClient,
|
|
24
|
+
createFlag: () => import_svelte.createFlag,
|
|
25
|
+
createFlagStore: () => import_svelte.createFlagStore,
|
|
26
|
+
createUserIdStore: () => import_svelte.createUserIdStore,
|
|
27
|
+
getSavvagent: () => import_svelte.getSavvagent,
|
|
28
|
+
initSavvagent: () => import_svelte.initSavvagent,
|
|
29
|
+
trackErrorClient: () => import_svelte.trackError
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(index_exports);
|
|
32
|
+
var import_svelte = require("@savvagent/svelte");
|
|
33
|
+
var import_sdk = require("@savvagent/sdk");
|
|
34
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
35
|
+
0 && (module.exports = {
|
|
36
|
+
FlagClient,
|
|
37
|
+
createFlag,
|
|
38
|
+
createFlagStore,
|
|
39
|
+
createUserIdStore,
|
|
40
|
+
getSavvagent,
|
|
41
|
+
initSavvagent,
|
|
42
|
+
trackErrorClient
|
|
43
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import {
|
|
3
|
+
initSavvagent,
|
|
4
|
+
getSavvagent,
|
|
5
|
+
createFlagStore,
|
|
6
|
+
createFlag,
|
|
7
|
+
createUserIdStore,
|
|
8
|
+
trackError
|
|
9
|
+
} from "@savvagent/svelte";
|
|
10
|
+
import { FlagClient } from "@savvagent/sdk";
|
|
11
|
+
export {
|
|
12
|
+
FlagClient,
|
|
13
|
+
createFlag,
|
|
14
|
+
createFlagStore,
|
|
15
|
+
createUserIdStore,
|
|
16
|
+
getSavvagent,
|
|
17
|
+
initSavvagent,
|
|
18
|
+
trackError as trackErrorClient
|
|
19
|
+
};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import * as _savvagent_sdk from '@savvagent/sdk';
|
|
2
|
+
import { FlagClientConfig, FlagClient, FlagContext } from '@savvagent/sdk';
|
|
3
|
+
import { RequestEvent } from '@sveltejs/kit';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Initialize the server-side Savvagent client.
|
|
7
|
+
* Call this once in hooks.server.ts or root +layout.server.ts.
|
|
8
|
+
*
|
|
9
|
+
* @param config - Client configuration
|
|
10
|
+
* @returns The FlagClient instance
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* // src/hooks.server.ts
|
|
15
|
+
* import { initSvelteKitServer } from '@savvagent/sveltekit/server';
|
|
16
|
+
*
|
|
17
|
+
* initSvelteKitServer({
|
|
18
|
+
* apiKey: process.env.SAVVAGENT_API_KEY!,
|
|
19
|
+
* applicationId: process.env.SAVVAGENT_APP_ID,
|
|
20
|
+
* });
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
declare function initSvelteKitServer(config: FlagClientConfig): FlagClient;
|
|
24
|
+
/**
|
|
25
|
+
* Get the server-side Savvagent client instance.
|
|
26
|
+
*
|
|
27
|
+
* @returns The FlagClient instance
|
|
28
|
+
* @throws Error if client is not initialized
|
|
29
|
+
*/
|
|
30
|
+
declare function getServerClient(): FlagClient;
|
|
31
|
+
/**
|
|
32
|
+
* Extract context from SvelteKit RequestEvent.
|
|
33
|
+
* Automatically extracts user_id from cookies and language from headers.
|
|
34
|
+
*
|
|
35
|
+
* @param event - SvelteKit RequestEvent
|
|
36
|
+
* @param overrides - Additional context properties
|
|
37
|
+
* @returns FlagContext
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* export async function load({ cookies, request }) {
|
|
42
|
+
* const context = getEventContext({ cookies, request });
|
|
43
|
+
* const enabled = await isEnabled('my-feature', context);
|
|
44
|
+
* return { enabled };
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
declare function getEventContext(event: Pick<RequestEvent, 'cookies' | 'request'>, overrides?: FlagContext): FlagContext;
|
|
49
|
+
/**
|
|
50
|
+
* Check if a feature flag is enabled in a SvelteKit load function.
|
|
51
|
+
*
|
|
52
|
+
* @param flagKey - The flag key to evaluate
|
|
53
|
+
* @param context - Optional context for targeting
|
|
54
|
+
* @returns Promise<boolean>
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* // +page.server.ts
|
|
59
|
+
* import { isEnabled } from '@savvagent/sveltekit/server';
|
|
60
|
+
*
|
|
61
|
+
* export async function load({ cookies, request }) {
|
|
62
|
+
* const enabled = await isEnabled('new-feature', {
|
|
63
|
+
* user_id: cookies.get('user_id'),
|
|
64
|
+
* });
|
|
65
|
+
*
|
|
66
|
+
* return { enabled };
|
|
67
|
+
* }
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
declare function isEnabled(flagKey: string, context?: FlagContext): Promise<boolean>;
|
|
71
|
+
/**
|
|
72
|
+
* Evaluate a feature flag with detailed result.
|
|
73
|
+
*
|
|
74
|
+
* @param flagKey - The flag key to evaluate
|
|
75
|
+
* @param context - Optional context for targeting
|
|
76
|
+
* @returns Promise<FlagEvaluationResult>
|
|
77
|
+
*/
|
|
78
|
+
declare function evaluate(flagKey: string, context?: FlagContext): Promise<_savvagent_sdk.FlagEvaluationResult>;
|
|
79
|
+
/**
|
|
80
|
+
* Execute code conditionally based on flag value.
|
|
81
|
+
*
|
|
82
|
+
* @param flagKey - The flag key to check
|
|
83
|
+
* @param callback - Function to execute if flag is enabled
|
|
84
|
+
* @param context - Optional context for targeting
|
|
85
|
+
* @returns Promise with callback result or null
|
|
86
|
+
*/
|
|
87
|
+
declare function withFlag<T>(flagKey: string, callback: () => T | Promise<T>, context?: FlagContext): Promise<T | null>;
|
|
88
|
+
/**
|
|
89
|
+
* Track an error with flag context.
|
|
90
|
+
*
|
|
91
|
+
* @param flagKey - The flag key associated with the error
|
|
92
|
+
* @param error - The error that occurred
|
|
93
|
+
* @param context - Optional context
|
|
94
|
+
*/
|
|
95
|
+
declare function trackError(flagKey: string, error: Error, context?: FlagContext): void;
|
|
96
|
+
/**
|
|
97
|
+
* Helper to evaluate flags in load functions with automatic event context.
|
|
98
|
+
*
|
|
99
|
+
* @param event - SvelteKit RequestEvent
|
|
100
|
+
* @param flagKey - The flag key to evaluate
|
|
101
|
+
* @param context - Optional additional context
|
|
102
|
+
* @returns Promise<boolean>
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* import { evaluateForEvent } from '@savvagent/sveltekit/server';
|
|
107
|
+
*
|
|
108
|
+
* export async function load(event) {
|
|
109
|
+
* const showBeta = await evaluateForEvent(event, 'beta-ui');
|
|
110
|
+
* return { showBeta };
|
|
111
|
+
* }
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
declare function evaluateForEvent(event: Pick<RequestEvent, 'cookies' | 'request'>, flagKey: string, context?: FlagContext): Promise<boolean>;
|
|
115
|
+
|
|
116
|
+
export { evaluate, evaluateForEvent, getEventContext, getServerClient, initSvelteKitServer, isEnabled, trackError, withFlag };
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import * as _savvagent_sdk from '@savvagent/sdk';
|
|
2
|
+
import { FlagClientConfig, FlagClient, FlagContext } from '@savvagent/sdk';
|
|
3
|
+
import { RequestEvent } from '@sveltejs/kit';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Initialize the server-side Savvagent client.
|
|
7
|
+
* Call this once in hooks.server.ts or root +layout.server.ts.
|
|
8
|
+
*
|
|
9
|
+
* @param config - Client configuration
|
|
10
|
+
* @returns The FlagClient instance
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* // src/hooks.server.ts
|
|
15
|
+
* import { initSvelteKitServer } from '@savvagent/sveltekit/server';
|
|
16
|
+
*
|
|
17
|
+
* initSvelteKitServer({
|
|
18
|
+
* apiKey: process.env.SAVVAGENT_API_KEY!,
|
|
19
|
+
* applicationId: process.env.SAVVAGENT_APP_ID,
|
|
20
|
+
* });
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
declare function initSvelteKitServer(config: FlagClientConfig): FlagClient;
|
|
24
|
+
/**
|
|
25
|
+
* Get the server-side Savvagent client instance.
|
|
26
|
+
*
|
|
27
|
+
* @returns The FlagClient instance
|
|
28
|
+
* @throws Error if client is not initialized
|
|
29
|
+
*/
|
|
30
|
+
declare function getServerClient(): FlagClient;
|
|
31
|
+
/**
|
|
32
|
+
* Extract context from SvelteKit RequestEvent.
|
|
33
|
+
* Automatically extracts user_id from cookies and language from headers.
|
|
34
|
+
*
|
|
35
|
+
* @param event - SvelteKit RequestEvent
|
|
36
|
+
* @param overrides - Additional context properties
|
|
37
|
+
* @returns FlagContext
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* export async function load({ cookies, request }) {
|
|
42
|
+
* const context = getEventContext({ cookies, request });
|
|
43
|
+
* const enabled = await isEnabled('my-feature', context);
|
|
44
|
+
* return { enabled };
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
declare function getEventContext(event: Pick<RequestEvent, 'cookies' | 'request'>, overrides?: FlagContext): FlagContext;
|
|
49
|
+
/**
|
|
50
|
+
* Check if a feature flag is enabled in a SvelteKit load function.
|
|
51
|
+
*
|
|
52
|
+
* @param flagKey - The flag key to evaluate
|
|
53
|
+
* @param context - Optional context for targeting
|
|
54
|
+
* @returns Promise<boolean>
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* // +page.server.ts
|
|
59
|
+
* import { isEnabled } from '@savvagent/sveltekit/server';
|
|
60
|
+
*
|
|
61
|
+
* export async function load({ cookies, request }) {
|
|
62
|
+
* const enabled = await isEnabled('new-feature', {
|
|
63
|
+
* user_id: cookies.get('user_id'),
|
|
64
|
+
* });
|
|
65
|
+
*
|
|
66
|
+
* return { enabled };
|
|
67
|
+
* }
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
declare function isEnabled(flagKey: string, context?: FlagContext): Promise<boolean>;
|
|
71
|
+
/**
|
|
72
|
+
* Evaluate a feature flag with detailed result.
|
|
73
|
+
*
|
|
74
|
+
* @param flagKey - The flag key to evaluate
|
|
75
|
+
* @param context - Optional context for targeting
|
|
76
|
+
* @returns Promise<FlagEvaluationResult>
|
|
77
|
+
*/
|
|
78
|
+
declare function evaluate(flagKey: string, context?: FlagContext): Promise<_savvagent_sdk.FlagEvaluationResult>;
|
|
79
|
+
/**
|
|
80
|
+
* Execute code conditionally based on flag value.
|
|
81
|
+
*
|
|
82
|
+
* @param flagKey - The flag key to check
|
|
83
|
+
* @param callback - Function to execute if flag is enabled
|
|
84
|
+
* @param context - Optional context for targeting
|
|
85
|
+
* @returns Promise with callback result or null
|
|
86
|
+
*/
|
|
87
|
+
declare function withFlag<T>(flagKey: string, callback: () => T | Promise<T>, context?: FlagContext): Promise<T | null>;
|
|
88
|
+
/**
|
|
89
|
+
* Track an error with flag context.
|
|
90
|
+
*
|
|
91
|
+
* @param flagKey - The flag key associated with the error
|
|
92
|
+
* @param error - The error that occurred
|
|
93
|
+
* @param context - Optional context
|
|
94
|
+
*/
|
|
95
|
+
declare function trackError(flagKey: string, error: Error, context?: FlagContext): void;
|
|
96
|
+
/**
|
|
97
|
+
* Helper to evaluate flags in load functions with automatic event context.
|
|
98
|
+
*
|
|
99
|
+
* @param event - SvelteKit RequestEvent
|
|
100
|
+
* @param flagKey - The flag key to evaluate
|
|
101
|
+
* @param context - Optional additional context
|
|
102
|
+
* @returns Promise<boolean>
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* import { evaluateForEvent } from '@savvagent/sveltekit/server';
|
|
107
|
+
*
|
|
108
|
+
* export async function load(event) {
|
|
109
|
+
* const showBeta = await evaluateForEvent(event, 'beta-ui');
|
|
110
|
+
* return { showBeta };
|
|
111
|
+
* }
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
declare function evaluateForEvent(event: Pick<RequestEvent, 'cookies' | 'request'>, flagKey: string, context?: FlagContext): Promise<boolean>;
|
|
115
|
+
|
|
116
|
+
export { evaluate, evaluateForEvent, getEventContext, getServerClient, initSvelteKitServer, isEnabled, trackError, withFlag };
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/server.ts
|
|
21
|
+
var server_exports = {};
|
|
22
|
+
__export(server_exports, {
|
|
23
|
+
evaluate: () => evaluate,
|
|
24
|
+
evaluateForEvent: () => evaluateForEvent,
|
|
25
|
+
getEventContext: () => getEventContext,
|
|
26
|
+
getServerClient: () => getServerClient,
|
|
27
|
+
initSvelteKitServer: () => initSvelteKitServer,
|
|
28
|
+
isEnabled: () => isEnabled,
|
|
29
|
+
trackError: () => trackError,
|
|
30
|
+
withFlag: () => withFlag
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(server_exports);
|
|
33
|
+
var import_sdk = require("@savvagent/sdk");
|
|
34
|
+
var serverClient = null;
|
|
35
|
+
function initSvelteKitServer(config) {
|
|
36
|
+
if (!serverClient) {
|
|
37
|
+
serverClient = new import_sdk.FlagClient(config);
|
|
38
|
+
}
|
|
39
|
+
return serverClient;
|
|
40
|
+
}
|
|
41
|
+
function getServerClient() {
|
|
42
|
+
if (!serverClient) {
|
|
43
|
+
throw new Error(
|
|
44
|
+
"SvelteKit server client not initialized. Call initSvelteKitServer() first."
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
return serverClient;
|
|
48
|
+
}
|
|
49
|
+
function getEventContext(event, overrides) {
|
|
50
|
+
const context = {
|
|
51
|
+
user_id: event.cookies.get("user_id"),
|
|
52
|
+
anonymous_id: event.cookies.get("savvagent_anonymous_id"),
|
|
53
|
+
session_id: event.cookies.get("session_id"),
|
|
54
|
+
language: event.request.headers.get("accept-language")?.split(",")[0] || void 0,
|
|
55
|
+
...overrides
|
|
56
|
+
};
|
|
57
|
+
return context;
|
|
58
|
+
}
|
|
59
|
+
async function isEnabled(flagKey, context) {
|
|
60
|
+
const client = getServerClient();
|
|
61
|
+
return client.isEnabled(flagKey, context);
|
|
62
|
+
}
|
|
63
|
+
async function evaluate(flagKey, context) {
|
|
64
|
+
const client = getServerClient();
|
|
65
|
+
return client.evaluate(flagKey, context);
|
|
66
|
+
}
|
|
67
|
+
async function withFlag(flagKey, callback, context) {
|
|
68
|
+
const client = getServerClient();
|
|
69
|
+
return client.withFlag(flagKey, callback, context);
|
|
70
|
+
}
|
|
71
|
+
function trackError(flagKey, error, context) {
|
|
72
|
+
const client = getServerClient();
|
|
73
|
+
client.trackError(flagKey, error, context);
|
|
74
|
+
}
|
|
75
|
+
async function evaluateForEvent(event, flagKey, context) {
|
|
76
|
+
const eventContext = getEventContext(event, context);
|
|
77
|
+
return isEnabled(flagKey, eventContext);
|
|
78
|
+
}
|
|
79
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
80
|
+
0 && (module.exports = {
|
|
81
|
+
evaluate,
|
|
82
|
+
evaluateForEvent,
|
|
83
|
+
getEventContext,
|
|
84
|
+
getServerClient,
|
|
85
|
+
initSvelteKitServer,
|
|
86
|
+
isEnabled,
|
|
87
|
+
trackError,
|
|
88
|
+
withFlag
|
|
89
|
+
});
|
package/dist/server.mjs
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// src/server.ts
|
|
2
|
+
import { FlagClient } from "@savvagent/sdk";
|
|
3
|
+
var serverClient = null;
|
|
4
|
+
function initSvelteKitServer(config) {
|
|
5
|
+
if (!serverClient) {
|
|
6
|
+
serverClient = new FlagClient(config);
|
|
7
|
+
}
|
|
8
|
+
return serverClient;
|
|
9
|
+
}
|
|
10
|
+
function getServerClient() {
|
|
11
|
+
if (!serverClient) {
|
|
12
|
+
throw new Error(
|
|
13
|
+
"SvelteKit server client not initialized. Call initSvelteKitServer() first."
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
return serverClient;
|
|
17
|
+
}
|
|
18
|
+
function getEventContext(event, overrides) {
|
|
19
|
+
const context = {
|
|
20
|
+
user_id: event.cookies.get("user_id"),
|
|
21
|
+
anonymous_id: event.cookies.get("savvagent_anonymous_id"),
|
|
22
|
+
session_id: event.cookies.get("session_id"),
|
|
23
|
+
language: event.request.headers.get("accept-language")?.split(",")[0] || void 0,
|
|
24
|
+
...overrides
|
|
25
|
+
};
|
|
26
|
+
return context;
|
|
27
|
+
}
|
|
28
|
+
async function isEnabled(flagKey, context) {
|
|
29
|
+
const client = getServerClient();
|
|
30
|
+
return client.isEnabled(flagKey, context);
|
|
31
|
+
}
|
|
32
|
+
async function evaluate(flagKey, context) {
|
|
33
|
+
const client = getServerClient();
|
|
34
|
+
return client.evaluate(flagKey, context);
|
|
35
|
+
}
|
|
36
|
+
async function withFlag(flagKey, callback, context) {
|
|
37
|
+
const client = getServerClient();
|
|
38
|
+
return client.withFlag(flagKey, callback, context);
|
|
39
|
+
}
|
|
40
|
+
function trackError(flagKey, error, context) {
|
|
41
|
+
const client = getServerClient();
|
|
42
|
+
client.trackError(flagKey, error, context);
|
|
43
|
+
}
|
|
44
|
+
async function evaluateForEvent(event, flagKey, context) {
|
|
45
|
+
const eventContext = getEventContext(event, context);
|
|
46
|
+
return isEnabled(flagKey, eventContext);
|
|
47
|
+
}
|
|
48
|
+
export {
|
|
49
|
+
evaluate,
|
|
50
|
+
evaluateForEvent,
|
|
51
|
+
getEventContext,
|
|
52
|
+
getServerClient,
|
|
53
|
+
initSvelteKitServer,
|
|
54
|
+
isEnabled,
|
|
55
|
+
trackError,
|
|
56
|
+
withFlag
|
|
57
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@savvagent/sveltekit",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "SvelteKit SDK for Savvagent feature flags with server and client support",
|
|
5
|
+
"author": "Savvagent",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"module": "dist/index.mjs",
|
|
9
|
+
"types": "dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./dist/index.mjs",
|
|
13
|
+
"require": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts"
|
|
15
|
+
},
|
|
16
|
+
"./server": {
|
|
17
|
+
"import": "./dist/server.mjs",
|
|
18
|
+
"require": "./dist/server.js",
|
|
19
|
+
"types": "./dist/server.d.ts"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"@sveltejs/kit": ">=1.0.0 || >=2.0.0",
|
|
27
|
+
"svelte": ">=4.0.0"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@savvagent/svelte": "1.0.0",
|
|
31
|
+
"@savvagent/sdk": "1.0.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@sveltejs/kit": "^2.5.0",
|
|
35
|
+
"svelte": "^5.0.0",
|
|
36
|
+
"tsup": "^8.0.0",
|
|
37
|
+
"typescript": "^5.4.0"
|
|
38
|
+
},
|
|
39
|
+
"keywords": [
|
|
40
|
+
"savvagent",
|
|
41
|
+
"feature-flags",
|
|
42
|
+
"sveltekit",
|
|
43
|
+
"svelte",
|
|
44
|
+
"server-side",
|
|
45
|
+
"feature-toggles"
|
|
46
|
+
],
|
|
47
|
+
"repository": {
|
|
48
|
+
"type": "git",
|
|
49
|
+
"url": "https://github.com/savvagent/savvagent-sdks",
|
|
50
|
+
"directory": "packages/sveltekit"
|
|
51
|
+
},
|
|
52
|
+
"bugs": {
|
|
53
|
+
"url": "https://github.com/savvagent/savvagent-sdks/issues"
|
|
54
|
+
},
|
|
55
|
+
"homepage": "https://github.com/savvagent/savvagent-sdks/tree/main/packages/sveltekit#readme",
|
|
56
|
+
"publishConfig": {
|
|
57
|
+
"access": "public"
|
|
58
|
+
},
|
|
59
|
+
"scripts": {
|
|
60
|
+
"build": "tsup src/index.ts src/server.ts --format cjs,esm --dts --external svelte --external @sveltejs/kit",
|
|
61
|
+
"dev": "tsup src/index.ts src/server.ts --format cjs,esm --dts --external svelte --external @sveltejs/kit --watch",
|
|
62
|
+
"test": "vitest",
|
|
63
|
+
"lint": "eslint src --ext .ts",
|
|
64
|
+
"format": "prettier --write \"src/**/*.ts\""
|
|
65
|
+
}
|
|
66
|
+
}
|