Skip to content

Commit c1add5c

Browse files
refactor: change WebDAV get API endpoints to new pattern v2
1 parent 15baefa commit c1add5c

File tree

1 file changed

+135
-30
lines changed

1 file changed

+135
-30
lines changed
Lines changed: 135 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,71 @@
11
import { api } from '@rocket.chat/core-services';
22
import { WebdavAccounts } from '@rocket.chat/models';
3-
import Ajv from 'ajv';
3+
import { ajv } from '@rocket.chat/rest-typings/src/v1/Ajv';
44

5+
import type { ExtractRoutesFromAPI } from '../ApiClass';
56
import { API } from '../api';
67
import { findWebdavAccountsByUserId } from '../lib/webdav';
78

8-
// TO-DO: remove this AJV instance and import one from the core-typings
9-
const ajv = new Ajv({ coerceTypes: true });
9+
const webdavGetMyAccountsEndpoints = API.v1.get(
10+
'webdav.getMyAccounts',
11+
{
12+
authRequired: true,
13+
response: {
14+
200: ajv.compile({
15+
type: 'object',
16+
properties: {
17+
accounts: {
18+
type: 'array',
19+
items: {
20+
type: 'object',
21+
properties: {
22+
_id: {
23+
type: 'string',
24+
},
25+
serverURL: {
26+
type: 'string',
27+
},
28+
username: {
29+
type: 'string',
30+
},
31+
name: {
32+
type: 'string',
33+
},
34+
},
35+
required: ['_id', 'serverURL', 'username', 'name'],
36+
additionalProperties: false,
37+
},
38+
},
39+
success: {
40+
type: 'boolean',
41+
description: 'Indicates if the request was successful.',
42+
},
43+
},
44+
required: ['success', 'accounts'],
45+
additionalProperties: false,
46+
}),
47+
401: ajv.compile({
48+
type: 'object',
49+
properties: {
50+
message: {
51+
type: 'string',
52+
},
53+
success: {
54+
type: 'boolean',
55+
description: 'Indicates if the request was successful.',
56+
},
57+
},
58+
required: ['success', 'message'],
59+
additionalProperties: false,
60+
}),
61+
},
62+
},
63+
async function action() {
64+
return API.v1.success({
65+
accounts: await findWebdavAccountsByUserId({ uid: this.userId }),
66+
});
67+
},
68+
);
1069

1170
type POSTRemoveWebdavAccount = {
1271
accountId: string;
@@ -25,37 +84,83 @@ const POSTRemoveWebdavAccountSchema = {
2584

2685
const isPOSTRemoveWebdavAccount = ajv.compile<POSTRemoveWebdavAccount>(POSTRemoveWebdavAccountSchema);
2786

28-
API.v1.addRoute(
29-
'webdav.getMyAccounts',
30-
{ authRequired: true },
31-
{
32-
async get() {
33-
return API.v1.success({
34-
accounts: await findWebdavAccountsByUserId({ uid: this.userId }),
35-
});
36-
},
37-
},
38-
);
39-
40-
API.v1.addRoute(
87+
API.v1.post(
4188
'webdav.removeWebdavAccount',
4289
{
4390
authRequired: true,
4491
validateParams: isPOSTRemoveWebdavAccount,
45-
},
46-
{
47-
async post() {
48-
const { accountId } = this.bodyParams;
49-
50-
const removed = await WebdavAccounts.removeByUserAndId(accountId, this.userId);
51-
if (removed) {
52-
void api.broadcast('notify.webdav', this.userId, {
53-
type: 'removed',
54-
account: { _id: accountId },
55-
});
56-
}
57-
58-
return API.v1.success({ result: removed });
92+
body: isPOSTRemoveWebdavAccount,
93+
response: {
94+
200: ajv.compile({
95+
type: 'object',
96+
properties: {
97+
result: {
98+
type: 'object',
99+
properties: {
100+
acknowledged: {
101+
type: 'boolean',
102+
},
103+
deletedCount: {
104+
type: 'integer',
105+
},
106+
},
107+
required: ['acknowledged', 'deletedCount'],
108+
additionalProperties: false,
109+
},
110+
success: {
111+
type: 'boolean',
112+
description: 'Indicates if the request was successful.',
113+
},
114+
},
115+
required: ['result', 'success'],
116+
additionalProperties: false,
117+
}),
118+
400: ajv.compile({
119+
type: 'object',
120+
properties: {
121+
errorType: {
122+
type: 'string',
123+
},
124+
error: {
125+
type: 'string',
126+
},
127+
success: {
128+
type: 'boolean',
129+
description: 'Indicates if the request was successful.',
130+
},
131+
},
132+
required: ['success', 'errorType', 'error'],
133+
additionalProperties: false,
134+
}),
135+
401: ajv.compile({
136+
type: 'object',
137+
properties: {
138+
message: {
139+
type: 'string',
140+
},
141+
success: {
142+
type: 'boolean',
143+
description: 'Indicates if the request was successful.',
144+
},
145+
},
146+
required: ['success', 'message'],
147+
additionalProperties: false,
148+
}),
59149
},
60150
},
151+
async function action() {
152+
const { accountId } = this.bodyParams;
153+
154+
const removed = await WebdavAccounts.removeByUserAndId(accountId, this.userId);
155+
if (removed) {
156+
void api.broadcast('notify.webdav', this.userId, {
157+
type: 'removed',
158+
account: { _id: accountId },
159+
});
160+
}
161+
162+
return API.v1.success({ result: removed });
163+
},
61164
);
165+
166+
export type WebdavEndpoints = ExtractRoutesFromAPI<typeof webdavGetMyAccountsEndpoints>;

0 commit comments

Comments
 (0)