-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add resend api * fix: mock resend in email test * fix: tests * fix: add appendAPIOptions to enqueue and batch * fix: add batch emails
- Loading branch information
Showing
9 changed files
with
187 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { describe, test } from "bun:test"; | ||
import { Client } from "../client"; | ||
import { resend } from "./email"; | ||
import { MOCK_QSTASH_SERVER_URL, mockQStashServer } from "../workflow/test-utils"; | ||
import { nanoid } from "../utils"; | ||
|
||
describe("email", () => { | ||
const qstashToken = nanoid(); | ||
const resendToken = nanoid(); | ||
const client = new Client({ baseUrl: MOCK_QSTASH_SERVER_URL, token: qstashToken }); | ||
|
||
test("should use resend", async () => { | ||
await mockQStashServer({ | ||
execute: async () => { | ||
await client.publishJSON({ | ||
api: { | ||
name: "email", | ||
provider: resend({ token: resendToken }), | ||
}, | ||
body: { | ||
from: "Acme <[email protected]>", | ||
to: ["[email protected]"], | ||
subject: "hello world", | ||
html: "<p>it works!</p>", | ||
}, | ||
}); | ||
}, | ||
responseFields: { | ||
body: { messageId: "msgId" }, | ||
status: 200, | ||
}, | ||
receivesRequest: { | ||
method: "POST", | ||
token: qstashToken, | ||
url: "http://localhost:8080/v2/publish/https://api.resend.com/emails", | ||
body: { | ||
from: "Acme <[email protected]>", | ||
to: ["[email protected]"], | ||
subject: "hello world", | ||
html: "<p>it works!</p>", | ||
}, | ||
headers: { | ||
authorization: `Bearer ${qstashToken}`, | ||
"upstash-forward-authorization": resendToken, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
test("should use resend with batch", async () => { | ||
await mockQStashServer({ | ||
execute: async () => { | ||
await client.publishJSON({ | ||
api: { | ||
name: "email", | ||
provider: resend({ token: resendToken, batch: true }), | ||
}, | ||
body: [ | ||
{ | ||
from: "Acme <[email protected]>", | ||
to: ["[email protected]"], | ||
subject: "hello world", | ||
html: "<h1>it works!</h1>", | ||
}, | ||
{ | ||
from: "Acme <[email protected]>", | ||
to: ["[email protected]"], | ||
subject: "world hello", | ||
html: "<p>it works!</p>", | ||
}, | ||
], | ||
}); | ||
}, | ||
responseFields: { | ||
body: { messageId: "msgId" }, | ||
status: 200, | ||
}, | ||
receivesRequest: { | ||
method: "POST", | ||
token: qstashToken, | ||
url: "http://localhost:8080/v2/publish/https://api.resend.com/emails/batch", | ||
body: [ | ||
{ | ||
from: "Acme <[email protected]>", | ||
to: ["[email protected]"], | ||
subject: "hello world", | ||
html: "<h1>it works!</h1>", | ||
}, | ||
{ | ||
from: "Acme <[email protected]>", | ||
to: ["[email protected]"], | ||
subject: "world hello", | ||
html: "<p>it works!</p>", | ||
}, | ||
], | ||
headers: { | ||
authorization: `Bearer ${qstashToken}`, | ||
"upstash-forward-authorization": resendToken, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export type EmailProviderReturnType = { | ||
owner: "resend"; | ||
baseUrl: "https://api.resend.com/emails" | "https://api.resend.com/emails/batch"; | ||
token: string; | ||
}; | ||
|
||
export const resend = ({ | ||
token, | ||
batch = false, | ||
}: { | ||
token: string; | ||
batch?: boolean; | ||
}): EmailProviderReturnType => { | ||
return { | ||
owner: "resend", | ||
baseUrl: `https://api.resend.com/emails${batch ? "/batch" : ""}`, | ||
token, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { resend } from "./email"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { PublishRequest } from "../client"; | ||
|
||
export const appendAPIOptions = (request: PublishRequest<unknown>, headers: Headers) => { | ||
if (request.api?.name === "email") { | ||
headers.set("Authorization", request.api.provider.token); | ||
request.method = request.method ?? "POST"; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters