@sealcode/sealgen 0.15.31 → 0.15.33
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/src/add-crud.d.ts +3 -0
- package/@types/src/page/mountable-with-fields.d.ts +6 -1
- package/@types/src/page/mountable-with-fields.test.d.ts +1 -0
- package/@types/src/templates/shared/collection-create-form.d.ts +1 -0
- package/@types/src/templates/shared/collection-list.d.ts +15 -0
- package/@types/src/templates/shared/shared-crud-form-fields.d.ts +1 -0
- package/@types/src/utils/write-file.d.ts +1 -0
- package/lib/page/mountable-with-fields.test.js +29 -0
- package/lib/templates/list.js +3 -3
- package/package.json +1 -1
- package/src/forms/fields/collection-field.ts +1 -1
- package/src/page/mountable-with-fields.test.ts +30 -0
- package/src/page/mountable-with-fields.ts +13 -4
- package/src/templates/list.ts +3 -3
|
@@ -10,6 +10,8 @@ export type PageErrorMessage = {
|
|
|
10
10
|
message: string;
|
|
11
11
|
};
|
|
12
12
|
export type Fields = Record<string, FormField<boolean, any>>;
|
|
13
|
+
type Resolved<T> = T extends Promise<infer X> ? X : never;
|
|
14
|
+
type ParsedValue<T extends FormField> = Resolved<ReturnType<T["getValue"]>>["parsed"];
|
|
13
15
|
export declare abstract class MountableWithFields<F extends Fields = Fields> extends Mountable {
|
|
14
16
|
fields: F;
|
|
15
17
|
field_names_prefix: string;
|
|
@@ -32,6 +34,9 @@ export declare abstract class MountableWithFields<F extends Fields = Fields> ext
|
|
|
32
34
|
renderError(_: Context, error: PageErrorMessage): Promise<FlatTemplatable>;
|
|
33
35
|
renderMessages(_: Context, data: FormData<keyof Fields>): FlatTemplatable;
|
|
34
36
|
abstract extractRawValues(ctx: Context): Promise<Record<string, FormDataValue>>;
|
|
35
|
-
getParsedValues(ctx: Context): Promise<
|
|
37
|
+
getParsedValues(ctx: Context): Promise<{
|
|
38
|
+
[field in keyof F]: F[field] extends FormField<true> ? Exclude<ParsedValue<F[field]>, null> : ParsedValue<F[field]>;
|
|
39
|
+
}>;
|
|
36
40
|
getDatabaseValues(ctx: Context): Promise<ShapeToType<FieldsToShape<F>>>;
|
|
37
41
|
}
|
|
42
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function createItemFormTemplate(create_action_name: string, newfilefullpath: string, collection_name: string, list_action: string): Promise<string>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare function makeDefaultFilterFields(collection_fields: {
|
|
2
|
+
name: string;
|
|
3
|
+
type: string;
|
|
4
|
+
}[]): string;
|
|
5
|
+
export declare function makeDefaultDisplayFields(collection_fields: {
|
|
6
|
+
name: string;
|
|
7
|
+
type: string;
|
|
8
|
+
}[]): string;
|
|
9
|
+
declare const default_hooks: {
|
|
10
|
+
pre_header_html: string;
|
|
11
|
+
post_header_html: string;
|
|
12
|
+
post_import_js: string;
|
|
13
|
+
};
|
|
14
|
+
export declare function collectionListTemplate(collection_name: string, action_name: string, newfilefullpath: string, _hooks?: Partial<typeof default_hooks>): Promise<string>;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function sharedCrudFormFields(collection_name: string, newfilefullpath: string): Promise<string>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function writeFile(path: string, content: string): Promise<void>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Form } from "../forms/form.js";
|
|
2
|
+
import { FieldTypes } from "sealious";
|
|
3
|
+
import { CollectionField } from "../forms/fields/collection-field.js";
|
|
4
|
+
describe("mountable-with-fields", () => {
|
|
5
|
+
it("properly types the getParsedValues method and respects required status", () => {
|
|
6
|
+
const fields = {
|
|
7
|
+
not_required: new CollectionField(false, new FieldTypes.Text()),
|
|
8
|
+
required: new CollectionField(true, new FieldTypes.Text())
|
|
9
|
+
};
|
|
10
|
+
new class extends Form {
|
|
11
|
+
constructor() {
|
|
12
|
+
super(...arguments);
|
|
13
|
+
this.fields = fields;
|
|
14
|
+
this.controls = [];
|
|
15
|
+
}
|
|
16
|
+
async onSubmit(ctx, form_values) {
|
|
17
|
+
const data = await this.getParsedValues(ctx);
|
|
18
|
+
data.not_required?.trim();
|
|
19
|
+
data.required.trim();
|
|
20
|
+
const { parsed: data2 } = await this.fields.not_required.getValue(
|
|
21
|
+
ctx,
|
|
22
|
+
form_values.raw_values
|
|
23
|
+
);
|
|
24
|
+
data2?.trim();
|
|
25
|
+
}
|
|
26
|
+
}();
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
//# sourceMappingURL=mountable-with-fields.test.js.map
|
package/lib/templates/list.js
CHANGED
|
@@ -29,7 +29,7 @@ const listTemplate = async (action_name, newfilefullpath) => {
|
|
|
29
29
|
]);
|
|
30
30
|
const importPath = curryImportPath(newfilefullpath);
|
|
31
31
|
const result = `import type { Context } from "koa";
|
|
32
|
-
import type { CollectionItem } from "sealious";
|
|
32
|
+
import type { CollectionItem, ItemListResult } from "sealious";
|
|
33
33
|
import type { FlatTemplatable } from "tempstream";
|
|
34
34
|
import { TempstreamJSX } from "tempstream";
|
|
35
35
|
import { ${uppercase_collection} } from "${importPath(
|
|
@@ -98,7 +98,7 @@ ${collection_fields.map((field) => {
|
|
|
98
98
|
] as {
|
|
99
99
|
field: string;
|
|
100
100
|
label: string;
|
|
101
|
-
format?: (value: unknown) => FlatTemplatable;
|
|
101
|
+
format?: (value: unknown, item: CollectionItem<typeof Opinions>) => FlatTemplatable;
|
|
102
102
|
}[];
|
|
103
103
|
|
|
104
104
|
export default new (class ${action_name}Page extends SealiousItemListPage<
|
|
@@ -152,7 +152,7 @@ async getFilterValues(ctx: Context) {
|
|
|
152
152
|
{displayFields.map(({ field, format }) => {
|
|
153
153
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any
|
|
154
154
|
const value = item.get(field as any);
|
|
155
|
-
return <td>{format ? format(value) : value}</td>;
|
|
155
|
+
return <td>{format ? format(value, item) : value}</td>;
|
|
156
156
|
})}
|
|
157
157
|
</tr>;
|
|
158
158
|
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Context } from "koa";
|
|
2
|
+
import { Form } from "../forms/form.js";
|
|
3
|
+
import { FieldTypes } from "sealious";
|
|
4
|
+
import { CollectionField } from "../forms/fields/collection-field.js";
|
|
5
|
+
import { FormData } from "../forms/form-types.js";
|
|
6
|
+
|
|
7
|
+
describe("mountable-with-fields", () => {
|
|
8
|
+
it("properly types the getParsedValues method and respects required status", () => {
|
|
9
|
+
const fields = {
|
|
10
|
+
not_required: new CollectionField(false, new FieldTypes.Text()),
|
|
11
|
+
required: new CollectionField(true, new FieldTypes.Text()),
|
|
12
|
+
};
|
|
13
|
+
new (class extends Form<typeof fields, void> {
|
|
14
|
+
fields = fields;
|
|
15
|
+
controls = [];
|
|
16
|
+
async onSubmit(ctx: Context, form_values: FormData) {
|
|
17
|
+
const data = await this.getParsedValues(ctx);
|
|
18
|
+
// this should not throw a Typescript error
|
|
19
|
+
data.not_required?.trim();
|
|
20
|
+
// this should work without the optional chaining:
|
|
21
|
+
data.required.trim();
|
|
22
|
+
const { parsed: data2 } = await this.fields.not_required.getValue(
|
|
23
|
+
ctx,
|
|
24
|
+
form_values.raw_values
|
|
25
|
+
);
|
|
26
|
+
data2?.trim();
|
|
27
|
+
}
|
|
28
|
+
})();
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -18,6 +18,11 @@ export type PageErrorMessage = { type: "access" | "internal"; message: string };
|
|
|
18
18
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
19
|
export type Fields = Record<string, FormField<boolean, any>>;
|
|
20
20
|
|
|
21
|
+
type Resolved<T> = T extends Promise<infer X> ? X : never;
|
|
22
|
+
type ParsedValue<T extends FormField> = Resolved<
|
|
23
|
+
ReturnType<T["getValue"]>
|
|
24
|
+
>["parsed"];
|
|
25
|
+
|
|
21
26
|
export abstract class MountableWithFields<
|
|
22
27
|
F extends Fields = Fields
|
|
23
28
|
> extends Mountable {
|
|
@@ -134,9 +139,11 @@ export abstract class MountableWithFields<
|
|
|
134
139
|
ctx: Context
|
|
135
140
|
): Promise<Record<string, FormDataValue>>;
|
|
136
141
|
|
|
137
|
-
async getParsedValues(
|
|
138
|
-
|
|
139
|
-
|
|
142
|
+
async getParsedValues(ctx: Context): Promise<{
|
|
143
|
+
[field in keyof F]: F[field] extends FormField<true>
|
|
144
|
+
? Exclude<ParsedValue<F[field]>, null>
|
|
145
|
+
: ParsedValue<F[field]>;
|
|
146
|
+
}> {
|
|
140
147
|
const raw_values = await this.extractRawValues(ctx);
|
|
141
148
|
const result: Record<string, unknown> = {};
|
|
142
149
|
const promises = Object.entries(this.fields).map(
|
|
@@ -147,7 +154,9 @@ export abstract class MountableWithFields<
|
|
|
147
154
|
}
|
|
148
155
|
);
|
|
149
156
|
await Promise.all(promises);
|
|
150
|
-
|
|
157
|
+
// TODO: remove this any. I don't have the strenght to deal with it now.
|
|
158
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-return
|
|
159
|
+
return result as any;
|
|
151
160
|
}
|
|
152
161
|
|
|
153
162
|
async getDatabaseValues(
|
package/src/templates/list.ts
CHANGED
|
@@ -35,7 +35,7 @@ export const listTemplate = async (
|
|
|
35
35
|
]);
|
|
36
36
|
const importPath = curryImportPath(newfilefullpath);
|
|
37
37
|
const result = `import type { Context } from "koa";
|
|
38
|
-
import type { CollectionItem } from "sealious";
|
|
38
|
+
import type { CollectionItem, ItemListResult } from "sealious";
|
|
39
39
|
import type { FlatTemplatable } from "tempstream";
|
|
40
40
|
import { TempstreamJSX } from "tempstream";
|
|
41
41
|
import { ${uppercase_collection} } from "${importPath(
|
|
@@ -113,7 +113,7 @@ ${collection_fields
|
|
|
113
113
|
] as {
|
|
114
114
|
field: string;
|
|
115
115
|
label: string;
|
|
116
|
-
format?: (value: unknown) => FlatTemplatable;
|
|
116
|
+
format?: (value: unknown, item: CollectionItem<typeof Opinions>) => FlatTemplatable;
|
|
117
117
|
}[];
|
|
118
118
|
|
|
119
119
|
export default new (class ${action_name}Page extends SealiousItemListPage<
|
|
@@ -167,7 +167,7 @@ async getFilterValues(ctx: Context) {
|
|
|
167
167
|
{displayFields.map(({ field, format }) => {
|
|
168
168
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any
|
|
169
169
|
const value = item.get(field as any);
|
|
170
|
-
return <td>{format ? format(value) : value}</td>;
|
|
170
|
+
return <td>{format ? format(value, item) : value}</td>;
|
|
171
171
|
})}
|
|
172
172
|
</tr>;
|
|
173
173
|
}
|