Skip to content

Commit 9bd0ae3

Browse files
committed
docs: add README
1 parent a30a868 commit 9bd0ae3

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

README.md

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,82 @@
1-
# data-source
1+
# Data Source
2+
3+
**Data Source** is a simple wrapper around data fetching. It is a kind of "port" in [clean architecture](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html). It allows you to make wrappers for stuff around data fetching depending on your use cases. **Data Source** uses [react-query](https://tanstack.com/query/latest) under the hood.
4+
5+
## Installation
6+
7+
```bash
8+
npm install @gravity-ui/data-source @tanstack/react-query
9+
```
10+
11+
`@tanstack/react-query` is a peer dependency.
12+
13+
## Getting started
14+
15+
Firstly, define a type of error and make your constructors for data sources and your error based on default constructors (makePlainQueryDataSource / makeInfiniteQueryDataSource). For example:
16+
17+
```ts
18+
import {makePlainQueryDataSource as makePlainQueryDataSourceBase} from '@gravity-ui/data-source';
19+
20+
export interface ApiError {
21+
title: string;
22+
code?: number;
23+
description?: string;
24+
}
25+
26+
export const makePlainQueryDataSource = <TParams, TRequest, TResponse, TData, TError = ApiError>(
27+
config: Omit<PlainQueryDataSource<TParams, TRequest, TResponse, TData, TError>, 'type'>,
28+
): PlainQueryDataSource<TParams, TRequest, TResponse, TData, TError> => {
29+
return makePlainQueryDataSourceBase(config);
30+
};
31+
```
32+
33+
Write a `DataLoader` component based on default. This is convenient to define your display of the loading status and errors. For example:
34+
35+
```tsx
36+
import {
37+
DataLoader as DataLoaderBase,
38+
DataLoaderProps as DataLoaderPropsBase,
39+
ErrorViewProps,
40+
} from '@gravity-ui/data-source';
41+
42+
export interface DataLoaderProps
43+
extends Omit<DataLoaderPropsBase<ApiError>, 'LoadingView' | 'ErrorView'> {
44+
LoadingView?: ComponentType;
45+
ErrorView?: ComponentType<ErrorViewProps<ApiError>>;
46+
}
47+
48+
export const DataLoader: React.FC<DataLoaderProps> = ({
49+
LoadingView = YourLoader,
50+
ErrorView = YourError,
51+
...restProps
52+
}) => {
53+
return <DataLoaderBase LoadingView={LoadingView} ErrorView={ErrorView} {...restProps} />;
54+
};
55+
```
56+
57+
Define your first data source:
58+
59+
```ts
60+
export const objectDataSource = makePlainQueryDataSource({
61+
// Keys have to be unique. Maybe you should create a helper for making names of data sources
62+
name: 'object',
63+
// skipContext is just a helper to skip 2 first parameters in the function (context and fetchContext)
64+
fetch: skipContext(objectFetch),
65+
});
66+
```
67+
68+
Use it in the application:
69+
70+
```tsx
71+
import {useQueryData} from '@gravity-ui/data-source';
72+
73+
export const SomeComponent: React.FC = () => {
74+
const {data, status, error, refetch} = useQueryData(objectDataSource, {objectId: 1});
75+
76+
return (
77+
<DataLoader status={status} error={error} errorAction={refetch}>
78+
{data && <ObjectComponent object={data} />}
79+
</DataLoader>
80+
);
81+
};
82+
```

0 commit comments

Comments
 (0)