Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@openfeature/server-sdk

Package Overview
Dependencies
Maintainers
4
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@openfeature/server-sdk - npm Package Compare versions

Comparing version
1.20.2
to
1.21.0
+36
-1
dist/cjs/index.js

@@ -73,3 +73,4 @@ "use strict";

ProviderEvents: () => import_core6.ServerProviderEvents,
ProviderStatus: () => import_core.ServerProviderStatus
ProviderStatus: () => import_core.ServerProviderStatus,
TypedInMemoryProvider: () => TypedInMemoryProvider
});

@@ -201,2 +202,35 @@ module.exports = __toCommonJS(index_exports);

};
var TypedInMemoryProvider = class extends InMemoryProvider {
constructor(flagConfiguration = {}) {
super(flagConfiguration);
}
/**
* Overwrites the configured flags.
* @param { FlagConfiguration } flagConfiguration new flag configuration
* @example
* ```
* provider.putConfiguration({
* 'my-flag': {
* variants: { on: true, off: false },
* defaultVariant: 'on',
* contextEvaluator: (ctx) => ctx?.user?.id === '123' ? 'on' : 'off',
* disabled: false,
* },
* });
*
* const flags = {
* 'my-flag': {
* variants: { on: true, off: false },
* defaultVariant: 'on',
* contextEvaluator: (ctx) => ctx?.user?.id === '123' ? 'on' : 'off',
* disabled: false,
* },
* } as const; // 'as const' needed to preserve the `defaultVariant` narrow literal type rather than `string`
* provider.putConfiguration(flags);
* ```
*/
putConfiguration(flagConfiguration) {
super.putConfiguration(flagConfiguration);
}
};

@@ -994,4 +1028,5 @@ // src/provider/multi-provider/multi-provider.ts

ProviderStatus,
TypedInMemoryProvider,
...require("@openfeature/core")
});
//# sourceMappingURL=index.js.map

@@ -170,2 +170,35 @@ var __defProp = Object.defineProperty;

};
var TypedInMemoryProvider = class extends InMemoryProvider {
constructor(flagConfiguration = {}) {
super(flagConfiguration);
}
/**
* Overwrites the configured flags.
* @param { FlagConfiguration } flagConfiguration new flag configuration
* @example
* ```
* provider.putConfiguration({
* 'my-flag': {
* variants: { on: true, off: false },
* defaultVariant: 'on',
* contextEvaluator: (ctx) => ctx?.user?.id === '123' ? 'on' : 'off',
* disabled: false,
* },
* });
*
* const flags = {
* 'my-flag': {
* variants: { on: true, off: false },
* defaultVariant: 'on',
* contextEvaluator: (ctx) => ctx?.user?.id === '123' ? 'on' : 'off',
* disabled: false,
* },
* } as const; // 'as const' needed to preserve the `defaultVariant` narrow literal type rather than `string`
* provider.putConfiguration(flags);
* ```
*/
putConfiguration(flagConfiguration) {
super.putConfiguration(flagConfiguration);
}
};

@@ -982,4 +1015,5 @@ // src/provider/multi-provider/multi-provider.ts

ServerProviderEvents as ProviderEvents,
ServerProviderStatus as ProviderStatus
ServerProviderStatus as ProviderStatus,
TypedInMemoryProvider
};
//# sourceMappingURL=index.js.map

@@ -1,2 +0,2 @@

import { BaseHook, FlagValue, EvaluationContext, HookHints, EvaluationDetails, JsonValue, CommonProvider, ServerProviderStatus, Logger, ResolutionDetails, GenericEventEmitter, ServerProviderEvents, ProviderEntryInput, ProviderMetadata, RegisteredProvider, BaseEvaluationStrategy, TrackingEventDetails, BaseStrategyProviderContext, BaseStrategyPerProviderContext, BaseProviderResolutionResult, BaseProviderResolutionSuccessResult, BaseProviderResolutionErrorResult, BaseFinalResult, BaseFirstMatchStrategy, BaseFirstSuccessfulStrategy, BaseComparisonStrategy, EvaluationLifeCycle, ManageContext, ManageLogger, Eventing, ClientMetadata, OpenFeatureCommonAPI, ProviderWrapper } from '@openfeature/core';
import { BaseHook, FlagValue, EvaluationContext, BooleanFlagKey, HookHints, EvaluationDetails, StringFlagKey, NumberFlagKey, ObjectFlagKey, JsonValue, CommonProvider, ServerProviderStatus, Logger, ResolutionDetails, GenericEventEmitter, ServerProviderEvents, ProviderEntryInput, ProviderMetadata, RegisteredProvider, BaseEvaluationStrategy, TrackingEventDetails, BaseStrategyProviderContext, BaseStrategyPerProviderContext, BaseProviderResolutionResult, BaseProviderResolutionSuccessResult, BaseProviderResolutionErrorResult, BaseFinalResult, BaseFirstMatchStrategy, BaseFirstSuccessfulStrategy, BaseComparisonStrategy, EvaluationLifeCycle, ManageContext, ManageLogger, Eventing, ClientMetadata, OpenFeatureCommonAPI, ProviderWrapper } from '@openfeature/core';
export * from '@openfeature/core';

@@ -8,2 +8,3 @@ export { ServerProviderEvents as ProviderEvents, ServerProviderStatus as ProviderStatus, StrategyEvaluationContext } from '@openfeature/core';

type ConstrainedFlagKey<T> = T extends boolean ? BooleanFlagKey : T extends number ? NumberFlagKey : T extends string ? StringFlagKey : T extends JsonValue ? ObjectFlagKey : never;
interface FlagEvaluationOptions {

@@ -16,3 +17,3 @@ hooks?: Hook[];

* Performs a flag evaluation that returns a boolean.
* @param {string} flagKey The flag key uniquely identifies a particular flag
* @param {BooleanFlagKey} flagKey The flag key uniquely identifies a particular flag
* @param {boolean} defaultValue The value returned if an error occurs

@@ -23,6 +24,6 @@ * @param {EvaluationContext} context The evaluation context used on an individual flag evaluation

*/
getBooleanValue(flagKey: string, defaultValue: boolean, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<boolean>;
getBooleanValue(flagKey: BooleanFlagKey, defaultValue: boolean, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<boolean>;
/**
* Performs a flag evaluation that a returns an evaluation details object.
* @param {string} flagKey The flag key uniquely identifies a particular flag
* @param {BooleanFlagKey} flagKey The flag key uniquely identifies a particular flag
* @param {boolean} defaultValue The value returned if an error occurs

@@ -33,6 +34,6 @@ * @param {EvaluationContext} context The evaluation context used on an individual flag evaluation

*/
getBooleanDetails(flagKey: string, defaultValue: boolean, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<boolean>>;
getBooleanDetails(flagKey: BooleanFlagKey, defaultValue: boolean, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<boolean>>;
/**
* Performs a flag evaluation that returns a string.
* @param {string} flagKey The flag key uniquely identifies a particular flag
* @param {StringFlagKey} flagKey The flag key uniquely identifies a particular flag
* @template {string} T A optional generic argument constraining the string

@@ -44,7 +45,7 @@ * @param {T} defaultValue The value returned if an error occurs

*/
getStringValue(flagKey: string, defaultValue: string, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<string>;
getStringValue<T extends string = string>(flagKey: string, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<T>;
getStringValue(flagKey: StringFlagKey, defaultValue: string, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<string>;
getStringValue<T extends string = string>(flagKey: StringFlagKey, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<T>;
/**
* Performs a flag evaluation that a returns an evaluation details object.
* @param {string} flagKey The flag key uniquely identifies a particular flag
* @param {StringFlagKey} flagKey The flag key uniquely identifies a particular flag
* @template {string} T A optional generic argument constraining the string

@@ -56,7 +57,7 @@ * @param {T} defaultValue The value returned if an error occurs

*/
getStringDetails(flagKey: string, defaultValue: string, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<string>>;
getStringDetails<T extends string = string>(flagKey: string, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<T>>;
getStringDetails(flagKey: StringFlagKey, defaultValue: string, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<string>>;
getStringDetails<T extends string = string>(flagKey: StringFlagKey, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<T>>;
/**
* Performs a flag evaluation that returns a number.
* @param {string} flagKey The flag key uniquely identifies a particular flag
* @param {NumberFlagKey} flagKey The flag key uniquely identifies a particular flag
* @template {number} T A optional generic argument constraining the number

@@ -68,7 +69,7 @@ * @param {T} defaultValue The value returned if an error occurs

*/
getNumberValue(flagKey: string, defaultValue: number, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<number>;
getNumberValue<T extends number = number>(flagKey: string, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<T>;
getNumberValue(flagKey: NumberFlagKey, defaultValue: number, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<number>;
getNumberValue<T extends number = number>(flagKey: NumberFlagKey, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<T>;
/**
* Performs a flag evaluation that a returns an evaluation details object.
* @param {string} flagKey The flag key uniquely identifies a particular flag
* @param {NumberFlagKey} flagKey The flag key uniquely identifies a particular flag
* @template {number} T A optional generic argument constraining the number

@@ -80,7 +81,7 @@ * @param {T} defaultValue The value returned if an error occurs

*/
getNumberDetails(flagKey: string, defaultValue: number, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<number>>;
getNumberDetails<T extends number = number>(flagKey: string, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<T>>;
getNumberDetails(flagKey: NumberFlagKey, defaultValue: number, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<number>>;
getNumberDetails<T extends number = number>(flagKey: NumberFlagKey, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<T>>;
/**
* Performs a flag evaluation that returns an object.
* @param {string} flagKey The flag key uniquely identifies a particular flag
* @param {ObjectFlagKey} flagKey The flag key uniquely identifies a particular flag
* @template {JsonValue} T A optional generic argument describing the structure

@@ -92,7 +93,7 @@ * @param {T} defaultValue The value returned if an error occurs

*/
getObjectValue(flagKey: string, defaultValue: JsonValue, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<JsonValue>;
getObjectValue<T extends JsonValue = JsonValue>(flagKey: string, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<T>;
getObjectValue(flagKey: ObjectFlagKey, defaultValue: JsonValue, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<JsonValue>;
getObjectValue<T extends JsonValue = JsonValue>(flagKey: ObjectFlagKey, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<T>;
/**
* Performs a flag evaluation that a returns an evaluation details object.
* @param {string} flagKey The flag key uniquely identifies a particular flag
* @param {ObjectFlagKey} flagKey The flag key uniquely identifies a particular flag
* @template {JsonValue} T A optional generic argument describing the structure

@@ -104,4 +105,4 @@ * @param {T} defaultValue The value returned if an error occurs

*/
getObjectDetails(flagKey: string, defaultValue: JsonValue, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<JsonValue>>;
getObjectDetails<T extends JsonValue = JsonValue>(flagKey: string, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<T>>;
getObjectDetails(flagKey: ObjectFlagKey, defaultValue: JsonValue, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<JsonValue>>;
getObjectDetails<T extends JsonValue = JsonValue>(flagKey: ObjectFlagKey, defaultValue: T, context?: EvaluationContext, options?: FlagEvaluationOptions): Promise<EvaluationDetails<T>>;
}

@@ -162,15 +163,19 @@

type Variants<T> = Record<string, T>;
type NoInfer<T> = [T][T extends unknown ? 0 : never];
type Simplify<T> = {
[K in keyof T]: T[K];
} & {};
type FlagVariants<T extends string> = Record<T, boolean> | Record<T, string> | Record<T, number> | Record<T, JsonValue>;
/**
* A Feature Flag definition, containing it's specification
*/
type Flag = {
type Flag<T extends string = string> = {
/**
* An object containing all possible flags mappings (variant -> flag value)
*/
variants: Variants<boolean> | Variants<string> | Variants<number> | Variants<JsonValue>;
variants: FlagVariants<T>;
/**
* The variant it will resolve to in STATIC evaluation
*/
defaultVariant: string;
defaultVariant: NoInfer<T>;
/**

@@ -187,8 +192,18 @@ * Determines if flag evaluation is enabled or not for this flag.

*/
contextEvaluator?: (ctx: EvaluationContext) => string;
contextEvaluator?: (ctx: EvaluationContext) => NoInfer<T>;
};
type FlagConfiguration = Record<string, Flag>;
/**
* The configuration object for the InMemoryProvider, containing all flags and their specifications.
*
* The generic ensures that the keys of the `variants` object in each flag specification are consistent with the `defaultVariant` and the return type of `contextEvaluator`.
*/
type FlagConfiguration<T extends Record<string, FlagVariants<string>> = Record<string, FlagVariants<string>>> = {
[K in keyof T]: Simplify<{
variants: T[K];
} & Omit<Flag<keyof T[K] & string>, 'variants'>>;
};
/**
* A simple OpenFeature provider intended for demos and as a test stub.
* @deprecated Use {@link TypedInMemoryProvider} for type-safe flag configuration.
*/

@@ -215,2 +230,105 @@ declare class InMemoryProvider implements Provider {

}
/**
* A simple OpenFeature provider intended for demos and as a test stub.
* @example
* ```
* const provider = new TypedInMemoryProvider({
* 'my-flag': {
* variants: { on: true, off: false },
* defaultVariant: 'on',
* contextEvaluator: (ctx) => ctx?.user?.id === '123' ? 'on' : 'off',
* disabled: false,
* },
* });
*
* const flags = {
* 'my-flag': {
* variants: { on: true, off: false },
* defaultVariant: 'on',
* contextEvaluator: (ctx) => ctx?.user?.id === '123' ? 'on' : 'off',
* disabled: false,
* },
* } as const; // 'as const' needed to preserve the `defaultVariant` narrow literal type rather than `string`
* const provider = new TypedInMemoryProvider(flags);
* ```
*/
declare class TypedInMemoryProvider<T extends Record<string, FlagVariants<string>> = Record<string, FlagVariants<string>>> extends InMemoryProvider {
constructor(flagConfiguration?: FlagConfiguration<T>);
/**
* Overwrites the configured flags.
* @param { FlagConfiguration } flagConfiguration new flag configuration
* @example
* ```
* provider.putConfiguration({
* 'my-flag': {
* variants: { on: true, off: false },
* defaultVariant: 'on',
* contextEvaluator: (ctx) => ctx?.user?.id === '123' ? 'on' : 'off',
* disabled: false,
* },
* });
*
* const flags = {
* 'my-flag': {
* variants: { on: true, off: false },
* defaultVariant: 'on',
* contextEvaluator: (ctx) => ctx?.user?.id === '123' ? 'on' : 'off',
* disabled: false,
* },
* } as const; // 'as const' needed to preserve the `defaultVariant` narrow literal type rather than `string`
* provider.putConfiguration(flags);
* ```
*/
putConfiguration<U extends Record<string, FlagVariants<string>> = Record<string, FlagVariants<string>>>(flagConfiguration: FlagConfiguration<U>): void;
}
/**
* The variants object for a flag in the {@link TypedInMemoryProvider}, containing all possible variants and their associated values.
*
* Can be used in combination with {@link InMemoryFlagConfiguration} to preserve type-safety when extending the provider class.
* @example
* ```
* export class CustomInMemoryProvider<
* T extends Record<string, InMemoryFlagVariants<string>> = Record<string, InMemoryFlagVariants<string>>,
* > extends TypedInMemoryProvider<T> {
* constructor(flagConfiguration: InMemoryFlagConfiguration<T>) {
* super(flagConfiguration);
* // custom logic ...
* }
*
* override putConfiguration<
* U extends Record<string, InMemoryFlagVariants<string>> = Record<string, InMemoryFlagVariants<string>>,
* >(flagConfiguration: InMemoryFlagConfiguration<U>) {
* super.putConfiguration(flagConfiguration);
* // custom logic ...
* }
* }
* ```
*/
type InMemoryFlagVariants<T extends string> = FlagVariants<T>;
/**
* The configuration object for the {@link TypedInMemoryProvider}, containing all flags and their specifications.
*
* Can be used in combination with {@link InMemoryFlagVariants} to preserve type-safety when extending the provider class.
*
* The generic ensures that the keys of the `variants` object in each flag specification are consistent with the `defaultVariant` and the return type of `contextEvaluator`.
* @example
* ```
* export class CustomInMemoryProvider<
* T extends Record<string, InMemoryFlagVariants<string>> = Record<string, InMemoryFlagVariants<string>>,
* > extends TypedInMemoryProvider<T> {
* constructor(flagConfiguration: InMemoryFlagConfiguration<T>) {
* super(flagConfiguration);
* // custom logic ...
* }
*
* override putConfiguration<
* U extends Record<string, InMemoryFlagVariants<string>> = Record<string, InMemoryFlagVariants<string>>,
* >(flagConfiguration: InMemoryFlagConfiguration<U>) {
* super.putConfiguration(flagConfiguration);
* // custom logic ...
* }
* }
* ```
*/
type InMemoryFlagConfiguration<T extends Record<string, FlagVariants<string>> = Record<string, FlagVariants<string>>> = FlagConfiguration<T>;

@@ -503,3 +621,3 @@ /**

export { AsyncLocalStorageTransactionContextPropagator, ComparisonStrategy, FirstMatchStrategy, FirstSuccessfulStrategy, InMemoryProvider, MultiProvider, NOOP_PROVIDER, NOOP_TRANSACTION_CONTEXT_PROPAGATOR, OpenFeature, OpenFeatureAPI, OpenFeatureEventEmitter };
export type { Client, Features, FinalResult, FlagEvaluationOptions, Hook, ManageTransactionContextPropagator, Provider, ProviderResolutionErrorResult, ProviderResolutionResult, ProviderResolutionSuccessResult, StrategyPerProviderContext, StrategyProviderContext, Tracking, TransactionContext, TransactionContextPropagator };
export { AsyncLocalStorageTransactionContextPropagator, ComparisonStrategy, FirstMatchStrategy, FirstSuccessfulStrategy, InMemoryProvider, MultiProvider, NOOP_PROVIDER, NOOP_TRANSACTION_CONTEXT_PROPAGATOR, OpenFeature, OpenFeatureAPI, OpenFeatureEventEmitter, TypedInMemoryProvider };
export type { Client, ConstrainedFlagKey, Features, FinalResult, FlagEvaluationOptions, Hook, InMemoryFlagConfiguration, InMemoryFlagVariants, ManageTransactionContextPropagator, Provider, ProviderResolutionErrorResult, ProviderResolutionResult, ProviderResolutionSuccessResult, StrategyPerProviderContext, StrategyProviderContext, Tracking, TransactionContext, TransactionContextPropagator };
+4
-3
{
"name": "@openfeature/server-sdk",
"version": "1.20.2",
"version": "1.21.0",
"description": "OpenFeature SDK for JavaScript",

@@ -51,7 +51,8 @@ "main": "./dist/cjs/index.js",

"peerDependencies": {
"@openfeature/core": "^1.9.2"
"@openfeature/core": "^1.10.0"
},
"devDependencies": {
"@openfeature/core": "^1.9.2"
}
},
"sideEffects": false
}
+19
-11

@@ -19,4 +19,4 @@ <!-- markdownlint-disable MD033 -->

<!-- x-release-please-start-version -->
<a href="https://github.com/open-feature/js-sdk/releases/tag/server-sdk-v1.20.2">
<img alt="Release" src="https://img.shields.io/static/v1?label=release&message=v1.20.2&color=blue&style=for-the-badge" />
<a href="https://github.com/open-feature/js-sdk/releases/tag/server-sdk-v1.21.0">
<img alt="Release" src="https://img.shields.io/static/v1?label=release&message=v1.21.0&color=blue&style=for-the-badge" />
</a>

@@ -166,3 +166,6 @@ <!-- x-release-please-end -->

// Create multi-provider with a strategy
const multiProvider = new MultiProvider([primaryProvider, backupProvider], new FirstMatchStrategy());
const multiProvider = new MultiProvider(
[{ provider: primaryProvider }, { provider: backupProvider }],
new FirstMatchStrategy(),
);

@@ -179,3 +182,4 @@ // Register the multi-provider

- `FirstMatchStrategy`: Returns the first successful result from the list of providers
- `FirstMatchStrategy`: Returns the first successful result from the list of providers (short-circuits on error)
- `FirstSuccessfulStrategy`: Returns the first successful result, ignoring errors from earlier providers
- `ComparisonStrategy`: Compares results from multiple providers and can handle discrepancies

@@ -193,3 +197,3 @@

const multiProvider = new MultiProvider(
[newProvider, oldProvider], // New provider is consulted first
[{ provider: newProvider }, { provider: oldProvider }], // New provider is consulted first
new FirstMatchStrategy(),

@@ -296,3 +300,3 @@ );

```ts
import { OpenFeature, InMemoryProvider } from '@openfeature/server-sdk';
import { OpenFeature, TypedInMemoryProvider } from '@openfeature/server-sdk';

@@ -308,12 +312,12 @@ const myFlags = {

},
};
} as const;
// Registering the default provider
OpenFeature.setProvider(InMemoryProvider(myFlags));
OpenFeature.setProvider(new TypedInMemoryProvider(myFlags));
// Registering a provider to a domain
OpenFeature.setProvider('my-domain', new InMemoryProvider(someOtherFlags));
OpenFeature.setProvider('my-domain', new TypedInMemoryProvider(someOtherFlags));
// A Client bound to the default provider
const clientWithDefault = OpenFeature.getClient();
// A Client bound to the InMemoryProvider provider
// A Client bound to the TypedInMemoryProvider provider
const domainScopedClient = OpenFeature.getClient('my-domain');

@@ -366,3 +370,3 @@ ```

app.use((req: Request, res: Response, next: NextFunction) => {
const ip = res.headers.get('X-Forwarded-For');
const ip = req.headers['x-forwarded-for'];
OpenFeature.setTransactionContext({ targetingKey: req.user.id, ipAddress: ip }, () => {

@@ -401,2 +405,6 @@ // The transaction context is used in any flag evaluation throughout the whole call chain of next

### Type-Safe Flag Keys
For enhanced type safety and autocompletion, you can override flag key types using TypeScript module augmentation. See the [`@openfeature/core` README](../shared/README.md#type-safe-flag-keys) for details.
## Extending

@@ -403,0 +411,0 @@

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display