Skip to content

Commit

Permalink
Implement new wasp/server/email API (#1701)
Browse files Browse the repository at this point in the history
  • Loading branch information
infomiho authored and sodic committed Jan 31, 2024
1 parent 8efabdf commit 1bd87ea
Show file tree
Hide file tree
Showing 22 changed files with 65 additions and 49 deletions.
6 changes: 3 additions & 3 deletions waspc/data/Generator/templates/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@
"./test/*": "./dist/test/*.js",
{=! Used by our code, uncodumented (but accessible) for users. =}
"./server/crud/*": "./dist/server/crud/*",
"./email": "./dist/email/index.js",
{=! Used by our code, uncodumented (but accessible) for users. =}
"./email/core/types": "./dist/email/core/types.js",
"./server/email/core/types": "./dist/server/email/core/types.js",
{=! Used by our code, uncodumented (but accessible) for users. =}
"./server/auth/email/utils": "./dist/server/auth/email/utils.js",
{=! Parts are used by users and documented (types), other parts are used by the framework code (entities). =}
Expand Down Expand Up @@ -118,7 +117,8 @@
"./client/auth": "./dist/client/auth/index.js",
"./server/auth": "./dist/server/auth/index.js",
"./server/crud": "./dist/server/crud/index.js",
"./client/crud": "./dist/client/crud/index.js"
"./client/crud": "./dist/client/crud/index.js",
"./server/email": "./dist/server/email/index.js"
},
{=!
TypeScript doesn't care about the redirects we define above in "exports" field; those
Expand Down
4 changes: 2 additions & 2 deletions waspc/data/Generator/templates/sdk/server/auth/email/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{={= =}=}}
import { signData } from 'wasp/auth/jwt'
import { emailSender } from 'wasp/email';
import { Email } from 'wasp/email/core/types';
import { emailSender } from 'wasp/server/email';
import { Email } from 'wasp/server/email/core/types';
import {
createProviderId,
updateAuthIdentityProviderData,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{{={= =}=}}
import { EmailFromField } from "wasp/email/core/types";
import { EmailFromField } from "./types";

// PRIVATE API
// Formats an email address and an optional name into a string that can be used
// as the "from" field in an email.
// { email: "test@test.com, name: "Test" } -> "Test <[email protected]>"
Expand All @@ -18,6 +19,7 @@ export function formatFromField({
}

{=# isDefaultFromFieldDefined =}
// PRIVATE API
export function getDefaultFromField(): EmailFromField {
return {
email: "{= defaultFromField.email =}",
Expand All @@ -28,6 +30,7 @@ export function getDefaultFromField(): EmailFromField {
}
{=/ isDefaultFromFieldDefined =}
{=^ isDefaultFromFieldDefined =}
// PRIVATE API
export function getDefaultFromField(): EmailFromField {
return {
email: "",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
{{={= =}=}}
{=# isSmtpProviderUsed =}
// PRIVATE API
export { initSmtpEmailSender as initEmailSender } from "./providers/smtp.js";
{=/ isSmtpProviderUsed =}
{=# isSendGridProviderUsed =}
// PRIVATE API
export { initSendGridEmailSender as initEmailSender } from "./providers/sendgrid.js";
{=/ isSendGridProviderUsed =}
{=# isMailgunProviderUsed =}
// PRIVATE API
export { initMailgunEmailSender as initEmailSender } from "./providers/mailgun.js";
{=/ isMailgunProviderUsed =}
{=# isDummyProviderUsed =}
// PRIVATE API
export { initDummyEmailSender as initEmailSender } from "./providers/dummy.js";
{=/ isDummyProviderUsed =}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { DummyEmailProvider, EmailSender } from "wasp/email/core/types";
import { DummyEmailProvider, EmailSender } from "../types";
import { getDefaultFromField } from "../helpers.js";

const yellowColor = "\x1b[33m%s\x1b[0m";

// PRIVATE API
export function initDummyEmailSender(
config?: DummyEmailProvider,
): EmailSender {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NodeMailgun } from "ts-mailgun";
import { getDefaultFromField } from "../helpers.js";
import type { MailgunEmailProvider, EmailSender } from "wasp/email/core/types";
import type { MailgunEmailProvider, EmailSender } from "../types";

// PRIVATE API
export function initMailgunEmailSender(
config: MailgunEmailProvider
): EmailSender {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import SendGrid from "@sendgrid/mail";
import { getDefaultFromField } from "../helpers.js";
import type { SendGridProvider, EmailSender } from "wasp/email/core/types";
import type { SendGridProvider, EmailSender } from from "../types";

// PRIVATE API
export function initSendGridEmailSender(
provider: SendGridProvider
): EmailSender {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { createTransport } from "nodemailer";
import { formatFromField, getDefaultFromField } from "../helpers.js";
import type { SMTPEmailProvider, EmailSender } from "wasp/email/core/types";
import type { SMTPEmailProvider, EmailSender } from "../types";

// PRIVATE API
export function initSmtpEmailSender(config: SMTPEmailProvider): EmailSender {
const transporter = createTransport({
host: config.host,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{{={= =}=}}
// PRIVATE API
export type EmailProvider = SMTPEmailProvider | SendGridProvider | MailgunEmailProvider | DummyEmailProvider;

// PRIVATE API
export type SMTPEmailProvider = {
type: "smtp";
host: string;
Expand All @@ -9,27 +11,33 @@ export type SMTPEmailProvider = {
password: string;
};

// PRIVATE API
export type SendGridProvider = {
type: "sendgrid";
apiKey: string;
};

// PRIVATE API
export type MailgunEmailProvider = {
type: "mailgun";
apiKey: string;
domain: string;
};

// PRIVATE API
export type DummyEmailProvider = {
type: "dummy";
}

// PRIVATE API
export type EmailSender = {
send: (email: Email) => Promise<SentMessageInfo>;
};

// PRIVATE API
export type SentMessageInfo = any;

// PRIVATE API
export type Email = {
{=# isDefaultFromFieldDefined =}
from?: EmailFromField;
Expand All @@ -43,6 +51,7 @@ export type Email = {
html: string;
};

// PRIVATE API
export type EmailFromField = {
name?: string;
email: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ const emailProvider = {
} as const;
{=/ isDummyProviderUsed =}

// PUBLIC API
export const emailSender = initEmailSender(emailProvider);
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Router, Request, Response, NextFunction } from "express";

import { ProviderConfig } from "wasp/auth/providers/types";
import type { EmailFromField } from "wasp/email/core/types";
import type { EmailFromField } from "wasp/server/email/core/types";

import { getLoginRoute } from "../email/login.js";
import { getSignupRoute } from "../email/signup.js";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
isEmailResendAllowed,
} from "wasp/server/auth/email/utils";
import { ensureValidEmail } from 'wasp/auth/validation';
import type { EmailFromField } from 'wasp/email/core/types';
import type { EmailFromField } from 'wasp/server/email/core/types';
import { GetPasswordResetEmailContentFn } from 'wasp/server/auth/email';
import { HttpError } from 'wasp/server';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response } from 'express';
import { EmailFromField } from "wasp/email/core/types";
import { EmailFromField } from "wasp/server/email/core/types";
import {
createUser,
createProviderId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { hashPassword } from './password.js'
import { verify } from './jwt.js'
import AuthError from 'wasp/core/AuthError'
import HttpError from 'wasp/core/HttpError'
import { prisma } from 'wasp/server'
import { prisma, HttpError, AuthError } from 'wasp/server'
import { sleep } from 'wasp/server/utils'
import {
type User,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import HttpError from 'wasp/core/HttpError';
import { HttpError } from 'wasp/server';

export const PASSWORD_FIELD = 'password';
const USERNAME_FIELD = 'username';
Expand Down
11 changes: 4 additions & 7 deletions waspc/examples/todo-typescript/.wasp/out/sdk/wasp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
"types": "tsc --declaration --emitDeclarationOnly --stripInternal --declarationDir dist"
},
"exports": {
"./core/HttpError": "./dist/core/HttpError.js",
"./core/AuthError": "./dist/core/AuthError.js",
"./core/config": "./dist/core/config.js",
"./core/stitches.config": "./dist/core/stitches.config.js",
"./core/storage": "./dist/core/storage.js",
Expand All @@ -36,17 +34,14 @@
"./universal/url": "./dist/universal/url.js",
"./universal/types": "./dist/universal/types.js",
"./universal/validators": "./dist/universal/validators.js",
"./server/middleware": "./dist/server/middleware/index.js",
"./server/utils": "./dist/server/utils.js",
"./server/actions": "./dist/server/actions/index.js",
"./server/queries": "./dist/server/queries/index.js",
"./server/auth/email": "./dist/server/auth/email/index.js",
"./dbSeed/types": "./dist/dbSeed/types.js",
"./test": "./dist/test/index.js",
"./test/*": "./dist/test/*.js",
"./server/crud/*": "./dist/server/crud/*",
"./email": "./dist/email/index.js",
"./email/core/types": "./dist/email/core/types.js",
"./server/email/core/types": "./dist/server/email/core/types.js",
"./server/auth/email/utils": "./dist/server/auth/email/utils.js",
"./jobs/*": "./dist/jobs/*.js",
"./jobs/pgBoss/types": "./dist/jobs/pgBoss/types.js",
Expand All @@ -56,6 +51,7 @@
"./webSocket/WebSocketProvider": "./dist/webSocket/WebSocketProvider.jsx",

"./server/types": "./dist/server/types/index.js",
"./server/middleware": "./dist/server/middleware/index.js",

"./server": "./dist/server/index.js",
"./server/api": "./dist/server/api/index.js",
Expand All @@ -64,7 +60,8 @@
"./client/auth": "./dist/client/auth/index.js",
"./server/auth": "./dist/server/auth/index.js",
"./server/crud": "./dist/server/crud/index.js",
"./client/crud": "./dist/client/crud/index.js"
"./client/crud": "./dist/client/crud/index.js",
"./server/email": "./dist/server/email/index.js"
},
"typesVersions": {
"*": {
Expand Down
2 changes: 1 addition & 1 deletion waspc/examples/todo-typescript/src/task/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
DeleteTasks,
} from 'wasp/server/actions/types'
import type { Task } from 'wasp/entities'
import { emailSender } from 'wasp/email'
import { emailSender } from 'wasp/server/email'

type CreateArgs = Pick<Task, 'description'>

Expand Down
8 changes: 4 additions & 4 deletions waspc/src/Wasp/Generator/SdkGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ import Wasp.Generator.Monad (Generator)
import qualified Wasp.Generator.NpmDependencies as N
import Wasp.Generator.SdkGenerator.AuthG (genAuth)
import Wasp.Generator.SdkGenerator.Client.AuthG (genNewClientAuth)
import Wasp.Generator.SdkGenerator.Client.CrudG (genNewClientrudApi)
import Wasp.Generator.SdkGenerator.Client.CrudG (genNewClientCrudApi)
import qualified Wasp.Generator.SdkGenerator.Common as C
import Wasp.Generator.SdkGenerator.CrudG (genCrud)
import Wasp.Generator.SdkGenerator.EmailSenderG (depsRequiredByEmail, genEmailSender)
import Wasp.Generator.SdkGenerator.JobGenerator (genJobTypes)
import Wasp.Generator.SdkGenerator.RouterGenerator (genRouter)
import Wasp.Generator.SdkGenerator.RpcGenerator (genRpc)
import Wasp.Generator.SdkGenerator.Server.AuthG (genNewServerApi)
import Wasp.Generator.SdkGenerator.Server.CrudG (genNewServerCrudApi)
import Wasp.Generator.SdkGenerator.Server.EmailSenderG (depsRequiredByEmail, genNewEmailSenderApi)
import Wasp.Generator.SdkGenerator.ServerApiG (genServerApi)
import Wasp.Generator.SdkGenerator.ServerOpsGenerator (genOperations)
import Wasp.Generator.SdkGenerator.WebSocketGenerator (depsRequiredByWebSockets, genWebSockets)
Expand Down Expand Up @@ -114,12 +114,12 @@ genSdkReal spec =
<++> genRouter spec
<++> genMiddleware spec
<++> genExportedTypesDir spec
<++> genEmailSender spec
-- New API
<++> genNewClientAuth spec
<++> genNewServerApi spec
<++> genNewServerCrudApi spec
<++> genNewClientrudApi spec
<++> genNewClientCrudApi spec
<++> genNewEmailSenderApi spec
where
genFileCopy = return . C.mkTmplFd

Expand Down
6 changes: 3 additions & 3 deletions waspc/src/Wasp/Generator/SdkGenerator/Client/CrudG.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Wasp.Generator.SdkGenerator.Client.CrudG
( genNewClientrudApi,
( genNewClientCrudApi,
)
where

Expand All @@ -17,8 +17,8 @@ import Wasp.Generator.Monad (Generator)
import qualified Wasp.Generator.SdkGenerator.Common as C
import Wasp.Util ((<++>))

genNewClientrudApi :: AppSpec -> Generator [FileDraft]
genNewClientrudApi spec =
genNewClientCrudApi :: AppSpec -> Generator [FileDraft]
genNewClientCrudApi spec =
if areThereAnyCruds
then
sequence
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ data EmailSenderProvider = EmailSenderProvider
data ProvidersDir

providersDirInSdkTemplatesDir :: Path' (Rel C.SdkTemplatesDir) (Dir ProvidersDir)
providersDirInSdkTemplatesDir = [reldir|email/core/providers|]
providersDirInSdkTemplatesDir = [reldir|server/email/core/providers|]

smtp :: EmailSenderProvider
smtp =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Wasp.Generator.SdkGenerator.EmailSenderG where
module Wasp.Generator.SdkGenerator.Server.EmailSenderG where

import Data.Aeson (object, (.=))
import qualified Data.Aeson as Aeson
Expand All @@ -17,8 +17,8 @@ import qualified Wasp.Generator.SdkGenerator.Common as C
import qualified Wasp.Generator.SdkGenerator.EmailSender.Providers as Providers
import Wasp.Util ((<++>))

genEmailSender :: AppSpec -> Generator [FileDraft]
genEmailSender spec = case maybeEmailSender of
genNewEmailSenderApi :: AppSpec -> Generator [FileDraft]
genNewEmailSenderApi spec = case maybeEmailSender of
Just emailSender ->
sequence
[ genIndex emailSender
Expand All @@ -31,7 +31,7 @@ genEmailSender spec = case maybeEmailSender of
genIndex :: EmailSender -> Generator FileDraft
genIndex email = return $ C.mkTmplFdWithData tmplPath tmplData
where
tmplPath = [relfile|email/index.ts|]
tmplPath = [relfile|server/email/index.ts|]
tmplData = getEmailProvidersJson email

genCore :: EmailSender -> Generator [FileDraft]
Expand All @@ -46,13 +46,13 @@ genCore email =
genCoreIndex :: EmailSender -> Generator FileDraft
genCoreIndex email = return $ C.mkTmplFdWithData tmplPath tmplData
where
tmplPath = [relfile|email/core/index.ts|]
tmplPath = [relfile|server/email/core/index.ts|]
tmplData = getEmailProvidersJson email

genCoreTypes :: EmailSender -> Generator FileDraft
genCoreTypes email = return $ C.mkTmplFdWithData tmplPath tmplData
where
tmplPath = [relfile|email/core/types.ts|]
tmplPath = [relfile|server/email/core/types.ts|]
tmplData =
object ["isDefaultFromFieldDefined" .= isDefaultFromFieldDefined]
isDefaultFromFieldDefined = isJust defaultFromField
Expand All @@ -61,7 +61,7 @@ genCoreTypes email = return $ C.mkTmplFdWithData tmplPath tmplData
genCoreHelpers :: EmailSender -> Generator FileDraft
genCoreHelpers email = return $ C.mkTmplFdWithData tmplPath tmplData
where
tmplPath = [relfile|email/core/helpers.ts|]
tmplPath = [relfile|server/email/core/helpers.ts|]
tmplData =
object
[ "defaultFromField"
Expand Down
Loading

0 comments on commit 1bd87ea

Please sign in to comment.