Skip to content

Commit

Permalink
Chore: convert invites, misc and subscriptions to TS and create defin…
Browse files Browse the repository at this point in the history
…itions (#25350)
  • Loading branch information
felipe-rod123 committed Jun 7, 2022
1 parent 7f6bc92 commit afcbf1c
Show file tree
Hide file tree
Showing 17 changed files with 836 additions and 209 deletions.
16 changes: 15 additions & 1 deletion apps/meteor/app/api/server/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type PartialThis = {
};

type ActionThis<TMethod extends Method, TPathPattern extends PathPattern, TOptions> = {
readonly requestIp: string;
urlParams: UrlParams<TPathPattern>;
// TODO make it unsafe
readonly queryParams: TMethod extends 'GET'
Expand Down Expand Up @@ -110,16 +111,29 @@ type ActionThis<TMethod extends Method, TPathPattern extends PathPattern, TOptio
};
/* @deprecated */
getUserFromParams(): IUser;
/* @deprecated */
getUserInfo(me: IUser): TOptions extends { authRequired: true }
? IUser & {
email?: string;
settings: {
profile: {};
preferences: unknown;
};
avatarUrl: string;
}
: undefined;
insertUserObject<T>({ object, userId }: { object: { [key: string]: unknown }; userId: string }): { [key: string]: unknown } & T;
composeRoomWithLastMessage(room: IRoom, userId: string): IRoom;
} & (TOptions extends { authRequired: true }
? {
readonly user: IUser;
readonly userId: string;
readonly token: string;
}
: {
readonly user: null;
readonly userId: null;
readonly userId: undefined;
readonly token?: string;
});

export type ResultFor<TMethod extends Method, TPathPattern extends PathPattern> =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* eslint-disable react-hooks/rules-of-hooks */
import { IInvite } from '@rocket.chat/core-typings';
import { isFindOrCreateInviteParams, isUseInviteTokenProps, isValidateInviteTokenProps } from '@rocket.chat/rest-typings';

import { API } from '../api';
import { findOrCreateInvite } from '../../../invites/server/functions/findOrCreateInvite';
import { removeInvite } from '../../../invites/server/functions/removeInvite';
Expand All @@ -7,24 +11,28 @@ import { validateInviteToken } from '../../../invites/server/functions/validateI

API.v1.addRoute(
'listInvites',
{ authRequired: true },
{
get() {
const result = Promise.await(listInvites(this.userId));
authRequired: true,
},
{
async get() {
const result = await listInvites(this.userId);
return API.v1.success(result);
},
},
);

API.v1.addRoute(
'findOrCreateInvite',
{ authRequired: true },
{
post() {
authRequired: true,
validateParams: isFindOrCreateInviteParams,
},
{
async post() {
const { rid, days, maxUses } = this.bodyParams;
const result = Promise.await(findOrCreateInvite(this.userId, { rid, days, maxUses }));

return API.v1.success(result);
return API.v1.success((await findOrCreateInvite(this.userId, { rid, days, maxUses })) as IInvite);
},
},
);
Expand All @@ -33,44 +41,44 @@ API.v1.addRoute(
'removeInvite/:_id',
{ authRequired: true },
{
delete() {
async delete() {
const { _id } = this.urlParams;
const result = Promise.await(removeInvite(this.userId, { _id }));

return API.v1.success(result);
return API.v1.success(await removeInvite(this.userId, { _id }));
},
},
);

API.v1.addRoute(
'useInviteToken',
{ authRequired: true },
{
post() {
authRequired: true,
validateParams: isUseInviteTokenProps,
},
{
async post() {
const { token } = this.bodyParams;
// eslint-disable-next-line react-hooks/rules-of-hooks
const result = Promise.await(useInviteToken(this.userId, token));

return API.v1.success(result);
return API.v1.success(await useInviteToken(this.userId, token));
},
},
);

API.v1.addRoute(
'validateInviteToken',
{ authRequired: false },
{
post() {
authRequired: false,
validateParams: isValidateInviteTokenProps,
},
{
async post() {
const { token } = this.bodyParams;

let valid = true;
try {
Promise.await(validateInviteToken(token));
} catch (e) {
valid = false;
return API.v1.success({ valid: Boolean(await validateInviteToken(token)) });
} catch (_) {
return API.v1.success({ valid: false });
}

return API.v1.success({ valid });
},
},
);
Loading

0 comments on commit afcbf1c

Please sign in to comment.