@kurrent/kurrentdb-client
Version:
KurrentDB gRPC NodeJS Client SDK
31 lines • 1.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toAuthorizationHeader = exports.isBearerCredentials = exports.isBasicCredentials = void 0;
/**
* Type predicate narrowing {@link Credentials} to {@link BasicCredentials}
* (username/password). Prefer this over inline `"username" in credentials`
* checks so the discrimination lives in one place.
*/
const isBasicCredentials = (credentials) => credentials !== undefined && "username" in credentials;
exports.isBasicCredentials = isBasicCredentials;
/**
* Type predicate narrowing {@link Credentials} to {@link BearerCredentials}.
*/
const isBearerCredentials = (credentials) => credentials !== undefined && "bearerToken" in credentials;
exports.isBearerCredentials = isBearerCredentials;
/**
* Render a {@link Credentials} value as the verbatim value of an
* `Authorization` HTTP header (`"Basic ..."` or `"Bearer ..."`).
*
* Shared between the gRPC metadata generator and the HTTP fallback so the two
* paths stay in lockstep.
*/
const toAuthorizationHeader = (credentials) => {
if ((0, exports.isBearerCredentials)(credentials)) {
return `Bearer ${credentials.bearerToken}`;
}
const auth = Buffer.from(`${credentials.username}:${credentials.password}`).toString("base64");
return `Basic ${auth}`;
};
exports.toAuthorizationHeader = toAuthorizationHeader;
//# sourceMappingURL=credentials.js.map