Skip to content

Commit

Permalink
refactor(typegen): moved banner into generateTypesForDocument
Browse files Browse the repository at this point in the history
  • Loading branch information
pas-mike committed Feb 15, 2024
1 parent 93023de commit 7425620
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
3 changes: 3 additions & 0 deletions packages/typegen/src/typegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import { generateTypesForDocument } from './typegen';
const examplePetAPIYAML = path.join(__dirname, '__tests__', 'resources', 'example-pet-api.openapi.yml');

describe('typegen', () => {
let banner: string;
let imports: string;
let schemaTypes: string;
let operationTypings: string;

beforeAll(async () => {
const types = await generateTypesForDocument(examplePetAPIYAML, {
transformOperationName: (operationId: string) => operationId,
banner: '/* eslint-disable */',
disableOptionalPathParameters: true,
});
imports = types[0];
schemaTypes = types[1];
operationTypings = types[2];
banner = types[3];
});

test('generates type files from valid v3 specification', async () => {
Expand Down
12 changes: 8 additions & 4 deletions packages/typegen/src/typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { JSONSchema } from '@apidevtools/json-schema-ref-parser/dist/lib/types';
interface TypegenOptions {
transformOperationName?: (operation: string) => string;
disableOptionalPathParameters?: boolean;
banner?: string;
}

interface ExportedType {
Expand Down Expand Up @@ -40,7 +41,7 @@ export async function main() {
.option('banner', {
alias: 'b',
type: 'string',
description: 'Add comment to the generated file e.g. /* eslint-disable */',
description: 'Add banner to generated file',
})
.option('disableOptionalPathParameters', {
type: 'boolean',
Expand All @@ -54,6 +55,7 @@ export async function main() {

const opts: TypegenOptions = {
transformOperationName: (operation: string) => operation,
banner: argv.banner,
};

if (argv.transformOperationName) {
Expand All @@ -74,8 +76,8 @@ export async function main() {

opts.disableOptionalPathParameters = argv.disableOptionalPathParameters ?? true;

const [imports, schemaTypes, operationTypings] = await generateTypesForDocument(argv._[0] as string, opts);
console.log(argv.banner ? `${argv.banner}\n` : '');
const [imports, schemaTypes, operationTypings, banner] = await generateTypesForDocument(argv._[0] as string, opts);
console.log(banner, '\n');
console.log(imports, '\n');
console.log(schemaTypes);
console.log(operationTypings);
Expand All @@ -100,6 +102,8 @@ export async function generateTypesForDocument(definition: Document | string, op
await api.init();
const operationTypings = generateOperationMethodTypings(api, exportedTypes, opts);

const banner = opts.banner ?? '';

const imports = [
'import type {',
' OpenAPIClient,',
Expand All @@ -110,7 +114,7 @@ export async function generateTypesForDocument(definition: Document | string, op
`} from 'openapi-client-axios';`,
].join('\n');

return [imports, schemaTypes, operationTypings];
return [imports, schemaTypes, operationTypings, banner];
}

function generateMethodForOperation(
Expand Down

0 comments on commit 7425620

Please sign in to comment.