Skip to content

Commit affd1ac

Browse files
chore: Add options to useEndpoint (#37020)
Co-authored-by: Tasso Evangelista <[email protected]>
1 parent 5e821ad commit affd1ac

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

apps/meteor/client/apps/gameCenter/hooks/useExternalComponentsQuery.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ export const useExternalComponentsQuery = () => {
66
return useQuery({
77
queryKey: ['apps/external-components'],
88

9-
queryFn: async () => {
10-
return (await getExternalComponents()).externalComponents;
9+
queryFn: async ({ signal }) => {
10+
return (await getExternalComponents(undefined, { signal })).externalComponents;
1111
},
1212

1313
staleTime: 10_000,

apps/meteor/client/providers/ServerProvider.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,28 @@ const callEndpoint = <TMethod extends Method, TPathPattern extends PathPattern>(
3030
pathPattern,
3131
keys,
3232
params,
33+
signal,
3334
}: {
3435
method: TMethod;
3536
pathPattern: TPathPattern;
3637
keys: UrlParams<TPathPattern>;
3738
params: OperationParams<TMethod, TPathPattern>;
39+
signal?: AbortSignal;
3840
}): Promise<Serialized<OperationResult<TMethod, TPathPattern>>> => {
3941
const compiledPath = compile(pathPattern, { encode: encodeURIComponent })(keys) as any;
4042

4143
switch (method) {
4244
case 'GET':
43-
return sdk.rest.get(compiledPath, params as any) as any;
45+
return sdk.rest.get(compiledPath, params as any, { signal }) as any;
4446

4547
case 'POST':
46-
return sdk.rest.post(compiledPath, params as any) as any;
48+
return sdk.rest.post(compiledPath, params as any, { signal }) as any;
4749

4850
case 'PUT':
49-
return sdk.rest.put(compiledPath, params as never) as never;
51+
return sdk.rest.put(compiledPath, params as never, { signal }) as never;
5052

5153
case 'DELETE':
52-
return sdk.rest.delete(compiledPath, params as any) as any;
54+
return sdk.rest.delete(compiledPath, params as any, { signal }) as any;
5355

5456
default:
5557
throw new Error('Invalid HTTP method');

packages/ui-contexts/src/ServerContext.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export type ServerContextValue = {
3232
pathPattern: TPathPattern;
3333
keys: UrlParams<TPathPattern>;
3434
params: OperationParams<TMethod, TPathPattern>;
35+
signal?: AbortSignal;
3536
}) => Promise<Serialized<OperationResult<TMethod, TPathPattern>>>;
3637
uploadToEndpoint: (
3738
endpoint: PathFor<'POST'>,

packages/ui-contexts/src/hooks/useEndpoint.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,20 @@ import { useCallback, useContext, useRef } from 'react';
44

55
import { ServerContext } from '../ServerContext';
66

7+
type EndpointOptions = {
8+
signal?: AbortSignal;
9+
};
10+
711
export type EndpointFunction<TMethod extends Method, TPathPattern extends PathPattern> =
812
undefined extends OperationParams<TMethod, TPathPattern>
9-
? (params?: OperationParams<TMethod, TPathPattern>) => Promise<Serialized<OperationResult<TMethod, TPathPattern>>>
10-
: (params: OperationParams<TMethod, TPathPattern>) => Promise<Serialized<OperationResult<TMethod, TPathPattern>>>;
13+
? (
14+
params?: OperationParams<TMethod, TPathPattern>,
15+
options?: EndpointOptions,
16+
) => Promise<Serialized<OperationResult<TMethod, TPathPattern>>>
17+
: (
18+
params: OperationParams<TMethod, TPathPattern>,
19+
options?: EndpointOptions,
20+
) => Promise<Serialized<OperationResult<TMethod, TPathPattern>>>;
1121

1222
export function useEndpoint<TMethod extends Method, TPathPattern extends PathPattern>(
1323
method: TMethod,
@@ -19,12 +29,13 @@ export function useEndpoint<TMethod extends Method, TPathPattern extends PathPat
1929
keysRef.current = keys;
2030

2131
return useCallback(
22-
(params: OperationParams<TMethod, TPathPattern> | undefined) =>
32+
(params: OperationParams<TMethod, TPathPattern> | undefined, options?: EndpointOptions) =>
2333
callEndpoint({
2434
method,
2535
pathPattern,
2636
keys: keysRef.current as UrlParams<TPathPattern>,
2737
params: params as OperationParams<TMethod, TPathPattern>,
38+
signal: options?.signal,
2839
}),
2940
[callEndpoint, pathPattern, method],
3041
);

0 commit comments

Comments
 (0)