Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export const useExternalComponentsQuery = () => {
return useQuery({
queryKey: ['apps/external-components'],

queryFn: async () => {
return (await getExternalComponents()).externalComponents;
queryFn: async ({ signal }) => {
return (await getExternalComponents(undefined, { signal })).externalComponents;
},

staleTime: 10_000,
Expand Down
10 changes: 6 additions & 4 deletions apps/meteor/client/providers/ServerProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,28 @@ const callEndpoint = <TMethod extends Method, TPathPattern extends PathPattern>(
pathPattern,
keys,
params,
signal,
}: {
method: TMethod;
pathPattern: TPathPattern;
keys: UrlParams<TPathPattern>;
params: OperationParams<TMethod, TPathPattern>;
signal?: AbortSignal;
}): Promise<Serialized<OperationResult<TMethod, TPathPattern>>> => {
const compiledPath = compile(pathPattern, { encode: encodeURIComponent })(keys) as any;

switch (method) {
case 'GET':
return sdk.rest.get(compiledPath, params as any) as any;
return sdk.rest.get(compiledPath, params as any, { signal }) as any;

case 'POST':
return sdk.rest.post(compiledPath, params as any) as any;
return sdk.rest.post(compiledPath, params as any, { signal }) as any;

case 'PUT':
return sdk.rest.put(compiledPath, params as never) as never;
return sdk.rest.put(compiledPath, params as never, { signal }) as never;

case 'DELETE':
return sdk.rest.delete(compiledPath, params as any) as any;
return sdk.rest.delete(compiledPath, params as any, { signal }) as any;

default:
throw new Error('Invalid HTTP method');
Expand Down
1 change: 1 addition & 0 deletions packages/ui-contexts/src/ServerContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type ServerContextValue = {
pathPattern: TPathPattern;
keys: UrlParams<TPathPattern>;
params: OperationParams<TMethod, TPathPattern>;
signal?: AbortSignal;
}) => Promise<Serialized<OperationResult<TMethod, TPathPattern>>>;
uploadToEndpoint: (
endpoint: PathFor<'POST'>,
Expand Down
17 changes: 14 additions & 3 deletions packages/ui-contexts/src/hooks/useEndpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@ import { useCallback, useContext, useRef } from 'react';

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

type EndpointOptions = {
signal?: AbortSignal;
};

export type EndpointFunction<TMethod extends Method, TPathPattern extends PathPattern> =
undefined extends OperationParams<TMethod, TPathPattern>
? (params?: OperationParams<TMethod, TPathPattern>) => Promise<Serialized<OperationResult<TMethod, TPathPattern>>>
: (params: OperationParams<TMethod, TPathPattern>) => Promise<Serialized<OperationResult<TMethod, TPathPattern>>>;
? (
params?: OperationParams<TMethod, TPathPattern>,
options?: EndpointOptions,
) => Promise<Serialized<OperationResult<TMethod, TPathPattern>>>
: (
params: OperationParams<TMethod, TPathPattern>,
options?: EndpointOptions,
) => Promise<Serialized<OperationResult<TMethod, TPathPattern>>>;

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

return useCallback(
(params: OperationParams<TMethod, TPathPattern> | undefined) =>
(params: OperationParams<TMethod, TPathPattern> | undefined, options?: EndpointOptions) =>
callEndpoint({
method,
pathPattern,
keys: keysRef.current as UrlParams<TPathPattern>,
params: params as OperationParams<TMethod, TPathPattern>,
signal: options?.signal,
}),
[callEndpoint, pathPattern, method],
);
Expand Down
Loading