Skip to content

Commit b406a60

Browse files
authored
feat(generator): Allow overrides during generation (fix #71) (#73)
1 parent 7596578 commit b406a60

File tree

16 files changed

+15722
-3394
lines changed

16 files changed

+15722
-3394
lines changed

.yarn/versions/37345876.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
releases:
2+
"@typoas/cli": minor
3+
"@typoas/generator": minor

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Main features are:
2020
- Automatically convert `format: 'date-time'` to JS `Date`
2121
- Handle **API Key**, **HTTP Config**, **OAuth2**<sup>1</sup> and **OIDC**<sup>1</sup> auth security schemes
2222
- JSDoc for schemas and operations
23+
- Override system for generated types
2324
- Uses `fetch` api (can be customized)
2425
- Non JSON content type support
2526
- Small bundle size
@@ -49,6 +50,7 @@ The project is split into 4 packages:
4950
- [Using with React Query](#using-with-react-query)
5051
- [Examples](#examples)
5152
- [Notes](#notes)
53+
- [Overrides](#overrides)
5254
- [External references](#external-references)
5355
- [Parameters serialization](#parameters-serialization)
5456
- [Migrating from v1 to v2](#migrating-from-v1-to-v2)
@@ -85,6 +87,8 @@ Here is a short list of supported command line options:
8587
--wrap-lines-at Define a maximum width for JS Doc comments, 0 to disable (default: 120)
8688
--only-types Use it to only generate types in #components/schemas/
8789
--no-fetcher-options Use it to disable the additional param added to every operations
90+
--override You can define a list of types that will be imported from a custom file instead of being generated. Must be used with --override-import
91+
--override-import Path to the file that will be imported to resolve overrides
8892
--version Output the version number
8993
-h, --help Display help for command
9094
```
@@ -280,6 +284,38 @@ You can find examples in the [`examples`](./examples) folder.
280284

281285
Here is some notes on some known issues.
282286

287+
### Overrides
288+
289+
Sometimes you may want to override some of the generated types.
290+
For example, Typoas will not generate Template Literal Types or Generics as it's hard to be defined in an OpenAPI spec.
291+
292+
This works by listing the types you want to override and giving the import path you want to use.
293+
294+
For example, if you want to override the PetStore `Category` type of your spec:
295+
296+
```typescript
297+
// my-overrides.ts
298+
export type Category = {
299+
id?: number;
300+
name?: string;
301+
description?: string;
302+
};
303+
```
304+
305+
When you generate the client, you can pass the `--overrides` option:
306+
307+
```bash
308+
yarn dlx @typoas/cli generate -i my-spec.json -o src/client.ts --override-import my-overrides.ts --override Category
309+
```
310+
311+
This will not generate the `Category` type but import it from the `my-overrides.ts` file. This allows easy customization of generated types.
312+
313+
There are a couple of things to note:
314+
315+
- The override name is the **sanitized** name of the type. You can first generate the client and see the generated types to know the name.
316+
- _Typoas_ will not check the override file. If the file isn't there or the type isn't exported, it will throw a TypeScript error.
317+
- You will have to export the type yourself, _Typoas_ will not do it for you (contrary to the generated types).
318+
283319
### External references
284320

285321
External references are not supported. Every `$ref` must be in the spec.

examples/scripts/generate.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import cli from '@typoas/cli';
22

3-
type Sample = { input: string; output: string };
3+
type Sample = {
4+
input: string;
5+
output: string;
6+
args?: string[];
7+
};
48

59
async function run() {
610
const context = {
@@ -25,6 +29,16 @@ async function run() {
2529
input: 'https://petstore3.swagger.io/api/v3/openapi.json',
2630
output: 'petstore',
2731
},
32+
{
33+
input: 'https://petstore3.swagger.io/api/v3/openapi.json',
34+
output: 'petstore-with-overrides',
35+
args: [
36+
'--override',
37+
'Category',
38+
'--override-import',
39+
'./petstore-overrides',
40+
],
41+
},
2842
];
2943

3044
for (const sample of samples) {
@@ -37,6 +51,7 @@ async function run() {
3751
`./src/${sample.output}.ts`,
3852
'-e',
3953
'-p',
54+
...(sample.args || []),
4055
],
4156
context,
4257
);

0 commit comments

Comments
 (0)