Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/red-squids-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rocket.chat/meteor": patch
"@rocket.chat/rest-typings": patch
---

Add OpenAPI support for the Rocket.Chat integrations.history API endpoints by migrating to a modern chained route definition syntax and utilizing shared AJV schemas for validation to enhance API documentation and ensure type safety through response validation.
148 changes: 116 additions & 32 deletions apps/meteor/app/api/server/v1/integrations.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { IIntegration, INewIncomingIntegration, INewOutgoingIntegration } from '@rocket.chat/core-typings';
import type { IIntegration, INewIncomingIntegration, INewOutgoingIntegration, IIntegrationHistory } from '@rocket.chat/core-typings';
import { Integrations, IntegrationHistory } from '@rocket.chat/models';
import type { PaginatedRequest, PaginatedResult } from '@rocket.chat/rest-typings';
import {
ajv,
isIntegrationsCreateProps,
isIntegrationsHistoryProps,
isIntegrationsRemoveProps,
isIntegrationsGetProps,
isIntegrationsUpdateProps,
Expand All @@ -22,6 +23,7 @@ import { updateIncomingIntegration } from '../../../integrations/server/methods/
import { addOutgoingIntegration } from '../../../integrations/server/methods/outgoing/addOutgoingIntegration';
import { deleteOutgoingIntegration } from '../../../integrations/server/methods/outgoing/deleteOutgoingIntegration';
import { updateOutgoingIntegration } from '../../../integrations/server/methods/outgoing/updateOutgoingIntegration';
import type { ExtractRoutesFromAPI } from '../ApiClass';
import { API } from '../api';
import { getPaginationItems } from '../helpers/getPaginationItems';
import { findOneIntegration } from '../lib/integrations';
Expand All @@ -43,45 +45,118 @@ API.v1.addRoute(
},
);

API.v1.addRoute(
type IntegrationsHistoryProps = PaginatedRequest<{ id: string }>;

const integrationsHistorySchema = {
type: 'object',
properties: {
id: { type: 'string', nullable: false, minLength: 1 },
offset: { type: 'number', nullable: true },
count: { type: 'number', nullable: true },
sort: { type: 'string', nullable: true },
query: { type: 'string', nullable: true },
},
required: ['id'],
additionalProperties: false,
};

const isIntegrationsHistoryProps = ajv.compile<IntegrationsHistoryProps>(integrationsHistorySchema);

const integrationsHistoryEndpoints = API.v1.get(
'integrations.history',
{
authRequired: true,
validateParams: isIntegrationsHistoryProps,
query: isIntegrationsHistoryProps,
permissionsRequired: {
GET: { permissions: ['manage-outgoing-integrations', 'manage-own-outgoing-integrations'], operation: 'hasAny' },
},
response: {
400: ajv.compile<{
error?: string;
errorType?: string;
stack?: string;
details?: string;
}>({
type: 'object',
properties: {
success: { type: 'boolean', enum: [false] },
stack: { type: 'string' },
error: { type: 'string' },
errorType: { type: 'string' },
details: { type: 'string' },
},
required: ['success'],
additionalProperties: false,
}),
401: ajv.compile({
type: 'object',
properties: {
success: { type: 'boolean', enum: [false] },
status: { type: 'string' },
message: { type: 'string' },
error: { type: 'string' },
errorType: { type: 'string' },
},
required: ['success'],
additionalProperties: false,
}),
403: ajv.compile({
type: 'object',
properties: {
success: { type: 'boolean', enum: [false] },
status: { type: 'string' },
message: { type: 'string' },
error: { type: 'string' },
errorType: { type: 'string' },
},
required: ['success'],
additionalProperties: false,
}),
200: ajv.compile<PaginatedResult<{ history: IIntegrationHistory[]; items: number }>>({
type: 'object',
properties: {
history: { type: 'array' },
offset: { type: 'string' },
items: { type: 'integer' },
count: { type: 'integer' },
total: { type: 'integer' },
success: { type: 'boolean', enum: [true] },
},
required: ['success'],
additionalProperties: false,
}),
},
},
{
async get() {
const { userId, queryParams } = this;

if (!queryParams.id || queryParams.id.trim() === '') {
return API.v1.failure('Invalid integration id.');
}

const { id } = queryParams;
const { offset, count } = await getPaginationItems(this.queryParams);
const { sort, fields: projection, query } = await this.parseJsonQuery();
const ourQuery = Object.assign(await mountIntegrationHistoryQueryBasedOnPermissions(userId, id), query);

const { cursor, totalCount } = IntegrationHistory.findPaginated(ourQuery, {
sort: sort || { _updatedAt: -1 },
skip: offset,
limit: count,
projection,
});

const [history, total] = await Promise.all([cursor.toArray(), totalCount]);

return API.v1.success({
history,
offset,
items: history.length,
count: history.length,
total,
});
},
async function action() {
const { userId, queryParams } = this;

if (!queryParams.id || queryParams.id.trim() === '') {
return API.v1.failure('Invalid integration id.');
}

const { id } = queryParams;
const { offset, count } = await getPaginationItems(this.queryParams);
const { sort, fields: projection, query } = await this.parseJsonQuery();
const ourQuery = Object.assign(await mountIntegrationHistoryQueryBasedOnPermissions(userId, id), query);

const { cursor, totalCount } = IntegrationHistory.findPaginated(ourQuery, {
sort: sort || { _updatedAt: -1 },
skip: offset,
limit: count,
projection,
});

const [history, total] = await Promise.all([cursor.toArray(), totalCount]);

return API.v1.success({
history,
offset,
items: history.length,
count: history.length,
total,
});
},
);

Expand Down Expand Up @@ -272,3 +347,12 @@ API.v1.addRoute(
},
},
);

type IntegrationsHistoryEndpoints = ExtractRoutesFromAPI<typeof integrationsHistoryEndpoints>;

export type IntegrationsEndpoints = IntegrationsHistoryEndpoints;

declare module '@rocket.chat/rest-typings' {
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
interface Endpoints extends IntegrationsHistoryEndpoints {}
}

This file was deleted.

1 change: 0 additions & 1 deletion packages/rest-typings/src/v1/integrations/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export * from './integrations';

export * from './IntegrationsCreateProps';
export * from './IntegrationsHistoryProps';
export * from './IntegrationsRemoveProps';
export * from './IntegrationsGetProps';
export * from './IntegrationsUpdateProps';
Expand Down
10 changes: 1 addition & 9 deletions packages/rest-typings/src/v1/integrations/integrations.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { IIntegration, IIntegrationHistory } from '@rocket.chat/core-typings';
import type { IIntegration } from '@rocket.chat/core-typings';

import type { IntegrationsCreateProps } from './IntegrationsCreateProps';
import type { IntegrationsGetProps } from './IntegrationsGetProps';
import type { IntegrationsHistoryProps } from './IntegrationsHistoryProps';
import type { IntegrationsListProps } from './IntegrationsListProps';
import type { IntegrationsRemoveProps } from './IntegrationsRemoveProps';
import type { IntegrationsUpdateProps } from './IntegrationsUpdateProps';
Expand All @@ -13,13 +12,6 @@ export type IntegrationsEndpoints = {
POST: (params: IntegrationsCreateProps) => { integration: IIntegration };
};

'/v1/integrations.history': {
GET: (params: IntegrationsHistoryProps) => PaginatedResult<{
history: IIntegrationHistory[];
items: number;
}>;
};

'/v1/integrations.list': {
GET: (params: IntegrationsListProps) => PaginatedResult<{
integrations: IIntegration[];
Expand Down
Loading