Skip to content

Commit

Permalink
add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jshawl committed Mar 27, 2024
1 parent ac25e35 commit bf89be4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const log = async ({ env, user_id, tags }) => {
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',
});
};
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
21 changes: 21 additions & 0 deletions src/mail.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,27 @@ describe('mail', () => {
},
);
});
it('logs', async () => {
global.fetch.mockResolvedValueOnce({ 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 bf89be4

Please sign in to comment.