优化内容交付 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;