You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
const input = JSON.parse(readFileSync("spec.json", "utf8")); // Input can be any JS object (OpenAPI format)
128
-
const output = swaggerToTS(input); // Outputs TypeScript defs as a string (to be parsed, or written to a file)
128
+
const output = openapiTS(input); // Outputs TypeScript defs as a string (to be parsed, or written to a file)
129
129
```
130
130
131
131
The Node API is a bit more flexible: it will only take a JS object as input (OpenAPI format), and return a string of TS
@@ -135,19 +135,44 @@ post-process, and save the output anywhere.
135
135
If your specs are in YAML, you’ll have to convert them to JS objects using a library such as [js-yaml][js-yaml]. If
136
136
you’re batching large folders of specs, [glob][glob] may also come in handy.
137
137
138
-
##Migrating from v1 to v2
138
+
#### Custom Formatter
139
139
140
-
[Migrating from v1 to v2](./docs/migrating-from-v1.md)
140
+
If using the Node.js API, you can optionally pass a **formatter**to openapi-typescript. This is useful if you want to override the default types and substitute your own.
141
141
142
-
## Project Goals
142
+
For example, say your schema has the following property:
143
+
144
+
```yaml
145
+
properties:
146
+
updated_at:
147
+
type: string
148
+
format: date-time
149
+
```
150
+
151
+
By default, this will generate a type `updated_at?: string;`. But we can override this by passing a formatter to the Node API, like so:
152
+
153
+
```js
154
+
const types = openapiTS(mySchema, {
155
+
formatter(node: SchemaObject) {
156
+
if (node.format === 'date-time') {
157
+
return "Date"; // return the TypeScript “Date” type, as a string
158
+
}
159
+
// for all other schema objects, let openapi-typescript decide (return undefined)
160
+
});
161
+
```
162
+
163
+
This will generate `updated_at?: Date` instead. Note that you will still have to do the parsing of your data yourself. But this will save you from having to also update all your types.
164
+
165
+
_Note: you don’t have to use `.format`—this is just an example! You can use any property on a schema object to overwrite its generated type if desired._
166
+
167
+
## 🏅 Project Goals
143
168
144
169
1. Support converting any OpenAPI 3.0 or 2.0 (Swagger) schema to TypeScript types, no matter how complicated
145
170
1. The generated TypeScript types **must** match your schema as closely as possible (i.e. don’t convert names to
146
171
`PascalCase`or follow any TypeScript-isms; faithfully reproduce your schema as closely as possible, capitalization
147
172
and all)
148
173
1. This library is a TypeScript generator, not a schema validator.
149
174
150
-
## Contributing
175
+
## 🤝 Contributing
151
176
152
177
PRs are welcome! Please see our [CONTRIBUTING.md](./CONTRIBUTING.md) guide. Opening an issue beforehand to discuss is
0 commit comments