Skip to content

Commit 7169bde

Browse files
committed
apiOptions docs
1 parent a8a03db commit 7169bde

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

static/app/utils/api/apiOptions.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,50 @@ function _apiOptionsInfinite<
9292
});
9393
}
9494

95+
/**
96+
* Type-safe factory for TanStack Query options that hit Sentry API endpoints.
97+
*
98+
* By default, `select` extracts the JSON body. To also access response headers
99+
* (e.g. `Link` for pagination), override with `selectJsonWithHeaders`.
100+
*
101+
* @example Basic usage
102+
* ```ts
103+
* const query = useQuery(
104+
* apiOptions.as<Project[]>()('/organizations/$organizationIdOrSlug/projects/', {
105+
* path: {organizationIdOrSlug: organization.slug},
106+
* staleTime: 30_000,
107+
* })
108+
* );
109+
* // query.data is Project[]
110+
* ```
111+
*
112+
* @example Conditional fetching
113+
* ```ts
114+
* const query = useQuery(
115+
* apiOptions.as<Project>()('/organizations/$organizationIdOrSlug/projects/$projectIdOrSlug/', {
116+
* path: projectSlug
117+
* ? {organizationIdOrSlug: organization.slug, projectIdOrSlug: projectSlug}
118+
* : skipToken,
119+
* staleTime: 30_000,
120+
* })
121+
* );
122+
* ```
123+
*
124+
* @example With response headers (pagination)
125+
* ```ts
126+
* const {data} = useQuery({
127+
* ...apiOptions.as<Item[]>()('/organizations/$organizationIdOrSlug/items/', {
128+
* path: {organizationIdOrSlug: organization.slug},
129+
* query: {cursor, per_page: 25},
130+
* staleTime: 0,
131+
* }),
132+
* select: selectJsonWithHeaders,
133+
* });
134+
* // data is ApiResponse<Item[]>
135+
* const items = data?.json ?? [];
136+
* const pageLinks = data?.headers.Link;
137+
* ```
138+
*/
95139
export const apiOptions = {
96140
as:
97141
<TManualData>() =>

0 commit comments

Comments
 (0)