Skip to content

Commit

Permalink
fix(jsdoc): add jsDoc to query parameters (#741)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaw3d authored Mar 2, 2023
1 parent e480184 commit b384ff4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
32 changes: 30 additions & 2 deletions packages/core/src/getters/query-params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,38 @@ describe('getQueryParams getter', () => {
});

expect(result?.schema.model.trim()).toBe(
`export type Params = { ${parameter.name}${
`export type Params = {\n${parameter.name}${
optional ? '?' : ''
}: string };`,
}: string;\n};`,
);
});
});

it('queryParamWithDescription should be documented', () => {
const result = getQueryParams({
queryParams: [
{
parameter: {
name: 'queryParamWithDescription',
in: 'query',
description: 'Parameter description.',
schema: {
type: 'string',
},
},
imports: [],
},
],
operationName: '',
context,
});
expect(result?.schema.model.trim()).toBe([
'export type Params = {',
'/**',
' * Parameter description.',
' */',
'queryParamWithDescription?: string;',
'};',
].join('\n'));
});
});
21 changes: 11 additions & 10 deletions packages/core/src/getters/query-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
GetterParameters,
GetterQueryParam,
} from '../types';
import { pascal, sanitize } from '../utils';
import { jsDoc, pascal, sanitize } from '../utils';
import { getEnum } from './enum';
import { getKey } from './keys';

Expand Down Expand Up @@ -52,12 +52,13 @@ const getQueryParamsTypes = (
});

const key = getKey(name);
const doc = jsDoc(parameter);

if (parameterImports.length) {
return {
definition: `${key}${!required || schema.default ? '?' : ''}: ${
definition: `${doc}${key}${!required || schema.default ? '?' : ''}: ${
parameterImports[0].name
}`,
};`,
imports: parameterImports,
schemas: [],
};
Expand All @@ -73,9 +74,9 @@ const getQueryParamsTypes = (
);

return {
definition: `${key}${
definition: `${doc}${key}${
!required || schema.default ? '?' : ''
}: ${enumName}`,
}: ${enumName};`,
imports: [{ name: enumName }],
schemas: [
...resolvedeValue.schemas,
Expand All @@ -84,9 +85,9 @@ const getQueryParamsTypes = (
};
}

const definition = `${key}${!required || schema.default ? '?' : ''}: ${
resolvedeValue.value
}`;
const definition = `${doc}${key}${
!required || schema.default ? '?' : ''
}: ${resolvedeValue.value};`;

return {
definition,
Expand Down Expand Up @@ -115,12 +116,12 @@ export const getQueryParams = ({
const schemas = types.flatMap(({ schemas }) => schemas);
const name = `${pascal(operationName)}${pascal(suffix)}`;

const type = types.map(({ definition }) => definition).join('; ');
const type = types.map(({ definition }) => definition).join('\n');
const allOptional = queryParams.every(({ parameter }) => !parameter.required);

const schema = {
name,
model: `export type ${name} = { ${type} };\n`,
model: `export type ${name} = {\n${type}\n};\n`,
imports,
};

Expand Down

1 comment on commit b384ff4

@vercel
Copy link

@vercel vercel bot commented on b384ff4 Mar 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.