openapi-typescript
Advanced tools
Comparing version 7.0.2 to 7.0.3
@@ -82,2 +82,14 @@ #!/usr/bin/env node | ||
/** | ||
* Normalize the output path into a file URL. | ||
* @param {string} output - The output path to be transformed. | ||
* @returns {URL} The transformed file URL. | ||
*/ | ||
function normalizeOutput(output) { | ||
if (path.isAbsolute(output)) { | ||
return new URL(`file://${output}}`); | ||
} | ||
return new URL(output, CWD); | ||
} | ||
/** | ||
* @param {string | URL} schema | ||
@@ -183,3 +195,3 @@ * @param {@type import('@redocly/openapi-core').Config} redocly | ||
} else { | ||
const outFile = new URL(flags.output, CWD); | ||
const outFile = normalizeOutput(flags.output); | ||
fs.mkdirSync(new URL(".", outFile), { recursive: true }); | ||
@@ -207,3 +219,3 @@ fs.writeFileSync(outFile, result, "utf8"); | ||
} else { | ||
const outFile = new URL(flags.output, CWD); | ||
const outFile = normalizeOutput(flags.output); | ||
fs.mkdirSync(new URL(".", outFile), { recursive: true }); | ||
@@ -210,0 +222,0 @@ fs.writeFileSync(outFile, result, "utf8"); |
# openapi-typescript | ||
## 7.0.3 | ||
### Patch Changes | ||
- [#1761](https://github.com/openapi-ts/openapi-typescript/pull/1761) [`8043c5d`](https://github.com/openapi-ts/openapi-typescript/commit/8043c5d27d3abf9df1a3e9cf031531a52921a122) Thanks [@yoshi2no](https://github.com/yoshi2no)! - fix: Enum option cannot generate enums if values are not valid property names | ||
## 7.0.2 | ||
@@ -4,0 +10,0 @@ |
@@ -8,16 +8,19 @@ /// <reference types="node" /> | ||
export * from "./lib/utils.js"; | ||
export * from "./transform/index.js"; | ||
export * from "./transform/components-object.js"; | ||
export * from "./transform/header-object.js"; | ||
export * from "./transform/media-type-object.js"; | ||
export { default as transformSchema } from "./transform/index.js"; | ||
export { default as transformComponentsObject } from "./transform/components-object.js"; | ||
export { default as transformHeaderObject } from "./transform/header-object.js"; | ||
export { default as transformMediaTypeObject } from "./transform/media-type-object.js"; | ||
export * from "./transform/operation-object.js"; | ||
export * from "./transform/parameter-object.js"; | ||
export { default as transformOperationObject } from "./transform/operation-object.js"; | ||
export { default as transformParameterObject } from "./transform/parameter-object.js"; | ||
export * from "./transform/path-item-object.js"; | ||
export * from "./transform/paths-object.js"; | ||
export * from "./transform/request-body-object.js"; | ||
export * from "./transform/response-object.js"; | ||
export * from "./transform/responses-object.js"; | ||
export { default as transformPathItemObject } from "./transform/path-item-object.js"; | ||
export { default as transformPathsObject } from "./transform/paths-object.js"; | ||
export { default as transformRequestBodyObject } from "./transform/request-body-object.js"; | ||
export { default as transformResponseObject } from "./transform/response-object.js"; | ||
export { default as transformResponsesObject } from "./transform/responses-object.js"; | ||
export * from "./transform/schema-object.js"; | ||
export { default as transformSchemaObject } from "./transform/schema-object.js"; | ||
export * from "./types.js"; | ||
export declare const COMMENT_HEADER = "/**\n * This file was auto-generated by openapi-typescript.\n * Do not make direct changes to the file.\n */\n\n"; | ||
export default function openapiTS(source: string | URL | OpenAPI3 | Buffer | Readable, options?: OpenAPITSOptions): Promise<ts.Node[]>; |
@@ -7,14 +7,17 @@ import { createConfig } from "@redocly/openapi-core"; | ||
export * from "./lib/utils.js"; | ||
export * from "./transform/index.js"; | ||
export * from "./transform/components-object.js"; | ||
export * from "./transform/header-object.js"; | ||
export * from "./transform/media-type-object.js"; | ||
export { default as transformSchema } from "./transform/index.js"; | ||
export { default as transformComponentsObject } from "./transform/components-object.js"; | ||
export { default as transformHeaderObject } from "./transform/header-object.js"; | ||
export { default as transformMediaTypeObject } from "./transform/media-type-object.js"; | ||
export * from "./transform/operation-object.js"; | ||
export * from "./transform/parameter-object.js"; | ||
export { default as transformOperationObject } from "./transform/operation-object.js"; | ||
export { default as transformParameterObject } from "./transform/parameter-object.js"; | ||
export * from "./transform/path-item-object.js"; | ||
export * from "./transform/paths-object.js"; | ||
export * from "./transform/request-body-object.js"; | ||
export * from "./transform/response-object.js"; | ||
export * from "./transform/responses-object.js"; | ||
export { default as transformPathItemObject } from "./transform/path-item-object.js"; | ||
export { default as transformPathsObject } from "./transform/paths-object.js"; | ||
export { default as transformRequestBodyObject } from "./transform/request-body-object.js"; | ||
export { default as transformResponseObject } from "./transform/response-object.js"; | ||
export { default as transformResponsesObject } from "./transform/responses-object.js"; | ||
export * from "./transform/schema-object.js"; | ||
export { default as transformSchemaObject } from "./transform/schema-object.js"; | ||
export * from "./types.js"; | ||
@@ -21,0 +24,0 @@ export const COMMENT_HEADER = `/** |
@@ -163,3 +163,11 @@ import { parseRef } from "@redocly/openapi-core/lib/ref-utils.js"; | ||
} | ||
name = name.replace(JS_PROPERTY_INDEX_INVALID_CHARS_RE, "_"); | ||
const invalidCharMatch = name.match(JS_PROPERTY_INDEX_INVALID_CHARS_RE); | ||
if (invalidCharMatch) { | ||
if (invalidCharMatch[0] === name) { | ||
name = `"${name}"`; | ||
} | ||
else { | ||
name = name.replace(JS_PROPERTY_INDEX_INVALID_CHARS_RE, "_"); | ||
} | ||
} | ||
} | ||
@@ -166,0 +174,0 @@ let member; |
{ | ||
"name": "openapi-typescript", | ||
"description": "Convert OpenAPI 3.0 & 3.1 schemas to TypeScript", | ||
"version": "7.0.2", | ||
"version": "7.0.3", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Drew Powers", |
<img src="../../docs/public/assets/openapi-ts.svg" alt="openapi-typescript" width="200" height="40" /> | ||
openapi-typescript generates TypeScript types from static <a href="https://spec.openapis.org/oas/latest.html" target="_blank" rel="noopener noreferrer">OpenAPI</a> schemas quickly using only Node.js. It is fast, lightweight, (almost) dependency-free, and no Java/node-gyp/running OpenAPI servers necessary. | ||
openapi-typescript turns [OpenAPI 3.0 & 3.1](https://spec.openapis.org/oas/latest.html) schemas into TypeScript quickly using Node.js. No Java/node-gyp/running OpenAPI servers necessary. | ||
The code is [MIT-licensed](./LICENSE) and free for use. | ||
> **Tip:** | ||
> New to OpenAPI? Speakeasyβs [Intro to OpenAPI](https://www.speakeasyapi.dev/openapi) is an accessible guide to newcomers that explains the βwhyβ and βhowβ of OpenAPI. | ||
## Features | ||
- β Supports OpenAPI 3.0 and 3.1 (including advanced features like <a href="https://spec.openapis.org/oas/v3.1.0#discriminator-object" target="_blank" rel="noopener noreferrer">discriminators</a>) | ||
- β Generate **runtime-free types** that outperform old-school codegen | ||
- β Supports OpenAPI 3.0 and 3.1 (including advanced features like [discriminators](https://spec.openapis.org/oas/v3.1.0#discriminator-object)) | ||
- β Generate **runtime-free types** that outperform old school codegen | ||
- β Load schemas from YAML or JSON, locally or remotely | ||
- β Native Node.js code is fast and generates types within milliseconds | ||
- β Generate types for even huge schemas within milliseconds | ||
_Note: OpenAPI 2.x is supported with versions `5.x` and previous_ | ||
## Examples | ||
@@ -20,3 +25,3 @@ | ||
This library requires the latest version of <a href="https://nodejs.org/en" target="_blank" rel="noopener noreferrer">Node.js</a> installed (20.x or higher recommended). With that present, run the following in your project: | ||
This library requires the latest version of [Node.js](https://nodejs.org) installed (20.x or higher recommended). With that present, run the following in your project: | ||
@@ -27,9 +32,27 @@ ```bash | ||
> β¨ **Tip** | ||
> | ||
> Enabling [noUncheckedIndexedAccess](https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAccess) in `tsconfig.json` can go along way to improve type safety ([read more](../../docs/src/content/docs/advanced.md#enable-nouncheckedindexedaccess-in-your-tsconfigjson)) | ||
And in your `tsconfig.json`, to load the types properly: | ||
```diff | ||
{ | ||
"compilerOptions": { | ||
+ "module": "ESNext", // or "NodeNext" | ||
+ "moduleResolution": "Bundler" // or "NodeNext" | ||
} | ||
} | ||
``` | ||
> **Highly recommended** | ||
> | ||
> Also adding the following can boost type safety: | ||
> ```diff | ||
> { | ||
> "compilerOptions": { | ||
> + "noUncheckedIndexedAccess": true | ||
> } | ||
> } | ||
> ``` | ||
## Basic usage | ||
First, generate a local type file by running `npx openapi-typescript`: | ||
First, generate a local type file by running `npx openapi-typescript`, first specifying your input schema (JSON or YAML), and where youβd like the `--output` (`-o`) to be saved: | ||
@@ -40,5 +63,3 @@ ```bash | ||
# π ./path/to/my/schema.yaml -> ./path/to/my/schema.d.ts [7ms] | ||
``` | ||
```bash | ||
# Remote schema | ||
@@ -49,6 +70,4 @@ npx openapi-typescript https://myapi.dev/api/v1/openapi.yaml -o ./path/to/my/schema.d.ts | ||
> β οΈ Be sure to <a href="https://redocly.com/docs/cli/commands/lint/" target="_blank" rel="noopener noreferrer">validate your schemas</a>! openapi-typescript will err on invalid schemas. | ||
Then in your TypeScript project, import types where needed: | ||
Then, import schemas from the generated file like so: | ||
```ts | ||
@@ -70,26 +89,12 @@ import type { paths, components } from "./my-openapi-3-schema"; // generated by openapi-typescript | ||
#### π¦ Globbing local schemas | ||
From here, you can use these types for any of the following (but not limited to): | ||
```bash | ||
npx openapi-typescript "specs/**/*.yaml" --output schemas/ | ||
- Using an OpenAPI-supported fetch client (like [openapi-fetch](https://openapi-ts.dev/openapi-fetch/)) | ||
- Asserting types for other API requestBodies and responses | ||
- Building core business logic based on API types | ||
- Validating mock test data is up-to-date with the current schema | ||
- Packaging API types into any npm packages you publish (such as client SDKs, etc.) | ||
# π specs/one.yaml -> schemas/specs/one.ts [7ms] | ||
# π specs/two.yaml -> schemas/specs/two.ts [7ms] | ||
# π specs/three.yaml -> schemas/specs/three.ts [7ms] | ||
``` | ||
_Thanks, [@sharmarajdaksh](https://github.com/sharmarajdaksh)!_ | ||
#### βοΈ Remote schemas | ||
```bash | ||
npx openapi-typescript https://petstore3.swagger.io/api/v3/openapi.yaml --output petstore.d.ts | ||
# π https://petstore3.swagger.io/api/v3/openapi.yaml -> petstore.d.ts [250ms] | ||
``` | ||
_Thanks, [@psmyrdek](https://github.com/psmyrdek)!_ | ||
## π Docs | ||
[View Docs](https://openapi-ts.dev/) |
@@ -11,14 +11,17 @@ import { createConfig } from "@redocly/openapi-core"; | ||
export * from "./lib/utils.js"; | ||
export * from "./transform/index.js"; | ||
export * from "./transform/components-object.js"; | ||
export * from "./transform/header-object.js"; | ||
export * from "./transform/media-type-object.js"; | ||
export { default as transformSchema } from "./transform/index.js"; | ||
export { default as transformComponentsObject } from "./transform/components-object.js"; | ||
export { default as transformHeaderObject } from "./transform/header-object.js"; | ||
export { default as transformMediaTypeObject } from "./transform/media-type-object.js"; | ||
export * from "./transform/operation-object.js"; | ||
export * from "./transform/parameter-object.js"; | ||
export { default as transformOperationObject } from "./transform/operation-object.js"; | ||
export { default as transformParameterObject } from "./transform/parameter-object.js"; | ||
export * from "./transform/path-item-object.js"; | ||
export * from "./transform/paths-object.js"; | ||
export * from "./transform/request-body-object.js"; | ||
export * from "./transform/response-object.js"; | ||
export * from "./transform/responses-object.js"; | ||
export { default as transformPathItemObject } from "./transform/path-item-object.js"; | ||
export { default as transformPathsObject } from "./transform/paths-object.js"; | ||
export { default as transformRequestBodyObject } from "./transform/request-body-object.js"; | ||
export { default as transformResponseObject } from "./transform/response-object.js"; | ||
export { default as transformResponsesObject } from "./transform/responses-object.js"; | ||
export * from "./transform/schema-object.js"; | ||
export { default as transformSchemaObject } from "./transform/schema-object.js"; | ||
export * from "./types.js"; | ||
@@ -25,0 +28,0 @@ |
@@ -288,3 +288,11 @@ import { parseRef } from "@redocly/openapi-core/lib/ref-utils.js"; | ||
} | ||
name = name.replace(JS_PROPERTY_INDEX_INVALID_CHARS_RE, "_"); | ||
const invalidCharMatch = name.match(JS_PROPERTY_INDEX_INVALID_CHARS_RE); | ||
if (invalidCharMatch) { | ||
if (invalidCharMatch[0] === name) { | ||
name = `"${name}"`; | ||
} else { | ||
name = name.replace(JS_PROPERTY_INDEX_INVALID_CHARS_RE, "_"); | ||
} | ||
} | ||
} | ||
@@ -291,0 +299,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
536060
10121
96