@platform/cell.client
Version:
A strongly typed HTTP client for operating with a CellOS service end-point.
52 lines (51 loc) • 1.89 kB
JavaScript
import { Uri, util } from '../common';
import { HttpClientCellFile } from './HttpClientCellFile';
import { HttpClientCellFiles } from './HttpClientCellFiles';
import { HttpClientCellLinks } from './HttpClientCellLinks';
export class HttpClientCell {
constructor(args) {
const { urls } = args;
this.args = args;
this.uri = Uri.parse(args.uri);
this.url = urls.cell(args.uri);
}
static create(args) {
return new HttpClientCell(args);
}
get file() {
const urls = this.args.urls;
const http = this.args.http;
return this._file || (this._file = HttpClientCellFile.create({ parent: this, urls, http }));
}
get files() {
const urls = this.args.urls;
const http = this.args.http;
return this._files || (this._files = HttpClientCellFiles.create({ parent: this, urls, http }));
}
toString() {
return this.uri.toString();
}
async exists() {
const res = await this.args.http.get(this.url.info.toString());
return res.status.toString().startsWith('2');
}
async info(options = {}) {
const http = this.args.http;
const url = this.url.info.query(options).toString();
const res = await http.get(url);
return util.fromHttpResponse(res).toClientResponse();
}
async links() {
const info = await this.info();
if (info.error) {
const message = `Failed to get links for '${this.uri.toString()}'. ${info.error.message}`;
return util.toError(info.status, info.error.type, message);
}
const http = this.args.http;
const cell = info.body.data;
const links = cell.links || {};
const urls = this.args.urls;
const body = HttpClientCellLinks.create({ links, urls, http });
return util.toClientResponse(200, body);
}
}