Skip to content

Commit adc5b67

Browse files
chore: Add OpenAPI support for the Rocket.Chat users.getAvatarSuggestion 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.
1 parent 55bf071 commit adc5b67

File tree

3 files changed

+125
-84
lines changed

3 files changed

+125
-84
lines changed

.changeset/rare-waves-help.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@rocket.chat/meteor": minor
3+
"@rocket.chat/rest-typings": minor
4+
---
5+
6+
Add OpenAPI support for the Rocket.Chat users.getAvatarSuggestion 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.

apps/meteor/app/api/server/v1/users.ts

Lines changed: 119 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import {
1919
isUsersCheckUsernameAvailabilityParamsGET,
2020
isUsersSendConfirmationEmailParamsPOST,
2121
ajv,
22+
validateBadRequestErrorResponse,
23+
validateUnauthorizedErrorResponse,
2224
} from '@rocket.chat/rest-typings';
25+
import { ajv } from '@rocket.chat/rest-typings/src/v1/Ajv';
2326
import { getLoginExpirationInMs, wrapExceptions } from '@rocket.chat/tools';
2427
import { Accounts } from 'meteor/accounts-base';
2528
import { Match, check } from 'meteor/check';
@@ -97,20 +100,6 @@ API.v1.addRoute(
97100
},
98101
);
99102

100-
API.v1.addRoute(
101-
'users.getAvatarSuggestion',
102-
{
103-
authRequired: true,
104-
},
105-
{
106-
async get() {
107-
const suggestions = await getAvatarSuggestionForUser(this.user);
108-
109-
return API.v1.success({ suggestions });
110-
},
111-
},
112-
);
113-
114103
API.v1.addRoute(
115104
'users.update',
116105
{ authRequired: true, twoFactorRequired: true, validateParams: isUsersUpdateParamsPOST },
@@ -764,72 +753,132 @@ API.v1.addRoute(
764753
},
765754
);
766755

767-
const usersEndpoints = API.v1.post(
768-
'users.createToken',
769-
{
770-
authRequired: true,
771-
body: ajv.compile<{ userId: string; secret: string }>({
772-
type: 'object',
773-
properties: {
774-
userId: {
775-
type: 'string',
776-
minLength: 1,
777-
},
778-
secret: {
779-
type: 'string',
780-
minLength: 1,
781-
},
782-
},
783-
required: ['userId', 'secret'],
784-
additionalProperties: false,
785-
}),
786-
response: {
787-
200: ajv.compile<{ data: { userId: string; authToken: string } }>({
756+
const usersEndpoints = API.v1
757+
.post(
758+
'users.createToken',
759+
{
760+
authRequired: true,
761+
body: ajv.compile<{ userId: string; secret: string }>({
788762
type: 'object',
789763
properties: {
790-
data: {
791-
type: 'object',
792-
properties: {
793-
userId: {
794-
type: 'string',
795-
minLength: 1,
796-
},
797-
authToken: {
798-
type: 'string',
799-
minLength: 1,
800-
},
801-
},
802-
required: ['userId'],
803-
additionalProperties: false,
764+
userId: {
765+
type: 'string',
766+
minLength: 1,
804767
},
805-
success: {
806-
type: 'boolean',
807-
enum: [true],
768+
secret: {
769+
type: 'string',
770+
minLength: 1,
808771
},
809772
},
810-
required: ['data', 'success'],
811-
additionalProperties: false,
812-
}),
813-
400: ajv.compile({
814-
type: 'object',
815-
properties: {
816-
success: { type: 'boolean', enum: [false] },
817-
error: { type: 'string' },
818-
errorType: { type: 'string' },
819-
},
820-
required: ['success'],
773+
required: ['userId', 'secret'],
821774
additionalProperties: false,
822775
}),
776+
response: {
777+
200: ajv.compile<{ data: { userId: string; authToken: string } }>({
778+
type: 'object',
779+
properties: {
780+
data: {
781+
type: 'object',
782+
properties: {
783+
userId: {
784+
type: 'string',
785+
minLength: 1,
786+
},
787+
authToken: {
788+
type: 'string',
789+
minLength: 1,
790+
},
791+
},
792+
required: ['userId'],
793+
additionalProperties: false,
794+
},
795+
success: {
796+
type: 'boolean',
797+
enum: [true],
798+
},
799+
},
800+
required: ['data', 'success'],
801+
additionalProperties: false,
802+
}),
803+
400: ajv.compile({
804+
type: 'object',
805+
properties: {
806+
success: { type: 'boolean', enum: [false] },
807+
error: { type: 'string' },
808+
errorType: { type: 'string' },
809+
},
810+
required: ['success'],
811+
additionalProperties: false,
812+
}),
813+
},
823814
},
824-
},
825-
async function action() {
826-
const user = await getUserFromParams(this.bodyParams);
815+
async function action() {
816+
const user = await getUserFromParams(this.bodyParams);
827817

828-
const data = await generateAccessToken(user._id, this.bodyParams.secret);
818+
const data = await generateAccessToken(user._id, this.bodyParams.secret);
829819

830-
return API.v1.success({ data });
831-
},
832-
);
820+
return API.v1.success({ data });
821+
},
822+
)
823+
.get(
824+
'users.getAvatarSuggestion',
825+
{
826+
authRequired: true,
827+
response: {
828+
400: validateBadRequestErrorResponse,
829+
401: validateUnauthorizedErrorResponse,
830+
200: ajv.compile<{
831+
suggestions: Record<
832+
string,
833+
{
834+
blob: string;
835+
contentType: string;
836+
service: string;
837+
url: string;
838+
}
839+
>;
840+
}>({
841+
type: 'object',
842+
properties: {
843+
success: {
844+
type: 'boolean',
845+
enum: [true],
846+
},
847+
suggestions: {
848+
type: 'object',
849+
additionalProperties: {
850+
type: 'object',
851+
properties: {
852+
blob: {
853+
type: 'string',
854+
},
855+
contentType: {
856+
type: 'string',
857+
},
858+
service: {
859+
type: 'string',
860+
},
861+
url: {
862+
type: 'string',
863+
format: 'uri',
864+
},
865+
},
866+
required: ['blob', 'contentType', 'service', 'url'],
867+
additionalProperties: false,
868+
},
869+
},
870+
},
871+
required: ['success', 'suggestions'],
872+
additionalProperties: false,
873+
}),
874+
},
875+
},
876+
async function action() {
877+
const suggestions = await getAvatarSuggestionForUser(this.user);
878+
879+
return API.v1.success({ suggestions });
880+
},
881+
);
833882

834883
API.v1.addRoute(
835884
'users.getPreferences',

packages/rest-typings/src/v1/users.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -255,20 +255,6 @@ export type UsersEndpoints = {
255255
};
256256
};
257257

258-
'/v1/users.getAvatarSuggestion': {
259-
GET: () => {
260-
suggestions: Record<
261-
string,
262-
{
263-
blob: string;
264-
contentType: string;
265-
service: string;
266-
url: string;
267-
}
268-
>;
269-
};
270-
};
271-
272258
'/v1/users.checkUsernameAvailability': {
273259
GET: (params: { username: string }) => {
274260
result: boolean;

0 commit comments

Comments
 (0)