Skip to content

Commit

Permalink
Merge pull request #179 from pas-mike/typegen-banner-option
Browse files Browse the repository at this point in the history
feat(typegen): banner comment option
  • Loading branch information
anttiviljami authored Feb 29, 2024
2 parents 63e99d8 + 9834f68 commit a437efe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 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
18 changes: 16 additions & 2 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 All @@ -37,6 +38,11 @@ export async function main() {
alias: 't',
type: 'string',
})
.option('banner', {
alias: 'b',
type: 'string',
description: 'Add banner to generated file',
})
.option('disableOptionalPathParameters', {
type: 'boolean',
description: 'Force all path parameters to be required',
Expand All @@ -49,6 +55,7 @@ export async function main() {

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

if (argv.transformOperationName) {
Expand All @@ -69,7 +76,12 @@ export async function main() {

opts.disableOptionalPathParameters = argv.disableOptionalPathParameters ?? true;

const [imports, schemaTypes, operationTypings] = await generateTypesForDocument(argv._[0] as string, opts);
const [imports, schemaTypes, operationTypings, banner] = await generateTypesForDocument(argv._[0] as string, opts);

if (banner?.length) {
console.log(banner, '\n');
}

console.log(imports, '\n');
console.log(schemaTypes);
console.log(operationTypings);
Expand All @@ -94,6 +106,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 @@ -104,7 +118,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 a437efe

Please sign in to comment.