@datastax/astra-db-ts
Version:
Data API TypeScript client
64 lines (63 loc) • 2.91 kB
JavaScript
;
// Copyright Datastax, Inc
// SPDX-License-Identifier: Apache-2.0
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoggingCfgHandler = void 0;
const opts_handlers_js_1 = require("../../lib/opts-handlers.js");
const decoders_1 = require("decoders");
const constants_js_1 = require("../../lib/logging/constants.js");
const utils_js_1 = require("../../lib/utils.js");
const monoid = opts_handlers_js_1.monoids.object({
layers: opts_handlers_js_1.monoids.array(),
});
const oneOfLoggingEvents = (0, decoders_1.either)((0, decoders_1.oneOf)(constants_js_1.LoggingEventsWithAll), (0, decoders_1.instanceOf)(RegExp)).describe('one of LoggingEvent (including "all"), or a regex which matches such');
const oneOfLoggingEventsWithoutAll = (0, decoders_1.either)((0, decoders_1.oneOf)(constants_js_1.LoggingEvents), (0, decoders_1.instanceOf)(RegExp)).describe('one of LoggingEvent (excluding "all"), or a regex which matches such');
const decoder = (0, decoders_1.nullish)((0, decoders_1.either)(oneOfLoggingEvents, (0, decoders_1.array)((0, decoders_1.either)(oneOfLoggingEvents, (0, decoders_1.exact)({
events: (0, decoders_1.either)(oneOfLoggingEvents, (0, decoders_1.nonEmptyArray)(oneOfLoggingEventsWithoutAll)),
emits: (0, utils_js_1.oneOrMany)((0, decoders_1.oneOf)(constants_js_1.LoggingOutputs)),
})))));
const transformer = decoder.transform((config) => {
if (!config) {
return monoid.empty;
}
if (config === 'all') {
return { layers: constants_js_1.LoggingDefaults };
}
if (typeof config === 'string') {
return { layers: [{ events: [config], emits: constants_js_1.LoggingDefaultOutputs[config] }] };
}
if (config instanceof RegExp) {
config = regex2events(config);
}
const layers = config.flatMap((c) => {
if (c === 'all') {
return constants_js_1.LoggingDefaults;
}
if (typeof c === 'string') {
return [{ events: [c], emits: constants_js_1.LoggingDefaultOutputs[c] }];
}
if (c instanceof RegExp) {
return regex2events(c).map((e) => ({ events: [e], emits: constants_js_1.LoggingDefaultOutputs[e] }));
}
return [{
events: buildEvents(c.events),
emits: Array.isArray(c.emits) ? c.emits : [c.emits],
}];
});
return { layers };
});
exports.LoggingCfgHandler = new opts_handlers_js_1.MonoidalOptionsHandler(transformer, monoid);
function regex2events(regex) {
return constants_js_1.LoggingEvents.filter((e) => regex.test(e));
}
function buildEvents(events) {
return (0, utils_js_1.toArray)(events).flatMap((e) => {
if (e === 'all') {
return constants_js_1.LoggingEvents;
}
if (e instanceof RegExp) {
return constants_js_1.LoggingEvents.filter((ee) => e.test(ee));
}
return e;
});
}