Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,13 @@ export const setApi = (newApi) => {
export const extendApi = (options) => {
api = getApi().extend(options)
}

const requestFn = async ({ url, method, pathParams, queryParams, ...rest }) => {
queryParams = JSON.parse(JSON.stringify(queryParams))

const urlPathParams = url.match(/{([^}]+)}/g)

if (urlPathParams) {
url = urlPathParams.reduce((acc, param) => acc.replace(param, pathParams[param.replace(/{|}/g, '')]), url)
} else {
queryParams = { ...queryParams, ...pathParams }
}

if (url.charAt(0) === '/') {
Expand Down Expand Up @@ -191,7 +190,7 @@ const requestFn = async ({ url, method, pathParams, queryParams, ...rest }) => {
return data
}

const queryFn = (options = {}) => (url, pathParams = {}, queryParams = {}) => {
const queryFn = ({ url, pathParams = {}, queryParams = {}, options = {} }) => {
const controller = new AbortController()
const { signal } = controller

Expand Down
4 changes: 2 additions & 2 deletions src/generatorApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ function generatorApiTypes({
output += `export const ${methodName} = (${ifElse(hasPathParams, 'pathParams, ')}${ifElse(
hasQueryParams,
'queryParams, '
)}options) => queryFn(options)('${route}'${ifElse(hasPathParams, ', pathParams')}${ifElse(
)}options) => queryFn({ url: '${route}'${ifElse(hasPathParams, ', pathParams')}${ifElse(
hasQueryParams,
', queryParams'
)})`
)}, options })`
} else {
output += `export const ${methodName} = (${ifElse(hasPathParams, 'pathParams, ')}${ifElse(
hasQueryParams,
Expand Down
22 changes: 15 additions & 7 deletions src/generatorHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@ function generatorHook({
const hookName = `use${operationType}${operationName}`

if (isOperationQuery) {
output += `export const ${hookName} = (${ifElse(hasPathParams, 'pathParams, ')}${ifElse(
output += `export const ${hookName} = (${ifElse(hasPathParams, 'pathParams = null, ')}${ifElse(
hasQueryParams,
'queryParams, '
'queryParams = null, '
)}config, options) => useQuery({
queryKey: ${ifElse(
queryKey: ['${route}'${ifElse(hasPathParams, ', pathParams')}${ifElse(
hasQueryParams,
', queryParams'
)}].filter(Boolean),
queryFn: () => queryFn({ url: '${route}'${ifElse(hasPathParams, ', pathParams')}${ifElse(
hasQueryParams,
', queryParams'
)}, options }),
enabled: ${ifElse(
hasPathParams,
'pathParams && ' + pathParams.map((param) => `pathParams.${param}`).join(' && ') + ' && '
)}['${route}'${ifElse(hasPathParams, ', pathParams')}${ifElse(hasQueryParams, ', queryParams')}],
queryFn: queryFn(options),
config
'!!pathParams && ' + pathParams.map((param) => `!!pathParams.${param}`).join(' && '),
true
)},
...config
})
${hookName}.queryKey = '${route}'`
}
Expand Down