UNPKG

swagger-typescript-generator

Version:

Generate typescript code from swagger.json. Before start this project I try use [swagger-typescript-codegen](https://github.com/mtennoe/swagger-typescript-codegen) based on [mustache templates](https://github.com/mtennoe/swagger-js-codegen/tree/master/lib

151 lines 6.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.defaultUtils = void 0; var utils_1 = require("./utils"); var JS_TYPES = ['number', 'boolean', 'string', 'array', 'file', 'date', 'object', 'void']; var getModelName = function (name) { var newName = ("" + name).replace(/[\[\]\.]/g, ''); return utils_1.capitalize(newName); }; var getMethodName = function (name) { return ("" + name) .replace(/[\[\]_\. ]/g, ' ') .split(' ') .map(function (x, idx) { return (idx === 0 ? utils_1.lowerlize(x) : utils_1.capitalize(x)); }) .join(''); }; var getEnumName = function (name) { var newName = ("" + name).replace(/[\[\]\.]/g, ''); newName = newName .split('_') .map(function (s) { return utils_1.capitalize(s); }) .join(''); return newName; }; var getFileName = function (name) { name = name.replace(/[\[\]\.]/g, ''); var words = name.split(/(?=[A-Z])/).map(function (i) { return i.toLowerCase(); }); words = words.filter(function (f) { return !['api', 'i'].includes(f); }); return words.join('-') + ".ts"; }; var getJsTypeInternal = function (type, schema) { if (type === 'integer') { return 'number'; } if (type === 'string' && schema.format === 'date-time') { return 'Date'; } if (type === 'array' || (schema === null || schema === void 0 ? void 0 : schema.type) === 'array') { var itemType = schema && schema['items'] ? getJsType(schema['items']) : 'any'; return "Array<" + itemType + ">"; } if (type === 'file') { return 'File'; } if (type && type.indexOf('#') >= 0) { var parts = type.split('/'); return getModelName("" + parts[parts.length - 1]); } return type; }; var getJsType = function (schema) { var res = ''; if (schema.$ref) { return getJsTypeInternal(schema.$ref, schema); } if (schema['schema']) { var res_1 = getJsTypeInternal(schema['schema'].$ref, schema['schema']); if (res_1) { return res_1; } return getJsTypeInternal(schema['schema'].type, schema['schema']); } var itemsRef = schema.items ? schema.items['$ref'] : schema['$ref']; if (itemsRef) { res = getJsTypeInternal(itemsRef, schema); } if (!res) { var additionalProperties = schema.additionalProperties; if (additionalProperties && additionalProperties['type']) { if (additionalProperties['items']) { res = getJsTypeInternal(additionalProperties['items'].$ref, additionalProperties['items']); } else { res = getJsTypeInternal(additionalProperties['type'], additionalProperties); } } } if (!res) { res = getJsTypeInternal(schema.type, schema); } return res; }; exports.defaultUtils = { getClassName: function (context, key) { var parts = key .replace(/[\{\}]/g, '') .replace(/[-_]/g, '/') .split('/'); var tmpName = parts .filter(function (f) { return f !== 'api'; }) .map(function (s) { return utils_1.capitalize(s); }) .join(''); return tmpName.endsWith('Api') ? tmpName : tmpName + 'Api'; }, getPathName: function (context, key) { var parts = key .replace(/[\{\}]/g, '') .replace(/[-_]/g, '/') .split('/'); var tmpName = parts .filter(function (f) { return f !== 'api'; }) .map(function (s) { return utils_1.capitalize(s); }) .join(''); return tmpName.endsWith('Api') ? tmpName : tmpName + 'Api'; }, getClassFileName: function (context, name) { return getFileName(name); }, getMethodName: function (context, name) { return getMethodName(name); }, getMethodParameterName: function (context, name) { return name; }, getMethodResponseType: function (context, schema) { return getJsType(schema); }, getMethodParameterType: function (context, schema) { return getJsType(schema); }, getModelName: function (context, name) { return getModelName(name); }, getModelFileName: function (context, name) { return getFileName(name); }, getModelType: function (context, schema) { return getJsType(schema); }, getModelPropertyType: function (context, schema) { return getJsType(schema); }, getEnumName: function (name) { return getEnumName(name); }, getEnumFileName: function (context, name) { return getFileName(name); }, escapeMethodQueryParameterName: function (name) { return ("" + name).replace(/[\[\]\.]/g, ''); }, isArray: function (schema) { var _a; return (schema && schema.type === 'array') || ((_a = schema === null || schema === void 0 ? void 0 : schema.schema) === null || _a === void 0 ? void 0 : _a.type) === 'array'; }, isEnum: function (schema) { var _a, _b; return ((_b = (_a = schema === null || schema === void 0 ? void 0 : schema.items) === null || _a === void 0 ? void 0 : _a.enum) === null || _b === void 0 ? void 0 : _b.length) > 0 || (schema === null || schema === void 0 ? void 0 : schema.enum) ? true : false; }, isJsType: function (name) { return JS_TYPES.includes(name.toLowerCase()) || name.toLowerCase().indexOf('array<') === 0; }, getArrayItemType: function (schema) { var type = getJsType(schema); if (type && type.indexOf('Array') === 0) { return type.replace(/Array/g, '').replace(/[\<\>]/g, ''); } return ''; }, getEnumValues: function (schema) { var _a; if (schema.enum) { return schema.enum; } else if ((_a = schema.items) === null || _a === void 0 ? void 0 : _a.enum) { return schema.items.enum; } }, getModelType2: function (schema) { return null; }, }; //# sourceMappingURL=default-utils.js.map