@viewdo/dxp-story-cli
Version:
DXP Story Management CLI
189 lines • 8.45 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiService = void 0;
const axios_1 = __importDefault(require("axios"));
const typedi_1 = require("typedi");
const ConsoleService_1 = require("./ConsoleService");
const ConventionService_1 = require("./ConventionService");
const Utilities_1 = require("./Utilities");
let ApiService = class ApiService {
constructor(console, conventions) {
this.console = console;
this._baseURL = `${conventions.admin_api_url}`;
this._api = axios_1.default.create({
baseURL: this._baseURL
});
}
withToken(token) {
this._api = axios_1.default.create({
baseURL: this._baseURL,
headers: {
'Authorization': `Bearer ${token}`,
'ContentType': 'application/json'
}
});
return this;
}
_handleError(err) {
return (0, Utilities_1.handleAxiosError)(err);
}
storyExists(story_key) {
let api_path = `/stories/${story_key}`;
console.debug(`API: HEAD ${this._baseURL}${api_path}`.gray);
return this._api.head(api_path)
.then(r => false)
.catch(r => r.response.status == 409);
}
getStory(story_key) {
let api_path = `/stories/${story_key}`;
this.console.debug(`API: GET ${this._baseURL}${api_path}`.gray);
return this._api.get(api_path)
.then(r => r.data)
.catch(this._handleError);
}
setStory(story_key_1, story_config_1) {
return __awaiter(this, arguments, void 0, function* (story_key, story_config, validateOnly = false) {
let api_path = `/stories/${story_key}?validateOnly=${validateOnly}`;
this.console.debug(`API: POST ${this._baseURL}${api_path}`.gray);
try {
return yield this._api.post(api_path, story_config);
}
catch (err) {
return this._handleError(err);
}
});
}
exportStory(story_key) {
let api_path = `/stories/${story_key}/export`;
this.console.debug(`API: GET ${this._baseURL}${api_path}`.gray);
return this._api.get(api_path)
.then(r => r.data)
.catch(this._handleError);
}
importStory(story_key, story_import_data) {
let api_path = `/stories/${story_key}`;
this.console.debug(`API: PATCH ${this._baseURL}${api_path}`.gray);
return this._api.patch(api_path, story_import_data)
.catch(this._handleError);
}
getOrganization(organization_key) {
let api_path = `/organizations/${organization_key}`;
this.console.debug(`API: GET ${this._baseURL}${api_path}`.gray);
return this._api.get(api_path)
.then(r => r.data)
.catch(this._handleError);
}
getOrganizationSecrets(organization_key) {
let api_path = `/organizations/${organization_key}/secret`;
this.console.debug(`API: GET ${this._baseURL}${api_path}`.gray);
return this._api.get(api_path)
.then(r => r.data)
.catch(this._handleError);
}
organizationSecretsExists(organization_key, secret_key, secret_version) {
let api_path = `/organizations/${organization_key}/secret/${secret_key}`;
if (secret_version)
api_path += `?secretVersion=${secret_version}`;
this.console.debug(`API: HEAD ${this._baseURL}${api_path}`.gray);
return this._api.head(api_path)
.then(r => r.data)
.catch(this._handleError);
}
setOrganizationSecret(organization_key, secret_key, secret_value) {
let api_path = `/organizations/${organization_key}/secret`;
this.console.debug(`API: PUT ${this._baseURL}${api_path}`.gray);
return this._api.put(api_path, { key: secret_key, secret: secret_value })
.then(r => r.data)
.catch(this._handleError);
}
getOrganizationServices(organization_key) {
let api_path = `/organizations/${organization_key}/services`;
this.console.debug(`API: GET ${this._baseURL}${api_path}`.gray);
return this._api.get(api_path)
.then(r => r.data.items)
.catch(this._handleError);
}
getOrganizationStories(organization_key) {
let api_path = `/organizations/${organization_key}/stories`;
this.console.debug(`API: GET ${this._baseURL}${api_path}`.gray);
return this._api.get(api_path)
.then(r => r.data.items)
.catch(this._handleError);
}
setOrganization(organization_key, organization_config) {
return __awaiter(this, void 0, void 0, function* () {
let api_path = `/organizations/${organization_key}`;
this.console.debug(`API: POST ${this._baseURL}${api_path}`.gray);
try {
return yield this._api.post(api_path, organization_config);
}
catch (err) {
return this._handleError(err);
}
});
}
exportOrganization(organization_key) {
let api_path = `/organizations/${organization_key}/export`;
this.console.debug(`API: GET ${this._baseURL}${api_path}`.gray);
return this._api.get(api_path)
.then(r => r.data)
.catch(this._handleError);
}
importOrganization(organization_key, organization_import_data) {
let api_path = `/organizations/${organization_key}`;
this.console.debug(`API: PATCH ${this._baseURL}${api_path}`.gray);
return this._api.patch(api_path, organization_import_data)
.catch(this._handleError);
}
validateFile(api_path, content, story, organization) {
return __awaiter(this, void 0, void 0, function* () {
this.console.debug(`API: POST ${this._baseURL}${api_path}`.gray);
try {
const r = yield this._api.post(api_path, { content, story, organization });
return r.data;
}
catch (err) {
return this._handleError(err);
}
});
}
getFile(api_path) {
this.console.debug(`API: GET ${this._baseURL}${api_path}`.gray);
return this._api.get(api_path)
.then(r => r.data.content)
.catch(this._handleError);
}
putFile(api_path, content) {
this.console.debug(`API: PUT {content} ${this._baseURL}${api_path}`.gray);
return this._api.put(api_path, { content })
.catch(this._handleError);
}
};
exports.ApiService = ApiService;
exports.ApiService = ApiService = __decorate([
(0, typedi_1.Service)(),
__metadata("design:paramtypes", [ConsoleService_1.ConsoleService,
ConventionService_1.ConventionService])
], ApiService);
//# sourceMappingURL=APIService.js.map