@platform/cell.client
Version:
A strongly typed HTTP client for operating with a CellOS service end-point.
46 lines (45 loc) • 1.41 kB
JavaScript
import { Schema, Uri } from '../common';
import { HttpClientFile } from './HttpClientFile';
export class HttpClientCellLinks {
constructor(args) {
const { links = {} } = args;
this.args = args;
this.list = Object.keys(links).map((key) => this.toLink(key, links[key]));
}
static create(args) {
return new HttpClientCellLinks(args);
}
get files() {
return this.list.filter((item) => item.type === 'FILE');
}
toObject() {
return this.args.links;
}
toLink(key, value) {
const { http, urls } = this.args;
const uri = Uri.parse(value);
const type = uri.parts.type;
if (type === 'FILE') {
let file;
const link = Schema.file.links.parseValue(value);
const uri = link.uri;
const hash = link.query.hash || '';
const { name, dir, path } = Schema.file.links.parseKey(key);
const res = {
type: 'FILE',
uri: uri.toString(),
key,
path,
dir,
name,
hash,
get file() {
return file || (file = HttpClientFile.create({ uri, urls, http }));
},
};
return res;
}
const res = { type: 'UNKNOWN', key, uri: value };
return res;
}
}