ElasticEmail / ElasticEmail.WebApiClient-js

Deprecated library. Easily send emails with Elastic Email using Web API JS Client https://elasticemail.com/
MIT License
15 stars 9 forks source link

Updating to this version broke my server (webapiclient v 2.0.12, node v8) #9

Open zhamid-src opened 4 years ago

zhamid-src commented 4 years ago

This call that was working perfectly fine, now fails.

const EEOptions = {
  apiKey: 'BA05CB368C38B35F6F92D6573EA12D5343F465D18656DD7C80C4F27A708D1DF4BDAF7974F20DCDA80F830C3D8BE86CEA',
  apiUri: 'https://api.elasticemail.com/',
  apiVersion: 'v2'
};

//************** */
// ElasticEmail Account APIs
//************** */
export async function eeAccountAddSubaccount(email: string) {
  const data = {
    email: email,
    password: 'placeHolderPassw0rd',
    confirmPassword: 'placeHolderPassw0rd'
  };

  const eeClient = require('elasticemail-webapiclient').client;

  const EE = new eeClient(EEOptions);

  // ***** -> the following fails with 'undefined'
  return await EE.Account.AddSubAccount(data).catch((err: any) => console.error('error from elasticEmail: ', err));
}

The call fails with an undefined error. For the same reason, EE.Account.GetSubAccountApiKey fails as well, which pretty much prevents me from doing any operation.

This is a blocking isue.

zhamid-src commented 4 years ago

FYI, I posted a simpler version. "return await" was there because this was initially wrapped in a try / catch block. I simplified the example to create the minimum code that reproduces the problem.

zhamid-src commented 4 years ago

I changed the code to manually making the GET call for testing :-

export async function eeAccountAddSubaccount(email: string) {

  const url = `${EEOptions.apiUri}${EEOptions.apiVersion}/account/addsubaccount?apiKey=${EEOptions.apiKey}&email=${email}&password=placeHolderPassw0rd&confirmPassword=placeHolderPassw0rd`;
  const headers = {
    'Content-Type': 'application/x-www-form-urlencoded'
  };

  return axios({
    method: 'GET',
    url: url,
    headers: headers
  });

This works. This indicates that the problem is the new JavaScript API and not my code.

ElasticEmail commented 4 years ago

@zhamid-src no, your code is using GET: We just tested the call with the following example:

               const options = {
                    apiKey: "secretapikey",
                    apiUri: 'https://api.elasticemail.com/',
                    apiVersion: 'v2'
                }
                const EE = new EEAPI.client(options);
                EE.Account.Load().then(function(resp) {
                    console.log(resp);
                })
                .catch((err) => {
                    console.log(err)
                });

                async function addSubAccount(params) {
                    return await EE.Account.AddSubAccount(params).catch((err) => console.error('error from elasticEmail: ', err));
                }

                const params = {
                    email: 'yourtestemail1@gmail.com',
                    password: 'foo123',
                    confirmPassword: 'foo123'
                }

                console.log(addSubAccount(params).then(resp => {console.log(resp)}));

and this works perfectly fine

zhamid-src commented 4 years ago

GET call worked (POST works too) because I did it manually. Depending on the parameters, the EE.Account.AddSubAccount call can fail when same parameters pass with me calling axios directly. That leads me to believe that it has to do with the way your API is creating form data, which shouldn't be necessary because axios handles it.