data-client-setup
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSetup Reactive Data Client
配置Reactive Data Client
Detection Steps
检测步骤
Before installing, detect the project type and protocol by checking these files:
在安装前,通过检查以下文件来检测项目类型和协议:
1. Detect Package Manager
1. 检测包管理器
Check which lock file exists:
- → use
yarn.lockyarn add - → use
pnpm-lock.yamlpnpm add - or
package-lock.json→ usebun.lockbornpm installbun add
检查存在哪种锁文件:
- → 使用
yarn.lockyarn add - → 使用
pnpm-lock.yamlpnpm add - 或
package-lock.json→ 使用bun.lockb或npm installbun add
2. Detect Project Type
2. 检测项目类型
Check dependencies:
package.json| Check | Project Type |
|---|---|
| NextJS |
| Expo |
| Vue |
| React Native |
| Plain React |
检查中的依赖项:
package.json| 检查项 | 项目类型 |
|---|---|
依赖中包含 | NextJS |
依赖中包含 | Expo |
依赖中包含 | Vue |
依赖中包含 | React Native |
依赖中包含 | 普通React |
3. Detect Protocol Type
3. 检测协议类型
Scan the codebase to determine which data-fetching protocols are used:
扫描代码库以确定使用的是哪种数据获取协议:
REST Detection
REST协议检测
Look for these patterns:
- calls with REST-style URLs (
fetch(),/api/, etc.)/users/ - HTTP client libraries: ,
axios,ky,gotinsuperagentpackage.json - Files with REST patterns: ,
api.ts,client.tsservices/*.ts - URL patterns with path parameters: ,
/users/:id/posts/:postId/comments - HTTP methods in code: ,
method: 'GET',method: 'POST',.get(.post(
查找以下模式:
- 带有REST风格URL的调用(如
fetch()、/api/等)/users/ - 中的HTTP客户端库:
package.json、axios、ky、gotsuperagent - 包含REST模式的文件:、
api.ts、client.tsservices/*.ts - 带有路径参数的URL模式:、
/users/:id/posts/:postId/comments - 代码中的HTTP方法:、
method: 'GET'、method: 'POST'、.get(.post(
GraphQL Detection
GraphQL协议检测
Look for these patterns:
- ,
@apollo/client,graphql-request,urqlingraphql-tagpackage.json - or
.graphqlfiles in the project.gql - `gql`` template literal tags
- GraphQL query patterns: ,
query {,mutation {subscription { - GraphQL endpoint URLs:
/graphql
查找以下模式:
- 中的
package.json、@apollo/client、graphql-request、urqlgraphql-tag - 项目中的或
.graphql文件.gql - `gql``模板字面量标签
- GraphQL查询模式:、
query {、mutation {subscription { - GraphQL端点URL:
/graphql
Custom Protocol Detection
自定义协议检测
For async operations that don't match REST or GraphQL:
- Custom async functions returning Promises
- Third-party SDK clients (Firebase, Supabase, AWS SDK, etc.)
- IndexedDB or other local async storage
对于不匹配REST或GraphQL的异步操作:
- 返回Promise的自定义异步函数
- 第三方SDK客户端(Firebase、Supabase、AWS SDK等)
- IndexedDB或其他本地异步存储
Installation
安装
Core Packages
核心包
| Framework | Core Package |
|---|---|
| React (all) | |
| Vue | |
| 框架 | 核心包 |
|---|---|
| 所有React框架 | |
| Vue | |
Install Command Examples
安装命令示例
React (NextJS, Expo, React Native, plain React):
bash
npm install @data-client/react && npm install -D @data-client/test
yarn add @data-client/react && yarn add -D @data-client/test
pnpm add @data-client/react && pnpm add -D @data-client/testVue:
bash
npm install @data-client/vue
yarn add @data-client/vue
pnpm add @data-client/vueReact(NextJS、Expo、React Native、普通React):
bash
npm install @data-client/react && npm install -D @data-client/test
yarn add @data-client/react && yarn add -D @data-client/test
pnpm add @data-client/react && pnpm add -D @data-client/testVue:
bash
npm install @data-client/vue
yarn add @data-client/vue
pnpm add @data-client/vueProvider Setup
提供者配置
After installing, add the provider at the top-level component.
安装完成后,在顶层组件中添加提供者。
NextJS (App Router)
NextJS(App Router)
Edit :
app/layout.tsxtsx
import { DataProvider } from '@data-client/react/nextjs';
export default function RootLayout({ children }) {
return (
<html>
<DataProvider>
<body>
{children}
</body>
</DataProvider>
</html>
);
}Important: NextJS uses import path.
@data-client/react/nextjs编辑:
app/layout.tsxtsx
import { DataProvider } from '@data-client/react/nextjs';
export default function RootLayout({ children }) {
return (
<html>
<DataProvider>
<body>
{children}
</body>
</DataProvider>
</html>
);
}重要提示:NextJS需使用导入路径。
@data-client/react/nextjsExpo
Expo
Edit :
app/_layout.tsxtsx
import { Stack } from 'expo-router';
import { DataProvider } from '@data-client/react';
export default function RootLayout() {
return (
<DataProvider>
<Stack>
<Stack.Screen name="index" />
</Stack>
</DataProvider>
);
}编辑:
app/_layout.tsxtsx
import { Stack } from 'expo-router';
import { DataProvider } from '@data-client/react';
export default function RootLayout() {
return (
<DataProvider>
<Stack>
<Stack.Screen name="index" />
</Stack>
</DataProvider>
);
}React Native
React Native
Edit entry file (e.g., ):
index.tsxtsx
import { DataProvider } from '@data-client/react';
import { AppRegistry } from 'react-native';
const Root = () => (
<DataProvider>
<App />
</DataProvider>
);
AppRegistry.registerComponent('MyApp', () => Root);编辑入口文件(如):
index.tsxtsx
import { DataProvider } from '@data-client/react';
import { AppRegistry } from 'react-native';
const Root = () => (
<DataProvider>
<App />
</DataProvider>
);
AppRegistry.registerComponent('MyApp', () => Root);Plain React (Vite, CRA, etc.)
普通React(Vite、CRA等)
Edit entry file (e.g., , , or ):
index.tsxmain.tsxsrc/index.tsxtsx
import { DataProvider } from '@data-client/react';
import ReactDOM from 'react-dom/client';
ReactDOM.createRoot(document.getElementById('root')!).render(
<DataProvider>
<App />
</DataProvider>,
);编辑入口文件(如、或):
index.tsxmain.tsxsrc/index.tsxtsx
import { DataProvider } from '@data-client/react';
import ReactDOM from 'react-dom/client';
ReactDOM.createRoot(document.getElementById('root')!).render(
<DataProvider>
<App />
</DataProvider>,
);Vue
Vue
Edit :
main.tsts
import { createApp } from 'vue';
import { DataClientPlugin } from '@data-client/vue';
import App from './App.vue';
const app = createApp(App);
app.use(DataClientPlugin, {
// optional overrides
// managers: getDefaultManagers(),
// initialState,
// Controller,
// gcPolicy,
});
app.mount('#app');编辑:
main.tsts
import { createApp } from 'vue';
import { DataClientPlugin } from '@data-client/vue';
import App from './App.vue';
const app = createApp(App);
app.use(DataClientPlugin, {
// 可选配置项
// managers: getDefaultManagers(),
// initialState,
// Controller,
// gcPolicy,
});
app.mount('#app');Protocol-Specific Setup
协议专属配置
After provider setup, apply the appropriate skill based on detected protocol:
提供者配置完成后,根据检测到的协议应用对应的技能:
REST APIs
REST API
Apply skill "data-client-rest-setup" which will:
- Install
@data-client/rest - Offer to create a custom class extending
BaseEndpointRestEndpoint - Configure common behaviors: urlPrefix, authentication, error handling
应用技能**"data-client-rest-setup"**,该技能将:
- 安装
@data-client/rest - 提供创建继承自的自定义
RestEndpoint类的选项BaseEndpoint - 配置通用行为:url前缀、身份验证、错误处理
GraphQL APIs
GraphQL API
Apply skill "data-client-graphql-setup" which will:
- Install
@data-client/graphql - Create and configure instance
GQLEndpoint - Set up authentication headers
应用技能**"data-client-graphql-setup"**,该技能将:
- 安装
@data-client/graphql - 创建并配置实例
GQLEndpoint - 设置身份验证请求头
Custom Async Operations
自定义异步操作
Apply skill "data-client-endpoint-setup" which will:
- Install
@data-client/endpoint - Offer to wrap existing async functions with
new Endpoint() - Configure schemas and caching options
应用技能**"data-client-endpoint-setup"**,该技能将:
- 安装
@data-client/endpoint - 提供使用包装现有异步函数的选项
new Endpoint() - 配置模式和缓存选项
Multiple Protocols
多协议场景
If multiple protocols are detected, apply multiple setup skills. Each protocol package can be installed alongside others.
如果检测到多种协议,可应用多个配置技能。每个协议对应的包可以共存安装。
Verification Checklist
验证清单
After setup, verify:
- Core packages installed in
package.json - Provider/Plugin wraps the app at root level
- Correct import path used (especially for NextJS)
@data-client/react/nextjs - No duplicate providers in component tree
- Protocol-specific setup completed via appropriate skill
配置完成后,验证以下内容:
- 核心包已安装至
package.json - 提供者/插件已在根层级包裹应用
- 使用了正确的导入路径(尤其注意NextJS需使用)
@data-client/react/nextjs - 组件树中无重复的提供者
- 已通过对应技能完成协议专属配置
Common Issues
常见问题
NextJS: Wrong Import Path
NextJS:导入路径错误
❌ Wrong:
tsx
import { DataProvider } from '@data-client/react';✅ Correct for NextJS:
tsx
import { DataProvider } from '@data-client/react/nextjs';❌ 错误写法:
tsx
import { DataProvider } from '@data-client/react';✅ NextJS正确写法:
tsx
import { DataProvider } from '@data-client/react/nextjs';Provider Not at Root
提供者未在根层级配置
The must wrap all components that use data-client hooks. Place it at the topmost level possible.
DataProviderDataProviderNext Steps
后续步骤
After core setup and protocol-specific setup:
- Define data schemas using - see skill "data-client-schema"
Entity - Use hooks like ,
useSuspense,useQuery- see skill "data-client-react" or "data-client-vue"useController - Define REST resources - see skill "data-client-rest"
核心配置和协议专属配置完成后:
- 使用定义数据模式 - 查看技能"data-client-schema"
Entity - 使用、
useSuspense、useQuery等钩子 - 查看技能"data-client-react"或"data-client-vue"useController - 定义REST资源 - 查看技能"data-client-rest"
References
参考资料
For detailed API documentation, see the references directory:
- DataProvider - Root provider component
- installation - Installation guide
- getDefaultManagers - Default managers
如需详细的API文档,请查看参考资料目录:
- DataProvider - 根提供者组件
- installation - 安装指南
- getDefaultManagers - 默认管理器