Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
chribjel committed Feb 22, 2024
1 parent 7669962 commit 4a2741c
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 130 deletions.
6 changes: 5 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"useTabs": true
"useTabs": true,
"semi": true,
"singleQuote": false,
"trailingComma": "es5",
"singleAttributePerLine": true
}
46 changes: 27 additions & 19 deletions adapters/postmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,33 @@ import type { ServerClient } from "postmark";
import { renderToStaticMarkup } from "react-dom/server";
import type { PostmarkProvider, SendEmailInput } from "../types";


function createPayloadFromEmailInput(email: SendEmailInput): Parameters<ServerClient["sendEmail"]>[0] {
return {
From: email.from,
To: Array.isArray(email.to) ? email.to.join(",") : email.to,
Cc: Array.isArray(email.cc) ? email.cc.join(",") : email.cc,
Bcc: Array.isArray(email.bcc) ? email.bcc.join(",") : email.bcc,
ReplyTo: Array.isArray(email.replyTo) ? email.replyTo.join(",") : email.replyTo,
Subject: email.subject,
...("html" in email && email.html
? { HtmlBody: email.html }
: "text" in email && email.text
? { TextBody: email.text }
: "react" in email && email.react
? { HtmlBody: renderToStaticMarkup(email.react) }
: { TextBody: "" }),
};
function createPayloadFromEmailInput(
email: SendEmailInput
): Parameters<ServerClient["sendEmail"]>[0] {
return {
From: email.from,
To: Array.isArray(email.to) ? email.to.join(",") : email.to,
Cc: Array.isArray(email.cc) ? email.cc.join(",") : email.cc,
Bcc: Array.isArray(email.bcc) ? email.bcc.join(",") : email.bcc,
ReplyTo: Array.isArray(email.replyTo)
? email.replyTo.join(",")
: email.replyTo,
Subject: email.subject,
...("html" in email && email.html
? { HtmlBody: email.html }
: "text" in email && email.text
? { TextBody: email.text }
: "react" in email && email.react
? { HtmlBody: renderToStaticMarkup(email.react) }
: { TextBody: "" }),
};
}

export function sendEmail(postmarkProvider: PostmarkProvider, email: SendEmailInput) {
return postmarkProvider.postmark.sendEmail(createPayloadFromEmailInput(email)) as any;
export function sendEmail(
postmarkProvider: PostmarkProvider,
email: SendEmailInput
) {
return postmarkProvider.postmark.sendEmail(
createPayloadFromEmailInput(email)
) as any;
}
43 changes: 25 additions & 18 deletions adapters/resend.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import type { Resend } from "resend";
import type { ResendProvider, SendEmailInput } from "../types";

function createPayloadFromEmailInput(email: SendEmailInput): Parameters<Resend["emails"]["send"]>[0] {
return {
to: email.to,
cc: email.cc,
bcc: email.bcc,
reply_to: email.replyTo,
from: email.from,
subject: email.subject,
...("html" in email && email.html
? { html: email.html }
: "text" in email && email.text
? { text: email.text }
: "react" in email && email.react
? { react: email.react }
: { text: "" }),
};
function createPayloadFromEmailInput(
email: SendEmailInput
): Parameters<Resend["emails"]["send"]>[0] {
return {
to: email.to,
cc: email.cc,
bcc: email.bcc,
reply_to: email.replyTo,
from: email.from,
subject: email.subject,
...("html" in email && email.html
? { html: email.html }
: "text" in email && email.text
? { text: email.text }
: "react" in email && email.react
? { react: email.react }
: { text: "" }),
};
}

export function sendEmail(resendProvider: ResendProvider, email: SendEmailInput) {
return resendProvider.resend.emails.send(createPayloadFromEmailInput(email)) as any;
export function sendEmail(
resendProvider: ResendProvider,
email: SendEmailInput
) {
return resendProvider.resend.emails.send(
createPayloadFromEmailInput(email)
) as any;
}
38 changes: 18 additions & 20 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

import type { Provider, SendEmailInput } from "./types";
import { sendEmail as sendEmailResend } from "./adapters/resend";
import { sendEmail as sendEmailPostmark } from "./adapters/postmark";


export function createClient(providers: Provider[]) {
return {
async sendEmail(email: SendEmailInput) {
Expand All @@ -20,9 +18,9 @@ export function createClient(providers: Provider[]) {
}
result = {
type: providerType,
index: providers.indexOf(provider),
custom: res
};
index: providers.indexOf(provider),
custom: res,
};
}
break;

Expand All @@ -34,25 +32,25 @@ export function createClient(providers: Provider[]) {
}
result = {
type: providerType,
index: providers.indexOf(provider),
resend: res as any
};
index: providers.indexOf(provider),
resend: res as any,
};
}
break;

case "postmark":
{
const res = sendEmailPostmark(provider, email);
if (res.ErrorCode) {
throw new Error(res.Message);
}
result = {
case "postmark":
{
const res = sendEmailPostmark(provider, email);
if (res.ErrorCode) {
throw new Error(res.Message);
}
result = {
type: providerType,
index: providers.indexOf(provider),
postmark: res as any
};
}
break;
index: providers.indexOf(provider),
postmark: res as any,
};
}
break;

default:
providerType satisfies never;
Expand Down
64 changes: 32 additions & 32 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
{
"name": "fallback-email",
"version": "0.0.3",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "bunx tsc",
"prepublishOnly": "bun run build"
},
"files": [
"dist"
],
"license": "MIT",
"homepage": "https://github.com/Decidable-AS/fallback-email",
"repository": {
"type": "git",
"url": "git+https://github.com/Decidable-AS/fallback-email.git"
},
"bugs": "https://github.com/wobsoriano/pkg-name/issues",
"author": "Christoffer Bjelke",
"devDependencies": {
"@types/bun": "latest",
"@types/react": "^18.2.57",
"@types/react-dom": "^18.2.19",
"bun-plugin-dts": "^0.2.1",
"postmark": "^4.0.2",
"prettier": "^3.2.5",
"resend": "^3.2.0",
"typescript": "^5.0.0"
},
"peerDependencies": {
"react-dom": "^18.2.0"
}
"name": "fallback-email",
"version": "0.0.3",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "bunx tsc",
"prepublishOnly": "bun run build"
},
"files": [
"dist"
],
"license": "MIT",
"homepage": "https://github.com/Decidable-AS/fallback-email",
"repository": {
"type": "git",
"url": "git+https://github.com/Decidable-AS/fallback-email.git"
},
"bugs": "https://github.com/wobsoriano/pkg-name/issues",
"author": "Christoffer Bjelke",
"devDependencies": {
"@types/bun": "latest",
"@types/react": "^18.2.57",
"@types/react-dom": "^18.2.19",
"bun-plugin-dts": "^0.2.1",
"postmark": "^4.0.2",
"prettier": "^3.2.5",
"resend": "^3.2.0",
"typescript": "^5.0.0"
},
"peerDependencies": {
"react-dom": "^18.2.0"
}
}
76 changes: 38 additions & 38 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,42 @@
// }

{
"compilerOptions": {
/* Base Options: */
"esModuleInterop": true,
"skipLibCheck": true,
"target": "es2022",
"allowJs": true,
"resolveJsonModule": true,
"moduleDetection": "force",
"isolatedModules": true,

/* Strictness */
"strict": true,
"noUncheckedIndexedAccess": true,

/* If transpiling with TypeScript: */
"moduleResolution": "NodeNext",
"module": "NodeNext",
"outDir": "dist",
"sourceMap": true,

/* AND if you're building for a library: */
"declaration": true,

/* AND if you're building for a library in a monorepo: */
// "composite": true,
// "declarationMap": true,

/* If NOT transpiling with TypeScript: */
// "moduleResolution": "Bundler",
// "module": "ESNext",
// "noEmit": true,

/* If your code runs in the DOM: */
// "lib": ["es2022", "dom", "dom.iterable"],

/* If your code doesn't run in the DOM: */
"lib": ["es2022"]
}
"compilerOptions": {
/* Base Options: */
"esModuleInterop": true,
"skipLibCheck": true,
"target": "es2022",
"allowJs": true,
"resolveJsonModule": true,
"moduleDetection": "force",
"isolatedModules": true,

/* Strictness */
"strict": true,
"noUncheckedIndexedAccess": true,

/* If transpiling with TypeScript: */
"moduleResolution": "NodeNext",
"module": "NodeNext",
"outDir": "dist",
"sourceMap": true,

/* AND if you're building for a library: */
"declaration": true,

/* AND if you're building for a library in a monorepo: */
// "composite": true,
// "declarationMap": true,

/* If NOT transpiling with TypeScript: */
// "moduleResolution": "Bundler",
// "module": "ESNext",
// "noEmit": true,

/* If your code runs in the DOM: */
// "lib": ["es2022", "dom", "dom.iterable"],

/* If your code doesn't run in the DOM: */
"lib": ["es2022"]
}
}
4 changes: 2 additions & 2 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export type ResendProvider = {
};

export type PostmarkProvider = {
type: "postmark";
postmark: ServerClient;
type: "postmark";
postmark: ServerClient;
};

export type Provider = CustomProvider | ResendProvider | PostmarkProvider;

0 comments on commit 4a2741c

Please sign in to comment.