Skip to content

Commit f2dede0

Browse files
committed
refactor endpoints to generate documentation
1 parent 67f15fa commit f2dede0

File tree

3 files changed

+207
-62
lines changed

3 files changed

+207
-62
lines changed
Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,88 @@
11
import { CustomSounds } from '@rocket.chat/models';
22
import { isCustomSoundsListProps } from '@rocket.chat/rest-typings';
3+
import { ajv } from '@rocket.chat/rest-typings/src/v1/Ajv';
34
import { escapeRegExp } from '@rocket.chat/string-helpers';
45

56
import { API } from '../api';
7+
import type { ExtractEndpoints } from '../definition';
68
import { getPaginationItems } from '../helpers/getPaginationItems';
79

8-
API.v1.addRoute(
10+
const customSounds = API.v1.get(
911
'custom-sounds.list',
10-
{ authRequired: true, validateParams: isCustomSoundsListProps },
1112
{
12-
async get() {
13-
const { offset, count } = await getPaginationItems(this.queryParams as Record<string, string | number | null | undefined>);
14-
const { sort, query } = await this.parseJsonQuery();
15-
16-
const { name } = this.queryParams;
13+
response: {
14+
200: ajv.compile({
15+
additionalProperties: false,
16+
type: 'object',
17+
properties: {
18+
count: {
19+
type: 'number',
20+
description: 'The number of sounds returned in this response.',
21+
},
22+
offset: {
23+
type: 'number',
24+
description: 'The number of sounds that were skipped in this response.',
25+
},
26+
total: {
27+
type: 'number',
28+
description: 'The total number of sounds that match the query.',
29+
},
30+
sounds: {
31+
type: 'array',
32+
items: {
33+
type: 'object',
34+
properties: {
35+
_id: {
36+
type: 'string',
37+
},
38+
name: {
39+
type: 'string',
40+
},
41+
extension: {
42+
type: 'string',
43+
},
44+
createdAt: {
45+
type: 'string',
46+
},
47+
updatedAt: {
48+
type: 'string',
49+
},
50+
},
51+
required: ['_id', 'name', 'extension', 'createdAt', 'updatedAt'],
52+
},
53+
},
54+
},
55+
required: ['count', 'offset', 'total', 'sounds'],
56+
}),
57+
},
58+
query: isCustomSoundsListProps,
59+
authRequired: true,
60+
},
61+
async function action() {
62+
const { offset, count } = await getPaginationItems(this.queryParams as Record<string, string | number | null | undefined>);
63+
const { sort, query } = await this.parseJsonQuery();
1764

18-
const filter = {
19-
...query,
20-
...(name ? { name: { $regex: escapeRegExp(name as string), $options: 'i' } } : {}),
21-
};
65+
const { name } = this.queryParams;
2266

23-
const { cursor, totalCount } = CustomSounds.findPaginated(filter, {
24-
sort: sort || { name: 1 },
25-
skip: offset,
26-
limit: count,
27-
});
67+
const filter = {
68+
...query,
69+
...(name ? { name: { $regex: escapeRegExp(name as string), $options: 'i' } } : {}),
70+
};
2871

29-
const [sounds, total] = await Promise.all([cursor.toArray(), totalCount]);
72+
const { cursor, totalCount } = CustomSounds.findPaginated(filter, {
73+
sort: sort || { name: 1 },
74+
skip: offset,
75+
limit: count,
76+
});
77+
const [sounds, total] = await Promise.all([cursor.toArray(), totalCount]);
3078

31-
return API.v1.success({
32-
sounds,
33-
count: sounds.length,
34-
offset,
35-
total,
36-
});
37-
},
79+
return API.v1.success({
80+
sounds,
81+
count: sounds.length,
82+
offset,
83+
total,
84+
});
3885
},
3986
);
87+
88+
export type CustomSoundsProps = ExtractEndpoints<typeof customSounds>;
Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
import { Federation, FederationEE } from '@rocket.chat/core-services';
22
import { License } from '@rocket.chat/license';
33
import { isFederationVerifyMatrixIdProps } from '@rocket.chat/rest-typings';
4+
import { ajv } from '@rocket.chat/rest-typings/src/v1/Ajv';
45

56
import { API } from '../api';
67

7-
API.v1.addRoute(
8-
'federation/matrixIds.verify',
9-
{
10-
authRequired: true,
11-
validateParams: isFederationVerifyMatrixIdProps,
12-
},
13-
{
14-
async get() {
8+
API.v1
9+
.get(
10+
'federation/matrixIds.verify',
11+
{
12+
authRequired: true,
13+
query: isFederationVerifyMatrixIdProps,
14+
response: {
15+
200: ajv.compile({
16+
type: 'object',
17+
properties: {
18+
results: {
19+
type: 'object',
20+
patternProperties: {
21+
'^[a-zA-Z0-9_-]+$': {
22+
type: 'string',
23+
},
24+
},
25+
additionalProperties: false,
26+
},
27+
},
28+
required: ['results'],
29+
}),
30+
},
31+
},
32+
async function () {
1533
const { matrixIds } = this.queryParams;
1634

1735
const federationService = License.hasValidLicense() ? FederationEE : Federation;
@@ -20,23 +38,39 @@ API.v1.addRoute(
2038

2139
return API.v1.success({ results: Object.fromEntries(results) });
2240
},
23-
},
24-
);
25-
26-
API.v1.addRoute(
27-
'federation/configuration.verify',
28-
{ authRequired: true, permissionsRequired: ['view-privileged-setting'] },
29-
{
30-
async get() {
41+
)
42+
.get(
43+
'federation/configuration.verify',
44+
{
45+
authRequired: true,
46+
permissionsRequired: ['view-privileged-setting'],
47+
response: {
48+
200: ajv.compile({
49+
type: 'object',
50+
properties: {
51+
externalReachability: { type: 'object', properties: { ok: { type: 'boolean' } }, required: ['ok'] },
52+
appservice: { type: 'object', properties: { ok: { type: 'boolean' } }, required: ['ok'] },
53+
},
54+
required: ['externalReachability', 'appservice'],
55+
}),
56+
400: ajv.compile({
57+
type: 'object',
58+
properties: {
59+
externalReachability: { type: 'object', properties: { ok: { type: 'boolean' } }, required: ['ok'] },
60+
appservice: { type: 'object', properties: { ok: { type: 'boolean' } }, required: ['ok'] },
61+
},
62+
required: ['externalReachability', 'appservice'],
63+
}),
64+
},
65+
},
66+
async () => {
3167
const service = License.hasValidLicense() ? FederationEE : Federation;
3268

3369
const status = await service.configurationStatus();
3470

3571
if (!status.externalReachability.ok || !status.appservice.ok) {
3672
return API.v1.failure(status);
3773
}
38-
3974
return API.v1.success(status);
4075
},
41-
},
42-
);
76+
);

apps/meteor/app/api/server/v1/invites.ts

Lines changed: 81 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
isValidateInviteTokenProps,
66
isSendInvitationEmailParams,
77
} from '@rocket.chat/rest-typings';
8+
import { ajv } from '@rocket.chat/rest-typings/src/v1/Ajv';
89

910
import { findOrCreateInvite } from '../../../invites/server/functions/findOrCreateInvite';
1011
import { listInvites } from '../../../invites/server/functions/listInvites';
@@ -14,33 +15,94 @@ import { useInviteToken } from '../../../invites/server/functions/useInviteToken
1415
import { validateInviteToken } from '../../../invites/server/functions/validateInviteToken';
1516
import { API } from '../api';
1617

17-
API.v1.addRoute(
18-
'listInvites',
19-
{
20-
authRequired: true,
21-
},
22-
{
23-
async get() {
18+
API.v1
19+
.get(
20+
'listInvites',
21+
{
22+
authRequired: true,
23+
response: {
24+
200: ajv.compile({
25+
additionalProperties: false,
26+
type: 'object',
27+
properties: {
28+
invites: {
29+
type: 'array',
30+
items: {
31+
type: 'object',
32+
properties: {
33+
_id: {
34+
type: 'string',
35+
},
36+
rid: {
37+
type: 'string',
38+
},
39+
createdAt: {
40+
type: 'string',
41+
},
42+
expireAt: {
43+
type: 'string',
44+
},
45+
maxUses: {
46+
type: 'number',
47+
},
48+
uses: {
49+
type: 'number',
50+
},
51+
},
52+
required: ['_id', 'rid', 'createdAt', 'expireAt', 'maxUses', 'uses'],
53+
},
54+
},
55+
},
56+
required: ['invites'],
57+
}),
58+
},
59+
},
60+
61+
async function () {
2462
const result = await listInvites(this.userId);
2563
return API.v1.success(result);
2664
},
27-
},
28-
);
65+
)
66+
.post(
67+
'findOrCreateInvite',
68+
{
69+
authRequired: true,
70+
body: isFindOrCreateInviteParams,
71+
response: {
72+
200: ajv.compile({
73+
additionalProperties: false,
74+
type: 'object',
75+
properties: {
76+
_id: {
77+
type: 'string',
78+
},
79+
rid: {
80+
type: 'string',
81+
},
82+
createdAt: {
83+
type: 'string',
84+
},
85+
expireAt: {
86+
type: 'string',
87+
},
88+
maxUses: {
89+
type: 'number',
90+
},
91+
uses: {
92+
type: 'number',
93+
},
94+
},
95+
required: ['_id', 'rid', 'createdAt', 'expireAt', 'maxUses', 'uses'],
96+
}),
97+
},
98+
},
2999

30-
API.v1.addRoute(
31-
'findOrCreateInvite',
32-
{
33-
authRequired: true,
34-
validateParams: isFindOrCreateInviteParams,
35-
},
36-
{
37-
async post() {
100+
async function () {
38101
const { rid, days, maxUses } = this.bodyParams;
39102

40103
return API.v1.success((await findOrCreateInvite(this.userId, { rid, days, maxUses })) as IInvite);
41104
},
42-
},
43-
);
105+
);
44106

45107
API.v1.addRoute(
46108
'removeInvite/:_id',

0 commit comments

Comments
 (0)