Skip to content

Commit 8d86adc

Browse files
committed
add package
1 parent 2917d87 commit 8d86adc

5 files changed

Lines changed: 157 additions & 0 deletions

File tree

index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as GetTestMail } from './lib/client';

lib/client.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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;

lib/models.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
export interface GetTestMail {
2+
emailAddress: string;
3+
expiresAt: string;
4+
message?: Message;
5+
}
6+
7+
export interface Message {
8+
id: string;
9+
from: string;
10+
to: string;
11+
subject: string;
12+
text: string;
13+
html: string;
14+
attachments: Attachment[];
15+
}
16+
17+
export interface Attachment {
18+
filename: string;
19+
mimeType: string;
20+
content: string;
21+
}
22+
23+
export interface Problem {
24+
type: string;
25+
title: string;
26+
detail: string;
27+
status: number;
28+
}
29+
30+
export type CreateNewRequest = {
31+
expiresAt?: string;
32+
};
33+
34+
export interface CreateNewResponse {
35+
getTestMail: GetTestMail;
36+
}
37+
38+
export interface WaitForMessageResponse {
39+
getTestMail: GetTestMail;
40+
}

package-lock.json

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "typescript-sdk",
3+
"version": "1.0.0",
4+
"description": "Typescript SDK for GetTestMail",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/GetTestMail/typescript-sdk.git"
12+
},
13+
"keywords": [
14+
"testing",
15+
"email",
16+
"GetTestMail",
17+
"typescript"
18+
],
19+
"author": "GetTestMail",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/GetTestMail/typescript-sdk/issues"
23+
},
24+
"homepage": "https://github.com/GetTestMail/typescript-sdk#readme",
25+
"devDependencies": {
26+
"typescript": "5.0.4"
27+
}
28+
}

0 commit comments

Comments
 (0)