@sealcode/sealgen 0.15.32 → 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.
@@ -11,6 +11,7 @@ export type PageErrorMessage = {
11
11
  };
12
12
  export type Fields = Record<string, FormField<boolean, any>>;
13
13
  type Resolved<T> = T extends Promise<infer X> ? X : never;
14
+ type ParsedValue<T extends FormField> = Resolved<ReturnType<T["getValue"]>>["parsed"];
14
15
  export declare abstract class MountableWithFields<F extends Fields = Fields> extends Mountable {
15
16
  fields: F;
16
17
  field_names_prefix: string;
@@ -34,7 +35,7 @@ export declare abstract class MountableWithFields<F extends Fields = Fields> ext
34
35
  renderMessages(_: Context, data: FormData<keyof Fields>): FlatTemplatable;
35
36
  abstract extractRawValues(ctx: Context): Promise<Record<string, FormDataValue>>;
36
37
  getParsedValues(ctx: Context): Promise<{
37
- [field in keyof F]: Resolved<ReturnType<F[field]["getValue"]>>["parsed"];
38
+ [field in keyof F]: F[field] extends FormField<true> ? Exclude<ParsedValue<F[field]>, null> : ParsedValue<F[field]>;
38
39
  }>;
39
40
  getDatabaseValues(ctx: Context): Promise<ShapeToType<FieldsToShape<F>>>;
40
41
  }
@@ -2,8 +2,11 @@ import { Form } from "../forms/form.js";
2
2
  import { FieldTypes } from "sealious";
3
3
  import { CollectionField } from "../forms/fields/collection-field.js";
4
4
  describe("mountable-with-fields", () => {
5
- it("properly types the getParsedValues method", () => {
6
- const fields = { name: new CollectionField(true, new FieldTypes.Text()) };
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
+ };
7
10
  new class extends Form {
8
11
  constructor() {
9
12
  super(...arguments);
@@ -12,8 +15,9 @@ describe("mountable-with-fields", () => {
12
15
  }
13
16
  async onSubmit(ctx, form_values) {
14
17
  const data = await this.getParsedValues(ctx);
15
- data.name?.trim();
16
- const { parsed: data2 } = await this.fields.name.getValue(
18
+ data.not_required?.trim();
19
+ data.required.trim();
20
+ const { parsed: data2 } = await this.fields.not_required.getValue(
17
21
  ctx,
18
22
  form_values.raw_values
19
23
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sealcode/sealgen",
3
- "version": "0.15.32",
3
+ "version": "0.15.33",
4
4
  "description": "Module to automate adding routes and collections to a sealious application",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",
@@ -5,16 +5,21 @@ import { CollectionField } from "../forms/fields/collection-field.js";
5
5
  import { FormData } from "../forms/form-types.js";
6
6
 
7
7
  describe("mountable-with-fields", () => {
8
- it("properly types the getParsedValues method", () => {
9
- const fields = { name: new CollectionField(true, new FieldTypes.Text()) };
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
+ };
10
13
  new (class extends Form<typeof fields, void> {
11
14
  fields = fields;
12
15
  controls = [];
13
16
  async onSubmit(ctx: Context, form_values: FormData) {
14
17
  const data = await this.getParsedValues(ctx);
15
18
  // this should not throw a Typescript error
16
- data.name?.trim();
17
- const { parsed: data2 } = await this.fields.name.getValue(
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(
18
23
  ctx,
19
24
  form_values.raw_values
20
25
  );
@@ -19,6 +19,9 @@ export type PageErrorMessage = { type: "access" | "internal"; message: string };
19
19
  export type Fields = Record<string, FormField<boolean, any>>;
20
20
 
21
21
  type Resolved<T> = T extends Promise<infer X> ? X : never;
22
+ type ParsedValue<T extends FormField> = Resolved<
23
+ ReturnType<T["getValue"]>
24
+ >["parsed"];
22
25
 
23
26
  export abstract class MountableWithFields<
24
27
  F extends Fields = Fields
@@ -137,9 +140,9 @@ export abstract class MountableWithFields<
137
140
  ): Promise<Record<string, FormDataValue>>;
138
141
 
139
142
  async getParsedValues(ctx: Context): Promise<{
140
- [field in keyof F]: Resolved<
141
- ReturnType<F[field]["getValue"]>
142
- >["parsed"];
143
+ [field in keyof F]: F[field] extends FormField<true>
144
+ ? Exclude<ParsedValue<F[field]>, null>
145
+ : ParsedValue<F[field]>;
143
146
  }> {
144
147
  const raw_values = await this.extractRawValues(ctx);
145
148
  const result: Record<string, unknown> = {};
@@ -151,7 +154,9 @@ export abstract class MountableWithFields<
151
154
  }
152
155
  );
153
156
  await Promise.all(promises);
154
- return result as ShapeToType<FieldsToShape<F>>;
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;
155
160
  }
156
161
 
157
162
  async getDatabaseValues(