Skip to content
Merged
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/pink-games-shake.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 oauth-apps.delete 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.
254 changes: 134 additions & 120 deletions apps/meteor/app/api/server/v1/oauthapps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
ajv,
isUpdateOAuthAppParams,
isOauthAppsGetParams,
isDeleteOAuthAppParams,
validateUnauthorizedErrorResponse,
validateBadRequestErrorResponse,
validateForbiddenErrorResponse,
Expand All @@ -18,50 +17,146 @@ import { updateOAuthApp } from '../../../oauth2-server-config/server/admin/metho
import type { ExtractRoutesFromAPI } from '../ApiClass';
import { API } from '../api';

const oauthAppsListEndpoints = API.v1.get(
'oauth-apps.list',
{
authRequired: true,
query: ajv.compile<{ uid?: string }>({
type: 'object',
properties: {
uid: {
type: 'string',
},
},
additionalProperties: false,
}),
permissionsRequired: ['manage-oauth-apps'],
response: {
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
403: validateForbiddenErrorResponse,
200: ajv.compile<{ oauthApps: IOAuthApps[] }>({
type DeleteOAuthAppParams = {
appId: string;
};

const DeleteOAuthAppParamsSchema = {
type: 'object',
properties: {
appId: {
type: 'string',
},
},
required: ['appId'],
additionalProperties: false,
};

const isDeleteOAuthAppParams = ajv.compile<DeleteOAuthAppParams>(DeleteOAuthAppParamsSchema);

export type OauthAppsAddParams = {
name: string;
active: boolean;
redirectUri: string;
};

const OauthAppsAddParamsSchema = {
type: 'object',
properties: {
name: {
type: 'string',
},
active: {
type: 'boolean',
},
redirectUri: {
type: 'string',
},
},
required: ['name', 'active', 'redirectUri'],
additionalProperties: false,
};

const isOauthAppsAddParams = ajv.compile<OauthAppsAddParams>(OauthAppsAddParamsSchema);

const oauthAppsEndpoints = API.v1
.get(
'oauth-apps.list',
{
authRequired: true,
query: ajv.compile<{ uid?: string }>({
type: 'object',
properties: {
oauthApps: {
type: 'array',
items: {
$ref: '#/components/schemas/IOAuthApps',
},
},
success: {
type: 'boolean',
enum: [true],
uid: {
type: 'string',
},
},
required: ['oauthApps', 'success'],
additionalProperties: false,
}),
permissionsRequired: ['manage-oauth-apps'],
response: {
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
403: validateForbiddenErrorResponse,
200: ajv.compile<{ oauthApps: IOAuthApps[] }>({
type: 'object',
properties: {
oauthApps: {
type: 'array',
items: {
$ref: '#/components/schemas/IOAuthApps',
},
},
success: {
type: 'boolean',
enum: [true],
},
},
required: ['oauthApps', 'success'],
additionalProperties: false,
}),
},
},
},

async function action() {
return API.v1.success({
oauthApps: await OAuthApps.find().toArray(),
});
},
);
async function action() {
return API.v1.success({
oauthApps: await OAuthApps.find().toArray(),
});
},
)
.post(
'oauth-apps.delete',
{
authRequired: true,
body: isDeleteOAuthAppParams,
permissionsRequired: ['manage-oauth-apps'],
response: {
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
403: validateForbiddenErrorResponse,
200: ajv.compile<boolean>({ type: 'boolean' }),
},
},

async function action() {
const { appId } = this.bodyParams;

const result = await deleteOAuthApp(this.userId, appId);

return API.v1.success(result);
},
)
.post(
'oauth-apps.create',
{
authRequired: true,
body: isOauthAppsAddParams,
permissionsRequired: ['manage-oauth-apps'],
response: {
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
403: validateForbiddenErrorResponse,
200: ajv.compile<{ application: IOAuthApps }>({
type: 'object',
properties: {
application: { $ref: '#/components/schemas/IOAuthApps' },
success: {
type: 'boolean',
enum: [true],
},
},
required: ['application', 'success'],
additionalProperties: false,
}),
},
},

async function action() {
const application = await addOAuthApp(this.bodyParams, this.userId);

return API.v1.success({ application });
},
);

API.v1.addRoute(
'oauth-apps.get',
Expand Down Expand Up @@ -108,90 +203,9 @@ API.v1.addRoute(
},
);

API.v1.addRoute(
'oauth-apps.delete',
{
authRequired: true,
validateParams: isDeleteOAuthAppParams,
permissionsRequired: ['manage-oauth-apps'],
},
{
async post() {
const { appId } = this.bodyParams;

const result = await deleteOAuthApp(this.userId, appId);

return API.v1.success(result);
},
},
);

export type OauthAppsAddParams = {
name: string;
active: boolean;
redirectUri: string;
};

const OauthAppsAddParamsSchema = {
type: 'object',
properties: {
name: {
type: 'string',
},
active: {
type: 'boolean',
},
redirectUri: {
type: 'string',
},
},
required: ['name', 'active', 'redirectUri'],
additionalProperties: false,
};

const isOauthAppsAddParams = ajv.compile<OauthAppsAddParams>(OauthAppsAddParamsSchema);

const oauthAppsCreateEndpoints = API.v1.post(
'oauth-apps.create',
{
authRequired: true,
body: isOauthAppsAddParams,
permissionsRequired: ['manage-oauth-apps'],
response: {
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
403: validateForbiddenErrorResponse,
200: ajv.compile<{ application: IOAuthApps }>({
type: 'object',
properties: {
application: { $ref: '#/components/schemas/IOAuthApps' },
success: {
type: 'boolean',
enum: [true],
},
},
required: ['application', 'success'],
additionalProperties: false,
}),
},
},

async function action() {
const application = await addOAuthApp(this.bodyParams, this.userId);

return API.v1.success({ application });
},
);

type OauthAppsCreateEndpoints = ExtractRoutesFromAPI<typeof oauthAppsCreateEndpoints>;

type OauthAppsListEndpoints = ExtractRoutesFromAPI<typeof oauthAppsListEndpoints>;

export type OAuthAppsEndpoints = OauthAppsCreateEndpoints | OauthAppsListEndpoints;
export type OauthAppsEndpoints = ExtractRoutesFromAPI<typeof oauthAppsEndpoints>;

declare module '@rocket.chat/rest-typings' {
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
interface Endpoints extends OauthAppsCreateEndpoints {}
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
interface Endpoints extends OauthAppsListEndpoints {}
interface Endpoints extends OauthAppsEndpoints {}
}
1 change: 0 additions & 1 deletion packages/rest-typings/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ export * from './v1/omnichannel';
export * from './v1/oauthapps';
export * from './v1/oauthapps/UpdateOAuthAppParamsPOST';
export * from './v1/oauthapps/OAuthAppsGetParamsGET';
export * from './v1/oauthapps/DeleteOAuthAppParamsDELETE';
export * from './helpers/PaginatedRequest';
export * from './helpers/PaginatedResult';
export * from './helpers/ReplacePlaceholders';
Expand Down
5 changes: 0 additions & 5 deletions packages/rest-typings/src/v1/oauthapps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { IOAuthApps } from '@rocket.chat/core-typings';

import type { DeleteOAuthAppParams } from './oauthapps/DeleteOAuthAppParamsDELETE';
import type { OauthAppsGetParams } from './oauthapps/OAuthAppsGetParamsGET';
import type { UpdateOAuthAppParams } from './oauthapps/UpdateOAuthAppParamsPOST';

Expand All @@ -14,8 +13,4 @@ export type OAuthAppsEndpoint = {
'/v1/oauth-apps.update': {
POST: (params: UpdateOAuthAppParams) => IOAuthApps | null;
};

'/v1/oauth-apps.delete': {
POST: (params: DeleteOAuthAppParams) => boolean;
};
};

This file was deleted.

Loading