@sealcode/sealgen 0.14.12 → 0.14.14

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.
@@ -1 +1,2 @@
1
+ export declare function generateURL(actionName: string, url: string): string;
1
2
  export declare function generateRoutes(): Promise<void>;
@@ -0,0 +1 @@
1
+ export {};
@@ -18,10 +18,11 @@ export declare abstract class MountableWithFields<F extends Fields = Fields> ext
18
18
  constructor();
19
19
  init(): void;
20
20
  makeFormControlContext(ctx: Context, data: FormData, validate: boolean, field_name_prefix?: string, form_id?: string): FormControlContext;
21
- validateValues(ctx: Context, data: Record<string, FormDataValue>): Promise<{
21
+ validateValues(_ctx: Context, _data: Record<string, FormDataValue>): Promise<{
22
22
  valid: boolean;
23
23
  error: string;
24
24
  }>;
25
+ getInitialValues(_ctx: Context): Promise<Record<string, FormDataValue>>;
25
26
  validate(ctx: Context, values: Record<string, FormDataValue>): Promise<{
26
27
  valid: boolean;
27
28
  field_errors: Partial<Record<keyof Fields, string>>;
@@ -194,7 +194,7 @@ class Form extends MountableWithFields {
194
194
  router.use(path, subrouter.routes(), subrouter.allowedMethods());
195
195
  }
196
196
  async extractRawValues(ctx) {
197
- return ctx.$body;
197
+ return Object.keys(ctx.$body).length ? ctx.$body : this.getInitialValues(ctx);
198
198
  }
199
199
  }
200
200
  export {
@@ -8,6 +8,8 @@ import { locator_is_visible } from "../test_utils/locator_is_visible.js";
8
8
  import { getBrowser } from "../utils/browser-creator.js";
9
9
  import { assertThrowsAsync } from "../utils/utils.js";
10
10
  import getPort from "get-port";
11
+ import { SimpleFormField } from "./fields/simple-form-field.js";
12
+ import { expect as PlaywritghtExpect } from "@playwright/test";
11
13
  const fields = {
12
14
  text: new Fields.SimpleFormField(true)
13
15
  };
@@ -311,6 +313,44 @@ describe("form test", () => {
311
313
  });
312
314
  });
313
315
  });
316
+ describe("getInitialValues tests", async () => {
317
+ let server;
318
+ afterEach(async () => {
319
+ server.close();
320
+ });
321
+ it("fills the form with values from getInitialValues", async () => {
322
+ const app = new Koa();
323
+ const router = new Router();
324
+ const fields2 = {
325
+ title: new SimpleFormField(true)
326
+ };
327
+ mount(
328
+ router,
329
+ "/",
330
+ new class extends Form {
331
+ constructor() {
332
+ super(...arguments);
333
+ this.fields = fields2;
334
+ this.controls = [new Controls.SimpleInput(fields2.title)];
335
+ }
336
+ async getInitialValues() {
337
+ return { title: "Hello" };
338
+ }
339
+ onSubmit() {
340
+ return null;
341
+ }
342
+ }(),
343
+ true
344
+ );
345
+ app.use(router.routes()).use(router.allowedMethods());
346
+ server = app.listen(port);
347
+ const response = await page.goto(`http://localhost:${port}`);
348
+ if (response?.status() != 200) {
349
+ throw new Error(`Should return 200 status and it returns ${response?.status()}`);
350
+ }
351
+ await PlaywritghtExpect(page.getByLabel("title")).toHaveValue("Hello");
352
+ });
353
+ });
314
354
  });
315
355
  export {
316
356
  form_factory
@@ -31,7 +31,7 @@ function generateURL(actionName, url) {
31
31
  (part) => part.startsWith(":") ? "${" + part.slice(1) + "}" : encodeURIComponent(part)
32
32
  ).join("/")}\`;
33
33
  ${actionName}URL.params = [${params.map((param) => `"${param}"`).join(",")}];
34
- ${actionName}URL.rawURL = "${url}";`;
34
+ ${actionName}URL.rawURL = "${encodeURI(url)}";`;
35
35
  }
36
36
  async function generateRoutes() {
37
37
  const files = await Promise.all(
@@ -85,6 +85,7 @@ ${files.map(
85
85
  );
86
86
  }
87
87
  export {
88
- generateRoutes
88
+ generateRoutes,
89
+ generateURL
89
90
  };
90
91
  //# sourceMappingURL=generate-routes.js.map
@@ -0,0 +1,40 @@
1
+ import { generateURL } from "./generate-routes.js";
2
+ import * as prettier from "prettier";
3
+ import { getPrettierConfig } from "./utils/prettier.js";
4
+ describe("generate routes", () => {
5
+ it("handles a url with params", async () => {
6
+ assert.strictEqual(
7
+ prettier.format(
8
+ generateURL("EditUser", "/admin/users/:id/edit/"),
9
+ await getPrettierConfig()
10
+ ),
11
+ prettier.format(
12
+ `export const EditUserURL = (id: string) => \`/admin/users/\${id}/edit/\`;
13
+ EditUserURL.params = ["id"];
14
+ EditUserURL.rawURL = "/admin/users/:id/edit/";`,
15
+ await getPrettierConfig()
16
+ )
17
+ );
18
+ });
19
+ it("handles polish characters in the url", () => {
20
+ assert.strictEqual(
21
+ generateURL("Users", "/admin/u\u017Cytkownicy/"),
22
+ `export const UsersURL = "/admin/u%C5%BCytkownicy/";`
23
+ );
24
+ });
25
+ it("handles a url with params and diacritics", async () => {
26
+ assert.strictEqual(
27
+ prettier.format(
28
+ generateURL("EditUser", "/admin/u\u017Cytkownicy/:id/edit/"),
29
+ await getPrettierConfig()
30
+ ),
31
+ prettier.format(
32
+ `export const EditUserURL = (id: string) => \`/admin/u%C5%BCytkownicy/\${id}/edit/\`;
33
+ EditUserURL.params = ["id"];
34
+ EditUserURL.rawURL = "/admin/u%C5%BCytkownicy/:id/edit/";`,
35
+ await getPrettierConfig()
36
+ )
37
+ );
38
+ });
39
+ });
40
+ //# sourceMappingURL=generate-routes.test.js.map
@@ -28,12 +28,15 @@ class MountableWithFields extends Mountable {
28
28
  );
29
29
  }
30
30
  // this one is meant to be overwritten
31
- async validateValues(ctx, data) {
31
+ async validateValues(_ctx, _data) {
32
32
  return {
33
33
  valid: true,
34
34
  error: ""
35
35
  };
36
36
  }
37
+ async getInitialValues(_ctx) {
38
+ return {};
39
+ }
37
40
  async validate(ctx, values) {
38
41
  const field_errors = {};
39
42
  let valid = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sealcode/sealgen",
3
- "version": "0.14.12",
3
+ "version": "0.14.14",
4
4
  "description": "Module to automate adding routes and collections to a sealious application",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -8,7 +8,6 @@ import { CheckboxedListField } from "../fields/checkboxed-list.js";
8
8
  import { Form } from "../form.js";
9
9
  import { CheckboxedListInput } from "./checkboxed-list-input.js";
10
10
  import { getBrowser } from "../../utils/browser-creator.js";
11
- import { sleep } from "../../utils/sleep.js";
12
11
 
13
12
  describe.only("checkboxed-list", () => {
14
13
  describe("basic tests", async () => {
@@ -7,9 +7,11 @@ import { Controls, Fields, Form, Mountable } from "../index.js";
7
7
  import { mount } from "../mount.js";
8
8
  import { locator_is_visible } from "../test_utils/locator_is_visible.js";
9
9
  import { getBrowser } from "../utils/browser-creator.js";
10
- import { assertThrowsAsync } from "../utils/utils.js";
10
+ import { assertThrowsAsync, sleep } from "../utils/utils.js";
11
11
  import { FormDataValue } from "./form-types.js";
12
12
  import getPort from "get-port";
13
+ import { SimpleFormField } from "./fields/simple-form-field.js";
14
+ import { expect as PlaywritghtExpect } from "@playwright/test";
13
15
 
14
16
  const fields = {
15
17
  text: new Fields.SimpleFormField(true),
@@ -355,4 +357,47 @@ describe("form test", () => {
355
357
  });
356
358
  });
357
359
  });
360
+
361
+ describe("getInitialValues tests", async () => {
362
+ let server: ReturnType<Koa["listen"]>;
363
+
364
+ afterEach(async () => {
365
+ server.close();
366
+ });
367
+
368
+ it("fills the form with values from getInitialValues", async () => {
369
+ const app = new Koa();
370
+ const router = new Router();
371
+
372
+ const fields = {
373
+ title: new SimpleFormField(true),
374
+ };
375
+
376
+ mount(
377
+ router,
378
+ "/",
379
+ new (class extends Form<typeof fields, null> {
380
+ fields = fields;
381
+ controls = [new Controls.SimpleInput(fields.title)];
382
+
383
+ async getInitialValues() {
384
+ return { title: "Hello" };
385
+ }
386
+
387
+ onSubmit() {
388
+ return null;
389
+ }
390
+ })(),
391
+ true
392
+ );
393
+
394
+ app.use(router.routes()).use(router.allowedMethods());
395
+ server = app.listen(port);
396
+ const response = await page.goto(`http://localhost:${port}`);
397
+ if (response?.status() != 200) {
398
+ throw new Error(`Should return 200 status and it returns ${response?.status()}`);
399
+ }
400
+ await PlaywritghtExpect(page.getByLabel("title")).toHaveValue("Hello");
401
+ });
402
+ });
358
403
  });
package/src/forms/form.ts CHANGED
@@ -269,6 +269,8 @@ export abstract class Form<
269
269
  async extractRawValues(
270
270
  ctx: Context
271
271
  ): Promise<Record<string, FormDataValue>> {
272
- return ctx.$body;
272
+ return Object.keys(ctx.$body).length
273
+ ? ctx.$body
274
+ : this.getInitialValues(ctx);
273
275
  }
274
276
  }
@@ -0,0 +1,42 @@
1
+ import { generateURL } from "./generate-routes.js";
2
+ import * as prettier from "prettier";
3
+ import { getPrettierConfig } from "./utils/prettier.js";
4
+
5
+ describe("generate routes", () => {
6
+ it("handles a url with params", async () => {
7
+ assert.strictEqual(
8
+ prettier.format(
9
+ generateURL("EditUser", "/admin/users/:id/edit/"),
10
+ await getPrettierConfig()
11
+ ),
12
+ prettier.format(
13
+ `export const EditUserURL = (id: string) => \`/admin/users/\${id}/edit/\`;
14
+ EditUserURL.params = ["id"];
15
+ EditUserURL.rawURL = "/admin/users/:id/edit/";`,
16
+ await getPrettierConfig()
17
+ )
18
+ );
19
+ });
20
+
21
+ it("handles polish characters in the url", () => {
22
+ assert.strictEqual(
23
+ generateURL("Users", "/admin/użytkownicy/"),
24
+ `export const UsersURL = "/admin/u%C5%BCytkownicy/";`
25
+ );
26
+ });
27
+
28
+ it("handles a url with params and diacritics", async () => {
29
+ assert.strictEqual(
30
+ prettier.format(
31
+ generateURL("EditUser", "/admin/użytkownicy/:id/edit/"),
32
+ await getPrettierConfig()
33
+ ),
34
+ prettier.format(
35
+ `export const EditUserURL = (id: string) => \`/admin/u%C5%BCytkownicy/\${id}/edit/\`;
36
+ EditUserURL.params = ["id"];
37
+ EditUserURL.rawURL = "/admin/u%C5%BCytkownicy/:id/edit/";`,
38
+ await getPrettierConfig()
39
+ )
40
+ );
41
+ });
42
+ });
@@ -24,7 +24,7 @@ async function extractActionName(full_file_path: string): Promise<string> {
24
24
  );
25
25
  }
26
26
 
27
- function generateURL(actionName: string, url: string) {
27
+ export function generateURL(actionName: string, url: string) {
28
28
  const parts = url.split("/");
29
29
  const params = parts
30
30
  .filter((part) => part.startsWith(":"))
@@ -44,7 +44,7 @@ function generateURL(actionName: string, url: string) {
44
44
  ${actionName}URL.params = [${params
45
45
  .map((param) => `"${param}"`)
46
46
  .join(",")}];
47
- ${actionName}URL.rawURL = "${url}";`;
47
+ ${actionName}URL.rawURL = "${encodeURI(url)}";`;
48
48
  }
49
49
 
50
50
  export async function generateRoutes(): Promise<void> {
@@ -55,8 +55,8 @@ export abstract class MountableWithFields<
55
55
 
56
56
  // this one is meant to be overwritten
57
57
  async validateValues(
58
- ctx: Context,
59
- data: Record<string, FormDataValue>
58
+ _ctx: Context,
59
+ _data: Record<string, FormDataValue>
60
60
  ): Promise<{ valid: boolean; error: string }> {
61
61
  return {
62
62
  valid: true,
@@ -64,6 +64,12 @@ export abstract class MountableWithFields<
64
64
  };
65
65
  }
66
66
 
67
+ async getInitialValues(
68
+ _ctx: Context
69
+ ): Promise<Record<string, FormDataValue>> {
70
+ return {};
71
+ }
72
+
67
73
  async validate(
68
74
  ctx: Context,
69
75
  values: Record<string, FormDataValue>