@coolio/http
Version:
HTTP networking client
78 lines • 4.47 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { HttpMethod, } from './httpClient.types';
import { bodySerializer } from './bodySerializer';
import { cacheParsedBody } from './helpers/parsedBodyCache.helper';
import { urlCombine, urlDestruct } from './helpers/urlEncoding.helper';
import { sanitizeHeaders } from './helpers';
import { bodyParser } from './bodyParser';
export const DEFAULT_REQUEST_TIMEOUT_MS = 5 * 60 * 1000;
const isHttpInterceptorInterface = (interceptor) => {
const method = interceptor.onIntercept;
return method !== null && method !== undefined;
};
const useInterceptor = (normalizedOptions) => (req, interceptor) => {
if (isHttpInterceptorInterface(interceptor)) {
return interceptor.onIntercept(req, normalizedOptions);
}
return interceptor(req, normalizedOptions);
};
export class HttpClient {
constructor(config) {
this.interceptors = [];
this.addInterceptor = (interceptor) => {
this.interceptors.push(interceptor);
return this;
};
this.get = (uri, options) => {
return this.request(uri, Object.assign(Object.assign({}, options), { body: undefined, method: HttpMethod.GET }));
};
this.post = (uri, options) => {
return this.request(uri, Object.assign(Object.assign({}, options), { method: HttpMethod.POST }));
};
this.put = (uri, options) => {
return this.request(uri, Object.assign(Object.assign({}, options), { method: HttpMethod.PUT }));
};
this.patch = (uri, options) => {
return this.request(uri, Object.assign(Object.assign({}, options), { method: HttpMethod.PATCH }));
};
this.delete = (uri, options) => {
return this.request(uri, Object.assign(Object.assign({}, options), { body: undefined, method: HttpMethod.DELETE }));
};
this.remove = (uri, options) => this.delete(uri, options);
this.handle = config.requestHandler;
this.bodyParser = config.bodyParser || bodyParser();
this.bodySerializer = config.bodySerializer || bodySerializer();
this.headers = config.headers;
this.baseUrl = config.baseUrl ? config.baseUrl.replace(/\/+$/, '') : undefined;
this.queryParserOptions = config.queryParserOptions;
this.querySerializerOptions = config.querySerializerOptions;
this.defaultRequestTimeout = config.requestTimeout || DEFAULT_REQUEST_TIMEOUT_MS;
}
request(url, options) {
return __awaiter(this, void 0, void 0, function* () {
if (this.baseUrl && url.startsWith('/')) {
url = `${this.baseUrl}${url}`;
}
const urlBreakdown = urlDestruct(urlCombine(url, options && options.query), this.queryParserOptions);
const normalizedOptions = Object.assign(Object.assign({}, options), { timeout: options.timeout || this.defaultRequestTimeout, method: options.method, url: urlBreakdown.url, query: urlBreakdown.query, headers: sanitizeHeaders(this.headers, options && options.headers), body: options && options.body });
normalizedOptions.body = this.bodySerializer(normalizedOptions);
const chain = this.interceptors.reduce(useInterceptor(normalizedOptions), () => __awaiter(this, void 0, void 0, function* () {
normalizedOptions.url = urlCombine(normalizedOptions.url, normalizedOptions.query, this.querySerializerOptions);
const response = yield this.handle(normalizedOptions);
const parsedResponse = this.bodyParser(response);
parsedResponse.parsedBody = cacheParsedBody(parsedResponse.parsedBody);
return parsedResponse;
}));
return chain();
});
}
}
//# sourceMappingURL=httpClient.js.map