ketting
Version:
Opiniated HATEAOS / Rest client.
44 lines • 1.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.factory = exports.HtmlState = void 0;
const base_state_1 = require("./base-state");
const util_1 = require("../http/util");
const html_1 = require("../util/html");
const link_1 = require("../link");
const uri_1 = require("../util/uri");
/**
* Represents a resource state in the HAL format
*/
class HtmlState extends base_state_1.BaseState {
serializeBody() {
return this.data;
}
clone() {
const state = new HtmlState(this.uri, this.data, new Headers(this.headers), new link_1.Links(this.uri, this.links), [], this.actionInfo);
state.client = this.client;
return state;
}
}
exports.HtmlState = HtmlState;
/**
* Turns a HTTP response into a HtmlState
*/
const factory = async (uri, response) => {
const body = await response.text();
const links = util_1.parseLink(uri, response.headers.get('Link'));
const htmlResult = html_1.parseHtml(uri, body);
links.add(...htmlResult.links);
return new HtmlState(uri, body, response.headers, links, [], htmlResult.forms.map(form => formToAction(uri, form)));
};
exports.factory = factory;
function formToAction(context, form) {
return {
uri: uri_1.resolve(context, form.action),
name: form.rel || form.id || '',
method: form.method || 'GET',
contentType: form.enctype || 'application/x-www-form-urlencoded',
// Fields are not yet supported :(
fields: [],
};
}
//# sourceMappingURL=html.js.map