openapi-json-schema
Advanced tools
Comparing version 1.1.3 to 2.0.0
@@ -1,3 +0,3 @@ | ||
export * from './lib'; | ||
export * from './lib/types'; | ||
export * from './lib/utils'; | ||
export * from './lib/index.js'; | ||
export * from './lib/types.js'; | ||
export * from './lib/utils.js'; |
@@ -1,15 +0,3 @@ | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./lib"), exports); | ||
__exportStar(require("./lib/types"), exports); | ||
__exportStar(require("./lib/utils"), exports); | ||
export * from './lib/index.js'; | ||
export * from './lib/types.js'; | ||
export * from './lib/utils.js'; |
import type { JSONSchema7 } from 'json-schema'; | ||
import type { OpenApiSchemaVersion, PartialOpenApiSchema } from './types'; | ||
import type { OpenApiSchemaVersion, PartialOpenApiSchema } from './types.js'; | ||
export interface JsonSchemaDocumentToOpenApiOptions { | ||
@@ -15,4 +15,4 @@ title: string; | ||
termsOfService?: string | undefined; | ||
contact?: import("./types").OpenApiSchemaContact | undefined; | ||
license?: import("./types").OpenApiLicenseContact | undefined; | ||
contact?: import("./types.js").OpenApiSchemaContact | undefined; | ||
license?: import("./types.js").OpenApiLicenseContact | undefined; | ||
'x-id'?: string | undefined; | ||
@@ -22,4 +22,4 @@ 'x-comment'?: string | undefined; | ||
paths: Record<string, unknown>; | ||
servers?: import("./types").OpenApiSchemaServer[] | undefined; | ||
components?: import("./types").OpenApiSchemaComponents | undefined; | ||
servers?: import("./types.js").OpenApiSchemaServer[] | undefined; | ||
components?: import("./types.js").OpenApiSchemaComponents | undefined; | ||
}; | ||
@@ -26,0 +26,0 @@ export declare function jsonSchemaDocumentToOpenApi(schema: JSONSchema7, options: JsonSchemaDocumentToOpenApiOptions): PartialOpenApiSchema; |
@@ -1,7 +0,4 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.openApiToJsonSchema = exports.jsonSchemaDocumentToOpenApi = exports.decorateOpenApi = void 0; | ||
const json_schema_to_openapi_1 = require("./json-schema-to-openapi"); | ||
const openapi_to_json_schema_1 = require("./openapi-to-json-schema"); | ||
function decorateOpenApi(schema, { title, version, schemaVersion = '3.0.0', }) { | ||
import { jsonSchemaTypeToOpenApi } from './json-schema-to-openapi.js'; | ||
import { openApiToJsonSchemaType } from './openapi-to-json-schema.js'; | ||
export function decorateOpenApi(schema, { title, version, schemaVersion = '3.0.0', }) { | ||
const info = { title, version }; | ||
@@ -24,4 +21,3 @@ if (schema.$id) { | ||
} | ||
exports.decorateOpenApi = decorateOpenApi; | ||
function jsonSchemaDocumentToOpenApi(schema, options) { | ||
export function jsonSchemaDocumentToOpenApi(schema, options) { | ||
const { definitions = {}, ...rest } = schema; | ||
@@ -31,8 +27,7 @@ return decorateOpenApi({ | ||
components: { | ||
schemas: Object.fromEntries(Object.entries(definitions).map(([name, schema]) => [name, (0, json_schema_to_openapi_1.jsonSchemaTypeToOpenApi)(schema)])), | ||
schemas: Object.fromEntries(Object.entries(definitions).map(([name, schema]) => [name, jsonSchemaTypeToOpenApi(schema)])), | ||
}, | ||
}, options); | ||
} | ||
exports.jsonSchemaDocumentToOpenApi = jsonSchemaDocumentToOpenApi; | ||
function openApiToJsonSchema(openApi) { | ||
export function openApiToJsonSchema(openApi) { | ||
const schemas = openApi.components.schemas; | ||
@@ -42,6 +37,5 @@ return { | ||
key, | ||
(0, openapi_to_json_schema_1.openApiToJsonSchemaType)(schemas[key]) | ||
openApiToJsonSchemaType(schemas[key]) | ||
])) | ||
}; | ||
} | ||
exports.openApiToJsonSchema = openApiToJsonSchema; |
@@ -1,3 +0,3 @@ | ||
import type { JSONSchema7Definition } from "json-schema"; | ||
import type { OpenApiSchemaTypeDefinition } from "./types"; | ||
import type { JSONSchema7Definition } from 'json-schema'; | ||
import type { OpenApiSchemaTypeDefinition } from './types.js'; | ||
export declare function jsonSchemaTypeToOpenApi(schema: JSONSchema7Definition): OpenApiSchemaTypeDefinition; |
@@ -1,5 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.jsonSchemaTypeToOpenApi = void 0; | ||
const utils_1 = require("./utils"); | ||
import { decodeRefNameJsonSchema, encodeRefNameOpenApi, recurseSchema, } from './utils.js'; | ||
function jsonSchemaTypeToOpenApiConvertType(schema) { | ||
@@ -41,7 +38,7 @@ if (schema.type === undefined) | ||
...node, | ||
$ref: (0, utils_1.encodeRefNameOpenApi)((0, utils_1.decodeRefNameJsonSchema)(node.$ref)), | ||
$ref: encodeRefNameOpenApi(decodeRefNameJsonSchema(node.$ref)), | ||
}; | ||
return node; | ||
} | ||
function jsonSchemaTypeToOpenApi(schema) { | ||
export function jsonSchemaTypeToOpenApi(schema) { | ||
if (typeof schema === 'boolean') | ||
@@ -52,4 +49,3 @@ return schema; | ||
// TODO: Proper draft 7-to-4 conversion | ||
return (0, utils_1.recurseSchema)(schema, jsonSchemaTypeToOpenApi); | ||
return recurseSchema(schema, jsonSchemaTypeToOpenApi); | ||
} | ||
exports.jsonSchemaTypeToOpenApi = jsonSchemaTypeToOpenApi; |
@@ -1,3 +0,3 @@ | ||
import type { JSONSchema7Definition } from "json-schema"; | ||
import type { OpenApiSchemaTypeDefinition } from "./types"; | ||
import type { JSONSchema7Definition } from 'json-schema'; | ||
import type { OpenApiSchemaTypeDefinition } from './types.js'; | ||
export declare function openApiToJsonSchemaType(schema: OpenApiSchemaTypeDefinition): JSONSchema7Definition; |
@@ -1,5 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.openApiToJsonSchemaType = void 0; | ||
const utils_1 = require("./utils"); | ||
import { decodeRefNameOpenApi, encodeRefNameJsonSchema, recurseSchema, } from './utils.js'; | ||
function openApiTypeToJsonSchema7Type(type, nullable) { | ||
@@ -26,7 +23,7 @@ if (typeof type === "undefined" || type === "any") | ||
...node, | ||
$ref: (0, utils_1.encodeRefNameJsonSchema)((0, utils_1.decodeRefNameOpenApi)(node.$ref)), | ||
$ref: encodeRefNameJsonSchema(decodeRefNameOpenApi(node.$ref)), | ||
}; | ||
return node; | ||
} | ||
function openApiToJsonSchemaType(schema) { | ||
export function openApiToJsonSchemaType(schema) { | ||
if (typeof schema === 'boolean') | ||
@@ -38,4 +35,3 @@ return schema; | ||
output = openApiToJsonSchema7Ref(output); | ||
return (0, utils_1.recurseSchema)(output, openApiToJsonSchemaType); | ||
return recurseSchema(output, openApiToJsonSchemaType); | ||
} | ||
exports.openApiToJsonSchemaType = openApiToJsonSchemaType; |
@@ -40,7 +40,7 @@ import type { JSONSchema4, JSONSchema4TypeName } from 'json-schema'; | ||
} | ||
export declare type OpenAPITypeName = JSONSchema4TypeName; | ||
export declare type OpenAPISchemaType = Omit<JSONSchema4, 'type'> & { | ||
export type OpenAPITypeName = JSONSchema4TypeName; | ||
export type OpenAPISchemaType = Omit<JSONSchema4, 'type'> & { | ||
type?: OpenAPITypeName; | ||
}; | ||
export declare type OpenApiSchemaTypeDefinition = OpenAPISchemaType | boolean; | ||
export type OpenApiSchemaTypeDefinition = OpenAPISchemaType | boolean; | ||
export interface OpenApiSchemaComponents { | ||
@@ -57,2 +57,2 @@ schemas?: Record<string, OpenApiSchemaTypeDefinition>; | ||
} | ||
export declare type OpenApiSchemaVersion = '3.0.3' | '3.0.2' | '3.0.1' | '3.0.0' | '3.0.0-rc2' | '3.0.0-rc1' | '3.0.0-rc0' | '2.0' | '2.0' | '1.2' | '1.1' | '1.0'; | ||
export type OpenApiSchemaVersion = '3.0.3' | '3.0.2' | '3.0.1' | '3.0.0' | '3.0.0-rc2' | '3.0.0-rc1' | '3.0.0-rc0' | '2.0' | '2.0' | '1.2' | '1.1' | '1.0'; |
@@ -1,2 +0,1 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
export {}; |
import type { JSONSchema7Definition } from 'json-schema'; | ||
import type { OpenApiSchemaTypeDefinition } from './types'; | ||
import type { OpenApiSchemaTypeDefinition } from './types.js'; | ||
export declare function encodePathPart(part: string): string; | ||
@@ -9,3 +9,3 @@ export declare function decodePathPart(part: string): string; | ||
export declare function decodeRefNameOpenApi(name: string): string; | ||
export declare type AnySchema = JSONSchema7Definition | OpenApiSchemaTypeDefinition; | ||
export type AnySchema = JSONSchema7Definition | OpenApiSchemaTypeDefinition; | ||
export declare function recurseSchema<T extends AnySchema, U extends AnySchema>(t: T, convert: (t: T) => U): U; |
@@ -1,17 +0,11 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.recurseSchema = exports.decodeRefNameOpenApi = exports.encodeRefNameOpenApi = exports.decodeRefNameJsonSchema = exports.encodeRefNameJsonSchema = exports.decodePathPart = exports.encodePathPart = void 0; | ||
function encodePathPart(part) { | ||
export function encodePathPart(part) { | ||
return encodeURIComponent(part); | ||
} | ||
exports.encodePathPart = encodePathPart; | ||
function decodePathPart(part) { | ||
export function decodePathPart(part) { | ||
return decodeURIComponent(part); | ||
} | ||
exports.decodePathPart = decodePathPart; | ||
function encodeRefNameJsonSchema(name) { | ||
export function encodeRefNameJsonSchema(name) { | ||
return `#/definitions/${encodePathPart(name)}`; | ||
} | ||
exports.encodeRefNameJsonSchema = encodeRefNameJsonSchema; | ||
function decodeRefNameJsonSchema(name) { | ||
export function decodeRefNameJsonSchema(name) { | ||
if (name.startsWith("#/definitions/")) | ||
@@ -21,8 +15,6 @@ return decodePathPart(name.slice(14)); | ||
} | ||
exports.decodeRefNameJsonSchema = decodeRefNameJsonSchema; | ||
function encodeRefNameOpenApi(name) { | ||
export function encodeRefNameOpenApi(name) { | ||
return `#/components/schemas/${encodePathPart(name)}`; | ||
} | ||
exports.encodeRefNameOpenApi = encodeRefNameOpenApi; | ||
function decodeRefNameOpenApi(name) { | ||
export function decodeRefNameOpenApi(name) { | ||
if (name.startsWith("#/components/schemas/")) | ||
@@ -32,7 +24,6 @@ return decodePathPart(name.slice(21)); | ||
} | ||
exports.decodeRefNameOpenApi = decodeRefNameOpenApi; | ||
function hasProperties(t) { | ||
return t && Object.keys(t).length > 0; | ||
} | ||
function recurseSchema(t, convert) { | ||
export function recurseSchema(t, convert) { | ||
if (typeof t !== 'object') | ||
@@ -54,3 +45,3 @@ return t; | ||
properties: Object.fromEntries(Object.keys(schema.properties) | ||
.map(key => { var _a; return [key, convert((_a = schema.properties) === null || _a === void 0 ? void 0 : _a[key])]; })), | ||
.map(key => [key, convert(schema.properties?.[key])])), | ||
}), | ||
@@ -60,3 +51,3 @@ ...(!hasProperties(schema.patternProperties) ? {} : | ||
patternProperties: Object.fromEntries(Object.keys(schema.patternProperties) | ||
.map(key => { var _a; return [key, convert((_a = schema.patternProperties) === null || _a === void 0 ? void 0 : _a[key])]; })), | ||
.map(key => [key, convert(schema.patternProperties?.[key])])), | ||
}), | ||
@@ -68,3 +59,3 @@ ...(typeof schema.additionalProperties !== 'object' ? {} | ||
dependencies: Object.fromEntries(Object.keys(schema.dependencies) | ||
.map(key => { var _a; return [key, convert((_a = schema.dependencies) === null || _a === void 0 ? void 0 : _a[key])]; })), | ||
.map(key => [key, convert(schema.dependencies?.[key])])), | ||
}), | ||
@@ -90,6 +81,5 @@ ...(typeof schema.propertyNames !== 'object' ? {} | ||
definitions: Object.fromEntries(Object.keys(schema.definitions) | ||
.map(key => { var _a; return [key, convert((_a = schema.definitions) === null || _a === void 0 ? void 0 : _a[key])]; })), | ||
.map(key => [key, convert(schema.definitions?.[key])])), | ||
}), | ||
}; | ||
} | ||
exports.recurseSchema = recurseSchema; |
{ | ||
"name": "openapi-json-schema", | ||
"version": "1.1.3", | ||
"version": "2.0.0", | ||
"description": "Minimalistic OpenAPI 3 ⬌ JSON Schema (draft 7) conversion", | ||
@@ -14,4 +14,6 @@ "author": "Gustaf Räntilä", | ||
"directories": {}, | ||
"type": "module", | ||
"sideEffects": false, | ||
"engines": { | ||
"node": ">=12" | ||
"node": ">=14.13.1 || >= 16" | ||
}, | ||
@@ -23,3 +25,3 @@ "files": [ | ||
"build": "rimraf dist && tsc -p tsconfig.prod.json", | ||
"test": "jest", | ||
"test": "NODE_OPTIONS=--experimental-vm-modules jest", | ||
"cz": "git-cz" | ||
@@ -39,2 +41,4 @@ }, | ||
"devDependencies": { | ||
"@babel/preset-env": "^7.20.2", | ||
"@babel/preset-typescript": "^7.18.6", | ||
"@types/jest": "^27.0.1", | ||
@@ -47,3 +51,3 @@ "@types/json-schema": "^7.0.9", | ||
"rimraf": "^3.0.2", | ||
"ts-jest": "^27.0.5", | ||
"ts-jest-resolver": "^2.0.0", | ||
"typescript": "^4.4.3" | ||
@@ -55,3 +59,4 @@ }, | ||
} | ||
} | ||
}, | ||
"packageManager": "yarn@3.2.4" | ||
} |
@@ -5,3 +5,2 @@ [![npm version][npm-image]][npm-url] | ||
[![coverage status][coverage-image]][coverage-url] | ||
[![Language grade: JavaScript][lgtm-image]][lgtm-url] | ||
@@ -23,2 +22,7 @@ | ||
## Versions | ||
* Since v2 this is a [pure ESM][pure-esm] package, and requires Node.js >=14.13.1. It cannot be used from CommonJS. | ||
# Usage | ||
@@ -48,7 +52,6 @@ | ||
[downloads-image]: https://img.shields.io/npm/dm/openapi-json-schema.svg | ||
[build-image]: https://img.shields.io/github/workflow/status/grantila/openapi-json-schema/Master.svg | ||
[build-image]: https://img.shields.io/github/actions/workflow/status/grantila/openapi-json-schema/master.yml?branch=master | ||
[build-url]: https://github.com/grantila/openapi-json-schema/actions?query=workflow%3AMaster | ||
[coverage-image]: https://coveralls.io/repos/github/grantila/openapi-json-schema/badge.svg?branch=master | ||
[coverage-url]: https://coveralls.io/github/grantila/openapi-json-schema?branch=master | ||
[lgtm-image]: https://img.shields.io/lgtm/grade/javascript/g/grantila/openapi-json-schema.svg?logo=lgtm&logoWidth=18 | ||
[lgtm-url]: https://lgtm.com/projects/g/grantila/openapi-json-schema/context:javascript | ||
[pure-esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c |
55
Yes
16855
11
309