backend: Add fields query parameter to get rooms and room details endpoints
This commit is contained in:
parent
f7542c14e2
commit
00b1d8be93
@ -133,10 +133,21 @@ paths:
|
|||||||
summary: Get a list of OpenVidu Meet rooms
|
summary: Get a list of OpenVidu Meet rooms
|
||||||
description: >
|
description: >
|
||||||
Retrieves a list of OpenVidu Meet rooms that are currently active.
|
Retrieves a list of OpenVidu Meet rooms that are currently active.
|
||||||
|
You can specify a comma-separated list of fields (using the "fields" query parameter) to include only
|
||||||
|
those properties in the response (e.g. roomName, preferences).
|
||||||
tags:
|
tags:
|
||||||
- Room
|
- Room
|
||||||
security:
|
security:
|
||||||
- apiKeyInHeader: []
|
- apiKeyInHeader: []
|
||||||
|
parameters:
|
||||||
|
- name: fields
|
||||||
|
in: query
|
||||||
|
required: false
|
||||||
|
description: |
|
||||||
|
Comma-separated list of fields to include in the response.
|
||||||
|
For example: "roomName,preferences". Only allowed fields will be returned.
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
responses:
|
responses:
|
||||||
'200':
|
'200':
|
||||||
description: Successfully retrieved the list of OpenVidu Meet rooms
|
description: Successfully retrieved the list of OpenVidu Meet rooms
|
||||||
@ -249,6 +260,8 @@ paths:
|
|||||||
summary: Get details of an OpenVidu Meet room
|
summary: Get details of an OpenVidu Meet room
|
||||||
description: >
|
description: >
|
||||||
Retrieves the details of an OpenVidu Meet room with the specified room name.
|
Retrieves the details of an OpenVidu Meet room with the specified room name.
|
||||||
|
Additionally, you can specify a comma-separated list of fields (using the "fields" query parameter)
|
||||||
|
to include only those properties in the response (e.g. roomName, preferences).
|
||||||
tags:
|
tags:
|
||||||
- Room
|
- Room
|
||||||
security:
|
security:
|
||||||
@ -260,6 +273,14 @@ paths:
|
|||||||
description: The name of the room to retrieve
|
description: The name of the room to retrieve
|
||||||
schema:
|
schema:
|
||||||
type: string
|
type: string
|
||||||
|
- name: fields
|
||||||
|
in: query
|
||||||
|
required: false
|
||||||
|
description: |
|
||||||
|
Comma-separated list of fields to include in the response.
|
||||||
|
For example: "roomName,preferences". Only allowed fields will be returned.
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
responses:
|
responses:
|
||||||
'200':
|
'200':
|
||||||
description: Successfully retrieved the OpenVidu Meet room
|
description: Successfully retrieved the OpenVidu Meet room
|
||||||
|
|||||||
@ -22,14 +22,21 @@ export const createRoom = async (req: Request, res: Response) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getRooms = async (_req: Request, res: Response) => {
|
export const getRooms = async (req: Request, res: Response) => {
|
||||||
const logger = container.get(LoggerService);
|
const logger = container.get(LoggerService);
|
||||||
|
const fields = req.query.fields as string[] | undefined;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
logger.verbose('Getting rooms');
|
logger.verbose('Getting rooms');
|
||||||
|
|
||||||
const roomService = container.get(RoomService);
|
const roomService = container.get(RoomService);
|
||||||
const rooms = await roomService.listOpenViduRooms();
|
const rooms = await roomService.listOpenViduRooms();
|
||||||
|
|
||||||
|
if (fields && fields.length > 0) {
|
||||||
|
const filteredRooms = rooms.map((room) => filterObjectFields(room, fields));
|
||||||
|
return res.status(200).json(filteredRooms);
|
||||||
|
}
|
||||||
|
|
||||||
return res.status(200).json(rooms);
|
return res.status(200).json(rooms);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error('Error getting rooms');
|
logger.error('Error getting rooms');
|
||||||
@ -41,12 +48,19 @@ export const getRoom = async (req: Request, res: Response) => {
|
|||||||
const logger = container.get(LoggerService);
|
const logger = container.get(LoggerService);
|
||||||
|
|
||||||
const { roomName } = req.params;
|
const { roomName } = req.params;
|
||||||
|
const fields = req.query.fields as string[] | undefined;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
logger.verbose(`Getting room with id '${roomName}'`);
|
logger.verbose(`Getting room with id '${roomName}'`);
|
||||||
|
|
||||||
const roomService = container.get(RoomService);
|
const roomService = container.get(RoomService);
|
||||||
const room = await roomService.getOpenViduRoom(roomName);
|
const room = await roomService.getOpenViduRoom(roomName);
|
||||||
|
|
||||||
|
if (fields && fields.length > 0) {
|
||||||
|
const filteredRoom = filterObjectFields(room, fields);
|
||||||
|
return res.status(200).json(filteredRoom);
|
||||||
|
}
|
||||||
|
|
||||||
return res.status(200).json(room);
|
return res.status(200).json(room);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`Error getting room with id '${roomName}'`);
|
logger.error(`Error getting room with id '${roomName}'`);
|
||||||
@ -106,6 +120,19 @@ export const updateRoomPreferences = async (req: Request, res: Response) => {
|
|||||||
// }
|
// }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const filterObjectFields = (obj: Record<string, any>, fields: string[]): Record<string, any> => {
|
||||||
|
return fields.reduce(
|
||||||
|
(acc, field) => {
|
||||||
|
if (Object.prototype.hasOwnProperty.call(obj, field)) {
|
||||||
|
acc[field] = obj[field];
|
||||||
|
}
|
||||||
|
|
||||||
|
return acc;
|
||||||
|
},
|
||||||
|
{} as Record<string, any>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
const handleError = (res: Response, error: OpenViduMeetError | unknown) => {
|
const handleError = (res: Response, error: OpenViduMeetError | unknown) => {
|
||||||
const logger = container.get(LoggerService);
|
const logger = container.get(LoggerService);
|
||||||
logger.error(String(error));
|
logger.error(String(error));
|
||||||
|
|||||||
@ -70,3 +70,14 @@ export const validateRoomRequest = (req: Request, res: Response, next: NextFunct
|
|||||||
req.body = data;
|
req.body = data;
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const validateGetRoomQueryParams = (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
const fieldsQuery = req.query.fields as string | undefined;
|
||||||
|
|
||||||
|
if (fieldsQuery) {
|
||||||
|
const fields = fieldsQuery.split(',').map((f) => f.trim());
|
||||||
|
req.query.fields = fields;
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import { Router } from 'express';
|
|||||||
import bodyParser from 'body-parser';
|
import bodyParser from 'body-parser';
|
||||||
import * as roomCtrl from '../controllers/room.controller.js';
|
import * as roomCtrl from '../controllers/room.controller.js';
|
||||||
import { withUserBasicAuth, withValidApiKey } from '../middlewares/auth.middleware.js';
|
import { withUserBasicAuth, withValidApiKey } from '../middlewares/auth.middleware.js';
|
||||||
import { validateRoomRequest } from '../middlewares/request-validators/room-validator.middleware.js';
|
import { validateGetRoomQueryParams, validateRoomRequest } from '../middlewares/request-validators/room-validator.middleware.js';
|
||||||
|
|
||||||
export const roomRouter = Router();
|
export const roomRouter = Router();
|
||||||
|
|
||||||
@ -11,8 +11,8 @@ roomRouter.use(bodyParser.json());
|
|||||||
|
|
||||||
// Room Routes
|
// Room Routes
|
||||||
roomRouter.post('/', /*withValidApiKey,*/ validateRoomRequest, roomCtrl.createRoom);
|
roomRouter.post('/', /*withValidApiKey,*/ validateRoomRequest, roomCtrl.createRoom);
|
||||||
roomRouter.get('/', withUserBasicAuth, roomCtrl.getRooms);
|
roomRouter.get('/', withUserBasicAuth, validateGetRoomQueryParams, roomCtrl.getRooms);
|
||||||
roomRouter.get('/:roomName', withUserBasicAuth, roomCtrl.getRoom);
|
roomRouter.get('/:roomName', withUserBasicAuth, validateGetRoomQueryParams, roomCtrl.getRoom);
|
||||||
roomRouter.delete('/:roomName', withUserBasicAuth, roomCtrl.deleteRooms);
|
roomRouter.delete('/:roomName', withUserBasicAuth, roomCtrl.deleteRooms);
|
||||||
|
|
||||||
// Room preferences
|
// Room preferences
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user