fix: removes exports of OpenFeatureClient class and makes event props readonly (#918)

<!-- Please use this template for your pull request. -->
<!-- Please use the sections that you need and delete other sections -->

## This PR
<!-- add the description of the PR here -->

Removes OpenFeatureClient class from exports and makes event details
readonly as described here:
https://github.com/open-feature/js-sdk/issues/799

Signed-off-by: Lukas Reining <lukas.reining@codecentric.de>
This commit is contained in:
Lukas Reining 2024-05-02 16:36:32 +02:00 committed by GitHub
parent 6dd558ee61
commit e9a25c21cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 63 additions and 50 deletions

View File

@ -1,2 +1 @@
export * from './client';
export * from './open-feature-client';

View File

@ -8,10 +8,11 @@ import {
objectOrUndefined,
stringOrUndefined,
} from '@openfeature/core';
import { Client, OpenFeatureClient } from './client';
import { Client } from './client';
import { OpenFeatureEventEmitter, ProviderEvents } from './events';
import { Hook } from './hooks';
import { NOOP_PROVIDER, Provider, ProviderStatus } from './provider';
import { OpenFeatureClient } from './client/open-feature-client';
// use a symbol as a key for the global singleton
const GLOBAL_OPENFEATURE_API_KEY = Symbol.for('@openfeature/web-sdk/api');
@ -26,10 +27,17 @@ type DomainRecord = {
const _globalThis = globalThis as OpenFeatureGlobal;
export class OpenFeatureAPI extends OpenFeatureCommonAPI<ClientProviderStatus, Provider, Hook> implements ManageContext<Promise<void>> {
export class OpenFeatureAPI
extends OpenFeatureCommonAPI<ClientProviderStatus, Provider, Hook>
implements ManageContext<Promise<void>>
{
protected _statusEnumType: typeof ProviderStatus = ProviderStatus;
protected _apiEmitter: GenericEventEmitter<ProviderEvents> = new OpenFeatureEventEmitter();
protected _defaultProvider: ProviderWrapper<Provider, ClientProviderStatus> = new ProviderWrapper(NOOP_PROVIDER, ProviderStatus.NOT_READY, this._statusEnumType);
protected _defaultProvider: ProviderWrapper<Provider, ClientProviderStatus> = new ProviderWrapper(
NOOP_PROVIDER,
ProviderStatus.NOT_READY,
this._statusEnumType,
);
protected _domainScopedProviders: Map<string, ProviderWrapper<Provider, ClientProviderStatus>> = new Map();
protected _createEventEmitter = () => new OpenFeatureEventEmitter();
@ -111,9 +119,7 @@ export class OpenFeatureAPI extends OpenFeatureCommonAPI<ClientProviderStatus, P
...unboundProviders,
];
await Promise.all(
allDomainRecords.map((dm) =>
this.runProviderContextChangeHandler(dm.domain, dm.wrapper, oldContext, context),
),
allDomainRecords.map((dm) => this.runProviderContextChangeHandler(dm.domain, dm.wrapper, oldContext, context)),
);
}
}

View File

@ -8,13 +8,13 @@ import {
JsonObject,
JsonValue,
OpenFeature,
OpenFeatureClient,
Provider,
ProviderFatalError,
ProviderStatus,
ResolutionDetails,
StandardResolutionReasons,
} from '../src';
import { OpenFeatureClient } from '../src/client/open-feature-client';
const BOOLEAN_VALUE = true;
const STRING_VALUE = 'val';

View File

@ -1,5 +1,6 @@
import { Paradigm } from '@openfeature/core';
import { OpenFeature, OpenFeatureAPI, OpenFeatureClient, Provider, ProviderStatus } from '../src';
import { OpenFeature, OpenFeatureAPI, Provider, ProviderStatus } from '../src';
import { OpenFeatureClient } from '../src/client/open-feature-client';
const mockProvider = (config?: { initialStatus?: ProviderStatus; runsOn?: Paradigm }) => {
return {

View File

@ -1,6 +1,6 @@
import { Test, TestingModule } from '@nestjs/testing';
import { getOpenFeatureClientToken, OpenFeatureModule, ServerProviderEvents } from '../src';
import { OpenFeature, OpenFeatureClient } from '@openfeature/server-sdk';
import { Client, OpenFeature } from '@openfeature/server-sdk';
import { getOpenFeatureDefaultTestModule } from './fixtures';
describe('OpenFeatureModule', () => {
@ -31,19 +31,19 @@ describe('OpenFeatureModule', () => {
it('should return the SDKs default provider and not throw', async () => {
expect(() => {
moduleWithoutProvidersRef.get<OpenFeatureClient>(getOpenFeatureClientToken());
moduleWithoutProvidersRef.get<Client>(getOpenFeatureClientToken());
}).not.toThrow();
});
});
it('should return the default provider', async () => {
const client = moduleRef.get<OpenFeatureClient>(getOpenFeatureClientToken());
const client = moduleRef.get<Client>(getOpenFeatureClientToken());
expect(client).toBeDefined();
expect(await client.getStringValue('testStringFlag', '')).toEqual('expected-string-value-default');
});
it('should inject the client with the given scope', async () => {
const client = moduleRef.get<OpenFeatureClient>(getOpenFeatureClientToken('domainScopedClient'));
const client = moduleRef.get<Client>(getOpenFeatureClientToken('domainScopedClient'));
expect(client).toBeDefined();
expect(await client.getStringValue('testStringFlag', '')).toEqual('expected-string-value-scoped');
});

View File

@ -1,14 +1,14 @@
import { Controller, Get, Injectable, UseInterceptors } from '@nestjs/common';
import { Observable, map } from 'rxjs';
import { BooleanFeatureFlag, ObjectFeatureFlag, NumberFeatureFlag, FeatureClient, StringFeatureFlag } from '../src';
import { OpenFeatureClient, EvaluationDetails, FlagValue } from '@openfeature/server-sdk';
import { Client, EvaluationDetails, FlagValue } from '@openfeature/server-sdk';
import { EvaluationContextInterceptor } from '../src';
@Injectable()
export class OpenFeatureTestService {
constructor(
@FeatureClient() public defaultClient: OpenFeatureClient,
@FeatureClient({ domain: 'domainScopedClient' }) public domainScopedClient: OpenFeatureClient,
@FeatureClient() public defaultClient: Client,
@FeatureClient({ domain: 'domainScopedClient' }) public domainScopedClient: Client,
) {}
public async serviceMethod(flag: EvaluationDetails<FlagValue>) {

View File

@ -1,2 +1 @@
export * from './client';
export * from './open-feature-client';

View File

@ -6,7 +6,7 @@ import {
objectOrUndefined,
stringOrUndefined,
} from '@openfeature/core';
import { Client, OpenFeatureClient } from './client';
import { Client } from './client';
import { OpenFeatureEventEmitter } from './events';
import { Hook } from './hooks';
import { NOOP_PROVIDER, Provider, ProviderStatus } from './provider';
@ -17,6 +17,7 @@ import {
TransactionContextPropagator,
} from './transaction-context';
import { ServerProviderStatus } from '@openfeature/core';
import { OpenFeatureClient } from './client/open-feature-client';
// use a symbol as a key for the global singleton
const GLOBAL_OPENFEATURE_API_KEY = Symbol.for('@openfeature/js-sdk/api');
@ -28,11 +29,17 @@ const _globalThis = globalThis as OpenFeatureGlobal;
export class OpenFeatureAPI
extends OpenFeatureCommonAPI<ServerProviderStatus, Provider, Hook>
implements ManageContext<OpenFeatureAPI>, ManageTransactionContextPropagator<OpenFeatureCommonAPI<ServerProviderStatus, Provider>>
implements
ManageContext<OpenFeatureAPI>,
ManageTransactionContextPropagator<OpenFeatureCommonAPI<ServerProviderStatus, Provider>>
{
protected _statusEnumType: typeof ProviderStatus = ProviderStatus;
protected _apiEmitter = new OpenFeatureEventEmitter();
protected _defaultProvider: ProviderWrapper<Provider, ServerProviderStatus> = new ProviderWrapper(NOOP_PROVIDER, ProviderStatus.NOT_READY, this._statusEnumType);
protected _defaultProvider: ProviderWrapper<Provider, ServerProviderStatus> = new ProviderWrapper(
NOOP_PROVIDER,
ProviderStatus.NOT_READY,
this._statusEnumType,
);
protected _domainScopedProviders: Map<string, ProviderWrapper<Provider, ServerProviderStatus>> = new Map();
protected _createEventEmitter = () => new OpenFeatureEventEmitter();

View File

@ -9,7 +9,6 @@ import {
JsonObject,
JsonValue,
OpenFeature,
OpenFeatureClient,
Provider,
ProviderFatalError,
ProviderStatus,
@ -18,6 +17,7 @@ import {
TransactionContext,
TransactionContextPropagator,
} from '../src';
import { OpenFeatureClient } from '../src/client/open-feature-client';
const BOOLEAN_VALUE = true;
const STRING_VALUE = 'val';

View File

@ -1,5 +1,6 @@
import { Paradigm } from '@openfeature/core';
import { OpenFeature, OpenFeatureAPI, OpenFeatureClient, Provider, ProviderStatus } from '../src';
import { OpenFeature, OpenFeatureAPI, Provider, ProviderStatus } from '../src';
import { OpenFeatureClient } from '../src/client/open-feature-client';
const mockProvider = (config?: { initialStatus?: ProviderStatus; runsOn?: Paradigm }) => {
return {

View File

@ -6,24 +6,24 @@ export type EventMetadata = {
};
export type CommonEventDetails = {
providerName: string;
readonly providerName: string;
/**
* @deprecated alias of "domain", use domain instead
*/
clientName?: string;
readonly clientName?: string;
readonly domain?: string;
};
type CommonEventProps = {
message?: string;
metadata?: EventMetadata;
readonly message?: string;
readonly metadata?: EventMetadata;
};
export type ReadyEvent = CommonEventProps;
export type ErrorEvent = CommonEventProps;
export type StaleEvent = CommonEventProps;
export type ReconcilingEvent = CommonEventProps & { errorCode: ErrorCode };
export type ConfigChangeEvent = CommonEventProps & { flagsChanged?: string[] };
export type ReconcilingEvent = CommonEventProps & { readonly errorCode: ErrorCode };
export type ConfigChangeEvent = CommonEventProps & { readonly flagsChanged?: string[] };
type ServerEventMap = {
[ServerProviderEvents.Ready]: ReadyEvent;