-
Notifications
You must be signed in to change notification settings - Fork 81
refactor: resend client options #865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Shubhdeep12
wants to merge
5
commits into
resend:canary
Choose a base branch
from
Shubhdeep12:feat/resend-options
base: canary
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+98
−24
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
28bfd9f
feat: enhance Resend client with options for base URL and user agent
Shubhdeep12 b8bbf0f
fix comment
Shubhdeep12 8954927
fix: update logical OR usage for base URL and user agent in Resend cl…
Shubhdeep12 38ba8fc
empty value in options can be explicitly passed by user
Shubhdeep12 d3d6ccf
Merge branch 'canary' into feat/resend-options
Shubhdeep12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -17,17 +17,25 @@ import { Webhooks } from './webhooks/webhooks'; | |
|
|
||
| const defaultBaseUrl = 'https://api.resend.com'; | ||
| const defaultUserAgent = `resend-node:${version}`; | ||
| const baseUrl = | ||
| typeof process !== 'undefined' && process.env | ||
| ? process.env.RESEND_BASE_URL || defaultBaseUrl | ||
| : defaultBaseUrl; | ||
| const userAgent = | ||
| typeof process !== 'undefined' && process.env | ||
| ? process.env.RESEND_USER_AGENT || defaultUserAgent | ||
| : defaultUserAgent; | ||
|
|
||
| /** optional options when creating the client instead of env variables */ | ||
| export type ResendClientOptions = { | ||
| baseUrl?: string; | ||
| userAgent?: string; | ||
| /** custom fetch can be used for tests. */ | ||
| fetch?: typeof fetch; | ||
| }; | ||
|
|
||
| function getEnv(name: string): string | undefined { | ||
| if (typeof process === 'undefined' || !process.env) return undefined; | ||
| return process.env[name]; | ||
| } | ||
|
|
||
| export class Resend { | ||
| private readonly headers: Headers; | ||
| private readonly baseUrl: string; | ||
| private readonly fetchImpl: typeof fetch; | ||
| private readonly _key: string; | ||
|
|
||
| readonly apiKeys = new ApiKeys(this); | ||
| readonly segments = new Segments(this); | ||
|
|
@@ -45,29 +53,35 @@ export class Resend { | |
| readonly templates = new Templates(this); | ||
| readonly topics = new Topics(this); | ||
|
|
||
| constructor(readonly key?: string) { | ||
| if (!key) { | ||
| if (typeof process !== 'undefined' && process.env) { | ||
| this.key = process.env.RESEND_API_KEY; | ||
| } | ||
|
|
||
| if (!this.key) { | ||
| throw new Error( | ||
| 'Missing API key. Pass it to the constructor `new Resend("re_123")`', | ||
| ); | ||
| } | ||
| constructor(key?: string, options?: ResendClientOptions) { | ||
| const apiKey = key ?? getEnv('RESEND_API_KEY'); | ||
| if (!apiKey) { | ||
| throw new Error( | ||
| 'Missing API key. Pass it to the constructor `new Resend("re_123")` or set RESEND_API_KEY.', | ||
| ); | ||
| } | ||
| this._key = apiKey; | ||
|
|
||
| this.baseUrl = | ||
| options?.baseUrl ?? (getEnv('RESEND_BASE_URL') || defaultBaseUrl); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Using Prompt for AI agents |
||
| const userAgent = | ||
| options?.userAgent ?? (getEnv('RESEND_USER_AGENT') || defaultUserAgent); | ||
| this.fetchImpl = options?.fetch ?? fetch; | ||
|
|
||
| this.headers = new Headers({ | ||
| Authorization: `Bearer ${this.key}`, | ||
| Authorization: `Bearer ${apiKey}`, | ||
| 'User-Agent': userAgent, | ||
| 'Content-Type': 'application/json', | ||
| }); | ||
| } | ||
|
|
||
| get key(): string { | ||
| return this._key; | ||
| } | ||
|
|
||
| async fetchRequest<T>(path: string, options = {}): Promise<Response<T>> { | ||
| try { | ||
| const response = await fetch(`${baseUrl}${path}`, options); | ||
| const response = await this.fetchImpl(`${this.baseUrl}${path}`, options); | ||
|
|
||
| if (!response.ok) { | ||
| try { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.