Skip to content

Commit

Permalink
chore: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yhnavein committed Jun 20, 2024
1 parent cba4f9a commit 7f54761
Show file tree
Hide file tree
Showing 18 changed files with 110 additions and 98 deletions.
4 changes: 2 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import { bold, cyan, red } from 'nanocolors';
import { Command } from 'commander';

import { CodeGenResult, runCodeGenerator } from './index';
import { FullAppOptions } from './types';
import { type CodeGenResult, runCodeGenerator } from './index';
import type { FullAppOptions } from './types';

const program = new Command();
program
Expand Down
4 changes: 2 additions & 2 deletions src/gen/js/createBarrel.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { camel } from 'case';
import { ClientOptions } from '../../types';
import type { ClientOptions } from '../../types';
import { renderFile } from '../templateManager';

export async function generateBarrelFile(clients: any[], clientOptions: ClientOptions) {
const files = [];

for (let name in clients) {
for (const name in clients) {
files.push(name);
}

Expand Down
6 changes: 3 additions & 3 deletions src/gen/js/genOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
orderBy,
upperFirst,
} from '../util';
import {
import type {
IServiceClient,
IApiOperation,
IOperationParam,
Expand All @@ -17,7 +17,7 @@ import {
} from './models';
import { generateBarrelFile } from './createBarrel';
import { renderFile } from '../templateManager';
import { ApiSpec, ApiOperation, ClientOptions, ApiOperationParam } from '../../types';
import type { ApiSpec, ApiOperation, ClientOptions, ApiOperationParam } from '../../types';

const MAX_QUERY_PARAMS: number = 1;

Expand All @@ -34,7 +34,7 @@ export default async function genOperations(
})) || '';
let queryDefinitions = {} as IQueryDefinitions;

for (let name in groups) {
for (const name in groups) {
const group = groups[name];
const [clientData, clientQueryDefinitions] = prepareClient(
(options.servicePrefix || '') + name,
Expand Down
54 changes: 27 additions & 27 deletions src/gen/js/genTypes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { ApiSpec } from '../../types';
import type { ApiSpec } from '../../types';
import genTypes, { renderQueryStringParameters, renderComment } from './genTypes';

const emptySpec: ApiSpec = {
Expand All @@ -15,15 +15,15 @@ const emptySpec: ApiSpec = {
};

describe('genTypes', () => {
it(`empty definitions is handled properly`, () => {
it('should handle empty definitions properly', () => {
const res = genTypes(emptySpec, {}, {} as any);

expect(res).to.be.equal('');
});

describe('enums', () => {
describe('int-serialized simple enums', () => {
it(`Swashbuckle's enum should be handled correctly`, () => {
it(`should handle Swashbuckle's enum correctly`, () => {
const res = genTypes(
emptySpec,
{
Expand All @@ -37,10 +37,10 @@ describe('genTypes', () => {
);

expect(res).to.be.ok;
expect(res.trim()).to.be.equal(`export type SomeEnum = 0 | 1;`);
expect(res.trim()).to.be.equal('export type SomeEnum = 0 | 1;');
});

it(`NSwag's enum should be handled correctly`, () => {
it(`should handle NSwag's enum correctly`, () => {
const res = genTypes(
emptySpec,
{
Expand All @@ -66,7 +66,7 @@ describe('genTypes', () => {
});

describe('string-serialized simple enums', () => {
it(`Swashbuckle's enum should be handled correctly`, () => {
it(`should handle Swashbuckle's enum correctly`, () => {
const res = genTypes(
emptySpec,
{
Expand All @@ -85,7 +85,7 @@ describe('genTypes', () => {
});

describe('x-enums', () => {
it(`should handle number-based x-enums correctly`, () => {
it('should handle number-based x-enums correctly', () => {
const res = genTypes(
emptySpec,
{
Expand All @@ -108,7 +108,7 @@ describe('genTypes', () => {
}`);
});

it(`should handle string-based x-enums correctly`, () => {
it('should handle string-based x-enums correctly', () => {
const res = genTypes(
emptySpec,
{
Expand All @@ -133,7 +133,7 @@ describe('genTypes', () => {
});

describe('normal objects', () => {
it(`should handle obj with no required fields`, () => {
it('should handle obj with no required fields', () => {
const res = genTypes(
emptySpec,
{
Expand All @@ -159,7 +159,7 @@ describe('genTypes', () => {
}`);
});

it(`should handle obj with all required fields`, () => {
it('should handle obj with all required fields', () => {
const res = genTypes(
emptySpec,
{
Expand Down Expand Up @@ -188,7 +188,7 @@ describe('genTypes', () => {
});

describe('objects with read-only fields', () => {
it(`should ignore read-only fields`, () => {
it('should ignore read-only fields', () => {
const res = genTypes(
emptySpec,
{
Expand Down Expand Up @@ -218,7 +218,7 @@ describe('genTypes', () => {
});

describe('renderQueryStringParameters', () => {
it(`empty list should work fine`, () => {
it('should handle empty list correctly', () => {
const def = {
type: 'object',
required: [],
Expand All @@ -230,7 +230,7 @@ describe('renderQueryStringParameters', () => {
expect(res).to.deep.equal([]);
});

it(`one element without dots should work fine`, () => {
it('should handle one element without dots', () => {
const def = {
type: 'object',
required: [],
Expand All @@ -252,7 +252,7 @@ describe('renderQueryStringParameters', () => {
expect(res[0]).to.contain('page?: number;');
});

it(`one element in dot notation should work fine`, () => {
it('should handle one element in a dot notation', () => {
const def = {
type: 'object',
required: [],
Expand All @@ -274,7 +274,7 @@ describe('renderQueryStringParameters', () => {
expect(textOnly(res[0])).to.be.equal(textOnly('parameters?: {page?: number; }'));
});

it(`two elements in dot notation should work fine`, () => {
it('should handle two elements in a dot notation', () => {
const def = {
type: 'object',
required: [],
Expand Down Expand Up @@ -304,7 +304,7 @@ describe('renderQueryStringParameters', () => {
);
});

it(`four elements in dot notation should work fine`, () => {
it('should handle four elements in a dot notation', () => {
const def = {
type: 'object',
required: [],
Expand Down Expand Up @@ -350,7 +350,7 @@ describe('renderQueryStringParameters', () => {
expect(textOnly(res[1])).to.be.equal(textOnly('else?: {page?: number; count?: number; }'));
});

it(`crazy case #1`, () => {
it('crazy case #1', () => {
const def = {
type: 'object',
required: [],
Expand Down Expand Up @@ -431,7 +431,7 @@ filter?: {
});

describe('renderComment', () => {
it(`it should render proper multiline comment`, () => {
it('should render proper multiline comment', () => {
const comment = `Quite a lenghty comment
With at least two lines`;
const res = renderComment(comment);
Expand All @@ -442,7 +442,7 @@ With at least two lines`;
*/`);
});

it(`it should render proper multiline comment with trimming`, () => {
it('should render proper multiline comment with trimming', () => {
const comment = ` Quite a lenghty comment
With at least two lines `;
const res = renderComment(comment);
Expand All @@ -453,28 +453,28 @@ With at least two lines`;
*/`);
});

it(`it should render proper one-line comment`, () => {
const comment = `One liner`;
it('should render proper one-line comment', () => {
const comment = 'One liner';
const res = renderComment(comment);

expect(res).to.be.equal(`// One liner`);
expect(res).to.be.equal('// One liner');
});

it(`it should render proper one-line comment with trimming`, () => {
const comment = ` One liner `;
it('should render proper one-line comment with trimming', () => {
const comment = ' One liner ';
const res = renderComment(comment);

expect(res).to.be.equal(`// One liner`);
expect(res).to.be.equal('// One liner');
});

it(`it should handle null comment`, () => {
it('should handle null comment', () => {
const comment = null;
const res = renderComment(comment);

expect(res).to.be.null;
});

it(`it should handle empty comment`, () => {
it('should handle empty comment', () => {
const comment = '';
const res = renderComment(comment);

Expand Down
21 changes: 10 additions & 11 deletions src/gen/js/genTypes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { dset as set } from 'dset';
import { join, uniq } from '../util';
import { getTSParamType } from './support';
import { IQueryDefinitions } from './models';
import { ApiSpec, ClientOptions } from '../../types';
import type { IQueryDefinitions } from './models';
import type { ApiSpec, ClientOptions } from '../../types';

export default function genTypes(
spec: ApiSpec,
Expand Down Expand Up @@ -60,10 +60,11 @@ function renderTsType(name, def, options: ClientOptions, typeToBeGeneric?: strin
lines.push(renderComment(def.description));
}

if (!!def['x-enumNames']) {
if (def['x-enumNames']) {
lines.push(renderXEnumType(name, def));
return lines;
} else if (!!def.enum) {
}
if (def.enum) {
lines.push(renderEnumType(name, def));
return lines;
}
Expand Down Expand Up @@ -164,12 +165,10 @@ function renderEnumType(name: string, def: any) {
return `export enum ${name} {
${enumKeys.join('\n')}
}\n`;
} else {
const values = (def.enum as any[])
.map((v) => (typeof v === 'number' ? v : `'${v}'`))
.join(' | ');
return `export type ${name} = ${values};\n`;
}

const values = (def.enum as any[]).map((v) => (typeof v === 'number' ? v : `'${v}'`)).join(' | ');
return `export type ${name} = ${values};\n`;
}

function renderTsInheritance(name: string, allOf: any[], options: ClientOptions) {
Expand Down Expand Up @@ -249,8 +248,8 @@ export function renderComment(comment: string | null) {
const commentLines = comment.split('\n');

if (commentLines.length === 1) {
return '// ' + comment.trim();
return `// ${comment.trim()}`;
}

return ' /**\n' + commentLines.map((line) => ' * ' + line.trim()).join('\n') + '\n */';
return ` /**\n${commentLines.map((line) => ` * ${line.trim()}`).join('\n')}\n */`;
}
2 changes: 1 addition & 1 deletion src/gen/js/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import genOperations from './genOperations';
import genTypes from './genTypes';
import { saveFile, prepareOutputFilename } from '../util';
import { ApiOperation, ApiSpec, ClientOptions } from '../../types';
import type { ApiOperation, ApiSpec, ClientOptions } from '../../types';

export default async function genCode(
spec: ApiSpec,
Expand Down
2 changes: 1 addition & 1 deletion src/gen/js/models.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiOperationParam } from '../../types';
import type { ApiOperationParam } from '../../types';

export interface IApiOperation {
returnType: string;
Expand Down
24 changes: 14 additions & 10 deletions src/gen/js/serializeQueryParam.angular1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ import { expect } from 'chai';
function serializeQueryParam(obj: any, property: string): string {
if (obj === null || obj === undefined || obj === '') {
return '';
} else if (obj instanceof Date) {
return property + '=' + encodeURIComponent(obj.toJSON());
} else if (Array.isArray(obj)) {
}
if (obj instanceof Date) {
return `${property}=${encodeURIComponent(obj.toJSON())}`;
}
if (Array.isArray(obj)) {
return Object.values(obj)
.map((value) => `${property}[]=${value}`)
.join('&');
} else if (typeof obj !== 'object') {
return property + '=' + encodeURIComponent(obj);
} else if (typeof obj === 'object') {
}
if (typeof obj !== 'object') {
return `${property}=${encodeURIComponent(obj)}`;
}
if (typeof obj === 'object') {
return Object.keys(obj)
.filter((key) => !!serializeQueryParam(obj[key], property + '.' + key))
.reduce((a: any, b) => a.push(serializeQueryParam(obj[b], property + '.' + b)) && a, [])
.filter((key) => !!serializeQueryParam(obj[key], `${property}.${key}`))
.reduce((a: any, b) => a.push(serializeQueryParam(obj[b], `${property}.${b}`)) && a, [])
.join('&');
} else {
return '';
}

return '';
}

describe('serializeQueryParam.angular1', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/gen/js/serializeQueryParam.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('serializeQueryParam / axios', () => {
});
});

it(`should handle array`, () => {
it('should handle array', () => {
const res = serializeQueryParam([1, 2, 3]);

expect(res.toString()).to.be.equal([1, 2, 3].toString());
Expand Down
2 changes: 1 addition & 1 deletion src/gen/js/support.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { ClientOptions } from '../../types';
import type { ClientOptions } from '../../types';
import { getTSParamType } from './support';

describe('getTSParamType', () => {
Expand Down
Loading

0 comments on commit 7f54761

Please sign in to comment.