Skip to content

Commit

Permalink
Chore: add Ajv JSON Schema to api/v1 (#25601)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipe-rod123 authored Jun 3, 2022
1 parent 7e8d6d2 commit 0b1073e
Show file tree
Hide file tree
Showing 36 changed files with 3,577 additions and 296 deletions.
11 changes: 9 additions & 2 deletions apps/meteor/app/api/server/v1/im.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
* Docs: https://github.com/RocketChat/developer-docs/blob/master/reference/api/rest-api/endpoints/team-collaboration-endpoints/im-endpoints
*/
import type { IMessage, IRoom, ISetting, ISubscription, IUpload, IUser } from '@rocket.chat/core-typings';
import { isDmDeleteProps, isDmFileProps, isDmMemberProps, isDmMessagesProps, isDmCreateProps } from '@rocket.chat/rest-typings';
import {
isDmDeleteProps,
isDmFileProps,
isDmMemberProps,
isDmMessagesProps,
isDmCreateProps,
isDmHistoryProps,
} from '@rocket.chat/rest-typings';
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';

Expand Down Expand Up @@ -240,7 +247,7 @@ API.v1.addRoute(

API.v1.addRoute(
['dm.history', 'im.history'],
{ authRequired: true },
{ authRequired: true, validateParams: isDmHistoryProps },
{
async get() {
const { offset = 0, count = 20 } = this.getPaginationItems();
Expand Down
7 changes: 4 additions & 3 deletions apps/meteor/app/livechat/imports/server/rest/departments.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isLivechatDepartmentProps } from '@rocket.chat/rest-typings';
import { Match, check } from 'meteor/check';

import { API } from '../../../../api/server';
Expand All @@ -14,9 +15,9 @@ import {

API.v1.addRoute(
'livechat/department',
{ authRequired: true },
{ authRequired: true, validateParams: isLivechatDepartmentProps },
{
get() {
async get() {
const { offset, count } = this.getPaginationItems();
const { sort } = this.parseJsonQuery();

Expand All @@ -26,7 +27,7 @@ API.v1.addRoute(
findDepartments({
userId: this.userId,
text,
enabled,
enabled: enabled === 'true',
onlyMyDepartments: onlyMyDepartments === 'true',
excludeDepartmentId,
pagination: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ export const useDepartmentsList = (
const t = useTranslation();
const [itemsList, setItemsList] = useState(() => new RecordList<ILivechatDepartmentRecord>());
const reload = useCallback(() => setItemsList(new RecordList<ILivechatDepartmentRecord>()), []);
const endpoint = 'livechat/department';

const getDepartments = useEndpoint('GET', endpoint);
const getDepartments = useEndpoint('GET', 'livechat/department');

useComponentDidUpdate(() => {
options && reload();
Expand All @@ -44,7 +43,7 @@ export const useDepartmentsList = (
count: end + start,
sort: `{ "name": 1 }`,
excludeDepartmentId: options.excludeDepartmentId,
enabled: options.enabled,
enabled: options.enabled ? 'true' : 'false',
});

const items = departments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const useQuery = (
userIdLoggedIn: string | null,
): {
sort: string;
open: boolean;
open: 'false';
roomName: string;
agents: string[];
count?: number;
Expand All @@ -31,7 +31,7 @@ const useQuery = (
useMemo(
() => ({
sort: JSON.stringify({ [column]: direction === 'asc' ? 1 : -1 }),
open: false,
open: 'false',
roomName: text || '',
agents: userIdLoggedIn ? [userIdLoggedIn] : [],
...(itemsPerPage && { count: itemsPerPage }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type useQueryType = (
debouncedSort: [string, 'asc' | 'desc'],
) => {
agentId?: ILivechatAgent['_id'];
includeOfflineAgents?: boolean;
includeOfflineAgents?: 'true' | 'false';
departmentId?: ILivechatAgent['_id'];
offset: number;
count: number;
Expand All @@ -25,7 +25,7 @@ export const useQuery: useQueryType = ({ servedBy, status, departmentId, itemsPe
useMemo(() => {
const query: {
agentId?: string;
includeOflineAgents?: boolean;
includeOflineAgents?: 'true' | 'false';
departmentId?: string;
sort: string;
count: number;
Expand All @@ -39,7 +39,7 @@ export const useQuery: useQueryType = ({ servedBy, status, departmentId, itemsPe
};

if (status !== 'online') {
query.includeOflineAgents = true;
query.includeOflineAgents = 'true';
}
if (servedBy) {
query.agentId = servedBy;
Expand Down
1 change: 0 additions & 1 deletion apps/meteor/tests/end-to-end/api/04-direct-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ describe('[Direct Messages]', function () {
.set(credentials)
.query({
roomId: directMessage._id,
userId: 'rocket.cat',
})
.expect('Content-Type', 'application/json')
.expect(200)
Expand Down
2 changes: 2 additions & 0 deletions packages/rest-typings/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ export * from './v1/channels/ChannelsConvertToTeamProps';
export * from './v1/channels/ChannelsSetReadOnlyProps';
export * from './v1/channels/ChannelsDeleteProps';
export * from './v1/dm';
export * from './v1/dm/DmHistoryProps';
export * from './v1/integrations';
export * from './v1/omnichannel';
export * from './v1/oauthapps';
export * from './helpers/PaginatedRequest';
export * from './helpers/PaginatedResult';
Expand Down
86 changes: 82 additions & 4 deletions packages/rest-typings/src/v1/banners.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,104 @@
import Ajv from 'ajv';
import type { BannerPlatform, IBanner } from '@rocket.chat/core-typings';

const ajv = new Ajv({
coerceTypes: true,
});

type BannersGetNew = {
platform: BannerPlatform;
bid: IBanner['_id'];
};

const BannersGetNewSchema = {
type: 'object',
properties: {
platform: {
type: 'string',
enum: ['1', '2'],
},
bid: {
type: 'string',
},
},
required: ['platform', 'bid'],
additionalProperties: false,
};

export const isBannersGetNewProps = ajv.compile<BannersGetNew>(BannersGetNewSchema);

type BannersId = {
platform: BannerPlatform;
};

const BannersIdSchema = {
type: 'object',
properties: {
platform: {
type: 'string',
},
},
required: ['platform'],
additionalProperties: false,
};

export const isBannersIdProps = ajv.compile<BannersId>(BannersIdSchema);

type Banners = {
platform: BannerPlatform;
};

const BannersSchema = {
type: 'object',
properties: {
platform: {
type: 'string',
},
},
required: ['platform'],
additionalProperties: false,
};

export const isBannersProps = ajv.compile<Banners>(BannersSchema);

type BannersDismiss = {
bannerId: string;
};

const BannersDismissSchema = {
type: 'object',
properties: {
bannerId: {
type: 'string',
},
},
required: ['bannerId'],
additionalProperties: false,
};

export const isBannersDismissProps = ajv.compile<BannersDismiss>(BannersDismissSchema);

export type BannersEndpoints = {
/* @deprecated */
'banners.getNew': {
GET: (params: { platform: BannerPlatform; bid: IBanner['_id'] }) => {
GET: (params: BannersGetNew) => {
banners: IBanner[];
};
};

'banners/:id': {
GET: (params: { platform: BannerPlatform }) => {
GET: (params: BannersId) => {
banners: IBanner[];
};
};

'banners': {
GET: (params: { platform: BannerPlatform }) => {
GET: (params: Banners) => {
banners: IBanner[];
};
};

'banners.dismiss': {
POST: (params: { bannerId: string }) => void;
POST: (params: BannersDismiss) => void;
};
};
Loading

0 comments on commit 0b1073e

Please sign in to comment.