Skip to content

Commit 4521484

Browse files
committed
docs: add getName hook docs
1 parent 0a9bb68 commit 4521484

2 files changed

Lines changed: 76 additions & 5 deletions

File tree

docs/openapi-ts/configuration/output.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ export default {
307307
```
308308
<!-- prettier-ignore-end -->
309309

310-
You can skip processing by adding the output path to the tools ignore file (for example `.eslintignore` or `.prettierignore`).
310+
You can skip processing by adding the output path to the tool's ignore file (for example `.eslintignore` or `.prettierignore`).
311311

312312
## Name Conflicts
313313

docs/openapi-ts/configuration/parser.md

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ export default {
550550
input: 'hey-api/backend', // sign up at app.heyapi.dev
551551
output: 'src/client',
552552
parser: {
553-
hooks: {}, // configure global hooks here // [!code ++]
553+
hooks: {}, // configure global hooks // [!code ++]
554554
},
555555
};
556556
```
@@ -562,7 +562,7 @@ export default {
562562
plugins: [
563563
{
564564
name: '@tanstack/react-query',
565-
'~hooks': {}, // configure plugin hooks here // [!code ++]
565+
'~hooks': {}, // configure plugin hooks // [!code ++]
566566
},
567567
],
568568
};
@@ -651,17 +651,88 @@ export default {
651651
parser: {
652652
hooks: {
653653
symbols: {
654-
getFilePath: (symbol) => {
654+
getFilePath: (symbol) => { // [!code ++]
655655
if (symbol.name) { // [!code ++]
656656
return symbol.name[0]?.toLowerCase(); // [!code ++]
657657
} // [!code ++]
658-
},
658+
}, // [!code ++]
659659
},
660660
},
661661
},
662662
};
663663
```
664664
<!-- prettier-ignore-end -->
665665
666+
Most plugins expose configuration options that allow you to rename many of the generated symbols. If you need even more control, use the `getName()` hook.
667+
668+
#### Example: Enum naming
669+
670+
By default, generated enums use the same name for both the type and the runtime value.
671+
672+
::: code-group
673+
674+
```ts [example]
675+
export const Flags = {
676+
ALPHA: 'alpha',
677+
BETA: 'beta',
678+
} as const;
679+
680+
export type Flags = (typeof Flags)[keyof typeof Flags];
681+
```
682+
683+
```js [config]
684+
export default {
685+
input: 'hey-api/backend', // sign up at app.heyapi.dev
686+
output: 'src/client',
687+
plugins: [
688+
{
689+
enums: 'javascript',
690+
name: '@hey-api/typescript',
691+
},
692+
],
693+
};
694+
```
695+
696+
:::
697+
698+
While this code works perfectly fine due to TypeScript's declaration merging, let's say we want to use a different name for the type. We can accomplish this with the `getName()` hook.
699+
700+
::: code-group
701+
702+
```ts [example]
703+
export const Flags = {
704+
ALPHA: 'alpha',
705+
BETA: 'beta',
706+
} as const;
707+
708+
export type FlagsType = (typeof Flags)[keyof typeof Flags]; // [!code ++]
709+
```
710+
711+
<!-- prettier-ignore-start -->
712+
```js [config]
713+
export default {
714+
input: 'hey-api/backend', // sign up at app.heyapi.dev
715+
output: 'src/client',
716+
plugins: [
717+
{
718+
enums: 'javascript',
719+
name: '@hey-api/typescript',
720+
'~hooks': {
721+
symbols: {
722+
getName({ meta, name, schema }) { // [!code ++]
723+
if (schema?.type === 'enum' && meta.category === 'type') { // [!code ++]
724+
return `${name}Type`; // [!code ++]
725+
} // [!code ++]
726+
}, // [!code ++]
727+
},
728+
},
729+
},
730+
],
731+
};
732+
```
733+
<!-- prettier-ignore-end -->
734+
735+
:::
736+
666737
<!--@include: ../../partials/examples.md-->
667738
<!--@include: ../../partials/sponsors.md-->

0 commit comments

Comments
 (0)