@gotamedia/oc
Version:
Set of OC helper for NodeJs Runtime.
75 lines (74 loc) • 3.56 kB
JavaScript
;
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.getImage = exports.getObject = void 0;
const errors_1 = require("../errors");
const configure_js_1 = require("../configure.js");
const getObject = (uuid) => __awaiter(void 0, void 0, void 0, function* () {
const { baseUrl, username, password } = configure_js_1.OC_SERVICE_CONFIG;
if (!uuid) {
throw new errors_1.ObjectsError("Missing required uuid for OC service, make sure to pass object uuid");
}
try {
const requestUrl = `${baseUrl}/objects/${uuid}`;
const credentials = Buffer.from(`${username}:${password}`).toString("base64");
const requestOptions = {
headers: {
Authorization: `Basic ${credentials}`
}
};
const response = yield fetch(requestUrl, requestOptions);
if (!response.ok) {
throw new errors_1.ObjectsError(`Could not fetch object ${uuid} from OpenContent`);
}
return yield response.text();
}
catch (error) {
throw new errors_1.ObjectsError(`Something went wrong while fetching object ${uuid} from OC`, {
cause: error
});
}
});
exports.getObject = getObject;
const getImage = (uuid) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
const { baseUrl, username, password } = configure_js_1.OC_SERVICE_CONFIG;
if (!uuid) {
throw new errors_1.ObjectsError("Missing required uuid for OC service, make sure to pass image uuid");
}
try {
const requestParams = {
headers: {
Authorization: `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`
}
};
const filesResponse = yield fetch(`${baseUrl}/objects/${uuid}/files`, requestParams);
if (!filesResponse.ok) {
throw new errors_1.ObjectsError(`Could not fetch image-files ${uuid} from OpenContent`);
}
const files = yield filesResponse.json();
const primaryFile = (_b = (_a = files["metadata"]) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b["filename"];
if (!primaryFile) {
throw new errors_1.ObjectsError(`Cannot determine primary filename when fetching image xml for ${uuid} from OpenContent`);
}
const imageXmlResponse = yield fetch(`${baseUrl}/objects/${uuid}/files/${primaryFile}`, requestParams);
if (!imageXmlResponse.ok) {
throw new errors_1.ObjectsError(`Could not fetch image-xml ${uuid}/files/${primaryFile} from OpenContent`);
}
return yield imageXmlResponse.text();
}
catch (error) {
throw new errors_1.ObjectsError(`Something went wrong while fetching image ${uuid} from OC`, {
cause: error
});
}
});
exports.getImage = getImage;