@ply-ct/ply
Version:
REST API Automated Testing
94 lines • 2.76 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Retrieval = void 0;
const cross_fetch_1 = __importDefault(require("cross-fetch"));
const location_1 = require("./location");
const storage_1 = require("./storage");
/**
* Abstracts retrieval from either URL or {@link Storage}.
*/
class Retrieval {
/**
* @param location file path or url (backslashes are replaced with slashes)
*/
constructor(location) {
this.location = new location_1.Location(location);
}
async read() {
if (this.storage) {
return Promise.resolve(this.storage.read());
}
else {
const response = await (0, cross_fetch_1.default)(this.location.path);
return await response.text();
}
}
sync() {
if (this.storage) {
return this.storage.read();
}
else {
throw new Error('Retrieval.sync() not supported for remote path: ' + this);
}
}
get exists() {
if (this.storage) {
return Promise.resolve(this.storage.exists);
}
else {
try {
return (0, cross_fetch_1.default)(this.location.path, { method: 'HEAD' }).then((response) => {
return Promise.resolve(response.ok);
});
}
catch (_a) {
return Promise.resolve(false);
}
}
}
write(contents) {
if (this.storage) {
this.storage.write(contents);
}
else {
throw new Error('Retrieval.write() not supported for remote path: ' + this);
}
}
append(contents) {
if (this.storage) {
this.storage.append(contents);
}
else {
throw new Error('Retrieval.append() not supported for remote path: ' + this);
}
}
insert(contents, start) {
if (this.storage) {
this.storage.insert(contents, start);
}
else {
throw new Error('Retrieval.insert() not supported for remote path: ' + this);
}
}
remove() {
if (this.storage) {
this.storage.remove();
}
else {
throw new Error('Retrieval.remove() not supported for remote path: ' + this);
}
}
toString() {
return this.location.toString();
}
get storage() {
if (!this.location.isUrl) {
return new storage_1.Storage(this.location.path);
}
}
}
exports.Retrieval = Retrieval;
//# sourceMappingURL=retrieval.js.map