@platform/cell.client
Version:
A strongly typed HTTP client for operating with a CellOS service end-point.
32 lines (31 loc) • 1.01 kB
JavaScript
import { Uri, util } from '../common';
export class HttpClientNs {
constructor(args) {
const { urls } = args;
this.args = args;
this.uri = Uri.parse(args.uri);
this.url = urls.ns(args.uri);
}
static create(args) {
return new HttpClientNs(args);
}
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 read(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 write(data, options = {}) {
const http = this.args.http;
const url = this.url.info.query(options).toString();
const res = await http.post(url, data);
return util.fromHttpResponse(res).toClientResponse();
}
}