Skip to content

Commit bf507f3

Browse files
Refactor IMAP service functions to use parameter objects for better readability and maintainability
Signed-off-by: Shahm Najeeb <Shahm_Najeeb@outlook.com>
1 parent ddcf446 commit bf507f3

6 files changed

Lines changed: 52 additions & 18 deletions

File tree

app/api/accounts/[accountId]/emails/[uid]/attachments/[partId]/route.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ export async function GET(
6464
}
6565

6666
// Get attachment
67-
const attachment = await getAttachment(accountId, folder, uidNum, partId)
67+
const attachment = await getAttachment({
68+
accountId,
69+
folderPath: folder,
70+
uid: uidNum,
71+
partId
72+
})
6873

6974
if (!attachment) {
7075
return NextResponse.json(

app/api/accounts/[accountId]/emails/[uid]/route.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ export async function GET(request: NextRequest, {params}: { params: Promise<{ ac
8686
}
8787

8888
// Get email
89-
const email = await getEmail(accountId, folder, uidNum)
89+
const email = await getEmail({
90+
accountId,
91+
folderPath: folder,
92+
uid: uidNum
93+
})
9094

9195
if (!email) {
9296
return NextResponse.json(

app/api/accounts/[accountId]/emails/route.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ export async function GET(request: NextRequest, {params}: { params: Promise<{ ac
7575
}
7676

7777
// Get emails
78-
const result = await listEmails(accountId, folder, {limit, offset})
78+
const result = await listEmails({
79+
accountId,
80+
folderPath: folder,
81+
options: {limit, offset}
82+
})
7983

8084
return NextResponse.json(
8185
result,

lib/imap-service.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Imap from "imap"
1414
import {simpleParser} from "mailparser"
1515
import {getAccountById} from "./imap-config"
1616
import DOMPurify from "isomorphic-dompurify"
17-
import {EmailCount, EmailDetail, EmailFolder} from "@/types";
17+
import {EmailCount, EmailDetail, EmailFolder, GetAttachmentParams, GetEmailParams, ListEmailsParams} from "@/types";
1818
import {FolderSettings} from "./settings"
1919

2020
/**
@@ -189,11 +189,8 @@ export async function listFolders(accountId: string): Promise<EmailFolder[]> {
189189
* List emails in a folder with pagination
190190
* Returns emails in reverse chronological order (newest first)
191191
*/
192-
export async function listEmails(
193-
accountId: string,
194-
folderPath: string,
195-
options: { limit?: number; offset?: number } = {},
196-
): Promise<EmailCount> {
192+
export async function listEmails(params: ListEmailsParams): Promise<EmailCount> {
193+
const {accountId, folderPath, options = {}} = params
197194
let imap: Imap | null = null
198195

199196
try {
@@ -307,7 +304,8 @@ export async function listEmails(
307304
/**
308305
* Get full email details including body
309306
*/
310-
export async function getEmail(accountId: string, folderPath: string, uid: number): Promise<EmailDetail | null> {
307+
export async function getEmail(params: GetEmailParams): Promise<EmailDetail | null> {
308+
const {accountId, folderPath, uid} = params
311309
let imap: Imap | null = null
312310

313311
try {
@@ -436,12 +434,11 @@ export async function getEmail(accountId: string, folderPath: string, uid: numbe
436434
* Get email attachment
437435
* Returns the decoded attachment content from the parsed email
438436
*/
439-
export async function getAttachment(
440-
accountId: string,
441-
folderPath: string,
442-
uid: number,
443-
partId: string
444-
): Promise<{ content: Buffer; contentType: string } | null> {
437+
export async function getAttachment(params: GetAttachmentParams): Promise<{
438+
content: Buffer;
439+
contentType: string
440+
} | null> {
441+
const {accountId, folderPath, uid, partId} = params
445442
let imap: Imap | null = null
446443

447444
try {

todo

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
imap-service should have all input types for functions be a proper indexed @/type
21
Many settings.ts are unused, please make them properly used
32
Change the bcrypt to be base64(bcrypt), where the env file contains a base64 encoded string, and the runtime decodes the base64 to get the bcrypt hash
43
Change setup-password to setup-env where it also runs 'openssl rand -base64 32' and also asks if you want to update/change imap config, which if yes allows a notepad to open with a temp.json file, which the user can edit, once it is complete it is then closed by user where the program transforms it into 1 line and puts in env var (the temp json must be the already existing env variable but formatted)

types/index.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,29 @@ export interface CacheItem<T> {
103103
data: T
104104
timestamp: number
105105
ttl: number
106-
}
106+
}
107+
108+
// IMAP Service Function Parameter Types
109+
export interface ListEmailsOptions {
110+
limit?: number
111+
offset?: number
112+
}
113+
114+
export interface GetEmailParams {
115+
accountId: string
116+
folderPath: string
117+
uid: number
118+
}
119+
120+
export interface GetAttachmentParams {
121+
accountId: string
122+
folderPath: string
123+
uid: number
124+
partId: string
125+
}
126+
127+
export interface ListEmailsParams {
128+
accountId: string
129+
folderPath: string
130+
options?: ListEmailsOptions
131+
}

0 commit comments

Comments
 (0)