-
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.
Add global headers to the client (#218)
* feat: add global headers to the client * chore: add tests for global headers * fix: move headers logic to inside of the methods, add tests * fix: headers type conflict * fix: add global header to schedule and enqueue * fix: type error --------- Co-authored-by: CahidArda <[email protected]>
- Loading branch information
Showing
8 changed files
with
385 additions
and
25 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 |
---|---|---|
|
@@ -7,10 +7,24 @@ import { nanoid } from "../utils"; | |
describe("email", () => { | ||
const qstashToken = nanoid(); | ||
const resendToken = nanoid(); | ||
const client = new Client({ baseUrl: MOCK_QSTASH_SERVER_URL, token: qstashToken }); | ||
|
||
const header = "my-header"; | ||
const headerValue = "my-header-value"; | ||
const globalHeader = "global-header"; | ||
const globalHeaderOverwritten = "global-header-overwritten"; | ||
const requestHeader = "request-header"; | ||
|
||
const globalHeaderValue = nanoid(); | ||
const overWrittenOldValue = nanoid(); | ||
const overWrittenNewValue = nanoid(); | ||
const requestHeaderValue = nanoid(); | ||
|
||
const client = new Client({ | ||
baseUrl: MOCK_QSTASH_SERVER_URL, | ||
token: qstashToken, | ||
headers: { | ||
[globalHeader]: globalHeaderValue, | ||
[globalHeaderOverwritten]: overWrittenOldValue, | ||
}, | ||
}); | ||
|
||
test("should use resend", async () => { | ||
await mockQStashServer({ | ||
|
@@ -20,15 +34,17 @@ describe("email", () => { | |
name: "email", | ||
provider: resend({ token: resendToken }), | ||
}, | ||
headers: { | ||
[header]: headerValue, | ||
}, | ||
body: { | ||
from: "Acme <[email protected]>", | ||
to: ["[email protected]"], | ||
subject: "hello world", | ||
html: "<p>it works!</p>", | ||
}, | ||
headers: { | ||
"content-type": "application/json", | ||
[globalHeaderOverwritten]: overWrittenNewValue, | ||
[requestHeader]: requestHeaderValue, | ||
}, | ||
}); | ||
}, | ||
responseFields: { | ||
|
@@ -46,10 +62,12 @@ describe("email", () => { | |
html: "<p>it works!</p>", | ||
}, | ||
headers: { | ||
"content-type": "application/json", | ||
authorization: `Bearer ${qstashToken}`, | ||
"upstash-forward-authorization": `Bearer ${resendToken}`, | ||
"content-type": "application/json", | ||
[`upstash-forward-${header}`]: headerValue, | ||
[`upstash-forward-${requestHeader}`]: requestHeaderValue, | ||
[`upstash-forward-${globalHeader}`]: globalHeaderValue, | ||
[`upstash-forward-${globalHeaderOverwritten}`]: overWrittenNewValue, | ||
"upstash-method": "POST", | ||
}, | ||
}, | ||
|
@@ -64,9 +82,6 @@ describe("email", () => { | |
name: "email", | ||
provider: resend({ token: resendToken, batch: true }), | ||
}, | ||
headers: { | ||
[header]: headerValue, | ||
}, | ||
body: [ | ||
{ | ||
from: "Acme <[email protected]>", | ||
|
@@ -81,6 +96,11 @@ describe("email", () => { | |
html: "<p>it works!</p>", | ||
}, | ||
], | ||
headers: { | ||
"content-type": "application/json", | ||
[globalHeaderOverwritten]: overWrittenNewValue, | ||
[requestHeader]: requestHeaderValue, | ||
}, | ||
}); | ||
}, | ||
responseFields: { | ||
|
@@ -108,9 +128,11 @@ describe("email", () => { | |
headers: { | ||
authorization: `Bearer ${qstashToken}`, | ||
"upstash-forward-authorization": `Bearer ${resendToken}`, | ||
"content-type": "application/json", | ||
[`upstash-forward-${header}`]: headerValue, | ||
"upstash-method": "POST", | ||
"content-type": "application/json", | ||
[`upstash-forward-${requestHeader}`]: requestHeaderValue, | ||
[`upstash-forward-${globalHeader}`]: globalHeaderValue, | ||
[`upstash-forward-${globalHeaderOverwritten}`]: overWrittenNewValue, | ||
}, | ||
}, | ||
}); | ||
|
@@ -124,9 +146,6 @@ describe("email", () => { | |
name: "email", | ||
provider: resend({ token: resendToken, batch: true }), | ||
}, | ||
headers: { | ||
[header]: headerValue, | ||
}, | ||
method: "PUT", | ||
body: [ | ||
{ | ||
|
@@ -170,10 +189,75 @@ describe("email", () => { | |
authorization: `Bearer ${qstashToken}`, | ||
"upstash-forward-authorization": `Bearer ${resendToken}`, | ||
"content-type": "application/json", | ||
[`upstash-forward-${header}`]: headerValue, | ||
"upstash-method": "PUT", | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
test("should be able to enqueue", async () => { | ||
const queueName = "resend-queue"; | ||
const queue = client.queue({ queueName }); | ||
await mockQStashServer({ | ||
execute: async () => { | ||
await queue.enqueueJSON({ | ||
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>", | ||
}, | ||
], | ||
headers: { | ||
"content-type": "application/json", | ||
[globalHeaderOverwritten]: overWrittenNewValue, | ||
[requestHeader]: requestHeaderValue, | ||
}, | ||
}); | ||
}, | ||
responseFields: { | ||
body: { messageId: "msgId" }, | ||
status: 200, | ||
}, | ||
receivesRequest: { | ||
method: "POST", | ||
token: qstashToken, | ||
url: "http://localhost:8080/v2/enqueue/resend-queue/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": `Bearer ${resendToken}`, | ||
"content-type": "application/json", | ||
"upstash-method": "POST", | ||
[`upstash-forward-${requestHeader}`]: requestHeaderValue, | ||
[`upstash-forward-${globalHeader}`]: globalHeaderValue, | ||
[`upstash-forward-${globalHeaderOverwritten}`]: overWrittenNewValue, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.