UNPKG

@api-buddy/sendgrid

Version:

API Buddy integration for SendGrid - Email delivery service for transactional and marketing emails

48 lines 2.01 kB
'use client'; "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SendGridProvider = SendGridProvider; exports.useSendGrid = useSendGrid; exports.withSendGrid = withSendGrid; const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const client_1 = require("../client"); const SendGridContext = (0, react_1.createContext)(undefined); /** * Provider component that makes the SendGrid client available to any nested components */ function SendGridProvider({ children, config }) { const client = (0, react_1.useMemo)(() => new client_1.SendGridClient(config), [config]); const value = (0, react_1.useMemo)(() => ({ client, config, }), [client, config]); return ((0, jsx_runtime_1.jsx)(SendGridContext.Provider, { value: value, children: children })); } /** * Hook to access the SendGrid client and config from the nearest SendGridProvider * @returns Object containing the SendGrid client and config * @throws Error if used outside of a SendGridProvider */ function useSendGrid() { const context = (0, react_1.useContext)(SendGridContext); if (context === undefined) { throw new Error('useSendGrid must be used within a SendGridProvider'); } return context; } /** * Higher-order component that provides the SendGrid client to the wrapped component * @param WrappedComponent The component to wrap with SendGrid context * @returns A new component with SendGrid context */ function withSendGrid(WrappedComponent) { const displayName = WrappedComponent.displayName || WrappedComponent.name || 'Component'; const ComponentWithSendGrid = (props) => { const { client, config } = useSendGrid(); return ((0, jsx_runtime_1.jsx)(WrappedComponent, { ...props, sendGridClient: client, sendGridConfig: config })); }; ComponentWithSendGrid.displayName = `withSendGrid(${displayName})`; return ComponentWithSendGrid; } //# sourceMappingURL=SendGridProvider.js.map