UNPKG

@platform/cell.client

Version:

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

110 lines (109 loc) 4.42 kB
import { ERROR, Schema, util } from '../common'; import { upload } from './HttpClientCellFiles.upload'; export class HttpClientCellFiles { constructor(args) { this.args = args; this.uri = args.parent.uri; } static create(args) { return new HttpClientCellFiles(args); } static parsePath(path) { const index = path.lastIndexOf('/'); const filename = index < 0 ? path : path.substring(index + 1); const dir = index < 0 ? '' : path.substring(0, index); return { path, filename, dir }; } async urls() { var _a; const getError = () => `Failed to get file URLs for [${this.uri.toString()}]`; const base = await this.base({ getError }); const { ok, status, error } = base; if (!ok) { return util.toClientResponse(status, {}, { error }); } const toUrl = (args) => { const { path, uri, url } = args; const { filename, dir } = HttpClientCellFiles.parsePath(path); const res = { uri, filename, dir, path, url }; return res; }; const body = (_a = base.body.urls) === null || _a === void 0 ? void 0 : _a.files.map(item => toUrl(item)); return util.toClientResponse(status, body || []); } async map() { const getError = () => `Failed to get file map for [${this.uri.toString()}]`; const base = await this.base({ getError }); const { ok, status } = base; const body = ok ? base.body.files : {}; const error = base.error; return util.toClientResponse(status, body, { error }); } async list() { var _a; const getError = () => `Failed to get file list for [${this.uri.toString()}]`; const base = await this.base({ getError }); if (!base.ok) { const { status } = base; const body = {}; const error = base.error; return util.toClientResponse(status, body, { error }); } const urls = ((_a = base.body.urls) === null || _a === void 0 ? void 0 : _a.files) || []; const map = base.body.files || {}; const ns = this.uri.parts.ns; const body = Object.keys(map).reduce((acc, fileid) => { const value = map[fileid]; if (value) { const uri = Schema.uri.create.file(ns, fileid); const url = urls.find(item => item.uri === uri); if (url) { const { path, filename, dir } = HttpClientCellFiles.parsePath(url.path); acc.push(Object.assign({ uri, path, filename, dir }, value)); } } return acc; }, []); return util.toClientResponse(200, body); } async upload(input, options = {}) { const { changes } = options; const { http, urls } = this.args; const cellUri = this.uri.toString(); return upload({ input, http, urls, cellUri, changes }); } async delete(filename) { const urls = this.args.parent.url; const http = this.args.http; return deleteFiles({ http, urls, filename, action: 'DELETE' }); } async unlink(filename) { const urls = this.args.parent.url; const http = this.args.http; return deleteFiles({ http, urls, filename, action: 'UNLINK' }); } async base(args = {}) { const http = this.args.http; const parent = this.args.parent; const url = parent.url.files.list.toString(); const files = await http.get(url); if (!files.ok) { const status = files.status; const type = status === 404 ? ERROR.HTTP.NOT_FOUND : ERROR.HTTP.SERVER; const message = args.getError ? args.getError({ status }) : `Failed to get files data for '${this.uri.toString()}'.`; return util.toError(status, type, message); } const body = files.json; return util.toClientResponse(200, body); } } export async function deleteFiles(args) { const { urls, action, filename, http } = args; const filenames = Array.isArray(filename) ? filename : [filename]; const body = { filenames, action }; const url = urls.files.delete; const res = await http.delete(url.toString(), body); return util.fromHttpResponse(res).toClientResponse(); }