Skip to content

Commit

Permalink
test: asObject for getParamsAST
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Oct 18, 2023
1 parent a435b1e commit 2da4c7a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
21 changes: 21 additions & 0 deletions packages/swagger/src/utils/getParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,25 @@ describe('getParams', () => {
).toString(),
).toBe('name: Pet["name"], age: Pet["age"]')
})

test('if operation returns a string with typed parameters and as an object', () => {
expect(
getParams(
{
name: 'Pet',
schema: {
properties: {
name: {
type: 'string',
},
age: {
type: 'number',
},
},
},
},
{ asObject: true },
).toString(),
).toBe('{ name, age }: Pet')
})
})
8 changes: 4 additions & 4 deletions packages/swagger/src/utils/getParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export function getASTParams(
if (asObject) {
return [
{
name: `{ ${getASTParams(operationSchema, { typed: true })
name: `{ ${getASTParams(operationSchema)
.map((item) => item.name)
.join(', ')} }`,
type: operationSchema?.name,
enabled: !!operationSchema?.name,
required: !!operationSchema?.schema.required?.length,
required: true,
},
]
}
Expand All @@ -41,9 +41,9 @@ type GetParamsResult = {
// TODO convert to class together with `createFunctionParams` and `getASTParams`
export function getParams(
operationSchema: OperationSchema | undefined,
{ typed = false, override }: { typed?: boolean; override?: (data: FunctionParamsAST) => FunctionParamsAST } = {},
{ typed = false, override, asObject = false }: { typed?: boolean; asObject?: boolean; override?: (data: FunctionParamsAST) => FunctionParamsAST } = {},
): GetParamsResult {
const ast = getASTParams(operationSchema, { typed, override })
const ast = getASTParams(operationSchema, { typed, override, asObject })
const functionParams = new FunctionParams()
functionParams.add(ast)

Expand Down

0 comments on commit 2da4c7a

Please sign in to comment.