@shelf/ssp-sdk

中文文档
12.0.1发布于 2024/09/19MITTypeScriptCommonJS

Self Service Portal SDK

原始使用文档
文档目录5 节
  1. ShelfSSPSDK
  2. Install
  3. Usage
  4. CommunicationChannel
  5. Usage

@shelf/ssp-sdk CircleCI

ShelfSSPSDK

The ShelfSSPSDK is a JavaScript library that allows you to easily integrate and interact with the ShelfSSP iframe-based Single-Page Application. It provides methods for initializing the ShelfSSP iframe, constructing URLs, tracking URL changes, handling events, and sending messages to the ShelfSSP.

Install

npm install @shelf/ssp-sdk

or

yarn add @shelf/ssp-sdk

As script

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@shelf/ssp-sdk@:tag-version:/lib/integration.self-service-portal.min.js"></script>

Usage

To use the ShelfSSPSDK, import it into your JavaScript or TypeScript file:

import { ShelfSSPSDK } from '@shelf/ssp-sdk';

Initialize the ShelfSSP iframe

To initialize the ShelfSSP iframe, use the initialize method:

ShelfSSPSDK.initialize({
  element: document.getElementById('shelf-ssp-container'),
  sspURL: 'https://example.com/ssp',
  lang: 'en',
  slug: 'product',
  searchParams: { category: 'electronics', brand: 'apple' },
  onReady: () => {
    console.log('ShelfSSP is ready');
  }
});

The initialize method takes an object with the following properties:

  • element (HTMLElement): The DOM element where the ShelfSSP iframe will be appended.
  • sspURL (string): The base URL of the ShelfSSP.
  • lang (string): The language parameter for the ShelfSSP URL.
  • slug (string): The slug parameter for the ShelfSSP URL.
  • searchParams (Record<string, string>): Additional query parameters for the ShelfSSP URL.
  • onReady (optional function): A callback function that will be called when the ShelfSSP iframe is loaded and ready.

Handle events from the ShelfSSP

To handle events sent from the ShelfSSP, use the on method:

ShelfSSPSDK.on('event', (data) => {
  console.log('Received event:', data);
});

The on method takes an event name and a callback function. The callback function will be called whenever an event with the specified name is received from the ShelfSSP.

Send messages to the ShelfSSP

To send messages to the ShelfSSP, use the postMessage method:

ShelfSSPSDK.postMessage('action', { payload: 'data' })
  .then((response) => {
    console.log('Received response:', response);
  });

The postMessage method takes a message type and an optional payload. It returns a promise that resolves with the response from the ShelfSSP.

Close the ShelfSSP iframe

To close the ShelfSSP iframe, use the close method:

ShelfSSPSDK.close();

The close method removes the ShelfSSP iframe from the DOM.

CommunicationChannel

The CommunicationChannel class is a utility class that provides methods for initializing and interacting with a communication channel using the structured-channel library.

Usage

To use the CommunicationChannel class, import it into your JavaScript or TypeScript file:

import { CommunicationChannel } from '@shelf/sdk-ssp';

Initialize the Communication Channel

Before using the communication channel, you need to initialize it by calling the init method:

await CommunicationChannel.init();

The init method establishes a connection with the structured-channel and prepares the communication channel for sending and receiving messages.

Send Messages

To send messages through the communication channel, use the send method:

await CommunicationChannel.send('action', { payload: 'data' });

The send method takes a message type and an optional payload. It sends the message through the communication channel and returns a promise that resolves with the response from the other end of the channel.

Handle Events

To handle events received through the communication channel, use the on method:

CommunicationChannel.on('event', (data) => {
  // Handle the event data
});

The on method takes an event name and a callback function. The callback function will be called whenever an event with the specified name is received through the communication channel.

Stop Handling Events

To stop handling events for a specific event name, use the off method:

CommunicationChannel.off('event', callback);

The off method removes the specified callback function from the event listeners for the given event name.

MIT © Shelf

版本与发布记录

@shelf/ssp-sdk 共发布 106 个版本,首次发布于 2024/01/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
12.0.1latest2024/09/192 年前
13.0.02024/06/102 年前
12.0.02024/03/282 年前
11.2.02024/03/212 年前
11.1.12024/03/212 年前
11.1.02024/03/212 年前

其他发布通道:beta → 1.0.0-beta.16。

关于 @shelf/ssp-sdk 的常见问题

@shelf/ssp-sdk 是什么?

自助服务门户SDK

如何安装 @shelf/ssp-sdk?

在项目目录运行 npm install @shelf/ssp-sdk,或使用 pnpm add / yarn add / bun add。

@shelf/ssp-sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@shelf/ssp-sdk 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@shelf/ssp-sdk 可以商用吗?

@shelf/ssp-sdk 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@shelf/ssp-sdk 还在维护吗?

最新版本 12.0.1 发布于 2024/09/19(2 年前);至今共发布 106 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/01/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。