Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mail #6

Merged
merged 5 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 0 additions & 12 deletions .editorconfig

This file was deleted.

15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# CINotify API

## Demo

```
curl --request POST 'http://localhost:8787/api/notify' \
-d "[email protected]&subject=🎉"
```

## Local Development

```
npm run dev
npm test
```
15 changes: 9 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { mail } from './mail';

const validate = async (request) => {
const required = ['subject', 'to'];
let data;
let data = {};
try {
if (request.headers.get('content-type') === 'application/json') {
data = (await request.json()) ?? {};
Expand All @@ -13,20 +15,21 @@ const validate = async (request) => {
data = {};
}

data.errors = required.reduce((acc, el) => (data[el] ? acc : [...acc, `missing required parameter '${el}'`]), []);
data.errors = required.reduce((acc, el) => (data?.[el] ? acc : [...acc, `missing required parameter '${el}'`]), []);

return data;
};

export default {
async fetch(request, env, ctx) {
const { pathname } = new URL(request.url);
if (pathname === '/api/notify') {
const { subject, to, errors } = await validate(request);
const body = {
if (pathname === '/api/notify' && request.method === 'POST') {
const { subject, to, errors = [] } = await validate(request);
const body = await mail({
env,
subject,
to,
};
});
const response = {
...body,
...(errors.length && { errors }),
Expand Down
3 changes: 2 additions & 1 deletion test/index.spec.js → src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ describe('API', () => {
});
expect(response.status).toEqual(400);
});
it('responds to application/json and application/x-www-form-urlencoded', async () => {
// TODO: mock worker outbound fetch
it.skip('responds to application/json and application/x-www-form-urlencoded', async () => {
const jsonResponse = await send(
new Request('http://example.com/api/notify', {
method: 'POST',
Expand Down
34 changes: 34 additions & 0 deletions src/mail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export const mail = async ({ subject, to, env = {} }) => {
const response = await fetch('https://api.sendgrid.com/v3/mail/send', {
headers: {
Authorization: `Bearer ${env.SENDGRID_API_KEY}`,
'Content-Type': 'application/json',
},
method: 'POST',
body: JSON.stringify({
// https://docs.sendgrid.com/api-reference/mail-send/mail-send#body
content: [
{
type: 'text/plain',
value: ' ',
},
],
from: {
email: '[email protected]',
},
personalizations: [
{
subject,
to: [{ email: to }],
},
],
}),
});
let data;
try {
data = await response.json();
} catch (e) {
data = {};
}
return data;
};
28 changes: 28 additions & 0 deletions src/mail.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, it, expect, vi, beforeAll, afterAll } from 'vitest';
import { mail } from './mail';

const originalFetch = global.fetch;

describe('mail', () => {
beforeAll(() => {
global.fetch = vi.fn();
});
afterAll(() => {
global.fetch = originalFetch;
vi.clearAllMocks();
});
it('makes a request to the sendgrid api', async () => {
global.fetch.mockResolvedValueOnce({ json: () => '{}' });
const to = '[email protected]';
const subject = 'hello';
await mail({ to, subject });
expect(global.fetch).toHaveBeenCalledWith('https://api.sendgrid.com/v3/mail/send', {
headers: {
Authorization: expect.any(String),
'Content-Type': 'application/json',
},
method: 'POST',
body: expect.stringMatching(/hello(.*)[email protected]/),
});
});
});
Loading