Skip to content

Commit 631cc4a

Browse files
committed
Update the documenting storybook
1 parent 3929eba commit 631cc4a

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

x-pack/plugins/infra/public/utils/data_search/data_search.stories.mdx

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,35 @@ be issued by calling the returned `search()` function. For each new request the
4343
hook emits an object describing the request and its state in the `requests$`
4444
`Observable`.
4545

46+
Since the specific response shape depends on the data strategy used, the hook
47+
takes a projection function, that is responsible for decoding the response in
48+
an appropriate way. Because most response projections follow a similar pattern
49+
there's a helper `normalizeDataSearchResponses(initialResponse,
50+
parseRawResponse)`, which generates an RxJS operator, that...
51+
52+
- emits an initial response containing the given `initialResponse` value
53+
- applies `parseRawResponse` to the `rawResponse` property of each emitted response
54+
- transforms transport layer errors as well as parsing errors into
55+
`SearchStrategyError`s
56+
4657
```typescript
58+
const parseMyCustomSearchResponse = normalizeDataSearchResponses(
59+
'initial value',
60+
decodeOrThrow(myCustomSearchResponsePayloadRT)
61+
);
62+
4763
const { search, requests$ } = useDataSearch({
4864
getRequest: useCallback((searchTerm: string) => ({
4965
request: {
5066
params: {
5167
searchTerm
52-
}
53-
}
54-
}), []);
68+
},
69+
options: {
70+
strategy: 'my-custom-search-strategy',
71+
},
72+
},
73+
}), []),
74+
parseResponses: parseMyCustomSearchResponse,
5575
});
5676
```
5777

@@ -68,10 +88,6 @@ observables are unsubscribed from for proper cancellation if a new request has
6888
been created. This uses RxJS's `switchMap()` operator under the hood. The hook
6989
also makes sure that all observables are unsubscribed from on unmount.
7090

71-
Since the specific response shape depends on the data strategy used, the hook
72-
takes a projection function, that is responsible for decoding the response in
73-
an appropriate way.
74-
7591
A request can fail due to various reasons that include servers-side errors,
7692
Elasticsearch shard failures and network failures. The intention is to map all
7793
of them to a common `SearchStrategyError` interface. While the
@@ -94,11 +110,7 @@ const {
94110
latestResponseErrors,
95111
loaded,
96112
total,
97-
} = useLatestPartialDataSearchResponse(
98-
requests$,
99-
'initialValue',
100-
useMemo(() => decodeOrThrow(mySearchStrategyResponsePayloadRT), []),
101-
);
113+
} = useLatestPartialDataSearchResponse(requests$);
102114
```
103115

104116
## Representing the request state to the user

0 commit comments

Comments
 (0)