Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/lib/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,70 @@ export class EmailAPI {
}
}

async getSenders(credentials?: ApiCredentials): Promise<any> {
Comment thread
shaply marked this conversation as resolved.
Outdated
const headers: Record<string, string> = {};
if (credentials?.userJwt) {
headers['X-User-JWT'] = credentials.userJwt;
}
if (credentials?.projectId !== undefined) {
headers['X-Project-Id'] = String(credentials.projectId);
}

const basePath =
(this.internalApi as any).configuration?.basePath ?? '';
const token =
(this.internalApi as any).configuration?.accessToken;
if (token) {
const tokenString = typeof token === 'function' ? await token('API_Key', []) : token;
if (tokenString) {
headers['Authorization'] = `Bearer ${tokenString}`;
}
}
Comment thread
shaply marked this conversation as resolved.
Outdated

const response = await fetch(`${basePath}/email/senders`, {
Comment thread
shaply marked this conversation as resolved.
method: 'GET',
headers,
});

if (!response.ok) {
throw new Error(`Failed to get senders: ${response.statusText}`);
}

return await response.json();
}

async getDomains(credentials?: ApiCredentials): Promise<any> {
const headers: Record<string, string> = {};
if (credentials?.userJwt) {
headers['X-User-JWT'] = credentials.userJwt;
}
if (credentials?.projectId !== undefined) {
headers['X-Project-Id'] = String(credentials.projectId);
}

const basePath =
(this.internalApi as any).configuration?.basePath ?? '';
const token =
(this.internalApi as any).configuration?.accessToken;
if (token) {
const tokenString = typeof token === 'function' ? await token('API_Key', []) : token;
if (tokenString) {
headers['Authorization'] = `Bearer ${tokenString}`;
}
}

const response = await fetch(`${basePath}/email/domains`, {
method: 'GET',
headers,
});

if (!response.ok) {
throw new Error(`Failed to get domains: ${response.statusText}`);
}

return await response.json();
}
Comment thread
shaply marked this conversation as resolved.
Outdated

async getStatistics(
options: {
startDate: string;
Expand Down
50 changes: 50 additions & 0 deletions tests/email/emailApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,53 @@ describe('verify domain validation tests', () => {
).rejects.toThrow(JunoValidationError);
});
});

describe('getSenders tests', () => {
it('throws an error when called without API key', async () => {
let emailApi = new EmailAPI();

await expect(
(async () => {
await emailApi.getSenders();
})()
).rejects.toThrow();
});

it('throws an error when called with invalid credentials', async () => {
let emailApi = new EmailAPI('http://localhost:invalid');

await expect(
(async () => {
await emailApi.getSenders({
userJwt: 'invalid-jwt',
projectId: 1,
});
})()
).rejects.toThrow();
});
});

describe('getDomains tests', () => {
it('throws an error when called without API key', async () => {
let emailApi = new EmailAPI();

await expect(
(async () => {
await emailApi.getDomains();
})()
).rejects.toThrow();
});

it('throws an error when called with invalid credentials', async () => {
let emailApi = new EmailAPI('http://localhost:invalid');

await expect(
(async () => {
await emailApi.getDomains({
userJwt: 'invalid-jwt',
projectId: 1,
});
})()
).rejects.toThrow();
});
});
Comment thread
shaply marked this conversation as resolved.
Loading