openapi-typescript
Advanced tools
Comparing version 3.0.0-alpha.0 to 3.0.0-alpha.1
@@ -153,3 +153,3 @@ 'use strict'; | ||
let output = ""; | ||
let required = (options === null || options === void 0 ? void 0 : options.required) || []; | ||
let required = options && options.required || []; | ||
Object.entries(obj).forEach(([key, value]) => { | ||
@@ -261,11 +261,22 @@ if (value.description) output += comment(value.description); | ||
function transformParametersArray(parameters, globalParameters) { | ||
function transformHeaderObjMap(headerMap) { | ||
let output = ""; | ||
Object.entries(headerMap).forEach(([k, v]) => { | ||
if (!v.schema) return; | ||
if (v.description) output += comment(v.description); | ||
const required = v.required ? "" : "?"; | ||
output += ` "${k}"${required}: ${transformSchemaObj(v.schema)}\n`; | ||
}); | ||
return output; | ||
} | ||
function transformParametersArray(parameters, globalParams) { | ||
let output = ""; | ||
let mappedParams = {}; | ||
parameters.forEach(paramObj => { | ||
if (paramObj.$ref && globalParameters) { | ||
if (paramObj.$ref && globalParams) { | ||
const paramName = paramObj.$ref.split("/").pop(); | ||
if (globalParameters[paramName]) { | ||
const reference = globalParameters[paramName]; | ||
if (globalParams[paramName]) { | ||
const reference = globalParams[paramName]; | ||
if (!mappedParams[reference.in]) mappedParams[reference.in] = {}; | ||
@@ -289,3 +300,2 @@ mappedParams[reference.in][reference.name || paramName] = _objectSpread2(_objectSpread2({}, reference), {}, { | ||
Object.entries(paramGroup).forEach(([paramName, paramObj]) => { | ||
if (!paramObj.schema) return; | ||
let paramComment = ""; | ||
@@ -296,3 +306,3 @@ if (paramObj.deprecated) paramComment += `@deprecated `; | ||
const required = paramObj.required ? `` : `?`; | ||
output += ` "${paramName}"${required}: ${transformSchemaObj(paramObj.schema)};\n`; | ||
output += ` "${paramName}"${required}: ${paramObj.schema ? transformSchemaObj(paramObj.schema) : "unknown"};\n`; | ||
}); | ||
@@ -304,34 +314,36 @@ output += ` }\n`; | ||
const resType = res => res === 204 || res >= 300 && res < 400 ? "never" : "unknown"; | ||
function transformResponsesObj(responsesObj) { | ||
let output = ""; | ||
Object.entries(responsesObj).forEach(([k, v]) => { | ||
if (v.description) output += comment(v.description); | ||
const resKey = parseInt(k, 10) ? k : `"${k}"`; | ||
Object.entries(responsesObj).forEach(([httpStatusCode, response]) => { | ||
if (response.description) output += comment(response.description); | ||
const statusCode = Number(httpStatusCode) || `"${httpStatusCode}"`; | ||
if (v.$ref) { | ||
output += ` ${resKey}: ${transformRef(v.$ref)};\n`; | ||
if (response.$ref) { | ||
output += ` ${statusCode}: ${transformRef(response.$ref)};\n`; | ||
return; | ||
} | ||
if (!v.content && !v.schema || v.content && !Object.keys(v.content).length) { | ||
output += ` ${resKey}: unknown;\n`; | ||
if (!response.content && !response.schema || response.content && !Object.keys(response.content).length) { | ||
output += ` ${statusCode}: ${resType(statusCode)};\n`; | ||
return; | ||
} | ||
output += ` ${resKey}: {\n`; | ||
output += ` ${statusCode}: {\n`; | ||
if (v.headers) { | ||
if (v.headers.$ref) { | ||
output += ` headers: ${transformRef(v.headers.$ref)};\n`; | ||
if (response.headers) { | ||
if (response.headers.$ref) { | ||
output += ` headers: ${transformRef(response.headers.$ref)};\n`; | ||
} else { | ||
output += ` headers: {\n ${transformSchemaObjMap(v.headers)}\n }\n`; | ||
output += ` headers: {\n ${transformHeaderObjMap(response.headers)}\n }\n`; | ||
} | ||
} | ||
if (v.content) { | ||
Object.entries(v.content).forEach(([contentType, contentResponse]) => { | ||
output += ` "${contentType}": ${transformSchemaObj(contentResponse.schema)};\n`; | ||
if (response.content) { | ||
Object.entries(response.content).forEach(([contentType, contentResponse]) => { | ||
output += ` "${contentType}": ${transformSchemaObj(contentResponse.schema)};\n`; | ||
}); | ||
} else if (v.schema) { | ||
output += ` schema: ${transformSchemaObj(v.schema)};\n`; | ||
} else if (response.schema) { | ||
output += ` schema: ${transformSchemaObj(response.schema)};\n`; | ||
} | ||
@@ -372,2 +384,39 @@ | ||
function transformPathsObj(paths, { | ||
operations, | ||
parameters | ||
}) { | ||
let output = ""; | ||
Object.entries(paths).forEach(([url, pathItem]) => { | ||
if (pathItem.description) output += comment(pathItem.description); | ||
if (pathItem.$ref) { | ||
output += ` "${url}": ${transformRef(pathItem.$ref)};\n`; | ||
return; | ||
} | ||
output += ` "${url}": {\n`; | ||
["get", "put", "post", "delete", "options", "head", "patch", "trace"].forEach(method => { | ||
const operation = pathItem[method]; | ||
if (!operation) return; | ||
if (operation.description) output += comment(operation.description); | ||
if (operation.operationId) { | ||
output += ` "${method}": operations["${operation.operationId}"];\n`; | ||
operations[operation.operationId] = operation; | ||
return; | ||
} | ||
output += ` "${method}": {\n ${transformOperationObj(operation, parameters)}\n }\n`; | ||
}); | ||
if (pathItem.parameters) { | ||
output += ` parameters: {\n ${transformParametersArray(pathItem.parameters, parameters)}\n }\n`; | ||
} | ||
output += ` }\n`; | ||
}); | ||
return output; | ||
} | ||
function transformAll(schema, { | ||
@@ -379,26 +428,27 @@ version, | ||
let operations = {}; | ||
output += `export interface paths {\n`; | ||
if (!rawSchema && schema.paths) { | ||
Object.entries(schema.paths).forEach(([url, pathItem]) => { | ||
if (pathItem.description) output += comment(pathItem.description); | ||
if (rawSchema) { | ||
switch (version) { | ||
case 2: | ||
{ | ||
return `export interface definitions {\n ${transformSchemaObjMap(schema, { | ||
required: Object.keys(schema) | ||
})}\n}`; | ||
} | ||
if (pathItem.$ref) { | ||
output += ` "${url}": ${transformRef(pathItem.$ref)};\n`; | ||
return; | ||
} | ||
case 3: | ||
{ | ||
return `export interface schemas {\n ${transformSchemaObjMap(schema, { | ||
required: Object.keys(schema) | ||
})}\n }\n\n`; | ||
} | ||
} | ||
} | ||
output += ` "${url}": {\n`; | ||
Object.entries(pathItem).forEach(([method, operation]) => { | ||
if (operation.description) output += comment(operation.description); | ||
output += `export interface paths {\n`; | ||
if (operation.operationId) { | ||
output += ` "${method}": operations["${operation.operationId}"];\n`; | ||
operations[operation.operationId] = operation; | ||
return; | ||
} | ||
output += ` "${method}": {\n ${transformOperationObj(operation, schema.parameters)};\n }\n`; | ||
}); | ||
output += ` }\n`; | ||
if (schema.paths) { | ||
output += transformPathsObj(schema.paths, { | ||
operations, | ||
parameters: schema.components && schema.components.parameters || schema.parameters | ||
}); | ||
@@ -412,6 +462,18 @@ } | ||
{ | ||
let schemaDefs = rawSchema ? schema : schema.definitions; | ||
output += `export interface definitions { ${transformSchemaObjMap(schemaDefs || {}, { | ||
required: Object.keys(schemaDefs) | ||
})}\n}`; | ||
output += `export interface definitions {\n ${transformSchemaObjMap(schema.definitions || {}, { | ||
required: Object.keys(schema.definitions) | ||
})}\n}\n\n`; | ||
if (schema.parameters) { | ||
const required = Object.keys(schema.parameters); | ||
output += `export interface parameters {\n ${transformSchemaObjMap(schema.parameters, { | ||
required | ||
})}\n }\n\n`; | ||
} | ||
if (schema.responses) { | ||
output += `export interface responses {\n ${transformResponsesObj(schema.responses)}\n }\n\n`; | ||
} | ||
break; | ||
} | ||
@@ -421,25 +483,51 @@ | ||
{ | ||
var _schema$components; | ||
output += `export interface components {\n`; | ||
output += `export interface components {\n`; | ||
let schemaDefs = rawSchema ? schema : (_schema$components = schema.components) === null || _schema$components === void 0 ? void 0 : _schema$components.schemas; | ||
if (schemaDefs) output += ` schemas: {\n ${transformSchemaObjMap(schemaDefs, { | ||
required: Object.keys(schemaDefs) | ||
})}\n }\n`; | ||
output += `}\n`; | ||
if (schema.components) { | ||
if (schema.components.schemas) { | ||
const required = Object.keys(schema.components.schemas); | ||
output += ` schemas: {\n ${transformSchemaObjMap(schema.components.schemas, { | ||
required | ||
})}\n }\n`; | ||
} | ||
if (schema.components.responses) { | ||
output += ` responses: {\n ${transformResponsesObj(schema.components.responses)}\n }\n`; | ||
} | ||
if (schema.components.parameters) { | ||
const required = Object.keys(schema.components.parameters); | ||
output += ` parameters: {\n ${transformSchemaObjMap(schema.components.parameters, { | ||
required | ||
})}\n }\n`; | ||
} | ||
if (schema.components.requestBodies) { | ||
const required = Object.keys(schema.components.requestBodies); | ||
output += ` requestBodies: {\n ${transformSchemaObjMap(schema.components.requestBodies, { | ||
required | ||
})}\n }\n`; | ||
} | ||
if (schema.components.headers) { | ||
output += ` headers: {\n ${transformHeaderObjMap(schema.components.headers)} }\n`; | ||
} | ||
} | ||
output += `}\n\n`; | ||
break; | ||
} | ||
} | ||
output += `export interface operations {\n`; | ||
if (Object.keys(operations).length) { | ||
output += `export interface operations {\n`; | ||
Object.entries(operations).forEach(([operationId, operation]) => { | ||
var _schema$components2; | ||
if (operation.description) output += comment(operation.description); | ||
output += ` "${operationId}": {\n ${transformOperationObj(operation, (_schema$components2 = schema.components) === null || _schema$components2 === void 0 ? void 0 : _schema$components2.parameters)};\n }\n`; | ||
output += ` "${operationId}": {\n ${transformOperationObj(operation, schema.components && schema.components.parameters)};\n }\n`; | ||
}); | ||
output += `}\n`; | ||
} | ||
return output; | ||
output += `}\n`; | ||
return output.trim(); | ||
} | ||
@@ -446,0 +534,0 @@ |
@@ -1,3 +0,6 @@ | ||
import { comment, transformRef } from "../utils"; | ||
import { comment } from "../utils"; | ||
import { transformHeaderObjMap } from "./headers"; | ||
import { transformOperationObj } from "./operation"; | ||
import { transformPathsObj } from "./paths"; | ||
import { transformResponsesObj } from "./responses"; | ||
import { transformSchemaObjMap } from "./schema"; | ||
@@ -7,23 +10,21 @@ export function transformAll(schema, { version, rawSchema }) { | ||
let operations = {}; | ||
if (rawSchema) { | ||
switch (version) { | ||
case 2: { | ||
return `export interface definitions {\n ${transformSchemaObjMap(schema, { | ||
required: Object.keys(schema), | ||
})}\n}`; | ||
} | ||
case 3: { | ||
return `export interface schemas {\n ${transformSchemaObjMap(schema, { | ||
required: Object.keys(schema), | ||
})}\n }\n\n`; | ||
} | ||
} | ||
} | ||
output += `export interface paths {\n`; | ||
if (!rawSchema && schema.paths) { | ||
Object.entries(schema.paths).forEach(([url, pathItem]) => { | ||
if (pathItem.description) | ||
output += comment(pathItem.description); | ||
if (pathItem.$ref) { | ||
output += ` "${url}": ${transformRef(pathItem.$ref)};\n`; | ||
return; | ||
} | ||
output += ` "${url}": {\n`; | ||
Object.entries(pathItem).forEach(([method, operation]) => { | ||
if (operation.description) | ||
output += comment(operation.description); | ||
if (operation.operationId) { | ||
output += ` "${method}": operations["${operation.operationId}"];\n`; | ||
operations[operation.operationId] = operation; | ||
return; | ||
} | ||
output += ` "${method}": {\n ${transformOperationObj(operation, schema.parameters)};\n }\n`; | ||
}); | ||
output += ` }\n`; | ||
if (schema.paths) { | ||
output += transformPathsObj(schema.paths, { | ||
operations, | ||
parameters: (schema.components && schema.components.parameters) || schema.parameters, | ||
}); | ||
@@ -34,27 +35,56 @@ } | ||
case 2: { | ||
let schemaDefs = rawSchema ? schema : schema.definitions; | ||
output += `export interface definitions { ${transformSchemaObjMap(schemaDefs || {}, { | ||
required: Object.keys(schemaDefs), | ||
})}\n}`; | ||
output += `export interface definitions {\n ${transformSchemaObjMap(schema.definitions || {}, { | ||
required: Object.keys(schema.definitions), | ||
})}\n}\n\n`; | ||
if (schema.parameters) { | ||
const required = Object.keys(schema.parameters); | ||
output += `export interface parameters {\n ${transformSchemaObjMap(schema.parameters, { | ||
required, | ||
})}\n }\n\n`; | ||
} | ||
if (schema.responses) { | ||
output += `export interface responses {\n ${transformResponsesObj(schema.responses)}\n }\n\n`; | ||
} | ||
break; | ||
} | ||
case 3: { | ||
output += `export interface components {\n`; | ||
let schemaDefs = rawSchema ? schema : schema.components?.schemas; | ||
if (schemaDefs) | ||
output += ` schemas: {\n ${transformSchemaObjMap(schemaDefs, { | ||
required: Object.keys(schemaDefs), | ||
})}\n }\n`; | ||
output += `}\n`; | ||
if (schema.components) { | ||
if (schema.components.schemas) { | ||
const required = Object.keys(schema.components.schemas); | ||
output += ` schemas: {\n ${transformSchemaObjMap(schema.components.schemas, { required })}\n }\n`; | ||
} | ||
if (schema.components.responses) { | ||
output += ` responses: {\n ${transformResponsesObj(schema.components.responses)}\n }\n`; | ||
} | ||
if (schema.components.parameters) { | ||
const required = Object.keys(schema.components.parameters); | ||
output += ` parameters: {\n ${transformSchemaObjMap(schema.components.parameters, { | ||
required, | ||
})}\n }\n`; | ||
} | ||
if (schema.components.requestBodies) { | ||
const required = Object.keys(schema.components.requestBodies); | ||
output += ` requestBodies: {\n ${transformSchemaObjMap(schema.components.requestBodies, { | ||
required, | ||
})}\n }\n`; | ||
} | ||
if (schema.components.headers) { | ||
output += ` headers: {\n ${transformHeaderObjMap(schema.components.headers)} }\n`; | ||
} | ||
} | ||
output += `}\n\n`; | ||
break; | ||
} | ||
} | ||
output += `export interface operations {\n`; | ||
if (Object.keys(operations).length) { | ||
output += `export interface operations {\n`; | ||
Object.entries(operations).forEach(([operationId, operation]) => { | ||
if (operation.description) | ||
output += comment(operation.description); | ||
output += ` "${operationId}": {\n ${transformOperationObj(operation, schema.components?.parameters)};\n }\n`; | ||
output += ` "${operationId}": {\n ${transformOperationObj(operation, schema.components && schema.components.parameters)};\n }\n`; | ||
}); | ||
output += `}\n`; | ||
} | ||
return output; | ||
output += `}\n`; | ||
return output.trim(); | ||
} |
import { transformSchemaObj } from "./schema"; | ||
import { comment } from "../utils"; | ||
export function transformParametersArray(parameters, globalParameters) { | ||
export function transformParametersArray(parameters, globalParams) { | ||
let output = ""; | ||
let mappedParams = {}; | ||
parameters.forEach((paramObj) => { | ||
if (paramObj.$ref && globalParameters) { | ||
if (paramObj.$ref && globalParams) { | ||
const paramName = paramObj.$ref.split("/").pop(); | ||
if (globalParameters[paramName]) { | ||
const reference = globalParameters[paramName]; | ||
if (globalParams[paramName]) { | ||
const reference = globalParams[paramName]; | ||
if (!mappedParams[reference.in]) | ||
@@ -29,4 +29,2 @@ mappedParams[reference.in] = {}; | ||
Object.entries(paramGroup).forEach(([paramName, paramObj]) => { | ||
if (!paramObj.schema) | ||
return; | ||
let paramComment = ""; | ||
@@ -40,3 +38,3 @@ if (paramObj.deprecated) | ||
const required = paramObj.required ? `` : `?`; | ||
output += ` "${paramName}"${required}: ${transformSchemaObj(paramObj.schema)};\n`; | ||
output += ` "${paramName}"${required}: ${paramObj.schema ? transformSchemaObj(paramObj.schema) : "unknown"};\n`; | ||
}); | ||
@@ -43,0 +41,0 @@ output += ` }\n`; |
import { comment, transformRef } from "../utils"; | ||
import { transformSchemaObj, transformSchemaObjMap } from "./schema"; | ||
import { transformHeaderObjMap } from "./headers"; | ||
import { transformSchemaObj } from "./schema"; | ||
const resType = (res) => (res === 204 || (res >= 300 && res < 400) ? "never" : "unknown"); | ||
export function transformResponsesObj(responsesObj) { | ||
let output = ""; | ||
Object.entries(responsesObj).forEach(([k, v]) => { | ||
if (v.description) | ||
output += comment(v.description); | ||
const resKey = parseInt(k, 10) ? k : `"${k}"`; | ||
if (v.$ref) { | ||
output += ` ${resKey}: ${transformRef(v.$ref)};\n`; | ||
Object.entries(responsesObj).forEach(([httpStatusCode, response]) => { | ||
if (response.description) | ||
output += comment(response.description); | ||
const statusCode = Number(httpStatusCode) || `"${httpStatusCode}"`; | ||
if (response.$ref) { | ||
output += ` ${statusCode}: ${transformRef(response.$ref)};\n`; | ||
return; | ||
} | ||
if ((!v.content && !v.schema) || (v.content && !Object.keys(v.content).length)) { | ||
output += ` ${resKey}: unknown;\n`; | ||
if ((!response.content && !response.schema) || (response.content && !Object.keys(response.content).length)) { | ||
output += ` ${statusCode}: ${resType(statusCode)};\n`; | ||
return; | ||
} | ||
output += ` ${resKey}: {\n`; | ||
if (v.headers) { | ||
if (v.headers.$ref) { | ||
output += ` headers: ${transformRef(v.headers.$ref)};\n`; | ||
output += ` ${statusCode}: {\n`; | ||
if (response.headers) { | ||
if (response.headers.$ref) { | ||
output += ` headers: ${transformRef(response.headers.$ref)};\n`; | ||
} | ||
else { | ||
output += ` headers: {\n ${transformSchemaObjMap(v.headers)}\n }\n`; | ||
output += ` headers: {\n ${transformHeaderObjMap(response.headers)}\n }\n`; | ||
} | ||
} | ||
if (v.content) { | ||
Object.entries(v.content).forEach(([contentType, contentResponse]) => { | ||
output += ` "${contentType}": ${transformSchemaObj(contentResponse.schema)};\n`; | ||
if (response.content) { | ||
Object.entries(response.content).forEach(([contentType, contentResponse]) => { | ||
output += ` "${contentType}": ${transformSchemaObj(contentResponse.schema)};\n`; | ||
}); | ||
} | ||
else if (v.schema) { | ||
output += ` schema: ${transformSchemaObj(v.schema)};\n`; | ||
else if (response.schema) { | ||
output += ` schema: ${transformSchemaObj(response.schema)};\n`; | ||
} | ||
@@ -34,0 +36,0 @@ output += ` }\n`; |
import { comment, nodeType, transformRef, tsArrayOf, tsIntersectionOf, tsPartial, tsTupleOf, tsUnionOf, } from "../utils"; | ||
export function transformSchemaObjMap(obj, options) { | ||
let output = ""; | ||
let required = options?.required || []; | ||
let required = (options && options.required) || []; | ||
Object.entries(obj).forEach(([key, value]) => { | ||
@@ -6,0 +6,0 @@ if (value.description) |
import { ParameterObject, ReferenceObject } from "../types"; | ||
export declare function transformParametersArray(parameters: (ReferenceObject | ParameterObject)[], globalParameters?: Record<string, ParameterObject>): string; | ||
export declare function transformParametersArray(parameters: (ReferenceObject | ParameterObject)[], globalParams?: Record<string, ParameterObject>): string; |
@@ -22,2 +22,3 @@ export interface OpenAPI2 { | ||
description?: string; | ||
required?: boolean; | ||
schema: ReferenceObject | SchemaObject; | ||
@@ -24,0 +25,0 @@ } |
{ | ||
"name": "openapi-typescript", | ||
"description": "Generate TypeScript types from Swagger OpenAPI specs", | ||
"version": "3.0.0-alpha.0", | ||
"version": "3.0.0-alpha.1", | ||
"license": "ISC", | ||
@@ -6,0 +6,0 @@ "bin": { |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
100898
29
1238
2