mobiletto-orm-typedef-gen 2.1.12 → 2.1.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.
@@ -7,6 +7,8 @@ export type TypeDeclaration = {
7
7
  };
8
8
  export type GenerateOptions = {
9
9
  outfile?: string;
10
+ header?: string;
11
+ typescript?: boolean;
10
12
  name?: string;
11
13
  disclaimer?: string;
12
14
  prepareContext?: (typeDef: MobilettoOrmTypeDef, ctx: Record<string, unknown>) => Record<string, unknown>;
@@ -80,6 +80,12 @@ const defaultPrepareContext = (typeDef, ctx) => {
80
80
  field.whenFieldNamesQuoted = field.whenFieldNames
81
81
  .filter((f) => f !== fieldName)
82
82
  .map((f) => JSON.stringify(f));
83
+ field.whenFieldSignature = field.whenFieldNames
84
+ .map((f) => {
85
+ const fld = ctx.fields[f];
86
+ return f + (fld.required ? "" : "?") + ": " + fld.type;
87
+ })
88
+ .join(", ");
83
89
  }
84
90
  if (field.test && field.test.message && field.test.valid) {
85
91
  field.testCode = field.test.valid.toString();
@@ -98,6 +104,7 @@ export const generate = (typeDef, templatePath, opts) => {
98
104
  if (!(typeDef instanceof MobilettoOrmTypeDef)) {
99
105
  typeDef = new MobilettoOrmTypeDef(typeDef);
100
106
  }
107
+ const typescript = !opts || opts.typescript !== false;
101
108
  const disclaimer = (opts === null || opts === void 0 ? void 0 : opts.disclaimer) ? opts.disclaimer : STANDARD_AUTOGEN_FILE_DISCLAIMER;
102
109
  const name = (opts === null || opts === void 0 ? void 0 : opts.name) ? opts.name : typeDef.typeName;
103
110
  const decls = [...findDecls(name, typeDef.fields), { name, fields: typeDef.fields, root: true }];
@@ -114,8 +121,10 @@ export const generate = (typeDef, templatePath, opts) => {
114
121
  fields: decl.fields,
115
122
  fieldNames: Object.keys(decl.fields),
116
123
  disclaimer: decls.length > 0 ? null : disclaimer,
124
+ header: decls.length > 0 ? null : opts === null || opts === void 0 ? void 0 : opts.header,
117
125
  first,
118
126
  root: decl.root || false,
127
+ typescript,
119
128
  });
120
129
  const preparedContext = (opts === null || opts === void 0 ? void 0 : opts.prepareContext) ? opts.prepareContext(typeDef, typeContext) : typeContext;
121
130
  first = false;
@@ -1,4 +1,5 @@
1
- {{#if disclaimer}}// {{{ disclaimer }}}
1
+ {{#if disclaimer}}// {{{ disclaimer }}}{{#if header}}
2
+ {{{header}}}{{/if}}
2
3
  {{/if}}export type {{ Name }}Type = {{#if root}}{
3
4
  _meta?: {
4
5
  id: string;
@@ -1,4 +1,5 @@
1
- {{#if disclaimer}}// {{{ disclaimer }}}
1
+ {{#if disclaimer}}// {{{ disclaimer }}}{{#if header}}
2
+ {{{header}}}{{/if}}
2
3
  import * as yup from "yup";{{/if}}
3
4
  export const {{Name}}Schema = yup.object({{{curlyOpen}}}{{#each fields}}
4
5
  {{name}}: {{#if typeIsObject}}{{ ../Name }}_{{name}}Schema{{else}}{{#if typeIsArray}}yup.array().of(yup.{{valueType}}{{else}}yup.{{type}}{{/if}}(){{#if isEnum}}
@@ -17,15 +18,15 @@ export const {{Name}}Schema = yup.object({{{curlyOpen}}}{{#each fields}}
17
18
  name: "{{test.message}}",
18
19
  message: '{{fullName}}_{{test.message}}',
19
20
  skipAbsent: true,
20
- test(value, ctx) {
21
+ test(value{{#if typescript}}: string | number | boolean | null | undefined{{/if}}, ctx) {
21
22
  const t = {{{testCode}}};
22
23
  return typeof value === "undefined" || value == null || t({ ...ctx.parent });
23
24
  }
24
25
  }){{/if}}{{#if hasWhen}}
25
26
  .when([{{{whenFieldNamesQuoted}}}], {
26
- is: ({{whenFieldNames}}) => {
27
+ is: ({{#if typescript}}{{whenFieldSignature}}{{else}}{{whenFieldNames}}{{/if}}) => {
27
28
  const w = {{{whenCode}}};
28
- const v = { {{whenFieldNames}} };
29
+ const v{{#if typescript}}: Record<string, unknown>{{/if}} = { {{whenFieldNames}} };
29
30
  return w(v);
30
31
  },
31
32
  then: (schema) => schema.required('{{fullName}}_required'),
@@ -7,6 +7,8 @@ export type TypeDeclaration = {
7
7
  };
8
8
  export type GenerateOptions = {
9
9
  outfile?: string;
10
+ header?: string;
11
+ typescript?: boolean;
10
12
  name?: string;
11
13
  disclaimer?: string;
12
14
  prepareContext?: (typeDef: MobilettoOrmTypeDef, ctx: Record<string, unknown>) => Record<string, unknown>;
@@ -80,6 +80,12 @@ const defaultPrepareContext = (typeDef, ctx) => {
80
80
  field.whenFieldNamesQuoted = field.whenFieldNames
81
81
  .filter((f) => f !== fieldName)
82
82
  .map((f) => JSON.stringify(f));
83
+ field.whenFieldSignature = field.whenFieldNames
84
+ .map((f) => {
85
+ const fld = ctx.fields[f];
86
+ return f + (fld.required ? "" : "?") + ": " + fld.type;
87
+ })
88
+ .join(", ");
83
89
  }
84
90
  if (field.test && field.test.message && field.test.valid) {
85
91
  field.testCode = field.test.valid.toString();
@@ -98,6 +104,7 @@ export const generate = (typeDef, templatePath, opts) => {
98
104
  if (!(typeDef instanceof MobilettoOrmTypeDef)) {
99
105
  typeDef = new MobilettoOrmTypeDef(typeDef);
100
106
  }
107
+ const typescript = !opts || opts.typescript !== false;
101
108
  const disclaimer = (opts === null || opts === void 0 ? void 0 : opts.disclaimer) ? opts.disclaimer : STANDARD_AUTOGEN_FILE_DISCLAIMER;
102
109
  const name = (opts === null || opts === void 0 ? void 0 : opts.name) ? opts.name : typeDef.typeName;
103
110
  const decls = [...findDecls(name, typeDef.fields), { name, fields: typeDef.fields, root: true }];
@@ -114,8 +121,10 @@ export const generate = (typeDef, templatePath, opts) => {
114
121
  fields: decl.fields,
115
122
  fieldNames: Object.keys(decl.fields),
116
123
  disclaimer: decls.length > 0 ? null : disclaimer,
124
+ header: decls.length > 0 ? null : opts === null || opts === void 0 ? void 0 : opts.header,
117
125
  first,
118
126
  root: decl.root || false,
127
+ typescript,
119
128
  });
120
129
  const preparedContext = (opts === null || opts === void 0 ? void 0 : opts.prepareContext) ? opts.prepareContext(typeDef, typeContext) : typeContext;
121
130
  first = false;
@@ -1,4 +1,5 @@
1
- {{#if disclaimer}}// {{{ disclaimer }}}
1
+ {{#if disclaimer}}// {{{ disclaimer }}}{{#if header}}
2
+ {{{header}}}{{/if}}
2
3
  {{/if}}export type {{ Name }}Type = {{#if root}}{
3
4
  _meta?: {
4
5
  id: string;
@@ -1,4 +1,5 @@
1
- {{#if disclaimer}}// {{{ disclaimer }}}
1
+ {{#if disclaimer}}// {{{ disclaimer }}}{{#if header}}
2
+ {{{header}}}{{/if}}
2
3
  import * as yup from "yup";{{/if}}
3
4
  export const {{Name}}Schema = yup.object({{{curlyOpen}}}{{#each fields}}
4
5
  {{name}}: {{#if typeIsObject}}{{ ../Name }}_{{name}}Schema{{else}}{{#if typeIsArray}}yup.array().of(yup.{{valueType}}{{else}}yup.{{type}}{{/if}}(){{#if isEnum}}
@@ -17,15 +18,15 @@ export const {{Name}}Schema = yup.object({{{curlyOpen}}}{{#each fields}}
17
18
  name: "{{test.message}}",
18
19
  message: '{{fullName}}_{{test.message}}',
19
20
  skipAbsent: true,
20
- test(value, ctx) {
21
+ test(value{{#if typescript}}: string | number | boolean | null | undefined{{/if}}, ctx) {
21
22
  const t = {{{testCode}}};
22
23
  return typeof value === "undefined" || value == null || t({ ...ctx.parent });
23
24
  }
24
25
  }){{/if}}{{#if hasWhen}}
25
26
  .when([{{{whenFieldNamesQuoted}}}], {
26
- is: ({{whenFieldNames}}) => {
27
+ is: ({{#if typescript}}{{whenFieldSignature}}{{else}}{{whenFieldNames}}{{/if}}) => {
27
28
  const w = {{{whenCode}}};
28
- const v = { {{whenFieldNames}} };
29
+ const v{{#if typescript}}: Record<string, unknown>{{/if}} = { {{whenFieldNames}} };
29
30
  return w(v);
30
31
  },
31
32
  then: (schema) => schema.required('{{fullName}}_required'),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mobiletto-orm-typedef-gen",
3
- "version": "2.1.12",
3
+ "version": "2.1.14",
4
4
  "type": "module",
5
5
  "description": "generate various files based on mobiletto-orm-typedef definitions",
6
6
  "keywords": [