Skip to content

Commit 62620f3

Browse files
chore: migrate roles.list endpoint to new chained API pattern with OpenAPI
1 parent 3c30636 commit 62620f3

File tree

3 files changed

+85
-46
lines changed

3 files changed

+85
-46
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@rocket.chat/meteor": patch
3+
"@rocket.chat/rest-typings": patch
4+
---
5+
6+
Migrates the `roles.list` REST API endpoint from the legacy `API.v1.addRoute` pattern to the new chained `API.v1.get()` pattern with AJV response schema validation and OpenAPI documentation support.

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

Lines changed: 79 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { api, Authorization } from '@rocket.chat/core-services';
22
import type { IRole } from '@rocket.chat/core-typings';
33
import { Roles, Users } from '@rocket.chat/models';
4-
import { ajv, isRoleAddUserToRoleProps, isRoleDeleteProps, isRoleRemoveUserFromRoleProps } from '@rocket.chat/rest-typings';
4+
import {
5+
ajv,
6+
isRoleAddUserToRoleProps,
7+
isRoleDeleteProps,
8+
isRoleRemoveUserFromRoleProps,
9+
validateUnauthorizedErrorResponse,
10+
} from '@rocket.chat/rest-typings';
511
import { check, Match } from 'meteor/check';
612
import { Meteor } from 'meteor/meteor';
713

@@ -17,18 +23,6 @@ import { API } from '../api';
1723
import { getPaginationItems } from '../helpers/getPaginationItems';
1824
import { getUserFromParams } from '../helpers/getUserFromParams';
1925

20-
API.v1.addRoute(
21-
'roles.list',
22-
{ authRequired: true },
23-
{
24-
async get() {
25-
const roles = await Roles.find({}, { projection: { _updatedAt: 0 } }).toArray();
26-
27-
return API.v1.success({ roles });
28-
},
29-
},
30-
);
31-
3226
API.v1.addRoute(
3327
'roles.sync',
3428
{ authRequired: true },
@@ -225,38 +219,82 @@ API.v1.addRoute(
225219
},
226220
);
227221

228-
const rolesRoutes = API.v1.get(
229-
'roles.getUsersInPublicRoles',
230-
{
231-
authRequired: true,
232-
response: {
233-
200: ajv.compile<{
234-
users: {
235-
_id: string;
236-
username: string;
237-
roles: string[];
238-
}[];
239-
}>({
240-
type: 'object',
241-
properties: {
242-
users: {
243-
type: 'array',
244-
items: {
245-
type: 'object',
246-
properties: { _id: { type: 'string' }, username: { type: 'string' }, roles: { type: 'array', items: { type: 'string' } } },
222+
const rolesRoutes = API.v1
223+
.get(
224+
'roles.list',
225+
{
226+
authRequired: true,
227+
response: {
228+
200: ajv.compile<{
229+
roles: IRole[];
230+
}>({
231+
type: 'object',
232+
properties: {
233+
roles: {
234+
type: 'array',
235+
items: {
236+
$ref: '#/components/schemas/IRole',
237+
},
238+
},
239+
success: {
240+
type: 'boolean',
241+
enum: [true],
247242
},
248243
},
249-
},
250-
}),
244+
required: ['roles', 'success'],
245+
additionalProperties: false,
246+
}),
247+
401: validateUnauthorizedErrorResponse,
248+
},
251249
},
252-
},
250+
async function action() {
251+
const roles = await Roles.find({}, { projection: { _updatedAt: 0 } }).toArray();
253252

254-
async () => {
255-
return API.v1.success({
256-
users: await Authorization.getUsersFromPublicRoles(),
257-
});
258-
},
259-
);
253+
return API.v1.success({ roles });
254+
},
255+
)
256+
.get(
257+
'roles.getUsersInPublicRoles',
258+
{
259+
authRequired: true,
260+
response: {
261+
200: ajv.compile<{
262+
users: {
263+
_id: string;
264+
username: string;
265+
roles: string[];
266+
}[];
267+
}>({
268+
type: 'object',
269+
properties: {
270+
users: {
271+
type: 'array',
272+
items: {
273+
type: 'object',
274+
properties: {
275+
_id: { type: 'string' },
276+
username: { type: 'string' },
277+
roles: { type: 'array', items: { type: 'string' } },
278+
},
279+
},
280+
},
281+
success: {
282+
type: 'boolean',
283+
enum: [true],
284+
},
285+
},
286+
required: ['users', 'success'],
287+
additionalProperties: false,
288+
}),
289+
401: validateUnauthorizedErrorResponse,
290+
},
291+
},
292+
async function action() {
293+
return API.v1.success({
294+
users: await Authorization.getUsersFromPublicRoles(),
295+
});
296+
},
297+
);
260298

261299
type RolesEndpoints = ExtractRoutesFromAPI<typeof rolesRoutes>;
262300

packages/rest-typings/src/v1/roles.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,6 @@ type RoleSyncProps = {
118118
};
119119

120120
export type RolesEndpoints = {
121-
'/v1/roles.list': {
122-
GET: () => {
123-
roles: IRole[];
124-
};
125-
};
126121
'/v1/roles.sync': {
127122
GET: (params: RoleSyncProps) => {
128123
roles: {

0 commit comments

Comments
 (0)