@gam-test/fetch-wrapper
Version:
A simple fetch wrapper for better error handling and less response context
126 lines (124 loc) • 4.82 kB
JavaScript
Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _DeleteHttpClientjs = require('./http/DeleteHttpClient.js');
var _GetHttpClientjs = require('./http/GetHttpClient.js');
var _HeadHttpClientjs = require('./http/HeadHttpClient.js');
var _PatchHttpClientjs = require('./http/PatchHttpClient.js');
var _PostHttpClientjs = require('./http/PostHttpClient.js');
var _PutHttpClientjs = require('./http/PutHttpClient.js');
class HttpClientManager {
#baseUrl;
#bodyParser;
#config;
constructor({ baseUrl, defaultBodyParser, config }) {
this.#baseUrl = baseUrl;
this.#bodyParser = defaultBodyParser;
this.#config = config;
}
#mergeConfig(config) {
if (!config)
return {
...this.#config,
bodyParser: this.#bodyParser,
headers: {
..._optionalChain([this, 'access', _ => _.#config, 'optionalAccess', _2 => _2.headers])
}
};
if ("bodyParser" in config) {
return {
...this.#config,
...config,
bodyParser: _nullishCoalesce(config.bodyParser, () => ( this.#bodyParser)),
headers: {
..._optionalChain([this, 'access', _3 => _3.#config, 'optionalAccess', _4 => _4.headers]),
..._optionalChain([config, 'optionalAccess', _5 => _5.headers])
}
};
}
return {
...this.#config,
...config,
bodyParser: this.#bodyParser,
headers: {
..._optionalChain([this, 'access', _6 => _6.#config, 'optionalAccess', _7 => _7.headers]),
..._optionalChain([config, 'optionalAccess', _8 => _8.headers])
}
};
}
#mergeConfigWithData(config) {
if (!config)
return {
...this.#config,
data: null,
bodyParser: this.#bodyParser,
headers: {
..._optionalChain([this, 'access', _9 => _9.#config, 'optionalAccess', _10 => _10.headers])
}
};
if ("bodyParser" in config) {
return {
...this.#config,
...config,
bodyParser: _nullishCoalesce(config.bodyParser, () => ( this.#bodyParser)),
headers: {
..._optionalChain([this, 'access', _11 => _11.#config, 'optionalAccess', _12 => _12.headers]),
..._optionalChain([config, 'optionalAccess', _13 => _13.headers])
}
};
}
return {
...this.#config,
...config,
bodyParser: this.#bodyParser,
headers: {
..._optionalChain([this, 'access', _14 => _14.#config, 'optionalAccess', _15 => _15.headers]),
..._optionalChain([config, 'optionalAccess', _16 => _16.headers])
}
};
}
async get(url, config) {
const client = new (0, _GetHttpClientjs.GetHttpClient)({
url: `${this.#baseUrl}${url}`
});
const mergedConfig = this.#mergeConfig(config);
return await client.request(mergedConfig);
}
async head(url, config) {
const client = new (0, _HeadHttpClientjs.HeadHttpClient)({
url: `${this.#baseUrl}${url}`
});
const mergedConfig = this.#mergeConfig(config);
return await client.request({
...mergedConfig,
bodyParser: void 0
});
}
async post(url, config) {
const client = new (0, _PostHttpClientjs.PostHttpClient)({
url: `${this.#baseUrl}${url}`
});
const mergedConfig = this.#mergeConfigWithData(config);
return await client.request(mergedConfig);
}
async patch(url, config) {
const client = new (0, _PatchHttpClientjs.PatchHttpClient)({
url: `${this.#baseUrl}${url}`
});
const mergedConfig = this.#mergeConfigWithData(config);
return await client.request(mergedConfig);
}
async put(url, config) {
const client = new (0, _PutHttpClientjs.PutHttpClient)({
url: `${this.#baseUrl}${url}`
});
const mergedConfig = this.#mergeConfigWithData(config);
return await client.request(mergedConfig);
}
async delete(url, config) {
const client = new (0, _DeleteHttpClientjs.DeleteHttpClient)({
url: `${this.#baseUrl}${url}`
});
const mergedConfig = this.#mergeConfigWithData(config);
return await client.request(mergedConfig);
}
}
exports.HttpClientManager = HttpClientManager;
//# sourceMappingURL=HttpClientManager.js.map
;