@episerver/content-delivery

中文文档
1.1.0发布于 2022/03/25Apache-2.0TypeScriptESM + CJS近 30 天下载 1.1万

用于 Episerver 内容交付 API 的 SDK.

中文使用文档
文档目录7 节
  1. 安装
  2. 实例
  3. 内容加载器
  4. 内容解析器
  5. 站点加载器
  6. 配置
  7. 更改默认配置

优化内容交付 JavaScript SDK - 内容加载器,内容解析器和站点加载器

用于从运行该程序的优化应用程序中加载和解析内容的 SDK 内容交付 API。SDK是用 TypeScript 编写的,包括类型.

请访问 优化世界 用于充分记录API.

注意此 SDK 需要您使用平面化的 JSON 格式, 参见下面的代码示例 如何配置 API 使用此格式 .

services.AddContentDeliveryApi(options => 
{
    options.FlattenPropertyModel = true;
});

安装

使用 npm :

$ npm install @episerver/content-delivery@latest

使用线条 :

$ yarn install @episerver/content-delivery@latest

实例

内容加载器

既然我们输入了内容装载器

import { ContentLoader } from '@episerver/content-delivery';

const contentLoader = new ContentLoader();

构造器需要一个 ContentDeliveryConfig 对象.

用标识符加载内容 :

contentLoader.getContent('38963a25-b6ec-493b-aca4-f4fbce1aed4d')
  .then((content) => {
    // Content was successfully loaded
  })
  .catch((contentLoaderError) => {
    console.error('Content not loaded', contentLoaderError.errorMessage);
  });

该函数可选 ContentRequest 对象,我们可以指定要装入哪个语言分支 branch 参数 :

contentLoader.getContent('38963a25-b6ec-493b-aca4-f4fbce1aed4d', { branch: 'en' })
  .then((content) => {
    // Content was successfully loaded
  });

我们可以具体说明哪些属性应被纳入与 select 参数 :

contentLoader.getContent('38963a25-b6ec-493b-aca4-f4fbce1aed4d', { select: ['heading', 'body'] })
  .then((content) => {
    // Content was successfully loaded
  });

我们可以确定哪些属性在回应中应该扩大,与 expand 参数 :

contentLoader.getContent('38963a25-b6ec-493b-aca4-f4fbce1aed4d', { expand: ['teasers'] })
  .then((content) => {
    // Content was successfully loaded
  });

引用属性(内容区和内容引用)仅包括默认引用的内容. 通过扩展这些属性,引用内容的内容也包含在响应中. 见 内容 API 文档 更多信息.

用父标识符装入孩子 :

contentLoader.getChildren('38963a25-b6ec-493b-aca4-f4fbce1aed4d')
  .then((children) => {
    // Children was successfully loaded
  });

该函数可选 ContentCollectionRequest 对象。 计外 ContentRequest 参数,我们也可以限制内容项的数量来加载:

contentLoader.getChildren('38963a25-b6ec-493b-aca4-f4fbce1aed4d', { top: 10 })
  .then((collection) => {
    // Children was successfully loaded
    var children = collection.items;
    var continuationToken = collection.continuationToken;
  });

在装入下一组内容项目时,请使用续作符:

contentLoader.getChildren('38963a25-b6ec-493b-aca4-f4fbce1aed4d', { continuationToken: continuationToken })
  .then((collection) => {
    // Children was successfully loaded
    var children = collection.items;
    // When we reached the last set, the continuation token won't be present.
    var continuationToken = collection.continuationToken;
  });

注意续作符号包含应装入多少项,不需要提供 top 参数.

用标识符装入祖先 :

contentLoader.getAncestors('38963a25-b6ec-493b-aca4-f4fbce1aed4d')
  .then((ancestors) => {
    // Ancestors was successfully loaded
  });

该函数可选 ContentRequest 对象.

ContentRequest 计划

{
  branch: 'en',
  select: [''],
  expand: [''],
}

ContentCollectionRequest 计划

{
  branch: 'en',
  select: [''],
  expand: [''],
  top: 0,
  continuationToken: '',
}

ContentLoaderError 计划

{
  data: {},
  errorCode: 404,
  errorMessage: '',
}

内容解析器

鉴于我们输入了内容解析器

import { ContentResolver } from '@episerver/content-delivery';

const contentResolver = new ContentResolver();

构造器需要一个 ContentDeliveryConfig 对象.

用 URL 解决内容 :

contentResolver.resolveContent('https://site.com/en/page1/page2/doesnt-exist/', false)
  .then((resolvedContent) => {
    // Content was resolved. Note that the promise is fulfilled
    // even though the content was not found. The return object contains a
    // status property you can check.
    var content = resolvedContent.content;
  })
  .catch((contentResolverError) => {
    // Promise is only rejected if there was a service error.
    console.error('Content not resolved', contentResolverError.errorMessage);
  });

第二个参数, matchExact,定义URL是部分匹配还是精确匹配。 URL的剩余路径,如果部分匹配,则可用于 remainingPath 在返回对象中,参见 ResolvedContent。该函数还需要可选的 ResolveContentRequest 对象.

ResolveContentRequest 计划

{
  select: [''],
  expand: [''],
}

ResolvedContent 计划

{
  content: {},
  branch: 'en',
  status: 'UNKNOWN|RESOLVED|NOTFOUND|UNAUTHORIZED|ACCESSDENIED',
  mode: 'DEFAULT|PREVIEW|EDIT',
  remainingPath: 'doesnt-exist/',
  siteId: '6ed7799f-efd8-4421-8eb3-c95bfbf7281d',
  startPageId: 'ff2e86ae-27a9-4417-b503-ecb9c2fdd63b',
}

ContentResolverError 计划

{
  errorMessage: '',
}

站点加载器

鉴于我们进口了现场装载器,并立即进行:

import { SiteLoader } from '@episerver/content-delivery';

const siteLoader = new SiteLoader();

构造器需要一个 ContentDeliveryConfig 对象.

以标识符装入站点 :

siteLoader.getSite('45872cd5-e63c-363e-caa4-35e6ab1ad6d4')
  .then((site) => {
    // Site was successfully loaded
  })
  .catch((siteLoaderError) => {
    console.error('Site not loaded', siteLoaderError.errorMessage);
  });

正在装入所有站点 :

siteLoader.getSites()
  .then((sites) => {
    // Sites was successfully listed
  });

配置

ContentDeliveryConfig 计划

{
  apiUrl: 'https://site.com/api/episerver/v2.0',
  getAccessToken: function (path) { 
    // Return access token.
  },
  selectAllProperties: true,
  expandAllProperties: false,
}

更改默认配置

默认配置与您传递到内容加载器和解析器类的构建器的配置结合.

import { defaultConfig } from '@episerver/content-delivery';

defaultConfig.apiUrl = 'https://site.com/api/episerver/v2.0';
defaultConfig.getAccessToken = () => authService.getAccessToken();
defaultConfig.selectAllProperties = true;
defaultConfig.expandAllProperties = true;

版本与发布记录

@episerver/content-delivery 共发布 3 个版本,首次发布于 2021/07/05;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.0latest2022/03/254 年前
1.0.02021/11/164 年前
1.0.0-pre.12021/07/055 年前

关于 @episerver/content-delivery 的常见问题

@episerver/content-delivery 是什么?

用于 Episerver 内容交付 API 的 SDK.

如何安装 @episerver/content-delivery?

在项目目录运行 npm install @episerver/content-delivery,或使用 pnpm add / yarn add / bun add。

@episerver/content-delivery 支持 TypeScript 吗?

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

@episerver/content-delivery 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@episerver/content-delivery 可以商用吗?

@episerver/content-delivery 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@episerver/content-delivery 的下载量有多少?

根据 npm 官方下载统计,@episerver/content-delivery 在 2026/10/14 – 2026/11/12 这 30 天内被下载 10,536 次,日均约 351 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@episerver/content-delivery 还在维护吗?

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