Strapi 客户端
¥Strapi Client
Strapi 客户端库简化了与 Strapi 后端的交互,提供了一种获取、创建、更新和删除内容的方法。本指南将引导你设置 Strapi 客户端、配置身份验证以及有效使用其主要功能。
¥The Strapi Client library simplifies interactions with your Strapi back end, providing a way to fetch, create, update, and delete content. This guide walks you through setting up the Strapi Client, configuring authentication, and using its key features effectively.
入门
¥Getting Started
-
Strapi 项目已创建并正在运行。如果你尚未设置,请按照 快速入门指南 创建一个。
¥A Strapi project has been created and is running. If you haven't set one up yet, follow the Quick Start Guide to create one.
-
你知道 Strapi 实例的内容 API 的 URL(例如
http://localhost:1337/api
)。¥You know the URL of the Content API of your Strapi instance (e.g.,
http://localhost:1337/api
).
安装
¥Installation
要在你的项目中使用 Strapi 客户端,请使用你首选的包管理器将其作为依赖安装:
¥To use the Strapi Client in your project, install it as a dependency using your preferred package manager:
- Yarn
- NPM
- pnpm
yarn add @strapi/client
npm install @strapi/client
pnpm add @strapi/client
基本配置
¥Basic configuration
要开始与 Strapi 后端交互,请初始化 Strapi 客户端并设置基本 API URL:
¥To start interacting with your Strapi back end, initialize the Strapi Client and set the base API URL:
import { strapi } from '@strapi/client';
const client = strapi({ baseURL: 'http://localhost:1337/api' });
如果你在浏览器环境中使用 Strapi 客户端,则可以使用 <script>
标签将其包含在内:
¥If you're using the Strapi Client in a browser environment, you can include it using a <script>
tag:
<script src="https://cdn.jsdelivr.net/npm/@strapi/client"></script>
<script>
const client = strapi.strapi({ baseURL: 'http://localhost:1337/api' });
</script>
验证
¥Authentication
Strapi 客户端支持不同的身份验证策略来访问 Strapi 后端中受保护的资源。
¥The Strapi Client supports different authentication strategies to access protected resources in your Strapi back end.
如果你的 Strapi 实例使用 API 令牌,请按如下方式配置 Strapi 客户端:
¥If your Strapi instance uses API tokens, configure the Strapi Client as follows:
const client = strapi({
baseURL: 'http://localhost:1337/api',
auth: 'your-api-token-here',
});
这允许你的请求自动包含必要的身份验证凭据。
¥This allows your requests to include the necessary authentication credentials automatically.
API 参考
¥API Reference
Strapi 客户端提供以下关键属性和方法用于与你的 Strapi 后端交互:
¥The Strapi Client provides the following key properties and methods for interacting with your Strapi back end:
范围 | 描述 |
---|---|
baseURL | Strapi 后端的基本 API URL。 |
fetch() | 一种用于发出类似于原生获取 API 的通用 API 请求的实用方法。 |
collection() | 管理集合类型资源(例如,博客文章、产品)。 |
single() | 管理单一类型资源(例如,主页设置、全局配置)。 |
通用获取
¥General purpose fetch
Strapi 客户端提供对底层 JavaScript fetch
函数的访问,以发出直接 API 请求。请求始终与客户端初始化期间提供的基本 URL 相关:
¥The Strapi Client provides access to the underlying JavaScript fetch
function to make direct API requests. The request is always relative to the base URL provided during client initialization:
const result = await client.fetch('articles', { method: 'GET' });
使用集合类型
¥Working with collection types
Strapi 中的集合类型是具有多个条目的实体(例如,包含多篇帖子的博客)。Strapi 客户端提供了一种 collection()
方法来与这些资源交互,可用的方法如下:
¥Collection types in Strapi are entities with multiple entries (e.g., a blog with many posts). The Strapi Client provides a collection()
method to interact with these resources, with the following methods available:
范围 | 描述 |
---|---|
find(queryParams?) | 获取具有可选过滤、排序或分页的多个文档。 |
findOne(documentID, queryParams?) | 通过其唯一 ID 检索单个文档。 |
create(data, queryParams?) | 在集合中创建一个新文档。 |
update(documentID, data, queryParams?) | 更新现有文档。 |
delete(documentID, queryParams?) | 更新现有文档。 |
使用示例:
¥Usage examples:
const articles = client.collection('articles');
// Fetch all english articles sorted by title
const allArticles = await articles.find({
locale: 'en',
sort: 'title',
});
// Fetch a single article
const singleArticle = await articles.findOne('article-document-id');
// Create a new article
const newArticle = await articles.create({ title: 'New Article', content: '...' });
// Update an existing article
const updatedArticle = await articles.update('article-document-id', { title: 'Updated Title' });
// Delete an article
await articles.delete('article-id');
使用单一类型
¥Working with single types
Strapi 中的单一类型代表仅存在一次的唯一内容条目(例如,主页设置或站点范围的配置)。Strapi 客户端提供了一种 single()
方法来与这些资源交互,可用的方法如下:
¥Single types in Strapi represent unique content entries that exist only once (e.g., the homepage settings or site-wide configurations). The Strapi Client provides a single()
method to interact with these resources, with the following methods available:
范围 | 描述 |
---|---|
find(queryParams?) | 获取文档。 |
update(documentID, data, queryParams?) | 更新文档。 |
delete(queryParams?) | 删除文档。 |
使用示例:
¥Usage examples:
const homepage = client.single('homepage');
// Fetch the default homepage content
const defaultHomepage = await homepage.find();
// Fetch the Spanish version of the homepage
const spanishHomepage = await homepage.find({ locale: 'es' });
// Update the homepage draft content
const updatedHomepage = await homepage.update(
{ title: 'Updated Homepage Title' },
{ status: 'draft' }
);
// Delete the homepage content
await homepage.delete();
有关 Strapi Strapi 客户端的更多详细信息,请参阅 软件包的 README。
¥More details about the Strapi Strapi Client might be found in the package's README.