New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

openapi-typescript

Package Overview
Dependencies
Maintainers
1
Versions
144
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

openapi-typescript - npm Package Compare versions

Comparing version 7.0.0-next.5 to 7.0.0-next.6

4

CONTRIBUTING.md

@@ -43,3 +43,3 @@ # Contributing

Working with the TypeScript AST can be daunting. Luckly, there’s [astexplorer.net](https://astexplorer.net) which makes it much more accessible. Rather than trying to build an AST from scratch (which is near impossible), instead:
Working with the TypeScript AST can be daunting. Luckily, there’s [astexplorer.net](https://astexplorer.net) which makes it much more accessible. Rather than trying to build an AST from scratch (which is near impossible), instead:

@@ -70,3 +70,3 @@ 1. Switch to the **typescript** parser in the top menu

It may be surprising to hear, but generating TypeScript types from OpenAPI is opinionated. Even though TypeScript and OpenAPI are close relatives—both JavaScript/JSON-based—they are nonetheless 2 different languages and thus there is room for interpretation. Further, some parts of the OpenAPI specification can be ambiguous on how they’re used, and what the expected type outcomes may be (though this is generally for more advanced usecasees, such as specific implementations of `anyOf` as well as [discriminator](https://spec.openapis.org/oas/latest.html#discriminatorObject) and complex polymorphism).
It may be surprising to hear, but generating TypeScript types from OpenAPI is opinionated. Even though TypeScript and OpenAPI are close relatives—both JavaScript/JSON-based—they are nonetheless 2 different languages and thus there is room for interpretation. Further, some parts of the OpenAPI specification can be ambiguous on how they’re used, and what the expected type outcomes may be (though this is generally for more advanced use cases, such as specific implementations of `anyOf` as well as [discriminator](https://spec.openapis.org/oas/latest.html#discriminatorObject) and complex polymorphism).

@@ -73,0 +73,0 @@ All that said, this library should strive to generate _the most predictable_ TypeScript output for a given schema. And to achieve that, it always helps to open an [issue](https://github.com/drwpow/openapi-typescript/issues) or [discussion](https://github.com/drwpow/openapi-typescript/discussions) to gather feedback.

@@ -32,6 +32,4 @@ import { createConfig } from "@redocly/openapi-core";

(await createConfig({
styleguide: {
rules: {
"operation-operationId-unique": { severity: "error" },
},
rules: {
"operation-operationId-unique": { severity: "error" },
},

@@ -38,0 +36,0 @@ }, { extends: ["minimal"] }));

@@ -37,4 +37,8 @@ import ts from "typescript";

]) {
keyedParameters["$ref" in parameter ? parameter.$ref : parameter.name] =
parameter;
const name = "$ref" in parameter
? options.ctx.resolve(parameter.$ref)?.name
: parameter.name;
if (name) {
keyedParameters[name] = parameter;
}
}

@@ -41,0 +45,0 @@ }

@@ -40,4 +40,6 @@ import { parseRef } from "@redocly/openapi-core/lib/ref-utils.js";

const metadata = schemaObject.enum.map((_, i) => ({
name: schemaObject["x-enum-varnames"]?.[i],
description: schemaObject["x-enum-descriptions"]?.[i],
name: schemaObject["x-enum-varnames"]?.[i] ??
schemaObject["x-enumNames"]?.[i],
description: schemaObject["x-enum-descriptions"]?.[i] ??
schemaObject["x-enumDescriptions"]?.[i],
}));

@@ -151,3 +153,9 @@ const enumType = tsEnum(enumName, schemaObject.enum, metadata, { export: true, readonly: options.ctx.immutable });

else if (schemaObject.items) {
itemType = transformSchemaObject(schemaObject.items, options);
if ("type" in schemaObject.items &&
schemaObject.items.type === "array") {
itemType = ts.factory.createArrayTypeNode(transformSchemaObject(schemaObject.items, options));
}
else {
itemType = transformSchemaObject(schemaObject.items, options);
}
}

@@ -186,3 +194,3 @@ const min = typeof schemaObject.minItems === "number" && schemaObject.minItems >= 0

}
return ts.isTupleTypeNode(itemType)
return ts.isTupleTypeNode(itemType) || ts.isArrayTypeNode(itemType)
? itemType

@@ -189,0 +197,0 @@ : ts.factory.createArrayTypeNode(itemType);

{
"name": "openapi-typescript",
"description": "Convert OpenAPI 3.0 & 3.1 schemas to TypeScript",
"version": "7.0.0-next.5",
"version": "7.0.0-next.6",
"author": {

@@ -43,26 +43,27 @@ "name": "Drew Powers",

"scripts": {
"build": "run-s -s build:*",
"build": "pnpm run build:clean && pnpm run build:esm && pnpm run build:cjs",
"build:clean": "del dist",
"build:esm": "tsc -p tsconfig.build.json",
"build:cjs": "esbuild --bundle --platform=node --target=es2019 --outfile=dist/index.cjs --external:@redocly/ajv --external:@redocly/openapi-core --external:typescript src/index.ts --footer:js=\"module.exports = module.exports.default;\"",
"build:cjs": "esbuild --bundle --platform=node --target=es2019 --outfile=dist/index.cjs --external:@redocly/ajv --external:@redocly/openapi-core --external:typescript src/index.ts",
"dev": "tsc -p tsconfig.build.json --watch",
"download:schemas": "vite-node ./scripts/download-schemas.ts",
"format": "prettier --write \"src/**/*\"",
"lint": "run-p -s lint:*",
"lint": "pnpm run \"/^lint:/\"",
"lint:js": "eslint \"{src,test}/**/*.{js,ts}\"",
"lint:prettier": "prettier --check \"src/**/*\"",
"prepare": "pnpm run build",
"test": "run-p -s test:*",
"test": "pnpm run \"/^test:/\"",
"test:examples": "tsc -p tsconfig.examples.json --noEmit",
"test:js": "vitest run",
"test:ts": "tsc --noEmit",
"update:examples": "pnpm run download:schemas && vite-node ./scripts/update-examples.ts",
"update:examples": "pnpm run build && pnpm run download:schemas && vite-node ./scripts/update-examples.ts",
"prepublish": "pnpm run build",
"version": "pnpm run build"
},
"peerDependencies": {
"typescript": "^5.x"
},
"dependencies": {
"@redocly/openapi-core": "^1.4.1",
"@redocly/openapi-core": "^1.6.0",
"ansi-colors": "^4.1.3",
"supports-color": "^9.4.0",
"typescript": "^5.3.2",
"yargs-parser": "^21.1.1"

@@ -73,10 +74,11 @@ },

"@types/js-yaml": "^4.0.9",
"@types/node": "^20.9.4",
"@types/node": "^20.11.5",
"degit": "^2.8.4",
"del-cli": "^5.1.0",
"esbuild": "^0.19.7",
"esbuild": "^0.19.11",
"execa": "^7.2.0",
"vite-node": "^0.34.6",
"vitest": "^0.34.6"
"typescript": "^5.3.3",
"vite-node": "^1.2.1",
"vitest": "^1.2.1"
}
}

@@ -23,3 +23,3 @@ <img src="../../docs/public/assets/openapi-ts.svg" alt="openapi-typescript" width="200" height="40" />

```bash
npm i -D openapi-typescript
npm i -D openapi-typescript typescript
```

@@ -61,4 +61,6 @@

// Response obj
type SuccessResponse = paths["/my/endpoint"]["get"]["responses"][200]["content"]["application/json"]["schema"];
type ErrorResponse = paths["/my/endpoint"]["get"]["responses"][500]["content"]["application/json"]["schema"];
type SuccessResponse =
paths["/my/endpoint"]["get"]["responses"][200]["content"]["application/json"]["schema"];
type ErrorResponse =
paths["/my/endpoint"]["get"]["responses"][500]["content"]["application/json"]["schema"];
```

@@ -65,0 +67,0 @@

@@ -54,7 +54,4 @@ import { createConfig } from "@redocly/openapi-core";

{
// @ts-expect-error This is OK
styleguide: {
rules: {
"operation-operationId-unique": { severity: "error" }, // throw error on duplicate operationIDs
},
rules: {
"operation-operationId-unique": { severity: "error" }, // throw error on duplicate operationIDs
},

@@ -61,0 +58,0 @@ },

@@ -91,5 +91,9 @@ import ts from "typescript";

]) {
// note: the actual key doesn’t matter here, as long as it can match between PathItem and OperationObject
keyedParameters["$ref" in parameter ? parameter.$ref : parameter.name] =
parameter;
const name =
"$ref" in parameter
? options.ctx.resolve<ParameterObject>(parameter.$ref)?.name
: parameter.name;
if (name) {
keyedParameters[name] = parameter;
}
}

@@ -96,0 +100,0 @@ }

@@ -118,4 +118,8 @@ import { parseRef } from "@redocly/openapi-core/lib/ref-utils.js";

const metadata = schemaObject.enum.map((_, i) => ({
name: schemaObject["x-enum-varnames"]?.[i],
description: schemaObject["x-enum-descriptions"]?.[i],
name:
schemaObject["x-enum-varnames"]?.[i] ??
schemaObject["x-enumNames"]?.[i],
description:
schemaObject["x-enum-descriptions"]?.[i] ??
schemaObject["x-enumDescriptions"]?.[i],
}));

@@ -302,3 +306,12 @@ const enumType = tsEnum(

else if (schemaObject.items) {
itemType = transformSchemaObject(schemaObject.items, options);
if (
"type" in schemaObject.items &&
schemaObject.items.type === "array"
) {
itemType = ts.factory.createArrayTypeNode(
transformSchemaObject(schemaObject.items, options),
);
} else {
itemType = transformSchemaObject(schemaObject.items, options);
}
}

@@ -351,5 +364,5 @@

return ts.isTupleTypeNode(itemType)
return ts.isTupleTypeNode(itemType) || ts.isArrayTypeNode(itemType)
? itemType
: ts.factory.createArrayTypeNode(itemType); // wrap itemType in array type, but only if not a tuple already
: ts.factory.createArrayTypeNode(itemType); // wrap itemType in array type, but only if not a tuple or array already
}

@@ -356,0 +369,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

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc