nocobase/docs/guide/kernel-principle/client-side-kernel.md

158 lines
3.2 KiB
Markdown
Raw Normal View History

---
order: 2
---
# Client-side Kernel
2021-10-31 01:44:52 +00:00
To allow more non-developers to participate, NocoBase provides a companion client-side plugin -- a visual configuration interface with no code. The core of this part is @nocobase/client, which ideally can be used within any front-end build tool or framework, e.g.
- umijs
- create-react-app
- icejs
- vite
- snowpack
- nextjs
2021-10-31 01:44:52 +00:00
- Other
2021-10-31 01:44:52 +00:00
For the time being only support umijs (packaging compilation is still some problems), the future will gradually support the above-listed frameworks.
2021-10-31 01:44:52 +00:00
The main components of the client include.
2021-10-31 01:44:52 +00:00
## Request
- API Client
- Request Hook
```ts
const api = new APIClient({
2021-10-31 01:44:52 +00:00
request,
});
api.auth();
api.get();
api.post();
api.resource('collections').create();
api.resource('collections').findOne({});
api.resource('collections').findMany({});
2021-10-31 01:44:52 +00:00
api.resource('collections').relationship('fields').of(1).create();
```
2021-10-31 01:44:52 +00:00
The following details are TBD, special resources
```js
api.collections.create();
api.uiSchemas.create();
```
Request Hook
[https://www.npmjs.com/package/@ahooksjs/use-request](https://www.npmjs.com/package/@ahooksjs/use-request)
```js
const { data } = useRequest(() => api.resource('users').findMany());
```
2021-10-31 01:44:52 +00:00
## Routing
- createRouteSwitch
```js
const RouteSwitch = createRouteSwitch({
2021-10-31 01:44:52 +00:00
components: {},
});
<RouteSwitch routes={[]} />
```
2021-10-31 01:44:52 +00:00
## Schema component
- createSchemaComponent
```js
function Hello() {
2021-10-31 01:44:52 +00:00
return <div>Hello Word</div>
}
const SchemaComponent = createSchemaComponent({
scope,
components: {
2021-10-31 01:44:52 +00:00
Hello
},
});
const schema = {
type: 'void',
2021-10-31 01:44:52 +00:00
'x-component': 'Hello',
};
<SchemaComponent schema={schema} />
```
2021-10-31 01:44:52 +00:00
## How do you assemble it?
<pre lang="tsx">
import { I18nextProvider } from 'react-i18next';
import { createRouteSwitch, APIClient } from '@nocobase/client';
const apiClient = new APIClient();
const i18n = i18next.createInstance();
const Hello = () => {
2021-10-31 01:44:52 +00:00
return <div>Hello</div>;
}
const SchemaComponent = createSchemaComponent({
2021-10-31 01:44:52 +00:00
components: {
Hello,
},
});
const PageTemplate = () => {
const schema = {
2021-10-31 01:44:52 +00:00
type: 'void',
'x-component': 'Hello',
};
2021-10-31 01:44:52 +00:00
return (
<SchemaComponent schema={schema}/>
);
}
const RouteSwitch = createRouteSwitch({
2021-10-31 01:44:52 +00:00
components: {
PageTemplate,
},
});
const routes = [
{ path: '/hello', component: 'Hello' },
];
function AntdProvider(props) {
2021-10-31 01:44:52 +00:00
// The locale here can be handled dynamically depending on the i18next
return (
2021-10-31 03:35:11 +00:00
<ConfigProvider locale={locale}>{props.children}</ConfigProvider>
);
}
const App = () => {
2021-10-31 01:44:52 +00:00
return (
<APIClientProvider client={apiClient}>
<I18nextProvider i18n={i18n}>
2021-10-31 03:35:11 +00:00
<AntdProvider>
<Router>
<RouteSwitch routes={routes}/>
</Router>
</AntdProvider>
</I18nextProvider>
</APIClientProvider>
);
}
</pre>
2021-10-31 01:44:52 +00:00
- APIClientProvider: provides the APIClient
- I18nextProvider: internationalization
- AntdProvider: handles the internationalization of antd components, which needs to be placed in I18nextProvider
- Router: route driver
- RouteSwitch: route distribution
2021-10-31 01:44:52 +00:00
The above code may seem a bit verbose, but the actual function and role of each part is not the same, so it is not suitable for over-encapsulation. If needed, it can be further encapsulated according to the actual situation.