@@ -5,7 +5,7 @@ API specification into an OpenAPI specification.
55
66## Install
77
8- ```
8+ ``` shell
99npm install --save-dev @api-ts/openapi-generator
1010```
1111
@@ -15,7 +15,7 @@ The **openapi-generator** assumes the io-ts-http `apiSpec` is exported in the to
1515of the Typescript file passed as an input parameter. The OpenAPI specification will be
1616written to stdout.
1717
18- ```
18+ ``` shell
1919ARGUMENTS:
2020 < file> - API route definition file
2121
@@ -35,32 +35,125 @@ For example:
3535npx openapi-generator src/index.ts
3636```
3737
38- ## Custom codec file
39-
40- ` openapi-generator ` only reads files in the specified package, and stops at the module
41- boundary. This allows it to work even without ` node_modules ` installed. It has built-in
42- support for ` io-ts ` , ` io-ts-types ` , and ` @api-ts/io-ts-http ` imports. If your package
43- imports codecs from another external library, then you will have to define them in a
44- custom configuration file so that ` openapi-generator ` will understand them. To do so,
45- create a JS file with the following format:
46-
47- ``` typescript
48- module .exports = (E ) => {
49- return {
50- ' io-ts-bigint' : {
51- BigIntFromString : () => E .right ({ type: ' string' }),
52- NonZeroBigInt : () => E .right ({ type: ' number' }),
53- NonZeroBigIntFromString : () => E .right ({ type: ' string' }),
54- NegativeBigIntFromString : () => E .right ({ type: ' string' }),
55- NonNegativeBigIntFromString : () => E .right ({ type: ' string' }),
56- PositiveBigIntFromString : () => E .right ({ type: ' string' }),
57- },
58- // ... and so on for other packages
59- };
60- };
61- ```
38+ ## Preparing a types package for reusable codecs
39+
40+ In order to use types from external ` io-ts ` types packages, you must ensure two things
41+ are done.
42+
43+ 1 . The package source code must be included in the bundle, as the generator is built to
44+ generate specs based from the Typescript AST. It is not set up to work with
45+ transpiled js code. You can do this by modifying your ` package.json ` to include your
46+ source code in the bundle. For example, if the source code is present in the ` src/ `
47+ directory, then add ` src/ ` to the files array in the ` package.json ` of your project.
48+ 2 . After Step 1, change the ` types ` field in the ` package.json ` to be the entry point of
49+ the types in the source code. For example, if the entrypoint is ` src/index.ts ` , then
50+ set ` "types": "src/index.ts" ` in the ` package.json `
51+
52+ ## Defining Custom Codecs
53+
54+ When working with ` openapi-generator ` , you may encounter challenges with handling custom
55+ codecs that require JavaScript interpretation or aren't natively supported by the
56+ generator. These issues typically arise with codecs such as ` new t.Type(...) ` and other
57+ primitives that aren't directly supported. However, there are two solutions to address
58+ these challenges effectively. Click [ here] ( #list-of-supported-io-ts-primitives ) for the
59+ list of supported primitives.
60+
61+ ### Solution 1: Defining Custom Codec Schemas in the Types Package (recommended)
62+
63+ ` openapi-generator ` now offers the ability to define the schema of custom codecs
64+ directly within the types package that defines them, rather than the downstream package
65+ that uses them. This approach is particularly useful for codecs that are used in many
66+ different types packages. Here’s how you can define schemas for your custom codecs in
67+ the upstream repository:
68+
69+ 1 . Create a file named ` openapi-gen.config.js ` in the root of your repository.
70+
71+ 2 . Add the following line to the ` package.json ` of the types package:
72+
73+ ``` json
74+ "customCodecFile" : " openapi-gen.config.js"
75+ ```
76+
77+ You must also add ` "openapi-gen.config.js" ` to the files field in the package.json,
78+ so that it is included in the final bundle.
79+
80+ 3 . In the ` openapi-gen.config.js ` file, define your custom codecs:
81+
82+ ``` javascript
83+ module .exports = (E ) => {
84+ return {
85+ SampleCodecDefinition : () =>
86+ E .right ({
87+ type: ' string' ,
88+ default: ' defaultString' ,
89+ minLength: 1 ,
90+ }),
91+ // ... rest of your custom codec definitions
92+ };
93+ };
94+ ```
95+
96+ By following these steps, the schemas for your custom codecs will be included in the
97+ generated API docs for any endpoints that use the respective codecs. The input parameter
98+ ` E ` is the namespace import of ` fp-ts/Either ` , and the return type should be a ` Record `
99+ containing AST definitions for external libraries. For more details, see
100+ [ KNOWN_IMPORTS] ( ./src/knownImports.ts ) .
101+
102+ ### Solution 2: Using a Custom Codec Configuration File
103+
104+ ` openapi-generator ` supports importing codecs from other packages in ` node_modules ` , but
105+ it struggles with ` io-ts ` primitives that need JavaScript interpretation, such as
106+ ` new t.Type(...) ` . To work around this, you can define schemas for these codecs in a
107+ configuration file within your downstream types package (where you generate the API
108+ docs). This allows the generator to understand and use these schemas where necessary.
109+ Follow these steps to create and use a custom codec configuration file:
110+
111+ 1 . Create a JavaScript file with the following format:
112+
113+ ``` javascript
114+ module .exports = (E ) => {
115+ return {
116+ ' io-ts-bigint' : {
117+ BigIntFromString : () => E .right ({ type: ' string' }),
118+ NonZeroBigInt : () => E .right ({ type: ' number' }),
119+ NonZeroBigIntFromString : () => E .right ({ type: ' string' }),
120+ NegativeBigIntFromString : () => E .right ({ type: ' string' }),
121+ NonNegativeBigIntFromString : () => E .right ({ type: ' string' }),
122+ PositiveBigIntFromString : () => E .right ({ type: ' string' }),
123+ },
124+ // ... and so on for other packages
125+ };
126+ };
127+ ```
128+
129+ 2 . The input parameter ` E ` is the namespace import of ` fp-ts/Either ` , which avoids
130+ issues with ` require ` . The return type should be a ` Record ` containing AST
131+ definitions for external libraries. For more information on the structure, refer to
132+ [ KNOWN_IMPORTS] ( ./src/knownImports.ts ) .
133+
134+ ## List of supported io-ts primitives
62135
63- The input parameter ` E ` is the namespace import of ` fp-ts/Either ` (so that trying to
64- ` require ` it from the config file isn't an issue), and the return type is a ` Record `
65- containing AST definitions for external libraries.
66- [ Refer to KNOWN_IMPORTS here for info on the structure] ( ./src/knownImports.ts )
136+ - string
137+ - number
138+ - bigint
139+ - boolean
140+ - null
141+ - nullType
142+ - undefined
143+ - unknown
144+ - any
145+ - array
146+ - readonlyArray
147+ - object
148+ - type
149+ - partial
150+ - exact
151+ - strict
152+ - record
153+ - union
154+ - intersection
155+ - literal
156+ - keyof
157+ - brand
158+ - UnknownRecord
159+ - void
0 commit comments