@sesamy/sesamy-js 1.54.0 → 1.55.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -174,6 +174,7 @@ These are the available configuration options, with their default values:
174
174
  domain: null, // Optional: custom Auth0 domain (e.g., 'auth.example.com')
175
175
  domains: [] // Optional: array of auth domain strings for multi-domain setups
176
176
  },
177
+ enablePaywallSettingsUrlFallback: false, // Optional: enable fallback to sesamy-paywall settings-url
177
178
  content: [
178
179
  {
179
180
  type: 'article',
@@ -1624,6 +1625,8 @@ console.log('Article title:', title);
1624
1625
 
1625
1626
  The content configuration is defined within the `Config` json. The following properties are available:
1626
1627
 
1628
+ ### Content Node Properties
1629
+
1627
1630
  - `article` (object): Defines the selector for the article element itself.
1628
1631
  - `image` (object): Specifies the selector and attribute for extracting article images.
1629
1632
  - `title` (object): Specifies the selector and attribute for extracting the article title.
@@ -1638,6 +1641,56 @@ Each field object contains the following properties:
1638
1641
  - `selector` (string): A CSS selector to target elements on the page.
1639
1642
  - `attribute` (string, optional): The attribute to retrieve from the selected element. If not specified, the text content of the element is used.
1640
1643
 
1644
+ ### Feature Flags
1645
+
1646
+ #### `enablePaywallSettingsUrlFallback`
1647
+
1648
+ **Type:** `boolean`
1649
+ **Default:** `false`
1650
+
1651
+ When enabled, this feature flag allows the content module to fall back to checking for a `settings-url` attribute on `<sesamy-paywall>` elements when no `paywallUrl` is found through the standard detection methods (meta tags or article attributes).
1652
+
1653
+ **Use Case:** This is useful for pages that use the `<sesamy-paywall>` custom element but don't explicitly set the paywall URL in meta tags or on the article element. The paywall component may have a `settings-url` attribute that points to the paywall configuration.
1654
+
1655
+ **Priority Order:**
1656
+
1657
+ 1. Article element `paywall-url` attribute
1658
+ 2. Content configuration `paywallUrl` property
1659
+ 3. Meta tags (`sesamy:paywall-url`, `og:paywall-url`, etc.)
1660
+ 4. **If `enablePaywallSettingsUrlFallback` is true:** `<sesamy-paywall settings-url="...">` attribute
1661
+
1662
+ **Example:**
1663
+
1664
+ ```javascript
1665
+ // Enable the fallback feature
1666
+ {
1667
+ clientId: "demo",
1668
+ enablePaywallSettingsUrlFallback: true,
1669
+ content: [
1670
+ {
1671
+ type: 'article',
1672
+ selectors: {
1673
+ article: { selector: 'sesamy-article' }
1674
+ }
1675
+ }
1676
+ ]
1677
+ }
1678
+ ```
1679
+
1680
+ ```html
1681
+ <!-- Page with sesamy-paywall element -->
1682
+ <sesamy-paywall settings-url="https://example.com/api/paywalls/123"></sesamy-paywall>
1683
+ <sesamy-article item-src="https://example.com/article/456">
1684
+ <!-- Article content -->
1685
+ </sesamy-article>
1686
+
1687
+ <script>
1688
+ // The content.get() method will now find the paywall URL from the sesamy-paywall element
1689
+ const article = window.sesamy.content.get('sesamy-article');
1690
+ console.log(article.paywallUrl); // "https://example.com/api/paywalls/123"
1691
+ </script>
1692
+ ```
1693
+
1641
1694
  ### Example Configuration
1642
1695
 
1643
1696
  Below is an example configuration for the content module:
@@ -1732,6 +1785,161 @@ console.log(articles);
1732
1785
 
1733
1786
  In this example, `content.list()` will return an array of articles extracted from the page based on the configured selectors. Each article object contains fields such as title, excerpt, image, price, currency, URL, and ID.
1734
1787
 
1788
+ #### `get(articleElementOrSelector: Element | string)`
1789
+
1790
+ Extracts article data from a specific element on the page.
1791
+
1792
+ ### Parameters
1793
+
1794
+ - `articleElementOrSelector` (Element | string): Either a DOM element or a CSS selector string identifying the article element.
1795
+
1796
+ ### Returns
1797
+
1798
+ - `Article | null`: An article object containing the extracted data, or `null` if the element is not found.
1799
+
1800
+ The returned article object contains the following properties:
1801
+
1802
+ - `title` (string, optional): The title of the article.
1803
+ - `excerpt` (string, optional): The article's excerpt.
1804
+ - `image` (string, optional): The source URL of the article's image.
1805
+ - `price` (number, optional): The price of the article.
1806
+ - `currency` (string, optional): The currency of the article price.
1807
+ - `url` (string, optional): The URL of the article.
1808
+ - `id` (string, optional): The article's unique ID.
1809
+ - `pass` (string, optional): Comma-separated list of passes required for access.
1810
+ - `paywallUrl` (string, optional): URL to the paywall configuration.
1811
+ - `accessLevel` ('public' | 'logged-in' | 'entitlement', optional): The access level required for the content.
1812
+ - `selector` (string): The CSS selector for the article element.
1813
+ - `element` (Element): The original DOM element for the article.
1814
+ - `hasAccess` (boolean, optional): Whether the user has access to the content. Set by `hasAccess()` method.
1815
+ - `entitlement` (Entitlement, optional): The entitlement object if access is granted via entitlement. Set by `hasAccess()` method.
1816
+
1817
+ ### Example Usage
1818
+
1819
+ ```js
1820
+ // Get article data by CSS selector
1821
+ const article = window.sesamy.content.get('article.premium-content');
1822
+ console.log('Article:', article);
1823
+
1824
+ // Get article data by DOM element
1825
+ const element = document.querySelector('.my-article');
1826
+ const article = window.sesamy.content.get(element);
1827
+ console.log('Article:', article);
1828
+ ```
1829
+
1830
+ #### `hasAccess(articleElementOrSelector: Element | string)`
1831
+
1832
+ Checks if the current user has access to the specified article. This method intelligently determines the access requirements based on the article's paywall configuration and decorates the article with access status and entitlement information.
1833
+
1834
+ ### Parameters
1835
+
1836
+ - `articleElementOrSelector` (Element | string): Either a DOM element or a CSS selector string identifying the article element.
1837
+
1838
+ ### Returns
1839
+
1840
+ - `Promise<Article>`: A promise that resolves to the article object decorated with access information:
1841
+ - `hasAccess` (boolean): `true` if the user has access, `false` otherwise
1842
+ - `entitlement` (Entitlement, optional): The entitlement object if access is granted via entitlement
1843
+ - All other article properties (title, url, etc.)
1844
+
1845
+ ### Behavior
1846
+
1847
+ 1. **Paywall-based Access**: If the article has a `paywallUrl`, the method fetches the paywall configuration to determine the access type based on the paywall template:
1848
+ - **Login Paywall** (`template: 'LOGIN'`): Sets `accessLevel` to `'logged-in'`, checks if the user is authenticated, and sets `hasAccess` accordingly.
1849
+ - **Entitlement Paywall** (`template: 'BOXES'`): Sets `accessLevel` to `'entitlement'`, checks for entitlement, and sets both `hasAccess` and `entitlement`.
1850
+
1851
+ 2. **Standard Entitlement Check**: If no `paywallUrl` is specified, the method uses the article's `url` and `pass` properties to check for entitlement access, setting both `hasAccess` and `entitlement`.
1852
+
1853
+ ### Example Usage
1854
+
1855
+ ```js
1856
+ // Check access for an article
1857
+ const article = await window.sesamy.content.hasAccess('.premium-article');
1858
+
1859
+ if (article.hasAccess) {
1860
+ console.log('User has access to:', article.title);
1861
+ if (article.entitlement) {
1862
+ console.log('Via entitlement:', article.entitlement.id);
1863
+ }
1864
+ // Show premium content
1865
+ } else {
1866
+ console.log('User does not have access');
1867
+ // Show paywall or login prompt
1868
+ }
1869
+
1870
+ // Use with async/await
1871
+ async function checkArticleAccess() {
1872
+ try {
1873
+ const article = await window.sesamy.content.hasAccess('article#main');
1874
+
1875
+ if (article.hasAccess) {
1876
+ // Unlock or display content
1877
+ console.log('Access granted to article:', article.url);
1878
+
1879
+ // Access entitlement details if available
1880
+ if (article.entitlement) {
1881
+ console.log('Entitlement type:', article.entitlement.type);
1882
+ console.log('Entitlement SKU:', article.entitlement.sku);
1883
+ }
1884
+ } else {
1885
+ // Redirect to paywall
1886
+ console.log('Access denied');
1887
+ }
1888
+ } catch (error) {
1889
+ console.error('Error checking access:', error);
1890
+ }
1891
+ }
1892
+ ```
1893
+
1894
+ #### `unlock(articleElementOrSelector: Element | string, lockedContentSelector?: string)`
1895
+
1896
+ Retrieves and unlocks premium content for an article that the user has entitlement access to.
1897
+
1898
+ ### Parameters
1899
+
1900
+ - `articleElementOrSelector` (Element | string): Either a DOM element or a CSS selector string identifying the article element.
1901
+ - `lockedContentSelector` (string, optional): A CSS selector for the specific locked content to retrieve. If not provided, uses the parent element of the article.
1902
+
1903
+ ### Returns
1904
+
1905
+ - `Promise<string>`: A promise that resolves to the unlocked HTML content.
1906
+
1907
+ ### Throws
1908
+
1909
+ - Error if the article element cannot be found.
1910
+ - Error if the article doesn't have a URL.
1911
+ - Error if the user doesn't have access to the content.
1912
+ - Error if the access token cannot be retrieved.
1913
+
1914
+ ### Example Usage
1915
+
1916
+ ```js
1917
+ // Unlock content and replace the locked element
1918
+ async function unlockArticle() {
1919
+ try {
1920
+ const unlockedContent = await window.sesamy.content.unlock('.premium-article');
1921
+
1922
+ // Replace the locked content with the unlocked content
1923
+ const article = document.querySelector('.premium-article');
1924
+ if (article && article.parentElement) {
1925
+ article.parentElement.innerHTML = unlockedContent;
1926
+ }
1927
+ } catch (error) {
1928
+ console.error('Failed to unlock content:', error);
1929
+ }
1930
+ }
1931
+
1932
+ // Unlock specific content within an article
1933
+ async function unlockSpecificContent() {
1934
+ const unlockedContent = await window.sesamy.content.unlock('article.premium', '.locked-section');
1935
+
1936
+ const lockedSection = document.querySelector('.locked-section');
1937
+ if (lockedSection) {
1938
+ lockedSection.innerHTML = unlockedContent;
1939
+ }
1940
+ }
1941
+ ```
1942
+
1735
1943
  ### Flags API
1736
1944
 
1737
1945
  The Flags API allows for client-side feature flag management. These flags are stored in the browser's localStorage and can be used to enable or disable features for specific users.