Skip to content

Commit d673a79

Browse files
refactor: extract account ID validation into a separate method for cleaner code and improved readability
1 parent 3ebee7e commit d673a79

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/lib/MailtrapClient.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ export default class MailtrapClient {
8383
this.sandbox = sandbox;
8484
}
8585

86+
/**
87+
* Validates that account ID is present, throws MailtrapError if missing.
88+
*/
89+
private validateAccountIdPresence(): void {
90+
if (!this.accountId) {
91+
throw new MailtrapError(ACCOUNT_ID_MISSING);
92+
}
93+
}
94+
8695
/**
8796
* Getter for Testing API. Warns if some of the required keys are missing.
8897
*/
@@ -91,9 +100,7 @@ export default class MailtrapClient {
91100
throw new MailtrapError(TEST_INBOX_ID_MISSING);
92101
}
93102

94-
if (!this.accountId) {
95-
throw new MailtrapError(ACCOUNT_ID_MISSING);
96-
}
103+
this.validateAccountIdPresence();
97104

98105
return new TestingAPI(this.axios, this.accountId);
99106
}
@@ -102,9 +109,7 @@ export default class MailtrapClient {
102109
* Getter for General API.
103110
*/
104111
get general() {
105-
if (!this.accountId) {
106-
throw new MailtrapError(ACCOUNT_ID_MISSING);
107-
}
112+
this.validateAccountIdPresence();
108113

109114
return new GeneralAPI(this.axios, this.accountId);
110115
}
@@ -113,9 +118,7 @@ export default class MailtrapClient {
113118
* Getter for Contacts API.
114119
*/
115120
get contacts() {
116-
if (!this.accountId) {
117-
throw new MailtrapError(ACCOUNT_ID_MISSING);
118-
}
121+
this.validateAccountIdPresence();
119122

120123
return new ContactsBaseAPI(this.axios, this.accountId);
121124
}
@@ -124,17 +127,13 @@ export default class MailtrapClient {
124127
* Getter for Contact Lists API.
125128
*/
126129
get contactLists() {
127-
if (!this.accountId) {
128-
throw new MailtrapError(ACCOUNT_ID_MISSING);
129-
}
130+
this.validateAccountIdPresence();
130131

131132
return new ContactListsBaseAPI(this.axios, this.accountId);
132133
}
133134

134135
get templates() {
135-
if (!this.accountId) {
136-
throw new MailtrapError(ACCOUNT_ID_MISSING);
137-
}
136+
this.validateAccountIdPresence();
138137

139138
return new TemplatesBaseAPI(this.axios, this.accountId);
140139
}

0 commit comments

Comments
 (0)