src/core/packages/pricing/browser-internal/README.md
The @kbn/core-pricing-browser-internal package provides the browser-side implementation of Kibana's pricing service. It allows client-side code to check if features are available based on the current pricing tier configuration.
This package is part of Kibana's core and is designed to be consumed by other plugins that need to conditionally render UI elements or enable/disable functionality based on the pricing tier.
The main service that handles fetching pricing configuration from the server and providing a client to check feature availability.
A client that allows plugins to check if a specific feature is available based on the current pricing tier configuration.
The browser-side pricing service works by:
/internal/core/pricingPricingTiersClient instance with the fetched configurationPlugins can then use the client to check if specific features are available based on the current pricing tier configuration.
To use the pricing service in your plugin, you can consume it directly from the provided core dependencies.
Here's an example of how to consume the pricing service in a plugin:
// my-plugin/public/plugin.ts
import { CoreSetup, CoreStart, Plugin } from '@kbn/core/public';
export class MyPlugin implements Plugin {
public start(core: CoreStart) {
// Check if a feature is available based on the current pricing tier
const isFeature1Available = core.pricing.isFeatureAvailable('my-plugin:feature1');
const isFeature2Available = core.pricing.isFeatureAvailable('my-plugin:feature2');
// Conditionally enable features based on availability
if (isFeature1Available) {
// Enable feature1
}
if (isFeature2Available) {
// Enable feature2
}
}
}
To use the pricing service in React components, you'll typically want to access it through the CoreStart provided to a plugin.
// my-plugin/public/application.tsx
import React from 'react';
import { CoreStart } from '@kbn/core/public';
interface MyComponentProps {
coreStart: CoreStart;
}
const MyComponent: React.FC<MyComponentProps> = ({ coreStart }) => {
const isFeatureAvailable = coreStart.pricing.isFeatureAvailable('my-plugin:feature1');
return (
<div>
{isFeatureAvailable ? (
<div>This feature is available in your current pricing tier!</div>
) : (
<div>This feature requires an upgrade to access.</div>
)}
</div>
);
};
When testing components that use the pricing service, you can use the @kbn/core-pricing-browser-mocks package to mock the pricing service:
import { pricingServiceMock } from '@kbn/core-pricing-browser-mocks';
import { render } from '@testing-library/react';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import { MyComponent } from './my_component';
describe('MyComponent', () => {
let pricingStart: ReturnType<typeof pricingServiceMock.createStartContract>;
beforeEach(() => {
pricingStart = pricingServiceMock.createStartContract();
// Mock feature availability
pricingStart.isFeatureAvailable.mockImplementation((featureId) => {
if (featureId === 'my-plugin:feature1') {
return true;
}
return false;
});
});
it('renders feature1 when available', () => {
const { getByText } = render(
// The Kibana context provides the services in the react tree, but it's not necessarily required if the services are provided by other means
<KibanaContextProvider services={{ pricing: pricingStart }}>
<MyComponent />
</KibanaContextProvider>
);
expect(getByText('Use Feature 1')).toBeInTheDocument();
});
});
@kbn/core-pricing-common: Contains common types and interfaces used by both server and browser packages.@kbn/core-pricing-browser: The public API for the pricing service that plugins can consume.@kbn/core-pricing-server: The server-side counterpart to the pricing service.@kbn/core-pricing-browser-mocks: Mocks for testing plugins that consume the pricing service.