Skip to content

Commit 42c0ca9

Browse files
authored
Warn if no models, instead of throwing an error (#67)
1 parent be51e19 commit 42c0ca9

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

src/createImports.mts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@ export const createImports = ({
2020
.find((sourceFile) => sourceFile.getFilePath().includes("services.ts"));
2121

2222
if (!modelsFile) {
23-
throw new Error("No models file found");
23+
console.warn(`
24+
⚠️ WARNING: No models file found.
25+
This may be an error if \`.components.schemas\` or \`.components.parameters\` is defined in your OpenAPI input.`);
2426
}
2527

2628
if (!serviceFile) {
2729
throw new Error("No service file found");
2830
}
2931

30-
const modalNames = Array.from(modelsFile.getExportedDeclarations().keys());
32+
const modelNames = modelsFile
33+
? Array.from(modelsFile.getExportedDeclarations().keys())
34+
: [];
3135

3236
const serviceExports = Array.from(
3337
serviceFile.getExportedDeclarations().keys()
@@ -41,7 +45,7 @@ export const createImports = ({
4145
name.endsWith("Data")
4246
);
4347

44-
return [
48+
const imports = [
4549
ts.factory.createImportDeclaration(
4650
undefined,
4751
ts.factory.createImportClause(
@@ -115,24 +119,29 @@ export const createImports = ({
115119
ts.factory.createStringLiteral(join("../requests")),
116120
undefined
117121
),
122+
];
123+
if (modelsFile) {
118124
// import all the models by name
119-
ts.factory.createImportDeclaration(
120-
undefined,
121-
ts.factory.createImportClause(
122-
false,
125+
imports.push(
126+
ts.factory.createImportDeclaration(
123127
undefined,
124-
ts.factory.createNamedImports([
125-
...modalNames.map((modelName) =>
126-
ts.factory.createImportSpecifier(
127-
false,
128-
undefined,
129-
ts.factory.createIdentifier(modelName)
130-
)
131-
),
132-
])
133-
),
134-
ts.factory.createStringLiteral(join("../requests/models")),
135-
undefined
136-
),
137-
];
128+
ts.factory.createImportClause(
129+
false,
130+
undefined,
131+
ts.factory.createNamedImports([
132+
...modelNames.map((modelName) =>
133+
ts.factory.createImportSpecifier(
134+
false,
135+
undefined,
136+
ts.factory.createIdentifier(modelName)
137+
)
138+
),
139+
])
140+
),
141+
ts.factory.createStringLiteral(join("../requests/models")),
142+
undefined
143+
)
144+
);
145+
}
146+
return imports;
138147
};

0 commit comments

Comments
 (0)