UNPKG

@platform/cell.client

Version:

A strongly typed HTTP client for operating with a CellOS service end-point.

186 lines (185 loc) 9.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var common_1 = require("../common"); var HttpClientCellFiles_upload_1 = require("./HttpClientCellFiles.upload"); var HttpClientCellFiles = (function () { function HttpClientCellFiles(args) { this.args = args; this.uri = args.parent.uri; } HttpClientCellFiles.create = function (args) { return new HttpClientCellFiles(args); }; HttpClientCellFiles.parsePath = function (path) { var index = path.lastIndexOf('/'); var filename = index < 0 ? path : path.substring(index + 1); var dir = index < 0 ? '' : path.substring(0, index); return { path: path, filename: filename, dir: dir }; }; HttpClientCellFiles.prototype.urls = function () { var _a; return tslib_1.__awaiter(this, void 0, void 0, function () { var getError, base, ok, status, error, toUrl, body; var _this = this; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: getError = function () { return "Failed to get file URLs for [" + _this.uri.toString() + "]"; }; return [4, this.base({ getError: getError })]; case 1: base = _b.sent(); ok = base.ok, status = base.status, error = base.error; if (!ok) { return [2, common_1.util.toClientResponse(status, {}, { error: error })]; } toUrl = function (args) { var path = args.path, uri = args.uri, url = args.url; var _a = HttpClientCellFiles.parsePath(path), filename = _a.filename, dir = _a.dir; var res = { uri: uri, filename: filename, dir: dir, path: path, url: url }; return res; }; body = (_a = base.body.urls) === null || _a === void 0 ? void 0 : _a.files.map(function (item) { return toUrl(item); }); return [2, common_1.util.toClientResponse(status, body || [])]; } }); }); }; HttpClientCellFiles.prototype.map = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var getError, base, ok, status, body, error; var _this = this; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: getError = function () { return "Failed to get file map for [" + _this.uri.toString() + "]"; }; return [4, this.base({ getError: getError })]; case 1: base = _a.sent(); ok = base.ok, status = base.status; body = ok ? base.body.files : {}; error = base.error; return [2, common_1.util.toClientResponse(status, body, { error: error })]; } }); }); }; HttpClientCellFiles.prototype.list = function () { var _a; return tslib_1.__awaiter(this, void 0, void 0, function () { var getError, base, status_1, body_1, error, urls, map, ns, body; var _this = this; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: getError = function () { return "Failed to get file list for [" + _this.uri.toString() + "]"; }; return [4, this.base({ getError: getError })]; case 1: base = _b.sent(); if (!base.ok) { status_1 = base.status; body_1 = {}; error = base.error; return [2, common_1.util.toClientResponse(status_1, body_1, { error: error })]; } urls = ((_a = base.body.urls) === null || _a === void 0 ? void 0 : _a.files) || []; map = base.body.files || {}; ns = this.uri.parts.ns; body = Object.keys(map).reduce(function (acc, fileid) { var value = map[fileid]; if (value) { var uri_1 = common_1.Schema.uri.create.file(ns, fileid); var url = urls.find(function (item) { return item.uri === uri_1; }); if (url) { var _a = HttpClientCellFiles.parsePath(url.path), path = _a.path, filename = _a.filename, dir = _a.dir; acc.push(tslib_1.__assign({ uri: uri_1, path: path, filename: filename, dir: dir }, value)); } } return acc; }, []); return [2, common_1.util.toClientResponse(200, body)]; } }); }); }; HttpClientCellFiles.prototype.upload = function (input, options) { if (options === void 0) { options = {}; } return tslib_1.__awaiter(this, void 0, void 0, function () { var changes, _a, http, urls, cellUri; return tslib_1.__generator(this, function (_b) { changes = options.changes; _a = this.args, http = _a.http, urls = _a.urls; cellUri = this.uri.toString(); return [2, HttpClientCellFiles_upload_1.upload({ input: input, http: http, urls: urls, cellUri: cellUri, changes: changes })]; }); }); }; HttpClientCellFiles.prototype.delete = function (filename) { return tslib_1.__awaiter(this, void 0, void 0, function () { var urls, http; return tslib_1.__generator(this, function (_a) { urls = this.args.parent.url; http = this.args.http; return [2, deleteFiles({ http: http, urls: urls, filename: filename, action: 'DELETE' })]; }); }); }; HttpClientCellFiles.prototype.unlink = function (filename) { return tslib_1.__awaiter(this, void 0, void 0, function () { var urls, http; return tslib_1.__generator(this, function (_a) { urls = this.args.parent.url; http = this.args.http; return [2, deleteFiles({ http: http, urls: urls, filename: filename, action: 'UNLINK' })]; }); }); }; HttpClientCellFiles.prototype.base = function (args) { if (args === void 0) { args = {}; } return tslib_1.__awaiter(this, void 0, void 0, function () { var http, parent, url, files, status_2, type, message, body; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: http = this.args.http; parent = this.args.parent; url = parent.url.files.list.toString(); return [4, http.get(url)]; case 1: files = _a.sent(); if (!files.ok) { status_2 = files.status; type = status_2 === 404 ? common_1.ERROR.HTTP.NOT_FOUND : common_1.ERROR.HTTP.SERVER; message = args.getError ? args.getError({ status: status_2 }) : "Failed to get files data for '" + this.uri.toString() + "'."; return [2, common_1.util.toError(status_2, type, message)]; } body = files.json; return [2, common_1.util.toClientResponse(200, body)]; } }); }); }; return HttpClientCellFiles; }()); exports.HttpClientCellFiles = HttpClientCellFiles; function deleteFiles(args) { return tslib_1.__awaiter(this, void 0, void 0, function () { var urls, action, filename, http, filenames, body, url, res; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: urls = args.urls, action = args.action, filename = args.filename, http = args.http; filenames = Array.isArray(filename) ? filename : [filename]; body = { filenames: filenames, action: action }; url = urls.files.delete; return [4, http.delete(url.toString(), body)]; case 1: res = _a.sent(); return [2, common_1.util.fromHttpResponse(res).toClientResponse()]; } }); }); } exports.deleteFiles = deleteFiles;