@kubb/plugin-oas
Version:
OpenAPI Specification (OAS) plugin for Kubb, providing core functionality for parsing and processing OpenAPI/Swagger schemas for code generation.
65 lines (58 loc) • 1.79 kB
text/typescript
import { camelCase, isValidVarName } from '@kubb/core/transformers'
import type { FunctionParamsAST } from '@kubb/core/utils'
import type { OasTypes } from '@kubb/oas'
import { isParameterObject } from '@kubb/oas'
import type { Params } from '@kubb/react-fabric/types'
import type { OperationSchema } from '../types.ts'
/**
*
* @deprecated
* TODO move to operationManager hook
*/
export function getASTParams(
operationSchema: OperationSchema | undefined,
{
typed = false,
override,
}: {
typed?: boolean
override?: (data: FunctionParamsAST) => FunctionParamsAST
} = {},
): FunctionParamsAST[] {
if (!operationSchema || !operationSchema.schema.properties || !operationSchema.name) {
return []
}
return Object.entries(operationSchema.schema.properties).map(([name, schema]: [string, OasTypes.SchemaObject]) => {
const isParam = isParameterObject(schema)
const data: FunctionParamsAST = {
name,
enabled: !!name,
required: isParam ? schema.required : true,
type: typed ? `${operationSchema.name}["${name}"]` : undefined,
}
return override ? override(data) : data
})
}
export function getPathParams(
operationSchema: OperationSchema | undefined,
options: {
typed?: boolean
casing?: 'camelcase'
override?: (data: FunctionParamsAST) => FunctionParamsAST
} = {},
) {
return getASTParams(operationSchema, options).reduce((acc, curr) => {
if (curr.name && curr.enabled) {
let name = isValidVarName(curr.name) ? curr.name : camelCase(curr.name)
if (options.casing === 'camelcase') {
name = camelCase(name)
}
acc[name] = {
default: curr.default,
type: curr.type,
optional: !curr.required,
}
}
return acc
}, {} as Params)
}