@ng-apimock/base-client
Version:
Base client for @ng-apimock/core
159 lines • 6.39 kB
JavaScript
"use strict";
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.DefaultConfiguration = exports.BaseClient = void 0;
const https = require("https");
const node_fetch_1 = require("node-fetch");
const urljoin = require("url-join");
const uuid = require("uuid");
const configuration_1 = require("./configuration");
Object.defineProperty(exports, "DefaultConfiguration", { enumerable: true, get: function () { return configuration_1.DefaultConfiguration; } });
class BaseClient {
constructor(configuration) {
this.ngApimockId = uuid.v4();
this.configuration = Object.assign(Object.assign({}, configuration_1.DefaultConfiguration), JSON.parse(JSON.stringify(configuration)));
this.baseUrl = urljoin(this.configuration.baseUrl, this.configuration.basePath);
this.agent = new https.Agent({
rejectUnauthorized: false
});
}
createPreset(name, includeMocks, includeVariables) {
return __awaiter(this, void 0, void 0, function* () {
const mocks = includeMocks
? (yield this.getMocks()).state
: {};
const variables = includeVariables
? (yield this.getVariables()).state
: {};
const payload = {
name,
mocks,
variables
};
return yield this.invoke('presets', 'POST', payload);
});
}
delayResponse(name, delay) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.invoke('mocks', 'PUT', { name, delay });
});
}
deleteVariable(key) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.invoke(`variables/${key}`, 'DELETE', {});
});
}
echoRequest(name, echo) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.invoke('mocks', 'PUT', { name, echo });
});
}
fetchResponse(request) {
return __awaiter(this, void 0, void 0, function* () {
return yield (0, node_fetch_1.default)(request);
});
}
getMocks() {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.invoke('mocks', 'GET', {});
return yield response.json();
});
}
getPresets() {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.invoke('presets', 'GET', {});
return yield response.json();
});
}
getRecordings() {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.invoke('recordings', 'GET', {});
return yield response.json();
});
}
getVariables() {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.invoke('variables', 'GET', {});
return yield response.json();
});
}
invoke(query, method, body) {
return __awaiter(this, void 0, void 0, function* () {
const requestInit = {
method,
headers: {
Cookie: `${this.configuration.identifier}=${this.ngApimockId}`,
'Content-Type': 'application/json'
}
};
if (['GET', 'HEAD'].indexOf(method) === -1) {
requestInit.body = JSON.stringify(body);
}
if (this.baseUrl.startsWith('https')) {
requestInit.agent = this.agent;
}
const url = urljoin(this.baseUrl, query);
return yield this.fetchResponse(new node_fetch_1.Request(url, requestInit))
.then((response) => {
if (response.ok) {
return response;
}
throw new Error(`An error occured while invoking ${url} that resulted in status code ${response.status}`);
});
});
}
recordRequests(record) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.invoke('actions', 'PUT', { action: 'record', record });
});
}
resetMocksToDefault() {
return __awaiter(this, void 0, void 0, function* () {
yield this.invoke('actions', 'PUT', { action: 'defaults' });
});
}
selectPreset(name) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.invoke('presets', 'PUT', { name });
});
}
selectScenario(name, scenario) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.invoke('mocks', 'PUT', { name, scenario });
});
}
setMocksToPassThrough() {
return __awaiter(this, void 0, void 0, function* () {
return yield this.invoke('actions', 'PUT', { action: 'passThroughs' });
});
}
setNgApimockCookie() {
return __awaiter(this, void 0, void 0, function* () {
yield this.openUrl(urljoin(this.baseUrl, 'init'));
yield this.setCookie(this.configuration.identifier, this.ngApimockId);
return this;
});
}
setVariable(key, value) {
return __awaiter(this, void 0, void 0, function* () {
const body = {};
body[key] = value;
return yield this.setVariables(body);
});
}
setVariables(variables) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.invoke('variables', 'PUT', variables);
});
}
}
exports.BaseClient = BaseClient;
//# sourceMappingURL=base.client.js.map