@sealcode/sealgen 0.3.1 → 0.4.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.
Files changed (111) hide show
  1. package/@types/forms/controls/checkboxed-list-input.d.ts +5 -6
  2. package/@types/forms/controls/dropdown.d.ts +5 -6
  3. package/@types/forms/controls/editable-collection-subset.d.ts +6 -6
  4. package/@types/forms/controls/form-control.d.ts +3 -4
  5. package/@types/forms/controls/form-field-control.d.ts +9 -8
  6. package/@types/forms/controls/frame.d.ts +2 -2
  7. package/@types/forms/controls/simple-input.d.ts +5 -5
  8. package/@types/forms/field.d.ts +47 -16
  9. package/@types/forms/fields/checkboxed-list.d.ts +16 -0
  10. package/@types/forms/fields/collection-field.d.ts +11 -0
  11. package/@types/forms/fields/field.d.ts +38 -0
  12. package/@types/forms/fields/number.d.ts +13 -0
  13. package/@types/forms/fields/pick-from-list.d.ts +16 -0
  14. package/@types/forms/fields/simple-form-field.d.ts +7 -0
  15. package/@types/forms/form-fields-list.d.ts +1 -1
  16. package/@types/forms/form.d.ts +23 -28
  17. package/@types/forms/simple-form-field.d.ts +6 -0
  18. package/@types/forms/validator.d.ts +0 -3
  19. package/@types/index.d.ts +6 -4
  20. package/@types/make-hidden-inputs.d.ts +4 -0
  21. package/@types/page/list.d.ts +23 -42
  22. package/@types/page/mountable.d.ts +36 -0
  23. package/@types/page/page.d.ts +9 -16
  24. package/@types/page/props-parser.d.ts +5 -10
  25. package/@types/templates/page.d.ts +1 -1
  26. package/lib/build.js +12 -3
  27. package/lib/build.js.map +1 -1
  28. package/lib/forms/controls/checkboxed-list-input.js +10 -17
  29. package/lib/forms/controls/checkboxed-list-input.js.map +1 -1
  30. package/lib/forms/controls/dropdown.js +16 -20
  31. package/lib/forms/controls/dropdown.js.map +1 -1
  32. package/lib/forms/controls/editable-collection-subset.js +33 -35
  33. package/lib/forms/controls/editable-collection-subset.js.map +1 -1
  34. package/lib/forms/controls/form-control.js.map +1 -1
  35. package/lib/forms/controls/form-field-control.js +2 -11
  36. package/lib/forms/controls/form-field-control.js.map +1 -1
  37. package/lib/forms/controls/simple-input.js +16 -22
  38. package/lib/forms/controls/simple-input.js.map +1 -1
  39. package/lib/forms/field.js +96 -20
  40. package/lib/forms/field.js.map +1 -1
  41. package/lib/forms/fields/checkboxed-list.js +43 -0
  42. package/lib/forms/fields/checkboxed-list.js.map +1 -0
  43. package/lib/forms/fields/collection-field.js +28 -0
  44. package/lib/forms/fields/collection-field.js.map +1 -0
  45. package/lib/forms/fields/field.js +66 -0
  46. package/lib/forms/fields/field.js.map +1 -0
  47. package/lib/forms/fields/number.js +45 -0
  48. package/lib/forms/fields/number.js.map +1 -0
  49. package/lib/forms/fields/pick-from-list.js +34 -0
  50. package/lib/forms/fields/pick-from-list.js.map +1 -0
  51. package/lib/forms/fields/simple-form-field.js +18 -0
  52. package/lib/forms/fields/simple-form-field.js.map +1 -0
  53. package/lib/forms/form.js +50 -96
  54. package/lib/forms/form.js.map +1 -1
  55. package/lib/forms/simple-form-field.js +18 -0
  56. package/lib/forms/simple-form-field.js.map +1 -0
  57. package/lib/forms/validator.js +0 -10
  58. package/lib/forms/validator.js.map +1 -1
  59. package/lib/generate-routes.js +18 -3
  60. package/lib/generate-routes.js.map +1 -1
  61. package/lib/index.js +4 -3
  62. package/lib/index.js.map +1 -1
  63. package/lib/make-hidden-inputs.js +17 -0
  64. package/lib/make-hidden-inputs.js.map +1 -0
  65. package/lib/page/list.js +64 -79
  66. package/lib/page/list.js.map +1 -1
  67. package/lib/page/mountable.js +56 -0
  68. package/lib/page/mountable.js.map +1 -0
  69. package/lib/page/page.js +11 -1
  70. package/lib/page/page.js.map +1 -1
  71. package/lib/page/props-parser.js +4 -47
  72. package/lib/page/props-parser.js.map +1 -1
  73. package/lib/templates/form.js +38 -18
  74. package/lib/templates/form.js.map +1 -1
  75. package/lib/templates/list.js +30 -20
  76. package/lib/templates/list.js.map +1 -1
  77. package/lib/templates/page.js +5 -4
  78. package/lib/templates/page.js.map +1 -1
  79. package/lib/utils/escape-url-params.js +1 -0
  80. package/lib/utils/escape-url-params.js.map +1 -1
  81. package/package.json +4 -3
  82. package/src/build.ts +14 -3
  83. package/src/forms/controls/checkboxed-list-input.ts +12 -23
  84. package/src/forms/controls/dropdown.ts +27 -32
  85. package/src/forms/controls/editable-collection-subset.ts +57 -54
  86. package/src/forms/controls/form-control.ts +5 -6
  87. package/src/forms/controls/form-field-control.ts +13 -29
  88. package/src/forms/controls/frame.ts +2 -2
  89. package/src/forms/controls/simple-input.ts +26 -23
  90. package/src/forms/fields/checkboxed-list.ts +61 -0
  91. package/src/forms/fields/collection-field.ts +54 -0
  92. package/src/forms/fields/field.ts +106 -0
  93. package/src/forms/fields/number.ts +53 -0
  94. package/src/forms/fields/pick-from-list.ts +49 -0
  95. package/src/forms/fields/simple-form-field.ts +20 -0
  96. package/src/forms/form.ts +97 -162
  97. package/src/generate-routes.ts +18 -7
  98. package/src/index.ts +6 -4
  99. package/src/make-hidden-inputs.ts +22 -0
  100. package/src/page/list.ts +113 -126
  101. package/src/page/mountable.ts +105 -0
  102. package/src/page/page.ts +20 -11
  103. package/src/page/props-parser.ts +9 -67
  104. package/src/templates/form.ts +38 -18
  105. package/src/templates/list.ts +30 -20
  106. package/src/templates/page.ts +6 -6
  107. package/src/utils/escape-url-params.ts +1 -0
  108. package/src/forms/controls.ts +0 -0
  109. package/src/forms/field.ts +0 -119
  110. package/src/forms/form-fields-list.ts +0 -7
  111. package/src/forms/validator.ts +0 -10
@@ -1,16 +1,15 @@
1
1
  /// <reference types="node" />
2
2
  import { BaseContext } from "koa";
3
- import { FormField } from "../field";
4
- import { Form, FormData } from "../form";
3
+ import { ChekboxedListField } from "../fields/field";
4
+ import { FormDataValue } from "../form";
5
5
  import { FormFieldControl } from "./controls";
6
6
  export declare class CheckboxedListInput extends FormFieldControl {
7
- fieldname: string;
7
+ field: ChekboxedListField;
8
8
  options: {
9
9
  label: string;
10
10
  };
11
- constructor(fieldname: string, options?: {
11
+ constructor(field: ChekboxedListField, options?: {
12
12
  label: string;
13
13
  });
14
- isValidFieldName(form: Form): boolean;
15
- _render(ctx: BaseContext, fields: FormField[], data: FormData): Promise<import("stream").Readable>;
14
+ render(ctx: BaseContext, data: Record<string, FormDataValue>): Promise<import("stream").Readable>;
16
15
  }
@@ -1,20 +1,19 @@
1
1
  /// <reference types="node" />
2
2
  import { BaseContext } from "koa";
3
- import { FormField } from "../field";
4
- import { FormData } from "../form";
3
+ import { FormField, PickFromListField } from "../fields/field";
4
+ import { FormDataValue } from "../form";
5
5
  import { FormFieldControl } from "./controls";
6
6
  export declare class Dropdown extends FormFieldControl {
7
- fieldname: string;
7
+ field: PickFromListField;
8
8
  options: {
9
9
  label: string;
10
10
  autosubmit?: boolean;
11
11
  autocomplete?: boolean;
12
12
  };
13
- constructor(fieldname: string, options?: {
13
+ constructor(field: PickFromListField, options?: {
14
14
  label: string;
15
15
  autosubmit?: boolean;
16
16
  autocomplete?: boolean;
17
17
  });
18
- areFieldNamesValid(fields: FormField[]): boolean;
19
- _render(ctx: BaseContext, fields: FormField[], data: FormData): import("stream").Readable;
18
+ render<Fields extends Record<string, FormField>>(ctx: BaseContext, data: Record<string, FormDataValue>): Promise<import("stream").Readable>;
20
19
  }
@@ -1,7 +1,7 @@
1
1
  import { BaseContext } from "koa";
2
- import { Templatable } from "tempstream";
3
- import { FormField } from "../field";
4
- import { FormData } from "../form";
2
+ import { FlatTemplatable } from "tempstream";
3
+ import { PickFromListField } from "../fields/field";
4
+ import { FormDataValue } from "../form";
5
5
  import { FormFieldControl } from "./controls";
6
6
  /**
7
7
  * This control has own forms in it so if you want to use it you
@@ -11,11 +11,11 @@ import { FormFieldControl } from "./controls";
11
11
  * this control for reference.
12
12
  */
13
13
  export declare class EditableCollectionSubset extends FormFieldControl {
14
- fieldname: string;
14
+ field: PickFromListField;
15
15
  actionname: string;
16
16
  listLabel?: string | undefined;
17
17
  selectLabel?: string | undefined;
18
- constructor(fieldname: string, actionname: string, listLabel?: string | undefined, selectLabel?: string | undefined);
19
- _render(ctx: BaseContext, fields: FormField[], data: FormData<string>): Templatable | Promise<Templatable>;
18
+ constructor(field: PickFromListField, actionname: string, listLabel?: string | undefined, selectLabel?: string | undefined);
19
+ render(ctx: BaseContext, data: Record<string, FormDataValue>): Promise<FlatTemplatable>;
20
20
  role: "input";
21
21
  }
@@ -1,8 +1,7 @@
1
1
  import { BaseContext } from "koa";
2
- import { Templatable } from "tempstream";
3
- import { FormField } from "../field";
4
- import { FormData } from "../form";
2
+ import { FlatTemplatable } from "tempstream";
3
+ import { FormDataValue, FormMessage } from "../form";
5
4
  export declare abstract class FormControl {
6
- abstract render(ctx: BaseContext, formFields: FormField[], data: FormData): Templatable | Promise<Templatable>;
5
+ abstract render(ctx: BaseContext, data: Record<string, FormDataValue>, messages: FormMessage[]): FlatTemplatable | Promise<FlatTemplatable>;
7
6
  abstract role: "input" | "decoration" | "messages" | "submit";
8
7
  }
@@ -1,13 +1,14 @@
1
1
  import { BaseContext } from "koa";
2
- import { Templatable } from "tempstream";
3
- import { FormField } from "../field";
4
- import { FormData } from "../form";
2
+ import { FlatTemplatable } from "tempstream";
3
+ import { FormField } from "../fields/field";
4
+ import { FieldValueType, FormDataValue } from "../form";
5
5
  import { FormControl } from "./form-control";
6
+ export declare type FormFieldsToTypes<FormFields extends readonly FormField[]> = {
7
+ [Index in keyof FormFields]: FormFields[Index] extends FormField ? FieldValueType<FormFields[Index]> : never;
8
+ };
6
9
  export declare abstract class FormFieldControl extends FormControl {
7
- fieldnames: string[];
10
+ fields: FormField[];
8
11
  role: "input";
9
- constructor(fieldnames: string[]);
10
- areFieldNamesValid(fields: FormField[]): boolean;
11
- abstract _render(ctx: BaseContext, fields: FormField[], data: FormData): Templatable | Promise<Templatable>;
12
- render(ctx: BaseContext, fields: FormField[], data: FormData): Templatable | Promise<Templatable>;
12
+ constructor(fields: FormField[]);
13
+ abstract render(ctx: BaseContext, data: Record<string, FormDataValue>): FlatTemplatable | Promise<FlatTemplatable>;
13
14
  }
@@ -1,4 +1,4 @@
1
- import { Templatable } from "tempstream";
1
+ import { FlatTemplatable } from "tempstream";
2
2
  import { FormControl } from "./controls";
3
3
  /**
4
4
  * This class will render `turbo-frame` tag so that u can
@@ -12,6 +12,6 @@ import { FormControl } from "./controls";
12
12
  export declare class Frame extends FormControl {
13
13
  src: string;
14
14
  constructor(src: string);
15
- render(): Templatable | Promise<Templatable>;
15
+ render(): FlatTemplatable | Promise<FlatTemplatable>;
16
16
  role: "decoration";
17
17
  }
@@ -1,9 +1,9 @@
1
1
  import { BaseContext } from "koa";
2
- import { FormField } from "../field";
3
- import { FormData } from "../form";
2
+ import { FormField } from "../fields/field";
3
+ import { FormDataValue } from "../form";
4
4
  import { FormFieldControl } from "./controls";
5
5
  export declare class SimpleInput extends FormFieldControl {
6
- fieldname: string;
6
+ field: FormField;
7
7
  options: {
8
8
  id?: string;
9
9
  label?: string;
@@ -14,7 +14,7 @@ export declare class SimpleInput extends FormFieldControl {
14
14
  readonly?: boolean;
15
15
  step?: number;
16
16
  };
17
- constructor(fieldname: string, options?: {
17
+ constructor(field: FormField, options?: {
18
18
  id?: string;
19
19
  label?: string;
20
20
  autocomplete?: boolean;
@@ -24,5 +24,5 @@ export declare class SimpleInput extends FormFieldControl {
24
24
  readonly?: boolean;
25
25
  step?: number;
26
26
  });
27
- _render(_: BaseContext, fields: FormField[], data: FormData): string;
27
+ render<Fields extends Record<string, FormField>>(ctx: BaseContext, data: Record<string, FormDataValue>): Promise<string>;
28
28
  }
@@ -1,42 +1,73 @@
1
1
  import { BaseContext } from "koa";
2
+ import { FormDataValue } from "..";
2
3
  export declare type FormFieldValidationResponse = {
3
4
  valid: boolean;
4
5
  message: string;
5
6
  };
6
- export declare type FormFieldValidationFn = (ctx: BaseContext, value: unknown, field: FormField) => Promise<FormFieldValidationResponse>;
7
- export declare class FormField<Fieldnames extends string = string> {
8
- name: Fieldnames;
7
+ export declare type FormFieldValidationFn<ValueType> = (ctx: BaseContext, value: ValueType, field: FormField<ValueType>) => Promise<FormFieldValidationResponse>;
8
+ export declare type FieldParsedValue<T> = T extends FormField<infer R> ? R : never;
9
+ export declare type FieldParseResult<T> = {
10
+ parsable: false;
11
+ error: string;
12
+ parsed_value: null;
13
+ } | {
14
+ parsable: true;
15
+ error: null;
16
+ parsed_value: T;
17
+ };
18
+ export declare abstract class FormField<ParsedValue extends unknown = unknown> {
9
19
  required: boolean;
10
- validator: FormFieldValidationFn;
11
- constructor(name: Fieldnames, required?: boolean, validator?: FormFieldValidationFn);
12
- _validate(ctx: BaseContext, value: unknown): Promise<FormFieldValidationResponse>;
20
+ name: string;
21
+ constructor(required?: boolean);
22
+ init(fieldname: string): void;
23
+ _validate(ctx: BaseContext, value: ParsedValue): Promise<FormFieldValidationResponse>;
24
+ abstract getEmptyValue(): ParsedValue;
25
+ isValueValid(ctx: BaseContext, value: ParsedValue): Promise<FormFieldValidationResponse>;
26
+ abstract parse(raw_value: FormDataValue): Promise<FieldParseResult<ParsedValue>>;
27
+ getValue(ctx: BaseContext, all_form_values: Record<string, FormDataValue>): Promise<{
28
+ valid: boolean;
29
+ message: string;
30
+ parsed: ParsedValue;
31
+ raw: FormDataValue;
32
+ }>;
33
+ }
34
+ export declare class SimpleFormField extends FormField<string> {
35
+ parse(raw_value: FormDataValue): Promise<FieldParseResult<string>>;
13
36
  getEmptyValue(): string;
14
37
  }
15
- export declare class PickFromListField<Fieldnames extends string = string> extends FormField<Fieldnames> {
16
- name: Fieldnames;
38
+ export declare class PickFromListField extends SimpleFormField {
17
39
  required: boolean;
18
40
  generateOptions: (ctx: BaseContext) => Promise<Record<string, string> | {
19
41
  [i: string]: string;
20
42
  }>;
21
43
  customValidation: (ctx: BaseContext, value: unknown, instance: PickFromListField) => Promise<FormFieldValidationResponse>;
22
- constructor(name: Fieldnames, required: boolean, generateOptions: (ctx: BaseContext) => Promise<Record<string, string> | {
44
+ constructor(required: boolean, generateOptions: (ctx: BaseContext) => Promise<Record<string, string> | {
23
45
  [i: string]: string;
24
46
  }>, customValidation?: (ctx: BaseContext, value: unknown, instance: PickFromListField) => Promise<FormFieldValidationResponse>);
47
+ isValueValid(ctx: BaseContext, value: string): Promise<FormFieldValidationResponse>;
25
48
  valueInList(ctx: BaseContext, value: unknown): Promise<FormFieldValidationResponse>;
49
+ getEmptyValue(): string;
26
50
  }
27
- export declare class ChekboxedListField<Fieldnames extends string = string> extends FormField<Fieldnames> {
28
- name: Fieldnames;
51
+ export declare class ChekboxedListField extends FormField<Record<string, "on">> {
29
52
  required: boolean;
30
53
  generateOptions: (ctx: BaseContext) => Promise<Record<string, string> | {
31
54
  [i: string]: string;
32
55
  }>;
33
56
  isVisible: (ctx: BaseContext) => Promise<boolean>;
34
- constructor(name: Fieldnames, required: boolean, generateOptions: (ctx: BaseContext) => Promise<Record<string, string> | {
57
+ constructor(required: boolean, generateOptions: (ctx: BaseContext) => Promise<Record<string, string> | {
35
58
  [i: string]: string;
36
59
  }>, isVisible?: (ctx: BaseContext) => Promise<boolean>);
37
- private isValueValid;
60
+ isValueValid(_: BaseContext, value: unknown): Promise<FormFieldValidationResponse>;
61
+ parse(raw_value: FormDataValue): Promise<FieldParseResult<Record<string, "on">>>;
62
+ getEmptyValue(): Record<string, "on">;
38
63
  }
39
- export declare class NumberField<Fieldnames extends string = string> extends FormField<Fieldnames> {
40
- constructor(field_name: Fieldnames, required: boolean);
41
- private isValueValid;
64
+ export declare class NumberField<DefaultValue extends number | null = null> extends FormField<number | DefaultValue> {
65
+ default_value: DefaultValue;
66
+ constructor(required: boolean | undefined, default_value: DefaultValue);
67
+ isValueValid(_: BaseContext, value: unknown): Promise<{
68
+ valid: boolean;
69
+ message: string;
70
+ }>;
71
+ parse(value: FormDataValue): Promise<FieldParseResult<number>>;
72
+ getEmptyValue(): DefaultValue;
42
73
  }
@@ -0,0 +1,16 @@
1
+ import { BaseContext } from "koa";
2
+ import { FormDataValue } from "../form";
3
+ import { FieldParseResult, FormField, FormFieldValidationResponse } from "./field";
4
+ export declare class ChekboxedListField extends FormField<Record<string, "on">> {
5
+ required: boolean;
6
+ generateOptions: (ctx: BaseContext) => Promise<Record<string, string> | {
7
+ [i: string]: string;
8
+ }>;
9
+ isVisible: (ctx: BaseContext) => Promise<boolean>;
10
+ constructor(required: boolean, generateOptions: (ctx: BaseContext) => Promise<Record<string, string> | {
11
+ [i: string]: string;
12
+ }>, isVisible?: (ctx: BaseContext) => Promise<boolean>);
13
+ isValueValid(_: BaseContext, value: unknown): Promise<FormFieldValidationResponse>;
14
+ parse(_: BaseContext, raw_value: FormDataValue): Promise<FieldParseResult<Record<string, "on">>>;
15
+ getEmptyValue(): Record<string, "on">;
16
+ }
@@ -0,0 +1,11 @@
1
+ import { BaseContext } from "koa";
2
+ import { Depromisify, Field, FieldOutput } from "sealious";
3
+ import { FormDataValue } from "../form";
4
+ import { FieldParseResult, FormField, FormFieldValidationResponse } from "./field";
5
+ export declare class CollectionField<F extends Field> extends FormField<FieldOutput<F>> {
6
+ sealiousField: F;
7
+ constructor(required: boolean, sealiousField: F);
8
+ parse(ctx: BaseContext, raw_value: FormDataValue): Promise<FieldParseResult<FieldOutput<F>>>;
9
+ isValueValid(ctx: BaseContext, value: FieldOutput<F>): Promise<FormFieldValidationResponse>;
10
+ getEmptyValue(): Depromisify<ReturnType<F["decode"]>>;
11
+ }
@@ -0,0 +1,38 @@
1
+ import { BaseContext } from "koa";
2
+ import { FormDataValue } from "../form";
3
+ export declare type FormFieldValidationResponse = {
4
+ valid: boolean;
5
+ message: string;
6
+ };
7
+ export declare type FormFieldValidationFn<ValueType> = (ctx: BaseContext, value: ValueType, field: FormField<ValueType>) => Promise<FormFieldValidationResponse>;
8
+ export declare type FieldParsedValue<T> = T extends FormField<infer R> ? R : never;
9
+ export declare type FieldParseResult<T> = {
10
+ parsable: false;
11
+ error: string;
12
+ parsed_value: null;
13
+ } | {
14
+ parsable: true;
15
+ error: null;
16
+ parsed_value: T;
17
+ };
18
+ export declare abstract class FormField<ParsedValue extends unknown = unknown> {
19
+ required: boolean;
20
+ name: string;
21
+ constructor(required?: boolean);
22
+ init(fieldname: string): void;
23
+ _validate(ctx: BaseContext, value: ParsedValue): Promise<FormFieldValidationResponse>;
24
+ abstract getEmptyValue(): ParsedValue;
25
+ isValueValid(ctx: BaseContext, value: ParsedValue): Promise<FormFieldValidationResponse>;
26
+ abstract parse(ctx: BaseContext, raw_value: FormDataValue): Promise<FieldParseResult<ParsedValue>>;
27
+ getValue(ctx: BaseContext, all_form_values: Record<string, FormDataValue>): Promise<{
28
+ valid: boolean;
29
+ message: string;
30
+ parsed: ParsedValue;
31
+ raw: FormDataValue;
32
+ }>;
33
+ }
34
+ export * from "./simple-form-field";
35
+ export * from "./checkboxed-list";
36
+ export * from "./collection-field";
37
+ export * from "./number";
38
+ export * from "./pick-from-list";
@@ -0,0 +1,13 @@
1
+ import { BaseContext } from "koa";
2
+ import { FormDataValue } from "../form";
3
+ import { FieldParseResult, FormField } from "./field";
4
+ export declare class NumberField<DefaultValue extends number | null = null> extends FormField<number | DefaultValue> {
5
+ default_value: DefaultValue;
6
+ constructor(required: boolean | undefined, default_value: DefaultValue);
7
+ isValueValid(_: BaseContext, value: unknown): Promise<{
8
+ valid: boolean;
9
+ message: string;
10
+ }>;
11
+ parse(_: BaseContext, value: FormDataValue): Promise<FieldParseResult<number>>;
12
+ getEmptyValue(): DefaultValue;
13
+ }
@@ -0,0 +1,16 @@
1
+ import { BaseContext } from "koa";
2
+ import { FormFieldValidationResponse } from "./field";
3
+ import { SimpleFormField } from "./simple-form-field";
4
+ export declare class PickFromListField extends SimpleFormField {
5
+ required: boolean;
6
+ generateOptions: (ctx: BaseContext) => Promise<Record<string, string> | {
7
+ [i: string]: string;
8
+ }>;
9
+ customValidation: (ctx: BaseContext, value: unknown, instance: PickFromListField) => Promise<FormFieldValidationResponse>;
10
+ constructor(required: boolean, generateOptions: (ctx: BaseContext) => Promise<Record<string, string> | {
11
+ [i: string]: string;
12
+ }>, customValidation?: (ctx: BaseContext, value: unknown, instance: PickFromListField) => Promise<FormFieldValidationResponse>);
13
+ isValueValid(ctx: BaseContext, value: string): Promise<FormFieldValidationResponse>;
14
+ valueInList(ctx: BaseContext, value: unknown): Promise<FormFieldValidationResponse>;
15
+ getEmptyValue(): string;
16
+ }
@@ -0,0 +1,7 @@
1
+ import { FieldParseResult, FormField } from "./field";
2
+ import { FormDataValue } from "../form";
3
+ import { BaseContext } from "koa";
4
+ export declare class SimpleFormField extends FormField<string | null> {
5
+ parse(_: BaseContext, raw_value: FormDataValue): Promise<FieldParseResult<string>>;
6
+ getEmptyValue(): string;
7
+ }
@@ -1,4 +1,4 @@
1
1
  import { FormField } from "./field";
2
2
  export declare class FormFieldsList {
3
- static getField(fields: FormField[], name: string): FormField<string> | undefined;
3
+ static getField(fields: FormField[], name: string): any;
4
4
  }
@@ -1,22 +1,24 @@
1
1
  /// <reference types="koa__router" />
2
2
  import { BaseContext } from "koa";
3
3
  import Router from "@koa/router";
4
- import { Templatable } from "tempstream";
5
- import { FormControl } from "./controls/controls";
6
- import { Mountable, PageErrorMessage } from "../page/page";
7
- import { FormField } from "./field";
4
+ import { FlatTemplatable, Templatable } from "tempstream";
5
+ import { FormField } from "./fields/field";
6
+ import { Fields, Mountable, PageErrorMessage } from "../page/mountable";
7
+ export declare type FormDataPrimitive = string | string[] | undefined;
8
+ export declare type FormDataValue = FormDataPrimitive | Record<string, FormDataPrimitive>;
9
+ export declare type FieldValueType<F extends FormField> = F extends FormField<infer R> ? R : never;
10
+ export declare type FormFieldsToValues<F extends Record<string, FormField<keyof F>>> = {
11
+ [Property in keyof F]: FieldValueType<F[Property]>;
12
+ };
13
+ export declare type FormMessage = {
14
+ type: "info" | "success" | "error";
15
+ text: string;
16
+ };
8
17
  export declare type FormData<Fieldnames extends string = string> = {
9
- values: Record<Fieldnames, string | string[] | number>;
10
- raw_values: Record<Fieldnames, string | string[] | number>;
11
- errors: Partial<Record<Fieldnames, string>>;
12
- messages: {
13
- type: "info" | "success" | "error";
14
- text: string;
15
- }[];
18
+ raw_values: Record<Fieldnames, FormDataValue>;
19
+ messages: FormMessage[];
16
20
  };
17
- export declare abstract class Form<Fieldnames extends string = string> implements Mountable {
18
- abstract fields: FormField<Fieldnames>[];
19
- abstract controls: FormControl[];
21
+ export declare abstract class Form<F extends Fields> extends Mountable<F> {
20
22
  defaultSuccessMessage: string;
21
23
  submitButtonText: string;
22
24
  canAccess(_: BaseContext): Promise<{
@@ -24,19 +26,12 @@ export declare abstract class Form<Fieldnames extends string = string> implement
24
26
  message: string;
25
27
  }>;
26
28
  renderError(_: BaseContext, error: PageErrorMessage): Promise<Templatable>;
27
- validate(_: BaseContext, __: Record<string, unknown>): Promise<{
28
- valid: boolean;
29
- error: string;
30
- }>;
31
- private _validate;
32
- render(ctx: BaseContext, data: FormData, _: string): Promise<Templatable>;
33
- renderMessages(_: BaseContext, data: FormData): Templatable;
34
- renderControls(ctx: BaseContext, data: FormData): Templatable;
35
- makeFormTag(path: string): string;
36
- private generateData;
37
- onValuesInvalid(ctx: BaseContext, errors: Record<Fieldnames, string>, form_path: string): Promise<void>;
38
- onError(ctx: BaseContext, error: unknown, form_path: string): Promise<void>;
39
- abstract onSubmit(ctx: BaseContext, values: Record<Fieldnames, string | string[] | number>): void | Promise<void>;
40
- onSuccess(ctx: BaseContext, form_path: string): Promise<void>;
29
+ render(ctx: BaseContext, data: FormData): Promise<Templatable>;
30
+ makeFormTag(): FlatTemplatable;
31
+ onValuesInvalid(ctx: BaseContext, form_messages: FormMessage[]): Promise<void>;
32
+ onError(ctx: BaseContext, data: FormData, error: unknown): Promise<void>;
33
+ abstract onSubmit(ctx: BaseContext, data: FormData): void | Promise<void>;
34
+ onSuccess(ctx: BaseContext, data: FormData): Promise<void>;
41
35
  mount(router: Router, path: string): void;
36
+ extractRawValues(ctx: BaseContext): Record<string, FormDataValue>;
42
37
  }
@@ -0,0 +1,6 @@
1
+ import { FieldParseResult, FormField } from "./field";
2
+ import { FormDataValue } from "./form";
3
+ export declare class SimpleFormField extends FormField<string> {
4
+ parse(raw_value: FormDataValue): Promise<FieldParseResult<string>>;
5
+ getEmptyValue(): string;
6
+ }
@@ -1,3 +0,0 @@
1
- import { Field } from "sealious";
2
- import { FormFieldValidationFn } from "./field";
3
- export declare function collectionFieldValidator(field: Field): FormFieldValidationFn;
package/@types/index.d.ts CHANGED
@@ -1,14 +1,16 @@
1
1
  import Sealious from "sealious";
2
+ import { FormDataValue } from "./forms/form";
2
3
  declare module "koa" {
3
4
  interface BaseContext {
4
5
  $context: Sealious.Context;
5
- $body: Record<string, unknown>;
6
+ $body: Record<string, FormDataValue>;
6
7
  }
7
8
  }
8
- export * from "./forms/form";
9
9
  export * from "./forms/controls/controls";
10
- export * from "./forms/field";
10
+ export * from "./forms/fields/field";
11
+ export * from "./page/mountable";
11
12
  export * from "./page/page";
13
+ export * from "./forms/form";
12
14
  export * from "./page/props-parser";
13
15
  export * from "./utils/utils";
14
- export * from "./forms/validator";
16
+ export * from "./page/list";
@@ -0,0 +1,4 @@
1
+ import { BaseContext } from "koa";
2
+ import { Templatable } from "tempstream";
3
+ import { FormDataValue, FormField } from ".";
4
+ export declare function makeHiddenInputs(ctx: BaseContext, fields: Record<string, FormField>, values: Record<string, FormDataValue>, fields_to_skip: string[]): Templatable;
@@ -1,57 +1,38 @@
1
- /// <reference types="node" />
2
1
  import { BaseContext } from "koa";
3
- import { Collection, CollectionItem } from "sealious";
4
- import { Templatable } from "tempstream";
2
+ import { Collection, CollectionItem, ItemList } from "sealious";
3
+ import { Templatable, FlatTemplatable } from "tempstream";
5
4
  import { Page } from "./page";
6
5
  import { ShapeToType } from "@sealcode/ts-predicates";
7
- import { PagePropsParser } from "./props-parser";
8
6
  import { FormFieldControl } from "../forms/controls/controls";
9
- import { FormField } from "../forms/field";
7
+ import { FormField, NumberField } from "../forms/fields/field";
8
+ import { FormDataValue } from "..";
10
9
  export declare const BasePagePropsShape: {};
11
10
  export declare type BasePageProps = ShapeToType<typeof BasePagePropsShape>;
12
- export declare const BaseListPagePropsShape: {
13
- readonly page: import("@sealcode/ts-predicates").Predicate<number>;
14
- readonly itemsPerPage: import("@sealcode/ts-predicates").Predicate<number>;
11
+ export declare const BaseListPageFields: {
12
+ readonly page: NumberField<1>;
13
+ readonly itemsPerPage: NumberField<12>;
15
14
  };
16
- export declare type BaseListPageProps = ShapeToType<typeof BaseListPagePropsShape>;
17
- export declare const BaseListPageDefaultProps: {
18
- page: number;
19
- itemsPerPage: number;
20
- };
21
- export declare type PropsErrors<PropsType> = Partial<Record<keyof PropsType, string>>;
22
- export declare abstract class ListPage<ItemType, PropsType extends BaseListPageProps = BaseListPageProps> extends Page {
23
- abstract getItems(ctx: BaseContext, props: PropsType): Promise<{
24
- items: ItemType[];
25
- }>;
26
- abstract getTotalPages(ctx: BaseContext, props: PropsType): Promise<number>;
27
- abstract renderItem(ctx: BaseContext, item: ItemType): Promise<Templatable>;
28
- abstract propsParser: PagePropsParser<PropsType>;
29
- filterFields: FormField<keyof PropsType>[];
15
+ export declare abstract class ListPage<ItemType, F extends typeof BaseListPageFields> extends Page<F> {
16
+ abstract getItems(ctx: BaseContext, page: number, itemsPerPage: number, values: Record<string, FormDataValue>): Promise<ItemType[]>;
17
+ abstract getTotalPages(ctx: BaseContext, itemsPerPage: number, values: Record<string, FormDataValue>): Promise<number>;
18
+ abstract renderItem(ctx: BaseContext, item: ItemType): Promise<FlatTemplatable>;
19
+ filterFields: Record<string, FormField>;
30
20
  filterControls: FormFieldControl[];
31
- renderListContainer(_: BaseContext, content: Templatable): Templatable;
32
- validateProps(ctx: BaseContext, props: PropsType): Promise<{
33
- valid: boolean;
34
- errors: PropsErrors<PropsType>;
35
- }>;
36
- getProps(ctx: BaseContext): Promise<{
37
- parsed_props: PropsType;
38
- errors: PropsErrors<PropsType>;
39
- raw_props: PropsType;
40
- }>;
41
- render(ctx: BaseContext): Promise<import("stream").Readable>;
42
- renderPagination(ctx: BaseContext, props: PropsType): Promise<import("stream").Readable>;
21
+ init(): void;
22
+ renderListContainer(_: BaseContext, content: Templatable): FlatTemplatable;
23
+ render(ctx: BaseContext): Promise<FlatTemplatable>;
24
+ renderPagination(ctx: BaseContext, values: Record<string, FormDataValue>): Promise<FlatTemplatable>;
43
25
  private renderPageButton;
44
- renderFilters(ctx: BaseContext, parsed_props: PropsType, // parsed props don't include wrong values
45
- raw_props: PropsType, errors: PropsErrors<PropsType>): import("stream").Readable;
26
+ getFilterValues(ctx: BaseContext): Promise<Record<string, unknown>>;
27
+ renderFilters(ctx: BaseContext): FlatTemplatable;
46
28
  }
47
- export declare abstract class SealiousItemListPage<C extends Collection, PageProps extends BaseListPageProps = BaseListPageProps> extends ListPage<CollectionItem<C>, PageProps> {
29
+ export declare abstract class SealiousItemListPage<C extends Collection, F extends typeof BaseListPageFields> extends ListPage<CollectionItem<C>, F> {
48
30
  collection: C;
49
31
  constructor(collection: C);
50
- getTotalPages(ctx: BaseContext, props: PageProps): Promise<number>;
51
- getItems(ctx: BaseContext, props: PageProps): Promise<{
52
- items: CollectionItem<C>[];
53
- }>;
54
- renderItem(_: BaseContext, item: CollectionItem<C>): Promise<Templatable>;
32
+ getTotalPages(ctx: BaseContext, itemsPerPage: number): Promise<number>;
33
+ protected addFilter(ctx: BaseContext, list: ItemList<C>): Promise<void>;
34
+ getItems(ctx: BaseContext, page: number, itemsPerPage: number): Promise<CollectionItem<C>[]>;
35
+ renderItem(_: BaseContext, item: CollectionItem<C>): Promise<FlatTemplatable>;
55
36
  canAccess: (ctx: BaseContext) => Promise<{
56
37
  canAccess: boolean;
57
38
  message: string;
@@ -0,0 +1,36 @@
1
+ /// <reference types="koa__router" />
2
+ import Router from "@koa/router";
3
+ import { FormControl } from "../forms/controls/form-control";
4
+ import { BaseContext } from "koa";
5
+ import { FlatTemplatable, Templatable } from "tempstream";
6
+ import { FormField } from "../forms/fields/field";
7
+ import { FormData, FormDataValue, FormMessage } from "../forms/form";
8
+ export declare type PageErrorMessage = {
9
+ type: "access" | "internal";
10
+ message: string;
11
+ };
12
+ export declare type Fields = Record<string, FormField<unknown>>;
13
+ export declare abstract class Mountable<F extends Fields = Fields> {
14
+ fields: F;
15
+ constructor();
16
+ abstract controls: FormControl[];
17
+ abstract mount(router: Router, path: string): void;
18
+ abstract canAccess(ctx: BaseContext): Promise<{
19
+ canAccess: boolean;
20
+ message: string;
21
+ }>;
22
+ abstract renderError(ctx: BaseContext, error: PageErrorMessage): Promise<Templatable>;
23
+ abstract extractRawValues(ctx: BaseContext): Record<string, FormDataValue>;
24
+ validateValues(ctx: BaseContext, data: Record<string, FormDataValue>): Promise<{
25
+ valid: boolean;
26
+ error: string;
27
+ }>;
28
+ protected validate(ctx: BaseContext, values: Record<string, FormDataValue>): Promise<{
29
+ valid: boolean;
30
+ field_errors: Partial<Record<keyof Fields, string>>;
31
+ form_messages: FormMessage[];
32
+ }>;
33
+ init(): void;
34
+ renderControls(ctx: BaseContext, data: FormData<keyof Fields>): FlatTemplatable;
35
+ renderMessages(_: BaseContext, data: FormData<keyof Fields>): Templatable;
36
+ }
@@ -1,26 +1,19 @@
1
1
  /// <reference types="koa__router" />
2
- /// <reference types="node" />
3
2
  import Router from "@koa/router";
4
3
  import { BaseContext } from "koa";
5
- import { Templatable } from "tempstream";
6
- export declare type PageErrorMessage = {
7
- type: "access" | "internal";
8
- message: string;
9
- };
10
- export interface Mountable {
11
- mount: (router: Router, path: string) => void;
12
- canAccess: (ctx: BaseContext) => Promise<{
13
- canAccess: boolean;
14
- message: string;
15
- }>;
16
- renderError(ctx: BaseContext, error: PageErrorMessage): Promise<Templatable>;
17
- }
18
- export declare abstract class Page implements Mountable {
4
+ import { FlatTemplatable, Templatable } from "tempstream";
5
+ import { FormDataValue } from "../forms/form";
6
+ import { Fields, Mountable, PageErrorMessage } from "./mountable";
7
+ import { PagePropsParser } from "./props-parser";
8
+ export declare abstract class Page<F extends Fields = Record<string, never>> extends Mountable<F> {
9
+ propsParser: PagePropsParser<Record<keyof Fields, unknown>>;
10
+ controls: never[];
19
11
  mount(router: Router, path: string): void;
20
12
  canAccess(_: BaseContext): Promise<{
21
13
  canAccess: boolean;
22
14
  message: string;
23
15
  }>;
24
- renderError(_: BaseContext, error: PageErrorMessage): Promise<import("stream").Readable>;
16
+ renderError(_: BaseContext, error: PageErrorMessage): Promise<FlatTemplatable>;
25
17
  abstract render(ctx: BaseContext): Promise<Templatable>;
18
+ extractRawValues(ctx: BaseContext): Record<string, FormDataValue>;
26
19
  }