@sealcode/sealgen 0.13.0 → 0.14.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
- /// <reference types="koa__router" resolution-mode="require"/>
2
+ /// <reference types="koa__router" />
3
3
  import Router from "@koa/router";
4
4
  import { Context } from "koa";
5
5
  import { Field, FieldsetOutput } from "sealious";
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
2
  /// <reference types="node" resolution-mode="require"/>
3
- /// <reference types="koa__router" resolution-mode="require"/>
3
+ /// <reference types="koa__router" />
4
4
  import Router from "@koa/router";
5
5
  import { Context } from "koa";
6
6
  import { ParsedUrlQuery } from "querystring";
@@ -1,4 +1,4 @@
1
- /// <reference types="koa__router" resolution-mode="require"/>
1
+ /// <reference types="koa__router" />
2
2
  import Router from "@koa/router";
3
3
  import { Context } from "koa";
4
4
  import { FlatTemplatable } from "tempstream";
@@ -1,7 +1,7 @@
1
- /// <reference types="koa__router" resolution-mode="require"/>
1
+ /// <reference types="koa__router" />
2
2
  /// <reference types="node" resolution-mode="require"/>
3
3
  import Router from "@koa/router";
4
- import Sealious from "sealious";
4
+ import { CollectionItem, File as SealiousFile, Field as SealiousField, Collection } from "sealious";
5
5
  import { Context } from "koa";
6
6
  import { FlatTemplatable } from "tempstream";
7
7
  import { MultipleFiles as MultipleFilesField } from "../fields/multiple-files.js";
@@ -10,7 +10,7 @@ import { FormControlContext } from "./form-control.js";
10
10
  export type MultipleFilesOptions = {
11
11
  label?: string;
12
12
  uploadLabel?: string;
13
- getAdditionalFields: (ctx: Context, file: Sealious.File) => Promise<Record<string, unknown>>;
13
+ getAdditionalFields: (ctx: Context, file: SealiousFile) => Promise<Record<string, unknown>>;
14
14
  };
15
15
  export declare class MultipleFiles extends FormFieldControl {
16
16
  field: MultipleFilesField;
@@ -19,18 +19,18 @@ export declare class MultipleFiles extends FormFieldControl {
19
19
  getClassModifiers(): string[];
20
20
  mount(router: Router): void;
21
21
  deleteFileAssociation(ctx: Context, file_item_id: string): Promise<void>;
22
- addFileAssociation(ctx: Context, file: Sealious.File): Promise<void>;
22
+ addFileAssociation(ctx: Context, file: SealiousFile): Promise<void>;
23
23
  getFrameRelativeURL(): string;
24
24
  getFrameID(): string;
25
25
  renderFrame(ctx: Context, content?: FlatTemplatable): import("stream").Readable;
26
26
  render(fctx: FormControlContext): FlatTemplatable | Promise<FlatTemplatable>;
27
- getReferencingCollection(): Sealious.Collection;
28
- getFileField(): Sealious.Field | null;
29
- extractFileFromItem(item: Sealious.CollectionItem): Promise<Sealious.File | null>;
30
- renderFileItemPreview(_fileItem: Sealious.CollectionItem, file: Sealious.File): Promise<FlatTemplatable>;
31
- renderFileItem(fileItem: Sealious.CollectionItem, file: Sealious.File): Promise<FlatTemplatable>;
32
- renderFileRemoveButton(fileItem: Sealious.CollectionItem): FlatTemplatable;
33
- getFileItems(ctx: Context): Promise<Sealious.CollectionItem<any>[]>;
27
+ getReferencingCollection(): Collection;
28
+ getFileField(): SealiousField | null;
29
+ extractFileFromItem(item: CollectionItem): Promise<SealiousFile | null>;
30
+ renderFileItemPreview(_fileItem: CollectionItem, file: SealiousFile): Promise<FlatTemplatable>;
31
+ renderFileItem(fileItem: CollectionItem, file: SealiousFile): Promise<FlatTemplatable>;
32
+ renderFileRemoveButton(fileItem: CollectionItem): FlatTemplatable;
33
+ getFileItems(ctx: Context): Promise<CollectionItem<any>[]>;
34
34
  getInputAttributes(): {
35
35
  type: string;
36
36
  name: string;
@@ -1,14 +1,14 @@
1
1
  import { Context } from "koa";
2
- import Sealious from "sealious";
2
+ import { File as SealiousFile } from "sealious";
3
3
  import { FormDataValue } from "../form-types.js";
4
4
  import { FieldParseResult, FormField } from "./field.js";
5
5
  export type FileContainer = {
6
- old: Sealious.File | null;
7
- new: Sealious.File | null;
6
+ old: SealiousFile | null;
7
+ new: SealiousFile | null;
8
8
  };
9
9
  export declare class File<Required extends boolean> extends FormField<Required, FileContainer> {
10
10
  parse(ctx: Context, raw_value: FormDataValue): Promise<FieldParseResult<FileContainer>>;
11
- getDatabaseValue(ctx: Context, data: Record<string, FormDataValue>): Promise<Sealious.File | undefined>;
11
+ getDatabaseValue(ctx: Context, data: Record<string, FormDataValue>): Promise<SealiousFile | undefined>;
12
12
  getEmptyValue(): {
13
13
  old: null;
14
14
  new: null;
@@ -1,4 +1,4 @@
1
- /// <reference types="koa__router" resolution-mode="require"/>
1
+ /// <reference types="koa__router" />
2
2
  import { Context } from "koa";
3
3
  import Router from "@koa/router";
4
4
  import { FlatTemplatable } from "tempstream";
@@ -1,4 +1,4 @@
1
- /// <reference types="koa__router" resolution-mode="require"/>
1
+ /// <reference types="koa__router" />
2
2
  import Router from "@koa/router";
3
3
  import { Context } from "koa";
4
4
  import { FlatTemplatable } from "tempstream";
@@ -1,8 +1,8 @@
1
- import Sealious, { App } from "sealious";
1
+ import { App, Context as SealiousContext } from "sealious";
2
2
  import { FormDataValue } from "./forms/form-types.js";
3
3
  declare module "koa" {
4
4
  interface BaseContext {
5
- $context: Sealious.Context;
5
+ $context: SealiousContext;
6
6
  $app: App;
7
7
  $body: Record<string, FormDataValue>;
8
8
  }
@@ -1,4 +1,4 @@
1
- /// <reference types="koa__router" resolution-mode="require"/>
1
+ /// <reference types="koa__router" />
2
2
  import Router from "@koa/router";
3
3
  import { Mountable } from "./page/mountable.js";
4
4
  export declare function mount(router: Router, url: string | {
@@ -1,4 +1,4 @@
1
- /// <reference types="koa__router" resolution-mode="require"/>
1
+ /// <reference types="koa__router" />
2
2
  import Router from "@koa/router";
3
3
  import { Context } from "koa";
4
4
  export declare abstract class Mountable {
@@ -1,4 +1,4 @@
1
- /// <reference types="koa__router" resolution-mode="require"/>
1
+ /// <reference types="koa__router" />
2
2
  import Router from "@koa/router";
3
3
  import { Context } from "koa";
4
4
  import { FlatTemplatable } from "tempstream";
@@ -1,4 +1,4 @@
1
- /// <reference types="koa__router" resolution-mode="require"/>
1
+ /// <reference types="koa__router" />
2
2
  import Router from "@koa/router";
3
3
  import { BaseContext } from "koa";
4
4
  import { Templatable } from "tempstream";
@@ -7,6 +7,7 @@ import { Mountable } from "./mountable.js";
7
7
  export type StatefulPageActionDescription<ActionName> = {
8
8
  action: ActionName;
9
9
  label?: string;
10
+ content?: string;
10
11
  } | ActionName;
11
12
  export type StateAndMetadata<State, Actions> = {
12
13
  state: State;
@@ -34,6 +35,7 @@ export declare abstract class StatefulPage<State extends Record<string, unknown>
34
35
  extractActionAndLabel<ActionName extends keyof Actions>(action_description: StatefulPageActionDescription<ActionName>): {
35
36
  action: string;
36
37
  label: string;
38
+ content: string;
37
39
  };
38
40
  makeActionURL<ActionName extends keyof Actions>(action_description: StatefulPageActionDescription<ActionName>, ...args: ExtractTail<ExtractTail<Parameters<Actions[ActionName]>>>): string;
39
41
  makeActionButton<ActionName extends keyof Actions>(_state: State, action_description: StatefulPageActionDescription<ActionName>, ...args: ExtractTail<ExtractTail<Parameters<Actions[ActionName]>>>): string;
@@ -42,7 +44,8 @@ export declare abstract class StatefulPage<State extends Record<string, unknown>
42
44
  label?: string;
43
45
  } | ActionName, ...args: ExtractTail<ExtractTail<Parameters<Actions[ActionName]>>>): string;
44
46
  rerender(): string;
45
- preprocessRequestBody(values: Record<string, unknown>): Promise<StateAndMetadata<State, Actions>>;
47
+ preprocessState(values: State): Promise<State>;
48
+ preprocessOverrides(state: State, values: Record<string, unknown>): Promise<Record<string, unknown>>;
46
49
  extractState(ctx: BaseContext): Promise<StateAndMetadata<State, Actions>>;
47
50
  mount(router: Router, path: string): void;
48
51
  }
@@ -1,4 +1,6 @@
1
- import Sealious from "sealious";
1
+ import {
2
+ File as SealiousFile
3
+ } from "sealious";
2
4
  import { inputWrapper } from "../../utils/input-wrapper.js";
3
5
  import { tempstream } from "tempstream";
4
6
  import { FormFieldControl } from "./form-field-control.js";
@@ -105,7 +107,7 @@ class MultipleFiles extends FormFieldControl {
105
107
  if (!sealious_field) {
106
108
  return null;
107
109
  }
108
- return await Sealious.File.fromID(
110
+ return await SealiousFile.fromID(
109
111
  item.collection.app,
110
112
  item.get(sealious_field.name).id
111
113
  );
@@ -1,6 +1,6 @@
1
1
  import { tempstream } from "tempstream";
2
2
  import { File } from "./file.js";
3
- import Sealious from "sealious";
3
+ import { File as SealiousFile } from "sealious";
4
4
  class Photo extends File {
5
5
  constructor(field, imageRouter, options) {
6
6
  super(field, options);
@@ -20,7 +20,7 @@ class Photo extends File {
20
20
  if (!id) {
21
21
  return empty_container;
22
22
  }
23
- const file = await Sealious.File.fromID(ctx.$app, id);
23
+ const file = await SealiousFile.fromID(ctx.$app, id);
24
24
  return tempstream`<div class="${class_name}">
25
25
  ${this.imageRouter.image({
26
26
  resolutions: [100],
@@ -4,19 +4,19 @@ import {
4
4
  is,
5
5
  predicates
6
6
  } from "@sealcode/ts-predicates";
7
- import Sealious from "sealious";
7
+ import { File as SealiousFile } from "sealious";
8
8
  import { FormField } from "./field.js";
9
9
  class File extends FormField {
10
10
  async parse(ctx, raw_value) {
11
11
  let result;
12
- if (raw_value instanceof Sealious.File || is(raw_value, predicates.object) && hasShape(
12
+ if (raw_value instanceof SealiousFile || is(raw_value, predicates.object) && hasShape(
13
13
  {
14
14
  filename: predicates.string,
15
15
  id: predicates.string
16
16
  },
17
17
  raw_value
18
18
  ) && raw_value.id) {
19
- const file = await Sealious.File.fromID(
19
+ const file = await SealiousFile.fromID(
20
20
  ctx.$app,
21
21
  raw_value.id
22
22
  );
@@ -35,12 +35,12 @@ class File extends FormField {
35
35
  const old_file = hasFieldOfType(raw_value, "old", predicates.object) && hasShape(
36
36
  { id: predicates.string, filename: predicates.string },
37
37
  raw_value.old
38
- ) && raw_value.old.id ? await Sealious.File.fromID(ctx.$app, raw_value.old.id) : null;
38
+ ) && raw_value.old.id ? await SealiousFile.fromID(ctx.$app, raw_value.old.id) : null;
39
39
  const new_file = hasFieldOfType(
40
40
  raw_value,
41
41
  "new",
42
42
  predicates.array(predicates.object)
43
- ) && raw_value.new[0] instanceof Sealious.File ? raw_value.new[0] : null;
43
+ ) && raw_value.new[0] instanceof SealiousFile ? raw_value.new[0] : null;
44
44
  result = {
45
45
  parsable: true,
46
46
  error: null,
package/dist/mount.js CHANGED
@@ -1,4 +1,4 @@
1
- import Sealious from "sealious";
1
+ import { Middlewares as SealiousMiddlewares } from "sealious";
2
2
  async function handleHtmlPromise(ctx, next) {
3
3
  await next();
4
4
  if (ctx.body instanceof Promise) {
@@ -31,11 +31,11 @@ function mount(router, url, mountable, use_dummy_app = false) {
31
31
  };
32
32
  await next();
33
33
  },
34
- Sealious.Middlewares.parseBody(),
34
+ SealiousMiddlewares.parseBody(),
35
35
  handleHtmlPromise
36
36
  ] : [
37
- Sealious.Middlewares.extractContext(),
38
- Sealious.Middlewares.parseBody(),
37
+ SealiousMiddlewares.extractContext(),
38
+ SealiousMiddlewares.parseBody(),
39
39
  handleHtmlPromise
40
40
  ];
41
41
  router.use(raw_url, ...args);
@@ -1,4 +1,4 @@
1
- import { predicates, hasShape, is, hasField } from "@sealcode/ts-predicates";
1
+ import { predicates, hasShape, is } from "@sealcode/ts-predicates";
2
2
  import deepmerge from "deepmerge";
3
3
  import { tempstream } from "tempstream";
4
4
  import { from_base64, to_base64 } from "../utils/base64.js";
@@ -31,15 +31,17 @@ class StatefulPage extends Mountable {
31
31
  </form>`;
32
32
  }
33
33
  extractActionAndLabel(action_description) {
34
- let label, action;
34
+ let label, action, content;
35
35
  if (is(action_description, predicates.object)) {
36
36
  action = action_description.action.toString();
37
37
  label = action_description.label || action;
38
+ content = action_description.content || label;
38
39
  } else {
39
40
  action = action_description.toString();
40
41
  label = action;
42
+ content = label;
41
43
  }
42
- return { action, label };
44
+ return { action, label, content };
43
45
  }
44
46
  makeActionURL(action_description, ...args) {
45
47
  const { action } = this.extractActionAndLabel(action_description);
@@ -48,15 +50,17 @@ class StatefulPage extends Mountable {
48
50
  )}`;
49
51
  }
50
52
  makeActionButton(_state, action_description, ...args) {
51
- const { label } = this.extractActionAndLabel(action_description);
53
+ let { label, content } = this.extractActionAndLabel(action_description);
52
54
  return (
53
55
  /* HTML */
54
56
  `
55
- <input
57
+ <button
56
58
  type="submit"
57
- value="${label}"
58
59
  formaction="${this.makeActionURL(action_description, ...args)}"
59
- />
60
+ title="${label}"
61
+ >
62
+ ${content}
63
+ </button>
60
64
  `
61
65
  );
62
66
  }
@@ -69,7 +73,10 @@ class StatefulPage extends Mountable {
69
73
  rerender() {
70
74
  return "this.closest('form').requestSubmit()";
71
75
  }
72
- async preprocessRequestBody(values) {
76
+ async preprocessState(values) {
77
+ return values;
78
+ }
79
+ async preprocessOverrides(state, values) {
73
80
  return values;
74
81
  }
75
82
  async extractState(ctx) {
@@ -85,31 +92,50 @@ class StatefulPage extends Mountable {
85
92
  console.error("Wrong data: ", ctx.$body);
86
93
  throw new Error("wrong formdata shape");
87
94
  }
88
- const prepared_body = await this.preprocessRequestBody(ctx.$body);
89
95
  const inputs = Object.fromEntries(
90
- Object.entries(prepared_body).filter(
96
+ Object.entries(ctx.$body).filter(
91
97
  ([key]) => !["action", "state", "args", "$"].includes(key)
92
98
  )
93
99
  );
94
- const original_state_string = prepared_body.state;
100
+ const original_state_string = ctx.$body.state;
95
101
  const original_state = JSON.parse(
96
102
  typeof original_state_string == "string" ? from_base64(original_state_string) : "{}"
97
103
  );
98
- const modified_state = deepmerge(
104
+ const $body = ctx.$body;
105
+ let state_overrides = $body.$ || {};
106
+ state_overrides = await this.preprocessOverrides(
99
107
  original_state,
100
- hasField("$", prepared_body) ? prepared_body.$ : {},
101
- {
102
- arrayMerge: (_target, source, _options) => {
103
- return source;
104
- }
105
- }
108
+ state_overrides
106
109
  );
107
- if (prepared_body.action && prepared_body.action_args) {
110
+ let modified_state = deepmerge(original_state, state_overrides, {
111
+ arrayMerge: (target, source, options) => {
112
+ const destination = target.slice();
113
+ source.forEach((item, index) => {
114
+ if (typeof destination[index] === "undefined") {
115
+ destination[index] = options.cloneUnlessOtherwiseSpecified(
116
+ item,
117
+ options
118
+ );
119
+ } else if (options.isMergeableObject(item)) {
120
+ destination[index] = deepmerge(
121
+ target[index],
122
+ item,
123
+ options
124
+ );
125
+ } else if (target.indexOf(item) === -1) {
126
+ destination.push(item);
127
+ }
128
+ });
129
+ return destination;
130
+ }
131
+ });
132
+ modified_state = await this.preprocessState(modified_state);
133
+ if (ctx.$body.action && ctx.$body.action_args) {
108
134
  return {
109
135
  state: modified_state,
110
- action: prepared_body.action,
136
+ action: ctx.$body.action,
111
137
  inputs,
112
- action_args: prepared_body.action_args
138
+ action_args: ctx.$body.action_args
113
139
  };
114
140
  } else {
115
141
  return {
@@ -117,7 +143,7 @@ class StatefulPage extends Mountable {
117
143
  action: null,
118
144
  inputs,
119
145
  action_args: null,
120
- $: hasField("$", prepared_body) ? prepared_body.$ : {}
146
+ $: ctx.$body.$ || {}
121
147
  };
122
148
  }
123
149
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sealcode/sealgen",
3
- "version": "0.13.0",
3
+ "version": "0.14.1",
4
4
  "description": "Module to automate adding routes and collections to a sealious application",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -71,6 +71,6 @@
71
71
  "peerDependencies": {
72
72
  "koa": "^2.13.0",
73
73
  "koa-responsive-image-router": "^0.2.6",
74
- "sealious": "^0.17.50"
74
+ "sealious": "^0.18.1"
75
75
  }
76
76
  }
@@ -1,5 +1,10 @@
1
1
  import Router from "@koa/router";
2
- import Sealious from "sealious";
2
+ import {
3
+ CollectionItem,
4
+ File as SealiousFile,
5
+ Field as SealiousField,
6
+ Collection,
7
+ } from "sealious";
3
8
  import { Context } from "koa";
4
9
  import { inputWrapper } from "../../utils/input-wrapper.js";
5
10
  import { FlatTemplatable, tempstream } from "tempstream";
@@ -14,7 +19,7 @@ export type MultipleFilesOptions = {
14
19
  uploadLabel?: string;
15
20
  getAdditionalFields: (
16
21
  ctx: Context,
17
- file: Sealious.File
22
+ file: SealiousFile
18
23
  ) => Promise<Record<string, unknown>>;
19
24
  };
20
25
 
@@ -52,7 +57,7 @@ export class MultipleFiles extends FormFieldControl {
52
57
  );
53
58
 
54
59
  router.post(this.getFrameRelativeURL() + "/add", async (ctx) => {
55
- let files = ctx.$body.files as Sealious.File | Sealious.File[];
60
+ let files = ctx.$body.files as SealiousFile | SealiousFile[];
56
61
  if (!files || !is(files, predicates.object)) {
57
62
  ctx.body = "Missing files";
58
63
  return;
@@ -64,7 +69,7 @@ export class MultipleFiles extends FormFieldControl {
64
69
  files.map((file) =>
65
70
  this.addFileAssociation(
66
71
  ctx,
67
- file as unknown as Sealious.File
72
+ file as unknown as SealiousFile
68
73
  )
69
74
  )
70
75
  );
@@ -81,7 +86,7 @@ export class MultipleFiles extends FormFieldControl {
81
86
  ].removeByID(ctx.$context, file_item_id);
82
87
  }
83
88
 
84
- async addFileAssociation(ctx: Context, file: Sealious.File) {
89
+ async addFileAssociation(ctx: Context, file: SealiousFile) {
85
90
  const file_field = this.getFileField();
86
91
  if (!file_field) {
87
92
  throw new Error("No file field in referencing collection");
@@ -130,7 +135,7 @@ export class MultipleFiles extends FormFieldControl {
130
135
  return result;
131
136
  }
132
137
 
133
- getFileField(): Sealious.Field | null {
138
+ getFileField(): SealiousField | null {
134
139
  for (const [_, field] of Object.entries(
135
140
  this.getReferencingCollection().fields
136
141
  )) {
@@ -142,28 +147,28 @@ export class MultipleFiles extends FormFieldControl {
142
147
  }
143
148
 
144
149
  async extractFileFromItem(
145
- item: Sealious.CollectionItem
146
- ): Promise<Sealious.File | null> {
150
+ item: CollectionItem
151
+ ): Promise<SealiousFile | null> {
147
152
  const sealious_field = this.getFileField();
148
153
  if (!sealious_field) {
149
154
  return null;
150
155
  }
151
- return await Sealious.File.fromID(
152
- (item.collection as Sealious.Collection).app,
156
+ return await SealiousFile.fromID(
157
+ (item.collection as Collection).app,
153
158
  item.get(sealious_field.name).id
154
159
  );
155
160
  }
156
161
 
157
162
  async renderFileItemPreview(
158
- _fileItem: Sealious.CollectionItem,
159
- file: Sealious.File
163
+ _fileItem: CollectionItem,
164
+ file: SealiousFile
160
165
  ): Promise<FlatTemplatable> {
161
166
  return file.filename || "no filename";
162
167
  }
163
168
 
164
169
  async renderFileItem(
165
- fileItem: Sealious.CollectionItem,
166
- file: Sealious.File
170
+ fileItem: CollectionItem,
171
+ file: SealiousFile
167
172
  ): Promise<FlatTemplatable> {
168
173
  return tempstream/* HTML */ `<li class="file-list-item">
169
174
  <a
@@ -177,7 +182,7 @@ export class MultipleFiles extends FormFieldControl {
177
182
  </li>`;
178
183
  }
179
184
 
180
- renderFileRemoveButton(fileItem: Sealious.CollectionItem): FlatTemplatable {
185
+ renderFileRemoveButton(fileItem: CollectionItem): FlatTemplatable {
181
186
  return /* HTML */ ` <form
182
187
  data-turbo-frame="${this.getFrameID()}"
183
188
  action="${this.getFrameRelativeURL()}/delete/${fileItem.id}"
@@ -221,10 +226,7 @@ export class MultipleFiles extends FormFieldControl {
221
226
  await this.extractFileFromItem(item),
222
227
  ])
223
228
  )
224
- ).filter(([_, f]) => f !== null) as [
225
- Sealious.CollectionItem,
226
- Sealious.File
227
- ][];
229
+ ).filter(([_, f]) => f !== null) as [CollectionItem, SealiousFile][];
228
230
 
229
231
  return inputWrapper(
230
232
  ["multiple-files", this.field.name, ...this.getClassModifiers()],
@@ -1,7 +1,7 @@
1
1
  import { Context } from "koa";
2
2
  import { FlatTemplatable, tempstream } from "tempstream";
3
3
  import { File } from "./file.js";
4
- import Sealious from "sealious";
4
+ import { File as SealiousFile } from "sealious";
5
5
  import { FormField } from "../fields/field.js";
6
6
  import { SimpleInputOptions } from "./simple-input.js";
7
7
  import { FormControlContext } from "./form-control.js";
@@ -34,7 +34,7 @@ export class Photo extends File {
34
34
  if (!id) {
35
35
  return empty_container;
36
36
  }
37
- const file = await Sealious.File.fromID(ctx.$app, id);
37
+ const file = await SealiousFile.fromID(ctx.$app, id);
38
38
  return tempstream/* HTML */ `<div class="${class_name}">
39
39
  ${this.imageRouter.image({
40
40
  resolutions: [100],
@@ -5,13 +5,13 @@ import {
5
5
  predicates,
6
6
  } from "@sealcode/ts-predicates";
7
7
  import { Context } from "koa";
8
- import Sealious from "sealious";
8
+ import { File as SealiousFile } from "sealious";
9
9
  import { FormDataValue } from "../form-types.js";
10
10
  import { FieldParseResult, FormField } from "./field.js";
11
11
 
12
12
  export type FileContainer = {
13
- old: Sealious.File | null;
14
- new: Sealious.File | null;
13
+ old: SealiousFile | null;
14
+ new: SealiousFile | null;
15
15
  };
16
16
 
17
17
  export class File<Required extends boolean> extends FormField<
@@ -24,7 +24,7 @@ export class File<Required extends boolean> extends FormField<
24
24
  ): Promise<FieldParseResult<FileContainer>> {
25
25
  let result;
26
26
  if (
27
- raw_value instanceof Sealious.File ||
27
+ raw_value instanceof SealiousFile ||
28
28
  (is(raw_value, predicates.object) &&
29
29
  hasShape(
30
30
  {
@@ -35,7 +35,7 @@ export class File<Required extends boolean> extends FormField<
35
35
  ) &&
36
36
  raw_value.id)
37
37
  ) {
38
- const file = await Sealious.File.fromID(
38
+ const file = await SealiousFile.fromID(
39
39
  ctx.$app,
40
40
  raw_value.id as string
41
41
  );
@@ -58,14 +58,14 @@ export class File<Required extends boolean> extends FormField<
58
58
  raw_value.old
59
59
  ) &&
60
60
  raw_value.old.id
61
- ? await Sealious.File.fromID(ctx.$app, raw_value.old.id)
61
+ ? await SealiousFile.fromID(ctx.$app, raw_value.old.id)
62
62
  : null;
63
63
  const new_file =
64
64
  hasFieldOfType(
65
65
  raw_value,
66
66
  "new",
67
67
  predicates.array(predicates.object)
68
- ) && raw_value.new[0] instanceof Sealious.File
68
+ ) && raw_value.new[0] instanceof SealiousFile
69
69
  ? raw_value.new[0]
70
70
  : null;
71
71
  result = {
package/src/index.ts CHANGED
@@ -1,9 +1,9 @@
1
- import Sealious, { App } from "sealious";
1
+ import { App, Context as SealiousContext } from "sealious";
2
2
  import { FormDataValue } from "./forms/form-types.js";
3
3
 
4
4
  declare module "koa" {
5
5
  interface BaseContext {
6
- $context: Sealious.Context;
6
+ $context: SealiousContext;
7
7
  $app: App;
8
8
  $body: Record<string, FormDataValue>;
9
9
  }
package/src/mount.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import Router from "@koa/router";
2
2
  import Koa, { BaseContext, Context } from "koa";
3
- import Sealious from "sealious";
3
+ import { Middlewares as SealiousMiddlewares } from "sealious";
4
4
  import { Mountable } from "./page/mountable.js";
5
5
 
6
6
  async function handleHtmlPromise(ctx: Context, next: Koa.Next) {
@@ -48,12 +48,12 @@ export function mount(
48
48
  // eslint-disable-next-line @typescript-eslint/no-unsafe-call
49
49
  await next();
50
50
  },
51
- Sealious.Middlewares.parseBody(),
51
+ SealiousMiddlewares.parseBody(),
52
52
  handleHtmlPromise,
53
53
  ]
54
54
  : [
55
- Sealious.Middlewares.extractContext(),
56
- Sealious.Middlewares.parseBody(),
55
+ SealiousMiddlewares.extractContext(),
56
+ SealiousMiddlewares.parseBody(),
57
57
  handleHtmlPromise,
58
58
  ];
59
59
 
@@ -11,6 +11,7 @@ export type StatefulPageActionDescription<ActionName> =
11
11
  | {
12
12
  action: ActionName;
13
13
  label?: string;
14
+ content?: string;
14
15
  }
15
16
  | ActionName;
16
17
 
@@ -85,16 +86,18 @@ export abstract class StatefulPage<
85
86
 
86
87
  extractActionAndLabel<ActionName extends keyof Actions>(
87
88
  action_description: StatefulPageActionDescription<ActionName>
88
- ): { action: string; label: string } {
89
- let label, action: string;
89
+ ): { action: string; label: string; content: string } {
90
+ let label, action, content: string;
90
91
  if (is(action_description, predicates.object)) {
91
92
  action = action_description.action.toString();
92
93
  label = action_description.label || action;
94
+ content = action_description.content || label;
93
95
  } else {
94
96
  action = action_description.toString();
95
97
  label = action;
98
+ content = label;
96
99
  }
97
- return { action, label };
100
+ return { action, label, content };
98
101
  }
99
102
 
100
103
  makeActionURL<ActionName extends keyof Actions>(
@@ -112,13 +115,15 @@ export abstract class StatefulPage<
112
115
  action_description: StatefulPageActionDescription<ActionName>,
113
116
  ...args: ExtractTail<ExtractTail<Parameters<Actions[ActionName]>>>
114
117
  ) {
115
- const { label } = this.extractActionAndLabel(action_description);
118
+ let { label, content } = this.extractActionAndLabel(action_description);
116
119
  return /* HTML */ `
117
- <input
120
+ <button
118
121
  type="submit"
119
- value="${label}"
120
122
  formaction="${this.makeActionURL(action_description, ...args)}"
121
- />
123
+ title="${label}"
124
+ >
125
+ ${content}
126
+ </button>
122
127
  `;
123
128
  }
124
129
 
@@ -141,10 +146,15 @@ export abstract class StatefulPage<
141
146
  return "this.closest('form').requestSubmit()";
142
147
  }
143
148
 
144
- async preprocessRequestBody(
149
+ async preprocessState(values: State): Promise<State> {
150
+ return values;
151
+ }
152
+
153
+ async preprocessOverrides(
154
+ state: State,
145
155
  values: Record<string, unknown>
146
- ): Promise<StateAndMetadata<State, Actions>> {
147
- return values as StateAndMetadata<State, Actions>;
156
+ ): Promise<Record<string, unknown>> {
157
+ return values;
148
158
  }
149
159
 
150
160
  async extractState(
@@ -164,43 +174,66 @@ export abstract class StatefulPage<
164
174
  console.error("Wrong data: ", ctx.$body);
165
175
  throw new Error("wrong formdata shape");
166
176
  }
167
- // giving extending classes a change to modify body before furhter processing
168
- const prepared_body = await this.preprocessRequestBody(ctx.$body);
177
+
169
178
  const inputs = Object.fromEntries(
170
- Object.entries(prepared_body).filter(
179
+ Object.entries(ctx.$body).filter(
171
180
  ([key]) => !["action", "state", "args", "$"].includes(key)
172
181
  )
173
182
  ) as Record<string, string>;
174
183
  // the "$" key is parsed as dot notation and overrides the state
175
184
 
176
- const original_state_string = prepared_body.state;
185
+ const original_state_string = ctx.$body.state;
177
186
  const original_state = JSON.parse(
178
187
  typeof original_state_string == "string"
179
188
  ? from_base64(original_state_string)
180
189
  : "{}"
181
190
  );
182
191
 
183
- const modified_state = deepmerge(
192
+ const $body = ctx.$body;
193
+ let state_overrides = $body.$ || {};
194
+ state_overrides = await this.preprocessOverrides(
184
195
  original_state,
185
- hasField("$", prepared_body) ? prepared_body.$ : {},
186
- {
187
- arrayMerge: (
188
- _target: any[],
189
- source: any[],
190
- _options: ArrayMergeOptions
191
- ) => {
192
- // https://github.com/TehShrike/deepmerge#user-content-arraymerge-example-combine-arrays
193
- return source;
194
- },
195
- }
196
- ) as State;
196
+ state_overrides
197
+ );
198
+ let modified_state = deepmerge(original_state, state_overrides, {
199
+ arrayMerge: (
200
+ target: any[],
201
+ source: any[],
202
+ options: ArrayMergeOptions
203
+ ) => {
204
+ // https://github.com/TehShrike/deepmerge?tab=readme-ov-file#arraymerge-example-combine-arrays
205
+ const destination = target.slice();
206
+
207
+ source.forEach((item, index) => {
208
+ if (typeof destination[index] === "undefined") {
209
+ destination[index] =
210
+ options.cloneUnlessOtherwiseSpecified(
211
+ item,
212
+ options
213
+ );
214
+ } else if (options.isMergeableObject(item)) {
215
+ destination[index] = deepmerge(
216
+ target[index],
217
+ item,
218
+ options
219
+ );
220
+ } else if (target.indexOf(item) === -1) {
221
+ destination.push(item);
222
+ }
223
+ });
224
+ return destination;
225
+ },
226
+ }) as State;
227
+
228
+ // giving extending classes a change to modify the state before furhter processing
229
+ modified_state = await this.preprocessState(modified_state);
197
230
 
198
- if (prepared_body.action && prepared_body.action_args) {
231
+ if (ctx.$body.action && ctx.$body.action_args) {
199
232
  return {
200
233
  state: modified_state,
201
- action: prepared_body.action as string,
234
+ action: ctx.$body.action as string,
202
235
  inputs,
203
- action_args: prepared_body.action_args,
236
+ action_args: ctx.$body.action_args,
204
237
  };
205
238
  } else {
206
239
  return {
@@ -208,7 +241,7 @@ export abstract class StatefulPage<
208
241
  action: null,
209
242
  inputs,
210
243
  action_args: null,
211
- $: hasField("$", prepared_body) ? prepared_body.$ : {},
244
+ $: ctx.$body.$ || {},
212
245
  };
213
246
  }
214
247
  }