1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ActionNotFound = exports.SimpleAction = void 0;
|
4 | const qs = require("querystring");
|
5 | class 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 |
|
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 | }
|
44 | exports.SimpleAction = SimpleAction;
|
45 | class ActionNotFound extends Error {
|
46 | }
|
47 | exports.ActionNotFound = ActionNotFound;
|
48 |
|
\ | No newline at end of file |