Skip to content

Commit

Permalink
Updating dependencies (#116)
Browse files Browse the repository at this point in the history
* Updating depenencies

* Updating dependencies
  • Loading branch information
ikemtz authored Feb 28, 2024
1 parent 0a242a2 commit 973de5f
Show file tree
Hide file tree
Showing 13 changed files with 2,420 additions and 17,905 deletions.
2 changes: 1 addition & 1 deletion azure-ci.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 6.$(DayOfYear)$(Rev:.rr)
name: 7.$(DayOfYear)$(Rev:.rr)
pool:
demands: npm

Expand Down
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ module.exports = {
coverageReporters: ['html', 'json', 'lcov', 'text', 'clover', 'cobertura'],
maxConcurrency: 1,
testTimeout: 30000,
moduleNameMapper: {
"^axios$": "axios/dist/node/axios.cjs"
}
};
3,551 changes: 2,364 additions & 1,187 deletions package-lock.json

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,36 @@
],
"homepage": "https://github.com/ikemtz/OpenApi-TS-Generator#readme",
"devDependencies": {
"@types/jest": "^27.4.1",
"@types/lodash": "^4.14.182",
"@types/node": "^16.11.27",
"@types/pluralize": "^0.0.29",
"@typescript-eslint/eslint-plugin": "^5.32.0",
"@typescript-eslint/parser": "^5.32.0",
"eslint": "^8.21.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.26.0",
"@types/jest": "^27.5.2",
"@types/lodash": "^4.14.202",
"@types/node": "^20.11.21",
"@types/pluralize": "^0.0.33",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"@typescript-eslint/parser": "^7.1.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^6.0.0",
"eslint-plugin-promise": "^6.1.1",
"jest": "^27.5.1",
"jest-junit": "^13.2.0",
"openapi3-ts": "^2.0.2",
"prettier": "^2.7.1",
"openapi3-ts": "^4.2.2",
"prettier": "^3.2.5",
"rxjs": "^7.6.0",
"ts-jest": "^27.1.3",
"typescript": "^4.6.3"
"typescript": "^4.9.5"
},
"dependencies": {
"axios": ">=0.x",
"axios": ">=1.x",
"handlebars": ">=4.x",
"lodash": ">=4.x",
"pluralize": "^8.0.0"
},
"peerDependencies": {
"axios": ">=0.x",
"axios": ">=1.x",
"handlebars": ">=4.x",
"lodash": ">=4.x",
"rxjs": ">=6.x"
"rxjs": "*"
}
}
2 changes: 1 addition & 1 deletion src/generators/barrel.generator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync, mkdirSync, rmdirSync, unlinkSync } from 'fs';
import { existsSync, mkdirSync, rmdirSync } from 'fs';
import { IGeneratorOptions, setGeneratorOptionDefaults } from '../models/generator-options';
import { MockConsoleLogger } from '../models/logger';
import { BarrelGenerator } from './barrel.generator';
Expand Down
5 changes: 4 additions & 1 deletion src/generators/base.generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ export abstract class BaseGenerator<TContextSchema> {
public abstract readonly GeneratorName: string;
public readonly template?: HandlebarsTemplateDelegate<TContextSchema>;
public readonly emptyArrayRegex = /, ]/g;
public constructor(public readonly generatorOptions: IGeneratorOptions, public readonly templateFilePath: string | undefined) {
public constructor(
public readonly generatorOptions: IGeneratorOptions,
public readonly templateFilePath: string | undefined,
) {
if (templateFilePath) {
const templateSource = readFileSync(templateFilePath, { encoding: 'utf8' });
this.template = compile(templateSource);
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as fs from 'fs';
import { OpenAPIObject } from 'openapi3-ts';
import { OpenAPIObject } from 'openapi3-ts/dist/oas30';
import {
FormGenerator,
EndPointsGenerator,
Expand All @@ -13,7 +13,7 @@ import {
import { IGeneratorOptions, setGeneratorOptionDefaults } from './models/generator-options';
import { ITemplateData } from './models/template-data';
import { OpenApiDocConverter } from './openapidoc-converter';
import axios from 'axios';
import Axios from 'axios';

export { nrsrxTypeFilterCallBack, nrsrxValuePropertyTypeFilterCallBack } from './models/nrsrx-filters';

Expand All @@ -28,7 +28,7 @@ export async function generateTsModels(options: IGeneratorOptions): Promise<void
async function getOpenApiDocumentAsync(options: IGeneratorOptions): Promise<OpenAPIObject> {
let apiDoc: OpenAPIObject;
if (options.openApiJsonUrl) {
const response = await axios.get(options.openApiJsonUrl, options.axiosConfig);
const response = await Axios.get(options.openApiJsonUrl, options.axiosConfig);
apiDoc = response.data as OpenAPIObject;
} else if (options.openApiJsonFileName) {
const response = fs.readFileSync(`${__dirname}/${options.openApiJsonFileName}`);
Expand Down
2 changes: 1 addition & 1 deletion src/models/generator-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface IGeneratorOptions {
// This will give you the option to specify your own HandleBar templates
templates?: ITemplates | null;
// If specified this the AxiosRequestConfig that will be used to request the OpenApi document.
axiosConfig?: AxiosRequestConfig<any>;
axiosConfig?: AxiosRequestConfig<unknown>;
}
export interface ITemplates {
// Used if genClasses is set to false (default)
Expand Down
4 changes: 2 additions & 2 deletions src/models/schema-info.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReferenceObject, SchemaObject } from 'openapi3-ts';
import { ReferenceObject, SchemaObject } from 'openapi3-ts/dist/oas30';
import { IEnumValue } from './enum-value';
import { defaultFilter, IGeneratorOptions } from './generator-options';
import { IReferenceProperty } from './reference-property';
Expand All @@ -25,6 +25,6 @@ export class SchemaWrapperInfo {
}

public updateReferenceProperties(options: IGeneratorOptions): void {
this.referenceProperties = this.referenceProperties.filter(options.referencePropertyTypeFilterCallBack || defaultFilter);
this.referenceProperties = this.referenceProperties.filter(options.referencePropertyTypeFilterCallBack ?? defaultFilter);
}
}
2 changes: 1 addition & 1 deletion src/openapidoc-converter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OpenAPIObject } from 'openapi3-ts';
import { OpenAPIObject } from 'openapi3-ts/dist/oas30';
import { SchemaWrapperInfo } from './models/schema-info';
import { OpenApiDocConverter } from './openapidoc-converter';

Expand Down
42 changes: 23 additions & 19 deletions src/openapidoc-converter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { OpenAPIObject, PathItemObject, ReferenceObject, SchemaObject } from 'openapi3-ts';
import { defaultFilter, IGeneratorOptions } from './models/generator-options';
import { SchemaWrapperInfo } from './models/schema-info';
import { IImportType, IPath, ITemplateData } from './models/template-data';
Expand All @@ -7,11 +6,15 @@ import { IReferenceProperty } from './models/reference-property';
import { IValueProperty } from './models/value-property';
import { singular } from 'pluralize';
import { camelCase, kebabCase, snakeCase, startCase } from 'lodash';
import { OpenAPIObject, ReferenceObject, SchemaObject } from 'openapi3-ts/dist/oas30';

export class OpenApiDocConverter {
public readonly endAlphaNumRegex = /[A-z0-9]*$/s;
public readonly startNumberregex = /^\d*/;
constructor(private readonly options: IGeneratorOptions, private readonly apiDocument: OpenAPIObject) { }
constructor(
private readonly options: IGeneratorOptions,
private readonly apiDocument: OpenAPIObject,
) {}

public convertDocument(): ITemplateData {
const entities = this.convertEntities();
Expand All @@ -21,7 +24,7 @@ export class OpenApiDocConverter {
public convertPaths(): IPath[] {
const paths: IPath[] = [];
for (const key in this.apiDocument.paths) {
const path = (this.apiDocument.paths[key] as PathItemObject) || {};
const path = this.apiDocument.paths[key] || {};
let tagLookup = path.get || path.post || path.put;
tagLookup = tagLookup || path.delete || path.patch;
const tag: string = (tagLookup?.tags || ['unknown_endpoint'])[0];
Expand All @@ -38,7 +41,7 @@ export class OpenApiDocConverter {

for (const schemaName in this.apiDocument.components?.schemas) {
if (this.apiDocument.components?.schemas[schemaName]) {
const schemaWrapperInfo = new SchemaWrapperInfo(this.apiDocument.components?.schemas[schemaName]);
const schemaWrapperInfo = new SchemaWrapperInfo(this.apiDocument.components?.schemas[schemaName] as SchemaObject);
if (schemaWrapperInfo.componentSchemaObject.enum) {
this.buildSchemaWrapperInfoForEnum(schemaWrapperInfo);
} else {
Expand All @@ -51,9 +54,9 @@ export class OpenApiDocConverter {
typeof t === 'string' || t instanceof String
? t
: {
...t,
key: t.key || 0,
},
...t,
key: t.key || 0,
},
),
name: schemaName,
kebabCasedName: kebabCase(schemaName),
Expand All @@ -75,7 +78,7 @@ export class OpenApiDocConverter {
schemaWrapperInfo.enumValues.push(
...(schemaWrapperInfo.componentSchemaObject.enum || []).map((x: string) => {
const key = this.startNumberregex.exec(x)?.at(0);
const name = this.endAlphaNumRegex.exec(x)?.at(0) || '';
const name = this.endAlphaNumRegex.exec(x)?.at(0) ?? '';
return {
key: key ? +key : 0,
name,
Expand Down Expand Up @@ -181,7 +184,7 @@ export class OpenApiDocConverter {
const typescriptType = this.getPropertyTypeScriptType(schemaWrapperInfo);
const isRequired = this.getIsRequired(propertyName, schemaWrapperInfo);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const refName: string = ((schemaWrapperInfo?.componentSchemaObject?.properties || {})[propertyName] as SchemaObject).$ref;
const refName: string = ((schemaWrapperInfo?.componentSchemaObject?.properties || {})[propertyName] as ReferenceObject).$ref;
const refObject = (this.apiDocument.components?.schemas || {})[refName] as SchemaObject;
const defaultValue = (schemaWrapperInfo.componentSchemaObject.default || refObject?.default || (refObject?.enum || [])[0]) as string;
if (!isRequired) {
Expand All @@ -202,14 +205,14 @@ export class OpenApiDocConverter {
}
getInitialTestValue(parentTypeName: string, propertyName: string, schemaWrapperInfo: SchemaWrapperInfo): string {
const typescriptType = this.getPropertyTypeScriptType(schemaWrapperInfo);
const schemaObject = (schemaWrapperInfo?.componentSchemaObject?.properties || {})[propertyName] as SchemaObject;
const schemaObject = (schemaWrapperInfo?.componentSchemaObject?.properties ?? {})[propertyName] as SchemaObject;
const maxLength = schemaWrapperInfo.propertySchemaObject.maxLength;
const minLength = schemaWrapperInfo.propertySchemaObject.minLength;
const minValue = schemaWrapperInfo.propertySchemaObject.minimum;
const email = schemaWrapperInfo.propertySchemaObject.format?.toLowerCase() === 'email';
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const refName: string = schemaObject.$ref || schemaObject.items?.$ref;
const refObject = (this.apiDocument.components?.schemas || {})[refName] as SchemaObject;
const refName: string = (schemaObject as unknown as ReferenceObject)?.$ref || (schemaObject.items as unknown as ReferenceObject)?.$ref;
const refObject = (this.apiDocument.components?.schemas ?? {})[refName] as SchemaObject;
const defaultValue = (schemaWrapperInfo.componentSchemaObject.default || refObject?.default || (refObject?.enum || [])[0]) as string;
if (defaultValue && refObject.enum) {
return `${schemaWrapperInfo.propertyReferenceObject['$ref']}.${defaultValue.split(' ').pop() as string}`;
Expand Down Expand Up @@ -250,7 +253,7 @@ export class OpenApiDocConverter {
propertyName: string,
schemaWrapperInfo: SchemaWrapperInfo,
): IReferenceProperty {
const propertySchema: SchemaObject = (this.apiDocument.components?.schemas || {})[this.parseRef(schemaWrapperInfo)];
const propertySchema: SchemaObject = (this.apiDocument.components?.schemas || {})[this.parseRef(schemaWrapperInfo)] as SchemaObject;
const refSchema = (schemaWrapperInfo?.componentSchemaObject?.properties || {})[propertyName] as SchemaObject;
const required = this.getIsRequired(propertyName, schemaWrapperInfo);
const validatorCount = this.getValidatorCount(propertyName, schemaWrapperInfo);
Expand Down Expand Up @@ -298,15 +301,15 @@ export class OpenApiDocConverter {

public getPropertyTypeScriptType(schemaWrapperInfo: SchemaWrapperInfo): string {
if (schemaWrapperInfo.propertySchemaObject.type === 'array' && schemaWrapperInfo.propertySchemaObject.items) {
return (schemaWrapperInfo.propertySchemaObject.items as { type: string; }).type;
return (schemaWrapperInfo.propertySchemaObject.items as { type: string }).type;
} else if (schemaWrapperInfo.propertySchemaObject.type === 'integer' && schemaWrapperInfo.propertySchemaObject.enum) {
return 'string | number';
} else if (schemaWrapperInfo.propertySchemaObject.type === 'integer') {
return 'number';
} else if (schemaWrapperInfo.propertySchemaObject.format === 'date' || schemaWrapperInfo.propertySchemaObject.format === 'date-time') {
return 'Date';
}
return schemaWrapperInfo.propertySchemaObject.type || 'string';
return (schemaWrapperInfo.propertySchemaObject.type as string) ?? 'string';
}

public parseRef(schemaWrapperInfo: SchemaWrapperInfo): string {
Expand All @@ -324,7 +327,8 @@ export class OpenApiDocConverter {
}

public getImportTypes(entityName: string, schemaWrapperInfo: SchemaWrapperInfo): IImportType[] {
const schemaProperties = ((this.apiDocument.components?.schemas || { properties: {} })[entityName] as SchemaObject).properties || {};
const schemaProperties =
((this.apiDocument.components?.schemas ?? { [entityName]: { properties: {} } })[entityName] as SchemaObject).properties ?? {};
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const properties = Object.keys(schemaProperties).map((key) => ({
key,
Expand All @@ -337,8 +341,8 @@ export class OpenApiDocConverter {
.map((t) => t.referenceTypeName)
.filter((value, index, array) => array.indexOf(value) === index)
.map((value): IImportType => {
const refSchema: SchemaObject = (this.apiDocument.components?.schemas || {})[value];
const props = properties.filter((t) => t.items.$ref === value || t.$ref === value);
const refSchema = (this.apiDocument.components?.schemas || {})[value] as SchemaObject;
const props = properties.filter((t) => (t.items as ReferenceObject).$ref === value || t.$ref === value);
return {
name: value,
kebabCasedTypeName: kebabCase(value),
Expand All @@ -352,7 +356,7 @@ export class OpenApiDocConverter {

public getIsRequired(propertyName: string, schemaWrapperInfo: SchemaWrapperInfo): boolean {
return (
((schemaWrapperInfo.componentSchemaObject.required || []).indexOf(propertyName) > -1 ||
((schemaWrapperInfo.componentSchemaObject.required ?? []).indexOf(propertyName) > -1 ||
(schemaWrapperInfo.propertySchemaObject.nullable === undefined ? false : !schemaWrapperInfo.propertySchemaObject.nullable)) &&
propertyName !== 'id'
);
Expand Down
Loading

0 comments on commit 973de5f

Please sign in to comment.