@@ -8,9 +8,9 @@ sidebar_position: 3
8
8
9
9
You need to configure the generator to:
10
10
11
- - Correctly interpret ` io-ts ` types from external packages
11
+ - Correctly interpret ` io-ts ` types from external packages.
12
12
- Define OpenAPI schemas for custom ` io-ts ` codecs that can't be automatically derived
13
- from the Abstract Syntax Tree (AST)
13
+ from the Abstract Syntax Tree (AST).
14
14
15
15
## Preparing external types packages
16
16
@@ -20,21 +20,21 @@ external package's `package.json`:
20
20
1 . Include source code in the published npm bundle by adding the source directory to the
21
21
` files ` array:
22
22
23
- ``` json
24
- // package.json of the external types package
25
- {
26
- "name" : " my-external-types" ,
27
- "version" : " 1.0.0" ,
28
- "files" : [
29
- " dist/" ,
30
- " src/" // Include source code
31
- ],
32
- "main" : " dist/index.js" ,
33
- "types" : " dist/index.d.ts" ,
34
- "source" : " src/index.ts" // Add this field
35
- // ... rest of package.json
36
- }
37
- ```
23
+ ``` json
24
+ // package.json of the external types package
25
+ {
26
+ "name" : " my-external-types" ,
27
+ "version" : " 1.0.0" ,
28
+ "files" : [
29
+ " dist/" ,
30
+ " src/" // Include source code
31
+ ],
32
+ "main" : " dist/index.js" ,
33
+ "types" : " dist/index.d.ts" ,
34
+ "source" : " src/index.ts" // Add this field
35
+ // ... rest of package.json
36
+ }
37
+ ```
38
38
39
39
2 . Specify the source entry point using the ` source ` field (for example,
40
40
` "source": "src/index.ts" ` )
@@ -51,48 +51,49 @@ You can define schemas directly within the package that declares the custom code
51
51
1 . Create a file named ` openapi-gen.config.js ` in the root of the types package
52
52
53
53
2 . Update the package's ` package.json ` to include:
54
- - The ` customCodecFile ` field pointing to this file
55
- - The config file in the ` files ` array
56
-
57
- ``` json
58
- // package.json of the types package defining custom codecs
59
- {
60
- "name" : " my-custom-codec-package" ,
61
- // ...
62
- "files" : [
63
- " dist/" ,
64
- " src/" ,
65
- " openapi-gen.config.js" // Include the config file
66
- ],
67
- "customCodecFile" : " openapi-gen.config.js" // Point to the file
68
- // ...
69
- }
70
- ```
54
+
55
+ - The ` customCodecFile ` field pointing to this file
56
+ - The config file in the ` files ` array
57
+
58
+ ``` json
59
+ // package.json of the types package defining custom codecs
60
+ {
61
+ "name" : " my-custom-codec-package" ,
62
+ // ...
63
+ "files" : [
64
+ " dist/" ,
65
+ " src/" ,
66
+ " openapi-gen.config.js" // Include the config file
67
+ ],
68
+ "customCodecFile" : " openapi-gen.config.js" // Point to the file
69
+ // ...
70
+ }
71
+ ```
71
72
72
73
3 . Structure the ` openapi-gen.config.js ` file as follows:
73
74
74
- ``` javascript
75
- // openapi-gen.config.js
76
- module .exports = (E ) => {
77
- return {
78
- // Key matches the exported codec name (e.g., export const MyCustomString = ...)
79
- MyCustomString : () =>
80
- E .right ({
81
- type: ' string' ,
82
- format: ' custom-format' ,
83
- description: ' A custom string type definition' ,
84
- }),
85
- AnotherCustomType : () =>
86
- E .right ({
87
- type: ' object' ,
88
- properties: {
89
- /* ... */
90
- },
91
- }),
92
- // ... other custom codec definitions
93
- };
94
- };
95
- ```
75
+ ``` javascript
76
+ // openapi-gen.config.js
77
+ module .exports = (E ) => {
78
+ return {
79
+ // Key matches the exported codec name (e.g., export const MyCustomString = ...)
80
+ MyCustomString : () =>
81
+ E .right ({
82
+ type: ' string' ,
83
+ format: ' custom-format' ,
84
+ description: ' A custom string type definition' ,
85
+ }),
86
+ AnotherCustomType : () =>
87
+ E .right ({
88
+ type: ' object' ,
89
+ properties: {
90
+ /* ... */
91
+ },
92
+ }),
93
+ // ... other custom codec definitions
94
+ };
95
+ };
96
+ ```
96
97
97
98
The exported function receives the ` fp-ts/Either ` namespace (` E ` ) as an argument. You
98
99
should return an object where:
@@ -109,25 +110,25 @@ path via the `--codec-file` option:
109
110
110
111
2 . Structure the file similarly to Method 1, but group definitions by package:
111
112
112
- ``` javascript
113
- // custom-codecs.js
114
- module .exports = (E ) => {
115
- return {
116
- ' io-ts-bigint' : {
117
- // Package name
118
- BigIntFromString : () => E .right ({ type: ' string' , format: ' bigint' }),
119
- NonZeroBigIntFromString : () =>
120
- E .right ({ type: ' string' , format: ' bigint' /* constraints */ }),
121
- // ... other codecs from 'io-ts-bigint'
122
- },
123
- ' my-other-custom-package' : {
124
- // Another package
125
- SomeType : () => E .right ({ type: ' number' , format: ' float' }),
126
- },
127
- // ... other packages
128
- };
129
- };
130
- ```
113
+ ``` javascript
114
+ // custom-codecs.js
115
+ module .exports = (E ) => {
116
+ return {
117
+ ' io-ts-bigint' : {
118
+ // Package name
119
+ BigIntFromString : () => E .right ({ type: ' string' , format: ' bigint' }),
120
+ NonZeroBigIntFromString : () =>
121
+ E .right ({ type: ' string' , format: ' bigint' /* constraints */ }),
122
+ // ... other codecs from 'io-ts-bigint'
123
+ },
124
+ ' my-other-custom-package' : {
125
+ // Another package
126
+ SomeType : () => E .right ({ type: ' number' , format: ' float' }),
127
+ },
128
+ // ... other packages
129
+ };
130
+ };
131
+ ```
131
132
132
133
In this structure:
133
134
@@ -136,6 +137,6 @@ In this structure:
136
137
137
138
3 . Run the generator with the ` --codec-file ` option:
138
139
139
- ``` shell
140
- npx openapi-generator --codec-file ./custom-codecs.js src/index.ts
141
- ```
140
+ ``` shell
141
+ npx openapi-generator --codec-file ./custom-codecs.js src/index.ts
142
+ ```
0 commit comments