@sealcode/sealgen 0.4.1 → 0.5.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 (98) hide show
  1. package/@types/forms/controls/checkboxed-list-input.d.ts +2 -2
  2. package/@types/forms/controls/controls.d.ts +1 -0
  3. package/@types/forms/controls/dropdown.d.ts +2 -2
  4. package/@types/forms/controls/editable-collection-subset.d.ts +2 -2
  5. package/@types/forms/controls/form-control.d.ts +2 -2
  6. package/@types/forms/controls/form-field-control.d.ts +2 -2
  7. package/@types/forms/controls/form-header.d.ts +4 -4
  8. package/@types/forms/controls/html.d.ts +9 -0
  9. package/@types/forms/controls/simple-input.d.ts +4 -2
  10. package/@types/forms/fields/checkboxed-list.d.ts +7 -7
  11. package/@types/forms/fields/collection-field.d.ts +3 -3
  12. package/@types/forms/fields/field.d.ts +6 -6
  13. package/@types/forms/fields/number.d.ts +3 -3
  14. package/@types/forms/fields/pick-from-list.d.ts +7 -7
  15. package/@types/forms/fields/simple-form-field.d.ts +2 -2
  16. package/@types/forms/form.d.ts +10 -10
  17. package/@types/index.d.ts +5 -3
  18. package/@types/make-hidden-inputs.d.ts +5 -4
  19. package/@types/page/access-control.d.ts +2 -2
  20. package/@types/page/list.d.ts +14 -14
  21. package/@types/page/mountable.d.ts +9 -9
  22. package/@types/page/page.d.ts +6 -6
  23. package/@types/page/props-parser.d.ts +4 -4
  24. package/@types/utils/translate.d.ts +3 -0
  25. package/@types/utils/url-with-new-params.d.ts +2 -2
  26. package/lib/build.js +4 -3
  27. package/lib/build.js.map +1 -1
  28. package/lib/forms/controls/checkboxed-list-input.js.map +1 -1
  29. package/lib/forms/controls/controls.js +3 -1
  30. package/lib/forms/controls/controls.js.map +1 -1
  31. package/lib/forms/controls/dropdown.js.map +1 -1
  32. package/lib/forms/controls/editable-collection-subset.js.map +1 -1
  33. package/lib/forms/controls/form-header.js.map +1 -1
  34. package/lib/forms/controls/html.js +19 -0
  35. package/lib/forms/controls/html.js.map +1 -0
  36. package/lib/forms/controls/simple-input.js +4 -2
  37. package/lib/forms/controls/simple-input.js.map +1 -1
  38. package/lib/forms/fields/checkboxed-list.js.map +1 -1
  39. package/lib/forms/fields/collection-field.js.map +1 -1
  40. package/lib/forms/fields/field.js +5 -1
  41. package/lib/forms/fields/field.js.map +1 -1
  42. package/lib/forms/fields/number.js.map +1 -1
  43. package/lib/forms/fields/pick-from-list.js.map +1 -1
  44. package/lib/forms/fields/simple-form-field.js.map +1 -1
  45. package/lib/forms/form.js.map +1 -1
  46. package/lib/generate-routes.js +22 -13
  47. package/lib/generate-routes.js.map +1 -1
  48. package/lib/index.js +16 -2
  49. package/lib/index.js.map +1 -1
  50. package/lib/make-hidden-inputs.js.map +1 -1
  51. package/lib/page/access-control.js.map +1 -1
  52. package/lib/page/list.js +2 -1
  53. package/lib/page/list.js.map +1 -1
  54. package/lib/page/mountable.js.map +1 -1
  55. package/lib/page/page.js.map +1 -1
  56. package/lib/page/props-parser.js.map +1 -1
  57. package/lib/templates/form.js +9 -16
  58. package/lib/templates/form.js.map +1 -1
  59. package/lib/templates/list.js +4 -4
  60. package/lib/templates/list.js.map +1 -1
  61. package/lib/templates/page.js +3 -3
  62. package/lib/templates/route-test.js +1 -1
  63. package/lib/templates/route-test.js.map +1 -1
  64. package/lib/utils/translate.js +10 -0
  65. package/lib/utils/translate.js.map +1 -0
  66. package/lib/utils/url-with-new-params.js.map +1 -1
  67. package/package.json +3 -3
  68. package/src/build.ts +4 -3
  69. package/src/forms/controls/checkboxed-list-input.ts +2 -2
  70. package/src/forms/controls/controls.ts +1 -0
  71. package/src/forms/controls/dropdown.ts +2 -2
  72. package/src/forms/controls/editable-collection-subset.ts +2 -2
  73. package/src/forms/controls/form-control.ts +2 -2
  74. package/src/forms/controls/form-field-control.ts +2 -2
  75. package/src/forms/controls/form-header.ts +3 -3
  76. package/src/forms/controls/html.ts +18 -0
  77. package/src/forms/controls/simple-input.ts +7 -6
  78. package/src/forms/fields/checkboxed-list.ts +5 -5
  79. package/src/forms/fields/collection-field.ts +3 -3
  80. package/src/forms/fields/field.ts +17 -7
  81. package/src/forms/fields/number.ts +3 -3
  82. package/src/forms/fields/pick-from-list.ts +5 -5
  83. package/src/forms/fields/simple-form-field.ts +2 -2
  84. package/src/forms/form.ts +10 -10
  85. package/src/generate-routes.ts +34 -13
  86. package/src/index.ts +5 -3
  87. package/src/make-hidden-inputs.ts +6 -5
  88. package/src/page/access-control.ts +2 -2
  89. package/src/page/list.ts +17 -24
  90. package/src/page/mountable.ts +11 -11
  91. package/src/page/page.ts +5 -5
  92. package/src/page/props-parser.ts +4 -7
  93. package/src/templates/form.ts +9 -16
  94. package/src/templates/list.ts +4 -4
  95. package/src/templates/page.ts +3 -3
  96. package/src/templates/route-test.ts +1 -1
  97. package/src/utils/translate.ts +13 -0
  98. package/src/utils/url-with-new-params.ts +2 -2
@@ -1,7 +1,7 @@
1
1
  import Router from "@koa/router";
2
2
  import { FormControl } from "../forms/controls/form-control";
3
- import { BaseContext } from "koa";
4
- import { FlatTemplatable, Templatable, tempstream } from "tempstream";
3
+ import { Context } from "koa";
4
+ import { FlatTemplatable, tempstream } from "tempstream";
5
5
  import { FormField } from "../forms/fields/field";
6
6
  import { FormData, FormDataValue, FormMessage } from "../forms/form";
7
7
 
@@ -20,19 +20,19 @@ export abstract class Mountable<F extends Fields = Fields> {
20
20
  abstract mount(router: Router, path: string): void;
21
21
 
22
22
  abstract canAccess(
23
- ctx: BaseContext
23
+ ctx: Context
24
24
  ): Promise<{ canAccess: boolean; message: string }>;
25
25
 
26
26
  abstract renderError(
27
- ctx: BaseContext,
27
+ ctx: Context,
28
28
  error: PageErrorMessage
29
- ): Promise<Templatable>;
29
+ ): Promise<FlatTemplatable>;
30
30
 
31
- abstract extractRawValues(ctx: BaseContext): Record<string, FormDataValue>;
31
+ abstract extractRawValues(ctx: Context): Record<string, FormDataValue>;
32
32
 
33
33
  // this one is meant to be overwritten
34
34
  async validateValues(
35
- ctx: BaseContext,
35
+ ctx: Context,
36
36
  data: Record<string, FormDataValue>
37
37
  ): Promise<{ valid: boolean; error: string }> {
38
38
  return {
@@ -42,7 +42,7 @@ export abstract class Mountable<F extends Fields = Fields> {
42
42
  }
43
43
 
44
44
  protected async validate(
45
- ctx: BaseContext,
45
+ ctx: Context,
46
46
  values: Record<string, FormDataValue>
47
47
  ): Promise<{
48
48
  valid: boolean;
@@ -83,7 +83,7 @@ export abstract class Mountable<F extends Fields = Fields> {
83
83
  }
84
84
 
85
85
  public renderControls(
86
- ctx: BaseContext,
86
+ ctx: Context,
87
87
  data: FormData<keyof Fields>
88
88
  ): FlatTemplatable {
89
89
  return tempstream/* HTML */ `${this.controls.map((control) =>
@@ -92,9 +92,9 @@ export abstract class Mountable<F extends Fields = Fields> {
92
92
  }
93
93
 
94
94
  public renderMessages(
95
- _: BaseContext,
95
+ _: Context,
96
96
  data: FormData<keyof Fields>
97
- ): Templatable {
97
+ ): FlatTemplatable {
98
98
  return tempstream/* HTML */ `<div class="form-messages">
99
99
  ${data.messages.map(
100
100
  (message) =>
package/src/page/page.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import Router from "@koa/router";
2
- import { BaseContext } from "koa";
2
+ import { Context } from "koa";
3
3
 
4
4
  import { FlatTemplatable, Templatable, tempstream } from "tempstream";
5
5
  import { AllQueryParams } from "..";
@@ -21,20 +21,20 @@ export abstract class Page<
21
21
  }
22
22
 
23
23
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
24
- async canAccess(_: BaseContext) {
24
+ async canAccess(_: Context) {
25
25
  return { canAccess: true, message: "" };
26
26
  }
27
27
 
28
28
  async renderError(
29
- _: BaseContext,
29
+ _: Context,
30
30
  error: PageErrorMessage
31
31
  ): Promise<FlatTemplatable> {
32
32
  return tempstream/* HTML */ `<div>${error.message}</div>`;
33
33
  }
34
34
 
35
- public abstract render(ctx: BaseContext): Promise<Templatable>;
35
+ public abstract render(ctx: Context): Promise<FlatTemplatable>;
36
36
 
37
- extractRawValues(ctx: BaseContext): Record<string, FormDataValue> {
37
+ extractRawValues(ctx: Context): Record<string, FormDataValue> {
38
38
  return this.propsParser.decode(ctx);
39
39
  }
40
40
  }
@@ -1,4 +1,4 @@
1
- import { BaseContext } from "koa";
1
+ import { Context } from "koa";
2
2
  import merge from "merge";
3
3
  import { FormData, FormDataValue } from "..";
4
4
  import { BasePageProps } from "./list";
@@ -10,14 +10,11 @@ export type EncodedProps = Record<string, unknown>;
10
10
  // or any other exotic types is done by fields logic.
11
11
 
12
12
  export abstract class PagePropsParser<PropsType> {
13
- abstract decode(ctx: BaseContext): Record<string, FormDataValue>;
13
+ abstract decode(ctx: Context): Record<string, FormDataValue>;
14
14
  abstract encode(props: Record<string, FormData>): EncodedProps;
15
15
  abstract getHTMLInputName(prop_name: string): string;
16
16
 
17
- overwriteProp(
18
- ctx: BaseContext,
19
- new_props: Partial<PropsType>
20
- ): EncodedProps {
17
+ overwriteProp(ctx: Context, new_props: Partial<PropsType>): EncodedProps {
21
18
  const result = {};
22
19
  merge.recursive(result, this.decode(ctx), new_props);
23
20
  return result;
@@ -27,7 +24,7 @@ export abstract class PagePropsParser<PropsType> {
27
24
  export class AllQueryParams<
28
25
  PropsType extends BasePageProps
29
26
  > extends PagePropsParser<PropsType> {
30
- decode(ctx: BaseContext): Record<string, FormDataValue> {
27
+ decode(ctx: Context): Record<string, FormDataValue> {
31
28
  return ctx.query;
32
29
  }
33
30
 
@@ -5,23 +5,16 @@ export async function formTemplate(
5
5
  newfilefullpath: string
6
6
  ): Promise<string> {
7
7
  const rel = curryImportPath(newfilefullpath);
8
- return `import { BaseContext } from "koa";
9
- import {
10
- CollectionField,
11
- Form,
12
- FormData,
13
- FormDataValue,
14
- SimpleFormField,
15
- SimpleInput,
16
- } from "@sealcode/sealgen";
8
+ return `import { Context } from "koa";
9
+ import { Form, FormData, FormDataValue, Fields, Controls } from "@sealcode/sealgen";
17
10
  import html from "../html";
18
11
  import { Users } from "${rel("src/back/collections/collections")}";
19
12
 
20
13
  export const actionName = "${action_name}";
21
14
 
22
15
  const fields = {
23
- email: new CollectionField(true, Users.fields.email),
24
- password: new SimpleFormField(true),
16
+ email: new Fields.CollectionField(true, Users.fields.email),
17
+ password: new Fields.SimpleFormField(true),
25
18
  };
26
19
 
27
20
  export default new (class ${action_name}Form extends Form<typeof fields> {
@@ -29,12 +22,12 @@ export default new (class ${action_name}Form extends Form<typeof fields> {
29
22
  fields = fields;
30
23
 
31
24
  controls = [
32
- new SimpleInput(fields.email, { label: "Email:", type: "email" }),
33
- new SimpleInput(fields.password, { label: "Password:", type: "password" }),
25
+ new Controls.SimpleInput(fields.email, { label: "Email:", type: "email" }),
26
+ new Controls.SimpleInput(fields.password, { label: "Password:", type: "password" }),
34
27
  ];
35
28
 
36
29
  async validateValues(
37
- ctx: BaseContext,
30
+ ctx: Context,
38
31
  data: Record<string, FormDataValue>
39
32
  ): Promise<{ valid: boolean; error: string }> {
40
33
  // just an example
@@ -48,7 +41,7 @@ export default new (class ${action_name}Form extends Form<typeof fields> {
48
41
  }
49
42
 
50
43
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
51
- async canAccess(_: BaseContext) {
44
+ async canAccess(_: Context) {
52
45
  return { canAccess: true, message: "" };
53
46
  }
54
47
 
@@ -57,7 +50,7 @@ export default new (class ${action_name}Form extends Form<typeof fields> {
57
50
  return;
58
51
  }
59
52
 
60
- async render(ctx: BaseContext, data: FormData) {
53
+ async render(ctx: Context, data: FormData) {
61
54
  return html(ctx, "${action_name}", await super.render(ctx, data));
62
55
  }
63
56
  })();
@@ -21,13 +21,13 @@ export const listTemplate = async (
21
21
  const uppercase_collection =
22
22
  collection[0].toLocaleUpperCase() + collection.slice(1);
23
23
  const rel = curryImportPath(newfilefullpath);
24
- return `import { BaseContext } from "koa";
24
+ return `import { Context } from "koa";
25
25
  import { CollectionItem } from "sealious";
26
26
  import { tempstream } from "tempstream";
27
27
  import { ${uppercase_collection} } from "${rel(
28
28
  "src/back/collections/collections"
29
29
  )}";
30
- import html from "../../html";
30
+ import { ${uppercase_collection} } from "${rel("src/back/html.ts")}";
31
31
  import { SealiousItemListPage, BaseListPageFields } from "@sealcode/sealgen";
32
32
 
33
33
  export const actionName = "${action_name}";
@@ -43,7 +43,7 @@ export default new (class ${action_name}Page extends SealiousItemListPage<
43
43
  filterFields = filterFields;
44
44
  filterControls = [];
45
45
 
46
- async render(ctx: BaseContext) {
46
+ async render(ctx: Context) {
47
47
  return html(
48
48
  ctx,
49
49
  "${uppercase_collection}",
@@ -64,7 +64,7 @@ export default new (class ${action_name}Page extends SealiousItemListPage<
64
64
  );
65
65
  }
66
66
 
67
- async renderItem(_: BaseContext, item: CollectionItem<typeof ${uppercase_collection}>) {
67
+ async renderItem(_: Context, item: CollectionItem<typeof ${uppercase_collection}>) {
68
68
  return tempstream\`<tr><td>\${item.id}</td>\${Object.keys(${uppercase_collection}.fields).map(
69
69
  (fieldname: keyof typeof ${uppercase_collection}["fields"]) =>
70
70
  tempstream\`<td>\${item.get(fieldname)}</td>\`
@@ -4,7 +4,7 @@ export async function pageTemplate(
4
4
  newfilefullpath: string
5
5
  ): Promise<string> {
6
6
  const rel = curryImportPath(newfilefullpath);
7
- return `import { BaseContext } from "koa";
7
+ return `import { Context } from "koa";
8
8
  import { tempstream } from "tempstream";
9
9
  import { Page } from "@sealcode/sealgen";
10
10
  import html from "${rel("src/back/html")}";
@@ -13,11 +13,11 @@ export const actionName = "${action_name}";
13
13
 
14
14
  export default new (class ${action_name}Page extends Page {
15
15
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
16
- async canAccess(_: BaseContext) {
16
+ async canAccess(_: Context) {
17
17
  return { canAccess: true, message: "" };
18
18
  }
19
19
 
20
- async render(ctx: BaseContext) {
20
+ async render(ctx: Context) {
21
21
  return html(ctx, "${action_name}", tempstream/* HTML */ \`<div></div>\`);
22
22
  }
23
23
  })();
@@ -8,7 +8,7 @@ export function testTemplate(action_name: string, new_file_fullpath: string) {
8
8
  import { LONG_TEST_TIMEOUT, webhintURL } from "${rel(
9
9
  "src/back/test_utils/webhint"
10
10
  )}";
11
- import { ${action_name}URL } from "${rel("src/back/routes/routes")}";
11
+ import { ${action_name}URL } from "${rel("src/back/routes/urls")}";
12
12
 
13
13
  describe("${action_name}", () => {
14
14
  it("doesn't crash", async function () {
@@ -0,0 +1,13 @@
1
+ import { Context } from "koa";
2
+ import { Translation } from "sealious";
3
+
4
+ export function t(
5
+ ctx: Context,
6
+ key: string,
7
+ props: any[],
8
+ default_translation: Translation
9
+ ) {
10
+ return /* HTML */ `<span translation-key="${key.replace('"', '\\"')}"
11
+ >${ctx.$app.getString(key, props, default_translation)}</span
12
+ >`;
13
+ }
@@ -1,8 +1,8 @@
1
- import { BaseContext } from "koa";
1
+ import { Context } from "koa";
2
2
  import qs from "qs";
3
3
 
4
4
  export function UrlWithNewParams(
5
- ctx: BaseContext,
5
+ ctx: Context,
6
6
  query_params: Record<string, unknown>
7
7
  ): string {
8
8
  return `${ctx.path}?${qs.stringify(query_params)}`;