| 45 | } |
| 46 | |
| 47 | export class MailPit implements EmailClient{ |
| 48 | private readonly apiUrl: string; |
| 49 | |
| 50 | constructor(baseUrl: string = 'http://localhost:8026') { |
| 51 | this.apiUrl = `${baseUrl}/api/v1`; |
| 52 | } |
| 53 | |
| 54 | async getMessages(limit: number = 50): Promise<EmailMessage[]> { |
| 55 | const response = await this.executeApiCall( |
| 56 | `messages?limit=${limit}`, |
| 57 | 'fetch messages' |
| 58 | ); |
| 59 | |
| 60 | return this.parseMessagesFromResponse(response); |
| 61 | } |
| 62 | |
| 63 | async getMessageDetailed(message: EmailMessage): Promise<EmailMessageDetailed> { |
| 64 | const response = await this.executeApiCall( |
| 65 | `message/${message.ID}`, |
| 66 | 'fetch message' |
| 67 | ); |
| 68 | |
| 69 | return await response.json() as EmailMessageDetailed; |
| 70 | } |
| 71 | |
| 72 | async searchByRecipient(recipient: string, options?: EmailSearchOptions): Promise<EmailMessage[]> { |
| 73 | return this.search({to: recipient}, options); |
| 74 | } |
| 75 | |
| 76 | async searchByContent(content: string, options?: EmailSearchOptions): Promise<EmailMessage[]> { |
| 77 | return this.search({subject: content}, options); |
| 78 | } |
| 79 | |
| 80 | async search(queryOptions:Partial<EmailSearchQuery>, options?: EmailSearchOptions): Promise<EmailMessage[]> { |
| 81 | const defaultOptions = {limit: 50, timeoutMs: 10000, numberOfMessages: null}; |
| 82 | const searchOptions = {...defaultOptions, ...options}; |
| 83 | |
| 84 | if (searchOptions.timeoutMs !== null) { |
| 85 | return await this.searchWithWait( |
| 86 | () => this.searchByQuery(queryOptions, searchOptions.limit), |
| 87 | searchOptions.timeoutMs, |
| 88 | searchOptions.numberOfMessages |
| 89 | ); |
| 90 | } |
| 91 | return await this.searchByQuery(queryOptions, searchOptions.limit); |
| 92 | } |
| 93 | |
| 94 | async searchByQuery(options: Partial<EmailSearchQuery>, limit: number = 50): Promise<EmailMessage[]> { |
| 95 | const queryString = Object.entries(options) |
| 96 | .filter((entry): entry is [string, string] => { |
| 97 | const [, value] = entry; |
| 98 | return value !== null && value !== undefined && value !== ''; |
| 99 | }) |
| 100 | .map(([key, value]) => `${encodeURIComponent(key)}:${encodeURIComponent(value)}`) |
| 101 | .join('+'); |
| 102 | |
| 103 | const response = await this.executeApiCall( |
| 104 | `search?query=${queryString}&limit=${limit}`, |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…