I'm currently utilizing the "openapi-typescript" tool to automatically generate all the types based on my swagger server specifications. In this process, I have created a Type called "GetUrls" which contains all the keys associated with the "get" method.
import { paths } from '../types/swagger.js'
...
type GetPaths = FilterPaths<paths, 'get'>
type GetUrls = keyof GetPaths & string
...
const get = async (url: GetUrls, params: Params) => {
//mapping over params
let path = ''
for (let param in params) {
path = url.replace('{' + param + '}', params[param])
}
type Schema = GetPaths[typeof url]['get']['responses']['200']['content']['*/*']
return (await axios.get<Schema>(path)).data
}
get('/api/user/{id}', { id: '123' }).then(data => console.log(data))
Although the autocompletion feature for the function "get" is working seamlessly, there seems to be an issue with the type inference.
Interestingly, when I manually include the "url" index, the functionality works as expected:
type Schema = GetPaths['/api/user/{id}']['get']['responses']['200']['content']['*/*']