@mgamadeus/ddd-web 0.12.0 → 0.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.
@@ -725,6 +725,101 @@ function replacePropertyTypes(body, typeTexts, requiredNames = /* @__PURE__ */ n
725
725
  return result;
726
726
  }
727
727
 
728
+ // src/codegen/request-builders.ts
729
+ function splitRequestBuilders(apiSource, requestsObjectName, requestsModuleSpecifier) {
730
+ const ts = loadTypeScript();
731
+ const sourceFile = ts.createSourceFile("api.ts", apiSource, ts.ScriptTarget.Latest, true);
732
+ const builders = [];
733
+ const replacements = [];
734
+ const visit = (node) => {
735
+ if (ts.isPropertyAssignment(node) && ts.isCallExpression(node.initializer)) {
736
+ const call = node.initializer;
737
+ const callee = call.expression;
738
+ const isBuildCall2 = ts.isPropertyAccessExpression(callee) && ["query", "mutation"].includes(callee.name.text) && ts.isIdentifier(callee.expression);
739
+ const definition = call.arguments[0];
740
+ if (isBuildCall2 && definition && ts.isObjectLiteralExpression(definition)) {
741
+ const endpointName = node.name.getText(sourceFile).replace(/^["']|["']$/g, "");
742
+ const queryProperty = definition.properties.find(
743
+ (property) => ts.isPropertyAssignment(property) && property.name.getText(sourceFile) === "query" && ts.isArrowFunction(property.initializer)
744
+ );
745
+ if (queryProperty && ts.isArrowFunction(queryProperty.initializer)) {
746
+ const builder = queryProperty.initializer;
747
+ const argType = call.typeArguments?.[1]?.getText(sourceFile);
748
+ const parameter = builder.parameters[0];
749
+ const parameterText = parameter && !parameter.type && argType && argType !== "void" ? `(${parameter.name.getText(sourceFile)}: ${argType})` : `(${builder.parameters.map((current) => current.getText(sourceFile)).join(", ")})`;
750
+ const bodyText = builder.body.getText(sourceFile);
751
+ builders.push({ name: endpointName, text: `${parameterText}: string | FetchArgs => ${bodyText}` });
752
+ replacements.push({
753
+ start: builder.getStart(sourceFile),
754
+ end: builder.getEnd(),
755
+ text: `${requestsObjectName}.${endpointName}`
756
+ });
757
+ }
758
+ }
759
+ }
760
+ ts.forEachChild(node, visit);
761
+ };
762
+ visit(sourceFile);
763
+ if (builders.length === 0) return { apiSource, requestsSource: "", count: 0 };
764
+ let replacedApiSource = apiSource;
765
+ for (const { start, end, text } of replacements.sort((a, b) => b.start - a.start)) {
766
+ replacedApiSource = replacedApiSource.slice(0, start) + text + replacedApiSource.slice(end);
767
+ }
768
+ const importDeclarations = sourceFile.statements.filter(ts.isImportDeclaration);
769
+ const importTexts = importDeclarations.map((declaration) => declaration.getText(sourceFile));
770
+ const requestsBody = `export const ${requestsObjectName} = {
771
+ ` + builders.map(({ name, text }) => ` ${/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name) ? name : JSON.stringify(name)}: ${text},`).join("\n") + "\n};\n";
772
+ const requestsSource = [
773
+ `import type { FetchArgs } from '@reduxjs/toolkit/query';`,
774
+ ...keepUsedImports(ts, importTexts, requestsBody)
775
+ ].join("\n") + `
776
+
777
+ ${requestsBody}`;
778
+ const apiWithoutImports = stripImports(ts, replacedApiSource);
779
+ const apiImports = [
780
+ ...keepUsedImports(ts, importTexts, apiWithoutImports),
781
+ `import { ${requestsObjectName} } from '${requestsModuleSpecifier}';`
782
+ ];
783
+ return { apiSource: `${apiImports.join("\n")}
784
+ ${apiWithoutImports}`, requestsSource, count: builders.length };
785
+ }
786
+ function stripImports(ts, source) {
787
+ const sourceFile = ts.createSourceFile("api.ts", source, ts.ScriptTarget.Latest, true);
788
+ let result = source;
789
+ for (const declaration of [...sourceFile.statements.filter(ts.isImportDeclaration)].reverse()) {
790
+ result = result.slice(0, declaration.getFullStart()) + result.slice(declaration.getEnd());
791
+ }
792
+ return result.replace(/^\s+/, "\n");
793
+ }
794
+ function keepUsedImports(ts, importTexts, body) {
795
+ const usedNames = /* @__PURE__ */ new Set();
796
+ const bodyFile = ts.createSourceFile("body.ts", body, ts.ScriptTarget.Latest, true);
797
+ const collect = (node) => {
798
+ if (ts.isIdentifier(node) && !(ts.isPropertyAccessExpression(node.parent) && node.parent.name === node) && !(ts.isPropertyAssignment(node.parent) && node.parent.name === node)) {
799
+ usedNames.add(node.text);
800
+ }
801
+ ts.forEachChild(node, collect);
802
+ };
803
+ collect(bodyFile);
804
+ const used = (name) => usedNames.has(name);
805
+ const kept = [];
806
+ for (const importText of importTexts) {
807
+ const sourceFile = ts.createSourceFile("import.ts", importText, ts.ScriptTarget.Latest, true);
808
+ const declaration = sourceFile.statements.find(ts.isImportDeclaration);
809
+ const bindings = declaration?.importClause?.namedBindings;
810
+ if (!declaration || !bindings || !ts.isNamedImports(bindings) || declaration.importClause?.name) {
811
+ kept.push(importText);
812
+ continue;
813
+ }
814
+ const elements = bindings.elements.filter((element) => used(element.name.text));
815
+ if (elements.length === 0) continue;
816
+ const typeOnly = declaration.importClause?.isTypeOnly ? "type " : "";
817
+ const specifier = declaration.moduleSpecifier.getText(sourceFile);
818
+ kept.push(`import ${typeOnly}{ ${elements.map((element) => element.getText(sourceFile)).join(", ")} } from ${specifier};`);
819
+ }
820
+ return kept;
821
+ }
822
+
728
823
  // src/codegen/endpoints.ts
729
824
  function toOpenApiPath(url) {
730
825
  return url.replace(/\$\{encodeURIComponent\(String\(queryArg\.([A-Za-z0-9_]+)\)\)\}/g, "{$1}").replace(/\$\{queryArg\.([A-Za-z0-9_]+)\}/g, "{$1}");
@@ -2082,8 +2177,17 @@ async function generateSdk(options = {}) {
2082
2177
  const apiSource = extractModels({ ...extractOptions, apiSource: multipart.source, responseKinds });
2083
2178
  if (!write) continue;
2084
2179
  const withHandlers = injectResponseHandlers(apiSource, handlerEndpoints);
2085
- files.set(api.outputFile, multipart.injected.length > 0 ? `${MULTIPART_IMPORT}
2086
- ${withHandlers}` : withHandlers);
2180
+ const finalApiSource = multipart.injected.length > 0 ? `${MULTIPART_IMPORT}
2181
+ ${withHandlers}` : withHandlers;
2182
+ if (loaded.config.endpoints?.requestBuilders) {
2183
+ const requestsStem = `${api.stem}Requests`;
2184
+ const split = splitRequestBuilders(finalApiSource, requestsStem, esm ? `./${requestsStem}.js` : `./${requestsStem}`);
2185
+ files.set(api.outputFile, split.apiSource);
2186
+ files.set(path9.join(api.outputDir, `${requestsStem}.ts`), split.requestsSource);
2187
+ logger.info(` ${split.count} request builder(s) in ${requestsStem}.ts`);
2188
+ } else {
2189
+ files.set(api.outputFile, finalApiSource);
2190
+ }
2087
2191
  const withExtension = (specifier) => esm ? `${specifier}.js` : specifier;
2088
2192
  const enhanced = buildEnhancedEndpoints({
2089
2193
  stem: api.stem,
@@ -2195,4 +2299,4 @@ export {
2195
2299
  generateSdk,
2196
2300
  linkSkills
2197
2301
  };
2198
- //# sourceMappingURL=chunk-J2RLNVKK.js.map
2302
+ //# sourceMappingURL=chunk-HVYT2X4V.js.map