diff --git a/.changeset/migrate-statistics-openapi.md b/.changeset/migrate-statistics-openapi.md new file mode 100644 index 0000000000000..d2f133cf2fdbb --- /dev/null +++ b/.changeset/migrate-statistics-openapi.md @@ -0,0 +1,5 @@ +--- +'@rocket.chat/meteor': patch +--- + +chore: Add OpenAPI support for statistics endpoint diff --git a/apps/meteor/app/api/server/v1/stats.ts b/apps/meteor/app/api/server/v1/stats.ts index 27cea2c310574..6f44ad69c59d2 100644 --- a/apps/meteor/app/api/server/v1/stats.ts +++ b/apps/meteor/app/api/server/v1/stats.ts @@ -1,23 +1,51 @@ +import { ajv, ajvQuery, validateBadRequestErrorResponse, validateUnauthorizedErrorResponse } from '@rocket.chat/rest-typings'; + import { getStatistics, getLastStatistics } from '../../../statistics/server'; import telemetryEvent from '../../../statistics/server/lib/telemetryEvents'; import { API } from '../api'; import { getPaginationItems } from '../helpers/getPaginationItems'; -API.v1.addRoute( +const StatisticsQuerySchema = { + type: 'object', + properties: { + refresh: { type: 'string', enum: ['true', 'false'] }, + }, + required: [], + additionalProperties: false, +} as const; + +const isStatisticsProps = ajvQuery.compile<{ refresh?: 'true' | 'false' }>(StatisticsQuerySchema); + +const statisticsResponseSchema = ajv.compile>({ + type: 'object', + properties: { + success: { type: 'boolean', enum: [true] }, + }, + required: ['success'], + additionalProperties: true, +}); + +API.v1.get( 'statistics', - { authRequired: true }, { - async get() { - const { refresh = 'false' } = this.queryParams; - - return API.v1.success( - await getLastStatistics({ - userId: this.userId, - refresh: refresh === 'true', - }), - ); + authRequired: true, + query: isStatisticsProps, + response: { + 200: statisticsResponseSchema, + 400: validateBadRequestErrorResponse, + 401: validateUnauthorizedErrorResponse, }, }, + async function action() { + const { refresh = 'false' } = this.queryParams; + + return API.v1.success( + await getLastStatistics({ + userId: this.userId, + refresh: refresh === 'true', + }), + ); + }, ); API.v1.addRoute(