Skip to content

Commit

Permalink
add logging (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
jshawl authored Mar 27, 2024
1 parent ac25e35 commit b63f670
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/handlers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { payload } from './mail';

describe('handlers', () => {
beforeAll(() => {
global.fetch = vi.fn().mockImplementation(() => new Response());
global.fetch = vi.fn().mockImplementation(() => new Response('{}'));
});

it('400 if missing required parameters', async () => {
Expand Down
20 changes: 20 additions & 0 deletions src/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const log = async ({ env, user_id, tags }) => {
const response = await fetch('https://api.logsnag.com/v1/log', {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${env.LOGSNAG_API_TOKEN}`,
},
body: JSON.stringify({
project: 'cinotify',
channel: 'api',
event: 'mail',
icon: '📬',
user_id,
tags,
}),
method: 'POST',
});
const data = await response.json();
// eslint-disable-next-line no-console
console.log(data);
};
11 changes: 11 additions & 0 deletions src/mail.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { log } from './log';

export const payload = ({ subject, to, type, body, attachments }) => ({
// https://docs.sendgrid.com/api-reference/mail-send/mail-send#body
attachments,
Expand Down Expand Up @@ -31,6 +33,15 @@ export const mail = async ({ env = {}, ...rest }) => {
method: 'POST',
body: JSON.stringify(payload({ ...rest })),
});
await log({
env,
user_id: rest.to?.split(',')[0],
tags: {
attachments: !!rest.attachments,
contentType: rest.type,
status: response.status,
},
});
if (response.status > 299) {
throw new Error((await response.json()).errors[0].message);
}
Expand Down
23 changes: 22 additions & 1 deletion src/mail.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('mail', () => {
global.fetch = vi.fn();
});
it('makes a request to the sendgrid api', async () => {
global.fetch.mockResolvedValueOnce({ json: () => '{}' });
global.fetch.mockResolvedValue({ json: () => '{}' });
await mail(input);
expect(global.fetch).toHaveBeenCalledWith(
'https://api.sendgrid.com/v3/mail/send',
Expand All @@ -45,6 +45,27 @@ describe('mail', () => {
},
);
});
it('logs', async () => {
global.fetch.mockResolvedValue({ json: () => '{}', status: 200 });
await mail({ ...input, to: '[email protected],[email protected]' });
expect(global.fetch).toHaveBeenCalledWith(
'https://api.logsnag.com/v1/log',
expect.objectContaining({
body: JSON.stringify({
project: 'cinotify',
channel: 'api',
event: 'mail',
icon: '📬',
user_id: '[email protected]',
tags: {
attachments: true,
contentType: 'text/html',
status: 200,
},
}),
}),
);
});
});

describe('payload', () => {
Expand Down

0 comments on commit b63f670

Please sign in to comment.