@sealcode/sealgen 0.4.2 → 0.5.2

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 (105) 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 +9 -9
  17. package/@types/index.d.ts +2 -2
  18. package/@types/make-hidden-inputs.d.ts +4 -3
  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 +8 -8
  22. package/@types/page/page.d.ts +5 -5
  23. package/@types/page/props-parser.d.ts +4 -4
  24. package/@types/utils/get-prettier-config.d.ts +2 -0
  25. package/@types/utils/prettier.d.ts +2 -0
  26. package/@types/utils/translate.d.ts +2 -2
  27. package/@types/utils/url-with-new-params.d.ts +2 -2
  28. package/lib/build.js +3 -2
  29. package/lib/build.js.map +1 -1
  30. package/lib/forms/controls/checkboxed-list-input.js.map +1 -1
  31. package/lib/forms/controls/controls.js +3 -1
  32. package/lib/forms/controls/controls.js.map +1 -1
  33. package/lib/forms/controls/dropdown.js.map +1 -1
  34. package/lib/forms/controls/editable-collection-subset.js.map +1 -1
  35. package/lib/forms/controls/form-header.js.map +1 -1
  36. package/lib/forms/controls/html.js +19 -0
  37. package/lib/forms/controls/html.js.map +1 -0
  38. package/lib/forms/controls/simple-input.js +3 -1
  39. package/lib/forms/controls/simple-input.js.map +1 -1
  40. package/lib/forms/fields/checkboxed-list.js.map +1 -1
  41. package/lib/forms/fields/collection-field.js.map +1 -1
  42. package/lib/forms/fields/field.js.map +1 -1
  43. package/lib/forms/fields/number.js.map +1 -1
  44. package/lib/forms/fields/pick-from-list.js.map +1 -1
  45. package/lib/forms/fields/simple-form-field.js.map +1 -1
  46. package/lib/forms/form.js.map +1 -1
  47. package/lib/generate-collections.js +22 -1
  48. package/lib/generate-collections.js.map +1 -1
  49. package/lib/generate-routes.js +35 -11
  50. package/lib/generate-routes.js.map +1 -1
  51. package/lib/index.js +15 -2
  52. package/lib/index.js.map +1 -1
  53. package/lib/make-hidden-inputs.js.map +1 -1
  54. package/lib/page/access-control.js.map +1 -1
  55. package/lib/page/list.js.map +1 -1
  56. package/lib/page/mountable.js.map +1 -1
  57. package/lib/page/page.js.map +1 -1
  58. package/lib/page/props-parser.js.map +1 -1
  59. package/lib/templates/form.js +9 -16
  60. package/lib/templates/form.js.map +1 -1
  61. package/lib/templates/list.js +4 -4
  62. package/lib/templates/list.js.map +1 -1
  63. package/lib/templates/page.js +3 -3
  64. package/lib/templates/route-test.js +1 -1
  65. package/lib/templates/route-test.js.map +1 -1
  66. package/lib/utils/get-prettier-config.js +36 -0
  67. package/lib/utils/get-prettier-config.js.map +1 -0
  68. package/lib/utils/prettier.js +36 -0
  69. package/lib/utils/prettier.js.map +1 -0
  70. package/lib/utils/translate.js.map +1 -1
  71. package/lib/utils/url-with-new-params.js.map +1 -1
  72. package/package.json +4 -3
  73. package/src/build.ts +3 -2
  74. package/src/forms/controls/checkboxed-list-input.ts +2 -2
  75. package/src/forms/controls/controls.ts +1 -0
  76. package/src/forms/controls/dropdown.ts +2 -2
  77. package/src/forms/controls/editable-collection-subset.ts +2 -2
  78. package/src/forms/controls/form-control.ts +2 -2
  79. package/src/forms/controls/form-field-control.ts +2 -2
  80. package/src/forms/controls/form-header.ts +3 -3
  81. package/src/forms/controls/html.ts +18 -0
  82. package/src/forms/controls/simple-input.ts +6 -5
  83. package/src/forms/fields/checkboxed-list.ts +5 -5
  84. package/src/forms/fields/collection-field.ts +3 -3
  85. package/src/forms/fields/field.ts +6 -6
  86. package/src/forms/fields/number.ts +3 -3
  87. package/src/forms/fields/pick-from-list.ts +5 -5
  88. package/src/forms/fields/simple-form-field.ts +2 -2
  89. package/src/forms/form.ts +9 -9
  90. package/src/generate-collections.ts +3 -1
  91. package/src/generate-routes.ts +31 -13
  92. package/src/index.ts +2 -2
  93. package/src/make-hidden-inputs.ts +4 -3
  94. package/src/page/access-control.ts +2 -2
  95. package/src/page/list.ts +14 -23
  96. package/src/page/mountable.ts +9 -9
  97. package/src/page/page.ts +5 -5
  98. package/src/page/props-parser.ts +4 -7
  99. package/src/templates/form.ts +9 -16
  100. package/src/templates/list.ts +4 -4
  101. package/src/templates/page.ts +3 -3
  102. package/src/templates/route-test.ts +1 -1
  103. package/src/utils/prettier.ts +13 -0
  104. package/src/utils/translate.ts +2 -2
  105. package/src/utils/url-with-new-params.ts +2 -2
package/src/page/list.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { BaseContext } from "koa";
1
+ import { Context } from "koa";
2
2
  import { Collection, CollectionItem, ItemFields, ItemList } from "sealious";
3
3
 
4
4
  import { Templatable, tempstream, FlatTemplatable } from "tempstream";
@@ -24,22 +24,19 @@ export abstract class ListPage<
24
24
  F extends typeof BaseListPageFields
25
25
  > extends Page<F> {
26
26
  abstract getItems(
27
- ctx: BaseContext,
27
+ ctx: Context,
28
28
  page: number,
29
29
  itemsPerPage: number,
30
30
  values: Record<string, FormDataValue>
31
31
  ): Promise<ItemType[]>;
32
32
 
33
33
  abstract getTotalPages(
34
- ctx: BaseContext,
34
+ ctx: Context,
35
35
  itemsPerPage: number,
36
36
  values: Record<string, FormDataValue>
37
37
  ): Promise<number>;
38
38
 
39
- abstract renderItem(
40
- ctx: BaseContext,
41
- item: ItemType
42
- ): Promise<FlatTemplatable>;
39
+ abstract renderItem(ctx: Context, item: ItemType): Promise<FlatTemplatable>;
43
40
 
44
41
  filterFields: Record<string, FormField> = {};
45
42
  filterControls: FormFieldControl[] = [];
@@ -51,11 +48,11 @@ export abstract class ListPage<
51
48
  }
52
49
  }
53
50
 
54
- renderListContainer(_: BaseContext, content: Templatable): FlatTemplatable {
51
+ renderListContainer(_: Context, content: Templatable): FlatTemplatable {
55
52
  return tempstream`<div>${content}</div>`;
56
53
  }
57
54
 
58
- async render(ctx: BaseContext): Promise<FlatTemplatable> {
55
+ async render(ctx: Context): Promise<FlatTemplatable> {
59
56
  const values = this.extractRawValues(ctx);
60
57
  const { parsed: page } = await this.fields.page.getValue(ctx, values);
61
58
  const { parsed: itemsPerPage } =
@@ -73,7 +70,7 @@ export abstract class ListPage<
73
70
  }
74
71
 
75
72
  async renderPagination(
76
- ctx: BaseContext,
73
+ ctx: Context,
77
74
  values: Record<string, FormDataValue>
78
75
  ): Promise<FlatTemplatable> {
79
76
  const { parsed: page } = await this.fields.page.getValue(ctx, values);
@@ -112,7 +109,7 @@ export abstract class ListPage<
112
109
  </center>`;
113
110
  }
114
111
 
115
- private renderPageButton(ctx: BaseContext, page: number, text: string) {
112
+ private renderPageButton(ctx: Context, page: number, text: string) {
116
113
  return /* HTML */ `<a
117
114
  href="${UrlWithNewParams(
118
115
  ctx,
@@ -125,7 +122,7 @@ export abstract class ListPage<
125
122
  >`;
126
123
  }
127
124
 
128
- async getFilterValues(ctx: BaseContext): Promise<Record<string, unknown>> {
125
+ async getFilterValues(ctx: Context): Promise<Record<string, unknown>> {
129
126
  const filter = {} as Record<string, unknown>;
130
127
  const raw_values = this.extractRawValues(ctx);
131
128
  for (const [fieldname, field] of Object.entries(this.filterFields)) {
@@ -136,7 +133,7 @@ export abstract class ListPage<
136
133
  return filter;
137
134
  }
138
135
 
139
- renderFilters(ctx: BaseContext): FlatTemplatable {
136
+ renderFilters(ctx: Context): FlatTemplatable {
140
137
  const values = this.extractRawValues(ctx);
141
138
  return tempstream/* HTML */ `<form method="GET">
142
139
  ${makeHiddenInputs(ctx, this.fields, values, [
@@ -159,20 +156,14 @@ export abstract class SealiousItemListPage<
159
156
  super();
160
157
  }
161
158
 
162
- async getTotalPages(
163
- ctx: BaseContext,
164
- itemsPerPage: number
165
- ): Promise<number> {
159
+ async getTotalPages(ctx: Context, itemsPerPage: number): Promise<number> {
166
160
  const query = this.collection.list(ctx.$context);
167
161
  await this.addFilter(ctx, query);
168
162
  const { items } = await query.fetch();
169
163
  return Math.ceil(items.length / itemsPerPage);
170
164
  }
171
165
 
172
- protected async addFilter(
173
- ctx: BaseContext,
174
- list: ItemList<C>
175
- ): Promise<void> {
166
+ protected async addFilter(ctx: Context, list: ItemList<C>): Promise<void> {
176
167
  const filters = await this.getFilterValues(ctx);
177
168
  if (Object.keys(filters).length) {
178
169
  list.filter(filters as Partial<ItemFields<C>>);
@@ -180,7 +171,7 @@ export abstract class SealiousItemListPage<
180
171
  }
181
172
 
182
173
  async getItems(
183
- ctx: BaseContext,
174
+ ctx: Context,
184
175
  page: number,
185
176
  itemsPerPage: number
186
177
  ): Promise<CollectionItem<C>[]> {
@@ -193,7 +184,7 @@ export abstract class SealiousItemListPage<
193
184
  }
194
185
 
195
186
  async renderItem(
196
- _: BaseContext,
187
+ _: Context,
197
188
  item: CollectionItem<C>
198
189
  ): Promise<FlatTemplatable> {
199
190
  return `<div>${item.id}</div>`;
@@ -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
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,7 +92,7 @@ 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
97
  ): FlatTemplatable {
98
98
  return tempstream/* HTML */ `<div class="form-messages">
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<FlatTemplatable>;
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 * as prettier from "prettier";
2
+ import { Options } from "prettier";
3
+ import _locreq from "locreq";
4
+ const target_locreq = _locreq(process.cwd());
5
+
6
+ export async function getPrettierConfig(): Promise<Options> {
7
+ return {
8
+ ...(await prettier.resolveConfig(
9
+ target_locreq.resolve("src/back/routes")
10
+ )),
11
+ parser: "typescript",
12
+ };
13
+ }
@@ -1,8 +1,8 @@
1
- import { BaseContext } from "koa";
1
+ import { Context } from "koa";
2
2
  import { Translation } from "sealious";
3
3
 
4
4
  export function t(
5
- ctx: BaseContext,
5
+ ctx: Context,
6
6
  key: string,
7
7
  props: any[],
8
8
  default_translation: Translation
@@ -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)}`;