openapi-codegen-typescript
Advanced tools
Comparing version 0.1.14 to 0.1.15
@@ -0,1 +1,13 @@ | ||
# v0.1.15 (Tue Dec 01 2020) | ||
#### 🐛 Bug Fix | ||
- feat(override): add enum schema override and add property descriptions (part 1) [#18](https://github.com/LandrAudio/openapi-codegen-typescript/pull/18) ([@nemrosim](https://github.com/nemrosim)) | ||
#### Authors: 1 | ||
- Artem Diashkin ([@nemrosim](https://github.com/nemrosim)) | ||
--- | ||
# v0.1.14 (Mon Sep 14 2020) | ||
@@ -2,0 +14,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { ConvertToMocksProps, GetSchemasProps } from './types'; | ||
import { ConvertToMocksProps, EnumSchema, GetSchemasProps } from './types'; | ||
/** | ||
@@ -32,6 +32,7 @@ * Get all interfaces that this schema exteds | ||
DTOs?: any; | ||
overrideSchemas?: Array<EnumSchema>; | ||
} | ||
export declare const parseSchema: ({ schema, name, DTOs }: ParseSchemaProps) => string; | ||
export declare const parseSchemas: ({ json, swaggerVersion }: GetSchemasProps) => string; | ||
export declare const convertToMocks: ({ json, fileName, folderPath, typesPath, swaggerVersion, }: ConvertToMocksProps) => string; | ||
export declare const parseSchema: ({ schema, name, DTOs, overrideSchemas }: ParseSchemaProps) => string; | ||
export declare const parseSchemas: ({ json, swaggerVersion, overrideSchemas }: GetSchemasProps) => string; | ||
export declare const convertToMocks: ({ json, fileName, folderPath, typesPath, swaggerVersion, overrideSchemas, }: ConvertToMocksProps) => string; | ||
export {}; |
@@ -87,3 +87,3 @@ "use strict"; | ||
exports.parseSchema = function (_a) { | ||
var schema = _a.schema, name = _a.name, DTOs = _a.DTOs; | ||
var schema = _a.schema, name = _a.name, DTOs = _a.DTOs, overrideSchemas = _a.overrideSchemas; | ||
var parseSwaggerJsonObject = function (obj, interfaces) { | ||
@@ -133,2 +133,3 @@ if (interfaces) { | ||
DTOs: DTOs, | ||
overrideSchemas: overrideSchemas, | ||
}); | ||
@@ -138,7 +139,13 @@ mocks.push(arrayOneOf); | ||
if ($ref) { | ||
var ref = mockGenerator.getRefTypeMock({ $ref: $ref, propertyName: propertyName, DTOs: DTOs }); | ||
var ref = mockGenerator.getRefTypeMock({ $ref: $ref, propertyName: propertyName, DTOs: DTOs, overrideSchemas: overrideSchemas }); | ||
mocks.push(ref); | ||
} | ||
if (xDictionaryKey && additionalProperties) { | ||
mocks.push(mockGenerator.getDictionaryMock({ propertyName: propertyName, xDictionaryKey: xDictionaryKey, additionalProperties: additionalProperties, DTOs: DTOs })); | ||
mocks.push(mockGenerator.getDictionaryMock({ | ||
propertyName: propertyName, | ||
xDictionaryKey: xDictionaryKey, | ||
additionalProperties: additionalProperties, | ||
DTOs: DTOs, | ||
overrideSchemas: overrideSchemas, | ||
})); | ||
} | ||
@@ -162,3 +169,3 @@ if (!type && !$ref && !oneOf) { | ||
exports.parseSchemas = function (_a) { | ||
var json = _a.json, swaggerVersion = _a.swaggerVersion; | ||
var json = _a.json, swaggerVersion = _a.swaggerVersion, overrideSchemas = _a.overrideSchemas; | ||
var schemas = shared_1.getSchemas({ json: json, swaggerVersion: swaggerVersion }); | ||
@@ -175,2 +182,3 @@ var DTOs = Object.keys(schemas); | ||
DTOs: schemas, | ||
overrideSchemas: overrideSchemas, | ||
}); | ||
@@ -188,3 +196,3 @@ resultString += result; | ||
exports.convertToMocks = function (_a) { | ||
var json = _a.json, fileName = _a.fileName, folderPath = _a.folderPath, typesPath = _a.typesPath, _b = _a.swaggerVersion, swaggerVersion = _b === void 0 ? 3 : _b; | ||
var json = _a.json, fileName = _a.fileName, folderPath = _a.folderPath, typesPath = _a.typesPath, _b = _a.swaggerVersion, swaggerVersion = _b === void 0 ? 3 : _b, overrideSchemas = _a.overrideSchemas; | ||
var schemas = shared_1.getSchemas({ json: json, swaggerVersion: swaggerVersion }); | ||
@@ -195,3 +203,3 @@ var imports = Object.keys(schemas).join(', '); | ||
var importsDescription = "import {" + imports + "} from '" + typesPath + "';\n"; | ||
var result = exports.parseSchemas({ json: json, swaggerVersion: swaggerVersion }); | ||
var result = exports.parseSchemas({ json: json, swaggerVersion: swaggerVersion, overrideSchemas: overrideSchemas }); | ||
var resultString = "" + disableNoUse + disableNoUsedVars + importsDescription + result; | ||
@@ -198,0 +206,0 @@ shared_1.writeToFile({ |
@@ -1,5 +0,5 @@ | ||
import { ConvertRefType, GetArrayOfItemsMockProps, GetArrayOfOneOfMockProps, GetDictionaryMockProps, GetNumberMockProps, GetRefTypeMockProps, GetStringMockProps, MockArrayProps } from './types'; | ||
import { ConvertRefType, EnumProps, EnumSchema, GetArrayOfItemsMockProps, GetArrayOfOneOfMockProps, GetDictionaryMockProps, GetNumberMockProps, GetRefTypeMockProps, GetStringMockProps, MockArrayProps } from './types'; | ||
export declare class MockGenerateHelper { | ||
private casual; | ||
constructor(casual: any); | ||
constructor(casual: Casual.Generators & Casual.Casual); | ||
getStringMock({ name, propertyName, format }: GetStringMockProps): MockArrayProps; | ||
@@ -32,10 +32,12 @@ /** | ||
* @param DTOs | ||
* @param overrideSchemas | ||
*/ | ||
getDtoMock({ propertyName, oneOf, DTOs }: GetArrayOfOneOfMockProps): MockArrayProps; | ||
getDictionaryMock({ propertyName, xDictionaryKey, additionalProperties, DTOs, }: GetDictionaryMockProps): MockArrayProps; | ||
getDtoMock({ propertyName, oneOf, DTOs, overrideSchemas }: GetArrayOfOneOfMockProps): MockArrayProps; | ||
getDictionaryMock({ propertyName, xDictionaryKey, additionalProperties, DTOs, overrideSchemas, }: GetDictionaryMockProps): MockArrayProps; | ||
getAnyMock({ propertyName }: { | ||
propertyName: string; | ||
}): MockArrayProps; | ||
getRefTypeMock: ({ $ref, propertyName, DTOs }: GetRefTypeMockProps) => MockArrayProps; | ||
getRefTypeMock: ({ $ref, propertyName, DTOs, overrideSchemas }: GetRefTypeMockProps) => MockArrayProps; | ||
static parseRefType: (refType: string[]) => string; | ||
static getOverridedSchema: (schemaName: string, overrideSchemas?: EnumSchema[] | undefined) => EnumProps | undefined; | ||
static joinVariableNamesAndValues: (varNamesAndValues: MockArrayProps[]) => string; | ||
@@ -42,0 +44,0 @@ static getMockTemplateString: ({ typeName, varNamesAndValues }: any) => string; |
@@ -13,3 +13,3 @@ "use strict"; | ||
this.getRefTypeMock = function (_a) { | ||
var $ref = _a.$ref, propertyName = _a.propertyName, DTOs = _a.DTOs; | ||
var $ref = _a.$ref, propertyName = _a.propertyName, DTOs = _a.DTOs, overrideSchemas = _a.overrideSchemas; | ||
var result = { | ||
@@ -20,4 +20,4 @@ propertyName: "TODO: FIX ERROR in " + propertyName + " ref:" + $ref, | ||
var refType = $ref.split('/'); | ||
var ref = MockGenerateHelper.parseRefType(refType); | ||
var schema = DTOs[ref]; | ||
var schemaName = MockGenerateHelper.parseRefType(refType); | ||
var schema = MockGenerateHelper.getOverridedSchema(schemaName, overrideSchemas) || DTOs[schemaName]; | ||
if (schema && schema.enum) { | ||
@@ -27,3 +27,3 @@ result = { propertyName: propertyName, value: "'" + schema.enum[0] + "'" }; | ||
else if (schema) { | ||
result = MockGenerateHelper.convertRefType({ propertyName: propertyName, ref: ref }); | ||
result = MockGenerateHelper.convertRefType({ propertyName: propertyName, ref: schemaName }); | ||
} | ||
@@ -54,2 +54,5 @@ return result; | ||
} | ||
else if (format === types_1.StringFormats.Uri) { | ||
value = "'" + this.casual.url + "'"; | ||
} | ||
if (!value) { | ||
@@ -142,8 +145,9 @@ value = 'TODO: FIX'; | ||
* @param DTOs | ||
* @param overrideSchemas | ||
*/ | ||
MockGenerateHelper.prototype.getDtoMock = function (_a) { | ||
var propertyName = _a.propertyName, oneOf = _a.oneOf, DTOs = _a.DTOs; | ||
var propertyName = _a.propertyName, oneOf = _a.oneOf, DTOs = _a.DTOs, overrideSchemas = _a.overrideSchemas; | ||
var refType = oneOf[0][types_1.SwaggerProps.$ref].split('/'); | ||
var ref = MockGenerateHelper.parseRefType(refType); | ||
var schema = DTOs[ref]; | ||
var schemaName = MockGenerateHelper.parseRefType(refType); | ||
var schema = MockGenerateHelper.getOverridedSchema(schemaName, overrideSchemas) || DTOs[schemaName]; | ||
if (schema && schema.enum) { | ||
@@ -153,10 +157,23 @@ return { propertyName: propertyName, value: "'" + schema.enum[0] + "'" }; | ||
else { | ||
return MockGenerateHelper.convertRefType({ propertyName: propertyName, ref: ref }); | ||
return MockGenerateHelper.convertRefType({ propertyName: propertyName, ref: schemaName }); | ||
} | ||
}; | ||
MockGenerateHelper.prototype.getDictionaryMock = function (_a) { | ||
var propertyName = _a.propertyName, xDictionaryKey = _a.xDictionaryKey, additionalProperties = _a.additionalProperties, DTOs = _a.DTOs; | ||
var propertyName = _a.propertyName, xDictionaryKey = _a.xDictionaryKey, additionalProperties = _a.additionalProperties, DTOs = _a.DTOs, overrideSchemas = _a.overrideSchemas; | ||
if (xDictionaryKey[types_1.SwaggerProps.$ref]) { | ||
var dictionaryRef = MockGenerateHelper.parseRefType(xDictionaryKey[types_1.SwaggerProps.$ref].split('/')); | ||
var dicSchema = DTOs[dictionaryRef]; | ||
var dictionaryRef_1 = MockGenerateHelper.parseRefType(xDictionaryKey[types_1.SwaggerProps.$ref].split('/')); | ||
var dicSchema = void 0; | ||
/** | ||
* Check if current schema is override | ||
*/ | ||
if ((overrideSchemas === null || overrideSchemas === void 0 ? void 0 : overrideSchemas.length) && overrideSchemas.find(function (e) { return e[dictionaryRef_1]; })) { | ||
// for TS happiness | ||
var overrideSchema = overrideSchemas.find(function (e) { return e[dictionaryRef_1]; }); | ||
if (overrideSchema) { | ||
dicSchema = overrideSchema[dictionaryRef_1]; | ||
} | ||
} | ||
else { | ||
dicSchema = DTOs[dictionaryRef_1]; | ||
} | ||
// Enum keys and Enum values | ||
@@ -211,2 +228,11 @@ if (additionalProperties[types_1.SwaggerProps.$ref]) { | ||
MockGenerateHelper.parseRefType = function (refType) { return refType[refType.length - 1]; }; | ||
MockGenerateHelper.getOverridedSchema = function (schemaName, overrideSchemas) { | ||
if ((overrideSchemas === null || overrideSchemas === void 0 ? void 0 : overrideSchemas.length) && overrideSchemas.find(function (e) { return e[schemaName]; })) { | ||
// for TS happiness | ||
var overrideSchema = overrideSchemas.find(function (e) { return e[schemaName]; }); | ||
if (overrideSchema) { | ||
return overrideSchema[schemaName]; | ||
} | ||
} | ||
}; | ||
MockGenerateHelper.joinVariableNamesAndValues = function (varNamesAndValues) { | ||
@@ -213,0 +239,0 @@ return varNamesAndValues.map(function (mock) { return " " + mock.propertyName + ": " + mock.value + ","; }).join('\n'); |
import { GetSchemasProps } from './types'; | ||
export declare const getSchemaProperties: (objectProps: any) => { | ||
propertyName: string; | ||
description: any; | ||
type: any; | ||
@@ -5,0 +6,0 @@ $ref: any; |
@@ -43,3 +43,3 @@ "use strict"; | ||
return Object.keys(objectProps).map(function (property) { | ||
var _a = objectProps[property], type = _a.type, $ref = _a.$ref, oneOf = _a.oneOf, format = _a.format, minLength = _a.minLength, maxLength = _a.maxLength, nullable = _a.nullable, items = _a.items, minimum = _a.minimum, maximum = _a.maximum, exclusiveMinimum = _a.exclusiveMinimum, exclusiveMaximum = _a.exclusiveMaximum, minItems = _a.minItems, maxItems = _a.maxItems, uniqueItems = _a.uniqueItems, | ||
var _a = objectProps[property], type = _a.type, description = _a.description, $ref = _a.$ref, oneOf = _a.oneOf, format = _a.format, minLength = _a.minLength, maxLength = _a.maxLength, nullable = _a.nullable, items = _a.items, minimum = _a.minimum, maximum = _a.maximum, exclusiveMinimum = _a.exclusiveMinimum, exclusiveMaximum = _a.exclusiveMaximum, minItems = _a.minItems, maxItems = _a.maxItems, uniqueItems = _a.uniqueItems, | ||
// Props for "Dictionary" type | ||
@@ -49,2 +49,3 @@ xDictionaryKey = _a["x-dictionaryKey"], additionalProperties = _a.additionalProperties; | ||
propertyName: property, | ||
description: description, | ||
type: type, | ||
@@ -51,0 +52,0 @@ $ref: $ref, |
@@ -56,3 +56,13 @@ export declare enum PropertyNames { | ||
nullable?: boolean; | ||
description?: string; | ||
} | ||
export interface EnumProps { | ||
type: string; | ||
description: string; | ||
enum: Array<string>; | ||
'x-enumNames'?: Array<string>; | ||
} | ||
export interface EnumSchema { | ||
[key: string]: EnumProps; | ||
} | ||
export interface ConvertToTypesProps { | ||
@@ -63,2 +73,3 @@ json: any; | ||
swaggerVersion: number; | ||
overrideSchemas?: Array<EnumSchema>; | ||
} | ||
@@ -71,2 +82,3 @@ export interface ConvertToMocksProps { | ||
swaggerVersion: number; | ||
overrideSchemas?: Array<EnumSchema>; | ||
} | ||
@@ -81,5 +93,7 @@ export interface GetSchemasProps { | ||
swaggerVersion?: number; | ||
overrideSchemas?: Array<EnumSchema>; | ||
} | ||
export interface ResultStringPropsForNumberType extends ResultStringProps { | ||
format?: string; | ||
description?: string; | ||
minimum?: number; | ||
@@ -92,2 +106,3 @@ maximum?: number; | ||
format?: string; | ||
description?: string; | ||
refType: string[]; | ||
@@ -100,2 +115,3 @@ minItems?: number; | ||
format?: string; | ||
description?: string; | ||
minLength?: number; | ||
@@ -125,2 +141,3 @@ maxLength?: number; | ||
DTOs: any; | ||
overrideSchemas?: Array<EnumSchema>; | ||
} | ||
@@ -132,2 +149,3 @@ export interface GetDictionaryMockProps extends PropertyNameProp { | ||
DTOs: any; | ||
overrideSchemas?: Array<EnumSchema>; | ||
} | ||
@@ -137,5 +155,6 @@ export interface GetRefTypeMockProps extends PropertyNameProp { | ||
DTOs: any; | ||
overrideSchemas?: Array<EnumSchema>; | ||
} | ||
export interface ParseProps { | ||
schema: any; | ||
schema: EnumProps; | ||
schemaKey: string; | ||
@@ -142,0 +161,0 @@ } |
import { ParseProps, ConvertToTypesProps, GetSchemasProps } from './types'; | ||
export declare const parseObject: ({ schema, schemaKey }: { | ||
schema: any; | ||
schemaKey: any; | ||
schemaKey: string; | ||
}) => string; | ||
@@ -11,3 +11,3 @@ /** | ||
export declare const parseEnum: ({ schema, schemaKey }: ParseProps) => string; | ||
export declare const parseSchemas: ({ json, swaggerVersion }: GetSchemasProps) => string; | ||
export declare const convertToTypes: ({ json, fileName, folderPath, swaggerVersion }: ConvertToTypesProps) => void; | ||
export declare const parseSchemas: ({ json, swaggerVersion, overrideSchemas }: GetSchemasProps) => string; | ||
export declare const convertToTypes: ({ json, fileName, folderPath, swaggerVersion, overrideSchemas, }: ConvertToTypesProps) => void; |
@@ -12,7 +12,7 @@ "use strict"; | ||
var parseProperty = function (_a) { | ||
var propertyName = _a.propertyName, nullable = _a.nullable; | ||
return " " + propertyName + (nullable ? '?' : '') + ": "; | ||
var propertyName = _a.propertyName, description = _a.description, nullable = _a.nullable; | ||
return getDescription(description) + " " + propertyName + (nullable ? '?' : '') + ": "; | ||
}; | ||
var getResultStringForNumberType = function (_a) { | ||
var propertyName = _a.propertyName, nullable = _a.nullable, format = _a.format, minimum = _a.minimum, maximum = _a.maximum, exclusiveMinimum = _a.exclusiveMinimum, exclusiveMaximum = _a.exclusiveMaximum; | ||
var propertyName = _a.propertyName, description = _a.description, nullable = _a.nullable, format = _a.format, minimum = _a.minimum, maximum = _a.maximum, exclusiveMinimum = _a.exclusiveMinimum, exclusiveMaximum = _a.exclusiveMaximum; | ||
var nameAndValue = parsePropertyName({ propertyName: propertyName, nullable: nullable, type: 'number' }); | ||
@@ -30,11 +30,12 @@ var formatString = parseFormat(format); | ||
var documentation = "" + formatString + minimumString + maximumString + exclusiveMinimumString + exclusiveMaximumString; | ||
return "" + nameAndValue + (shouldShowDocs ? " // " + documentation : '') + "\n"; | ||
return "" + getDescription(description) + nameAndValue + (shouldShowDocs ? " // " + documentation : '') + "\n"; | ||
}; | ||
var getResultStringForBooleanType = function (_a) { | ||
var propertyName = _a.propertyName, nullable = _a.nullable; | ||
var propertyName = _a.propertyName, description = _a.description, nullable = _a.nullable; | ||
var nameAndValue = " " + propertyName + (nullable ? '?' : '') + ": boolean;"; | ||
return nameAndValue + "\n"; | ||
return "" + getDescription(description) + nameAndValue + "\n"; | ||
}; | ||
var getDescription = function (description) { return "" + (description ? "/**\n * " + description + " \n */\n" : ''); }; | ||
var getResultStringForStringType = function (_a) { | ||
var propertyName = _a.propertyName, nullable = _a.nullable, format = _a.format, minLength = _a.minLength, maxLength = _a.maxLength; | ||
var propertyName = _a.propertyName, description = _a.description, nullable = _a.nullable, format = _a.format, minLength = _a.minLength, maxLength = _a.maxLength; | ||
var nameAndValue = parsePropertyName({ propertyName: propertyName, nullable: nullable, type: 'string' }); | ||
@@ -48,6 +49,6 @@ var formatString = parseFormat(format); | ||
var documentation = "" + formatString + minString + maxString; | ||
return "" + nameAndValue + (shouldShowDocs ? " // " + documentation : '') + "\n"; | ||
return "" + getDescription(description) + nameAndValue + (shouldShowDocs ? " // " + documentation : '') + "\n"; | ||
}; | ||
var getResultStringForArrayType = function (_a) { | ||
var propertyName = _a.propertyName, nullable = _a.nullable, refType = _a.refType, format = _a.format, minItems = _a.minItems, maxItems = _a.maxItems, uniqueItems = _a.uniqueItems; | ||
var propertyName = _a.propertyName, description = _a.description, nullable = _a.nullable, refType = _a.refType, format = _a.format, minItems = _a.minItems, maxItems = _a.maxItems, uniqueItems = _a.uniqueItems; | ||
var nameAndValue = parsePropertyName({ propertyName: propertyName, nullable: nullable, type: parseRefType(refType) + "[]" }); | ||
@@ -64,21 +65,18 @@ var formatString = parseFormat(format); | ||
var documentation = "" + formatString + minItemsString + maxItemsString + uniqueItemsString; | ||
return "" + nameAndValue + (shouldShowDocs ? " // " + documentation : '') + "\n"; | ||
return "" + getDescription(description) + nameAndValue + (shouldShowDocs ? " // " + documentation : '') + "\n"; | ||
}; | ||
var convertToTypesFromSchemaProperties = function (_a) { | ||
var schema = _a.schema, schemaKey = _a.schemaKey, interfaces = _a.interfaces; | ||
var documentation = ''; | ||
if (schema.description) { | ||
documentation = "/**\n * " + schema['description'] + "\n */\n"; | ||
} | ||
var getStandardString = function (_a) { | ||
var propertyName = _a.propertyName, nullable = _a.nullable, refType = _a.refType, format = _a.format, isArray = _a.isArray; | ||
return "" + parseProperty({ propertyName: propertyName, nullable: nullable }) + parseRefType(refType) + (isArray ? '[]' : '') + ";" + parseFormat(format) + "\n"; | ||
var propertyName = _a.propertyName, description = _a.description, nullable = _a.nullable, refType = _a.refType, format = _a.format, isArray = _a.isArray; | ||
return "" + parseProperty({ propertyName: propertyName, description: description, nullable: nullable }) + parseRefType(refType) + (isArray ? '[]' : '') + ";" + parseFormat(format) + "\n"; | ||
}; | ||
var result = documentation + "export interface " + schemaKey + (interfaces ? " extends " + interfaces.join(', ') + " " : ' ') + "{\n"; | ||
var result = getDescription(schema.description) + "export interface " + schemaKey + (interfaces ? " extends " + interfaces.join(', ') + " " : ' ') + "{\n"; | ||
if (schema.properties) { | ||
shared_1.getSchemaProperties(schema.properties).map(function (_a) { | ||
var propertyName = _a.propertyName, $ref = _a.$ref, items = _a.items, type = _a.type, nullable = _a.nullable, format = _a.format, maxLength = _a.maxLength, minLength = _a.minLength, oneOf = _a.oneOf, minimum = _a.minimum, maximum = _a.maximum, exclusiveMinimum = _a.exclusiveMinimum, exclusiveMaximum = _a.exclusiveMaximum, minItems = _a.minItems, maxItems = _a.maxItems, uniqueItems = _a.uniqueItems, xDictionaryKey = _a.xDictionaryKey, additionalProperties = _a.additionalProperties; | ||
var propertyName = _a.propertyName, description = _a.description, $ref = _a.$ref, items = _a.items, type = _a.type, nullable = _a.nullable, format = _a.format, maxLength = _a.maxLength, minLength = _a.minLength, oneOf = _a.oneOf, minimum = _a.minimum, maximum = _a.maximum, exclusiveMinimum = _a.exclusiveMinimum, exclusiveMaximum = _a.exclusiveMaximum, minItems = _a.minItems, maxItems = _a.maxItems, uniqueItems = _a.uniqueItems, xDictionaryKey = _a.xDictionaryKey, additionalProperties = _a.additionalProperties; | ||
if (type === types_1.DataTypes.String) { | ||
result += getResultStringForStringType({ | ||
propertyName: propertyName, | ||
description: description, | ||
nullable: nullable, | ||
@@ -93,2 +91,3 @@ format: format, | ||
propertyName: propertyName, | ||
description: description, | ||
nullable: nullable, | ||
@@ -103,3 +102,3 @@ format: format, | ||
if (type === types_1.DataTypes.Boolean) { | ||
result += getResultStringForBooleanType({ propertyName: propertyName, nullable: nullable }); | ||
result += getResultStringForBooleanType({ propertyName: propertyName, description: description, nullable: nullable }); | ||
} | ||
@@ -111,2 +110,3 @@ if (type === types_1.DataTypes.Array && items) { | ||
propertyName: propertyName, | ||
description: description, | ||
nullable: nullable, | ||
@@ -137,2 +137,3 @@ refType: refType, | ||
propertyName: propertyName, | ||
description: description, | ||
nullable: nullable, | ||
@@ -144,3 +145,10 @@ }) + type_1 + shouldShowBrackets + ";" + parseFormat(format) + (maxItems ? " // maxItems: " + maxItems : '') + "\n"; | ||
var refType = oneOf[0][types_1.SwaggerProps.$ref].split('/'); | ||
result += getStandardString({ propertyName: propertyName, nullable: nullable, refType: refType, format: format, isArray: false }); | ||
result += getStandardString({ | ||
propertyName: propertyName, | ||
description: description, | ||
nullable: nullable, | ||
refType: refType, | ||
format: format, | ||
isArray: false, | ||
}); | ||
} | ||
@@ -166,3 +174,3 @@ if ($ref) { | ||
var additionalRef = parseRefType(additionalProperties[types_1.SwaggerProps.$ref].split('/')); | ||
result += " " + propertyName + ": {\n[key in " + dictionaryRef + "]: " + additionalRef + "; \n }; \n"; | ||
result += getDescription(description) + " " + propertyName + ": {\n[key in " + dictionaryRef + "]: " + additionalRef + "; \n }; \n"; | ||
// Enum keys and Boolean values | ||
@@ -173,3 +181,3 @@ } | ||
var dictionaryRef = parseRefType(xDictionaryKey[types_1.SwaggerProps.$ref].split('/')); | ||
result += " " + propertyName + ": {\n[key in " + dictionaryRef + "]: boolean; \n }; \n"; | ||
result += getDescription(description) + " " + propertyName + ": {\n[key in " + dictionaryRef + "]: boolean; \n }; \n"; | ||
} | ||
@@ -208,3 +216,4 @@ } | ||
var schema = _a.schema, schemaKey = _a.schemaKey; | ||
var result = "export type " + schemaKey + " = "; | ||
var description = schema.description; | ||
var result = (description ? "/**\n * " + description + " \n */\n" : '') + "export type " + schemaKey + " = "; | ||
var enums = schema.enum; | ||
@@ -219,3 +228,3 @@ var len = enums.length; | ||
exports.parseSchemas = function (_a) { | ||
var json = _a.json, swaggerVersion = _a.swaggerVersion; | ||
var json = _a.json, swaggerVersion = _a.swaggerVersion, overrideSchemas = _a.overrideSchemas; | ||
var schemas = shared_1.getSchemas({ json: json, swaggerVersion: swaggerVersion }); | ||
@@ -232,6 +241,27 @@ if (schemas) { | ||
if (schema[types_1.SwaggerProps.Type] === types_1.DataTypes.Object || schema[types_1.SwaggerProps.AllOf]) { | ||
result_1 += exports.parseObject({ schema: schema, schemaKey: schemaKey }); | ||
/** | ||
* Sometimes in swagger v2 schema key could be named as SomeDto[AnotherDto] | ||
*/ | ||
if (swaggerVersion === 2 && schemaKey.includes('[') && schemaKey.includes(']')) { | ||
var strings = schemaKey.split('['); | ||
result_1 += exports.parseObject({ schema: schema, schemaKey: strings[0] }); | ||
} | ||
else { | ||
result_1 += exports.parseObject({ schema: schema, schemaKey: schemaKey }); | ||
} | ||
} | ||
else if (schema.type === types_1.DataTypes.String) { | ||
result_1 += exports.parseEnum({ schema: schema, schemaKey: schemaKey }); | ||
/** | ||
* Check if current schema is override | ||
*/ | ||
if ((overrideSchemas === null || overrideSchemas === void 0 ? void 0 : overrideSchemas.length) && overrideSchemas.find(function (e) { return e[schemaKey]; })) { | ||
// for TS happiness | ||
var overrideSchema = overrideSchemas.find(function (e) { return e[schemaKey]; }); | ||
if (overrideSchema) { | ||
result_1 += exports.parseEnum({ schema: overrideSchema[schemaKey], schemaKey: schemaKey }); | ||
} | ||
} | ||
else { | ||
result_1 += exports.parseEnum({ schema: schema, schemaKey: schemaKey }); | ||
} | ||
} | ||
@@ -254,4 +284,4 @@ else { | ||
exports.convertToTypes = function (_a) { | ||
var json = _a.json, fileName = _a.fileName, folderPath = _a.folderPath, swaggerVersion = _a.swaggerVersion; | ||
var resultString = exports.parseSchemas({ json: json, swaggerVersion: swaggerVersion }); | ||
var json = _a.json, fileName = _a.fileName, folderPath = _a.folderPath, swaggerVersion = _a.swaggerVersion, overrideSchemas = _a.overrideSchemas; | ||
var resultString = exports.parseSchemas({ json: json, swaggerVersion: swaggerVersion, overrideSchemas: overrideSchemas }); | ||
shared_1.writeToFile({ | ||
@@ -258,0 +288,0 @@ folderPath: folderPath, |
{ | ||
"name": "openapi-codegen-typescript", | ||
"version": "0.1.14", | ||
"version": "0.1.15", | ||
"description": "OpenApi codegen for generating types an mocks from swagger json file", | ||
@@ -43,3 +43,3 @@ "main": "dist/index.js", | ||
"@typescript-eslint/parser": "2.20.0", | ||
"auto": "^9.53.1", | ||
"auto": "^10.4.2", | ||
"eslint": "6.8.0", | ||
@@ -46,0 +46,0 @@ "eslint-config-prettier": "6.10.0", |
171
README.md
@@ -5,53 +5,117 @@ # OpenApi-codegen-typescript | ||
- `yarn add -D openapi-codegen-typescript` | ||
- `npm install openapi-codegen-typescript --save-dev` | ||
- `yarn add -D openapi-codegen-typescript` | ||
- `npm install openapi-codegen-typescript --save-dev` | ||
## Description | ||
What is this library for? | ||
- For fetching json file (mostly for "Swagger json") | ||
#### Example: | ||
```javascript | ||
const { fetchSwaggerJsonFile } = require('openapi-codegen-typescript'); | ||
async function doSomething() { | ||
const json = await fetchSwaggerJsonFile('https://custom/swagger.json'); | ||
console.log("Json Result", json) | ||
} | ||
``` | ||
- For converting swagger.json file to typescript types | ||
#### Example: | ||
```javascript | ||
const { fetchSwaggerJsonFile, convertToTypes } = require('openapi-codegen-typescript'); | ||
async function doSomething() { | ||
const json = await fetchSwaggerJsonFile('https://custom/swagger.json'); | ||
convertToTypes({ json, fileName: 'dtoAPI', folderPath: 'src/types/generated', swaggerVersion: 3 }); | ||
} | ||
``` | ||
- For fetching json file (mostly for "Swagger json") | ||
This function ('doSomething()') fetches json file from urls, then converts json | ||
#### Example: | ||
```javascript | ||
const { fetchSwaggerJsonFile } = require('openapi-codegen-typescript'); | ||
async function doSomething() { | ||
const json = await fetchSwaggerJsonFile('https://custom/swagger.json'); | ||
console.log('Json Result', json); | ||
} | ||
``` | ||
- For converting swagger.json file to typescript types | ||
#### Example: | ||
```javascript | ||
const { fetchSwaggerJsonFile, convertToTypes } = require('openapi-codegen-typescript'); | ||
async function doSomething() { | ||
const json = await fetchSwaggerJsonFile('https://custom/swagger.json'); | ||
convertToTypes({ json, fileName: 'dtoAPI', folderPath: 'src/types/generated', swaggerVersion: 3 }); | ||
} | ||
``` | ||
This function ('doSomething()') fetches json file from urls, then converts json | ||
to typescript "types" and writes "types" to file ('src/types/generated/dtoAPI') | ||
If "swaggerVersion" will not be provided - it will be set to "3" by default. | ||
- For generating mock files that are using converted types | ||
#### Example: | ||
```javascript | ||
const { fetchSwaggerJsonFile, convertToTypes } = require('openapi-codegen-typescript'); | ||
async function doSomething() { | ||
const json = await fetchSwaggerJsonFile('https://custom/swagger.json'); | ||
convertToMocks({ | ||
json, | ||
fileName: 'dtoAPI', | ||
folderPath: 'src/mocks/generated', | ||
typesPath: '../../types/generated/dtoAPI', | ||
swaggerVersion: 3, | ||
}); | ||
} | ||
``` | ||
- For generating mock files that are using converted types | ||
#### Example: | ||
```javascript | ||
const { fetchSwaggerJsonFile, convertToTypes } = require('openapi-codegen-typescript'); | ||
async function doSomething() { | ||
const json = await fetchSwaggerJsonFile('https://custom/swagger.json'); | ||
convertToMocks({ | ||
json, | ||
fileName: 'dtoAPI', | ||
folderPath: 'src/mocks/generated', | ||
typesPath: '../../types/generated/dtoAPI', | ||
swaggerVersion: 3, | ||
}); | ||
} | ||
``` | ||
This function ('doSomething()') fetches json file from urls, then converts json to "mocks" and writes "mocks" to file | ||
('src/mocks/generated/dtoAPI') with imports from typescript types ('src/types/generated/dtoAPI') | ||
('src/mocks/generated/dtoAPI') with imports from typescript types ('src/types/generated/dtoAPI') | ||
If "swaggerVersion" will not be provided - it will be set to "3" by default. | ||
## Overriding enum schema type | ||
You can override open-api enum types and mocks by specifying `overrideSchemas` prop. | ||
Let's imagine that we have this schema enum type in a json file: | ||
```json | ||
{ | ||
"SomeType": { | ||
"type": "string", | ||
"description": "", | ||
"x-enumNames": ["Audio", "Video", "Image", "Text", "Raw"], | ||
"enum": ["Audio", "Video", "Image", "Text", "Raw"] | ||
} | ||
} | ||
``` | ||
Overriding example: | ||
```javascript | ||
const { fetchSwaggerJsonFile, convertToTypes, convertToMocks } = require('openapi-codegen-typescript'); | ||
const url = 'https://someLinkToSwagger/v2/swagger.json'; | ||
async function main() { | ||
const json = await fetchSwaggerJsonFile(url); | ||
const overrideSchemas = [ | ||
{ | ||
ServiceOfferKind: { | ||
type: 'string', | ||
description: 'Warning! This type is overrided', | ||
enum: ['masteringAndDistribution', 'video', 'samples', 'mastering', 'distribution', 'sessions'], | ||
}, | ||
}, | ||
]; | ||
convertToTypes({ | ||
json, | ||
fileName: 'typesAPI', | ||
folderPath: 'src/types/generated', | ||
swaggerVersion: 3, | ||
overrideSchemas, | ||
}); | ||
convertToMocks({ | ||
json, | ||
fileName: 'mocksAPI', | ||
folderPath: 'src/mocks/generated', | ||
typesPath: '../../types/generated/typesAPI', | ||
swaggerVersion: 3, | ||
}); | ||
} | ||
main(); | ||
``` | ||
## AllInOne Example: | ||
@@ -65,11 +129,11 @@ | ||
async function main() { | ||
const json = await fetchSwaggerJsonFile(petShopLink); | ||
convertToTypes({ json, fileName: 'typesAPI', folderPath: 'src/types/generated', swaggerVersion: 3 }); | ||
convertToMocks({ | ||
json, | ||
fileName: 'mocksAPI', | ||
folderPath: 'src/mocks/generated', | ||
typesPath: '../../types/generated/typesAPI', | ||
swaggerVersion: 3, | ||
}); | ||
const json = await fetchSwaggerJsonFile(petShopLink); | ||
convertToTypes({ json, fileName: 'typesAPI', folderPath: 'src/types/generated', swaggerVersion: 3 }); | ||
convertToMocks({ | ||
json, | ||
fileName: 'mocksAPI', | ||
folderPath: 'src/mocks/generated', | ||
typesPath: '../../types/generated/typesAPI', | ||
swaggerVersion: 3, | ||
}); | ||
} | ||
@@ -83,7 +147,9 @@ | ||
#### fetchSwaggerJsonFile(url) | ||
`url`: `string` - url to swagger json file | ||
`url`: `string` - url to swagger json file | ||
Returns a swagger json object; | ||
#### convertToTypes({ json, fileName, folderPath, swaggerVersion }) | ||
`json`: `object` - swagger json data; | ||
@@ -97,6 +163,7 @@ `fileName`: `string` - name of the file, where typescript types data will be saved; | ||
#### convertToMocks({ json, fileName, folderPath, typesPath, swaggerVersion }) | ||
`json`: `object` - swagger json data; | ||
`fileName`: `string` - name of the file, where mocks data will be saved; | ||
`folderPath`: `string` - folder path the `fileName`, where mocks data will be saved; | ||
`typesPath`: `string` - folder path to `types`, where typescript types data are saved. | ||
`typesPath`: `string` - folder path to `types`, where typescript types data are saved. | ||
Path to `types` will be inserted to the type imports in generated mocks (generated -> import {...} from `typesPath`;); | ||
@@ -103,0 +170,0 @@ `swaggerVersion`: `number` - version of the swagger json file (specification); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
68019
1232
169