@sesamy/sesamy-js 1.13.7 → 1.15.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
@@ -118,6 +118,15 @@ These are the available configuration options, with their default values:
118
118
  endpoint: 'https://auth.sesamy.com',
119
119
  redirectUri: window.location.origin
120
120
  },
121
+ content: {
122
+ article: {
123
+ select: 'article'
124
+ }
125
+ },
126
+ tranforms: {
127
+ enabled: false,
128
+ rules: []
129
+ }
121
130
  }
122
131
  ```
123
132
 
@@ -248,3 +257,176 @@ setToken(accessToken, expiresIn)
248
257
  console.error('Failed to set token:', error);
249
258
  });
250
259
  ```
260
+
261
+ # Services
262
+
263
+ ## `generateLink(params: GenerateAccountLink | GenerateConsumeLink)`
264
+
265
+ Generates a link to a Sesamy hosted service such as account or consume. If the user is authenticated, the link will be signed so that the user can access the service without logging in again.
266
+
267
+ ### Parameters
268
+
269
+ - params (GenerateAccountLink | GenerateConsumeLink): The parameters for generating the link.
270
+ - target (string): The target service, either 'account' or 'consume'.
271
+ - sku (string, required for 'consume' target): The SKU of the product to consume.
272
+ - episodeId (string, optional for 'consume' target): The ID of the episode to consume.
273
+ - shorten (boolean, optional): If true, the link will be shortened.
274
+ - ttl (number, optional): The time-to-live for the shortened link in seconds.
275
+ - redirectUrl (string, optional): The URL to redirect to after the link is accessed.
276
+
277
+ ### Returns
278
+
279
+ string: The generated link URL. If shorten is true, the shortened URL is returned.
280
+
281
+ ### Example
282
+
283
+ The following example demonstrates how to generate a link to the Sesamy account page:
284
+
285
+ ```javascript
286
+ import { generateLink } from '@sesamy/sesamy-js';
287
+
288
+ // Generate a link to the account page
289
+ const linkParams = {
290
+ target: 'account',
291
+ shorten: true,
292
+ ttl: 3600, // 1 hour in seconds
293
+ redirectUrl: 'https://www.yourdomain.com/account',
294
+ };
295
+
296
+ generateLink(linkParams)
297
+ .then(link => {
298
+ console.log('Generated link:', link);
299
+ })
300
+ .catch(error => {
301
+ console.error('Error generating link:', error);
302
+ });
303
+ ```
304
+
305
+ And here is an example for generating a link to consume a specific product:
306
+
307
+ ```javascript
308
+ import { generateLink } from '@sesamy/sesamy-js';
309
+
310
+ // Generate a link to consume a product
311
+ const linkParams = {
312
+ target: 'consume',
313
+ sku: 'product-sku',
314
+ episodeId: 'episode-id',
315
+ shorten: true,
316
+ ttl: 3600, // 1 hour in seconds
317
+ redirectUrl: 'https://www.yourdomain.com/consume',
318
+ };
319
+
320
+ generateLink(linkParams)
321
+ .then(link => {
322
+ console.log('Generated link:', link);
323
+ })
324
+ .catch(error => {
325
+ console.error('Error generating link:', error);
326
+ });
327
+ ```
328
+
329
+ ## Transform
330
+
331
+ The transform module allows for dynamic content manipulation based on specified rules. This can include inserting, replacing, or removing elements on a webpage.
332
+
333
+ ### Configuration
334
+
335
+ The transform configuration is defined within the `Config` interface. The following properties are available:
336
+
337
+ - `enabled` (boolean, optional): Enables or disables the transform module.
338
+ - `rules` (array): An array of rule objects that define the transformations to apply.
339
+
340
+ Each rule object contains the following properties:
341
+
342
+ - `selector` (string): A CSS selector to target elements on the page.
343
+ - `transform` (string): The type of transformation to apply. Valid values are 'insert', 'replace', and 'remove'.
344
+ - `content` (string, optional): The content to insert or replace. Required for 'insert' and 'replace' transforms.
345
+ - `contentType` (string): Specifies the type of content. Valid values are 'html', 'selector', and 'url'.
346
+ - `path` (string, optional): A regular expression to match the URL path. If provided, the rule will only apply when the path matches the current URL.
347
+ - `entitlement` (string, optional): Specifies an entitlement required for the rule to apply.
348
+ - `authenticated` (boolean, optional): Specifies if the rule should only apply to authenticated users.
349
+
350
+ ### Example Configuration
351
+
352
+ Below is an example configuration for the transform module:
353
+
354
+ ```json
355
+ {
356
+ "transform": {
357
+ "enabled": true,
358
+ "rules": [
359
+ {
360
+ "selector": "#example",
361
+ "transform": "replace",
362
+ "content": "<p>New content</p>",
363
+ "contentType": "html"
364
+ },
365
+ {
366
+ "selector": ".ad-banner",
367
+ "transform": "remove"
368
+ },
369
+ {
370
+ "selector": ".insert-content",
371
+ "transform": "insert",
372
+ "content": "#source-element",
373
+ "contentType": "selector"
374
+ },
375
+ {
376
+ "selector": ".load-from-url",
377
+ "transform": "insert",
378
+ "content": "https://example.com/content",
379
+ "contentType": "url"
380
+ }
381
+ ]
382
+ }
383
+ }
384
+ ```
385
+
386
+ ### Usage
387
+
388
+ To use the transform module, include the configuration in your initialization script:
389
+
390
+ ```html
391
+ <script type="application/json" id="sesamy-js">
392
+ {
393
+ "clientId": "demo",
394
+ "transform": {
395
+ "enabled": true,
396
+ "rules": [
397
+ {
398
+ "selector": "#example",
399
+ "transform": "replace",
400
+ "content": "<p>New content</p>",
401
+ "contentType": "html"
402
+ },
403
+ {
404
+ "selector": ".ad-banner",
405
+ "transform": "remove"
406
+ },
407
+ {
408
+ "selector": ".insert-content",
409
+ "transform": "insert",
410
+ "content": "#source-element",
411
+ "contentType": "selector"
412
+ },
413
+ {
414
+ "selector": ".load-from-url",
415
+ "transform": "insert",
416
+ "content": "https://example.com/content",
417
+ "contentType": "url"
418
+ }
419
+ ]
420
+ }
421
+ }
422
+ </script>
423
+ ```
424
+
425
+ This configuration will replace the content of the element with the ID `example`, remove elements with the class `ad-banner`, insert the content of the element with the ID `source-element` into elements with the class `insert-content`, and load content from a URL into elements with the class `load-from-url`.
426
+
427
+ ### Notes
428
+
429
+ - The `content` property is required for 'insert' and 'replace' transforms.
430
+ - The `contentType` property specifies how to interpret the `content` property: as raw HTML, a CSS selector, or a URL to fetch content from.
431
+ - The `path` property can be used to apply rules conditionally based on the current URL.
432
+ - The `authenticated` property ensures that rules only apply to authenticated users if set to true.