1- import axios , { AxiosError , AxiosResponse } from 'axios' ;
2- import { MailcowErrorResponse , MailcowException } from './types' ;
1+ import axios from 'axios' ;
2+ import { BaseResponse , MailcowException , MailcowResponse } from './types' ;
33import MailcowClient from './index' ;
44
55/**
@@ -24,6 +24,24 @@ export function wrapPromiseToArray<T>(promise: Promise<T | T[]>): Promise<T[]> {
2424 } ) ;
2525}
2626
27+ function isErrorType ( type : string | undefined ) : boolean {
28+ return type === 'danger' || type === 'error' ;
29+ }
30+
31+ /**
32+ * Checks if a Mailcow API response indicates an error.
33+ * Throws a MailcowException if a definite error is detected.
34+ */
35+ function checkMailcowResponse ( res : MailcowResponse | BaseResponse ) : void {
36+ // Accepts either a single object or an array
37+ const arr = Array . isArray ( res ) ? res : [ res ] ;
38+ for ( const item of arr ) {
39+ if ( isErrorType ( item . type ) ) {
40+ throw new MailcowException ( Array . isArray ( item . msg ) ? item . msg . join ( ', ' ) : item . msg ) ;
41+ }
42+ }
43+ }
44+
2745/**
2846 * Factory method patterns for creating Axios Requests.
2947 * @internal
@@ -41,38 +59,37 @@ export default class RequestFactory {
4159 * @param payload - The payload to send with the request.
4260 */
4361 async post < T , P extends object > ( route : string , payload : P ) : Promise < T > {
44- return new Promise ( ( resolve , reject ) => {
45- axios
46- . post ( this . ctx . BASE_URL + route , payload , this . ctx . AXIOS_CONFIG )
47- // On succes
48- . then ( ( res : AxiosResponse < T > ) => {
49- resolve ( res . data ) ;
50- } )
51- // On error
52- . catch ( ( e : AxiosError < MailcowErrorResponse > ) => {
53- const { msg } = e . response . data ;
54- reject ( new MailcowException ( msg ) ) ;
55- } ) ;
56- } ) ;
62+ try {
63+ const res = await axios . post < T > ( this . ctx . BASE_URL + route , payload , this . ctx . AXIOS_CONFIG ) ;
64+ // Only throws if response has danger or error type
65+ checkMailcowResponse ( res . data as unknown as MailcowResponse ) ;
66+ return res . data ;
67+ } catch ( e ) {
68+ if ( axios . isAxiosError ( e ) && e . response ?. data ) {
69+ // Check if the response *itself* is an error type
70+ checkMailcowResponse ( e . response . data as unknown as MailcowResponse ) ;
71+ // If no definite error, throw generic
72+ throw new MailcowException ( 'Unknown Mailcow error' ) ;
73+ }
74+ throw e ;
75+ }
5776 }
5877
5978 /**
6079 * GET Request Factory
6180 * @param route - The route to which to send the request.
6281 */
6382 async get < T > ( route : string ) : Promise < T > {
64- return new Promise ( ( resolve , reject ) => {
65- axios
66- . get ( this . ctx . BASE_URL + route , this . ctx . AXIOS_CONFIG )
67- // On succes
68- . then ( ( res : AxiosResponse < T > ) => {
69- resolve ( res . data ) ;
70- } )
71- // On error
72- . catch ( ( e : AxiosError < MailcowErrorResponse > ) => {
73- const { msg } = e . response . data ;
74- reject ( new MailcowException ( msg ) ) ;
75- } ) ;
76- } ) ;
83+ try {
84+ const res = await axios . get < T > ( this . ctx . BASE_URL + route , this . ctx . AXIOS_CONFIG ) ;
85+ checkMailcowResponse ( res . data as unknown as MailcowResponse ) ;
86+ return res . data ;
87+ } catch ( e ) {
88+ if ( axios . isAxiosError ( e ) && e . response ?. data ) {
89+ checkMailcowResponse ( e . response . data as unknown as MailcowResponse ) ;
90+ throw new MailcowException ( 'Unknown Mailcow error' ) ;
91+ }
92+ throw e ;
93+ }
7794 }
7895}
0 commit comments