Skip to content

Commit 0980e3e

Browse files
committed
feat: ability to send custom ts code translator;
fix: problem with dot in query params
1 parent 4ad99d3 commit 0980e3e

File tree

17 files changed

+353
-142
lines changed

17 files changed

+353
-142
lines changed

CHANGELOG.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
# next release
22

3+
fix: problems with dot in query params (hard fix) (#460)
4+
feature: ability to send custom Ts output code translator to js. Example:
5+
```ts
6+
const { Translator } = require("swagger-typescript-api/src/translators/translator");
7+
const { JavascriptTranslator } = require("swagger-typescript-api/src/translators/javascript");
8+
9+
class MyTranslator extends Translator { // or use extends JavascriptTranslator
10+
translate({ fileName, fileExtension, fileContent }) {
11+
// format ts\js code with using this codeFormatter (prettier + ts import fixer)
12+
this.codeFormatter.format(fileContent)
13+
// config of the code gen process
14+
this.config.
15+
// logger
16+
this.logger.
17+
18+
return [
19+
{
20+
fileName,
21+
fileExtension,
22+
fileContent,
23+
}
24+
]
25+
}
26+
}
27+
```
28+
329
## 12.0.2
430

531
fix: missing option `--extract-enums` (#344)

index.d.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,37 @@ interface GenerateApiParamsBase {
168168

169169
/** configuration for fetching swagger schema requests */
170170
requestOptions?: null | Partial<import("node-fetch").RequestInit>;
171+
172+
/** ts compiler configuration object (for --to-js option) */
173+
compilerTsConfig?: Record<string, any>;
174+
175+
/**
176+
* custom ts->* translator
177+
* do not use constructor args, it can break functionality of this property, just send class reference
178+
*
179+
* @example
180+
* ```ts
181+
* const { Translator } = require("swagger-typescript-api/src/translators/translator");
182+
*
183+
* class MyTranslator extends Translator {
184+
*
185+
* translate({ fileName, fileExtension, fileContent }) {
186+
* this.codeFormatter.format()
187+
* this.config.
188+
* this.logger.
189+
*
190+
* return [
191+
* {
192+
* fileName,
193+
* fileExtension,
194+
* fileContent,
195+
* }
196+
* ]
197+
* }
198+
* }
199+
* ```
200+
*/
201+
customTranslator?: new () => typeof import("./src/translators/translator").Translator;
171202
}
172203

173204
type CodeGenConstruct = {
@@ -505,6 +536,9 @@ export interface GenerateApiConfiguration {
505536
hooks: Hooks;
506537
enumNamesAsValues: boolean;
507538
version: string;
539+
compilerTsConfig: Record<string, any>;
540+
/** do not use constructor args, it can break functionality of this property, just send class reference */
541+
customTranslator?: new (...args: never[]) => typeof import("./src/translators/translator").Translator;
508542
internalTemplateOptions: {
509543
addUtilRequiredKeysType: boolean;
510544
};
@@ -569,9 +603,18 @@ export interface GenerateApiConfiguration {
569603
};
570604
}
571605

606+
type FileInfo = {
607+
/** @example myFilename */
608+
fileName: string;
609+
/** @example .d.ts */
610+
fileExtension: string;
611+
/** content of the file */
612+
fileContent: string;
613+
};
614+
572615
export interface GenerateApiOutput {
573616
configuration: GenerateApiConfiguration;
574-
files: { name: string; content: string; declaration: { name: string; content: string } | null }[];
617+
files: FileInfo[];
575618
createFile: (params: { path: string; fileName: string; content: string; withPrefix?: boolean }) => void;
576619
renderTemplate: (
577620
templateContent: string,

0 commit comments

Comments
 (0)