swagger-typescript-api
Advanced tools
Comparing version
# next release | ||
# 1.6.2 | ||
Fixes: | ||
- Nullable not included in type definition ([issue](https://github.com/acacode/swagger-typescript-api/issues/36)) | ||
Internal: | ||
- Update `swagger2openapi`(`6.0.0`) dependency | ||
# 1.6.1 | ||
@@ -4,0 +14,0 @@ |
{ | ||
"name": "swagger-typescript-api", | ||
"version": "1.6.1", | ||
"version": "1.6.2", | ||
"description": "Create typescript api module from swagger schema", | ||
@@ -39,3 +39,3 @@ "scripts": { | ||
"prettier": "^2.0.2", | ||
"swagger2openapi": "^5.4.0" | ||
"swagger2openapi": "^6.0.0" | ||
}, | ||
@@ -42,0 +42,0 @@ "bin": { |
@@ -14,3 +14,3 @@ const _ = require("lodash"); | ||
const findSchemaType = schema => { | ||
const findSchemaType = (schema) => { | ||
if (schema.enum) return "enum"; | ||
@@ -23,5 +23,8 @@ if (schema.properties) return "object"; | ||
const getPrimitiveType = property => { | ||
const type = _.get(property, "type"); | ||
return typeAliases[type] || type || DEFAULT_PRIMITIVE_TYPE; | ||
const getPrimitiveType = (property) => { | ||
const { type, nullable } = property || {}; | ||
const primitiveType = typeAliases[type] || type; | ||
return primitiveType | ||
? (nullable && `${primitiveType} | null`) || primitiveType | ||
: DEFAULT_PRIMITIVE_TYPE; | ||
}; | ||
@@ -36,3 +39,3 @@ | ||
const getRefType = property => { | ||
const getRefType = (property) => { | ||
const ref = property && property["$ref"]; | ||
@@ -42,3 +45,3 @@ return (ref && config.componentsMap[ref]) || null; | ||
const getRefTypeName = property => { | ||
const getRefTypeName = (property) => { | ||
const refTypeInfo = getRefType(property); | ||
@@ -48,3 +51,3 @@ return refTypeInfo && checkAndRenameModelName(refTypeInfo.typeName); | ||
const getType = property => { | ||
const getType = (property) => { | ||
if (!property) return DEFAULT_PRIMITIVE_TYPE; | ||
@@ -56,3 +59,3 @@ | ||
const getObjectTypeContent = properties => { | ||
const getObjectTypeContent = (properties) => { | ||
return _.map(properties, (property, name) => { | ||
@@ -75,3 +78,3 @@ // TODO: probably nullable should'n be use as required/no-required conditions | ||
const complexSchemaParsers = { | ||
oneOf: schema => { | ||
oneOf: (schema) => { | ||
// T1 | T2 | ||
@@ -81,7 +84,7 @@ const combined = _.map(schema.oneOf, complexTypeGetter); | ||
}, | ||
allOf: schema => { | ||
allOf: (schema) => { | ||
// T1 & T2 | ||
return _.map(schema.allOf, complexTypeGetter).join(" & "); | ||
}, | ||
anyOf: schema => { | ||
anyOf: (schema) => { | ||
// T1 | T2 | (T1 & T2) | ||
@@ -92,3 +95,3 @@ const combined = _.map(schema.anyOf, complexTypeGetter); | ||
// TODO | ||
not: schema => { | ||
not: (schema) => { | ||
// TODO | ||
@@ -98,3 +101,3 @@ }, | ||
const getComplexType = schema => { | ||
const getComplexType = (schema) => { | ||
if (schema.oneOf) return "oneOf"; | ||
@@ -121,3 +124,3 @@ if (schema.allOf) return "allOf"; | ||
description: formatDescription(schema.description), | ||
content: _.map(schema.enum, key => ({ | ||
content: _.map(schema.enum, (key) => ({ | ||
key, | ||
@@ -131,3 +134,3 @@ type, | ||
if (_.isArray(schema.required) && schema.properties) { | ||
schema.required.forEach(requiredFieldName => { | ||
schema.required.forEach((requiredFieldName) => { | ||
if (schema.properties[requiredFieldName]) { | ||
@@ -149,3 +152,3 @@ schema.properties[requiredFieldName].required = true; | ||
description: formatDescription(schema.description), | ||
allFieldsAreOptional: !_.some(_.values(typeContent), part => part.isRequired), | ||
allFieldsAreOptional: !_.some(_.values(typeContent), (part) => part.isRequired), | ||
content: typeContent, | ||
@@ -188,3 +191,3 @@ }; | ||
const checkAndFixSchema = schema => { | ||
const checkAndFixSchema = (schema) => { | ||
if (schema.items && !schema.type) { | ||
@@ -225,6 +228,6 @@ schema.type = "array"; | ||
const parseSchemas = components => | ||
const parseSchemas = (components) => | ||
_.map(_.get(components, "schemas"), (schema, typeName) => parseSchema(schema, typeName)); | ||
const getInlineParseContent = rawTypeData => | ||
const getInlineParseContent = (rawTypeData) => | ||
parseSchema(rawTypeData, null, inlineExtraFormatters).content; | ||
@@ -231,0 +234,0 @@ |
@@ -1,59 +0,65 @@ | ||
const _ = require('lodash'); | ||
const _ = require("lodash"); | ||
const { checkAndRenameModelName } = require("./modelNames"); | ||
const formatters = { | ||
'enum': content => _.map(content, ({ key, value }) => ` ${key} = ${value}`).join(',\n'), | ||
'intEnum': content => _.map(content, ({ value }) => value).join(' | '), | ||
'object': content => _.map(content, part => { | ||
const extraSpace = ' ' | ||
const result = `${extraSpace}${part.field};\n`; | ||
enum: (content) => _.map(content, ({ key, value }) => ` ${key} = ${value}`).join(",\n"), | ||
intEnum: (content) => _.map(content, ({ value }) => value).join(" | "), | ||
object: (content) => | ||
_.map(content, (part) => { | ||
const extraSpace = " "; | ||
const result = `${extraSpace}${part.field};\n`; | ||
const comments = [ | ||
part.title, | ||
part.description | ||
].filter(Boolean) | ||
const comments = [part.title, part.description].filter(Boolean); | ||
const commonText = comments.length ? [ | ||
'', | ||
'/**', | ||
...comments.reduce((acc, comment) => [...acc, ...comment.split(/\n/g).map(part => ` * ${part}`)], []), | ||
' */' | ||
].map(part => `${extraSpace}${part}\n`).join('') : ''; | ||
const commonText = comments.length | ||
? [ | ||
"", | ||
"/**", | ||
...comments.reduce( | ||
(acc, comment) => [...acc, ...comment.split(/\n/g).map((part) => ` * ${part}`)], | ||
[], | ||
), | ||
" */", | ||
] | ||
.map((part) => `${extraSpace}${part}\n`) | ||
.join("") | ||
: ""; | ||
return `${commonText}${result}`; | ||
}).join(''), | ||
'type': content => { | ||
if (content.includes(' & ')) { | ||
return content.split(' & ').map(checkAndRenameModelName).join(' & ') | ||
return `${commonText}${result}`; | ||
}).join(""), | ||
type: (content) => { | ||
if (content.includes(" & ")) { | ||
return content.split(" & ").map(checkAndRenameModelName).join(" & "); | ||
} | ||
if (content.includes(' | ')) { | ||
return content.split(' | ').map(checkAndRenameModelName).join(' | ') | ||
if (content.includes(" | ")) { | ||
return content.split(" | ").map(checkAndRenameModelName).join(" | "); | ||
} | ||
return content | ||
return content; | ||
}, | ||
'primitive': content => checkAndRenameModelName(content), | ||
} | ||
primitive: (content) => checkAndRenameModelName(content), | ||
}; | ||
const inlineExtraFormatters = { | ||
'object': (parsedSchema) => { | ||
object: (parsedSchema) => { | ||
return { | ||
...parsedSchema, | ||
typeIdentifier: parsedSchema.content.length ? parsedSchema.typeIdentifier : 'type', | ||
content: parsedSchema.content.length ? `{ ${parsedSchema.content.map(part => part.field).join(', ')} }` : 'object' | ||
} | ||
typeIdentifier: parsedSchema.content.length ? parsedSchema.typeIdentifier : "type", | ||
content: parsedSchema.content.length | ||
? `{ ${parsedSchema.content.map((part) => part.field).join(", ")} }` | ||
: "object", | ||
}; | ||
}, | ||
'enum': (parsedSchema) => { | ||
enum: (parsedSchema) => { | ||
return { | ||
...parsedSchema, | ||
content: _.map(parsedSchema.content, ({ value }) => `${value}`).join(' | ') | ||
} | ||
} | ||
} | ||
content: _.map(parsedSchema.content, ({ value }) => `${value}`).join(" | "), | ||
}; | ||
}, | ||
}; | ||
module.exports = { | ||
formatters, | ||
inlineExtraFormatters, | ||
} | ||
}; |
53283
0.93%1006
1.11%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated