1- import Try from '@borisnl/tried' ;
1+ import { Try } from '@borisnl/tried' ;
22import ky from 'ky' ;
3- import { Meel , MeelError , SentMeel , SentMeelConstructor } from '.' ;
3+ import { Meel , MeelError , MeelErrorConstructor , SentMeel , SentMeelConstructor } from '.' ;
44
55/**
66 * MeelSender is a class that sends Meel instances to a specified base url.
@@ -36,7 +36,7 @@ export class MeelSender {
3636 * @return {Promise<SentMeel> } SentMeel instance
3737 * @throws {MeelError } If the mail could not be sent
3838 */
39- public async send ( mail : Meel ) : Promise < SentMeel > {
39+ public async send ( mail : Meel ) : Promise < ( SentMeel | MeelError ) > {
4040 return this . batchSend ( [ mail ] ) . then ( data => data [ 0 ] ) ;
4141 }
4242
@@ -47,10 +47,14 @@ export class MeelSender {
4747 * @returns {Promise<SentMeel> } SentMeel instance
4848 * @throws {MeelError } If the mail could not be sent
4949 */
50- public async batchSend ( mails : Meel [ ] ) : Promise < SentMeel [ ] > {
50+ public async batchSend ( mails : Meel [ ] ) : Promise < ( SentMeel | MeelError ) [ ] > {
5151 const response = await Try ( ( ) =>
5252 ky
53- . post < SentMeelConstructor [ ] > ( `${ this . baseUrl } /mails/send` , {
53+ . post < ( {
54+ Ok : SentMeelConstructor
55+ } | {
56+ Err : MeelErrorConstructor
57+ } ) [ ] > ( `${ this . baseUrl } /mails/send` , {
5458 body : JSON . stringify (
5559 mails . map ( mail => mail . toPlainObject ( ) ) ,
5660 ) ,
@@ -62,9 +66,14 @@ export class MeelSender {
6266 ) ;
6367
6468 if ( ! response ) {
65- throw new MeelError ( 'Failed to send mail' , 500 ) ;
69+ throw new MeelError ( {
70+ status_code : 500 ,
71+ error_code : 0 ,
72+ message : 'Failed to send mail' ,
73+ details : { } ,
74+ } ) ;
6675 }
6776
68- return response . map ( ( data : SentMeelConstructor ) => new SentMeel ( data ) ) ;
77+ return response . map ( ( data ) => ( 'Ok' in data ) ? new SentMeel ( data . Ok ) : new MeelError ( data . Err ) ) ;
6978 }
7079}
0 commit comments