acacode / swagger-typescript-api

Generate the API Client for Fetch or Axios from an OpenAPI Specification
MIT License
3.38k stars 361 forks source link

With `toJS` and `modular`, it makes `import { ... } from './data-contracts'` disappeared #979

Open leezu2020 opened 2 weeks ago

leezu2020 commented 2 weeks ago

Hi, there.

Using with toJS and modular, import { ... } from './data-contracts' is disappeared. Without toJS, it seems to be going well.

I don't know why it's fine, doing like below.

// src/translators/javascript.ts
...
export class JavascriptTranslator extends Translator {
  ...
  translate = async (input) => {
    const compiled = this.compileTSCode(input);

    const jsFileName = `${input.fileName}${typescript.Extension.Js}`;
    const dtsFileName = `${input.fileName}${typescript.Extension.Dts}`;
    const sourceContent = compiled[jsFileName];
    const tsImportRows = input.fileContent
      .split("\n")
      .filter((line) => line.startsWith("import "));
    const declarationContent = compiled[dtsFileName]
      .split("\n")
      .map((line) => {
        // I commented out this lines.
        // if (line.startsWith("import ")) {
        //  return tsImportRows.shift();
        // }
        return line;
      })
      .join("\n");

    return [
      {
        fileName: input.fileName,
        fileExtension: typescript.Extension.Js,
        fileContent: await this.codeFormatter.formatCode(sourceContent),
      },
      {
        fileName: input.fileName,
        fileExtension: typescript.Extension.Dts,
        fileContent: await this.codeFormatter.formatCode(declarationContent),
      },
    ];
  };
}