sendgrid-nodejs icon indicating copy to clipboard operation
sendgrid-nodejs copied to clipboard

Email Client should be enhanced for instances specific to it's key

Open aimythgit opened this issue 4 years ago • 6 comments

Issue Summary

The email client should allow instances of client as opposed to a singleton to facilitate multiple keys Almost every SaaS provider client has instances and this client too should provide an option while the current simpler one can be used by those who do not have this use case. The whole purpose of a client is for the statefulness else the corresponding stateless REST API can be used directly without the clients.

Code Snippet

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);

paste code here

const SgMailClient = require('@sendgrid/mailClient');

const client1 = new SgMailClient();
client1.setApiKey(process.env.SENDGRIDClient1_API_KEY);

const client2 = new SgMailClient();
client2.setApiKey(process.env.SENDGRIDClient2_API_KEY);

// logic for using client 1 or client 2 to send mail

aimythgit avatar Jul 04 '21 22:07 aimythgit

Hi @aimythgit this should help. Let us know if you run into issues doing that.

shwetha-manvinkurke avatar Jul 12 '21 20:07 shwetha-manvinkurke

I saw this but didn't see a signature document for the send method.

const sgClient1 = new Client(); const sgClient2 = new Client(); sgClient1.setApiKey('KEY1'); sgClient2.setApiKey('KEY2');

// What should be this method. As it can't be send as in the client context this should be sendMail... sgClient1.send(msg)

or const mailClient1 = sgClient1.getMailClient(); mailClient1.send(msg);

Besides the client is for all functionality of sendgrid, would prefer only for emailclient if possible.

aimythgit avatar Jul 20 '21 00:07 aimythgit

Hello @aimythgit,

I believe you should use sgClient1.send(msg) as demonstrated here. This package is the closest thing we have to an email only helper library.

With best regards,

Elmer

thinkingserious avatar Jul 20 '21 23:07 thinkingserious

Elmer, Thanks for the clarification but the current sendMail package is a singleton, that's why I have asked for this feature.

Either expose the mail client from this package that support instances @sendgrid/client or allow multiple instance from the @sendgrid/mailClient package

aimythgit avatar Jul 21 '21 02:07 aimythgit

This issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.

eshanholtz avatar Jul 21 '21 23:07 eshanholtz

While the default export of @sendgrid/mail is indeed a singleton, you can also use the class to create multiple instances like so (though the documentation does not mention it, it works perfectly):

const { MailService } = require('@sendgrid/mail');

const service1 = new MailService();
service1.setApiKey('API_KEY1');

const service2 = new MailService();
service2.setApiKey('API_KEY2');

// alternatively you can also pass in the client (so you can reuse it for the other non-mail apis)
const { Client } = require('@sendgrid/client');

const client3 = new Client();
client3.setApiKey('API_KEY3');

const service3 = new MailService();
service3.setClient(client3);

await service3.send({...});
await client3.request({...}); // for non-mail related apis

danmana avatar Feb 23 '22 09:02 danmana