UNPKG

ketting

Version:

Opiniated HATEAOS / Rest client.

48 lines 1.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ActionNotFound = exports.SimpleAction = void 0; const qs = require("querystring"); class SimpleAction { constructor(client, formInfo) { this.client = client; for (const [k, v] of Object.entries(formInfo)) { this[k] = v; } } /** * Execute the action or submit the form. */ async submit(formData) { const uri = new URL(this.uri); if (this.method === 'GET') { uri.search = qs.stringify(formData); const resource = this.client.go(uri.toString()); return resource.get(); } let body; switch (this.contentType) { case 'application/x-www-form-urlencoded': body = qs.stringify(formData); break; case 'application/json': body = JSON.stringify(formData); break; default: throw new Error(`Serializing mimetype ${this.contentType} is not yet supported in actions`); } const response = await this.client.fetcher.fetchOrThrow(uri.toString(), { method: this.method, body, headers: { 'Content-Type': this.contentType } }); const state = this.client.getStateForResponse(uri.toString(), response); return state; } } exports.SimpleAction = SimpleAction; class ActionNotFound extends Error { } exports.ActionNotFound = ActionNotFound; //# sourceMappingURL=action.js.map