-
Notifications
You must be signed in to change notification settings - Fork 34
feat: add custom return path flag and CID/URL attachment syntax #352
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
Merged
Merged
Changes from all commits
Commits
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
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 |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import type { Attachment } from 'resend'; | ||
|
|
||
| export interface AttachmentSpec { | ||
| source: string; | ||
| isUrl: boolean; | ||
| filename?: string; | ||
| contentType?: string; | ||
| contentId?: string; | ||
| } | ||
|
|
||
| // Split only on recognized ";key=" tokens so paths and MIME parameters | ||
| // (e.g. "type=text/plain;charset=utf-8") containing ";" or "=" still parse. | ||
| const PARAM_SPLIT = /;(cid|type|filename)=/; | ||
| const PARAM_LIKE = /;[\w-]+=/; | ||
|
|
||
| const SPEC_FIELDS = { | ||
| cid: 'contentId', | ||
| type: 'contentType', | ||
| filename: 'filename', | ||
| } as const; | ||
|
|
||
| export function parseAttachmentSpec(value: string): AttachmentSpec { | ||
| const segments = value.split(PARAM_SPLIT); | ||
| const source = segments[0]; | ||
| if (!source) { | ||
| throw new Error(`Missing file path or URL in attachment "${value}".`); | ||
| } | ||
| if (PARAM_LIKE.test(source)) { | ||
| throw new Error( | ||
| `Unrecognized attachment parameter in "${value}". Supported: ;cid=, ;type=, ;filename= (use --attachments-file for paths containing ";key=").`, | ||
| ); | ||
| } | ||
| const spec: AttachmentSpec = { | ||
| source, | ||
| isUrl: /^https?:\/\//i.test(source), | ||
| }; | ||
| for (let i = 1; i < segments.length; i += 2) { | ||
| const key = segments[i] as keyof typeof SPEC_FIELDS; | ||
| const paramValue = segments[i + 1]; | ||
| const field = SPEC_FIELDS[key]; | ||
| if (spec[field] !== undefined) { | ||
| throw new Error(`Duplicate ";${key}=" in attachment "${value}".`); | ||
| } | ||
| if (!paramValue) { | ||
| throw new Error(`Empty ";${key}=" in attachment "${value}".`); | ||
| } | ||
| // MIME parameters like "type=text/plain;charset=utf-8" are legitimate, | ||
| // but a ";key=" inside a cid or filename value is a typo. | ||
| if (field !== 'contentType' && PARAM_LIKE.test(paramValue)) { | ||
| throw new Error( | ||
| `Unrecognized attachment parameter in "${value}". Supported: ;cid=, ;type=, ;filename= (use --attachments-file for paths containing ";key=").`, | ||
| ); | ||
| } | ||
| spec[field] = paramValue; | ||
| } | ||
| return spec; | ||
| } | ||
|
|
||
| const FIELD_ALIASES: Record<string, string> = { | ||
| content_type: 'contentType', | ||
| content_id: 'contentId', | ||
| }; | ||
|
|
||
| const ALLOWED_FIELDS = new Set([ | ||
| 'content', | ||
| 'filename', | ||
| 'path', | ||
| 'contentType', | ||
| 'contentId', | ||
| ]); | ||
|
|
||
| export function parseAttachmentsJson(raw: string): Attachment[] { | ||
| let parsed: unknown; | ||
| try { | ||
| parsed = JSON.parse(raw); | ||
| } catch { | ||
| throw new Error('Attachments file is not valid JSON.'); | ||
| } | ||
| if (!Array.isArray(parsed)) { | ||
| throw new Error( | ||
| 'Attachments file must contain a JSON array of attachment objects.', | ||
| ); | ||
| } | ||
| return parsed.map((item, i) => { | ||
| if (item === null || typeof item !== 'object' || Array.isArray(item)) { | ||
| throw new Error(`Attachment at index ${i} must be a JSON object.`); | ||
| } | ||
| const attachment: Record<string, string> = {}; | ||
| for (const [rawKey, fieldValue] of Object.entries(item)) { | ||
| const key = FIELD_ALIASES[rawKey] ?? rawKey; | ||
| if (!ALLOWED_FIELDS.has(key)) { | ||
| throw new Error( | ||
| `Attachment at index ${i} has unsupported field "${rawKey}". Supported: content, filename, path, content_type/contentType, content_id/contentId.`, | ||
| ); | ||
| } | ||
| if (key in attachment) { | ||
| throw new Error( | ||
| `Attachment at index ${i} sets "${key}" more than once (snake_case and camelCase are aliases).`, | ||
| ); | ||
| } | ||
| if (typeof fieldValue !== 'string') { | ||
| throw new Error( | ||
| `Attachment at index ${i}: "${rawKey}" must be a string.`, | ||
| ); | ||
| } | ||
| attachment[key] = fieldValue; | ||
| } | ||
| if (!attachment.content && !attachment.path) { | ||
| throw new Error( | ||
| `Attachment at index ${i} must include "content" (base64) or "path" (hosted URL).`, | ||
| ); | ||
| } | ||
| return attachment as Attachment; | ||
| }); | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Unknown suffixes following a valid suffix are accepted as part of the preceding value, so
--attachment "a.png;cid=logo;foo=bar"does not returninvalid_attachment. Validate parameter-like tokens in every suffix value (while preserving MIME parameters in;type=) before constructing metadata.Prompt for AI agents