Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(typegen): banner comment option #179

Merged
merged 6 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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