@mgamadeus/ddd-web 0.7.0 → 0.9.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/.claude/skills/ddd-web-specialist/SKILL.md +3 -2
- package/.claude/skills/ddd-web-specialist/references/consumer.md +15 -0
- package/CHANGELOG.md +27 -0
- package/README.md +3 -0
- package/dist/api/index.d.ts +23 -6
- package/dist/api/index.js +29 -8
- package/dist/api/index.js.map +1 -1
- package/dist/{chunk-RCXFU5IA.js → chunk-TPFRUK5O.js} +158 -11
- package/dist/chunk-TPFRUK5O.js.map +1 -0
- package/dist/codegen/cli.js +1 -1
- package/dist/codegen/index.d.ts +33 -1
- package/dist/codegen/index.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-RCXFU5IA.js.map +0 -1
|
@@ -31,6 +31,9 @@ function resolveApis(loaded) {
|
|
|
31
31
|
if (!Array.isArray(config.apis) || config.apis.length === 0) {
|
|
32
32
|
throw new Error(`No apis configured in ${loaded.configPath}.`);
|
|
33
33
|
}
|
|
34
|
+
if (config.models?.enums === "const" && config.models.naming !== "document") {
|
|
35
|
+
throw new Error(`models.enums 'const' needs models.naming 'document' (${loaded.configPath}).`);
|
|
36
|
+
}
|
|
34
37
|
return config.apis.map((entry, index) => {
|
|
35
38
|
for (const key of ["schemaFile", "apiFile", "outputFile"]) {
|
|
36
39
|
if (typeof entry[key] !== "string" || entry[key] === "") {
|
|
@@ -541,6 +544,111 @@ function loadTypeScript() {
|
|
|
541
544
|
return cached;
|
|
542
545
|
}
|
|
543
546
|
|
|
547
|
+
// src/codegen/document-models.ts
|
|
548
|
+
var titleOfSchema = (index) => `DddModel${index}`;
|
|
549
|
+
var isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
550
|
+
function prepareDocumentForModels(doc, options) {
|
|
551
|
+
const documentModels = /* @__PURE__ */ new Map();
|
|
552
|
+
const naming = options.naming === "document";
|
|
553
|
+
const enumConsts = options.enums === "const";
|
|
554
|
+
if (!naming && !enumConsts && !options.nullableRefs && (options.idType ?? "document") === "document") {
|
|
555
|
+
return { doc, documentModels };
|
|
556
|
+
}
|
|
557
|
+
const prepared = structuredClone(doc);
|
|
558
|
+
const schemas = prepared.components?.schemas ?? {};
|
|
559
|
+
const original = doc.components?.schemas ?? {};
|
|
560
|
+
Object.keys(schemas).forEach((schemaKey, index) => {
|
|
561
|
+
const schema = schemas[schemaKey];
|
|
562
|
+
if (!isObject(schema)) return;
|
|
563
|
+
if (naming) {
|
|
564
|
+
schema.title = titleOfSchema(index);
|
|
565
|
+
documentModels.set(titleOfSchema(index), { schemaKey, schema: original[schemaKey] });
|
|
566
|
+
}
|
|
567
|
+
for (const [propertyName2, property] of Object.entries(schema.properties ?? {})) {
|
|
568
|
+
if (!isObject(property)) continue;
|
|
569
|
+
if (options.nullableRefs && typeof property.$ref === "string" && property.nullable === true) {
|
|
570
|
+
schema.properties[propertyName2] = { allOf: [{ $ref: property.$ref }], nullable: true };
|
|
571
|
+
continue;
|
|
572
|
+
}
|
|
573
|
+
const idType = options.idType ?? "document";
|
|
574
|
+
if (propertyName2 === "id" && idType !== "document" && isStringOrIntegerUnion(property)) {
|
|
575
|
+
schema.properties[propertyName2] = {
|
|
576
|
+
type: idType === "number" ? "integer" : "string",
|
|
577
|
+
...isNullable(property) ? { nullable: true } : {}
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
});
|
|
582
|
+
return { doc: prepared, documentModels };
|
|
583
|
+
}
|
|
584
|
+
function isStringOrIntegerUnion(property) {
|
|
585
|
+
const types = new Set((property.oneOf ?? []).map((variant) => isObject(variant) ? variant.type : void 0));
|
|
586
|
+
return types.has("string") && (types.has("integer") || types.has("number"));
|
|
587
|
+
}
|
|
588
|
+
function isNullable(property) {
|
|
589
|
+
return property.nullable === true || (property.oneOf ?? []).some((variant) => isObject(variant) && variant.nullable === true);
|
|
590
|
+
}
|
|
591
|
+
function placeDocumentModel(model) {
|
|
592
|
+
const parts = model.schemaKey.split(".");
|
|
593
|
+
return { segments: parts.slice(0, -1), leafName: parts[parts.length - 1] };
|
|
594
|
+
}
|
|
595
|
+
function findDocumentModel(rtkName, documentModels) {
|
|
596
|
+
const match = rtkName.match(/^(DddModel\d+)(Read|Write)?$/);
|
|
597
|
+
const model = match ? documentModels.get(match[1]) : void 0;
|
|
598
|
+
return model ? { model, suffix: match[2] ?? "" } : null;
|
|
599
|
+
}
|
|
600
|
+
function enumKeyOf(value) {
|
|
601
|
+
if (value.trim() === "") return "EMPTY";
|
|
602
|
+
const key = value.replace(/[ /\\]+/g, "_");
|
|
603
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key) ? key : JSON.stringify(key);
|
|
604
|
+
}
|
|
605
|
+
function applyEnumConsts(body, modelName, schema) {
|
|
606
|
+
const consts = /* @__PURE__ */ new Map();
|
|
607
|
+
const typeTexts = /* @__PURE__ */ new Map();
|
|
608
|
+
for (const [propertyName2, property] of Object.entries(schema.properties ?? {})) {
|
|
609
|
+
if (!isObject(property)) continue;
|
|
610
|
+
const isArray = isObject(property.items) && Array.isArray(property.items.enum);
|
|
611
|
+
const values = (isArray ? property.items.enum : property.enum)?.filter((value) => typeof value === "string" || typeof value === "number");
|
|
612
|
+
if (!values || values.length === 0) continue;
|
|
613
|
+
const constName = `${modelName}${propertyName2.charAt(0).toUpperCase()}${propertyName2.slice(1)}`;
|
|
614
|
+
if (!/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(constName)) continue;
|
|
615
|
+
const members = [...new Set(values.map(String))].map((value) => ` ${enumKeyOf(value)}: ${JSON.stringify(value)},`).join("\n");
|
|
616
|
+
consts.set(constName, `export const ${constName} = {
|
|
617
|
+
${members}
|
|
618
|
+
};
|
|
619
|
+
|
|
620
|
+
export const ${constName}Strict = {
|
|
621
|
+
${members}
|
|
622
|
+
} as const;`);
|
|
623
|
+
const memberType = `(typeof ${constName}Strict)[keyof typeof ${constName}Strict]`;
|
|
624
|
+
const nullable = property.nullable === true || (property.enum ?? []).includes(null);
|
|
625
|
+
typeTexts.set(propertyName2, `${isArray ? `Array<${memberType}>` : memberType}${nullable ? " | null" : ""}`);
|
|
626
|
+
}
|
|
627
|
+
return { body: replacePropertyTypes(body, typeTexts), consts };
|
|
628
|
+
}
|
|
629
|
+
function replacePropertyTypes(body, typeTexts) {
|
|
630
|
+
if (typeTexts.size === 0) return body;
|
|
631
|
+
const ts = loadTypeScript();
|
|
632
|
+
const sourceFile = ts.createSourceFile("model.ts", body, ts.ScriptTarget.Latest, true);
|
|
633
|
+
const declaration = sourceFile.statements.find(
|
|
634
|
+
(statement) => ts.isTypeAliasDeclaration(statement) || ts.isInterfaceDeclaration(statement)
|
|
635
|
+
);
|
|
636
|
+
if (!declaration) return body;
|
|
637
|
+
const members = ts.isInterfaceDeclaration(declaration) ? declaration.members : ts.isTypeLiteralNode(declaration.type) ? declaration.type.members : ts.isIntersectionTypeNode(declaration.type) ? declaration.type.types.flatMap((part) => ts.isTypeLiteralNode(part) ? [...part.members] : []) : [];
|
|
638
|
+
const replacements = [];
|
|
639
|
+
for (const member of members) {
|
|
640
|
+
if (!ts.isPropertySignature(member) || !member.type || !member.name) continue;
|
|
641
|
+
const name = ts.isIdentifier(member.name) || ts.isStringLiteral(member.name) ? member.name.text : member.name.getText(sourceFile);
|
|
642
|
+
const text = typeTexts.get(name);
|
|
643
|
+
if (text !== void 0) replacements.push({ start: member.type.getStart(sourceFile), end: member.type.getEnd(), text });
|
|
644
|
+
}
|
|
645
|
+
let result = body;
|
|
646
|
+
for (const { start, end, text } of replacements.sort((a, b) => b.start - a.start)) {
|
|
647
|
+
result = result.slice(0, start) + text + result.slice(end);
|
|
648
|
+
}
|
|
649
|
+
return result;
|
|
650
|
+
}
|
|
651
|
+
|
|
544
652
|
// src/codegen/endpoints.ts
|
|
545
653
|
function toOpenApiPath(url) {
|
|
546
654
|
return url.replace(/\$\{encodeURIComponent\(String\(queryArg\.([A-Za-z0-9_]+)\)\)\}/g, "{$1}").replace(/\$\{queryArg\.([A-Za-z0-9_]+)\}/g, "{$1}");
|
|
@@ -1130,7 +1238,15 @@ function extractModels(input) {
|
|
|
1130
1238
|
const name = files.get(filePath)?.match(EXPORTED_NAME)?.[1];
|
|
1131
1239
|
if (name && !nameOwners.has(name)) nameOwners.set(name, path4.resolve(filePath).toLowerCase());
|
|
1132
1240
|
}
|
|
1241
|
+
const documentModels = input.documentModels ?? /* @__PURE__ */ new Map();
|
|
1242
|
+
const constsByFile = input.constsByFile ?? /* @__PURE__ */ new Map();
|
|
1133
1243
|
const placement = (oldName) => {
|
|
1244
|
+
const documentModel = findDocumentModel(oldName, documentModels);
|
|
1245
|
+
if (documentModel) {
|
|
1246
|
+
const { segments, leafName } = placeDocumentModel(documentModel.model);
|
|
1247
|
+
const planned = path4.join(modelsDir, ...segments, `${leafName}${documentModel.suffix}.ts`);
|
|
1248
|
+
return { filePath: files.findPath(planned) ?? planned, leafName: `${leafName}${documentModel.suffix}`, segments };
|
|
1249
|
+
}
|
|
1134
1250
|
const match = registryMatch(oldName, registry);
|
|
1135
1251
|
if (match) {
|
|
1136
1252
|
const planned = path4.join(modelsDir, ...match.segments, `${match.leafName}.ts`);
|
|
@@ -1141,6 +1257,7 @@ function extractModels(input) {
|
|
|
1141
1257
|
};
|
|
1142
1258
|
const originalNames = exportedTypeDeclarations(input.apiSource).map((declaration) => declaration.name);
|
|
1143
1259
|
const renameMap = /* @__PURE__ */ new Map();
|
|
1260
|
+
const documentModelByNewName = /* @__PURE__ */ new Map();
|
|
1144
1261
|
const targetByNewName = /* @__PURE__ */ new Map();
|
|
1145
1262
|
const nameByPath = /* @__PURE__ */ new Map();
|
|
1146
1263
|
for (const oldName of originalNames) {
|
|
@@ -1160,6 +1277,8 @@ function extractModels(input) {
|
|
|
1160
1277
|
nameByPath.set(pathKey, name);
|
|
1161
1278
|
renameMap.set(oldName, name);
|
|
1162
1279
|
targetByNewName.set(name, filePath);
|
|
1280
|
+
const documentModel = findDocumentModel(oldName, documentModels);
|
|
1281
|
+
if (documentModel && documentModel.suffix === "") documentModelByNewName.set(name, documentModel.model);
|
|
1163
1282
|
}
|
|
1164
1283
|
const renameRegex = alternation([...renameMap.keys()]);
|
|
1165
1284
|
const renamedSource = renameRegex ? input.apiSource.replace(renameRegex, (_, name) => renameMap.get(name) ?? name) : input.apiSource;
|
|
@@ -1191,6 +1310,15 @@ function extractModels(input) {
|
|
|
1191
1310
|
}
|
|
1192
1311
|
const kind = kindByName.get(name);
|
|
1193
1312
|
let typedBody = body;
|
|
1313
|
+
const documentModel = documentModelByNewName.get(name);
|
|
1314
|
+
if (input.enumConsts && documentModel) {
|
|
1315
|
+
const applied = applyEnumConsts(body, name, documentModel.schema);
|
|
1316
|
+
typedBody = applied.body;
|
|
1317
|
+
const fileKey = path4.resolve(filePath).toLowerCase();
|
|
1318
|
+
const fileConsts2 = constsByFile.get(fileKey) ?? /* @__PURE__ */ new Map();
|
|
1319
|
+
for (const [constName, text] of applied.consts) fileConsts2.set(constName, text);
|
|
1320
|
+
if (fileConsts2.size > 0) constsByFile.set(fileKey, fileConsts2);
|
|
1321
|
+
}
|
|
1194
1322
|
if (kind === "text") {
|
|
1195
1323
|
const replaced = body.replace(/(=\s*\/\*\* status \d+[^*]*\*\/)\s*\{\}/g, "$1 string");
|
|
1196
1324
|
typedBody = replaced !== body ? replaced : `export type ${name} = string;`;
|
|
@@ -1212,7 +1340,8 @@ function extractModels(input) {
|
|
|
1212
1340
|
|
|
1213
1341
|
export default ${name};
|
|
1214
1342
|
`;
|
|
1215
|
-
|
|
1343
|
+
const fileConsts = constsByFile.get(path4.resolve(filePath).toLowerCase());
|
|
1344
|
+
files.set(filePath, fileConsts ? withEnumConsts(content, name, [...fileConsts.values()]) : content);
|
|
1216
1345
|
if (isPresentation(path4.relative(modelsDir, filePath))) continue;
|
|
1217
1346
|
barrelEntries.push(`export * from '${importSpecifier(indexPath, filePath, esm)}';`);
|
|
1218
1347
|
}
|
|
@@ -1245,30 +1374,42 @@ function writeBarrel(files, indexPath, newEntries) {
|
|
|
1245
1374
|
files.set(indexPath, `${merged.join("\n")}
|
|
1246
1375
|
`);
|
|
1247
1376
|
}
|
|
1377
|
+
function withEnumConsts(content, name, constTexts) {
|
|
1378
|
+
const defaultExport = `export default ${name};`;
|
|
1379
|
+
const withoutConsts = content.replace(/\nexport const \w+ = \{[\s\S]*?\n\}(?: as const)?;\n/g, "\n");
|
|
1380
|
+
const at = withoutConsts.lastIndexOf(defaultExport);
|
|
1381
|
+
const head = (at >= 0 ? withoutConsts.slice(0, at) : withoutConsts).replace(/\s+$/, "");
|
|
1382
|
+
return `${head}
|
|
1383
|
+
|
|
1384
|
+
${constTexts.join("\n\n")}
|
|
1385
|
+
|
|
1386
|
+
${defaultExport}
|
|
1387
|
+
`;
|
|
1388
|
+
}
|
|
1248
1389
|
|
|
1249
1390
|
// src/codegen/multipart.ts
|
|
1250
1391
|
var MULTIPART_FILES_ARG = "multipartFiles";
|
|
1251
1392
|
var BODY_FIELD = "body";
|
|
1252
1393
|
var FILE_PART_TYPE = "(Blob | { uri: string; name: string; type: string })";
|
|
1253
|
-
var
|
|
1394
|
+
var isObject2 = (value) => !!value && typeof value === "object" && !Array.isArray(value);
|
|
1254
1395
|
function resolveRef(doc, value) {
|
|
1255
1396
|
let current = value;
|
|
1256
|
-
for (let depth = 0; depth < 5 &&
|
|
1397
|
+
for (let depth = 0; depth < 5 && isObject2(current) && typeof current.$ref === "string"; depth++) {
|
|
1257
1398
|
const segments = current.$ref.replace(/^#\//, "").split("/");
|
|
1258
1399
|
let target = doc;
|
|
1259
|
-
for (const segment of segments) target =
|
|
1400
|
+
for (const segment of segments) target = isObject2(target) ? target[segment.replace(/~1/g, "/").replace(/~0/g, "~")] : void 0;
|
|
1260
1401
|
current = target;
|
|
1261
1402
|
}
|
|
1262
|
-
return
|
|
1403
|
+
return isObject2(current) ? current : void 0;
|
|
1263
1404
|
}
|
|
1264
1405
|
var isJsonMediaType = (type) => /(^|\/|\+)json$/.test(type.split(";")[0].trim().toLowerCase());
|
|
1265
1406
|
function readMultipartOperation(doc, requestBody) {
|
|
1266
1407
|
const content = resolveRef(doc, requestBody)?.content;
|
|
1267
|
-
if (!
|
|
1408
|
+
if (!isObject2(content)) return null;
|
|
1268
1409
|
const multipartKey = Object.keys(content).find((type) => type.split(";")[0].trim().toLowerCase() === "multipart/form-data");
|
|
1269
|
-
if (!multipartKey || !
|
|
1410
|
+
if (!multipartKey || !isObject2(content[multipartKey])) return null;
|
|
1270
1411
|
const schema = resolveRef(doc, content[multipartKey].schema);
|
|
1271
|
-
const properties =
|
|
1412
|
+
const properties = isObject2(schema?.properties) ? schema.properties : {};
|
|
1272
1413
|
const fields = Object.keys(properties).filter((name) => name !== BODY_FIELD);
|
|
1273
1414
|
if (fields.length === 0) return null;
|
|
1274
1415
|
const requiredList = Array.isArray(schema?.required) ? schema.required.filter((name) => typeof name === "string") : [];
|
|
@@ -1774,6 +1915,7 @@ async function generateSdk(options = {}) {
|
|
|
1774
1915
|
const files = new OutputFiles();
|
|
1775
1916
|
if (steps.length < pipeline.length) files.seedFromDisk(modelsDir);
|
|
1776
1917
|
const prettierConfigFile = await findPrettierConfigFile(loaded.rootDir);
|
|
1918
|
+
const constsByFile = /* @__PURE__ */ new Map();
|
|
1777
1919
|
const specDir = fs8.mkdtempSync(path9.join(os.tmpdir(), "ddd-web-spec-"));
|
|
1778
1920
|
try {
|
|
1779
1921
|
for (const [i, { api, write, doc: rawDoc }] of steps.entries()) {
|
|
@@ -1781,7 +1923,9 @@ async function generateSdk(options = {}) {
|
|
|
1781
1923
|
logger.info(`\u2500\u2500 ${api.stem} (${i + 1}/${steps.length})${write ? "" : " (models only)"}`);
|
|
1782
1924
|
const index = indexOperations(doc, api.sideEffects, api.skipUpdatePostTwins);
|
|
1783
1925
|
if (index.skippedTwins.size > 0) logger.info(` skipping ${index.skippedTwins.size} POST twin(s) of PATCH upserts`);
|
|
1784
|
-
const
|
|
1926
|
+
const modelOptions = loaded.config.models ?? {};
|
|
1927
|
+
const prepared = prepareDocumentForModels(doc, modelOptions);
|
|
1928
|
+
const rtk = await runRtkCodegen(api, prepared.doc, index, path9.join(specDir, `${i}.json`), prettierConfigFile);
|
|
1785
1929
|
const endpoints = parseApiEndpoints(rtk.code, logger, rtk.locations);
|
|
1786
1930
|
const handlerEndpoints = detectResponseHandlerEndpoints(doc, endpoints, api.responseHandlers);
|
|
1787
1931
|
const responseKinds = /* @__PURE__ */ new Map();
|
|
@@ -1802,7 +1946,10 @@ async function generateSdk(options = {}) {
|
|
|
1802
1946
|
files,
|
|
1803
1947
|
logger,
|
|
1804
1948
|
esm,
|
|
1805
|
-
responseKinds
|
|
1949
|
+
responseKinds,
|
|
1950
|
+
documentModels: prepared.documentModels,
|
|
1951
|
+
enumConsts: modelOptions.enums === "const",
|
|
1952
|
+
constsByFile
|
|
1806
1953
|
});
|
|
1807
1954
|
if (!write) continue;
|
|
1808
1955
|
const withHandlers = injectResponseHandlers(apiSource, handlerEndpoints);
|
|
@@ -1918,4 +2065,4 @@ export {
|
|
|
1918
2065
|
generateSdk,
|
|
1919
2066
|
linkSkills
|
|
1920
2067
|
};
|
|
1921
|
-
//# sourceMappingURL=chunk-
|
|
2068
|
+
//# sourceMappingURL=chunk-TPFRUK5O.js.map
|