Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

openapi-codegen-typescript

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

openapi-codegen-typescript - npm Package Compare versions

Comparing version 0.1.7 to 0.1.8

dist/MockGenerateHelper.d.ts

12

CHANGELOG.md

@@ -0,1 +1,13 @@

# v0.1.8 (Thu Jul 30 2020)
#### 🐛 Bug Fix
- fix(mocks-codegen): fix email and duration mocks, fix multiple extend DTOs mocks, add and use 'indefinite' npm package [#9](https://github.com/LandrAudio/openapi-codegen-typescript/pull/9) ([@nemrosim](https://github.com/nemrosim))
#### Authors: 1
- Artem Diashkin ([@nemrosim](https://github.com/nemrosim))
---
# (Mon Jul 20 2020)

@@ -2,0 +14,0 @@

42

dist/mockConverter.d.ts
import { ConvertToMocksProps, GetSchemasProps } from './types';
export declare const getSchemaInterfaces: (schema: any) => string[] | undefined;
export declare const combineProperties: ({ schema, schemas, interfaces }: any) => any;
export declare const convertRefType: ({ propertyName, ref, isArray, }: {
propertyName: string;
ref: any;
isArray?: boolean | undefined;
}) => {
propertyName: string;
value: string;
};
export declare const parseSchema: ({ schema, name, DTOs }: {
/**
* Get all interfaces that this schema exteds
* @param schema DTO schema
* @param DTOs all DTOs
*/
export declare const getSchemaInterfaces: (schema: any, DTOs: any) => string[] | undefined;
interface CombinePropertiesProps {
schema: any;
schemas: any;
interfaces: Array<string>;
}
/**
* Combines all properties from extended DTOs into one object.
*
* @param schema
* @param schemas
* @param interfaces
*/
export declare const combineProperties: ({ schema, schemas, interfaces }: CombinePropertiesProps) => any;
interface ParseSchemaProps {
schema: any;
/**
* DTO name
* Examples: MembersEmailDto, InviteMembersRequestDto, InviteAssetsMembersRequestDto
*/
name: any;
/**
* All parsed DTOs from swagger json file
*/
DTOs?: any;
}) => string;
}
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 {};
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -9,43 +20,37 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

var casual_1 = __importDefault(require("casual"));
var parseRefType = function (refType) { return refType[refType.length - 1]; };
var getVariables = function (varNamesAndValues) {
return varNamesAndValues.map(function (mock) { return " " + mock.propertyName + ": " + mock.value + ","; }).join('\n');
var MockGenerateHelper_1 = require("./MockGenerateHelper");
var getIsSchemaContainsAllOfArray = function (schema) {
return schema && schema[types_1.SwaggerProps.AllOf] && Array.isArray(schema[types_1.SwaggerProps.AllOf]);
};
var isAn = function (word) {
var symbol = word[0].toLowerCase();
var isAn = symbol === 'a' || symbol === 'e' || symbol === 'i' || symbol === 'o' || symbol === 'y' || symbol === 'u';
return isAn ? 'an' : 'a';
};
var getMockTemplateString = function (_a) {
var typeName = _a.typeName, varNamesAndValues = _a.varNamesAndValues;
return "\nexport const " + isAn(typeName) + typeName + "API = (overrides?: Partial<" + typeName + ">): " + typeName + " => {\n return {\n " + getVariables(varNamesAndValues) + "\n ...overrides,\n };\n};\n";
};
function getStringFakeValue(_a) {
var name = _a.name, propertyName = _a.propertyName, format = _a.format, minLength = _a.minLength, maxLength = _a.maxLength;
casual_1.default.seed(shared_1.hashedString(name + propertyName));
if (!format) {
return "'" + propertyName + "-" + name.toLowerCase() + "'";
}
else if (format === types_1.StringFormats.Guid || propertyName === types_1.PropertyNames.Id) {
return "'" + casual_1.default.uuid + "'";
}
else {
switch (format) {
case types_1.StringFormats.DateTime: {
return "'2019-06-10T06:20:01.389Z'";
/**
* Get all interfaces that this schema exteds
* @param schema DTO schema
* @param DTOs all DTOs
*/
exports.getSchemaInterfaces = function (schema, DTOs) {
if (getIsSchemaContainsAllOfArray(schema)) {
var result_1 = [];
schema[types_1.SwaggerProps.AllOf]
.filter(function (e) { return e[types_1.SwaggerProps.$ref]; })
.forEach(function (obj) {
var _a;
var refType = obj[types_1.SwaggerProps.$ref].split('/');
/*
Example: will return InviteMembersRequestDto
if "SomeDTo extends InviteMembersRequestDto"
*/
var parsedRefType = MockGenerateHelper_1.MockGenerateHelper.parseRefType(refType);
// Repeat "getSchemaInterfaces" in cycle for inner interfaces
var newSchema = DTOs[parsedRefType];
if (getIsSchemaContainsAllOfArray(newSchema)) {
(_a = exports.getSchemaInterfaces(newSchema, DTOs)) === null || _a === void 0 ? void 0 : _a.forEach(function (b) {
result_1.push(b);
});
}
case types_1.StringFormats.Date: {
return "'2019-06-10'";
else if (newSchema) {
result_1.push(parsedRefType);
}
}
}
}
exports.getSchemaInterfaces = function (schema) {
if (schema[types_1.SwaggerProps.AllOf] && Array.isArray(schema[types_1.SwaggerProps.AllOf])) {
return schema[types_1.SwaggerProps.AllOf]
.filter(function (e) { return e[types_1.SwaggerProps.$ref]; })
.map(function (obj) {
var refType = obj[types_1.SwaggerProps.$ref].split('/');
return parseRefType(refType);
result_1.push(parsedRefType);
});
return result_1;
}

@@ -56,124 +61,87 @@ else {

};
/**
* Combines all properties from extended DTOs into one object.
*
* @param schema
* @param schemas
* @param interfaces
*/
exports.combineProperties = function (_a) {
var schema = _a.schema, schemas = _a.schemas, interfaces = _a.interfaces;
if (interfaces) {
var properties_1 = Object.assign({}, schema.properties);
interfaces.map(function (interfaceName) {
var dto = schemas[interfaceName];
if (dto && dto.properties) {
properties_1 = Object.assign(properties_1, dto.properties);
var combinedProperties = __assign({}, schema.properties);
interfaces.forEach(function (interfaceName) {
var dto = schemas[interfaceName];
if (dto) {
var extendedDtoProps = void 0;
if (dto.properties) {
extendedDtoProps = dto.properties;
}
});
schema.properties = properties_1;
return Object.assign({}, schema);
}
else {
return schema;
}
else if (dto[types_1.SwaggerProps.AllOf] && dto[types_1.SwaggerProps.AllOf][1].properties) {
extendedDtoProps = dto[types_1.SwaggerProps.AllOf][1].properties;
}
combinedProperties = __assign(__assign({}, combinedProperties), extendedDtoProps);
}
});
schema.properties = combinedProperties;
return __assign({}, schema);
};
exports.convertRefType = function (_a) {
var propertyName = _a.propertyName, ref = _a.ref, _b = _a.isArray, isArray = _b === void 0 ? false : _b;
if (isArray) {
return { propertyName: propertyName, value: ownPropString(propertyName, "[" + isAn(ref) + ref + "API()]") };
}
else {
return { propertyName: propertyName, value: ownPropString(propertyName, "" + isAn(ref) + ref + "API()") };
}
};
var ownPropString = function (propName, result) {
return "overrides?." + propName + " || " + result;
};
exports.parseSchema = function (_a) {
var schema = _a.schema, name = _a.name, DTOs = _a.DTOs;
var parseSwaggerJsonObject = function (obj, interfaces) {
obj = exports.combineProperties({ schema: obj, schemas: DTOs, interfaces: interfaces });
if (interfaces) {
obj = exports.combineProperties({ schema: obj, schemas: DTOs, interfaces: interfaces });
}
var mocks = [];
if (obj.properties) {
shared_1.getSchemaProperties(obj.properties).map(function (_a) {
var propertyName = _a.propertyName, $ref = _a.$ref, items = _a.items, type = _a.type, format = _a.format, maxLength = _a.maxLength, minLength = _a.minLength, oneOf = _a.oneOf, minimum = _a.minimum, maximum = _a.maximum;
var schemaProperties = shared_1.getSchemaProperties(obj.properties);
schemaProperties.forEach(function (props) {
var propertyName = props.propertyName, $ref = props.$ref, items = props.items, type = props.type, format = props.format, maxLength = props.maxLength, minLength = props.minLength, oneOf = props.oneOf, minimum = props.minimum, maximum = props.maximum;
casual_1.default.seed(shared_1.hashedString(name + propertyName));
var mockGenerator = new MockGenerateHelper_1.MockGenerateHelper(casual_1.default);
if (type === types_1.DataTypes.String) {
mocks.push({
var stringMock = mockGenerator.getStringMock({
name: name,
propertyName: propertyName,
value: getStringFakeValue({ name: name, propertyName: propertyName, format: format, minLength: minLength, maxLength: maxLength }),
format: format,
minLength: minLength,
maxLength: maxLength,
});
mocks.push(stringMock);
}
if (type === types_1.DataTypes.Integer || type === types_1.DataTypes.Number) {
mocks.push({
propertyName: propertyName,
value: type === types_1.DataTypes.Integer
? casual_1.default.integer(minimum || 0, maximum || 30)
: casual_1.default.double(minimum || 0, maximum || 30),
});
var numberMock = mockGenerator.getNumberMock({ propertyName: propertyName, type: type, minimum: minimum, maximum: maximum });
mocks.push(numberMock);
}
if (type === types_1.DataTypes.Boolean) {
mocks.push({ propertyName: propertyName, value: true });
var boolMock = mockGenerator.getBooleanMock(propertyName);
mocks.push(boolMock);
}
if (type === types_1.DataTypes.Array && items) {
if (items[types_1.SwaggerProps.$ref]) {
var refType = items[types_1.SwaggerProps.$ref].split('/');
var ref = parseRefType(refType);
var schema_1 = DTOs[ref];
if (schema_1 && schema_1.enum) {
mocks.push({ propertyName: propertyName, value: "['" + schema_1.enum[0] + "']" });
}
else {
mocks.push(exports.convertRefType({ propertyName: propertyName, ref: ref, isArray: true }));
}
}
else {
var type_1 = items.oneOf
? parseRefType(items.oneOf[0][types_1.SwaggerProps.$ref].split('/'))
: items.type;
if (items.oneOf) {
var schema_2 = DTOs[type_1];
if (schema_2 && schema_2.enum) {
mocks.push({ propertyName: propertyName, value: "['" + schema_2.enum[0] + "']" });
}
}
else {
if (items.type === types_1.DataTypes.Number) {
mocks.push({ propertyName: propertyName, value: "[" + casual_1.default.double() + "," + casual_1.default.double() + "]" });
}
else {
mocks.push({ propertyName: propertyName, value: "['" + casual_1.default.word + "']" });
}
}
}
var arrayOfItemsMock = mockGenerator.getArrayOfItemsMock({
propertyName: propertyName,
items: items,
DTOs: DTOs,
});
mocks.push(arrayOfItemsMock);
}
if (oneOf && Array.isArray(oneOf) && oneOf[0][types_1.SwaggerProps.$ref]) {
var refType = oneOf[0][types_1.SwaggerProps.$ref].split('/');
var ref = parseRefType(refType);
var schema_3 = DTOs[ref];
if (schema_3 && schema_3.enum) {
mocks.push({ propertyName: propertyName, value: "'" + schema_3.enum[0] + "'" });
}
else {
mocks.push(exports.convertRefType({ propertyName: propertyName, ref: ref }));
}
var arrayOneOf = mockGenerator.getDtoMock({
propertyName: propertyName,
oneOf: oneOf,
DTOs: DTOs,
});
mocks.push(arrayOneOf);
}
if ($ref) {
var refType = $ref.split('/');
var ref = parseRefType(refType);
var schema_4 = DTOs[ref];
if (schema_4 && schema_4.enum) {
mocks.push({ propertyName: propertyName, value: "'" + schema_4.enum[0] + "'" });
}
else if (schema_4) {
mocks.push(exports.convertRefType({ propertyName: propertyName, ref: ref }));
}
else {
mocks.push({
propertyName: "ERROR in " + propertyName + " ref:" + ref,
value: 'NULL',
});
}
var ref = mockGenerator.getRefTypeMock({ $ref: $ref, propertyName: propertyName, DTOs: DTOs });
mocks.push(ref);
}
});
}
return getMockTemplateString({ typeName: name, varNamesAndValues: mocks });
return MockGenerateHelper_1.MockGenerateHelper.getMockTemplateString({ typeName: name, varNamesAndValues: mocks });
};
if (schema[types_1.SwaggerProps.AllOf] && Array.isArray(schema[types_1.SwaggerProps.AllOf])) {
var interfaces = exports.getSchemaInterfaces(schema);
return parseSwaggerJsonObject(schema.allOf.find(function (schema) { return schema.type; }), interfaces);
var interfaces = exports.getSchemaInterfaces(schema, DTOs);
var object = schema.allOf.find(function (schema) { return schema.type; });
return parseSwaggerJsonObject(object, interfaces);
}

@@ -189,3 +157,3 @@ else {

var resultString = '';
DTOs.map(function (dtoName) {
DTOs.forEach(function (dtoName) {
try {

@@ -192,0 +160,0 @@ var schema = schemas[dtoName];

@@ -29,3 +29,4 @@ export declare enum PropertyNames {

Ipv4 = "ipv4",
Ipv6 = "ipv6"
Ipv6 = "ipv6",
TimeSpan = "time-span"
}

@@ -48,4 +49,9 @@ export declare enum StringAdditionalProps {

}
export interface ResultStringProps {
export interface PropertyNameProp {
/**
* DTO\'s property name
*/
propertyName: string;
}
export interface ResultStringProps extends PropertyNameProp {
nullable?: boolean;

@@ -94,6 +100,28 @@ }

}
export interface MockArrayProps {
propertyName: string;
export interface MockArrayProps extends PropertyNameProp {
value: any;
}
export interface GetStringMockProps extends PropertyNameProp {
name: string;
format: string;
minLength: number;
maxLength: number;
}
export interface GetNumberMockProps extends PropertyNameProp {
type: DataTypes.Integer | DataTypes.Number;
minimum: number;
maximum: number;
}
export interface GetArrayOfItemsMockProps extends PropertyNameProp {
items: any;
DTOs: any;
}
export interface GetArrayOfOneOfMockProps extends PropertyNameProp {
oneOf: any;
DTOs: any;
}
export interface GetRefTypeMockProps extends PropertyNameProp {
$ref: string;
DTOs: any;
}
export interface ParseProps {

@@ -109,13 +137,8 @@ schema: any;

}
export interface Props {
swaggerJsonUrl: string;
export interface ConvertRefType extends PropertyNameProp {
/**
* Example: './src/types'
* Reference to another object DTO
*/
folderPath: string;
/**
* Example: swagger-profiles
*/
fileName: string;
shouldGenerateMocks?: boolean;
ref: string;
isArray?: boolean;
}

@@ -36,2 +36,3 @@ "use strict";

StringFormats["Ipv6"] = "ipv6";
StringFormats["TimeSpan"] = "time-span";
})(StringFormats = exports.StringFormats || (exports.StringFormats = {}));

@@ -38,0 +39,0 @@ var StringAdditionalProps;

{
"name": "openapi-codegen-typescript",
"version": "0.1.7",
"version": "0.1.8",
"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.47.0",
"auto": "^9.49.1",
"eslint": "6.8.0",

@@ -59,3 +59,5 @@ "eslint-config-prettier": "6.10.0",

"dependencies": {
"@types/indefinite": "^2.3.0",
"casual": "1.6.2",
"indefinite": "^2.3.2",
"node-fetch": "2.6.0"

@@ -62,0 +64,0 @@ },

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc