Skip to content

Commit 7ff50b3

Browse files
committed
fix swagger
1 parent cc7f3c6 commit 7ff50b3

10 files changed

+254
-247
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"@nestjs-modules/mailer": "^1.9.1",
4848
"@nestjs/axios": "^3.0.0",
4949
"@nestjs/common": "^10.1.3",
50+
"@nestjs/swagger": "^7.1.8",
5051
"@nestjs/config": "^3.0.0",
5152
"@nestjs/core": "^10.1.3",
5253
"@nestjs/jwt": "^10.1.0",
@@ -78,7 +79,6 @@
7879
"@compodoc/compodoc": "^1.1.21",
7980
"@nestjs/cli": "^10.1.12",
8081
"@nestjs/schematics": "10.0.2",
81-
"@nestjs/swagger": "^7.1.8",
8282
"@nestjs/testing": "^10.1.3",
8383
"@swc/cli": "^0.1.62",
8484
"@swc/core": "^1.3.93",

src/accounts/_dto/account.dto.ts

+239
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
import { IsBoolean, IsEnum, IsInt, IsObject, IsOptional, IsString, Max, Min, Validate, ValidateNested } from 'class-validator'
2+
import { UniqueFieldValidator } from '~/_common/validators/unique.field.validator'
3+
import { Type } from 'class-transformer'
4+
import { ApiProperty } from '@nestjs/swagger'
5+
import { ImapFlowOptions } from 'imapflow'
6+
7+
export class AccountsFileV1 {
8+
@IsEnum(['1'])
9+
public version: string
10+
11+
@ValidateNested({ each: true })
12+
@Validate(UniqueFieldValidator, ['id'])
13+
@Type(() => AccountsMetadataV1)
14+
public accounts: AccountsMetadataV1[]
15+
}
16+
17+
export class AccountsMetadataImapAuthV1 {
18+
@IsString()
19+
@ApiProperty()
20+
public user: string
21+
22+
@IsString()
23+
@IsOptional()
24+
@ApiProperty()
25+
public pass?: string
26+
27+
@IsString()
28+
@IsOptional()
29+
@ApiProperty()
30+
public accessToken?: string
31+
32+
// noinspection JSUnusedGlobalSymbols
33+
public toJSON() {
34+
// Fix to hide auth params in JSON.stringify calls
35+
// NestJS fill arguments with array of data, so we can check it
36+
// eslint-disable-next-line prefer-rest-params
37+
if (arguments.length > 0) {
38+
const data: any = { user: '******' }
39+
if (this.pass) data.pass = '******'
40+
if (this.accessToken) data.accessToken = '******'
41+
return data
42+
}
43+
return this
44+
}
45+
}
46+
47+
export class AccountsMetadataImapV1 implements ImapFlowOptions {
48+
@IsString()
49+
@ApiProperty()
50+
public host: string
51+
52+
@IsInt()
53+
@Min(25)
54+
@Max(65535)
55+
@IsOptional()
56+
@ApiProperty()
57+
public port: number
58+
59+
@ValidateNested()
60+
@Type(() => AccountsMetadataImapAuthV1)
61+
@ApiProperty()
62+
public auth: AccountsMetadataImapAuthV1
63+
64+
@IsBoolean()
65+
@IsOptional()
66+
@ApiProperty()
67+
public secure?: boolean
68+
69+
@IsString()
70+
@IsOptional()
71+
@ApiProperty()
72+
public servername?: string
73+
74+
@IsBoolean()
75+
@IsOptional()
76+
@ApiProperty()
77+
public disableCompression?: boolean
78+
79+
@IsObject()
80+
@IsOptional()
81+
@ApiProperty()
82+
// eslint-disable-next-line @typescript-eslint/ban-types
83+
public tls?: object
84+
85+
@IsInt()
86+
@Min(5_000)
87+
@Max(60_000)
88+
@IsOptional()
89+
@ApiProperty()
90+
public maxIdleTime: number = 60_000
91+
}
92+
93+
export class AccountsMetadataSmtpAuthV1 {
94+
@IsString()
95+
@ApiProperty()
96+
public user: string
97+
98+
@IsString()
99+
@ApiProperty()
100+
public pass: string
101+
}
102+
103+
export class AccountsMetadataSmtpV1 {
104+
@IsString()
105+
@ApiProperty()
106+
public host: string
107+
108+
@IsInt()
109+
@Min(25, { message: 'Port must be between 25 and 65535' })
110+
@Max(65535)
111+
@IsOptional()
112+
@ApiProperty()
113+
public port?: number = 25
114+
115+
@IsString()
116+
@IsOptional()
117+
@ApiProperty()
118+
public from?: string
119+
120+
@IsBoolean()
121+
@IsOptional()
122+
@ApiProperty()
123+
public ignoreTLS: boolean = false
124+
125+
@IsBoolean()
126+
@IsOptional()
127+
@ApiProperty()
128+
public secure: boolean = true
129+
130+
@ValidateNested()
131+
@IsOptional()
132+
@Type(() => AccountsMetadataSmtpAuthV1)
133+
@ApiProperty()
134+
public auth?: AccountsMetadataSmtpAuthV1
135+
136+
// noinspection JSUnusedGlobalSymbols
137+
public toJSON() {
138+
// Fix to hide auth params in JSON.stringify calls
139+
// NestJS fill arguments with array of data, so we can check it
140+
// eslint-disable-next-line prefer-rest-params
141+
if (arguments.length > 0) {
142+
const data: any = {}
143+
return data
144+
}
145+
return this
146+
}
147+
}
148+
149+
export class AccountsMetadataWebhooksCronV1 {
150+
@IsBoolean()
151+
@ApiProperty()
152+
public enabled: boolean
153+
154+
@IsString()
155+
@ApiProperty()
156+
public pattern: string
157+
158+
@IsString()
159+
@IsOptional()
160+
@ApiProperty()
161+
public seq: string
162+
}
163+
164+
export enum AccountsMetadataWebhookAlg {
165+
SHA256 = 'sha256',
166+
}
167+
168+
export class AccountsMetadataWebhooksV1 {
169+
@IsString()
170+
@ApiProperty()
171+
public id: string
172+
173+
@IsBoolean()
174+
@ApiProperty()
175+
public enabled: boolean
176+
177+
@IsString()
178+
@ApiProperty()
179+
public url: string
180+
181+
@IsString()
182+
@ApiProperty()
183+
public secret: string
184+
185+
@IsOptional()
186+
@ValidateNested()
187+
@Type(() => AccountsMetadataWebhooksCronV1)
188+
@ApiProperty()
189+
public cron?: AccountsMetadataWebhooksCronV1
190+
191+
@IsOptional()
192+
@IsEnum(AccountsMetadataWebhookAlg)
193+
@ApiProperty()
194+
public alg: string = AccountsMetadataWebhookAlg.SHA256
195+
196+
// noinspection JSUnusedGlobalSymbols
197+
public toJSON() {
198+
// Fix to hide auth params in JSON.stringify calls
199+
// NestJS fill arguments with array of data, so we can check it
200+
// eslint-disable-next-line prefer-rest-params
201+
if (arguments.length > 0) {
202+
const data: any = { ...this }
203+
if (data.secret) data.secret = '******'
204+
return data
205+
}
206+
return this
207+
}
208+
}
209+
210+
export class AccountsMetadataV1 {
211+
[key: string]: unknown
212+
213+
@IsString()
214+
@ApiProperty()
215+
public id: string
216+
217+
@IsString()
218+
@ApiProperty()
219+
public name: string
220+
221+
@IsOptional()
222+
@ValidateNested()
223+
@Type(() => AccountsMetadataImapV1)
224+
@ApiProperty()
225+
public imap?: AccountsMetadataImapV1
226+
227+
@IsOptional()
228+
@ValidateNested()
229+
@Type(() => AccountsMetadataSmtpV1)
230+
@ApiProperty()
231+
public smtp?: AccountsMetadataSmtpV1
232+
233+
@IsOptional()
234+
@ValidateNested({ each: true })
235+
@Type(() => AccountsMetadataWebhooksV1)
236+
@Validate(UniqueFieldValidator, ['id'])
237+
@ApiProperty({ type: [AccountsMetadataWebhooksV1] })
238+
public webhooks?: AccountsMetadataWebhooksV1[]
239+
}

src/accounts/accounts.controller.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { AbstractController } from '~/_common/abstracts/abstract.controller'
2020
import { FSWatcher, watch } from 'fs'
2121
import { Observable } from 'rxjs'
2222
import { AccountsService } from './accounts.service'
23-
import { ACCOUNTS_FILE_PATH, AccountsMetadataV1, readAccountsFile } from './accounts.setup'
23+
import { ACCOUNTS_FILE_PATH, readAccountsFile } from './accounts.setup'
2424
import { UseRoles } from 'nest-access-control'
2525
import { ScopesEnum } from '~/_common/enums/scopes.enum'
2626
import { ActionEnum } from '~/_common/enums/action.enum'
@@ -33,6 +33,7 @@ import { ApiTags } from '@nestjs/swagger'
3333
import { FilesInterceptor } from '@nestjs/platform-express'
3434
import { AccountSubmitDto } from '~/accounts/_dto/account-submit.dto'
3535
import { AccountSubmitedDto } from '~/accounts/_dto/account-submited.dto'
36+
import { AccountsMetadataV1 } from '~/accounts/_dto/account.dto'
3637

3738
@ApiTags('accounts')
3839
@Controller('accounts')

src/accounts/accounts.service.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ import { ImapFlow } from 'imapflow'
1212
import { LRUCache } from 'lru-cache'
1313
import { AbstractService } from '~/_common/abstracts/abstract.service'
1414
import { InjectImapflow } from '~/imapflow/imapflow.decorators'
15-
import { AccountsFileV1, AccountsMetadataV1, readAccountsFile, writeAccountsFile } from './accounts.setup'
15+
import { readAccountsFile, writeAccountsFile } from './accounts.setup'
1616
import { PartialType } from '@nestjs/swagger'
1717
import { MailerService } from '@nestjs-modules/mailer'
1818
import { AccountSubmitDto } from '~/accounts/_dto/account-submit.dto'
1919
import { AccountSubmitedDto } from '~/accounts/_dto/account-submited.dto'
2020
import os from 'os'
2121
import gateway from 'default-gateway'
22+
import { AccountsFileV1, AccountsMetadataV1 } from '~/accounts/_dto/account.dto'
2223

2324
class InternalAccountMetadataV1 extends PartialType(AccountsMetadataV1) {}
2425

0 commit comments

Comments
 (0)