Skip to content
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
3 changes: 2 additions & 1 deletion .astro/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/// <reference types="astro/client" />
/// <reference path="content.d.ts" />
/// <reference path="content.d.ts" />
/// <reference path="env.d.ts" />
2 changes: 2 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BREVO_API_KEY=<insert here>
BREVO_LIST_ID=3
3 changes: 3 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/prepare
- run: cp .env.template .env
- run: pnpm build
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/prepare
- run: pnpm astro sync
- run: pnpm lint
lint_knip:
name: Lint Knip
Expand Down Expand Up @@ -47,6 +49,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/prepare
- run: pnpm astro sync
- run: pnpm tsc

name: CI
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.astro
.env
.vercel
/dist
/node_modules
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ pnpm i
pnpm dev
```

### Environment Variables

To get the newsletter API running, copy `.env.template` to an `.env` file and fill in the Brevo API key value from our password manager.
This is not necessary unless you want to work on the newsletter API.

## Contributors

<!-- spellchecker: disable -->
Expand Down
8 changes: 7 additions & 1 deletion astro.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import vercel from "@astrojs/vercel";
import { konamiEmojiBlast } from "@konami-emoji-blast/astro";
import { defineConfig } from "astro/config";
import { defineConfig, envField } from "astro/config";

export default defineConfig({
adapter: vercel({
webAnalytics: { enabled: true },
}),
env: {
schema: {
BREVO_API_KEY: envField.string({ access: "secret", context: "server" }),
BREVO_LIST_ID: envField.number({ access: "public", context: "server" }),
},
},
image: {
layout: "constrained",
responsiveStyles: true,
Expand Down
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"words": [
"astropub",
"bootcamps",
"brevo",
"d'œuvres",
"devries",
"dimitri",
Expand Down
1 change: 1 addition & 0 deletions eslint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export default defineConfig(
},
},
rules: {
"n/no-missing-import": "off",
"n/no-unpublished-import": "off",

// Stylistic concerns that don't interfere with Prettier
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"sharp": "^0.34.5",
"temporal-polyfill": "^0.3.0",
"uqr": "^0.1.2",
"wifi-share-link": "^0.1.2"
"wifi-share-link": "^0.1.2",
"zod": "^4.3.6"
},
"devDependencies": {
"@eslint-community/eslint-plugin-eslint-comments": "^4.6.0",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 2 additions & 7 deletions src/components/Newsletter.astro
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ import Heading from "./Heading.astro";
Sign up to receive announcements and important SquiggleConf info.
</Heading>

<form
action="https://app.loops.so/api/newsletter-form/clwpkthom0067d11n8jbrxb3b"
id="newsletter-form"
method="POST"
>
<form action="/api/newsletter" id="newsletter-form" method="POST">
<label class="email-label" for="email">Email Address</label>
<input
class="email-input"
Expand Down Expand Up @@ -103,10 +99,9 @@ import Heading from "./Heading.astro";
"Content-Type": "application/x-www-form-urlencoded",
},
})
.then((response) => [response.ok, response.json(), response] as const)
.then((response) => [response.ok, response.text(), response] as const)
.then(([ok, dataPromise, response]) => {
dataPromise.then((data) => {
console.log({ data });
if (ok) {
setMessage("Thanks for signing up!", "happy");
form.reset();
Expand Down
52 changes: 52 additions & 0 deletions src/pages/api/newsletter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { APIRoute } from "astro";
import { BREVO_API_KEY, BREVO_LIST_ID } from "astro:env/server";
import { z } from "zod";

const bodySchema = z.object({
email: z.email(),
});

export const POST: APIRoute = async ({ request }) => {
const formData = Object.fromEntries(await request.formData());
const body = bodySchema.safeParse(formData);
if (body.error) {
return new Response("Invalid body", {
status: 400,
statusText: body.error.message,
});
}

const { email } = body.data;

try {
const response = await fetch("https://api.brevo.com/v3/contacts", {
body: JSON.stringify({
email,
listIds: [BREVO_LIST_ID],
}),
headers: {
"api-key": BREVO_API_KEY,
"Content-Type": "application/json",
},
method: "POST",
});

console.log(response);

return response.ok || isBrevoDuplicateIdentifier(await response.json())
? new Response("Thanks for signing up!", { status: 200 })
: new Response("Error", { status: 400 });
} catch (error) {
console.error(error);
return new Response("Error", { status: 400 });
}
};

function isBrevoDuplicateIdentifier(json: unknown) {
return (
typeof json === "object" &&
!!json &&
"code" in json &&
json.code === "duplicate_parameter"
);
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
"strict": true,
"target": "ESNext"
},
"include": ["api", "src"]
"include": ["src"]
}
Loading