@famgia/omnify-laravel 0.0.79 → 0.0.80
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/dist/{chunk-6QYGCK3D.js → chunk-FR6LGETT.js} +410 -11
- package/dist/chunk-FR6LGETT.js.map +1 -0
- package/dist/index.cjs +409 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1 -1
- package/dist/plugin.cjs +409 -10
- package/dist/plugin.cjs.map +1 -1
- package/dist/plugin.d.cts +25 -0
- package/dist/plugin.d.ts +25 -0
- package/dist/plugin.js +1 -1
- package/package.json +4 -4
- package/dist/chunk-6QYGCK3D.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -2699,6 +2699,33 @@ function generateStoreRules(propName, propDef, schema, schemas, options) {
|
|
|
2699
2699
|
}
|
|
2700
2700
|
}
|
|
2701
2701
|
break;
|
|
2702
|
+
default:
|
|
2703
|
+
const customType = options.customTypes.get(propDef.type);
|
|
2704
|
+
if (customType && !customType.compound) {
|
|
2705
|
+
if (propDef.type === "JapanesePhone") {
|
|
2706
|
+
rules.push("'string'");
|
|
2707
|
+
rules.push("'max:15'");
|
|
2708
|
+
rules.push("'regex:/^\\d{2,4}-\\d{2,4}-\\d{4}$/'");
|
|
2709
|
+
} else if (propDef.type === "JapanesePostalCode") {
|
|
2710
|
+
rules.push("'string'");
|
|
2711
|
+
rules.push("'max:8'");
|
|
2712
|
+
rules.push("'regex:/^\\d{3}-\\d{4}$/'");
|
|
2713
|
+
} else {
|
|
2714
|
+
const sqlType = customType.sql?.sqlType ?? "VARCHAR";
|
|
2715
|
+
const sqlLength = customType.sql?.length ?? 255;
|
|
2716
|
+
if (sqlType === "VARCHAR" || sqlType === "TEXT") {
|
|
2717
|
+
rules.push("'string'");
|
|
2718
|
+
if (sqlType === "VARCHAR") {
|
|
2719
|
+
rules.push(`'max:${sqlLength}'`);
|
|
2720
|
+
}
|
|
2721
|
+
} else if (sqlType === "INT" || sqlType === "TINYINT" || sqlType === "BIGINT") {
|
|
2722
|
+
rules.push("'integer'");
|
|
2723
|
+
} else if (sqlType === "DECIMAL" || sqlType === "FLOAT") {
|
|
2724
|
+
rules.push("'numeric'");
|
|
2725
|
+
}
|
|
2726
|
+
}
|
|
2727
|
+
}
|
|
2728
|
+
break;
|
|
2702
2729
|
}
|
|
2703
2730
|
if (prop.unique === true) {
|
|
2704
2731
|
rules.push(`'unique:${tableName}'`);
|
|
@@ -2781,12 +2808,100 @@ function generateUpdateRules(propName, propDef, schema, schemas, options) {
|
|
|
2781
2808
|
}
|
|
2782
2809
|
}
|
|
2783
2810
|
break;
|
|
2811
|
+
default:
|
|
2812
|
+
const customType = options.customTypes.get(propDef.type);
|
|
2813
|
+
if (customType && !customType.compound) {
|
|
2814
|
+
if (propDef.type === "JapanesePhone") {
|
|
2815
|
+
rules.push("'string'");
|
|
2816
|
+
rules.push("'max:15'");
|
|
2817
|
+
rules.push("'regex:/^\\d{2,4}-\\d{2,4}-\\d{4}$/'");
|
|
2818
|
+
} else if (propDef.type === "JapanesePostalCode") {
|
|
2819
|
+
rules.push("'string'");
|
|
2820
|
+
rules.push("'max:8'");
|
|
2821
|
+
rules.push("'regex:/^\\d{3}-\\d{4}$/'");
|
|
2822
|
+
} else {
|
|
2823
|
+
const sqlType = customType.sql?.sqlType ?? "VARCHAR";
|
|
2824
|
+
const sqlLength = customType.sql?.length ?? 255;
|
|
2825
|
+
if (sqlType === "VARCHAR" || sqlType === "TEXT") {
|
|
2826
|
+
rules.push("'string'");
|
|
2827
|
+
if (sqlType === "VARCHAR") {
|
|
2828
|
+
rules.push(`'max:${sqlLength}'`);
|
|
2829
|
+
}
|
|
2830
|
+
} else if (sqlType === "INT" || sqlType === "TINYINT" || sqlType === "BIGINT") {
|
|
2831
|
+
rules.push("'integer'");
|
|
2832
|
+
} else if (sqlType === "DECIMAL" || sqlType === "FLOAT") {
|
|
2833
|
+
rules.push("'numeric'");
|
|
2834
|
+
}
|
|
2835
|
+
}
|
|
2836
|
+
}
|
|
2837
|
+
break;
|
|
2784
2838
|
}
|
|
2785
2839
|
if (prop.unique === true) {
|
|
2786
2840
|
rules.push(`Rule::unique('${tableName}')->ignore($this->route('${modelVar}'))`);
|
|
2787
2841
|
}
|
|
2788
2842
|
return rules;
|
|
2789
2843
|
}
|
|
2844
|
+
function getCompoundFieldRules(typeName, suffix, field, fieldOverride) {
|
|
2845
|
+
const rules = [];
|
|
2846
|
+
const sql = field.sql;
|
|
2847
|
+
const length = fieldOverride?.length ?? sql?.length ?? 255;
|
|
2848
|
+
switch (typeName) {
|
|
2849
|
+
case "JapaneseName":
|
|
2850
|
+
rules.push("'string'");
|
|
2851
|
+
rules.push(`'max:${length}'`);
|
|
2852
|
+
if (suffix === "KanaLastname" || suffix === "KanaFirstname") {
|
|
2853
|
+
rules.push("'regex:/^[\\x{30A0}-\\x{30FF}\\x{3000}-\\x{303F}\\x{FF00}-\\x{FF9F}\\s]+$/u'");
|
|
2854
|
+
}
|
|
2855
|
+
break;
|
|
2856
|
+
case "JapaneseAddress":
|
|
2857
|
+
if (suffix === "PostalCode") {
|
|
2858
|
+
rules.push("'string'");
|
|
2859
|
+
rules.push("'max:8'");
|
|
2860
|
+
rules.push("'regex:/^\\d{3}-\\d{4}$/'");
|
|
2861
|
+
} else if (suffix === "PrefectureId") {
|
|
2862
|
+
rules.push("'integer'");
|
|
2863
|
+
rules.push("'between:1,47'");
|
|
2864
|
+
} else {
|
|
2865
|
+
rules.push("'string'");
|
|
2866
|
+
rules.push(`'max:${length}'`);
|
|
2867
|
+
}
|
|
2868
|
+
break;
|
|
2869
|
+
case "JapaneseBankAccount":
|
|
2870
|
+
if (suffix === "BankCode") {
|
|
2871
|
+
rules.push("'string'");
|
|
2872
|
+
rules.push("'size:4'");
|
|
2873
|
+
rules.push("'regex:/^\\d{4}$/'");
|
|
2874
|
+
} else if (suffix === "BranchCode") {
|
|
2875
|
+
rules.push("'string'");
|
|
2876
|
+
rules.push("'size:3'");
|
|
2877
|
+
rules.push("'regex:/^\\d{3}$/'");
|
|
2878
|
+
} else if (suffix === "AccountType") {
|
|
2879
|
+
rules.push("'string'");
|
|
2880
|
+
rules.push("Rule::in(['1', '2', '4'])");
|
|
2881
|
+
} else if (suffix === "AccountNumber") {
|
|
2882
|
+
rules.push("'string'");
|
|
2883
|
+
rules.push("'max:7'");
|
|
2884
|
+
rules.push("'regex:/^\\d{1,7}$/'");
|
|
2885
|
+
} else if (suffix === "AccountHolder") {
|
|
2886
|
+
rules.push("'string'");
|
|
2887
|
+
rules.push(`'max:${length}'`);
|
|
2888
|
+
rules.push("'regex:/^[\\x{30A0}-\\x{30FF}\\x{3000}-\\x{303F}\\x{FF00}-\\x{FF9F}\\s]+$/u'");
|
|
2889
|
+
}
|
|
2890
|
+
break;
|
|
2891
|
+
default:
|
|
2892
|
+
if (sql?.sqlType === "TINYINT" || sql?.sqlType === "INT" || sql?.sqlType === "BIGINT") {
|
|
2893
|
+
rules.push("'integer'");
|
|
2894
|
+
if (sql?.unsigned) {
|
|
2895
|
+
rules.push("'min:0'");
|
|
2896
|
+
}
|
|
2897
|
+
} else {
|
|
2898
|
+
rules.push("'string'");
|
|
2899
|
+
rules.push(`'max:${length}'`);
|
|
2900
|
+
}
|
|
2901
|
+
break;
|
|
2902
|
+
}
|
|
2903
|
+
return rules;
|
|
2904
|
+
}
|
|
2790
2905
|
function expandCompoundTypeFields(propName, propDef, options) {
|
|
2791
2906
|
const typeDef = options.customTypes.get(propDef.type);
|
|
2792
2907
|
if (!typeDef || !typeDef.compound || !typeDef.expand) {
|
|
@@ -2800,18 +2915,18 @@ function expandCompoundTypeFields(propName, propDef, options) {
|
|
|
2800
2915
|
const suffixSnake = toSnakeCase(field.suffix);
|
|
2801
2916
|
const fieldName = `${snakeName}_${suffixSnake}`;
|
|
2802
2917
|
const fieldOverride = prop.fields?.[field.suffix];
|
|
2803
|
-
const
|
|
2918
|
+
const fieldDefNullable = field.sql?.nullable ?? false;
|
|
2919
|
+
const fieldNullable = fieldOverride?.nullable ?? fieldDefNullable ?? isNullable2;
|
|
2804
2920
|
const rules = [];
|
|
2805
2921
|
if (!fieldNullable) {
|
|
2806
2922
|
rules.push("'required'");
|
|
2807
2923
|
} else {
|
|
2808
2924
|
rules.push("'nullable'");
|
|
2809
2925
|
}
|
|
2810
|
-
|
|
2811
|
-
|
|
2812
|
-
const
|
|
2813
|
-
|
|
2814
|
-
fields.push({ fieldName, rules });
|
|
2926
|
+
const typeRules = getCompoundFieldRules(propDef.type, field.suffix, field, fieldOverride);
|
|
2927
|
+
rules.push(...typeRules);
|
|
2928
|
+
const needsRuleImport = rules.some((r) => r.includes("Rule::"));
|
|
2929
|
+
fields.push({ fieldName, rules, needsRuleImport });
|
|
2815
2930
|
}
|
|
2816
2931
|
return fields;
|
|
2817
2932
|
}
|
|
@@ -2845,6 +2960,7 @@ function generateStoreRequestBase(schema, schemas, options) {
|
|
|
2845
2960
|
const expandedFields = expandCompoundTypeFields(propName, propDef, options);
|
|
2846
2961
|
if (expandedFields.length > 0) {
|
|
2847
2962
|
for (const field of expandedFields) {
|
|
2963
|
+
if (field.needsRuleImport) needsRuleImport = true;
|
|
2848
2964
|
rulesLines.push(` '${field.fieldName}' => [${field.rules.join(", ")}],`);
|
|
2849
2965
|
fieldList.push(field.fieldName);
|
|
2850
2966
|
attributeLines.push(` '${field.fieldName}' => '${escapePhpString2(field.fieldName)}',`);
|
|
@@ -2941,6 +3057,7 @@ function generateUpdateRequestBase(schema, schemas, options) {
|
|
|
2941
3057
|
const expandedFields = expandCompoundTypeFields(propName, propDef, options);
|
|
2942
3058
|
if (expandedFields.length > 0) {
|
|
2943
3059
|
for (const field of expandedFields) {
|
|
3060
|
+
if (field.needsRuleImport) needsRuleImport = true;
|
|
2944
3061
|
const updateRules = field.rules.map((r) => r === "'required'" ? "'sometimes'" : r);
|
|
2945
3062
|
rulesLines.push(` '${field.fieldName}' => [${updateRules.join(", ")}],`);
|
|
2946
3063
|
attributeLines.push(` '${field.fieldName}' => '${escapePhpString2(field.fieldName)}',`);
|
|
@@ -2948,7 +3065,7 @@ function generateUpdateRequestBase(schema, schemas, options) {
|
|
|
2948
3065
|
continue;
|
|
2949
3066
|
}
|
|
2950
3067
|
const rules = generateUpdateRules(propName, propDef, schema, schemas, options);
|
|
2951
|
-
if (rules.some((r) => r.includes("Rule::")
|
|
3068
|
+
if (rules.some((r) => r.includes("Rule::"))) needsRuleImport = true;
|
|
2952
3069
|
rulesLines.push(` '${snakeName}' => [${rules.join(", ")}],`);
|
|
2953
3070
|
const displayName = getDisplayName(propDef.displayName, options.locale, propName);
|
|
2954
3071
|
attributeLines.push(` '${snakeName}' => '${escapePhpString2(displayName)}',`);
|
|
@@ -3174,6 +3291,230 @@ function getRequestPath(request) {
|
|
|
3174
3291
|
return request.path;
|
|
3175
3292
|
}
|
|
3176
3293
|
|
|
3294
|
+
// src/resource/generator.ts
|
|
3295
|
+
var import_omnify_types4 = require("@famgia/omnify-types");
|
|
3296
|
+
var DEFAULT_OPTIONS3 = {
|
|
3297
|
+
baseResourceNamespace: "App\\Http\\Resources\\OmnifyBase",
|
|
3298
|
+
resourceNamespace: "App\\Http\\Resources",
|
|
3299
|
+
baseResourcePath: "app/Http/Resources/OmnifyBase",
|
|
3300
|
+
resourcePath: "app/Http/Resources",
|
|
3301
|
+
customTypes: /* @__PURE__ */ new Map(),
|
|
3302
|
+
locale: "en"
|
|
3303
|
+
};
|
|
3304
|
+
var SKIP_FIELDS2 = /* @__PURE__ */ new Set([
|
|
3305
|
+
"password",
|
|
3306
|
+
"remember_token"
|
|
3307
|
+
]);
|
|
3308
|
+
function resolveOptions4(options) {
|
|
3309
|
+
return {
|
|
3310
|
+
baseResourceNamespace: options?.baseResourceNamespace ?? DEFAULT_OPTIONS3.baseResourceNamespace,
|
|
3311
|
+
resourceNamespace: options?.resourceNamespace ?? DEFAULT_OPTIONS3.resourceNamespace,
|
|
3312
|
+
baseResourcePath: options?.baseResourcePath ?? DEFAULT_OPTIONS3.baseResourcePath,
|
|
3313
|
+
resourcePath: options?.resourcePath ?? DEFAULT_OPTIONS3.resourcePath,
|
|
3314
|
+
customTypes: options?.customTypes ?? /* @__PURE__ */ new Map(),
|
|
3315
|
+
locale: options?.locale ?? DEFAULT_OPTIONS3.locale
|
|
3316
|
+
};
|
|
3317
|
+
}
|
|
3318
|
+
function getModuleName2(schema) {
|
|
3319
|
+
if (schema.module) {
|
|
3320
|
+
return schema.module;
|
|
3321
|
+
}
|
|
3322
|
+
return "";
|
|
3323
|
+
}
|
|
3324
|
+
function getPropertyOutput(propName, propDef, schemas, options) {
|
|
3325
|
+
const snakeName = toSnakeCase(propName);
|
|
3326
|
+
const lines = [];
|
|
3327
|
+
if (SKIP_FIELDS2.has(snakeName)) {
|
|
3328
|
+
return lines;
|
|
3329
|
+
}
|
|
3330
|
+
if (propDef.type === "Association") {
|
|
3331
|
+
const assoc = propDef;
|
|
3332
|
+
const targetClass = assoc.target ? toPascalCase(assoc.target) : "";
|
|
3333
|
+
switch (assoc.relation) {
|
|
3334
|
+
case "ManyToOne":
|
|
3335
|
+
case "OneToOne":
|
|
3336
|
+
lines.push(` '${snakeName}_id' => $this->${snakeName}_id,`);
|
|
3337
|
+
lines.push(` '${snakeName}' => $this->whenLoaded('${toCamelCase(propName)}', fn() => new ${targetClass}Resource($this->${toCamelCase(propName)})),`);
|
|
3338
|
+
break;
|
|
3339
|
+
case "OneToMany":
|
|
3340
|
+
case "ManyToMany":
|
|
3341
|
+
lines.push(` '${snakeName}' => $this->whenLoaded('${toCamelCase(propName)}', fn() => ${targetClass}Resource::collection($this->${toCamelCase(propName)})),`);
|
|
3342
|
+
break;
|
|
3343
|
+
case "MorphTo":
|
|
3344
|
+
lines.push(` '${snakeName}_type' => $this->${snakeName}_type,`);
|
|
3345
|
+
lines.push(` '${snakeName}_id' => $this->${snakeName}_id,`);
|
|
3346
|
+
lines.push(` '${snakeName}' => $this->whenLoaded('${toCamelCase(propName)}'),`);
|
|
3347
|
+
break;
|
|
3348
|
+
case "MorphOne":
|
|
3349
|
+
case "MorphMany":
|
|
3350
|
+
lines.push(` '${snakeName}' => $this->whenLoaded('${toCamelCase(propName)}', fn() => ${targetClass}Resource::collection($this->${toCamelCase(propName)})),`);
|
|
3351
|
+
break;
|
|
3352
|
+
}
|
|
3353
|
+
return lines;
|
|
3354
|
+
}
|
|
3355
|
+
const typeDef = options.customTypes.get(propDef.type);
|
|
3356
|
+
if (typeDef?.compound && typeDef.expand) {
|
|
3357
|
+
for (const field of typeDef.expand) {
|
|
3358
|
+
const suffixSnake = toSnakeCase(field.suffix);
|
|
3359
|
+
const fieldName = `${snakeName}_${suffixSnake}`;
|
|
3360
|
+
lines.push(` '${fieldName}' => $this->${fieldName},`);
|
|
3361
|
+
}
|
|
3362
|
+
if (typeDef.accessors) {
|
|
3363
|
+
for (const accessor of typeDef.accessors) {
|
|
3364
|
+
const accessorName = `${snakeName}_${toSnakeCase(accessor.name)}`;
|
|
3365
|
+
lines.push(` '${accessorName}' => $this->${accessorName},`);
|
|
3366
|
+
}
|
|
3367
|
+
}
|
|
3368
|
+
return lines;
|
|
3369
|
+
}
|
|
3370
|
+
lines.push(` '${snakeName}' => $this->${snakeName},`);
|
|
3371
|
+
return lines;
|
|
3372
|
+
}
|
|
3373
|
+
function generateResourceBase(schema, schemas, options) {
|
|
3374
|
+
const className = toPascalCase(schema.name);
|
|
3375
|
+
const module2 = getModuleName2(schema);
|
|
3376
|
+
const namespaceModule = module2 ? `\\${module2}` : "";
|
|
3377
|
+
const namespace = `${options.baseResourceNamespace}${namespaceModule}`;
|
|
3378
|
+
const properties = schema.properties ?? {};
|
|
3379
|
+
const outputLines = [];
|
|
3380
|
+
const imports = /* @__PURE__ */ new Set();
|
|
3381
|
+
if (schema.options?.id !== false) {
|
|
3382
|
+
outputLines.push(` 'id' => $this->id,`);
|
|
3383
|
+
}
|
|
3384
|
+
for (const [propName, propDef] of Object.entries(properties)) {
|
|
3385
|
+
const lines = getPropertyOutput(propName, propDef, schemas, options);
|
|
3386
|
+
outputLines.push(...lines);
|
|
3387
|
+
if (propDef.type === "Association") {
|
|
3388
|
+
const assoc = propDef;
|
|
3389
|
+
if (assoc.target) {
|
|
3390
|
+
const targetModule = getModuleName2(schemas[assoc.target] ?? schema);
|
|
3391
|
+
const targetModuleNs = targetModule ? `\\${targetModule}` : "";
|
|
3392
|
+
imports.add(`use ${options.resourceNamespace}${targetModuleNs}\\${toPascalCase(assoc.target)}Resource;`);
|
|
3393
|
+
}
|
|
3394
|
+
}
|
|
3395
|
+
}
|
|
3396
|
+
if (schema.options?.timestamps !== false) {
|
|
3397
|
+
outputLines.push(` 'created_at' => $this->created_at?->toISOString(),`);
|
|
3398
|
+
outputLines.push(` 'updated_at' => $this->updated_at?->toISOString(),`);
|
|
3399
|
+
}
|
|
3400
|
+
if (schema.options?.softDelete) {
|
|
3401
|
+
outputLines.push(` 'deleted_at' => $this->deleted_at?->toISOString(),`);
|
|
3402
|
+
}
|
|
3403
|
+
const importLines = Array.from(imports).sort().join("\n");
|
|
3404
|
+
const importBlock = importLines ? `
|
|
3405
|
+
${importLines}` : "";
|
|
3406
|
+
const content = `<?php
|
|
3407
|
+
|
|
3408
|
+
/**
|
|
3409
|
+
* AUTO-GENERATED BY OMNIFY - DO NOT EDIT!
|
|
3410
|
+
*
|
|
3411
|
+
* This file is generated from Omnify schema: ${schema.name}
|
|
3412
|
+
* Re-run \`npx omnify generate\` to update.
|
|
3413
|
+
*
|
|
3414
|
+
* @generated
|
|
3415
|
+
*/
|
|
3416
|
+
|
|
3417
|
+
namespace ${namespace};
|
|
3418
|
+
|
|
3419
|
+
use Illuminate\\Http\\Request;
|
|
3420
|
+
use Illuminate\\Http\\Resources\\Json\\JsonResource;${importBlock}
|
|
3421
|
+
|
|
3422
|
+
class ${className}ResourceBase extends JsonResource
|
|
3423
|
+
{
|
|
3424
|
+
/**
|
|
3425
|
+
* Transform the resource into an array.
|
|
3426
|
+
*
|
|
3427
|
+
* @return array<string, mixed>
|
|
3428
|
+
*/
|
|
3429
|
+
protected function schemaArray(Request $request): array
|
|
3430
|
+
{
|
|
3431
|
+
return [
|
|
3432
|
+
${outputLines.join("\n")}
|
|
3433
|
+
];
|
|
3434
|
+
}
|
|
3435
|
+
}
|
|
3436
|
+
`;
|
|
3437
|
+
const modulePath = module2 ? `/${module2}` : "";
|
|
3438
|
+
return {
|
|
3439
|
+
path: `${options.baseResourcePath}${modulePath}/${className}ResourceBase.php`,
|
|
3440
|
+
content,
|
|
3441
|
+
type: "base",
|
|
3442
|
+
overwrite: true,
|
|
3443
|
+
schemaName: schema.name,
|
|
3444
|
+
module: module2
|
|
3445
|
+
};
|
|
3446
|
+
}
|
|
3447
|
+
function generateResource(schema, options) {
|
|
3448
|
+
const className = toPascalCase(schema.name);
|
|
3449
|
+
const module2 = getModuleName2(schema);
|
|
3450
|
+
const namespaceModule = module2 ? `\\${module2}` : "";
|
|
3451
|
+
const namespace = `${options.resourceNamespace}${namespaceModule}`;
|
|
3452
|
+
const baseNamespace = `${options.baseResourceNamespace}${namespaceModule}`;
|
|
3453
|
+
const content = `<?php
|
|
3454
|
+
|
|
3455
|
+
/**
|
|
3456
|
+
* ${className} Resource
|
|
3457
|
+
*
|
|
3458
|
+
* SAFE TO EDIT - This file is never overwritten by Omnify.
|
|
3459
|
+
*/
|
|
3460
|
+
|
|
3461
|
+
namespace ${namespace};
|
|
3462
|
+
|
|
3463
|
+
use Illuminate\\Http\\Request;
|
|
3464
|
+
use ${baseNamespace}\\${className}ResourceBase;
|
|
3465
|
+
|
|
3466
|
+
class ${className}Resource extends ${className}ResourceBase
|
|
3467
|
+
{
|
|
3468
|
+
/**
|
|
3469
|
+
* Transform the resource into an array.
|
|
3470
|
+
*
|
|
3471
|
+
* @return array<string, mixed>
|
|
3472
|
+
*/
|
|
3473
|
+
public function toArray(Request $request): array
|
|
3474
|
+
{
|
|
3475
|
+
return array_merge($this->schemaArray($request), [
|
|
3476
|
+
// Custom fields here
|
|
3477
|
+
]);
|
|
3478
|
+
}
|
|
3479
|
+
|
|
3480
|
+
/**
|
|
3481
|
+
* Get additional data that should be returned with the resource array.
|
|
3482
|
+
*
|
|
3483
|
+
* @return array<string, mixed>
|
|
3484
|
+
*/
|
|
3485
|
+
public function with(Request $request): array
|
|
3486
|
+
{
|
|
3487
|
+
return [
|
|
3488
|
+
// Additional metadata here
|
|
3489
|
+
];
|
|
3490
|
+
}
|
|
3491
|
+
}
|
|
3492
|
+
`;
|
|
3493
|
+
const modulePath = module2 ? `/${module2}` : "";
|
|
3494
|
+
return {
|
|
3495
|
+
path: `${options.resourcePath}${modulePath}/${className}Resource.php`,
|
|
3496
|
+
content,
|
|
3497
|
+
type: "user",
|
|
3498
|
+
overwrite: false,
|
|
3499
|
+
schemaName: schema.name,
|
|
3500
|
+
module: module2
|
|
3501
|
+
};
|
|
3502
|
+
}
|
|
3503
|
+
function generateResources(schemas, options) {
|
|
3504
|
+
const resolved = resolveOptions4(options);
|
|
3505
|
+
const resources = [];
|
|
3506
|
+
for (const schema of Object.values(schemas)) {
|
|
3507
|
+
if (schema.kind === "enum") continue;
|
|
3508
|
+
if (schema.options?.hidden === true) continue;
|
|
3509
|
+
resources.push(generateResourceBase(schema, schemas, resolved));
|
|
3510
|
+
resources.push(generateResource(schema, resolved));
|
|
3511
|
+
}
|
|
3512
|
+
return resources;
|
|
3513
|
+
}
|
|
3514
|
+
function getResourcePath(resource) {
|
|
3515
|
+
return resource.path;
|
|
3516
|
+
}
|
|
3517
|
+
|
|
3177
3518
|
// src/plugin.ts
|
|
3178
3519
|
function getExistingMigrationTables(migrationsDir) {
|
|
3179
3520
|
const existingTables = /* @__PURE__ */ new Set();
|
|
@@ -3282,10 +3623,34 @@ var LARAVEL_CONFIG_SCHEMA = {
|
|
|
3282
3623
|
description: "Generate Laravel FormRequest classes for validation",
|
|
3283
3624
|
default: false,
|
|
3284
3625
|
group: "options"
|
|
3626
|
+
},
|
|
3627
|
+
{
|
|
3628
|
+
key: "resourcesPath",
|
|
3629
|
+
type: "path",
|
|
3630
|
+
label: "Resources Path",
|
|
3631
|
+
description: "Directory for user-editable API Resource files",
|
|
3632
|
+
default: "app/Http/Resources",
|
|
3633
|
+
group: "output"
|
|
3634
|
+
},
|
|
3635
|
+
{
|
|
3636
|
+
key: "baseResourcesPath",
|
|
3637
|
+
type: "path",
|
|
3638
|
+
label: "Base Resources Path",
|
|
3639
|
+
description: "Directory for auto-generated base API Resource files",
|
|
3640
|
+
default: "app/Http/Resources/OmnifyBase",
|
|
3641
|
+
group: "output"
|
|
3642
|
+
},
|
|
3643
|
+
{
|
|
3644
|
+
key: "generateResources",
|
|
3645
|
+
type: "boolean",
|
|
3646
|
+
label: "Generate Resources",
|
|
3647
|
+
description: "Generate Laravel API Resource classes",
|
|
3648
|
+
default: false,
|
|
3649
|
+
group: "options"
|
|
3285
3650
|
}
|
|
3286
3651
|
]
|
|
3287
3652
|
};
|
|
3288
|
-
function
|
|
3653
|
+
function resolveOptions5(options) {
|
|
3289
3654
|
return {
|
|
3290
3655
|
migrationsPath: options?.migrationsPath ?? "database/migrations/omnify",
|
|
3291
3656
|
modelsPath: options?.modelsPath ?? "app/Models",
|
|
@@ -3303,11 +3668,16 @@ function resolveOptions4(options) {
|
|
|
3303
3668
|
baseRequestsPath: options?.baseRequestsPath ?? "app/Http/Requests/OmnifyBase",
|
|
3304
3669
|
requestNamespace: options?.requestNamespace ?? "App\\Http\\Requests",
|
|
3305
3670
|
baseRequestNamespace: options?.baseRequestNamespace ?? "App\\Http\\Requests\\OmnifyBase",
|
|
3306
|
-
generateRequests: options?.generateRequests ?? false
|
|
3671
|
+
generateRequests: options?.generateRequests ?? false,
|
|
3672
|
+
resourcesPath: options?.resourcesPath ?? "app/Http/Resources",
|
|
3673
|
+
baseResourcesPath: options?.baseResourcesPath ?? "app/Http/Resources/OmnifyBase",
|
|
3674
|
+
resourceNamespace: options?.resourceNamespace ?? "App\\Http\\Resources",
|
|
3675
|
+
baseResourceNamespace: options?.baseResourceNamespace ?? "App\\Http\\Resources\\OmnifyBase",
|
|
3676
|
+
generateResources: options?.generateResources ?? false
|
|
3307
3677
|
};
|
|
3308
3678
|
}
|
|
3309
3679
|
function laravelPlugin(options) {
|
|
3310
|
-
const resolved =
|
|
3680
|
+
const resolved = resolveOptions5(options);
|
|
3311
3681
|
const migrationGenerator = {
|
|
3312
3682
|
name: "laravel-migrations",
|
|
3313
3683
|
description: "Generate Laravel migration files",
|
|
@@ -3506,6 +3876,32 @@ function laravelPlugin(options) {
|
|
|
3506
3876
|
}));
|
|
3507
3877
|
}
|
|
3508
3878
|
};
|
|
3879
|
+
const resourceGenerator = {
|
|
3880
|
+
name: "laravel-resources",
|
|
3881
|
+
description: "Generate Laravel API Resource classes",
|
|
3882
|
+
generate: async (ctx) => {
|
|
3883
|
+
const resourceOptions = {
|
|
3884
|
+
resourceNamespace: resolved.resourceNamespace,
|
|
3885
|
+
baseResourceNamespace: resolved.baseResourceNamespace,
|
|
3886
|
+
resourcePath: resolved.resourcesPath,
|
|
3887
|
+
baseResourcePath: resolved.baseResourcesPath,
|
|
3888
|
+
customTypes: ctx.customTypes
|
|
3889
|
+
};
|
|
3890
|
+
const resources = generateResources(ctx.schemas, resourceOptions);
|
|
3891
|
+
return resources.map((resource) => ({
|
|
3892
|
+
path: getResourcePath(resource),
|
|
3893
|
+
content: resource.content,
|
|
3894
|
+
type: "other",
|
|
3895
|
+
// Skip writing user resources if they already exist
|
|
3896
|
+
skipIfExists: !resource.overwrite,
|
|
3897
|
+
metadata: {
|
|
3898
|
+
resourceType: resource.type,
|
|
3899
|
+
schemaName: resource.schemaName,
|
|
3900
|
+
module: resource.module
|
|
3901
|
+
}
|
|
3902
|
+
}));
|
|
3903
|
+
}
|
|
3904
|
+
};
|
|
3509
3905
|
const generators = [migrationGenerator];
|
|
3510
3906
|
if (resolved.generateModels) {
|
|
3511
3907
|
generators.push(modelGenerator);
|
|
@@ -3516,6 +3912,9 @@ function laravelPlugin(options) {
|
|
|
3516
3912
|
if (resolved.generateRequests) {
|
|
3517
3913
|
generators.push(requestGenerator);
|
|
3518
3914
|
}
|
|
3915
|
+
if (resolved.generateResources) {
|
|
3916
|
+
generators.push(resourceGenerator);
|
|
3917
|
+
}
|
|
3519
3918
|
return {
|
|
3520
3919
|
name: "@famgia/omnify-laravel",
|
|
3521
3920
|
version: "0.0.14",
|