@datastax/astra-db-ts
Version:
Data API TypeScript client
49 lines (48 loc) • 1.98 kB
JavaScript
// Copyright Datastax, Inc
// SPDX-License-Identifier: Apache-2.0
import { MonoidalOptionsHandler, monoids } from '../../lib/opts-handlers.js';
import { TokenProvider } from '../../lib/index.js';
import { exact, nullish, oneOf, optional, regex, string } from 'decoders';
import { Timeouts } from '../../lib/api/timeouts/timeouts.js';
import { InternalLogger } from '../../lib/logging/internal-logger.js';
import { TableSerDes } from '../../documents/tables/ser-des/ser-des.js';
import { CollSerDes } from '../../documents/collections/ser-des/ser-des.js';
const monoid = monoids.object({
logging: InternalLogger.cfg,
keyspace: monoids.optional(),
dataApiPath: monoids.optional(),
collSerdes: CollSerDes.cfg,
tableSerdes: TableSerDes.cfg,
timeoutDefaults: Timeouts.cfg,
token: TokenProvider.opts,
});
const decoder = nullish(exact({
logging: InternalLogger.cfg.decoder,
token: TokenProvider.opts.decoder,
dataApiPath: optional(string),
keyspace: optional(regex(/^\w{1,48}$/, 'Expected a string of 1-48 alphanumeric characters')),
timeoutDefaults: Timeouts.cfg.decoder,
serdes: optional(exact({
collection: CollSerDes.cfg.decoder,
table: TableSerDes.cfg.decoder,
mutateInPlace: optional(oneOf([true, false])),
})),
}));
const transformer = decoder.transform((input) => {
var _a, _b;
if (!input) {
return monoid.empty;
}
if (input.serdes) {
(_a = input.serdes.collection).mutateInPlace ?? (_a.mutateInPlace = input.serdes.mutateInPlace);
(_b = input.serdes.table).mutateInPlace ?? (_b.mutateInPlace = input.serdes.mutateInPlace);
}
return {
...input,
keyspace: input.keyspace,
dataApiPath: input.dataApiPath,
collSerdes: input.serdes?.collection ?? CollSerDes.cfg.empty,
tableSerdes: input.serdes?.table ?? TableSerDes.cfg.empty,
};
});
export const DbOptsHandler = new MonoidalOptionsHandler(transformer, monoid);