@omnifyjp/omnify 3.13.0 → 3.14.0
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.
- package/package.json +6 -6
- package/ts-dist/generator.js +8 -0
- package/ts-dist/metadata-generator.d.ts +2 -0
- package/ts-dist/metadata-generator.js +6 -0
- package/ts-dist/payload-builder-generator.d.ts +9 -0
- package/ts-dist/payload-builder-generator.js +80 -12
- package/ts-dist/php/index.js +4 -1
- package/ts-dist/php/schema-reader.d.ts +11 -0
- package/ts-dist/php/schema-reader.js +18 -0
- package/ts-dist/php/service-generator.d.ts +6 -2
- package/ts-dist/php/service-generator.js +573 -263
- package/ts-dist/ts-hooks-generator.d.ts +10 -0
- package/ts-dist/ts-hooks-generator.js +131 -0
- package/ts-dist/ts-query-keys-generator.d.ts +9 -0
- package/ts-dist/ts-query-keys-generator.js +52 -0
- package/ts-dist/ts-service-generator.d.ts +10 -0
- package/ts-dist/ts-service-generator.js +190 -0
- package/ts-dist/types.d.ts +12 -0
- package/ts-dist/zod-generator.js +33 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@omnifyjp/omnify",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.14.0",
|
|
4
4
|
"description": "Schema-driven code generation for Laravel, TypeScript, and SQL",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -36,10 +36,10 @@
|
|
|
36
36
|
"zod": "^3.24.0"
|
|
37
37
|
},
|
|
38
38
|
"optionalDependencies": {
|
|
39
|
-
"@omnifyjp/omnify-darwin-arm64": "3.
|
|
40
|
-
"@omnifyjp/omnify-darwin-x64": "3.
|
|
41
|
-
"@omnifyjp/omnify-linux-x64": "3.
|
|
42
|
-
"@omnifyjp/omnify-linux-arm64": "3.
|
|
43
|
-
"@omnifyjp/omnify-win32-x64": "3.
|
|
39
|
+
"@omnifyjp/omnify-darwin-arm64": "3.14.0",
|
|
40
|
+
"@omnifyjp/omnify-darwin-x64": "3.14.0",
|
|
41
|
+
"@omnifyjp/omnify-linux-x64": "3.14.0",
|
|
42
|
+
"@omnifyjp/omnify-linux-arm64": "3.14.0",
|
|
43
|
+
"@omnifyjp/omnify-win32-x64": "3.14.0"
|
|
44
44
|
}
|
|
45
45
|
}
|
package/ts-dist/generator.js
CHANGED
|
@@ -17,6 +17,9 @@ import { generateEnums, generatePluginEnums, formatEnum, formatTypeAlias, extrac
|
|
|
17
17
|
import { generateZodSchemas, generateDisplayNames, getExcludedFields, formatZodSchemasSection, formatZodModelFile, } from './zod-generator.js';
|
|
18
18
|
import { generateI18nFileContent } from './i18n-generator.js';
|
|
19
19
|
import { buildSchemaMetadata, formatMetadataConst, } from './metadata-generator.js';
|
|
20
|
+
import { generateTsServices } from './ts-service-generator.js';
|
|
21
|
+
import { generateTsQueryKeys } from './ts-query-keys-generator.js';
|
|
22
|
+
import { generateTsHooks } from './ts-hooks-generator.js';
|
|
20
23
|
import { buildFormShape, formatPayloadBuilderSection, } from './payload-builder-generator.js';
|
|
21
24
|
/** Auto-generated file header. */
|
|
22
25
|
function generateBaseHeader() {
|
|
@@ -493,6 +496,11 @@ export function generateTypeScript(input) {
|
|
|
493
496
|
files.push(generatePayloadHelpersFile(options));
|
|
494
497
|
// I18n
|
|
495
498
|
files.push(generateI18nFile(options));
|
|
499
|
+
// Frontend service layer: BaseService + QueryKeys + Hooks (issue #58)
|
|
500
|
+
// Only for schemas with options.api or options.service.
|
|
501
|
+
files.push(...generateTsServices(schemas, options));
|
|
502
|
+
files.push(...generateTsQueryKeys(schemas));
|
|
503
|
+
files.push(...generateTsHooks(schemas));
|
|
496
504
|
// Index re-exports
|
|
497
505
|
files.push(generateIndexFile(schemas, schemaEnums, pluginEnums, inlineTypeAliases, hasFiles));
|
|
498
506
|
return files;
|
|
@@ -44,6 +44,8 @@ export interface MetadataField {
|
|
|
44
44
|
label: Record<string, string>;
|
|
45
45
|
/** Localized placeholder dictionary (locale → string). Optional. */
|
|
46
46
|
placeholder?: Record<string, string>;
|
|
47
|
+
/** Localized description dictionary (locale → string). Optional. Issue #59. */
|
|
48
|
+
description?: Record<string, string>;
|
|
47
49
|
}
|
|
48
50
|
/** Aggregations exported alongside the per-field entries for ergonomics. */
|
|
49
51
|
export interface MetadataAggregations {
|
|
@@ -191,6 +191,9 @@ export function buildSchemaMetadata(schema, allSchemas, options, displayNames) {
|
|
|
191
191
|
const placeholder = displayNames.propertyPlaceholders[fieldKey];
|
|
192
192
|
if (placeholder)
|
|
193
193
|
meta.placeholder = placeholder;
|
|
194
|
+
const description = displayNames.propertyDescriptions[fieldKey];
|
|
195
|
+
if (description)
|
|
196
|
+
meta.description = description;
|
|
194
197
|
if (fieldType === 'enum') {
|
|
195
198
|
if (typeof prop.enum === 'string') {
|
|
196
199
|
meta.enum = prop.enum;
|
|
@@ -314,6 +317,9 @@ export function formatMetadataConst(meta) {
|
|
|
314
317
|
if (field.placeholder) {
|
|
315
318
|
parts.push(` placeholder: ${jsLiteral(field.placeholder, 6)},\n`);
|
|
316
319
|
}
|
|
320
|
+
if (field.description) {
|
|
321
|
+
parts.push(` description: ${jsLiteral(field.description, 6)},\n`);
|
|
322
|
+
}
|
|
317
323
|
parts.push(` },\n`);
|
|
318
324
|
}
|
|
319
325
|
parts.push(` },\n\n`);
|
|
@@ -38,6 +38,15 @@ export interface FormField {
|
|
|
38
38
|
nullable: boolean;
|
|
39
39
|
/** Default value for `empty<Model>CreateForm()`, as a JS literal string. */
|
|
40
40
|
defaultLiteral: string;
|
|
41
|
+
/**
|
|
42
|
+
* When true, the field is intentionally omitted from the
|
|
43
|
+
* `empty<Model>CreateForm()` factory body. Used for optional enum
|
|
44
|
+
* reference fields where no sensible default exists — the interface
|
|
45
|
+
* marks the field `?:` so omitting the key is valid, and this is
|
|
46
|
+
* safer than emitting `undefined` under `exactOptionalPropertyTypes`.
|
|
47
|
+
* Issue omnify-jp/omnify-go#56.
|
|
48
|
+
*/
|
|
49
|
+
omitFromEmpty?: boolean;
|
|
41
50
|
}
|
|
42
51
|
/** Resolved per-schema form metadata. */
|
|
43
52
|
export interface SchemaFormShape {
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
*/
|
|
23
23
|
import { toSnakeCase } from './interface-generator.js';
|
|
24
24
|
import { classifyFieldType } from './metadata-generator.js';
|
|
25
|
+
import { toEnumMemberName } from './enum-generator.js';
|
|
25
26
|
// ---------------------------------------------------------------------------
|
|
26
27
|
// Build the IR
|
|
27
28
|
// ---------------------------------------------------------------------------
|
|
@@ -71,26 +72,86 @@ function tsTypeForField(metaType, prop) {
|
|
|
71
72
|
return 'string';
|
|
72
73
|
}
|
|
73
74
|
}
|
|
74
|
-
|
|
75
|
-
|
|
75
|
+
/**
|
|
76
|
+
* Resolve an enum's value list either from a plugin enum bank
|
|
77
|
+
* (customTypes.enums) or from a schema-defined enum (`kind: enum` schemas).
|
|
78
|
+
* Returns undefined if the enum reference can't be resolved at codegen time.
|
|
79
|
+
*/
|
|
80
|
+
function resolveEnumValueList(prop, allSchemas, pluginEnums) {
|
|
81
|
+
if (typeof prop.enum === 'string') {
|
|
82
|
+
// Plugin enum first (customTypes.enums), then schema enum.
|
|
83
|
+
const fromPlugin = pluginEnums[prop.enum];
|
|
84
|
+
if (fromPlugin)
|
|
85
|
+
return fromPlugin;
|
|
86
|
+
const schemaEnum = allSchemas[prop.enum];
|
|
87
|
+
if (schemaEnum?.values && schemaEnum.values.length > 0) {
|
|
88
|
+
return schemaEnum.values.map((v) => v.value);
|
|
89
|
+
}
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
if (Array.isArray(prop.enum)) {
|
|
93
|
+
return prop.enum.map((v) => typeof v === 'string' ? v : v.value);
|
|
94
|
+
}
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
function defaultLiteralForField(metaType, prop, required, allSchemas, pluginEnums) {
|
|
98
|
+
// -----------------------------------------------------------------
|
|
99
|
+
// Enum fields — reference the enum member, not a string literal.
|
|
100
|
+
//
|
|
101
|
+
// `status: 'free'` is not assignable to `TableStatus` under strict
|
|
102
|
+
// TypeScript because enum types are nominal. We must emit
|
|
103
|
+
// `TableStatus.Free` instead. For inline enums (array of strings) the
|
|
104
|
+
// TS type is a literal union, so string literals are fine — that case
|
|
105
|
+
// falls through to the JSON.stringify path at the bottom.
|
|
106
|
+
//
|
|
107
|
+
// Issue omnify-jp/omnify-go#56.
|
|
108
|
+
// -----------------------------------------------------------------
|
|
109
|
+
if (metaType === 'enum' && typeof prop.enum === 'string') {
|
|
110
|
+
const enumName = prop.enum;
|
|
111
|
+
// Optional / nullable enum refs: omit from the factory body entirely.
|
|
112
|
+
// The form-state interface already marks the field `?:`, so skipping
|
|
113
|
+
// the key is a valid object literal under strict TS regardless of
|
|
114
|
+
// `exactOptionalPropertyTypes`.
|
|
115
|
+
if (!required) {
|
|
116
|
+
return { literal: '', omit: true };
|
|
117
|
+
}
|
|
118
|
+
// Pick the default value: honor YAML `default` first, else first
|
|
119
|
+
// declared enum member.
|
|
120
|
+
let rawValue;
|
|
121
|
+
if (prop.default !== undefined && prop.default !== null) {
|
|
122
|
+
rawValue = String(prop.default);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
const values = resolveEnumValueList(prop, allSchemas, pluginEnums);
|
|
126
|
+
if (values && values.length > 0) {
|
|
127
|
+
rawValue = values[0];
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (rawValue === undefined) {
|
|
131
|
+
// Can't resolve the enum values at codegen time (unlikely, but
|
|
132
|
+
// keeps the generator total). Fall back to omitting — the dev
|
|
133
|
+
// will get a TS error at form init and can fix it manually.
|
|
134
|
+
return { literal: '', omit: true };
|
|
135
|
+
}
|
|
136
|
+
return { literal: `${enumName}.${toEnumMemberName(rawValue)}` };
|
|
137
|
+
}
|
|
138
|
+
// Honor an explicit YAML default when present (non-enum path).
|
|
76
139
|
if (prop.default !== undefined && prop.default !== null) {
|
|
77
|
-
return JSON.stringify(prop.default);
|
|
140
|
+
return { literal: JSON.stringify(prop.default) };
|
|
78
141
|
}
|
|
79
|
-
//
|
|
80
|
-
//
|
|
142
|
+
// Inline enum (array form) — the form-state tsType is a literal union,
|
|
143
|
+
// so a string literal is directly assignable. Use the first value.
|
|
81
144
|
if (metaType === 'enum') {
|
|
82
145
|
if (Array.isArray(prop.enum) && prop.enum.length > 0) {
|
|
83
146
|
const first = prop.enum[0];
|
|
84
147
|
const val = typeof first === 'string' ? first : first.value;
|
|
85
|
-
return JSON.stringify(val);
|
|
148
|
+
return { literal: JSON.stringify(val) };
|
|
86
149
|
}
|
|
87
|
-
|
|
88
|
-
// string and let the dev select on first interaction.
|
|
89
|
-
return "''";
|
|
150
|
+
return { literal: "''" };
|
|
90
151
|
}
|
|
91
152
|
if (metaType === 'boolean')
|
|
92
|
-
return 'false';
|
|
93
|
-
return PRIMITIVE_TS_DEFAULTS[metaType];
|
|
153
|
+
return { literal: 'false' };
|
|
154
|
+
return { literal: PRIMITIVE_TS_DEFAULTS[metaType] };
|
|
94
155
|
}
|
|
95
156
|
/** Build the form shape IR from a schema. */
|
|
96
157
|
export function buildFormShape(schema, allSchemas, options) {
|
|
@@ -129,6 +190,7 @@ export function buildFormShape(schema, allSchemas, options) {
|
|
|
129
190
|
: prop.rules?.required === true
|
|
130
191
|
? true
|
|
131
192
|
: !(prop.nullable ?? false);
|
|
193
|
+
const { literal, omit } = defaultLiteralForField(metaType, prop, required, allSchemas, options.customTypes.enums);
|
|
132
194
|
fields.push({
|
|
133
195
|
fieldName,
|
|
134
196
|
tsType: tsTypeForField(metaType, prop),
|
|
@@ -136,7 +198,8 @@ export function buildFormShape(schema, allSchemas, options) {
|
|
|
136
198
|
translatable: prop.translatable === true,
|
|
137
199
|
required,
|
|
138
200
|
nullable: prop.nullable ?? false,
|
|
139
|
-
defaultLiteral:
|
|
201
|
+
defaultLiteral: literal,
|
|
202
|
+
omitFromEmpty: omit,
|
|
140
203
|
});
|
|
141
204
|
}
|
|
142
205
|
return { modelName: schema.name, fields };
|
|
@@ -184,6 +247,11 @@ export function formatPayloadBuilderSection(shape, defaultLocale) {
|
|
|
184
247
|
parts.push(`export function empty${modelName}CreateForm(): ${modelName}CreateFormState {\n`);
|
|
185
248
|
parts.push(` return {\n`);
|
|
186
249
|
for (const f of fields) {
|
|
250
|
+
// Skip fields marked for omission (e.g. optional enum references
|
|
251
|
+
// with no sensible default — the interface marks them `?:`).
|
|
252
|
+
// Issue omnify-jp/omnify-go#56.
|
|
253
|
+
if (f.omitFromEmpty)
|
|
254
|
+
continue;
|
|
187
255
|
if (f.translatable) {
|
|
188
256
|
parts.push(` ${f.fieldName}: emptyLocaleMap(),\n`);
|
|
189
257
|
}
|
package/ts-dist/php/index.js
CHANGED
|
@@ -60,7 +60,6 @@ export function generatePhp(data, overrides) {
|
|
|
60
60
|
if (reader.hasApiSchemas()) {
|
|
61
61
|
files.push(...generateSchemaConfig(reader, config));
|
|
62
62
|
files.push(...generateControllers(reader, config));
|
|
63
|
-
files.push(...generateServices(reader, config));
|
|
64
63
|
files.push(...generateRoutes(reader, config));
|
|
65
64
|
// Issue #35: opt-in OpenAPI / Swagger annotation scaffold.
|
|
66
65
|
// Controller-level method attributes are emitted inline by
|
|
@@ -68,6 +67,10 @@ export function generatePhp(data, overrides) {
|
|
|
68
67
|
// we add the standalone Common / Info / Schemas files.
|
|
69
68
|
files.push(...generateOpenApi(reader, config));
|
|
70
69
|
}
|
|
70
|
+
// Service layer (schemas with options.api OR options.service). Issue #57.
|
|
71
|
+
if (reader.hasApiSchemas() || reader.hasServiceSchemas()) {
|
|
72
|
+
files.push(...generateServices(reader, config));
|
|
73
|
+
}
|
|
71
74
|
// Issue #34: every `kind: enum` schema becomes a global PHP enum class.
|
|
72
75
|
files.push(...generateEnums(reader, config));
|
|
73
76
|
// Per-schema files
|
|
@@ -57,6 +57,17 @@ export declare class SchemaReader {
|
|
|
57
57
|
getFileConfig(): import("../types.js").FileConfigExport | null;
|
|
58
58
|
/** Get all schemas that have File-type properties. */
|
|
59
59
|
getSchemasWithFileProperties(): Record<string, SchemaDefinition>;
|
|
60
|
+
/** Get global audit config from schemas.json. */
|
|
61
|
+
getAuditConfig(): {
|
|
62
|
+
model?: string;
|
|
63
|
+
createdBy?: boolean;
|
|
64
|
+
updatedBy?: boolean;
|
|
65
|
+
deletedBy?: boolean;
|
|
66
|
+
} | null;
|
|
67
|
+
/** Get schemas that have service options configured (options.service). */
|
|
68
|
+
getSchemasWithService(): Record<string, import('../types.js').SchemaDefinition>;
|
|
69
|
+
/** Check if any schema has service options. */
|
|
70
|
+
hasServiceSchemas(): boolean;
|
|
60
71
|
/** Get translatable field names (snake_case) for a schema. */
|
|
61
72
|
getTranslatableFields(schemaName: string): string[];
|
|
62
73
|
}
|
|
@@ -201,6 +201,24 @@ export class SchemaReader {
|
|
|
201
201
|
}
|
|
202
202
|
return result;
|
|
203
203
|
}
|
|
204
|
+
/** Get global audit config from schemas.json. */
|
|
205
|
+
getAuditConfig() {
|
|
206
|
+
return this.data.auditConfig ?? null;
|
|
207
|
+
}
|
|
208
|
+
/** Get schemas that have service options configured (options.service). */
|
|
209
|
+
getSchemasWithService() {
|
|
210
|
+
const result = {};
|
|
211
|
+
for (const [name, schema] of Object.entries(this.getProjectVisibleObjectSchemas())) {
|
|
212
|
+
if (schema.options?.service) {
|
|
213
|
+
result[name] = schema;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return result;
|
|
217
|
+
}
|
|
218
|
+
/** Check if any schema has service options. */
|
|
219
|
+
hasServiceSchemas() {
|
|
220
|
+
return Object.keys(this.getSchemasWithService()).length > 0;
|
|
221
|
+
}
|
|
204
222
|
/** Get translatable field names (snake_case) for a schema. */
|
|
205
223
|
getTranslatableFields(schemaName) {
|
|
206
224
|
const schema = this.getSchema(schemaName);
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Generates base + editable service classes for schemas with `options.api
|
|
2
|
+
* Generates base + editable service classes for schemas with `options.api`
|
|
3
|
+
* or `options.service`.
|
|
4
|
+
*
|
|
5
|
+
* Issue #57: Full BaseService codegen with translatable, softDelete, audit,
|
|
6
|
+
* DB::transaction, eagerLoad/eagerCount, lookupFields, defaultSort.
|
|
3
7
|
*/
|
|
4
8
|
import { SchemaReader } from './schema-reader.js';
|
|
5
9
|
import type { GeneratedFile, PhpConfig } from './types.js';
|
|
6
|
-
/** Generate service classes for all schemas with api config. */
|
|
10
|
+
/** Generate service classes for all schemas with api or service config. */
|
|
7
11
|
export declare function generateServices(reader: SchemaReader, config: PhpConfig): GeneratedFile[];
|