@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
@@ -27,38 +27,48 @@ import { tempstream } from "tempstream";
27
27
  import { ${uppercase_collection} } from "${rel(
28
28
  "src/back/collections/collections"
29
29
  )}";
30
- import html from "${rel("src/back/html.ts")}";
31
- import {
32
- BaseListPageDefaultProps,
33
- BaseListPageProps,
34
- BaseListPagePropsShape,
35
- SealiousItemListPage,
36
- } from "${rel("src/back/page/list.ts")}";
37
- import { AllQueryParams } from "${rel("src/back/page/props-parser.ts")}";
30
+ import html from "../../html";
31
+ import { SealiousItemListPage, BaseListPageFields } from "@sealcode/sealgen";
38
32
 
39
33
  export const actionName = "${action_name}";
40
34
 
41
- export default new (class ${action_name}Page extends SealiousItemListPage<typeof ${uppercase_collection}> {
42
- propsParser = new AllQueryParams<BaseListPageProps>(
43
- BaseListPagePropsShape,
44
- BaseListPageDefaultProps
45
- );
35
+ const filterFields = {};
36
+
37
+ export default new (class ${action_name}Page extends SealiousItemListPage<
38
+ typeof ${uppercase_collection},
39
+ typeof BaseListPageFields
40
+ > {
41
+ fields = BaseListPageFields;
42
+
43
+ filterFields = filterFields;
44
+ filterControls = [];
46
45
 
47
46
  async render(ctx: BaseContext) {
48
47
  return html(
49
48
  ctx,
50
- "${uppercase_collection}",
49
+ "${uppercase_collection}",
51
50
  tempstream/* HTML */ \`<div>
52
- <h2>${uppercase_collection} List</h2>
53
- <ul>
54
- \${super.render(ctx)}
55
- </ul>
51
+ <h2>${uppercase_collection} List</h2>
52
+ <table>
53
+ <thead>
54
+ <th>id</th>
55
+ \${Object.keys(${uppercase_collection}.fields).map(
56
+ (fieldname) => \`<th>\${fieldname}</th>\`
57
+ )}
58
+ </thead>
59
+ <tbody>
60
+ \${super.render(ctx)}
61
+ </tbody>
62
+ </table>
56
63
  </div>\`
57
64
  );
58
65
  }
59
66
 
60
- async renderItem(_: BaseContext, item: CollectionItem<typeof ${uppercase_collection}>) {
61
- return tempstream\`<li>\${item.id}</li>\`;
67
+ async renderItem(_: BaseContext, item: CollectionItem<typeof ${uppercase_collection}>) {
68
+ return tempstream\`<tr><td>\${item.id}</td>\${Object.keys(${uppercase_collection}.fields).map(
69
+ (fieldname: keyof typeof ${uppercase_collection}["fields"]) =>
70
+ tempstream\`<td>\${item.get(fieldname)}</td>\`
71
+ )}</tr>\`;
62
72
  }
63
73
  })(${uppercase_collection});
64
74
  `;
@@ -1,24 +1,24 @@
1
- import { importPath } from "../utils/import-path";
2
-
1
+ import { curryImportPath } from "../utils/import-path";
3
2
  export async function pageTemplate(
4
3
  action_name: string,
5
- fullpath: string
4
+ newfilefullpath: string
6
5
  ): Promise<string> {
6
+ const rel = curryImportPath(newfilefullpath);
7
7
  return `import { BaseContext } from "koa";
8
8
  import { tempstream } from "tempstream";
9
- import { Page } from "${importPath(fullpath, "src/back/page/page")}";
9
+ import { Page } from "@sealcode/sealgen";
10
+ import html from "${rel("src/back/html")}";
10
11
 
11
12
  export const actionName = "${action_name}";
12
13
 
13
14
  export default new (class ${action_name}Page extends Page {
14
-
15
15
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
16
16
  async canAccess(_: BaseContext) {
17
17
  return { canAccess: true, message: "" };
18
18
  }
19
19
 
20
20
  async render(ctx: BaseContext) {
21
- return tempstream/* HTML */ \`<div></div>\`;
21
+ return html(ctx, "${action_name}", tempstream/* HTML */ \`<div></div>\`);
22
22
  }
23
23
  })();
24
24
  `;
@@ -2,6 +2,7 @@ export function escape_url_params(url: string): string {
2
2
  return url
3
3
  .split("/")
4
4
  .map((e) => (e.startsWith(":") ? `[${e.slice(1)}]` : e))
5
+ .filter((e) => e != "")
5
6
  .join("/");
6
7
  }
7
8
 
File without changes
@@ -1,119 +0,0 @@
1
- import { is, predicates } from "@sealcode/ts-predicates";
2
- import { BaseContext } from "koa";
3
-
4
- export type FormFieldValidationResponse = { valid: boolean; message: string };
5
-
6
- export type FormFieldValidationFn = (
7
- ctx: BaseContext,
8
- value: unknown,
9
- field: FormField
10
- ) => Promise<FormFieldValidationResponse>;
11
-
12
- export class FormField<Fieldnames extends string = string> {
13
- constructor(
14
- public name: Fieldnames,
15
- public required: boolean = false,
16
- public validator: FormFieldValidationFn = async () => ({
17
- valid: true,
18
- message: "",
19
- })
20
- ) {}
21
-
22
- public async _validate(
23
- ctx: BaseContext,
24
- value: unknown
25
- ): Promise<FormFieldValidationResponse> {
26
- if (this.required && (value == "" || value == null || value == undefined)) {
27
- return { valid: false, message: "This field is required" };
28
- }
29
- return this.validator(ctx, value, this);
30
- }
31
-
32
- public getEmptyValue() {
33
- return "";
34
- }
35
- }
36
-
37
- export class PickFromListField<
38
- Fieldnames extends string = string
39
- > extends FormField<Fieldnames> {
40
- constructor(
41
- public name: Fieldnames,
42
- public required: boolean = false,
43
- public generateOptions: (
44
- ctx: BaseContext
45
- ) => Promise<Record<string, string> | { [i: string]: string }>,
46
- public customValidation: (
47
- ctx: BaseContext,
48
- value: unknown,
49
- instance: PickFromListField
50
- ) => Promise<FormFieldValidationResponse> = (ctx, value, instance) =>
51
- instance.valueInList(ctx, value)
52
- ) {
53
- super(name, required, (ctx, value) => this.customValidation(ctx, value, this));
54
- }
55
-
56
- async valueInList(
57
- ctx: BaseContext,
58
- value: unknown
59
- ): Promise<FormFieldValidationResponse> {
60
- const options = await this.generateOptions(ctx);
61
- if (!is(value, predicates.string)) {
62
- return { valid: false, message: "not a string" };
63
- }
64
- if (!Object.keys(options).includes(value)) {
65
- return { valid: false, message: `"${value}" is not one of the options` };
66
- }
67
- return { valid: true, message: "" };
68
- }
69
- }
70
-
71
- export class ChekboxedListField<
72
- Fieldnames extends string = string
73
- > extends FormField<Fieldnames> {
74
- constructor(
75
- public name: Fieldnames,
76
- public required: boolean = false,
77
- public generateOptions: (
78
- ctx: BaseContext
79
- ) => Promise<Record<string, string> | { [i: string]: string }>,
80
- public isVisible: (ctx: BaseContext) => Promise<boolean> = () =>
81
- Promise.resolve(true)
82
- ) {
83
- super(name, required, (ctx, value) => this.isValueValid(ctx, value));
84
- }
85
-
86
- private async isValueValid(
87
- _: BaseContext,
88
- value: unknown
89
- ): Promise<FormFieldValidationResponse> {
90
- if (is(value, predicates.string)) {
91
- return { valid: false, message: "you need an array" };
92
- }
93
- if (is(value, predicates.null)) {
94
- return { valid: false, message: "you need an array" };
95
- }
96
- return { valid: true, message: "" };
97
- }
98
- }
99
-
100
- export class NumberField<
101
- Fieldnames extends string = string
102
- > extends FormField<Fieldnames> {
103
- constructor(field_name: Fieldnames, required: boolean) {
104
- super(field_name, required, (_, value) => this.isValueValid(_, value));
105
- }
106
-
107
- private async isValueValid(_: BaseContext, value: unknown) {
108
- if (
109
- (is(value, predicates.string) &&
110
- !isNaN(parseFloat(value)) &&
111
- parseFloat(value).toString() == value.trim()) ||
112
- is(value, predicates.number) ||
113
- ((is(value, predicates.undefined) || value == "") && !this.required)
114
- ) {
115
- return { valid: true, message: "" };
116
- }
117
- return { valid: false, message: "Proszę wprowadzić liczbę" };
118
- }
119
- }
@@ -1,7 +0,0 @@
1
- import { FormField } from "./field";
2
-
3
- export class FormFieldsList {
4
- static getField(fields: FormField[], name: string) {
5
- return fields.find((f) => f.name == name);
6
- }
7
- }
@@ -1,10 +0,0 @@
1
- import { BaseContext } from "koa";
2
- import { Field } from "sealious";
3
- import { FormFieldValidationFn } from "./field";
4
-
5
- export function collectionFieldValidator(field: Field): FormFieldValidationFn {
6
- return async (ctx: BaseContext, value) => {
7
- const { valid, reason } = await field.checkValue(ctx.$context, value, undefined);
8
- return { valid, message: reason || (valid ? "Wrong value" : "") };
9
- };
10
- }