The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/apps/meteor/client/providers/ServerProvider.tsx

86 lines
2.6 KiB

import type { Serialized } from '@rocket.chat/core-typings';
import type {
ServerMethodName,
ServerMethodParameters,
ServerMethodReturn,
StreamerCallbackArgs,
StreamNames,
StreamKeys,
} from '@rocket.chat/ddp-client';
import type { Method, PathFor, OperationParams, OperationResult, UrlParams, PathPattern } from '@rocket.chat/rest-typings';
import type { UploadResult } from '@rocket.chat/ui-contexts';
import { ServerContext } from '@rocket.chat/ui-contexts';
import { Meteor } from 'meteor/meteor';
import { compile } from 'path-to-regexp';
import type { ReactNode } from 'react';
import { sdk } from '../../app/utils/client/lib/SDKClient';
import { Info as info } from '../../app/utils/rocketchat.info';
const absoluteUrl = (path: string): string => Meteor.absoluteUrl(path);
const callMethod = <MethodName extends ServerMethodName>(
methodName: MethodName,
...args: ServerMethodParameters<MethodName>
): Promise<ServerMethodReturn<MethodName>> => Meteor.callAsync(methodName, ...args);
const callEndpoint = <TMethod extends Method, TPathPattern extends PathPattern>({
method,
pathPattern,
keys,
params,
}: {
method: TMethod;
pathPattern: TPathPattern;
keys: UrlParams<TPathPattern>;
params: OperationParams<TMethod, TPathPattern>;
}): 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;
case 'POST':
return sdk.rest.post(compiledPath, params as any) as any;
case 'PUT':
return sdk.rest.put(compiledPath, params as never) as never;
case 'DELETE':
return sdk.rest.delete(compiledPath, params as any) as any;
default:
throw new Error('Invalid HTTP method');
}
};
const uploadToEndpoint = (endpoint: PathFor<'POST'>, formData: any): Promise<UploadResult> => sdk.rest.post(endpoint as any, formData);
const getStream =
<N extends StreamNames>(
streamName: N,
_options?: {
retransmit?: boolean | undefined;
retransmitToSelf?: boolean | undefined;
},
) =>
<K extends StreamKeys<N>>(eventName: K, callback: (...args: StreamerCallbackArgs<N, K>) => void): (() => void) =>
sdk.stream(streamName, [eventName], callback).stop;
const contextValue = {
info,
absoluteUrl,
callMethod,
callEndpoint,
uploadToEndpoint,
getStream,
disconnect: () => Meteor.disconnect(),
reconnect: () => Meteor.reconnect(),
};
type ServerProviderProps = { children?: ReactNode };
const ServerProvider = ({ children }: ServerProviderProps) => <ServerContext.Provider children={children} value={contextValue} />;
export default ServerProvider;