@api-buddy/sendgrid
Version:
API Buddy integration for SendGrid - Email delivery service for transactional and marketing emails
85 lines • 2.43 kB
JavaScript
'use client';
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useSendEmail = useSendEmail;
exports.useSendMultipleEmails = useSendMultipleEmails;
const react_1 = require("react");
const client_1 = require("../client");
/**
* Hook for sending emails with SendGrid
* @returns Object containing the send function, loading state, and error state
*/
function useSendEmail() {
const [isLoading, setIsLoading] = (0, react_1.useState)(false);
const [error, setError] = (0, react_1.useState)(null);
const [response, setResponse] = (0, react_1.useState)(null);
/**
* Send an email using SendGrid
* @param options Email options
* @returns Promise with the SendGrid response
*/
const send = (0, react_1.useCallback)(async (options) => {
setIsLoading(true);
setError(null);
try {
const result = await (0, client_1.sendEmail)(options);
setResponse(result);
return result;
}
catch (err) {
setError(err);
throw err;
}
finally {
setIsLoading(false);
}
}, []);
const reset = (0, react_1.useCallback)(() => {
setError(null);
setResponse(null);
}, []);
return {
send,
isLoading,
error,
response,
reset,
};
}
/**
* Hook for sending multiple emails with SendGrid
* @returns Object containing the send function, loading state, and error state
*/
function useSendMultipleEmails() {
const [isLoading, setIsLoading] = (0, react_1.useState)(false);
const [error, setError] = (0, react_1.useState)(null);
const [response, setResponse] = (0, react_1.useState)(null);
const send = (0, react_1.useCallback)(async (messages) => {
setIsLoading(true);
setError(null);
try {
const result = await (0, client_1.sendMultipleEmails)(messages);
setResponse(result);
return result;
}
catch (err) {
setError(err);
throw err;
}
finally {
setIsLoading(false);
}
}, []);
const reset = (0, react_1.useCallback)(() => {
setError(null);
setResponse(null);
}, []);
return {
send,
isLoading,
error,
response,
reset,
};
}
//# sourceMappingURL=useSendEmail.js.map