@datastax/astra-db-ts
Version:
Data API TypeScript client
120 lines (119 loc) • 3.83 kB
JavaScript
;
// Copyright Datastax, Inc
// SPDX-License-Identifier: Apache-2.0
Object.defineProperty(exports, "__esModule", { value: true });
exports.monoids = exports.MonoidalOptionsHandler = exports.OptionsHandler = exports.OptionParseError = void 0;
const utils_js_1 = require("../lib/utils.js");
class OptionParseError extends Error {
constructor(message, field) {
super(message);
Object.defineProperty(this, "field", {
enumerable: true,
configurable: true,
writable: true,
value: field
});
}
}
exports.OptionParseError = OptionParseError;
class OptionsHandler {
constructor(decoder) {
Object.defineProperty(this, "decoder", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "parseable", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "parsed", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.decoder = decoder;
}
parse(input, field) {
try {
return asParsed(this.decoder.verify(input));
}
catch (e) {
if (!(e instanceof Error && e.name === 'Decoding error')) {
throw e;
}
const message = (!(0, utils_js_1.isNullish)(field))
? `Error parsing '${field}': ${e.message}`
: e.message;
throw new OptionParseError(message, field);
}
}
parseWithin(obj, field) {
return this.parse(obj?.[(0, utils_js_1.splitWithIncludesCheck)(field, '.').at(-1)], field);
}
}
exports.OptionsHandler = OptionsHandler;
class MonoidalOptionsHandler extends OptionsHandler {
constructor(decoder, monoid) {
super(decoder);
Object.defineProperty(this, "monoid", {
enumerable: true,
configurable: true,
writable: true,
value: monoid
});
Object.defineProperty(this, "empty", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.empty = asParsed(monoid.empty);
}
concat(configs) {
if (configs.length <= 1) {
return configs[0] ?? this.empty; // minor optimization
}
return asParsed(this.monoid.concat(configs));
}
concatParse(configs, raw, field) {
return this.concat([...configs, this.parse(raw, field)]);
}
concatParseWithin(configs, obj, field) {
return this.concat([...configs, this.parseWithin(obj, field)]);
}
}
exports.MonoidalOptionsHandler = MonoidalOptionsHandler;
const asParsed = (input) => input;
exports.monoids = {
optional: () => ({
empty: undefined,
concat: (0, utils_js_1.findLast)((a) => a !== undefined && a !== null),
}),
array: () => ({
empty: [],
concat: (as) => as.flat(),
}),
prependingArray: () => ({
empty: [],
concat: (as) => as.reduce((acc, next) => [...next, ...acc], []),
}),
object(schema) {
const schemaEntries = Object.entries(schema);
const empty = Object.fromEntries(schemaEntries.map(([k, v]) => [k, v.empty]));
const concat = (configs) => {
const result = { ...empty };
for (const config of configs) {
for (const [key, monoid] of schemaEntries) {
result[key] = monoid.concat([result[key], config[key]]);
}
}
return result;
};
return { empty, concat };
},
};