Skip to content

Commit

Permalink
Work around type checking issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan committed Jul 1, 2022
1 parent a56230e commit cf08a10
Show file tree
Hide file tree
Showing 19 changed files with 45 additions and 55 deletions.
6 changes: 2 additions & 4 deletions apps/meteor/client/hooks/useEndpointAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import { useCallback } from 'react';
export const useEndpointAction = <TMethod extends Method, TPath extends PathFor<TMethod>>(
method: TMethod,
path: TPath,
params: Serialized<OperationParams<TMethod, MatchPathPattern<TPath>>> = {} as Serialized<
OperationParams<TMethod, MatchPathPattern<TPath>>
>,
params?: OperationParams<TMethod, MatchPathPattern<TPath>>,
successMessage?: string,
): (() => Promise<Serialized<OperationResult<TMethod, MatchPathPattern<TPath>>>>) => {
const sendData = useEndpoint(method, path);
const dispatchToastMessage = useToastMessageDispatch();

return useCallback(async () => {
try {
const data = await sendData(params);
const data = await sendData(params as any);

if (successMessage) {
dispatchToastMessage({ type: 'success', message: successMessage });
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/client/hooks/useEndpointActionExperimental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const useEndpointActionExperimental = <TMethod extends Method, TPath exte
path: TPath,
successMessage?: string,
): ((
params: Serialized<OperationParams<TMethod, MatchPathPattern<TPath>>>,
params: OperationParams<TMethod, MatchPathPattern<TPath>>,
) => Promise<Serialized<OperationResult<TMethod, MatchPathPattern<TPath>>>>) => {
const sendData = useEndpoint(method, path);
const dispatchToastMessage = useToastMessageDispatch();
Expand Down
11 changes: 2 additions & 9 deletions apps/meteor/client/hooks/useEndpointData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@ import { AsyncState, useAsyncState } from './useAsyncState';

export const useEndpointData = <TPath extends PathFor<'GET'>>(
endpoint: TPath,
params: void extends OperationParams<'GET', MatchPathPattern<TPath>>
? void
: Serialized<OperationParams<'GET', MatchPathPattern<TPath>>> = undefined as void extends OperationParams<
'GET',
MatchPathPattern<TPath>
>
? void
: Serialized<OperationParams<'GET', MatchPathPattern<TPath>>>,
params?: OperationParams<'GET', MatchPathPattern<TPath>>,
initialValue?:
| Serialized<OperationResult<'GET', MatchPathPattern<TPath>>>
| (() => Serialized<OperationResult<'GET', MatchPathPattern<TPath>>>),
Expand All @@ -27,7 +20,7 @@ export const useEndpointData = <TPath extends PathFor<'GET'>>(

const fetchData = useCallback(() => {
reset();
getData(params)
getData(params as any)
.then(resolve)
.catch((error) => {
console.error(error);
Expand Down
13 changes: 8 additions & 5 deletions apps/meteor/client/providers/ServerProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,27 @@ const callMethod = <MethodName extends ServerMethodName>(
const callEndpoint = <TMethod extends Method, TPath extends PathFor<TMethod>>(
method: TMethod,
path: TPath,
params: Serialized<OperationParams<TMethod, MatchPathPattern<TPath>>>,
params: OperationParams<TMethod, MatchPathPattern<TPath>>,
): Promise<Serialized<OperationResult<TMethod, MatchPathPattern<TPath>>>> => {
switch (method) {
case 'GET':
return APIClient.get(path as Parameters<typeof APIClient.get>[0], params as any | undefined) as any;
return APIClient.get(path as any, params as any) as any;

case 'POST':
return APIClient.post(path as Parameters<typeof APIClient.post>[0], params as never) as ReturnType<typeof APIClient.post>;
return APIClient.post(path as any, params as any) as any;

case 'PUT':
return APIClient.put(path as any, params as any) as any;

case 'DELETE':
return APIClient.delete(path as Parameters<typeof APIClient.delete>[0], params as never) as ReturnType<typeof APIClient.delete>;
return APIClient.delete(path as any, params as any) as any;

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

const uploadToEndpoint = (endpoint: PathFor<'POST'>, formData: any): Promise<UploadResult> => APIClient.post(endpoint, formData as never);
const uploadToEndpoint = (endpoint: PathFor<'POST'>, formData: any): Promise<UploadResult> => APIClient.post(endpoint as any, formData);

const getStream = (streamName: string, options: {} = {}): (<T>(eventName: string, callback: (data: T) => void) => () => void) => {
const streamer = Meteor.StreamerCentral.instances[streamName]
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/client/stories/contexts/ServerContextMock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ const ServerContextMock = ({

return {
match: (method: string, path: string): boolean => _method === method && pathRegexp.test(path[0] === '/' ? path : `/v1/${path}`),
handler,
handler: handler as any,
};
},
);

const _callEndpoint: ServerContextValue['callEndpoint'] = async <TMethod extends Method, TPath extends PathFor<TMethod>>(
method: TMethod,
path: TPath,
params: Serialized<OperationParams<TMethod, MatchPathPattern<TPath>>>,
params: OperationParams<TMethod, MatchPathPattern<TPath>>,
): Promise<Serialized<OperationResult<TMethod, MatchPathPattern<TPath>>>> => {
const mockedEndpoint = mockedEndpoints.find((endpoint) => endpoint.match(method, path));
const handler = mockedEndpoint?.handler;
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/client/views/admin/apps/AppReleases.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type release = {
};

const AppReleases = ({ id }: { id: string }): JSX.Element => {
const { value, phase, error } = useEndpointData(`/apps/${id}/versions`);
const { value, phase, error } = useEndpointData(`/apps/${id}/versions`) as any;

const [releases, setReleases] = useState([] as release[]);

Expand All @@ -29,7 +29,7 @@ const AppReleases = ({ id }: { id: string }): JSX.Element => {
const { apps } = value;

setReleases(
apps.map((app) => ({
apps.map((app: any) => ({
version: app.version,
createdDate: app.createdDate,
detailedChangelog: app.detailedChangelog,
Expand Down
6 changes: 3 additions & 3 deletions apps/meteor/client/views/admin/apps/hooks/useAppInfo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { App } from '@rocket.chat/core-typings';
import { useEndpoint, EndpointFunction } from '@rocket.chat/ui-contexts';
import { useEndpoint } from '@rocket.chat/ui-contexts';
import { useState, useEffect, useContext } from 'react';

import { ISettings } from '../../../../../app/apps/client/@types/IOrchestrator';
Expand Down Expand Up @@ -29,7 +29,7 @@ export const useAppInfo = (appId: string): AppInfo | undefined => {
const getApis = useEndpoint('GET', `/apps/${appId}/apis`);

// TODO: remove EndpointFunction<'GET', 'apps/:id'>
const getBundledIn = useEndpoint('GET', `/apps/${appId}`) as EndpointFunction<'GET', '/apps/:id'>;
const getBundledIn = useEndpoint('GET', `/apps/${appId}`) as any;

useEffect(() => {
const apps: App[] = [];
Expand Down Expand Up @@ -70,7 +70,7 @@ export const useAppInfo = (appId: string): AppInfo | undefined => {
update: 'true',
appVersion: appId,
})
.then(({ app }) => {
.then(({ app }: any) => {
appResult.tosLink = app.tosLink;
appResult.privacyLink = app.privacyLink;
return getBundledInApp(app);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export const useChannelsList = ({ period, offset, count }: UseChannelsListOption
const { start, end } = getPeriodRange(period);

const response = await getChannelsList({
start: start.toISOString(),
end: end.toISOString(),
start,
end,
offset,
count,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const useMessageOrigins = ({ period }: UseMessageOriginsOptions) => {
const { start, end } = getPeriodRange(period);

const response = await getMessageOrigins({
start: start.toISOString(),
end: end.toISOString(),
start,
end,
});

return response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const useMessagesSent = ({ period }: UseMessagesSentOptions) => {
const { start, end } = getPeriodRange(period);

const response = await getMessagesSent({
start: start.toISOString(),
end: end.toISOString(),
start,
end,
});

return response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const useTopFivePopularChannels = ({ period }: UseTopFivePopularChannelsO
const { start, end } = getPeriodRange(period);

const response = await getTopFivePopularChannels({
start: start.toISOString(),
end: end.toISOString(),
start,
end,
});

return response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const useActiveUsers = ({ utc }: UseActiveUsersOptions) => {
const { start, end } = getPeriodRange('last 30 days', utc);

const response = await getActiveUsers({
start: (utc ? moment.utc(start) : moment(start)).subtract(29, 'days').toISOString(),
end: end.toISOString(),
start: (utc ? moment.utc(start) : moment(start)).subtract(29, 'days').toDate(),
end,
});

return response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const useHourlyChatActivity = ({ displacement, utc }: UseHourlyChatActivi
const day = (utc ? moment.utc().endOf('day') : moment().endOf('day')).subtract(displacement, 'days').toDate();

const response = await getHourlyChatActivity({
start: day.toISOString(),
start: day,
});

return response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const useNewUsers = ({ period, utc }: UseNewUsersOptions) => {
const { start, end } = getPeriodRange(period, utc);

const response = await getNewUsers({
start: start.toISOString(),
end: end.toISOString(),
start,
end,
});

return response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const useUsersByTimeOfTheDay = ({ period, utc }: UseUsersByTimeOfTheDayOp
const { start, end } = getPeriodRange(period, utc);

const response = await getUsersByTimeOfTheDay({
start: start.toISOString(),
end: end.toISOString(),
start,
end,
});

return response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const useWeeklyChatActivity = ({ displacement, utc }: UseWeeklyChatActivi
const day = (utc ? moment.utc().endOf('day') : moment().endOf('day')).subtract(displacement, 'weeks').toDate();

const response = await getWeeklyChatActivity({
start: day.toISOString(),
start: day,
});

return response
Expand Down
6 changes: 1 addition & 5 deletions packages/rest-typings/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,7 @@ export type JoinPathPattern<TBasePath extends string, TSubPathPattern extends st
`${TBasePath}/${TSubPathPattern}` | TSubPathPattern
>;

type GetParams<TOperation> = TOperation extends (...args: any) => any
? Parameters<TOperation>[0] extends void
? void
: Parameters<TOperation>[0]
: never;
type GetParams<TOperation> = TOperation extends (...args: any) => any ? Parameters<TOperation>[0] : never;

type GetResult<TOperation> = TOperation extends (...args: any) => any ? ReturnType<TOperation> : never;

Expand Down
2 changes: 1 addition & 1 deletion packages/ui-contexts/src/ServerContext/ServerContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type ServerContextValue = {
callEndpoint: <TMethod extends Method, TPath extends PathFor<TMethod>>(
method: TMethod,
path: TPath,
params: Serialized<OperationParams<TMethod, MatchPathPattern<TPath>>>,
params: OperationParams<TMethod, MatchPathPattern<TPath>>,
) => Promise<Serialized<OperationResult<TMethod, MatchPathPattern<TPath>>>>;
uploadToEndpoint: (
endpoint: PathFor<'POST'>,
Expand Down
14 changes: 7 additions & 7 deletions packages/ui-contexts/src/hooks/useEndpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import { useCallback, useContext } from 'react';

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

export type EndpointFunction<TMethod extends Method, TPathPattern extends PathPattern> = (
params: Serialized<OperationParams<TMethod, TPathPattern>>,
) => Promise<Serialized<OperationResult<TMethod, TPathPattern>>>;
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>>>;

export const useEndpoint = <TMethod extends Method, TPath extends PathFor<TMethod>>(
method: TMethod,
path: TPath,
): EndpointFunction<TMethod, MatchPathPattern<TPath>> => {
const { callEndpoint } = useContext(ServerContext);

return useCallback(
(params: Serialized<OperationParams<TMethod, MatchPathPattern<TPath>>>) => callEndpoint(method, path, params),
[callEndpoint, path, method],
);
return useCallback((params: any) => callEndpoint(method, path, params), [callEndpoint, path, method]);
};

0 comments on commit cf08a10

Please sign in to comment.