Skip to content

refactor(api): migrate statistics endpoints to chained API pattern#39158

Open
smirk-dev wants to merge 1 commit intoRocketChat:developfrom
smirk-dev:refactor/statistics-api-chained-pattern
Open

refactor(api): migrate statistics endpoints to chained API pattern#39158
smirk-dev wants to merge 1 commit intoRocketChat:developfrom
smirk-dev:refactor/statistics-api-chained-pattern

Conversation

@smirk-dev
Copy link
Contributor

@smirk-dev smirk-dev commented Feb 27, 2026

Summary

Migrates all 3 statistics API endpoints from the legacy API.v1.addRoute() pattern to the new chained API.v1.get()/API.v1.post() pattern with OpenAPI-compatible AJV response schemas, query/body validation, and declare module augmentation for type-safe route registration.

This continues the ongoing REST API Migration effort toward full OpenAPI spec compliance — cc @diegolmello @guijin.


Endpoints Migrated

1. GET /v1/statistics

  • Query validation: Reuses existing isStatisticsProps validator from rest-typings
  • Response schema: IStats object with additionalProperties: true (stats are large dynamic objects)
  • Error responses: 401 Unauthorized, 403 Forbidden

2. GET /v1/statistics.list

  • Query validation: Reuses existing isStatisticsListProps validator (paginated request with fields, count, offset, sort, query)
  • Response schema: Paginated result with statistics[], count, offset, total
  • Error responses: 401 Unauthorized, 403 Forbidden

3. POST /v1/statistics.telemetry

  • Body validation: New isTelemetryPayload AJV validator requiring params[] array with eventName string
  • Response schema: Empty success ({ success: true })
  • Error responses: 401 Unauthorized

Architecture Changes

Before After
API.v1.addRoute("statistics", ...) const statisticsEndpoints = API.v1.get("statistics", ...)
No query/body validation AJV validators on query: and body:
No response schemas OpenAPI-compatible response: { 200, 401, 403 }
Types via StatisticsEndpoints in Endpoints union Types via declare module augmentation + ExtractRoutesFromAPI

Changed Files

File Change
apps/meteor/app/api/server/v1/stats.ts Full migration of 3 endpoints to chained pattern
packages/rest-typings/src/index.ts Removed StatisticsEndpoints from Endpoints interface, added export * from "./v1/statistics" barrel export
.changeset/refactor-statistics-api-chained-pattern.md Changeset

Validation

  • Zero TypeScript errors (VS Code diagnostics clean)
  • Prettier formatting unchanged (already compliant)
  • All existing validators (isStatisticsProps, isStatisticsListProps) reused — no behavioral changes

Summary by CodeRabbit

  • Refactor
    • Restructured statistics API endpoints with improved request validation and error handling. Statistics queries and telemetry submissions now include comprehensive payload validation and explicit authentication checks.

Migrates all 3 statistics API endpoints from the legacy API.v1.addRoute()
pattern to the new chained API.v1.get()/API.v1.post() pattern:

- GET statistics: with query validation (isStatisticsProps) and IStats response schema
- GET statistics.list: with query validation (isStatisticsListProps) and paginated response schema
- POST statistics.telemetry: with body validation (TelemetryPayload) and empty success response schema

Changes:
- apps/meteor/app/api/server/v1/stats.ts: Full endpoint migration with
  OpenAPI-compatible AJV response schemas, query/body validators, and
  declare module augmentation for type-safe route registration
- packages/rest-typings/src/index.ts: Removed StatisticsEndpoints from
  Endpoints interface (now provided via module augmentation), added
  barrel re-export for statistics validators
- Added 401/403 error response validators to all endpoints

This is part of the ongoing REST API migration effort toward full
OpenAPI spec compliance.
@smirk-dev smirk-dev requested review from a team as code owners February 27, 2026 16:38
@dionisio-bot
Copy link
Contributor

dionisio-bot bot commented Feb 27, 2026

Looks like this PR is not ready to merge, because of the following issues:

  • This PR is missing the 'stat: QA assured' label

Please fix the issues and try again

If you have any trouble, please check the PR guidelines

@changeset-bot
Copy link

changeset-bot bot commented Feb 27, 2026

🦋 Changeset detected

Latest commit: dcd5143

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 41 packages
Name Type
@rocket.chat/meteor Patch
@rocket.chat/core-typings Patch
@rocket.chat/rest-typings Patch
@rocket.chat/uikit-playground Patch
@rocket.chat/api-client Patch
@rocket.chat/apps Patch
@rocket.chat/core-services Patch
@rocket.chat/cron Patch
@rocket.chat/ddp-client Patch
@rocket.chat/fuselage-ui-kit Patch
@rocket.chat/gazzodown Patch
@rocket.chat/http-router Patch
@rocket.chat/livechat Patch
@rocket.chat/model-typings Patch
@rocket.chat/ui-avatar Patch
@rocket.chat/ui-client Patch
@rocket.chat/ui-contexts Patch
@rocket.chat/ui-voip Patch
@rocket.chat/web-ui-registration Patch
@rocket.chat/account-service Patch
@rocket.chat/authorization-service Patch
@rocket.chat/ddp-streamer Patch
@rocket.chat/omnichannel-transcript Patch
@rocket.chat/presence-service Patch
@rocket.chat/queue-worker Patch
@rocket.chat/abac Patch
@rocket.chat/federation-matrix Patch
@rocket.chat/license Patch
@rocket.chat/media-calls Patch
@rocket.chat/omnichannel-services Patch
@rocket.chat/pdf-worker Patch
@rocket.chat/presence Patch
rocketchat-services Patch
@rocket.chat/models Patch
@rocket.chat/network-broker Patch
@rocket.chat/omni-core-ee Patch
@rocket.chat/mock-providers Patch
@rocket.chat/ui-video-conf Patch
@rocket.chat/instance-status Patch
@rocket.chat/omni-core Patch
@rocket.chat/server-fetch Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 27, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2a32553 and dcd5143.

📒 Files selected for processing (3)
  • .changeset/refactor-statistics-api-chained-pattern.md
  • apps/meteor/app/api/server/v1/stats.ts
  • packages/rest-typings/src/index.ts
📜 Recent review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: cubic · AI code reviewer
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx,js}

📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)

**/*.{ts,tsx,js}: Write concise, technical TypeScript/JavaScript with accurate typing in Playwright tests
Avoid code comments in the implementation

Files:

  • packages/rest-typings/src/index.ts
  • apps/meteor/app/api/server/v1/stats.ts
🧠 Learnings (7)
📓 Common learnings
Learnt from: ahmed-n-abdeltwab
Repo: RocketChat/Rocket.Chat PR: 38974
File: apps/meteor/app/api/server/v1/im.ts:220-221
Timestamp: 2026-02-24T19:09:09.561Z
Learning: In RocketChat/Rocket.Chat OpenAPI migration PRs for apps/meteor/app/api/server/v1 endpoints, maintainers prefer to avoid any logic changes; style-only cleanups (like removing inline comments) may be deferred to follow-ups to keep scope tight.
📚 Learning: 2026-02-24T19:09:09.561Z
Learnt from: ahmed-n-abdeltwab
Repo: RocketChat/Rocket.Chat PR: 38974
File: apps/meteor/app/api/server/v1/im.ts:220-221
Timestamp: 2026-02-24T19:09:09.561Z
Learning: In RocketChat/Rocket.Chat OpenAPI migration PRs for apps/meteor/app/api/server/v1 endpoints, maintainers prefer to avoid any logic changes; style-only cleanups (like removing inline comments) may be deferred to follow-ups to keep scope tight.

Applied to files:

  • .changeset/refactor-statistics-api-chained-pattern.md
📚 Learning: 2026-02-25T20:10:16.987Z
Learnt from: ahmed-n-abdeltwab
Repo: RocketChat/Rocket.Chat PR: 38913
File: packages/ddp-client/src/legacy/types/SDKLegacy.ts:34-34
Timestamp: 2026-02-25T20:10:16.987Z
Learning: In the RocketChat/Rocket.Chat monorepo, packages/ddp-client and apps/meteor do not use TypeScript project references. Module augmentations in apps/meteor (e.g., declare module 'rocket.chat/rest-typings') are not visible when compiling packages/ddp-client in isolation, which is why legacy SDK methods that depend on OperationResult types for OpenAPI-migrated endpoints must remain commented out.

Applied to files:

  • .changeset/refactor-statistics-api-chained-pattern.md
  • apps/meteor/app/api/server/v1/stats.ts
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In the Rocket.Chat repository, do not reference Biome lint rules in code review feedback. Biome is not used even if biome.json exists; only reference Biome rules if there is explicit, project-wide usage documented. For TypeScript files, review lint implications without Biome guidance unless the project enables Biome rules.

Applied to files:

  • packages/rest-typings/src/index.ts
  • apps/meteor/app/api/server/v1/stats.ts
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In this repository (RocketChat/Rocket.Chat), Biome lint rules are not used even if a biome.json exists. When reviewing TypeScript files (e.g., packages/ui-voip/src/providers/useMediaSession.ts), ensure lint suggestions do not reference Biome-specific rules. Rely on general ESLint/TypeScript lint rules and project conventions instead.

Applied to files:

  • packages/rest-typings/src/index.ts
  • apps/meteor/app/api/server/v1/stats.ts
📚 Learning: 2026-02-24T19:09:01.522Z
Learnt from: ahmed-n-abdeltwab
Repo: RocketChat/Rocket.Chat PR: 38974
File: apps/meteor/app/api/server/v1/im.ts:220-221
Timestamp: 2026-02-24T19:09:01.522Z
Learning: In Rocket.Chat OpenAPI migration PRs for endpoints under apps/meteor/app/api/server/v1, avoid introducing logic changes. Only perform scope-tight changes that preserve behavior; style-only cleanups (e.g., removing inline comments) may be deferred to follow-ups to keep the migration PR focused.

Applied to files:

  • apps/meteor/app/api/server/v1/stats.ts
📚 Learning: 2026-02-23T17:53:06.802Z
Learnt from: ggazzo
Repo: RocketChat/Rocket.Chat PR: 35995
File: apps/meteor/app/api/server/v1/rooms.ts:1107-1112
Timestamp: 2026-02-23T17:53:06.802Z
Learning: During PR reviews that touch endpoint files under apps/meteor/app/api/server/v1, enforce strict scope: if a PR targets a specific endpoint (e.g., rooms.favorite), do not propose changes to unrelated endpoints (e.g., rooms.invite) unless maintainers explicitly request them. Focus feedback on the touched endpoint's behavior, API surface, and related tests; avoid broad cross-endpoint changes in the same PR unless requested.

Applied to files:

  • apps/meteor/app/api/server/v1/stats.ts
🔇 Additional comments (7)
.changeset/refactor-statistics-api-chained-pattern.md (1)

1-5: LGTM!

The changeset accurately documents the migration scope and uses an appropriate patch bump for this internal refactoring.

packages/rest-typings/src/index.ts (1)

256-256: LGTM!

The barrel export correctly exposes the statistics types, aligning with the module augmentation approach used in the migration.

apps/meteor/app/api/server/v1/stats.ts (5)

1-14: LGTM!

Imports are well-organized and all are utilized in the implementation.


16-47: LGTM!

The endpoint correctly implements the chained API pattern with appropriate query validation and response schemas. The additionalProperties: true accommodates the dynamic nature of IStats.


49-108: LGTM!

The paginated endpoint correctly implements the chained API pattern with proper response schema. The type assertion on line 92 is a reasonable workaround for the getPaginationItems signature.


173-186: LGTM!

The module augmentation correctly extends the Endpoints interface using ExtractRoutesFromAPI, enabling type-safe route registration. The approach aligns with the established pattern for OpenAPI migrations.


143-145: Remove the deprecated validateParams property—use body alone.

The validateParams option is deprecated. POST endpoints should validate using the body property only. The handler already accesses the validated data via this.bodyParams on line 162. Remove line 144.


Walkthrough

Refactors statistics API endpoints (statistics, statistics.list, statistics.telemetry) from legacy API.v1.addRoute to chained API.v1.get()/post() pattern, with OpenAPI-compatible AJV validation schemas, request/response validation, and type-safe module augmentation for Endpoints.

Changes

Cohort / File(s) Summary
Changelog
.changeset/refactor-statistics-api-chained-pattern.md
Documents migration of three statistics endpoints to chained API pattern with OpenAPI schemas and validation.
Statistics API Refactoring
apps/meteor/app/api/server/v1/stats.ts
Converts legacy route handlers into named endpoint constants with explicit auth, AJV query/body validation schemas, telemetry payload types, and structured action functions. Introduces statisticsEndpoints, statisticsListEndpoints, statisticsTelemetryEndpoints constants and corresponding type extractions via ExtractRoutesFromAPI.
Rest Typings Restructuring
packages/rest-typings/src/index.ts
Changes from direct StatisticsEndpoints interface extension to re-exporting the statistics module, allowing types to be extended via module augmentation in the implementation file.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Suggested labels

type: chore

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'refactor(api): migrate statistics endpoints to chained API pattern' accurately and clearly describes the main change: migrating statistics API endpoints from the legacy addRoute pattern to the chained API pattern.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

No issues found across 3 files

@ggazzo ggazzo added this to the 8.3.0 milestone Feb 27, 2026
@codecov
Copy link

codecov bot commented Feb 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 70.69%. Comparing base (2a32553) to head (dcd5143).
⚠️ Report is 5 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop   #39158      +/-   ##
===========================================
- Coverage    70.70%   70.69%   -0.01%     
===========================================
  Files         3195     3195              
  Lines       113095   113095              
  Branches     20487    20514      +27     
===========================================
- Hits         79963    79957       -6     
- Misses       31084    31086       +2     
- Partials      2048     2052       +4     
Flag Coverage Δ
unit 71.33% <ø> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@smirk-dev
Copy link
Contributor Author

@ggazzo any further changes required sir, i see you have added the pr to a milestone, but it seems there has been no action taken on the pr yet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants