@platformatic/kafka
Version:
Modern and performant client for Apache Kafka
37 lines (36 loc) • 1.48 kB
JavaScript
import { AuthenticationError } from "../../errors.js";
export function getCredential(label, credentialOrProvider, callback) {
if (typeof credentialOrProvider === 'string') {
callback(null, credentialOrProvider);
return;
}
else if (typeof credentialOrProvider !== 'function') {
callback(new AuthenticationError(`The ${label} should be a string or a function.`), undefined);
return;
}
try {
const credential = credentialOrProvider();
if (typeof credential === 'string') {
callback(null, credential);
return;
}
else if (typeof credential?.then !== 'function') {
callback(new AuthenticationError(`The ${label} provider should return a string or a promise that resolves to a string.`), undefined);
return;
}
credential
.then(token => {
if (typeof token !== 'string') {
process.nextTick(callback, new AuthenticationError(`The ${label} provider should resolve to a string.`), undefined);
return;
}
process.nextTick(callback, null, token);
})
.catch(error => {
process.nextTick(callback, new AuthenticationError(`The ${label} provider threw an error.`, { cause: error }));
});
}
catch (error) {
callback(new AuthenticationError(`The ${label} provider threw an error.`, { cause: error }), undefined);
}
}