简介
@microsoft/sp-client-base
是一个面向 Microsoft SharePoint 开发的 npm 包,提供了客户端 JavaScript API 和基础组件,帮助开发者在 SharePoint 2016 和 SharePoint Online 等环境下快速开发并部署 SharePoint 网页和应用程序。
该包主要包含以下 API:
SPHttpClient
:封装了与 SharePoint REST 服务交互的 HTTP 请求功能。SPHttpClientResponse
:封装了 SPHttpClient 返回的 HTTP 响应。SPHttpClientConfiguration
:用于配置 SPHttpClient。SPClientConfiguration
:该类已弃用,现在推荐使用SPHttpClientConfiguration
。SPHttpClientBatch
:封装了 SharePoint REST 服务的批处理功能,可在同一请求中处理多个 REST 请求。SPHttpClientBatchConfiguration
:用于配置 SPHttpClientBatch。SPHttpClientBatchResponse
:封装了 SPHttpClientBatch 返回的 HTTP 响应。
安装
通过 npm 安装:
--- ------- ------------------------- ------
使用
导入
在需要使用 @microsoft/sp-client-base
的地方导入:
------ - ------------- -------------------- - ---- ----------------------------
SPHttpClient
使用 SPHttpClient
发起 HTTP 请求,需要通过 SPHttpClientResponse
对象来获取响应结果。
----- ----------- ------------ - -------------------------- ----------------------------- --------------------- ------------------------------- ---------------- --------------------- -- - ----------------------------- ----------------------------- ---
get(url: string, config?: ISPHttpClientConfiguration)
:发起 GET 请求。post(url: string, config?: ISPHttpClientConfiguration)
:发起 POST 请求。put(url: string, config?: ISPHttpClientConfiguration)
:发起 PUT 请求。delete(url: string, config?: ISPHttpClientConfiguration)
:发起 DELETE 请求。
其中 config
参数可以是 SPHttpClient.configurations.v1
、SPHttpClient.configurations.v2
或者自定义 SPHttpClientConfiguration
对象。
SPHttpClientConfiguration
通过 SPHttpClientConfiguration
配置 SPHttpClient
对象的行为,包括请求头、超时等信息。以下是一些常用的配置项:
----- ------- ------------------------- - - -------- - ------- ----------------------------------- -- ----- ---------------- ------ ----- ------ --- ------ ----------- ------------ -------------- ----- ------- --------- --------- --------- -------------- -------- ---- -- ------------------------------ --------------------- --------
其中:
headers
:请求头配置。body
:请求体配置。cache
、credentials
、mode
、redirect
、referrer
:与 Fetch API 相同,见 MDN 文档.timeout
:超时时间,默认为 30 秒。
SPHttpClientBatch
使用 SPHttpClientBatch
可以在同一请求中处理多个 REST 请求,此方法可以提高性能。
----- ------ ----------------- - --------------------------------------- ----- -------- ------ - -------------- --------------------- ----------------- - ------------------- ------------------------------------ ---------------- --------------------- -- - ----------------------------- ----------------------------- --- ----------------- - ------------------- ------------------------------------ ---------------- --------------------- -- - ----------------------------- ----------------------------- --- ----------------------- -- - ------------------ -------- ------------ ---
beginBatch(config?: ISPHttpClientBatchConfiguration)
:开始一个新的批处理请求。addRequest(url: string, method: string, config?: ISPHttpClientConfiguration)
:将一个新的请求添加到批处理请求队列中。execute()
:开始执行批处理请求。
其中 config
参数可以通过 SPHttpClientBatch.configurations.v1
或者自定义 SPHttpClientBatchConfiguration
对象。
总结
@microsoft/sp-client-base
提供了一系列的 API 和基础组件,帮助开发者在 SharePoint 平台上快速构建网页和应用程序。本文介绍了其中较为核心的两个功能:SPHttpClient
和 SPHttpClientBatch
,读者可以通过示例代码加深理解,并在实际开发中灵活运用。
来源:JavaScript中文网 ,转载请联系管理员! 本文地址:https://www.javascriptcn.com/post/microsoft-sp-client-base