@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.
- package/@types/forms/controls/checkboxed-list-input.d.ts +5 -6
- package/@types/forms/controls/dropdown.d.ts +5 -6
- package/@types/forms/controls/editable-collection-subset.d.ts +6 -6
- package/@types/forms/controls/form-control.d.ts +3 -4
- package/@types/forms/controls/form-field-control.d.ts +9 -8
- package/@types/forms/controls/frame.d.ts +2 -2
- package/@types/forms/controls/simple-input.d.ts +5 -5
- package/@types/forms/field.d.ts +47 -16
- package/@types/forms/fields/checkboxed-list.d.ts +16 -0
- package/@types/forms/fields/collection-field.d.ts +11 -0
- package/@types/forms/fields/field.d.ts +38 -0
- package/@types/forms/fields/number.d.ts +13 -0
- package/@types/forms/fields/pick-from-list.d.ts +16 -0
- package/@types/forms/fields/simple-form-field.d.ts +7 -0
- package/@types/forms/form-fields-list.d.ts +1 -1
- package/@types/forms/form.d.ts +23 -28
- package/@types/forms/simple-form-field.d.ts +6 -0
- package/@types/forms/validator.d.ts +0 -3
- package/@types/index.d.ts +6 -4
- package/@types/make-hidden-inputs.d.ts +4 -0
- package/@types/page/list.d.ts +23 -42
- package/@types/page/mountable.d.ts +36 -0
- package/@types/page/page.d.ts +9 -16
- package/@types/page/props-parser.d.ts +5 -10
- package/@types/templates/page.d.ts +1 -1
- package/lib/build.js +12 -3
- package/lib/build.js.map +1 -1
- package/lib/forms/controls/checkboxed-list-input.js +10 -17
- package/lib/forms/controls/checkboxed-list-input.js.map +1 -1
- package/lib/forms/controls/dropdown.js +16 -20
- package/lib/forms/controls/dropdown.js.map +1 -1
- package/lib/forms/controls/editable-collection-subset.js +33 -35
- package/lib/forms/controls/editable-collection-subset.js.map +1 -1
- package/lib/forms/controls/form-control.js.map +1 -1
- package/lib/forms/controls/form-field-control.js +2 -11
- package/lib/forms/controls/form-field-control.js.map +1 -1
- package/lib/forms/controls/simple-input.js +16 -22
- package/lib/forms/controls/simple-input.js.map +1 -1
- package/lib/forms/field.js +96 -20
- package/lib/forms/field.js.map +1 -1
- package/lib/forms/fields/checkboxed-list.js +43 -0
- package/lib/forms/fields/checkboxed-list.js.map +1 -0
- package/lib/forms/fields/collection-field.js +28 -0
- package/lib/forms/fields/collection-field.js.map +1 -0
- package/lib/forms/fields/field.js +66 -0
- package/lib/forms/fields/field.js.map +1 -0
- package/lib/forms/fields/number.js +45 -0
- package/lib/forms/fields/number.js.map +1 -0
- package/lib/forms/fields/pick-from-list.js +34 -0
- package/lib/forms/fields/pick-from-list.js.map +1 -0
- package/lib/forms/fields/simple-form-field.js +18 -0
- package/lib/forms/fields/simple-form-field.js.map +1 -0
- package/lib/forms/form.js +50 -96
- package/lib/forms/form.js.map +1 -1
- package/lib/forms/simple-form-field.js +18 -0
- package/lib/forms/simple-form-field.js.map +1 -0
- package/lib/forms/validator.js +0 -10
- package/lib/forms/validator.js.map +1 -1
- package/lib/generate-routes.js +18 -3
- package/lib/generate-routes.js.map +1 -1
- package/lib/index.js +4 -3
- package/lib/index.js.map +1 -1
- package/lib/make-hidden-inputs.js +17 -0
- package/lib/make-hidden-inputs.js.map +1 -0
- package/lib/page/list.js +64 -79
- package/lib/page/list.js.map +1 -1
- package/lib/page/mountable.js +56 -0
- package/lib/page/mountable.js.map +1 -0
- package/lib/page/page.js +11 -1
- package/lib/page/page.js.map +1 -1
- package/lib/page/props-parser.js +4 -47
- package/lib/page/props-parser.js.map +1 -1
- package/lib/templates/form.js +38 -18
- package/lib/templates/form.js.map +1 -1
- package/lib/templates/list.js +30 -20
- package/lib/templates/list.js.map +1 -1
- package/lib/templates/page.js +5 -4
- package/lib/templates/page.js.map +1 -1
- package/lib/utils/escape-url-params.js +1 -0
- package/lib/utils/escape-url-params.js.map +1 -1
- package/package.json +4 -3
- package/src/build.ts +14 -3
- package/src/forms/controls/checkboxed-list-input.ts +12 -23
- package/src/forms/controls/dropdown.ts +27 -32
- package/src/forms/controls/editable-collection-subset.ts +57 -54
- package/src/forms/controls/form-control.ts +5 -6
- package/src/forms/controls/form-field-control.ts +13 -29
- package/src/forms/controls/frame.ts +2 -2
- package/src/forms/controls/simple-input.ts +26 -23
- package/src/forms/fields/checkboxed-list.ts +61 -0
- package/src/forms/fields/collection-field.ts +54 -0
- package/src/forms/fields/field.ts +106 -0
- package/src/forms/fields/number.ts +53 -0
- package/src/forms/fields/pick-from-list.ts +49 -0
- package/src/forms/fields/simple-form-field.ts +20 -0
- package/src/forms/form.ts +97 -162
- package/src/generate-routes.ts +18 -7
- package/src/index.ts +6 -4
- package/src/make-hidden-inputs.ts +22 -0
- package/src/page/list.ts +113 -126
- package/src/page/mountable.ts +105 -0
- package/src/page/page.ts +20 -11
- package/src/page/props-parser.ts +9 -67
- package/src/templates/form.ts +38 -18
- package/src/templates/list.ts +30 -20
- package/src/templates/page.ts +6 -6
- package/src/utils/escape-url-params.ts +1 -0
- package/src/forms/controls.ts +0 -0
- package/src/forms/field.ts +0 -119
- package/src/forms/form-fields-list.ts +0 -7
- package/src/forms/validator.ts +0 -10
package/src/templates/list.ts
CHANGED
|
@@ -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 "
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
49
|
+
"${uppercase_collection}",
|
|
51
50
|
tempstream/* HTML */ \`<div>
|
|
52
|
-
|
|
53
|
-
<
|
|
54
|
-
|
|
55
|
-
|
|
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
|
-
|
|
61
|
-
return tempstream\`<
|
|
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
|
`;
|
package/src/templates/page.ts
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { curryImportPath } from "../utils/import-path";
|
|
3
2
|
export async function pageTemplate(
|
|
4
3
|
action_name: string,
|
|
5
|
-
|
|
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 "
|
|
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
|
`;
|
package/src/forms/controls.ts
DELETED
|
File without changes
|
package/src/forms/field.ts
DELETED
|
@@ -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
|
-
}
|
package/src/forms/validator.ts
DELETED
|
@@ -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
|
-
}
|