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
-[openapi-typescript-fetch](https://www.npmjs.com/package/openapi-typescript-fetch) by [@ajaishankar](https://github.com/ajaishankar)
113
98
114
-
You can generate a fully-typed [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) client from openapiTS types with the [openapi-typescript-fetch](https://www.npmjs.com/package/openapi-typescript-fetch) package:
✨ **Tip**: Automatically-typed fetch wrappers are better to use than manually-assigned generics. The latter is not only more work, but it can be error-prone (which makes your OpenAPI typing worthless if it can’t catch all your errors!).
136
116
137
117
### 📖 Options
138
118
139
119
The following flags can be appended to the CLI command.
|`--help`||| Display inline help message and exit |
144
-
|`--version`||| Display this library’s version and exit |
145
-
|`--output [location]`|`-o`| (stdout) | Where should the output file be saved? |
146
-
|`--auth [token]`||| Provide an auth token to be passed along in the request (only if accessing a private schema) |
147
-
|`--header`|`-x`|| Provide an array of or singular headers as an alternative to a JSON object. Each header must follow the `key: value` pattern |
148
-
|`--headers-object="{…}"`|`-h`|| Provide a JSON object as string of HTTP headers for remote schema request. This will take priority over `--header`|
149
-
|`--http-method`|`-m`|`GET`| Provide the HTTP Verb/Method for fetching a schema from a remote URL |
150
-
|`--immutable-types`||`false`| Generates immutable types (readonly properties and readonly array) |
151
-
|`--additional-properties`||`false`| Allow arbitrary properties for all schema objects without `additionalProperties: false`|
152
-
|`--empty-objects-unknown`||`false`| Allow arbitrary properties for schema objects with no specified properties, and no specified `additionalProperties`|
153
-
|`--default-non-nullable`||`false`| Treat schema objects with default values as non-nullable |
154
-
|`--export-type`|`-t`|`false`| Export `type` instead of `interface`|
155
-
|`--path-params-as-types`||`false`| Allow dynamic string lookups on the `paths` object |
156
-
|`--support-array-length`||`false`| Generate tuples using array `minItems` / `maxItems`|
|`--help`||| Display inline help message and exit |
124
+
|`--version`||| Display this library’s version and exit |
125
+
|`--output [location]`|`-o`| (stdout) | Where should the output file be saved? |
126
+
|`--auth [token]`||| Provide an auth token to be passed along in the request (only if accessing a private schema) |
127
+
|`--header`|`-x`|| Provide an array of or singular headers as an alternative to a JSON object. Each header must follow the `key: value` pattern |
128
+
|`--headers-object="{…}"`|`-h`|| Provide a JSON object as string of HTTP headers for remote schema request. This will take priority over `--header`|
129
+
|`--http-method`|`-m`|`GET`| Provide the HTTP Verb/Method for fetching a schema from a remote URL |
130
+
|`--immutable-types`||`false`| Generates immutable types (readonly properties and readonly array) |
131
+
|`--additional-properties`||`false`| Allow arbitrary properties for all schema objects without `additionalProperties: false`|
132
+
|`--empty-objects-unknown`||`false`| Allow arbitrary properties for schema objects with no specified properties, and no specified `additionalProperties`|
133
+
|`--default-non-nullable`||`false`| Treat schema objects with default values as non-nullable |
134
+
|`--export-type`|`-t`|`false`| Export `type` instead of `interface`|
135
+
|`--path-params-as-types`||`false`| Allow dynamic string lookups on the `paths` object |
136
+
|`--support-array-length`||`false`| Generate tuples using array `minItems` / `maxItems`|
Though this is a contrived example, you could use this feature to automatically infer typing based on the URL in a fetch client or in some other useful place in your application.
@@ -233,7 +213,7 @@ import fs from "node:fs";
233
213
import openapiTS from "openapi-typescript";
234
214
235
215
// example 1: load [object] as schema (JSON only)
236
-
const schema = await fs.promises.readFile("spec.json", "utf8") // must be OpenAPI JSON
216
+
const schema = await fs.promises.readFile("spec.json", "utf8"); // must be OpenAPI JSON
0 commit comments