idempotency (#282)
This commit is contained in:
@@ -67,16 +67,23 @@ type BatchEmailResponse = {
|
||||
error: ErrorResponse | null;
|
||||
};
|
||||
|
||||
type EmailRequestOptions = {
|
||||
idempotencyKey?: string;
|
||||
};
|
||||
|
||||
export class Emails {
|
||||
constructor(private readonly usesend: UseSend) {
|
||||
this.usesend = usesend;
|
||||
}
|
||||
|
||||
async send(payload: SendEmailPayload) {
|
||||
return this.create(payload);
|
||||
async send(payload: SendEmailPayload, options?: EmailRequestOptions) {
|
||||
return this.create(payload, options);
|
||||
}
|
||||
|
||||
async create(payload: SendEmailPayload): Promise<CreateEmailResponse> {
|
||||
async create(
|
||||
payload: SendEmailPayload,
|
||||
options?: EmailRequestOptions,
|
||||
): Promise<CreateEmailResponse> {
|
||||
if (payload.react) {
|
||||
payload.html = await render(payload.react as React.ReactElement);
|
||||
delete payload.react;
|
||||
@@ -84,7 +91,10 @@ export class Emails {
|
||||
|
||||
const data = await this.usesend.post<CreateEmailResponseSuccess>(
|
||||
"/emails",
|
||||
payload
|
||||
payload,
|
||||
options?.idempotencyKey
|
||||
? { headers: { "Idempotency-Key": options.idempotencyKey } }
|
||||
: undefined,
|
||||
);
|
||||
|
||||
return data;
|
||||
@@ -96,11 +106,17 @@ export class Emails {
|
||||
* @param payload An array of email payloads. Max 100 emails.
|
||||
* @returns A promise that resolves to the list of created email IDs or an error.
|
||||
*/
|
||||
async batch(payload: BatchEmailPayload): Promise<BatchEmailResponse> {
|
||||
async batch(
|
||||
payload: BatchEmailPayload,
|
||||
options?: EmailRequestOptions,
|
||||
): Promise<BatchEmailResponse> {
|
||||
// Note: React element rendering is not supported in batch mode.
|
||||
const response = await this.usesend.post<BatchEmailResponseSuccess>(
|
||||
"/emails/batch",
|
||||
payload
|
||||
payload,
|
||||
options?.idempotencyKey
|
||||
? { headers: { "Idempotency-Key": options.idempotencyKey } }
|
||||
: undefined,
|
||||
);
|
||||
return {
|
||||
data: response.data ? response.data.data : null,
|
||||
|
||||
Reference in New Issue
Block a user