UNPKG

rest-assured-ts

Version:
189 lines (188 loc) 6.99 kB
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 got from "got"; // tslint:disable-next-line:no-var-requires const FormData = require("form-data"); const tunnel = require("tunnel"); let responseData; /*CRUD operations PUT,POST,GET,DELETE and PATCH */ export function makeHttpRequest(url, headerOptions, httpMethod, inputBody, formFlag) { return __awaiter(this, void 0, void 0, function* () { if (headerOptions === null) { headerOptions = { Accept: "application/json" }; } if (inputBody === null) { inputBody = ""; } else if (formFlag) { if (inputBody != null) { inputBody = JSON.parse(inputBody); } } if (httpMethod === null) { httpMethod = "GET"; //default GET method } if (formFlag === null) { formFlag = false; } try { responseData = yield got(url, { headers: headerOptions, body: inputBody, form: formFlag, method: httpMethod, timeout: 5000, throwHttpErrors: false, retry: 1 }); // Note: Using "any" for the type, since got has a number of variants for the options parameter. // It would be better to replace the "any" with a more specific type, if that can be determined. } catch (error) { exception(error); } return responseData; }); } /*Sending Form-Data as requestBody for CRUD operations PUT,POST,DELETE and PATCH */ export function makeHttpRequestWithFormData(url, headerOptions, HttpMethod, formDataMap) { return __awaiter(this, void 0, void 0, function* () { const form = new FormData(); if (headerOptions == null) { headerOptions = { Accept: "application/json" }; } if (HttpMethod == null) { HttpMethod = "GET"; //default GET method } // @ts-ignore for (const [key, value] of formDataMap) { form.append(key, value); } try { responseData = yield got(url, { headers: headerOptions, body: form, method: HttpMethod, throwHttpErrors: false, retry: 1 }); // Note: Using "any" for the type, since got has a number of variants for the options parameter. // It would be better to replace the "any" with a more specific type, if that can be determined. } catch (error) { exception(error); } return responseData; }); } /*Sending Form-Data as requestBody for CRUD operations PUT,POST,DELETE and PATCH */ export function restRequestWithFormData(url, options) { return __awaiter(this, void 0, void 0, function* () { const form = new FormData(); if (options.headerOptions === null) { options.headerOptions = { Accept: "application/json" }; } if (options.httpMethod === null) { options.httpMethod = "GET"; //default GET method } if (options.timeOut === null) { options.timeOut = 5000; } // @ts-ignore for (const [key, value] of options.formDataMap) { form.append(key, value); } try { responseData = yield got(url, { headers: options.headerOptions, body: form, timeout: options.timeOut, method: options.httpMethod, throwHttpErrors: false, retry: 1, }); // Note: Using "any" for the type, since got has a number of variants for the options parameter. // It would be better to replace the "any" with a more specific type, if that can be determined. } catch (error) { exception(error); } return responseData; }); } export function restRequest(url, options) { return __awaiter(this, void 0, void 0, function* () { if (options.headerOptions === null) { options.headerOptions = { Accept: "application/json" }; } if (options.inputBody === null) { options.inputBody = ""; } else if (options.formFlag) { if (options.inputBody != null) { options.inputBody = JSON.parse(options.inputBody); } } if (options.httpMethod === null) { options.httpMethod = "GET"; //default GET method } if (options.formFlag === null) { options.formFlag = false; } if (options.retry === null) { options.retry = 1; } if (options.timeOut === null) { options.timeOut = 5000; } if (options.agent === null) { options.agent = {}; } try { responseData = yield got(url, { headers: options.headerOptions, body: options.inputBody, form: options.formFlag, method: options.httpMethod, timeout: options.timeOut, throwHttpErrors: false, retry: options.retry, agent: options.agent, }); // Note: Using "any" for the type, since got has a number of variants for the options parameter. // It would be better to replace the "any" with a more specific type, if that can be determined. } catch (error) { exception(error); } return responseData; }); } function exception(error) { try { const customError = JSON.parse(JSON.stringify(error)); const message = { "message": error.message, "name": customError.name, "code": customError.code, "host": customError.host, "url": customError.url, "path": customError.path, "body": JSON.stringify(customError.body, null, " ") }; try { responseData = error.response; JSON.parse(responseData.body); return responseData; } catch (err) { throw JSON.stringify(message, null, " "); } } catch (err) { throw err; } }