Documentation for the ParityDeals React SDK
@paritydeals/react-sdk
provides a seamless way to integrate ParityDeals entitlement management into your React applications.
ParityDealsProvider
) to initialize and configure the SDK.react
as a peer dependency.
ParityDealsProvider
ParityDealsProvider
. You need to wrap your application (or the relevant part of it) with this provider. It initializes the SDK and makes entitlement data available to its children components via context.
ParityDealsProvider
customerId: string
customer_id
).accessToken: string | null
Authorization: Bearer <accessToken>
header.ParityDealsProvider
fetchEntitlementsOnMount: true
in the config
prop, or if config
or this specific option is omitted), the SDK will attempt to fetch all entitlements for the specified customerId
as soon as the ParityDealsProvider
is mounted.
The isLoading
state from useParityDeals()
will be true
during this initial fetch.
fetchEntitlementsOnMount
is set to false
, or if you need to re-fetch entitlements at any point (e.g., after a user action that might change their entitlements), you can use the refreshAllEntitlements
function.
useParityDeals()
ParityDealsContextValue
object containing:
allEntitlements: AsyncState<EntitlementMap | null>
: The state object for all fetched entitlements.
data
: An EntitlementMap
(a Record<string, AnyEntitlement>
) where keys are featureKey
s, or null
if not loaded or error.isLoading
: Boolean indicating if the allEntitlements
data is currently being fetched/refreshed.error
: An Error
object if the last fetch failed, otherwise null
.refreshAllEntitlements: () => Promise<void>
: Function to manually trigger a re-fetch of all entitlements.getEntitlement: <T extends AnyEntitlement>(featureKey: string, type: T['type']) => AsyncState<T | null>
: Function to get a specific entitlement’s state from the cache.hasAccess: (featureKey: string) => boolean | undefined
: A utility function to quickly check access for a feature. Returns true
if access is granted, false
if denied or feature not found, and undefined
if data is loading or an error occurred.isLoading: boolean
: A global loading state indicating if the SDK is performing its initial configured fetch or a refresh operation.error: Error | null
: A global error state reflecting any error from the last fetch operation initiated by the provider.customerId: string
: The customer ID passed to the provider.apiUrl: string
: The base API URL passed to the provider.entitlementsPath: string
: The entitlements path passed to the provider.useAllEntitlements()
allEntitlements
state object.
AsyncState<EntitlementMap | null>
useBooleanEntitlement(featureKey: string)
featureKey: string
: The key of the boolean feature.AsyncState<BooleanEntitlement | null>
useConfigEntitlement(featureKey: string)
featureKey: string
: The key of the configuration feature.AsyncState<ConfigEntitlement | null>
(where ConfigEntitlement
has a configuration: number | null
property).useMeteredEntitlement(featureKey: string)
featureKey: string
: The key of the metered feature.AsyncState<MeteredEntitlement | null>
featureKey: string
: The unique key of the feature to check.children: ReactNode | ((data: E) => ReactNode)
:
ReactNode
, it’s rendered when the user is entitled and conditions are met.BooleanEntitlement
, ConfigEntitlement
, or MeteredEntitlement
) and its return value is rendered.fallback?: ReactNode
: Content to render if the user is not entitled, or if data is loading (and no loadingComponent
is provided), or if an error occurs. Defaults to null
.loadingComponent?: ReactNode
: Specific content to render while the entitlement data is loading. Overrides fallback
during loading.ShowWhenBooleanEntitled
hasAccess: true
).
ShowWhenConfigEntitled
(config: number) => ReactNode
(Required to be a function for this component).ShowWhenMeteredEntitled
remaining
usage is greater than 0 or unlimited (limit
is null
).
condition?: (data: MeteredEntitlement) => boolean
: A custom function to further determine if children should render based on the metered entitlement data.ReactNode
or (data: MeteredEntitlement) => ReactNode
.ParityDealsProviderOptions
: Configuration for the provider.ParityDealsContextValue
: The shape of the object returned by useParityDeals()
.AsyncState<T>
: Generic type for asynchronous data states.EntitlementMap
: The structure of the cached entitlements (Record<string, AnyEntitlement>
).Entitlement
, BooleanEntitlement
, ConfigEntitlement
, MeteredEntitlement
: Processed entitlement types.ApiRequestConfig
, ApiResponse
: Interfaces related to the underlying apiRequest
service.RawEntitlementsApiResponse
and related Raw...
types: Represent the structure of the direct API response.@paritydeals/react-sdk/types
(assuming your src/types/index.ts
re-exports them) or directly from the main SDK entry if configured.
onError
callback in ParityDealsProviderOptions
can be used to globally handle errors that occur during the entitlement fetching process initiated by the provider.useAllEntitlements
, useBooleanEntitlement
, etc.) returns an AsyncState
object which includes an error: Error | null
property. You can check this to handle errors specific to that data slice.useParityDeals()
also returns a global error: Error | null
state, reflecting errors from the provider’s most recent fetch operation.ParityDealsProvider
and providing the required props (customerId
, accessToken
)useBooleanEntitlement
or conditional components like ShowWhenBooleanEntitled
in your components to control UI and behavior based on entitlements.apiRequest
function for making HTTP requests. The SDK’s ParityDealsProvider
passes configuration like timeout
, maxRetries
, backoffBaseDelay
, and accessToken
to this function.
apiRequest
service (e.g., in src/services/makeRequest.service.ts
) that matches the ApiRequestConfig
and ApiResponse
interfaces used by the SDK. This service should handle actual HTTP communication, including appending query parameters passed via ApiRequestConfig.queryParams
and using the accessToken
to set the Authorization
header.apiRequest
function included in the SDK’s source code is for demonstration and type-checking purposes only and will not make real API calls.src/
) is organized as follows:
components/
: Contains conditional rendering React components.
ShowWhenBooleanEntitled.tsx
ShowWhenConfigEntitled.tsx
ShowWhenMeteredEntitled.tsx
ShowWhenEntitledInternal.tsx
(Internal helper, not typically exported)index.ts
(Exports public components)context/
: Defines the React Context.
ParityDealsContext.ts
hooks/
: Contains all custom React Hooks.
useParityDeals.ts
useAllEntitlements.ts
useBooleanEntitlement.ts
useConfigEntitlement.ts
useMeteredEntitlement.ts
index.ts
(Exports all hooks)provider/
: Contains the main ParityDealsProvider
.
ParityDealsProvider.tsx
types/
: Contains all TypeScript definitions.
api.types.ts
(Raw types from API)sdk.types.ts
(Processed types for SDK and public use)index.ts
(Re-exports all types)utils/
: Contains utility functions.
transformations.ts
(For transformApiEntitlements
)index.ts
(Exports utilities)index.ts
: The main entry point of the SDK, re-exporting all public APIs (provider, hooks, components, types).environment
option to sandbox
in the ParityDealsProvider
.