The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/apps/meteor/app/api/server/default/openApi.ts

96 lines
2.2 KiB

import { schemas } from '@rocket.chat/core-typings';
import type { Route } from '@rocket.chat/http-router';
import { isOpenAPIJSONEndpoint } from '@rocket.chat/rest-typings';
import express from 'express';
import { WebApp } from 'meteor/webapp';
import swaggerUi from 'swagger-ui-express';
import { settings } from '../../../settings/server';
import { API } from '../api';
import { getTrimmedServerVersion } from '../lib/getTrimmedServerVersion';
const app = express();
const getTypedRoutes = (
typedRoutes: Record<string, Record<string, Route>>,
{ withUndocumented = false }: { withUndocumented?: boolean } = {},
): Record<string, Record<string, Route>> => {
if (withUndocumented) {
return typedRoutes;
}
return Object.entries(typedRoutes).reduce(
(acc, [path, methods]) => {
const filteredMethods = Object.entries(methods)
.filter(([_, options]) => !options?.tags?.includes('Missing Documentation'))
.reduce(
(acc, [method, options]) => {
acc[method] = options;
return acc;
},
{} as Record<string, Route>,
);
if (Object.keys(filteredMethods).length > 0) {
acc[path] = filteredMethods;
}
return acc;
},
{} as Record<string, Record<string, Route>>,
);
};
const makeOpenAPIResponse = (paths: Record<string, Record<string, Route>>) => ({
openapi: '3.0.3',
info: {
title: 'Rocket.Chat API',
description: 'Rocket.Chat API',
version: getTrimmedServerVersion(),
},
servers: [
{
url: settings.get('Site_Url'),
},
],
components: {
securitySchemes: {
userId: {
type: 'apiKey',
in: 'header',
name: 'X-User-Id',
},
authToken: {
type: 'apiKey',
in: 'header',
name: 'X-Auth-Token',
},
},
schemas: schemas.components.schemas,
},
schemas: schemas.components.schemas,
paths,
});
API.default.addRoute(
'docs/json',
{ authRequired: false, validateParams: isOpenAPIJSONEndpoint },
{
get() {
const { withUndocumented = false } = this.queryParams;
return API.default.success(makeOpenAPIResponse(getTypedRoutes(API.api.typedRoutes, { withUndocumented })));
},
},
);
app.use(
'/api-docs',
swaggerUi.serve,
swaggerUi.setup(null, {
swaggerOptions: {
url: `${settings.get('Site_Url')}/api/docs/json`,
},
}),
);
WebApp.connectHandlers.use(app);