@datastax/astra-db-ts
Version:
Data API TypeScript client
61 lines (60 loc) • 2.52 kB
JavaScript
;
// Copyright Datastax, Inc
// SPDX-License-Identifier: Apache-2.0
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpOptsHandler = void 0;
const decoders_1 = require("decoders");
const utils_js_1 = require("../../lib/utils.js");
const opts_handlers_js_1 = require("../../lib/opts-handlers.js");
const index_js_1 = require("../../lib/index.js");
const utils_js_2 = require("../../documents/utils.js");
const fetchH2 = (0, decoders_1.define)((module, ok, err) => {
if (module && typeof module === 'object') {
const verified = (0, decoders_1.inexact)({
TimeoutError: utils_js_1.function_,
context: utils_js_1.function_,
}).verify({
TimeoutError: module.TimeoutError,
context: module.context,
});
return ok(verified);
}
else {
return err(`fetchH2 should be set to \`import * as fetchH2 from 'fetch-h2'\` or \`const fetchH2 = require('fetch-h2')\`; got ${(0, utils_js_2.betterTypeOf)(module)}`);
}
});
const decoder = (0, decoders_1.nullish)((0, decoders_1.taggedUnion)('client', {
'fetch-h2': (0, decoders_1.object)({
client: (0, decoders_1.constant)('fetch-h2'),
preferHttp2: (0, decoders_1.optional)(decoders_1.boolean),
http1: (0, decoders_1.optional)((0, decoders_1.object)({
keepAlive: (0, decoders_1.optional)(decoders_1.boolean),
keepAliveMS: (0, decoders_1.optional)(decoders_1.positiveInteger),
maxSockets: (0, decoders_1.optional)(decoders_1.positiveInteger),
maxFreeSockets: (0, decoders_1.optional)((0, decoders_1.either)(decoders_1.positiveInteger, (0, decoders_1.constant)(Infinity))),
})),
fetchH2: fetchH2,
}),
'fetch': (0, decoders_1.object)({
client: (0, decoders_1.constant)('fetch'),
}),
'custom': (0, decoders_1.object)({
client: (0, decoders_1.constant)('custom'),
fetcher: (0, decoders_1.inexact)({
fetch: utils_js_1.function_,
close: (0, decoders_1.optional)(utils_js_1.function_),
}),
}),
}));
const transformer = decoder.transform((opts) => {
const ctx = (opts?.client === 'fetch-h2')
? new index_js_1.FetchH2(opts) :
(opts?.client === 'custom')
? opts.fetcher
: new index_js_1.FetchNative();
return {
ctx: ctx,
closed: { ref: false },
};
});
exports.HttpOptsHandler = new opts_handlers_js_1.OptionsHandler(transformer);