Skip to content

Commit e320614

Browse files
feat(openapi-generator): suggest resolution for unknown codec types
The Problem ----------- If you try to import a codec from `io-ts-types` like this: ```typescript import { DateFromISOString} from 'io-ts-types/DateFromISOString'; ``` you will see an error like this: ``` node_modules/@api-ts/openapi-generator/dist/src/codec.js:287 if (init.type === 'Identifier' || init.type === 'MemberExpression') { ^ TypeError: Cannot read properties of null (reading 'type') ``` This is not a helpful error message, as it does not indicate 1. What went wrong? 2. What does the user need to do to fix it? Additional Context ------------------ Since codecs from `io-ts-types` are frequently used, @api-ts/openapi-generator ships with knowledge of `io-ts-types` codecs[^1]. However, these defaults only match against the user's code when the user imports from the top-level `io-ts-types` package, like this: ```typescript import { DateFromISOString} from 'io-ts-types'; ``` This Change ----------- This diff detects when @api-ts/openapi-generator encounters a codec for which it cannot determine encode/decode types. This happens when a codec is declared with `t.Type` instead of the io-ts combinators, which happens in `io-ts-types` and in some third-party modules. In this case, the generator will now print a message informing the user what went wrong, and provides a suggestion on how the user should resolve the issue: > [ERROR] Could not determine encode/decode types for codec 'MyCustomCodec' in '~/workspace/node_modules/my-custom-types/lib/MyCustomType.d.ts' > Consider defining a custom codec for this type. > > https://github.com/BitGo/api-ts/tree/master/packages/openapi-generator#4-defining-custom-codecs Additionally, when the path defining the codec comes from `io-ts-types`, the message is even more specific: > [ERROR] Could not determine encode/decode types for codec 'DateFromISOString' in '~/workspace/node_modules/io-ts-types/lib/DateFromISOString.d.ts' > It looks like this codec comes from io-ts-types. Try importing directly from io-ts-types instead: > > ``` > import { DateFromISOString } from 'io-ts-types'; > ``` Considerations -------------- I was not able to easily write any tests for this functionality, since our test harness does not currently handle failure cases and asserting against given error messages. [^1]: https://github.com/BitGo/api-ts/blob/b214578caa8272e8370025ae3ef6fd7bf45d514c/packages/openapi-generator/src/knownImports.ts#L334
1 parent 2ae878a commit e320614

File tree

1 file changed

+27
-0
lines changed
  • packages/openapi-generator/src

1 file changed

+27
-0
lines changed

packages/openapi-generator/src/cli.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,33 @@ const app = command({
179179
}
180180
const [newSourceFile, init, comment] = initE.right;
181181

182+
if (init === null) {
183+
console.log({ ref });
184+
let errorMessage = `Could not determine encode/decode types for codec '${ref.name}' in '${ref.location}'`;
185+
if (ref.location.includes('/node_modules/io-ts-types/')) {
186+
errorMessage += `
187+
It looks like this codec comes from io-ts-types. Try importing directly from io-ts-types instead:
188+
189+
\`\`\`
190+
import { ${ref.name} } from 'io-ts-types';
191+
\`\`\`
192+
`;
193+
} else {
194+
errorMessage += `
195+
Consider defining a custom codec for this type.
196+
197+
https://github.com/BitGo/api-ts/tree/master/packages/openapi-generator#4-defining-custom-codecs
198+
`;
199+
}
200+
logError(
201+
errorMessage
202+
.split('\n')
203+
.map((line) => line.trimStart())
204+
.join('\n'),
205+
);
206+
process.exit(1);
207+
}
208+
182209
const codecE = parseCodecInitializer(project, newSourceFile, init);
183210
if (E.isLeft(codecE)) {
184211
logError(

0 commit comments

Comments
 (0)