UNPKG

ketting

Version:

Opinionated HATEOAS / Rest client.

41 lines (33 loc) 1.1 kB
import { BaseState } from './base-state.js'; import { parseLink } from '../http/util.js'; import { parseHtml, HtmlForm } from '#html'; import { ActionInfo } from '../action.js'; import { resolve } from '../util/uri.js'; import { StateFactory } from './interface.js'; /** * Turns a HTTP response into a HtmlState */ export const factory:StateFactory = async (client, uri, response): Promise<BaseState<string>> => { const body = await response.text(); const links = parseLink(uri, response.headers.get('Link')); const htmlResult = parseHtml(uri, body); links.add(...htmlResult.links); return new BaseState({ client, uri, data: body, headers: response.headers, links, actions: htmlResult.forms.map(form => formToAction(uri, form)), }); }; function formToAction(context: string, form: HtmlForm): ActionInfo { return { uri: 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: [], }; }