1+ import { CreateNewRequest , CreateNewResponse , GetTestMail , Problem , WaitForMessageResponse } from "./models" ;
2+
3+ class GetTestMailClient {
4+ private apiKey : string ;
5+ private baseUrl : string ;
6+
7+ constructor ( apiKey : string , baseUrl : string = 'https://gettestmail.com/api' ) {
8+ this . apiKey = apiKey ;
9+ this . baseUrl = baseUrl ;
10+ }
11+
12+ async createNew ( request : CreateNewRequest ) : Promise < CreateNewResponse > {
13+ const response = await fetch ( `${ this . baseUrl } /gettestmail` , {
14+ method : 'POST' ,
15+ headers : {
16+ 'Content-Type' : 'application/json' ,
17+ 'X-API-Key' : this . apiKey ,
18+ } ,
19+ body : JSON . stringify ( request ) ,
20+ } ) ;
21+
22+ if ( ! response . ok ) {
23+ const problem : Problem = await response . json ( ) ;
24+ throw new Error ( problem . detail ) ;
25+ }
26+
27+ const getTestMail : GetTestMail = await response . json ( ) ;
28+ return { getTestMail } ;
29+ }
30+
31+ async waitForMessage ( id : string ) : Promise < WaitForMessageResponse > {
32+ const response = await fetch ( `${ this . baseUrl } /gettestmail/${ id } ` , {
33+ method : 'GET' ,
34+ headers : {
35+ 'Content-Type' : 'application/json' ,
36+ 'X-API-Key' : this . apiKey ,
37+ } ,
38+ } ) ;
39+
40+ if ( ! response . ok ) {
41+ const problem : Problem = await response . json ( ) ;
42+ throw new Error ( problem . detail ) ;
43+ }
44+
45+ const getTestMail : GetTestMail = await response . json ( ) ;
46+ return { getTestMail } ;
47+ }
48+ }
49+
50+
51+ export default GetTestMailClient ;
0 commit comments