@@ -56,59 +56,170 @@ const OpenAPI = require('openapi-typescript-codegen');
5656
5757OpenAPI .generate ({
5858 input: ' ./api/openapi.json' ,
59- output: ' ./dist '
59+ output: ' ./generated '
6060});
6161```
6262
6363Or by providing the JSON directly:
6464
6565``` javascript
6666const OpenAPI = require (' openapi-typescript-codegen' );
67+
6768const spec = require (' ./api/openapi.json' );
6869
6970OpenAPI .generate ({
7071 input: spec,
71- output: ' ./dist '
72+ output: ' ./generated '
7273});
7374```
7475
7576## Features
7677
7778### Argument-style vs. Object-style
78- There's no [ named parameter] ( https://en.wikipedia.org/wiki/Named_parameter ) in JS/TS , because of that,
79- we offer an option ` --useOptions ` to generate code in two different styles.
79+ There's no [ named parameter] ( https://en.wikipedia.org/wiki/Named_parameter ) in Javascript or Typescript , because of
80+ that, we offer the flag ` --useOptions ` to generate code in two different styles.
8081
8182Argument-style:
8283``` typescript
8384function createUser(name : string , password : string , type ? : string , address ? : string ) {
8485 // ...
8586}
8687
87- // usage
88+ // Usage
8889createUser (' Jack' , ' 123456' , undefined , ' NY US' );
8990```
9091
9192Object-style:
9293``` typescript
93- interface CreateUserOptions {
94+ function createUser({ name , password , type , address } : {
9495 name: string ,
9596 password: string ,
9697 type? : string
9798 address? : string
98- }
99-
100- function createUser({ name , password , type , address }: CreateUserOptions ) {
99+ }) {
101100 // ...
102101}
103102
104- // usage
103+ // Usage
105104createUser ({
106105 name: ' Jack' ,
107106 password: ' 123456' ,
108107 address: ' NY US'
109108});
110109```
111110
111+
112+ ### Runtime schemas
113+ By default the OpenAPI generator only exports interfaces for your models. These interfaces will help you during
114+ development, but will not be available in javascript during runtime. However Swagger allows you to define properties
115+ that can be useful during runtime, for instance: ` maxLength ` of a string or a ` pattern ` to match, etc. Let's say
116+ we have the following model:
117+
118+ ``` json
119+ {
120+ "MyModel" : {
121+ "required" : [
122+ " key" ,
123+ " name"
124+ ],
125+ "type" : " object" ,
126+ "properties" : {
127+ "key" : {
128+ "maxLength" : 64 ,
129+ "pattern" : " ^[a-zA-Z0-9_]*$" ,
130+ "type" : " string"
131+ },
132+ "name" : {
133+ "maxLength" : 255 ,
134+ "type" : " string"
135+ },
136+ "enabled" : {
137+ "type" : " boolean" ,
138+ "readOnly" : true
139+ },
140+ "modified" : {
141+ "type" : " string" ,
142+ "format" : " date-time" ,
143+ "readOnly" : true
144+ }
145+ }
146+ }
147+ }
148+ ```
149+
150+ This will generate the following interface:
151+
152+ ``` typescript
153+ export interface ModelWithPattern {
154+ key: string ;
155+ name: string ;
156+ readonly enabled? : boolean ;
157+ readonly modified? : string ;
158+ }
159+ ```
160+
161+ The interface does not contain any properties like ` maxLength ` or ` pattern ` . However they could be useful
162+ if we wanted to create some form where a user could create such a model. In that form you would iterate
163+ over the properties to render form fields based on their type and validate the input based on the ` maxLength `
164+ or ` pattern ` property. This requires us to have this information somewhere... For this we can use the
165+ flag ` --exportSchemas ` to generate a runtime model next to the normal interface:
166+
167+ ``` typescript
168+ export const $ModelWithPattern = {
169+ properties: {
170+ key: {
171+ type: ' string' ,
172+ isRequired: true ,
173+ maxLength: 64 ,
174+ pattern: ' ^[a-zA-Z0-9_]*$' ,
175+ },
176+ name: {
177+ type: ' string' ,
178+ isRequired: true ,
179+ maxLength: 255 ,
180+ },
181+ enabled: {
182+ type: ' boolean' ,
183+ isReadOnly: true ,
184+ },
185+ modified: {
186+ type: ' string' ,
187+ isReadOnly: true ,
188+ format: ' date-time' ,
189+ },
190+ },
191+ };
192+ ```
193+
194+ These runtime object are prefixed with a ` $ ` character and expose all the interesting attributes of a model
195+ and it's properties. We can now use this object to generate the form:
196+
197+ ``` typescript jsx
198+ import { $ModelWithPattern } from ' ./generated' ;
199+
200+ // Some pseudo code to iterate over the properties and return a form field
201+ // the form field could be some abstract component that renders the correct
202+ // field type and validation rules based on the given input.
203+ const formFields = Object .entries ($ModelWithPattern .properties ).map (([key , value ]) => (
204+ < FormField
205+ name = {key }
206+ type = {value.type }
207+ format = {value.format }
208+ maxLength = {value.maxLength }
209+ pattern = {value.pattern }
210+ isReadOnly = {value.isReadOnly }
211+ / >
212+ ));
213+
214+ const MyForm = () => (
215+ <form >
216+ {formFields }
217+ < / form >
218+ );
219+
220+ ```
221+
222+
112223### Enum with custom names and descriptions
113224You can use ` x-enum-varnames ` and ` x-enum-descriptions ` in your spec to generate enum with custom names and descriptions.
114225It's not in official [ spec] ( https://github.com/OAI/OpenAPI-Specification/issues/681 ) yet. But its a supported extension
@@ -159,6 +270,7 @@ The OpenAPI generator supports Bearer Token authorization. In order to enable th
159270of tokens in each request you can set the token using the global OpenAPI configuration:
160271
161272``` typescript
162- import { OpenAPI } from ' ./'
163- OpenAPI .TOKEN = ' some-bearer-token'
273+ import { OpenAPI } from ' ./generated' ;
274+
275+ OpenAPI .TOKEN = ' some-bearer-token' ;
164276```
0 commit comments