-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgeneratorHook.js
More file actions
55 lines (50 loc) · 1.38 KB
/
generatorHook.js
File metadata and controls
55 lines (50 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const { ifElse } = require('./utils')
function generatorHook({
verb,
route,
operationName,
operationType,
isOperationQuery,
pathParams,
hasPathParams,
hasQueryParams,
}) {
let output = ''
const hookName = `use${operationType}${operationName}`
if (isOperationQuery) {
output += `export const ${hookName} = (${ifElse(hasPathParams, 'pathParams = null, ')}${ifElse(
hasQueryParams,
'queryParams = null, '
)}config, options) => useQuery({
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(' && '),
true
)},
...config
})
${hookName}.queryKey = '${route}'`
}
// mutation hook
else {
output += `export const ${hookName} = (${ifElse(hasPathParams, 'pathParams, ')}${ifElse(
hasQueryParams,
'queryParams, '
)}config, options) => useMutation(mutationFn('${verb}', '${route}', ${ifElse(
hasPathParams,
'pathParams',
'{}'
)}, ${ifElse(hasQueryParams, 'queryParams', '{}')}, options), config)`
}
return `${output}
`
}
module.exports = generatorHook