openapi-codegen-typescript
Advanced tools
Comparing version 0.1.15 to 0.1.16
@@ -0,1 +1,13 @@ | ||
# v0.1.16 (Tue Dec 08 2020) | ||
#### 🐛 Bug Fix | ||
- fix(type): fix mocks codegen for the Swagger v2 version `Type[AnotherType]` annotation (part 1) [#22](https://github.com/LandrAudio/openapi-codegen-typescript/pull/22) ([@nemrosim](https://github.com/nemrosim)) | ||
#### Authors: 1 | ||
- Artem Diashkin ([@nemrosim](https://github.com/nemrosim)) | ||
--- | ||
# v0.1.15 (Tue Dec 01 2020) | ||
@@ -2,0 +14,0 @@ |
@@ -27,3 +27,3 @@ import { ConvertToMocksProps, EnumSchema, GetSchemasProps } from './types'; | ||
*/ | ||
name: any; | ||
name: string; | ||
/** | ||
@@ -36,4 +36,4 @@ * All parsed DTOs from swagger json file | ||
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 declare const parseSchemas: ({ json, overrideSchemas }: GetSchemasProps) => string; | ||
export declare const convertToMocks: ({ json, fileName, folderPath, typesPath, overrideSchemas, }: ConvertToMocksProps) => string; | ||
export {}; |
@@ -99,2 +99,5 @@ "use strict"; | ||
xDictionaryKey = props.xDictionaryKey, additionalProperties = props.additionalProperties; | ||
if (name.includes('[') && name.includes(']')) { | ||
name = name.split('[')[0]; | ||
} | ||
casual_1.default.seed(shared_1.hashedString(name + propertyName)); | ||
@@ -167,4 +170,4 @@ var mockGenerator = new MockGenerateHelper_1.MockGenerateHelper(casual_1.default); | ||
exports.parseSchemas = function (_a) { | ||
var json = _a.json, swaggerVersion = _a.swaggerVersion, overrideSchemas = _a.overrideSchemas; | ||
var schemas = shared_1.getSchemas({ json: json, swaggerVersion: swaggerVersion }); | ||
var json = _a.json, overrideSchemas = _a.overrideSchemas; | ||
var schemas = shared_1.getSchemas({ json: json }); | ||
var DTOs = Object.keys(schemas); | ||
@@ -193,9 +196,19 @@ var resultString = ''; | ||
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, overrideSchemas = _a.overrideSchemas; | ||
var schemas = shared_1.getSchemas({ json: json, swaggerVersion: swaggerVersion }); | ||
var imports = Object.keys(schemas).join(', '); | ||
var json = _a.json, fileName = _a.fileName, folderPath = _a.folderPath, typesPath = _a.typesPath, overrideSchemas = _a.overrideSchemas; | ||
var schemas = shared_1.getSchemas({ json: json }); | ||
var imports = Object.keys(schemas) | ||
.map(function (dtoName) { | ||
// Sometimes in swagger 2.0 version could be such name as SomeDto[AnotherDto] | ||
if (shared_1.isSwaggerV2(json) && dtoName.includes('[') && dtoName.includes(']')) { | ||
return dtoName.split('[')[0]; | ||
} | ||
else { | ||
return dtoName; | ||
} | ||
}) | ||
.join(', '); | ||
var disableNoUse = '/* eslint-disable @typescript-eslint/no-use-before-define */\n'; | ||
var disableNoUsedVars = '/* eslint-disable @typescript-eslint/no-unused-vars */\n'; | ||
var importsDescription = "import {" + imports + "} from '" + typesPath + "';\n"; | ||
var result = exports.parseSchemas({ json: json, swaggerVersion: swaggerVersion, overrideSchemas: overrideSchemas }); | ||
var result = exports.parseSchemas({ json: json, overrideSchemas: overrideSchemas }); | ||
var resultString = "" + disableNoUse + disableNoUsedVars + importsDescription + result; | ||
@@ -202,0 +215,0 @@ shared_1.writeToFile({ |
@@ -1,2 +0,2 @@ | ||
import { GetSchemasProps } from './types'; | ||
import { GetSchemasProps, SwaggerV2, SwaggerV3 } from './types'; | ||
export declare const getSchemaProperties: (objectProps: any) => { | ||
@@ -29,3 +29,5 @@ propertyName: string; | ||
export declare const hashedString: (string: string) => number; | ||
export declare const getSchemas: ({ json, swaggerVersion }: GetSchemasProps) => any; | ||
export declare function isSwaggerV3(json: SwaggerV2 | SwaggerV3): json is SwaggerV3; | ||
export declare function isSwaggerV2(json: SwaggerV2 | SwaggerV3): json is SwaggerV2; | ||
export declare const getSchemas: ({ json }: GetSchemasProps) => any; | ||
export declare const writeToFile: ({ folderPath, fileName, resultString, }: { | ||
@@ -32,0 +34,0 @@ folderPath: string; |
@@ -95,13 +95,24 @@ "use strict"; | ||
}; | ||
function isSwaggerV3(json) { | ||
var _a; | ||
return Boolean((_a = json.openapi) === null || _a === void 0 ? void 0 : _a.match(/^3.*/)) && Boolean(json.components); | ||
} | ||
exports.isSwaggerV3 = isSwaggerV3; | ||
function isSwaggerV2(json) { | ||
var _a; | ||
return Boolean((_a = json.swagger) === null || _a === void 0 ? void 0 : _a.match(/^2.*/)) && Boolean(json.definitions); | ||
} | ||
exports.isSwaggerV2 = isSwaggerV2; | ||
exports.getSchemas = function (_a) { | ||
var json = _a.json, _b = _a.swaggerVersion, swaggerVersion = _b === void 0 ? 3 : _b; | ||
var _c, _d; | ||
switch (swaggerVersion) { | ||
case 3: | ||
return (_c = json === null || json === void 0 ? void 0 : json.components) === null || _c === void 0 ? void 0 : _c.schemas; | ||
case 2: | ||
return json === null || json === void 0 ? void 0 : json.definitions; | ||
default: | ||
return (_d = json === null || json === void 0 ? void 0 : json.components) === null || _d === void 0 ? void 0 : _d.schemas; | ||
var json = _a.json; | ||
var _b; | ||
if (isSwaggerV3(json)) { | ||
return (_b = json === null || json === void 0 ? void 0 : json.components) === null || _b === void 0 ? void 0 : _b.schemas; | ||
} | ||
else if (isSwaggerV2(json)) { | ||
return json === null || json === void 0 ? void 0 : json.definitions; | ||
} | ||
else { | ||
throw Error('Schema parse exception. Unsupported version'); | ||
} | ||
}; | ||
@@ -108,0 +119,0 @@ exports.writeToFile = function (_a) { |
@@ -71,21 +71,23 @@ export declare enum PropertyNames { | ||
folderPath: string; | ||
swaggerVersion: number; | ||
overrideSchemas?: Array<EnumSchema>; | ||
} | ||
export interface ConvertToMocksProps { | ||
json: any; | ||
json: SwaggerV2 | SwaggerV3; | ||
fileName: string; | ||
folderPath: string; | ||
typesPath: string; | ||
swaggerVersion: number; | ||
overrideSchemas?: Array<EnumSchema>; | ||
} | ||
export interface SwaggerV2 { | ||
swagger: '2.0'; | ||
definitions?: any; | ||
} | ||
export interface SwaggerV3 { | ||
openapi: '3.0.0'; | ||
components?: { | ||
schemas?: any; | ||
}; | ||
} | ||
export interface GetSchemasProps { | ||
json: { | ||
components?: { | ||
schemas?: any; | ||
}; | ||
definitions?: any; | ||
}; | ||
swaggerVersion?: number; | ||
json: SwaggerV2 | SwaggerV3; | ||
overrideSchemas?: Array<EnumSchema>; | ||
@@ -92,0 +94,0 @@ } |
@@ -11,3 +11,3 @@ import { ParseProps, ConvertToTypesProps, GetSchemasProps } from './types'; | ||
export declare const parseEnum: ({ schema, schemaKey }: ParseProps) => string; | ||
export declare const parseSchemas: ({ json, swaggerVersion, overrideSchemas }: GetSchemasProps) => string; | ||
export declare const convertToTypes: ({ json, fileName, folderPath, swaggerVersion, overrideSchemas, }: ConvertToTypesProps) => void; | ||
export declare const parseSchemas: ({ json, overrideSchemas }: GetSchemasProps) => string; | ||
export declare const convertToTypes: ({ json, fileName, folderPath, overrideSchemas }: ConvertToTypesProps) => void; |
@@ -216,4 +216,4 @@ "use strict"; | ||
exports.parseSchemas = function (_a) { | ||
var json = _a.json, swaggerVersion = _a.swaggerVersion, overrideSchemas = _a.overrideSchemas; | ||
var schemas = shared_1.getSchemas({ json: json, swaggerVersion: swaggerVersion }); | ||
var json = _a.json, overrideSchemas = _a.overrideSchemas; | ||
var schemas = shared_1.getSchemas({ json: json }); | ||
if (schemas) { | ||
@@ -232,3 +232,3 @@ var schemasKeys = Object.keys(schemas); | ||
*/ | ||
if (swaggerVersion === 2 && schemaKey.includes('[') && schemaKey.includes(']')) { | ||
if (shared_1.isSwaggerV2(json) && schemaKey.includes('[') && schemaKey.includes(']')) { | ||
var strings = schemaKey.split('['); | ||
@@ -272,4 +272,4 @@ result_1 += exports.parseObject({ schema: schema, schemaKey: strings[0] }); | ||
exports.convertToTypes = function (_a) { | ||
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 }); | ||
var json = _a.json, fileName = _a.fileName, folderPath = _a.folderPath, overrideSchemas = _a.overrideSchemas; | ||
var resultString = exports.parseSchemas({ json: json, overrideSchemas: overrideSchemas }); | ||
shared_1.writeToFile({ | ||
@@ -276,0 +276,0 @@ folderPath: folderPath, |
{ | ||
"name": "openapi-codegen-typescript", | ||
"version": "0.1.15", | ||
"version": "0.1.16", | ||
"description": "OpenApi codegen for generating types an mocks from swagger json file", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -34,3 +34,3 @@ # OpenApi-codegen-typescript | ||
const json = await fetchSwaggerJsonFile('https://custom/swagger.json'); | ||
convertToTypes({ json, fileName: 'dtoAPI', folderPath: 'src/types/generated', swaggerVersion: 3 }); | ||
convertToTypes({ json, fileName: 'dtoAPI', folderPath: 'src/types/generated' }); | ||
} | ||
@@ -41,3 +41,2 @@ ``` | ||
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. | ||
@@ -57,4 +56,3 @@ - For generating mock files that are using converted types | ||
folderPath: 'src/mocks/generated', | ||
typesPath: '../../types/generated/dtoAPI', | ||
swaggerVersion: 3, | ||
typesPath: '../../types/generated/dtoAPI' | ||
}); | ||
@@ -66,3 +64,2 @@ } | ||
('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. | ||
@@ -109,4 +106,3 @@ ## Overriding enum schema type | ||
folderPath: 'src/types/generated', | ||
swaggerVersion: 3, | ||
overrideSchemas, | ||
overrideSchemas | ||
}); | ||
@@ -117,4 +113,3 @@ convertToMocks({ | ||
folderPath: 'src/mocks/generated', | ||
typesPath: '../../types/generated/typesAPI', | ||
swaggerVersion: 3, | ||
typesPath: '../../types/generated/typesAPI' | ||
}); | ||
@@ -135,3 +130,3 @@ } | ||
const json = await fetchSwaggerJsonFile(petShopLink); | ||
convertToTypes({ json, fileName: 'typesAPI', folderPath: 'src/types/generated', swaggerVersion: 3 }); | ||
convertToTypes({ json, fileName: 'typesAPI', folderPath: 'src/types/generated' }); | ||
convertToMocks({ | ||
@@ -141,4 +136,3 @@ json, | ||
folderPath: 'src/mocks/generated', | ||
typesPath: '../../types/generated/typesAPI', | ||
swaggerVersion: 3, | ||
typesPath: '../../types/generated/typesAPI' | ||
}); | ||
@@ -158,3 +152,3 @@ } | ||
#### convertToTypes({ json, fileName, folderPath, swaggerVersion }) | ||
#### convertToTypes({ json, fileName, folderPath }) | ||
@@ -164,7 +158,6 @@ `json`: `object` - swagger json data; | ||
`folderPath`: `string` - folder path the `fileName`, where typescript types data will be saved; | ||
`swaggerVersion`: `number` - version of the swagger json file (specification). `3` by default; | ||
Returns `void`; | ||
#### convertToMocks({ json, fileName, folderPath, typesPath, swaggerVersion }) | ||
#### convertToMocks({ json, fileName, folderPath, typesPath }) | ||
@@ -176,4 +169,3 @@ `json`: `object` - swagger json data; | ||
Path to `types` will be inserted to the type imports in generated mocks (generated -> import {...} from `typesPath`;); | ||
`swaggerVersion`: `number` - version of the swagger json file (specification); | ||
Returns `void`; |
68971
18
1279
161