UNPKG

1.59 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ActionNotFound = exports.SimpleAction = void 0;
4const qs = require("querystring");
5class SimpleAction {
6 constructor(client, formInfo) {
7 this.client = client;
8 for (const [k, v] of Object.entries(formInfo)) {
9 this[k] = v;
10 }
11 }
12 /**
13 * Execute the action or submit the form.
14 */
15 async submit(formData) {
16 const uri = new URL(this.uri);
17 if (this.method === 'GET') {
18 uri.search = qs.stringify(formData);
19 const resource = this.client.go(uri.toString());
20 return resource.get();
21 }
22 let body;
23 switch (this.contentType) {
24 case 'application/x-www-form-urlencoded':
25 body = qs.stringify(formData);
26 break;
27 case 'application/json':
28 body = JSON.stringify(formData);
29 break;
30 default:
31 throw new Error(`Serializing mimetype ${this.contentType} is not yet supported in actions`);
32 }
33 const response = await this.client.fetcher.fetchOrThrow(uri.toString(), {
34 method: this.method,
35 body,
36 headers: {
37 'Content-Type': this.contentType
38 }
39 });
40 const state = this.client.getStateForResponse(uri.toString(), response);
41 return state;
42 }
43}
44exports.SimpleAction = SimpleAction;
45class ActionNotFound extends Error {
46}
47exports.ActionNotFound = ActionNotFound;
48//# sourceMappingURL=action.js.map
\No newline at end of file