Update Biome formatting settings, improved self-hosting capability and reformatted all files

This commit is contained in:
Dries Augustyns
2024-08-02 10:52:42 +02:00
parent b7463af088
commit e8731048c4
59 changed files with 841 additions and 2982 deletions
+4 -31
View File
@@ -1,21 +1,9 @@
import {
Controller,
Delete,
Get,
Middleware,
Post,
Put,
} from "@overnightjs/core";
import { Controller, Delete, Get, Middleware, Post, Put } from "@overnightjs/core";
import { ActionSchemas, UtilitySchemas } from "@plunk/shared";
import type { Request, Response } from "express";
import { prisma } from "../../database/prisma";
import { NotFound } from "../../exceptions";
import {
type IJwt,
type ISecret,
isAuthenticated,
isValidSecretKey,
} from "../../middleware/auth";
import { type IJwt, type ISecret, isAuthenticated, isValidSecretKey } from "../../middleware/auth";
import { ActionService } from "../../services/ActionService";
import { EventService } from "../../services/EventService";
import { MembershipService } from "../../services/MembershipService";
@@ -83,14 +71,7 @@ export class Actions {
throw new NotFound("project");
}
const {
name,
runOnce,
delay,
template: templateId,
events,
notevents,
} = ActionSchemas.create.parse(req.body);
const { name, runOnce, delay, template: templateId, events, notevents } = ActionSchemas.create.parse(req.body);
const template = await TemplateService.id(templateId);
@@ -160,15 +141,7 @@ export class Actions {
throw new NotFound("project");
}
const {
id,
template: templateId,
events,
notevents,
name,
runOnce,
delay,
} = ActionSchemas.update.parse(req.body);
const { id, template: templateId, events, notevents, name, runOnce, delay } = ActionSchemas.update.parse(req.body);
let action = await ActionService.id(id);
+6 -28
View File
@@ -1,22 +1,10 @@
import {
Controller,
Delete,
Get,
Middleware,
Post,
Put,
} from "@overnightjs/core";
import { Controller, Delete, Get, Middleware, Post, Put } from "@overnightjs/core";
import { CampaignSchemas, UtilitySchemas } from "@plunk/shared";
import dayjs from "dayjs";
import type { Request, Response } from "express";
import { prisma } from "../../database/prisma";
import { HttpException, NotFound } from "../../exceptions";
import {
type IJwt,
type ISecret,
isAuthenticated,
isValidSecretKey,
} from "../../middleware/auth";
import { type IJwt, type ISecret, isAuthenticated, isValidSecretKey } from "../../middleware/auth";
import { CampaignService } from "../../services/CampaignService";
import { EmailService } from "../../services/EmailService";
import { MembershipService } from "../../services/MembershipService";
@@ -39,10 +27,7 @@ export class Campaigns {
throw new NotFound("campaign");
}
const isMember = await MembershipService.isMember(
campaign.projectId,
userId,
);
const isMember = await MembershipService.isMember(campaign.projectId, userId);
if (!isMember) {
throw new NotFound("campaign");
@@ -122,10 +107,7 @@ export class Campaigns {
await EmailService.send({
from: {
name: project.from ?? project.name,
email:
project.verified && project.email
? project.email
: "no-reply@useplunk.dev",
email: project.verified && project.email ? project.email : "no-reply@useplunk.dev",
},
to: members.map((m) => m.email),
content: {
@@ -197,9 +179,7 @@ export class Campaigns {
throw new NotFound("project");
}
let { subject, body, recipients, style } = CampaignSchemas.create.parse(
req.body,
);
let { subject, body, recipients, style } = CampaignSchemas.create.parse(req.body);
if (recipients.length === 1 && recipients[0] === "all") {
const projectContacts = await prisma.contact.findMany({
@@ -251,9 +231,7 @@ export class Campaigns {
}
// eslint-disable-next-line prefer-const
let { id, subject, body, recipients, style } = CampaignSchemas.update.parse(
req.body,
);
let { id, subject, body, recipients, style } = CampaignSchemas.update.parse(req.body);
if (recipients.length === 1 && recipients[0] === "all") {
const projectContacts = await prisma.contact.findMany({
+5 -23
View File
@@ -1,22 +1,10 @@
import {
Controller,
Delete,
Get,
Middleware,
Post,
Put,
} from "@overnightjs/core";
import { Controller, Delete, Get, Middleware, Post, Put } from "@overnightjs/core";
import { ContactSchemas, UtilitySchemas } from "@plunk/shared";
import type { Request, Response } from "express";
import z from "zod";
import { prisma } from "../../database/prisma";
import { HttpException, NotFound } from "../../exceptions";
import {
type IKey,
type ISecret,
isValidKey,
isValidSecretKey,
} from "../../middleware/auth";
import { type IKey, type ISecret, isValidKey, isValidSecretKey } from "../../middleware/auth";
import { ActionService } from "../../services/ActionService";
import { ContactService } from "../../services/ContactService";
import { EventService } from "../../services/EventService";
@@ -150,9 +138,7 @@ export class Contacts {
await redis.del(Keys.Contact.id(contact.id));
await redis.del(Keys.Contact.email(project.id, contact.email));
return res
.status(200)
.json({ success: true, contact: contact.id, subscribed: false });
return res.status(200).json({ success: true, contact: contact.id, subscribed: false });
}
@Post("subscribe")
@@ -203,9 +189,7 @@ export class Contacts {
await redis.del(Keys.Contact.id(contact.id));
await redis.del(Keys.Contact.email(project.id, contact.email));
return res
.status(200)
.json({ success: true, contact: contact.id, subscribed: true });
return res.status(200).json({ success: true, contact: contact.id, subscribed: true });
}
@Post()
@@ -262,9 +246,7 @@ export class Contacts {
throw new NotFound("project");
}
const { id, email, subscribed, data } = ContactSchemas.update.parse(
req.body,
);
const { id, email, subscribed, data } = ContactSchemas.update.parse(req.body);
let contact = await ContactService.id(id);
+6 -28
View File
@@ -1,22 +1,9 @@
import { randomBytes } from "node:crypto";
import {
Controller,
Delete,
Get,
Middleware,
Post,
Put,
} from "@overnightjs/core";
import { Controller, Delete, Get, Middleware, Post, Put } from "@overnightjs/core";
import { TemplateSchemas, UtilitySchemas } from "@plunk/shared";
import type { Request, Response } from "express";
import { prisma } from "../../database/prisma";
import { NotAllowed, NotFound } from "../../exceptions";
import {
type IJwt,
type ISecret,
isAuthenticated,
isValidSecretKey,
} from "../../middleware/auth";
import { type IJwt, type ISecret, isAuthenticated, isValidSecretKey } from "../../middleware/auth";
import { MembershipService } from "../../services/MembershipService";
import { ProjectService } from "../../services/ProjectService";
import { TemplateService } from "../../services/TemplateService";
@@ -38,10 +25,7 @@ export class Templates {
throw new NotFound("template");
}
const isMember = await MembershipService.isMember(
template.projectId,
userId,
);
const isMember = await MembershipService.isMember(template.projectId, userId);
if (!isMember) {
throw new NotFound("template");
@@ -117,9 +101,7 @@ export class Templates {
throw new NotFound("project");
}
const { subject, body, type, style } = TemplateSchemas.create.parse(
req.body,
);
const { subject, body, type, style } = TemplateSchemas.create.parse(req.body);
const template = await prisma.template.create({
data: {
@@ -169,9 +151,7 @@ export class Templates {
throw new NotFound("project");
}
const { id, subject, body, type, style } = TemplateSchemas.update.parse(
req.body,
);
const { id, subject, body, type, style } = TemplateSchemas.update.parse(req.body);
let template = await TemplateService.id(id);
@@ -238,9 +218,7 @@ export class Templates {
const actions = await TemplateService.actions(id);
if (actions && actions.length > 0) {
throw new NotAllowed(
"This template is being used by an action. Unlink the action before deleting the template.",
);
throw new NotAllowed("This template is being used by an action. Unlink the action before deleting the template.");
}
await prisma.template.delete({ where: { id } });
+22 -63
View File
@@ -1,21 +1,11 @@
import {
ChildControllers,
Controller,
Middleware,
Post,
} from "@overnightjs/core";
import { ChildControllers, Controller, Middleware, Post } from "@overnightjs/core";
import { EventSchemas } from "@plunk/shared";
import dayjs from "dayjs";
import type { Request, Response } from "express";
import signale from "signale";
import { prisma } from "../../database/prisma";
import { HttpException, NotAllowed } from "../../exceptions";
import {
type IKey,
type ISecret,
isValidKey,
isValidSecretKey,
} from "../../middleware/auth";
import { type IKey, type ISecret, isValidKey, isValidSecretKey } from "../../middleware/auth";
import { ActionService } from "../../services/ActionService";
import { ContactService } from "../../services/ContactService";
import { EmailService } from "../../services/EmailService";
@@ -30,13 +20,7 @@ import { Events } from "./Events";
import { Templates } from "./Templates";
@Controller("v1")
@ChildControllers([
new Actions(),
new Templates(),
new Campaigns(),
new Contacts(),
new Events(),
])
@ChildControllers([new Actions(), new Templates(), new Campaigns(), new Contacts(), new Events()])
export class V1 {
@Post()
@Post("track")
@@ -53,14 +37,9 @@ export class V1 {
const result = EventSchemas.post.safeParse(req.body);
if (!result.success) {
signale.warn(
`${project.name} tried tracking an event with invalid data: ${JSON.stringify(req.body)}`,
);
signale.warn(`${project.name} tried tracking an event with invalid data: ${JSON.stringify(req.body)}`);
if ("unionErrors" in result.error.issues[0]) {
throw new HttpException(
400,
result.error.issues[0].unionErrors[0].errors[0].message,
);
throw new HttpException(400, result.error.issues[0].unionErrors[0].errors[0].message);
}
throw new HttpException(400, result.error.issues[0].message);
@@ -78,10 +57,7 @@ export class V1 {
event = await prisma.event.create({
data: { name, projectId: project.id },
});
redis.set(
Keys.Event.event(project.id, event.name),
JSON.stringify(event),
);
redis.set(Keys.Event.event(project.id, event.name), JSON.stringify(event));
redis.set(Keys.Event.id(event.id), JSON.stringify(event));
redis.del(Keys.Project.events(project.id, true));
@@ -139,9 +115,7 @@ export class V1 {
void ActionService.trigger({ event, contact, project });
signale.success(
`${project.name} triggered ${event.name} for ${contact.email}`,
);
signale.success(`${project.name} triggered ${event.name} for ${contact.email}`);
return res.status(200).json({
success: true,
@@ -166,30 +140,20 @@ export class V1 {
if (!result.success) {
if ("unionErrors" in result.error.issues[0]) {
throw new HttpException(
400,
result.error.issues[0].unionErrors[0].errors[0].message,
);
throw new HttpException(400, result.error.issues[0].unionErrors[0].errors[0].message);
}
throw new HttpException(400, result.error.issues[0].message);
}
const { from, name, reply, to, subject, body, subscribed, headers } =
result.data;
const { from, name, reply, to, subject, body, subscribed, headers } = result.data;
if (!project.email || !project.verified) {
throw new HttpException(
401,
"Verify your domain before you start sending",
);
throw new HttpException(401, "Verify your domain before you start sending");
}
if (from && from.split("@")[1] !== project.email?.split("@")[1]) {
throw new HttpException(
401,
"Custom from address must be from a verified domain",
);
throw new HttpException(401, "Custom from address must be from a verified domain");
}
const emails: {
@@ -231,16 +195,15 @@ export class V1 {
}
}
const { subject: enrichedSubject, body: enrichedBody } =
EmailService.format({
subject,
body,
data: {
plunk_id: contact.id,
plunk_email: contact.email,
...JSON.parse(contact.data ?? "{}"),
},
});
const { subject: enrichedSubject, body: enrichedBody } = EmailService.format({
subject,
body,
data: {
plunk_id: contact.id,
plunk_email: contact.email,
...JSON.parse(contact.data ?? "{}"),
},
});
const { messageId } = await EmailService.send({
from: {
@@ -287,12 +250,8 @@ export class V1 {
redis.del(Keys.Project.emails(project.id));
redis.del(Keys.Project.emails(project.id, { count: true }));
signale.success(
`${project.name} sent a transactional email to ${to.join(", ")}`,
);
signale.success(`${project.name} sent a transactional email to ${to.join(", ")}`);
return res
.status(200)
.json({ success: true, emails, timestamp: dayjs().toISOString() });
return res.status(200).json({ success: true, emails, timestamp: dayjs().toISOString() });
}
}