Skip to content

Commit ad17d15

Browse files
committed
..
1 parent d4a5881 commit ad17d15

File tree

5 files changed

+67
-22
lines changed

5 files changed

+67
-22
lines changed

apps/meteor/app/utils/client/lib/RestApiClient.ts

+24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Meteor } from 'meteor/meteor';
33
import { Accounts } from 'meteor/accounts-base';
44

55
import { baseURI } from '../../../../client/lib/baseURI';
6+
import { process2faReturn } from '../../../../client/lib/2fa/process2faReturn';
67

78
export class RestApiClient extends RestClient {
89
getCredentials():
@@ -26,3 +27,26 @@ export class RestApiClient extends RestClient {
2627
export const APIClient = new RestApiClient({
2728
baseUrl: baseURI.replace(/\/$/, ''),
2829
});
30+
31+
APIClient.use(function (request, next) {
32+
try {
33+
return next(...request);
34+
} catch (e) {
35+
return new Promise((resolve, reject) => {
36+
process2faReturn({
37+
error: e,
38+
result: null,
39+
emailOrUsername: undefined,
40+
originalCallback: () => reject(e),
41+
onCode(code, method) {
42+
return resolve(
43+
next(request[0], request[1], {
44+
...request[2],
45+
headers: { ...request[2].headers, 'x-2fa-code': code, 'x-2fa-method': method },
46+
}),
47+
);
48+
},
49+
});
50+
});
51+
}
52+
});

apps/meteor/client/startup/routes.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ FlowRouter.route('/meet/:rid', {
6565
async action(_params, queryParams) {
6666
if (queryParams?.token !== undefined) {
6767
// visitor login
68-
const result = await APIClient.get(`/v1/livechat/visitor/${queryParams.token}`);
68+
const result = (await APIClient.get(`/v1/livechat/visitor/${queryParams.token}`)) as ILivechatVisitor;
6969

7070
if ('visitor' in result) {
7171
appLayout.render(<MeetPage />);

apps/meteor/ee/client/ecdh.ts

+10-14
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,14 @@ async function initEncryptedSession(): Promise<void> {
6565
}
6666

6767
initEncryptedSession();
68+
APIClient.use(async (request, next) => {
69+
const session = await sessionPromise;
6870

69-
// const _jqueryCall = APIClient._jqueryCall?.bind(APIClient);
70-
71-
// APIClient._jqueryCall = async (method, endpoint, params, body, headers = {}): Promise<any> => {
72-
// const session = await sessionPromise;
73-
74-
// if (!session) {
75-
// return _jqueryCall(method, endpoint, params, body, headers);
76-
// }
77-
78-
// const result = await _jqueryCall(method, endpoint, params, body, headers, 'text');
79-
// const decrypted = await session.decrypt(result);
80-
// const parsed = JSON.parse(decrypted);
81-
// return parsed;
82-
// };
71+
if (!session) {
72+
return next(...request);
73+
}
74+
const result = await (await next(...request)).text();
75+
const decrypted = await session.decrypt(result);
76+
const parsed = JSON.parse(decrypted);
77+
return parsed;
78+
});

packages/api-client/src/RestClientInterface.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import type { Serialized } from '@rocket.chat/core-typings/dist';
22
import type { MatchPathPattern, OperationParams, OperationResult, PathFor } from '@rocket.chat/rest-typings';
33

4+
type Next<T extends (...args: any[]) => any> = (...args: Parameters<T>) => ReturnType<T>;
5+
6+
export type Middleware<T extends (...args: any[]) => any> = (context: Parameters<T>, next: Next<T>) => ReturnType<T>;
7+
48
// eslint-disable-next-line @typescript-eslint/interface-name-prefix
59
export interface RestClientInterface {
610
get<TPath extends PathFor<'GET'>>(
@@ -38,4 +42,8 @@ export interface RestClientInterface {
3842
}
3943
| undefined;
4044
setCredentials(credentials: undefined | { 'X-User-Id': string; 'X-Auth-Token': string }): void;
45+
46+
use(middleware: Middleware<RestClientInterface['send']>): void;
47+
48+
send(endpoint: string, method: string, options: Omit<RequestInit, 'method'>): Promise<Response>;
4149
}

packages/api-client/src/index.ts

+24-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import type { Serialized } from '../../core-typings/dist';
22
import type { MatchPathPattern, OperationParams, OperationResult, PathFor } from '../../rest-typings/dist';
3-
import type { RestClientInterface } from './RestClientInterface';
3+
import type { Middleware, RestClientInterface } from './RestClientInterface';
44

55
export { RestClientInterface };
66

7+
const pipe =
8+
<T extends (...args: any[]) => any>(fn: T) =>
9+
(...args: Parameters<T>): ReturnType<T> =>
10+
fn(...args);
11+
712
export class RestClient implements RestClientInterface {
813
private readonly baseUrl: string;
914

@@ -61,7 +66,9 @@ export class RestClient implements RestClientInterface {
6166
: Record<string, string>,
6267
options?: Omit<RequestInit, 'method'>,
6368
): Promise<TPath extends PathFor<'GET'> ? Serialized<OperationResult<'GET', MatchPathPattern<TPath>>> : unknown> {
64-
return this.send(`${endpoint}?${this.getParams(params)}`, 'GET', options);
69+
return this.send(`${endpoint}?${this.getParams(params)}`, 'GET', options).then(function (response) {
70+
return response.json();
71+
});
6572
}
6673

6774
post: RestClientInterface['post'] = (endpoint, params, { headers, ...options } = {}) => {
@@ -75,6 +82,8 @@ export class RestClient implements RestClientInterface {
7582
},
7683

7784
...options,
85+
}).then(function (response) {
86+
return response.json();
7887
});
7988
};
8089

@@ -89,11 +98,15 @@ export class RestClient implements RestClientInterface {
8998
},
9099

91100
...options,
101+
}).then(function (response) {
102+
return response.json();
92103
});
93104
};
94105

95106
delete: RestClientInterface['delete'] = (endpoint, params, options) => {
96-
return this.send(endpoint, 'DELETE', options);
107+
return this.send(endpoint, 'DELETE', options).then(function (response) {
108+
return response.json();
109+
});
97110
};
98111

99112
protected getCredentialsAsHeaders(): Record<string, string> {
@@ -106,14 +119,12 @@ export class RestClient implements RestClientInterface {
106119
: {};
107120
}
108121

109-
protected send<T>(endpoint: string, method: string, { headers, ...options }: Omit<RequestInit, 'method'> = {}): Promise<T> {
122+
send(endpoint: string, method: string, { headers, ...options }: Omit<RequestInit, 'method'> = {}): Promise<Response> {
110123
return fetch(`${this.baseUrl}${endpoint}`, {
111124
...options,
112125
headers: { ...this.getCredentialsAsHeaders(), ...this.headers, ...headers },
113126
method,
114-
}).then(function (response) {
115-
return response.json();
116-
}) as Promise<T>;
127+
});
117128
}
118129

119130
protected getParams(data: Record<string, object | number | string | boolean> | void): string {
@@ -127,4 +138,10 @@ export class RestClient implements RestClientInterface {
127138
.join('&')
128139
: '';
129140
}
141+
142+
use(middleware: Middleware<RestClientInterface['send']>): void {
143+
this.send = function (this: RestClient, ...context: Parameters<RestClientInterface['send']>): ReturnType<RestClientInterface['send']> {
144+
return middleware(context, pipe(this.send.bind(this)));
145+
} as RestClientInterface['send'];
146+
}
130147
}

0 commit comments

Comments
 (0)