Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: add Ajv JSON Schema to api/v1 #25601

Merged
merged 24 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c7f4ac5
chore: add Ajv to banners.ts (work in progress)
felipe-rod123 May 23, 2022
ae1de99
fix banners.ts and chat.ts JSON Schemas
felipe-rod123 May 25, 2022
3019f40
add cloud.ts and directory.ts json schemas
felipe-rod123 May 25, 2022
ec2000a
add more schemas
felipe-rod123 May 25, 2022
bb6c62d
add invites, ldap, licenses.ts json schemas
felipe-rod123 May 25, 2022
c187396
fix omnichannel.ts json schema (work in progress)
felipe-rod123 May 25, 2022
8f93798
fix more omnichannel.ts json schemas (work in progress)
felipe-rod123 May 25, 2022
b4d7745
chore: remove JSONSchemaType (work in progress)
felipe-rod123 May 27, 2022
1f83338
add JSON Schema to more files
felipe-rod123 May 27, 2022
b6c39b7
Merge remote-tracking branch 'origin/develop' into chore/add-json-sch…
ggazzo May 30, 2022
b360fb3
lint
ggazzo May 30, 2022
830f53a
Merge remote-tracking branch 'origin/develop' into chore/add-json-sch…
ggazzo Jun 1, 2022
669531e
Merge remote-tracking branch 'origin/chore/add-json-schema-to-api/v1'…
felipe-rod123 Jun 1, 2022
f21aa9a
add compile types to Ajv (work in progress)
felipe-rod123 Jun 1, 2022
3ba3bf5
add more Ajv compile types
felipe-rod123 Jun 2, 2022
cbe65a9
add voip.ts endpoint validations
felipe-rod123 Jun 2, 2022
4f571e7
Merge branch 'develop' into chore/add-json-schema-to-api/v1
felipe-rod123 Jun 3, 2022
dd9015e
Merge remote-tracking branch 'origin/develop' into chore/add-json-sch…
ggazzo Jun 3, 2022
a8ed0ec
Merge branch 'chore/add-json-schema-to-api/v1' of github.com:RocketCh…
ggazzo Jun 3, 2022
48c8c16
Lint
ggazzo Jun 3, 2022
25eeb05
Fix '/im.history' test
ggazzo Jun 3, 2022
cfdecd9
fix ominchannel departments
ggazzo Jun 3, 2022
5096876
Apply suggestions from code review
ggazzo Jun 3, 2022
f8287c2
Fix review
ggazzo Jun 3, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
ggazzo marked this conversation as resolved.
Show resolved Hide resolved
});

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
2 changes: 1 addition & 1 deletion apps/meteor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"obj:dev": "TEST_MODE=true yarn dev",
"stylelint": "stylelint \"app/**/*.css\" \"client/**/*.css\" \"app/**/*.less\" \"client/**/*.less\" \"ee/**/*.less\"",
"stylelint:fix": "stylelint --fix \"app/**/*.css\" \"client/**/*.css\" \"app/**/*.less\" \"client/**/*.less\" \"ee/**/*.less\"",
"typecheck": "cross-env NODE_OPTIONS=\"--max-old-space-size=4092\" tsc --noEmit --skipLibCheck",
"typecheck": "cross-env NODE_OPTIONS=\"--max-old-space-size=8184\" tsc --noEmit --skipLibCheck",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😭

"deploy": "npm run build && pm2 startOrRestart pm2.json",
"coverage": "nyc -r html mocha --config ./.mocharc.js",
"testci": "node .scripts/start.js",
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 @@ -155,7 +155,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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since bannerPlatform is an enum, maybe you can use the enum key here?

Suggested change
type: 'string',
enum: ['1', '2'],

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