Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a top-level method for accessing providers #1152

Merged
merged 2 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/server/src/open-feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,27 @@ export class OpenFeatureAPI
return this;
}

/**
* Get the default provider.
*
* Note that it isn't recommended to interact with the provider directly, but rather through
* an OpenFeature client.
* @returns {Provider} Default Provider
*/
getProvider(): Provider;
/**
* Get the provider bound to the specified domain.
*
* Note that it isn't recommended to interact with the provider directly, but rather through
* an OpenFeature client.
* @param {string} domain An identifier which logically binds clients with providers
* @returns {Provider} Domain-scoped provider
*/
getProvider(domain?: string): Provider;
getProvider(domain?: string): Provider {
return this.getProviderForClient(domain);
}

setContext(context: EvaluationContext): this {
this._context = context;
return this;
Expand Down
12 changes: 6 additions & 6 deletions packages/server/test/open-feature.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ describe('OpenFeature', () => {
it('should set the default provider if no domain is provided', () => {
const provider = mockProvider();
OpenFeature.setProvider(provider);
const client = OpenFeature.getClient();
expect(client.metadata.providerMetadata.name).toEqual(provider.metadata.name);
const registeredProvider = OpenFeature.getProvider();
expect(registeredProvider).toEqual(provider);
});

it('should not change providers associated with a domain when setting a new default provider', () => {
Expand All @@ -85,11 +85,11 @@ describe('OpenFeature', () => {
OpenFeature.setProvider(provider);
OpenFeature.setProvider(domain, fakeProvider);

const defaultClient = OpenFeature.getClient();
const domainSpecificClient = OpenFeature.getClient(domain);
const defaultProvider = OpenFeature.getProvider();
const domainSpecificProvider = OpenFeature.getProvider(domain);

expect(defaultClient.metadata.providerMetadata.name).toEqual(provider.metadata.name);
expect(domainSpecificClient.metadata.providerMetadata.name).toEqual(fakeProvider.metadata.name);
expect(defaultProvider).toEqual(provider);
expect(domainSpecificProvider).toEqual(fakeProvider);
});

it('should bind a new provider to existing clients in a matching domain', () => {
Expand Down
27 changes: 24 additions & 3 deletions packages/web/src/open-feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,27 @@ export class OpenFeatureAPI
return this;
}

/**
* Get the default provider.
*
* Note that it isn't recommended to interact with the provider directly, but rather through
* an OpenFeature client.
* @returns {Provider} Default Provider
*/
getProvider(): Provider;
/**
* Get the provider bound to the specified domain.
*
* Note that it isn't recommended to interact with the provider directly, but rather through
* an OpenFeature client.
* @param {string} domain An identifier which logically binds clients with providers
* @returns {Provider} Domain-scoped provider
*/
getProvider(domain?: string): Provider;
getProvider(domain?: string): Provider {
return this.getProviderForClient(domain);
}

/**
* Sets the evaluation context globally.
* This will be used by all providers that have not bound to a domain.
Expand Down Expand Up @@ -325,9 +346,9 @@ export class OpenFeatureAPI
}

/**
* A factory function for creating new named OpenFeature clients. Clients can contain
* their own state (e.g. logger, hook, context). Multiple clients can be used
* to segment feature flag configuration.
* A factory function for creating new domain-scoped OpenFeature clients. Clients
* can contain their own state (e.g. logger, hook, context). Multiple domains
* can be used to segment feature flag configuration.
*
* If there is already a provider bound to this name via {@link this.setProvider setProvider}, this provider will be used.
* Otherwise, the default provider is used until a provider is assigned to that name.
Expand Down
12 changes: 6 additions & 6 deletions packages/web/test/open-feature.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ describe('OpenFeature', () => {
it('should set the default provider if no domain is provided', () => {
const provider = mockProvider();
OpenFeature.setProvider(provider);
const client = OpenFeature.getClient();
expect(client.metadata.providerMetadata.name).toEqual(provider.metadata.name);
const registeredProvider = OpenFeature.getProvider();
expect(registeredProvider).toEqual(provider);
});

it('should not change providers associated with a domain when setting a new default provider', () => {
Expand All @@ -86,11 +86,11 @@ describe('OpenFeature', () => {
OpenFeature.setProvider(provider);
OpenFeature.setProvider(domain, fakeProvider);

const defaultClient = OpenFeature.getClient();
const domainSpecificClient = OpenFeature.getClient(domain);
const defaultProvider = OpenFeature.getProvider();
const domainSpecificProvider = OpenFeature.getProvider(domain);

expect(defaultClient.metadata.providerMetadata.name).toEqual(provider.metadata.name);
expect(domainSpecificClient.metadata.providerMetadata.name).toEqual(fakeProvider.metadata.name);
expect(defaultProvider).toEqual(provider);
expect(domainSpecificProvider).toEqual(fakeProvider);
});

it('should bind a new provider to existing clients in a matching domain', () => {
Expand Down