client.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const httpTransport = require("@bbc/http-transport");
const _ = require("lodash");
const configuration_1 = require("./configuration");
const REDIRECT = '3';
class HttpTransportClient {
    /**
     * HttpTransport rest client implementation
     * @param {ClientParams} params - Client parameters
     * @see ClientParams
     */
    constructor(params) {
        this.params = params;
        this.timeout = _.get(this.params, 'timeout', configuration_1.defaultRequestOptions.timeout);
        this.client = configuration_1.configureClient(params);
    }
    async patch(uri, body, opts) {
        const req = this.client.patch(uri, body);
        const res = await req
            .headers(_.get(opts, 'headers'))
            .query(_.get(opts, 'qs'))
            .asResponse();
        return this.toResponse(res);
    }
    /**
     * Executes a HTTP GET
     *
     * @return a Promise<Response> instance
     * @param {string} uri - uri to request
     * @param {RequestOptions} opts - optional request configuration
     */
    async get(uri, opts) {
        const req = this.client.get(uri);
        const res = await req
            .headers(_.get(opts, 'headers'))
            .timeout(this.timeout)
            .query(_.get(opts, 'qs'))
            .asResponse();
        return this.toResponse(res);
    }
    /**
     * Executes a HTTP POST
     *
     * @return a Promise<Response> instance
     * @param {string} uri - uri to request
     * @param {RequestBody} body - request body
     * @param {RequestOptions} opts - optional request configuration
     */
    async post(uri, body, opts) {
        const req = this.client.post(uri, body);
        const res = await req
            .headers(_.get(opts, 'headers'))
            .query(_.get(opts, 'qs'))
            .asResponse();
        return this.toResponse(res);
    }
    /**
     * Executes a HTTP PUT
     *
     * @return a Promise<Response> instance
     * @param {string} uri - uri to request
     * @param {RequestBody} body - request body
     * @param {RequestOptions} opts - optional request configuration
     */
    async put(uri, body, opts) {
        const req = this.client;
        if (opts && opts.json !== undefined) {
            req.use(httpTransport.setContextProperty({
                json: opts.json
            }, 'opts'));
        }
        const res = await req.put(uri, body)
            .headers(_.get(opts, 'headers'))
            .query(_.get(opts, 'qs'))
            .asResponse();
        return this.toResponse(res);
    }
    /**
     * Executes a HTTP HEAD
     *
     * @return a Promise<Response> instance
     * @param {string} uri - uri to request
     * @param {RequestOptions} opts - optional request configuration
     */
    async head(uri, opts) {
        const req = this.client.head(uri);
        const res = await req
            .headers(_.get(opts, 'headers'))
            .query(_.get(opts, 'qs'))
            .asResponse();
        return this.toResponse(res);
    }
    /**
     * Executes a HTTP DELETE
     *
     * @return a Promise<Response> instance
     * @param {string} uri - uri to request
     * @param {RequestOptions} opts - optional request configuration
     */
    async delete(uri, opts) {
        const req = this.client.delete(uri);
        const res = await req
            .headers(_.get(opts, 'headers'))
            .query(_.get(opts, 'qs'))
            .asResponse();
        return this.toResponse(res);
    }
    toResponse(res) {
        return {
            body: res.body,
            headers: res.headers,
            ok: res.statusCode === 200,
            redirected: res.statusCode.toString().slice(0, 1) === REDIRECT,
            status: res.statusCode,
            statusText: _.get(res, 'httpResponse.statusMessage'),
            url: res.url
        };
    }
}
exports.HttpTransportClient = HttpTransportClient;