Issue running fetch-plus-basicauth
I am getting an error when using the fetch-plus-basicauth module.
I create the import:
import plusBasicAuth from "fetch-plus-basicauth";
then I run:
endpoint.addMiddleware(plusBasicAuth("user", 'API_KEY'));
I get the error:
TypeError: btoa() function required but not available
I have tried installing and importing the npm btoa module (https://www.npmjs.com/package/btoa) but this does not seem to resolve the issue.
You can either use global.btoa = require("btoa") to polyfill the function, or pass it as third parameter to the middleware like plusBasicAuth("user", "API_KEY", require("btoa")).
That works great Rick, Many Thanks for your help.
Unfortunately I now seem to get an issue when posting data to the endpoint. When adding an email address to the body option this does not get passed with the request. I have tried using the request method to post but this does not seem to exist when inspecting the fetch plus object returned.
Can you share some code?
Of course.
const sendNewsletterSubscription = (data) => { return mailchimp.browse( ["3.0", "lists", config.mailchimp.id, "members", ""], { method: "POST", body: { EMAIL: data.EMAIL }, query: { "u": config.mailchimp.u, "id": config.mailchimp.id } } ).then(json => { return json; }).catch(error => { console.log(error); throw error; });` };
As you can see I am using the browse method but I have the same issue when using read aswell. When I attempt to add the email to the request object then the @ is escaped which means that the email address is escaped with %2540 when it is passed to the server.
Please find the middleware created for this to connect client to server below:
`import fetch from "isomorphic-fetch"; import fetchPlus from "fetch-plus"; import plusJson from "fetch-plus-json"; import plusBearerauth from "fetch-plus-bearerauth"; import plusBasicAuth from "fetch-plus-basicauth";
const config = require("config/config");
const mailchimpService = () => {
if (__SERVER__) {
const host = `${config.mailchimp.dc}.api.mailchimp.com`;
return `http://${host}`;
}
if (__CLIENT__) {
const {protocol, hostname, port} = window.location;
return `${protocol}//${hostname}:${port}/api/mailchimp`;
}
};
const endpoint = fetchPlus.connectEndpoint(mailchimpService());
endpoint.addMiddleware(plusBasicAuth("user", ${config.mailchimp.apiKey}, require("btoa")));
//endpoint.addMiddleware(plusJson());
module.exports = endpoint;`