@zowe/imperative
Version:
framework for building configurable CLIs
502 lines • 27.8 kB
JavaScript
"use strict";
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RestClient = void 0;
const RestConstants_1 = require("./RestConstants");
const HTTPVerb_1 = require("./types/HTTPVerb");
const AbstractRestClient_1 = require("./AbstractRestClient");
const utilities_1 = require("../../../utilities");
const AbstractRestClientProperties_1 = require("./types/AbstractRestClientProperties");
/**
* Class to handle http(s) requests, build headers, collect data, report status codes, and header responses
* and passes control to session object for maintaining connection information (tokens, checking for timeout, etc...)
* @export
* @class RestClient
* @extends {AbstractRestClient}
*/
class RestClient extends AbstractRestClient_1.AbstractRestClient {
/**
* Wrap get for common error handling and supporting generic JSON types
* @static
* @template T - object type to return
* @param {AbstractSession} session - representing connection to this api
* @param {string} resource - the API URI that we are targeting
* @param {any[]} reqHeaders - headers for the http(s) request
* @returns {Promise<T>} - object on promise
* @throws if the request gets a status code outside of the 200 range
* or other connection problems occur (e.g. connection refused)
* @memberof RestClient
*/
static getExpectJSON(session_1, resource_1) {
return __awaiter(this, arguments, void 0, function* (session, resource, reqHeaders = []) {
const data = yield this.getExpectString(session, resource, reqHeaders);
return utilities_1.JSONUtils.parse(data, "The get request appeared to succeed, but the response was not in the expected format");
});
}
/**
* Wrap put for common error handling and supporting generic JSON types
* @static
* @template T - object type to return
* @param {AbstractSession} session - representing connection to this api
* @param {string} resource - the API URI that we are targeting
* @param {any[]} reqHeaders - headers for the http(s) request
* @param {any} payload - data to write on the http(s) request
* @returns {Promise<T>} - object on promise
* @throws if the request gets a status code outside of the 200 range
* or other connection problems occur (e.g. connection refused)
* @memberof RestClient
*/
static putExpectJSON(session_1, resource_1) {
return __awaiter(this, arguments, void 0, function* (session, resource, reqHeaders = [], payload) {
const data = yield this.putExpectString(session, resource, reqHeaders, payload);
return utilities_1.JSONUtils.parse(data, "The put request appeared to succeed, but the response was not in the expected format");
});
}
/**
* Wrap post for common error handling and supporting generic JSON types
* @static
* @template T - object type to return
* @param {AbstractSession} session - representing connection to this api
* @param {string} resource - the API URI that we are targeting
* @param {any[]} reqHeaders - headers for the http(s) request
* @param {any} payload - data to write on the http(s) request
* @returns {Promise<T>} - object on promise
* @throws if the request gets a status code outside of the 200 range
* or other connection problems occur (e.g. connection refused)
* @memberof RestClient
*/
static postExpectJSON(session_1, resource_1) {
return __awaiter(this, arguments, void 0, function* (session, resource, reqHeaders = [], payload) {
const data = yield this.postExpectString(session, resource, reqHeaders, payload);
return utilities_1.JSONUtils.parse(data, "The post request appeared to succeed, but the response was not in the expected format");
});
}
/**
* Wrap post for common error handling and supporting generic JSON types
* @static
* @template T - object type to return
* @param {AbstractSession} session - representing connection to this api
* @param {string} resource - the API URI that we are targeting
* @param {any[]} reqHeaders - headers for the http(s) request
* @returns {Promise<T>} - object on promise
* @throws if the request gets a status code outside of the 200 range
* or other connection problems occur (e.g. connection refused)
* @memberof RestClient
*/
static deleteExpectJSON(session_1, resource_1) {
return __awaiter(this, arguments, void 0, function* (session, resource, reqHeaders = []) {
const data = yield this.deleteExpectString(session, resource, reqHeaders);
return utilities_1.JSONUtils.parse(data, "The delete request appeared to succeed, but the response was not in the expected format");
});
}
/**
* REST HTTP GET operation
* @static
* @param {AbstractSession} session - representing connection to this api
* @param {string} resource - URI for which this request should go against
* @param {any} reqHeaders - headers to include in the REST request
* @returns {Promise<Buffer>} - response body content from http(s) call
* @throws if the request gets a status code outside of the 200 range
* or other connection problems occur (e.g. connection refused)
* @memberof RestClient
*/
static getExpectBuffer(session_1, resource_1) {
return __awaiter(this, arguments, void 0, function* (session, resource, reqHeaders = []) {
const client = new this(session);
yield client.request({ resource, request: HTTPVerb_1.HTTP_VERB.GET, reqHeaders });
return client.data;
});
}
/**
* REST HTTP PUT operation
* @static
* @param {AbstractSession} session - representing connection to this api
* @param {string} resource - URI for which this request should go against
* @param {object[]} reqHeaders - headers to include in the REST request
* @param {any} data - payload data
* @returns {Promise<Buffer>} - response body content from http(s) call
* @throws if the request gets a status code outside of the 200 range
* or other connection problems occur (e.g. connection refused)
* @memberof RestClient
*/
static putExpectBuffer(session_1, resource_1) {
return __awaiter(this, arguments, void 0, function* (session, resource, reqHeaders = [], data) {
const client = new this(session);
yield client.request({ resource, request: HTTPVerb_1.HTTP_VERB.PUT, reqHeaders, writeData: data });
return client.data;
});
}
/**
* REST HTTP POST operation
* @static
* @param {AbstractSession} session - representing connection to this api
* @param {string} resource - URI for which this request should go against
* @param {object[]} reqHeaders - headers to include in the REST request
* @param {any} data - payload data
* @returns {Promise<Buffer>} - response body content from http(s) call
* @throws if the request gets a status code outside of the 200 range
* or other connection problems occur (e.g. connection refused)
* @memberof RestClient
*/
static postExpectBuffer(session_1, resource_1) {
return __awaiter(this, arguments, void 0, function* (session, resource, reqHeaders = [], data) {
const client = new this(session);
yield client.request({ resource, request: HTTPVerb_1.HTTP_VERB.POST, reqHeaders, writeData: data });
return client.data;
});
}
/**
* REST HTTP DELETE operation
* @static
* @param {AbstractSession} session - representing connection to this api
* @param {string} resource - URI for which this request should go against
* @param {any} reqHeaders - headers to include in the REST request
* @returns {Promise<Buffer>} - response body content from http(s) call
* @throws if the request gets a status code outside of the 200 range
* or other connection problems occur (e.g. connection refused)
* @memberof RestClient
*/
static deleteExpectBuffer(session_1, resource_1) {
return __awaiter(this, arguments, void 0, function* (session, resource, reqHeaders = []) {
const client = new this(session);
yield client.request({ resource, request: HTTPVerb_1.HTTP_VERB.DELETE, reqHeaders });
return client.data;
});
}
/**
* REST HTTP GET operation
* @static
* @param {AbstractSession} session - representing connection to this api
* @param {string} resource - URI for which this request should go against
* @param {any} reqHeaders - headers to include in the REST request
* @returns {Promise<string>} - response body content from http(s) call
* @throws if the request gets a status code outside of the 200 range
* or other connection problems occur (e.g. connection refused)
* @memberof RestClient
*/
static getExpectString(session, resource, reqHeaders = []) {
return new this(session).request({ resource, request: HTTPVerb_1.HTTP_VERB.GET, reqHeaders });
}
/**
* REST HTTP PUT operation
* @static
* @param {AbstractSession} session - representing connection to this api
* @param {string} resource - URI for which this request should go against
* @param {object[]} reqHeaders - headers to include in the REST request
* @param {any} data - payload data
* @returns {Promise<string>} - response body content from http(s) call
* @throws if the request gets a status code outside of the 200 range
* or other connection problems occur (e.g. connection refused)
* @memberof RestClient
*/
static putExpectString(session, resource, reqHeaders = [], data) {
return new this(session).request({ resource, request: HTTPVerb_1.HTTP_VERB.PUT, reqHeaders, writeData: data });
}
/**
* REST HTTP POST operation
* @static
* @param {AbstractSession} session - representing connection to this api
* @param {string} resource - URI for which this request should go against
* @param {object[]} reqHeaders - headers to include in the REST request
* @param {any} data - payload data
* @returns {Promise<string>} - response body content from http(s) call
* @throws if the request gets a status code outside of the 200 range
* or other connection problems occur (e.g. connection refused)
* @memberof RestClient
*/
static postExpectString(session, resource, reqHeaders = [], data) {
return new this(session).request({ resource, request: HTTPVerb_1.HTTP_VERB.POST, reqHeaders, writeData: data });
}
/**
* REST HTTP DELETE operation
* @static
* @param {AbstractSession} session - representing connection to this api
* @param {string} resource - URI for which this request should go against
* @param {any} reqHeaders - headers to include in the REST request
* @returns {Promise<string>} - response body content from http(s) call
* @throws if the request gets a status code outside of the 200 range
* or other connection problems occur (e.g. connection refused)
* @memberof RestClient
*/
static deleteExpectString(session, resource, reqHeaders = []) {
return new this(session).request({ resource, request: HTTPVerb_1.HTTP_VERB.DELETE, reqHeaders });
}
/**
* REST HTTP GET operation - streaming the response to a writable stream
* @static
* @param {AbstractSession} session - representing connection to this api
* @param {string} resource - URI for which this request should go against
* @param {any} reqHeaders - headers to include in the REST request
* @param responseStream - the stream to which the response data will be written
* @param normalizeResponseNewLines - streaming only - true if you want newlines to be \r\n on windows
* when receiving data from the server to responseStream. Don't set this for binary responses
* @param {ITaskWithStatus} task - task used to update the user on the progress of their request
* @returns {Promise<string>} - empty string - data is not buffered for this request
* @throws if the request gets a status code outside of the 200 range
* or other connection problems occur (e.g. connection refused)
* @memberof RestClient
*/
static getStreamed(session, resource, reqHeaders = [], responseStream, normalizeResponseNewLines, task) {
return new this(session).request({ resource, request: HTTPVerb_1.HTTP_VERB.GET, reqHeaders, responseStream,
normalizeResponseNewLines, task });
}
/**
* REST HTTP PUT operation with streamed response and request
* @static
* @param {AbstractSession} session - representing connection to this api
* @param {string} resource - URI for which this request should go against
* @param {object[]} reqHeaders - headers to include in the REST request
* @param {any} responseStream - stream to which the response data will be written
* @param {any} requestStream - stream from which payload data will be read
* @param normalizeResponseNewLines - streaming only - true if you want newlines to be \r\n on windows
* when receiving data from the server to responseStream. Don't set this for binary responses
* @param normalizeRequestNewLines - streaming only - true if you want \r\n to be replaced with \n when sending
* data to the server from requestStream. Don't set this for binary requests
* @param {ITaskWithStatus} task - task used to update the user on the progress of their request
* @returns {Promise<string>} - empty string - data is not buffered for streamed requests
* @throws if the request gets a status code outside of the 200 range
* or other connection problems occur (e.g. connection refused)
* @memberof RestClient
*/
static putStreamed(session, resource, reqHeaders = [], responseStream, requestStream, normalizeResponseNewLines, normalizeRequestNewLines, task) {
return new this(session).request({ resource, request: HTTPVerb_1.HTTP_VERB.PUT, reqHeaders, responseStream, requestStream,
normalizeResponseNewLines, normalizeRequestNewLines, task });
}
/**
* REST HTTP PUT operation with only streamed request, buffers response data and returns it
* @static
* @param {AbstractSession} session - representing connection to this api
* @param {string} resource - URI for which this request should go against
* @param {object[]} reqHeaders - headers to include in the REST request
* @param {any} requestStream - stream from which payload data will be read
* @param normalizeRequestNewLines - streaming only - true if you want \r\n to be replaced with \n when sending
* data to the server from requestStream. Don't set this for binary requests
* @param {ITaskWithStatus} task - task used to update the user on the progress of their request
* @returns {Promise<string>} - string of the response
* @throws if the request gets a status code outside of the 200 range
* or other connection problems occur (e.g. connection refused)
* @memberof RestClient
*/
static putStreamedRequestOnly(session, resource, reqHeaders = [], requestStream, normalizeRequestNewLines, task) {
return new this(session).request({ resource, request: HTTPVerb_1.HTTP_VERB.PUT, reqHeaders, requestStream,
normalizeRequestNewLines, task });
}
/**
* REST HTTP POST operation streaming both the request and the response
* @static
* @param {AbstractSession} session - representing connection to this api
* @param {string} resource - URI for which this request should go against
* @param {object[]} reqHeaders - headers to include in the REST request
* @param {any} responseStream - stream to which the response data will be written
* @param {any} requestStream - stream from which payload data will be read
* @param normalizeResponseNewLines - streaming only - true if you want newlines to be \r\n on windows
* when receiving data from the server to responseStream. Don't set this for binary responses
* @param normalizeRequestNewLines - streaming only - true if you want \r\n to be replaced with \n when sending
* data to the server from requestStream. Don't set this for binary requests
* @param {ITaskWithStatus} task - task used to update the user on the progress of their request
* @returns {Promise<string>} - empty string - data is not buffered for this request
* @throws if the request gets a status code outside of the 200 range
* or other connection problems occur (e.g. connection refused)
* @memberof RestClient
*/
static postStreamed(session, resource, reqHeaders = [], responseStream, requestStream, normalizeResponseNewLines, normalizeRequestNewLines, task) {
return new this(session).request({ resource, request: HTTPVerb_1.HTTP_VERB.POST, reqHeaders, responseStream, requestStream,
normalizeResponseNewLines, normalizeRequestNewLines, task });
}
/**
* REST HTTP POST operation, streaming only the request and not the response
* @static
* @param {AbstractSession} session - representing connection to this api
* @param {string} resource - URI for which this request should go against
* @param {object[]} reqHeaders - headers to include in the REST request
* @param {any} requestStream - stream from which payload data will be read
* @param normalizeRequestNewLines - streaming only - true if you want \r\n to be replaced with \n when sending
* data to the server from requestStream. Don't set this for binary requests
* @param {ITaskWithStatus} task - task used to update the user on the progress of their request
* @returns {Promise<string>} - string of the response
* @throws if the request gets a status code outside of the 200 range
* or other connection problems occur (e.g. connection refused)
* @memberof RestClient
*/
static postStreamedRequestOnly(session, resource, reqHeaders = [], requestStream, normalizeRequestNewLines, task) {
return new this(session).request({ resource, request: HTTPVerb_1.HTTP_VERB.POST, reqHeaders, requestStream,
normalizeRequestNewLines, task });
}
/**
* REST HTTP DELETE operation
* @static
* @param {AbstractSession} session - representing connection to this api
* @param {string} resource - URI for which this request should go against
* @param {any} reqHeaders - headers to include in the REST request
* @param {any} responseStream - stream to which the response data will be written
* @param {ITaskWithStatus} task - task used to update the user on the progress of their request
* @param normalizeResponseNewLines - streaming only - true if you want newlines to be \r\n on windows
* when receiving data from the server to responseStream. Don't set this for binary responses
* @returns {Promise<string>} - empty string - data is not buffered for streamed requests
* @throws if the request gets a status code outside of the 200 range
* or other connection problems occur (e.g. connection refused)
* @memberof RestClient
*/
static deleteStreamed(session, resource, reqHeaders = [], responseStream, normalizeResponseNewLines, task) {
return new this(session).request({ resource, request: HTTPVerb_1.HTTP_VERB.DELETE, reqHeaders,
responseStream, normalizeResponseNewLines, task });
}
/**
* REST HTTP GET operation returning full HTTP(S) Response
* @static
* @param {AbstractSession} session - representing connection to this api
* @param {IOptionsFullRequest} options - URI for which this request should go against
* @returns {Promise<IRestClientResponse>} - full response or filtered based on provided params
* @throws if the request gets a status code outside of the 200 range
* or other connection problems occur (e.g. connection refused)
* @memberof RestClient
*/
static getExpectFullResponse(session, options) {
return __awaiter(this, void 0, void 0, function* () {
const requestOptions = {
resource: options.resource,
request: HTTPVerb_1.HTTP_VERB.GET,
reqHeaders: options.reqHeaders,
writeData: options.writeData,
responseStream: options.responseStream,
requestStream: options.requestStream,
normalizeResponseNewLines: options.normalizeResponseNewLines,
normalizeRequestNewLines: options.normalizeRequestNewLines,
task: options.task,
};
const client = new this(session);
yield client.request(requestOptions);
return this.extractExpectedData(client, options.dataToReturn);
});
}
/**
* REST HTTP PUT operation returning full HTTP(S) Response
* @static
* @param {AbstractSession} session - representing connection to this api
* @param {IOptionsFullRequest} options - list of parameters
* @returns {Promise<IRestClientResponse>} - response content from http(s) call
* @throws if the request gets a status code outside of the 200 range
* or other connection problems occur (e.g. connection refused)
* @memberof RestClient
*/
static putExpectFullResponse(session, options) {
return __awaiter(this, void 0, void 0, function* () {
const requestOptions = {
resource: options.resource,
request: HTTPVerb_1.HTTP_VERB.PUT,
reqHeaders: options.reqHeaders,
writeData: options.writeData,
responseStream: options.responseStream,
requestStream: options.requestStream,
normalizeResponseNewLines: options.normalizeResponseNewLines,
normalizeRequestNewLines: options.normalizeRequestNewLines,
task: options.task,
};
const client = new this(session);
yield client.request(requestOptions);
return this.extractExpectedData(client, options.dataToReturn);
});
}
/**
* REST HTTP delete operation returning full HTTP(S) Response
* @static
* @param {AbstractSession} session - representing connection to this api
* @param {IOptionsFullRequest} options - list of parameters
* @returns {Promise<IRestClientResponse>} - response content from http(s) call
* @throws if the request gets a status code outside of the 200 range
* or other connection problems occur (e.g. connection refused)
* @memberof RestClient
*/
static deleteExpectFullResponse(session, options) {
return __awaiter(this, void 0, void 0, function* () {
const requestOptions = {
resource: options.resource,
request: HTTPVerb_1.HTTP_VERB.DELETE,
reqHeaders: options.reqHeaders,
writeData: options.writeData,
responseStream: options.responseStream,
requestStream: options.requestStream,
normalizeResponseNewLines: options.normalizeResponseNewLines,
normalizeRequestNewLines: options.normalizeRequestNewLines,
task: options.task,
};
const client = new this(session);
yield client.request(requestOptions);
return this.extractExpectedData(client, options.dataToReturn);
});
}
/**
* REST HTTP post operation returning full HTTP(S) Response
* @static
* @param {AbstractSession} session - representing connection to this api
* @param {IOptionsFullRequest} options - list of parameters
* @returns {Promise<IRestClientResponse>} - response content from http(s) call
* @throws if the request gets a status code outside of the 200 range
* or other connection problems occur (e.g. connection refused)
* @memberof RestClient
*/
static postExpectFullResponse(session, options) {
return __awaiter(this, void 0, void 0, function* () {
const requestOptions = {
resource: options.resource,
request: HTTPVerb_1.HTTP_VERB.POST,
reqHeaders: options.reqHeaders,
writeData: options.writeData,
responseStream: options.responseStream,
requestStream: options.requestStream,
normalizeResponseNewLines: options.normalizeResponseNewLines,
normalizeRequestNewLines: options.normalizeRequestNewLines,
task: options.task,
};
const client = new this(session);
yield client.request(requestOptions);
return this.extractExpectedData(client, options.dataToReturn);
});
}
/**
* Helper method to return an indicator for whether or not a URI contains a query string.
* @static
* @param {string} query - URI
* @returns {boolean} - true if query is contained within URI
* @memberof RestClient
*/
static hasQueryString(query) {
return query.slice(-1) !== RestConstants_1.RestConstants.QUERY_ID;
}
/**
* Helper method to extract requested data from response object
* If list is not passed, returns entire response
* @static
* @param {any} client - HTTP(S) response object
* @param {string[]} toReturn - list with object properties to return
* @returns {IRestClientResponse} - trimmed or full response object based on the list provided
* @memberof RestClient
*/
static extractExpectedData(client, toReturn = Object.values(AbstractRestClientProperties_1.CLIENT_PROPERTY)) {
const tailoredResult = {};
toReturn.forEach((property) => {
tailoredResult[property] = client[property];
});
return tailoredResult;
}
}
exports.RestClient = RestClient;
//# sourceMappingURL=RestClient.js.map