@sealcode/sealgen 0.15.12 → 0.15.14

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.
@@ -16,7 +16,7 @@ export type UnboundControlStorage<F extends {
16
16
  export declare function UnboundControl<F extends {
17
17
  new (...args: any): FormFieldControl;
18
18
  }>(controlClass: F, arbitrary_field_name: string, params: ConstructorParameters<F>["1"]): UnboundControlStorage;
19
- export declare class StructuredArray<Subfields extends Record<string, Field>> extends FormFieldControl {
19
+ export declare class StructuredArray<Subfields extends Record<string, Field<any>>> extends FormFieldControl {
20
20
  field: StructuredArrayField<Subfields>;
21
21
  item_controls: Array<FormControl | UnboundControlStorage>;
22
22
  constructor(field: StructuredArrayField<Subfields>, item_controls: Array<FormControl | UnboundControlStorage>);
@@ -26,7 +26,7 @@ export declare class MultipleFiles extends FormFieldControl {
26
26
  renderFrame(_ctx: Context, content?: FlatTemplatable): import("stream").Readable;
27
27
  render(fctx: FormControlContext): FlatTemplatable | Promise<FlatTemplatable>;
28
28
  getReferencingCollection(): Collection;
29
- getFileField(): SealiousField | null;
29
+ getFileField(): SealiousField<any> | null;
30
30
  extractFileFromItem(item: CollectionItem<Collection>): Promise<FilePointer | null>;
31
31
  renderFileItemPreview(_fileItem: CollectionItem, file: FilePointer): Promise<FlatTemplatable>;
32
32
  renderFileItem(fileItem: CollectionItem, file: FilePointer): Promise<FlatTemplatable>;
@@ -1,15 +1,15 @@
1
1
  import { Context } from "koa";
2
- import { Depromisify, Field, FieldOutput } from "sealious";
2
+ import { ExtractFieldDecoded, Field } from "sealious";
3
3
  import { FormDataValue } from "../form-types.js";
4
4
  import { FieldParseResult, FormField, FormFieldValidationResponse } from "./field.js";
5
- export declare class CollectionField<F extends Field, Required extends boolean> extends FormField<Required, FieldOutput<F> | null> {
5
+ export declare class CollectionField<F extends Field<any>, Required extends boolean> extends FormField<Required, ExtractFieldDecoded<F> | null> {
6
6
  sealiousField: F;
7
7
  constructor(required: Required, sealiousField: F);
8
8
  predicate: (v: unknown, w: unknown) => {
9
9
  readonly match: true;
10
10
  readonly wildcard: unknown;
11
11
  };
12
- parse(ctx: Context, raw_value: FormDataValue): Promise<FieldParseResult<FieldOutput<F> | null>>;
13
- isValueValid(ctx: Context, value: FieldOutput<F>): Promise<FormFieldValidationResponse>;
14
- getEmptyValue(): Depromisify<ReturnType<F["decode"]>>;
12
+ parse(ctx: Context, raw_value: FormDataValue): Promise<FieldParseResult<ExtractFieldDecoded<F> | null>>;
13
+ isValueValid(ctx: Context, value: ExtractFieldDecoded<F>): Promise<FormFieldValidationResponse>;
14
+ getEmptyValue(): ExtractFieldDecoded<F>;
15
15
  }
@@ -1,6 +1,6 @@
1
1
  import { Field } from "sealious";
2
2
  import { PickFromListField } from "./pick-from-list.js";
3
3
  export declare class ExistingSealiousValue<Required extends boolean> extends PickFromListField<Required> {
4
- constructor(required: Required, field: Field);
4
+ constructor(required: Required, field: Field<unknown>);
5
5
  getEmptyValue(): string;
6
6
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -2,7 +2,7 @@ import { Context } from "koa";
2
2
  import { Field, FieldsetOutput, FieldTypes } from "sealious";
3
3
  import { FormDataValue } from "../form-types.js";
4
4
  import { FormField } from "./field.js";
5
- export declare class StructuredArrayField<Fields extends Record<string, Field>> extends FormField<true, FieldTypes.ArrayStorageInput<FieldsetOutput<Fields>>> {
5
+ export declare class StructuredArrayField<Fields extends Record<string, Field<unknown>>> extends FormField<true, FieldTypes.ArrayStorageInput<FieldsetOutput<Fields>>> {
6
6
  sealious_field: FieldTypes.StructuredArray<Fields>;
7
7
  constructor(sealious_field: FieldTypes.StructuredArray<Fields>);
8
8
  parse(_context: Context, value: FormDataValue): Promise<{
@@ -1,6 +1,6 @@
1
1
  import { Field } from "sealious";
2
2
  import { FlatTemplatable } from "tempstream";
3
- export type ListFilterRender = (value: string, field: Field) => FlatTemplatable;
4
- export declare const BooleanListFilter: (value: string, field: Field) => string;
5
- export declare const SimpleTextFilter: (value: string, field: Field) => string;
3
+ export type ListFilterRender = (value: string, field: Field<any>) => FlatTemplatable;
4
+ export declare const BooleanListFilter: (value: string, field: Field<any>) => string;
5
+ export declare const SimpleTextFilter: (value: string, field: Field<any>) => string;
6
6
  export declare const DefaultListFilters: Record<string, ListFilterRender>;
@@ -158,7 +158,10 @@ class StructuredArray extends FormFieldControl {
158
158
  ctx.body = this._render(
159
159
  ctx,
160
160
  form_id,
161
- await item.getDecodedBody(ctx.$context, {})
161
+ await item.getDecodedBody(
162
+ ctx.$context,
163
+ {}
164
+ )
162
165
  );
163
166
  });
164
167
  }
@@ -87,6 +87,7 @@ class MultipleFiles extends FormFieldControl {
87
87
  const result = this.field.collection_field.app.collections[this.field.collection_field.referencing_collection];
88
88
  return result;
89
89
  }
90
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
90
91
  getFileField() {
91
92
  for (const [_, field] of Object.entries(
92
93
  this.getReferencingCollection().fields
@@ -0,0 +1,58 @@
1
+ import Router from "@koa/router";
2
+ import getPort from "get-port";
3
+ import Koa from "koa";
4
+ import { mount } from "../../mount.js";
5
+ import { Form } from "../form.js";
6
+ import { getBrowser } from "../../utils/browser-creator.js";
7
+ import { SimpleFormField } from "./simple-form-field.js";
8
+ import { SimpleInput } from "../controls/simple-input.js";
9
+ describe("basic field", () => {
10
+ describe("basic tests", async () => {
11
+ let server;
12
+ let browser;
13
+ let page;
14
+ afterEach(() => {
15
+ server.close();
16
+ });
17
+ it("sends the values", async () => {
18
+ const port = await getPort();
19
+ console.log(`Using port ${port} form checkboxed-list-input.test.ts`);
20
+ const app = new Koa();
21
+ const router = new Router();
22
+ const fields = {
23
+ text: new SimpleFormField(true)
24
+ };
25
+ let received_values;
26
+ mount(
27
+ router,
28
+ "/",
29
+ new class extends Form {
30
+ constructor() {
31
+ super(...arguments);
32
+ this.fields = fields;
33
+ this.controls = [new SimpleInput(fields.text)];
34
+ }
35
+ async onSubmit(ctx, data) {
36
+ received_values = await this.getParsedValues(ctx);
37
+ const { parsed: email } = await this.fields.text.getValue(
38
+ ctx,
39
+ data.raw_values
40
+ );
41
+ email?.padStart(2, "0");
42
+ }
43
+ async canAccess(_) {
44
+ return { canAccess: true, message: "" };
45
+ }
46
+ }(),
47
+ true
48
+ );
49
+ app.use(router.routes()).use(router.allowedMethods());
50
+ server = app.listen(port);
51
+ browser = await getBrowser();
52
+ const context = await browser.newContext();
53
+ page = await context.newPage();
54
+ await page.goto(`http://localhost:${port}`);
55
+ });
56
+ });
57
+ });
58
+ //# sourceMappingURL=field.test.js.map
@@ -65,7 +65,9 @@ ${files.map(
65
65
  ).join("\n")}
66
66
 
67
67
  export default function mountAutoRoutes(router: Router) {
68
- ${files.map(
68
+ ${files.sort(
69
+ ({ url: url1 }, { url: url2 }) => url1.includes(":") ? 1 : url2.includes(":") ? -1 : url1 < url2 ? -1 : 1
70
+ ).map(
69
71
  ({ actionName }) => ` mount(router, URLs.${actionName}URL, ${actionName});`
70
72
  ).join("\n")}
71
73
  }`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sealcode/sealgen",
3
- "version": "0.15.12",
3
+ "version": "0.15.14",
4
4
  "description": "Module to automate adding routes and collections to a sealious application",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",
@@ -80,6 +80,6 @@
80
80
  "peerDependencies": {
81
81
  "koa": "^2.13.0",
82
82
  "koa-responsive-image-router": "^0.2.24",
83
- "sealious": "^0.19.7"
83
+ "sealious": "^0.19.8"
84
84
  }
85
85
  }
@@ -30,7 +30,7 @@ export function UnboundControl<
30
30
  }
31
31
 
32
32
  export class StructuredArray<
33
- Subfields extends Record<string, Field>
33
+ Subfields extends Record<string, Field<any>>
34
34
  > extends FormFieldControl {
35
35
  constructor(
36
36
  public field: StructuredArrayField<Subfields>,
@@ -224,7 +224,10 @@ export class StructuredArray<
224
224
  ctx.body = this._render(
225
225
  ctx,
226
226
  form_id,
227
- await item.getDecodedBody(ctx.$context, {})
227
+ (await item.getDecodedBody(
228
+ ctx.$context,
229
+ {}
230
+ )) as unknown as Record<string, FormDataValue>
228
231
  );
229
232
  });
230
233
  }
@@ -128,7 +128,8 @@ export class MultipleFiles extends FormFieldControl {
128
128
  return result;
129
129
  }
130
130
 
131
- getFileField(): SealiousField | null {
131
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
132
+ getFileField(): SealiousField<any> | null {
132
133
  for (const [_, field] of Object.entries(
133
134
  this.getReferencingCollection().fields
134
135
  )) {
@@ -1,6 +1,6 @@
1
1
  import { predicates } from "@sealcode/ts-predicates";
2
2
  import { Context } from "koa";
3
- import { Depromisify, Field, FieldOutput } from "sealious";
3
+ import { Depromisify, ExtractFieldDecoded, Field } from "sealious";
4
4
  import { FormDataValue } from "../form-types.js";
5
5
 
6
6
  import {
@@ -10,9 +10,10 @@ import {
10
10
  } from "./field.js";
11
11
 
12
12
  export class CollectionField<
13
- F extends Field,
13
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
+ F extends Field<any>,
14
15
  Required extends boolean
15
- > extends FormField<Required, FieldOutput<F> | null> {
16
+ > extends FormField<Required, ExtractFieldDecoded<F> | null> {
16
17
  constructor(required: Required, public sealiousField: F) {
17
18
  super(required);
18
19
  }
@@ -22,7 +23,7 @@ export class CollectionField<
22
23
  async parse(
23
24
  ctx: Context,
24
25
  raw_value: FormDataValue
25
- ): Promise<FieldParseResult<FieldOutput<F> | null>> {
26
+ ): Promise<FieldParseResult<ExtractFieldDecoded<F> | null>> {
26
27
  if (raw_value === undefined) {
27
28
  return { parsable: true, parsed_value: null, error: null };
28
29
  }
@@ -45,7 +46,7 @@ export class CollectionField<
45
46
 
46
47
  public async isValueValid(
47
48
  ctx: Context,
48
- value: FieldOutput<F>
49
+ value: ExtractFieldDecoded<F>
49
50
  ): Promise<FormFieldValidationResponse> {
50
51
  const { valid, reason } = await this.sealiousField.checkValue(
51
52
  ctx.$context,
@@ -56,7 +57,8 @@ export class CollectionField<
56
57
  return { valid, message: reason || (!valid ? "Wrong value" : "") };
57
58
  }
58
59
 
59
- public getEmptyValue(): Depromisify<ReturnType<F["decode"]>> {
60
- return "" as unknown as Depromisify<ReturnType<F["decode"]>>;
60
+ public getEmptyValue(): ExtractFieldDecoded<F> {
61
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
62
+ return "" as unknown as ExtractFieldDecoded<F>;
61
63
  }
62
64
  }
@@ -4,13 +4,13 @@ import { PickFromListField } from "./pick-from-list.js";
4
4
  export class ExistingSealiousValue<
5
5
  Required extends boolean
6
6
  > extends PickFromListField<Required> {
7
- constructor(required: Required, field: Field) {
7
+ constructor(required: Required, field: Field<unknown>) {
8
8
  super(required, async (ctx) => {
9
9
  const { items } = await field.collection.list(ctx.$context).fetch();
10
10
  // eslint-disable-next-line @typescript-eslint/no-unsafe-return
11
11
  return Object.fromEntries(
12
12
  items.map((item) => {
13
- const value = item.get(field.name);
13
+ const value = item.get(field.name) as unknown;
14
14
  return [value, value];
15
15
  })
16
16
  );
@@ -0,0 +1,67 @@
1
+ import Router from "@koa/router";
2
+ import getPort from "get-port";
3
+ import { Browser, Page } from "playwright";
4
+ import type { Context } from "koa";
5
+ import Koa from "koa";
6
+ import { mount } from "../../mount.js";
7
+ import { Form } from "../form.js";
8
+ import { getBrowser } from "../../utils/browser-creator.js";
9
+ import assert from "assert";
10
+ import { SimpleFormField } from "./simple-form-field.js";
11
+ import { SimpleInput } from "../controls/simple-input.js";
12
+ import { FormData } from "../form-types.js";
13
+
14
+ describe("basic field", () => {
15
+ describe("basic tests", async () => {
16
+ let server: ReturnType<Koa["listen"]>;
17
+ let browser: Browser;
18
+ let page: Page;
19
+
20
+ afterEach(() => {
21
+ server.close();
22
+ });
23
+
24
+ it("sends the values", async () => {
25
+ const port = await getPort();
26
+ console.log(`Using port ${port} form checkboxed-list-input.test.ts`);
27
+ const app = new Koa();
28
+ const router = new Router();
29
+
30
+ const fields = {
31
+ text: new SimpleFormField(true),
32
+ };
33
+
34
+ let received_values: unknown;
35
+
36
+ mount(
37
+ router,
38
+ "/",
39
+ new (class extends Form<typeof fields, unknown> {
40
+ fields = fields;
41
+ controls = [new SimpleInput(fields.text)];
42
+
43
+ async onSubmit(ctx: Context, data: FormData) {
44
+ received_values = await this.getParsedValues(ctx);
45
+ const { parsed: email } = await this.fields.text.getValue(
46
+ ctx,
47
+ data.raw_values
48
+ );
49
+ email?.padStart(2, "0"); // if the typechecks are ok, this will not throw
50
+ }
51
+
52
+ async canAccess(_: Context) {
53
+ return { canAccess: true, message: "" };
54
+ }
55
+ })(),
56
+ true
57
+ );
58
+ app.use(router.routes()).use(router.allowedMethods());
59
+ server = app.listen(port);
60
+
61
+ browser = await getBrowser();
62
+ const context = await browser.newContext();
63
+ page = await context.newPage();
64
+ await page.goto(`http://localhost:${port}`);
65
+ });
66
+ });
67
+ });
@@ -4,7 +4,7 @@ import { FormDataValue } from "../form-types.js";
4
4
  import { FormField } from "./field.js";
5
5
 
6
6
  export class StructuredArrayField<
7
- Fields extends Record<string, Field>
7
+ Fields extends Record<string, Field<unknown>>
8
8
  > extends FormField<
9
9
  true,
10
10
  FieldTypes.ArrayStorageInput<FieldsetOutput<Fields>>
@@ -90,6 +90,16 @@ ${files
90
90
 
91
91
  export default function mountAutoRoutes(router: Router) {
92
92
  ${files
93
+
94
+ .sort(({ url: url1 }, { url: url2 }) =>
95
+ url1.includes(":") // make sure the parametrized ones come later, so the more concrete ones show up first. This is not a full solution, but works for single-parameter urls
96
+ ? 1
97
+ : url2.includes(":")
98
+ ? -1
99
+ : url1 < url2
100
+ ? -1
101
+ : 1
102
+ )
93
103
  .map(
94
104
  ({ actionName }) =>
95
105
  ` mount(router, URLs.${actionName}URL, ${actionName});`
@@ -1,9 +1,12 @@
1
1
  import { Field } from "sealious";
2
2
  import { FlatTemplatable } from "tempstream";
3
3
 
4
- export type ListFilterRender = (value: string, field: Field) => FlatTemplatable;
4
+ export type ListFilterRender = (
5
+ value: string,
6
+ field: Field<any>
7
+ ) => FlatTemplatable;
5
8
 
6
- export const BooleanListFilter = (value: string, field: Field) => {
9
+ export const BooleanListFilter = (value: string, field: Field<any>) => {
7
10
  return /* HTML */ `<label>
8
11
  ${field.name}
9
12
  <select name="filter[${field.name}]" autocomplete="off">
@@ -28,7 +31,7 @@ export const BooleanListFilter = (value: string, field: Field) => {
28
31
  </label>`;
29
32
  };
30
33
 
31
- export const SimpleTextFilter = (value: string, field: Field) => {
34
+ export const SimpleTextFilter = (value: string, field: Field<any>) => {
32
35
  return /* HTML */ `<label>
33
36
  ${field.name}
34
37
  <input