|
1 | 1 | import ts from 'typescript';
|
2 | 2 | import { getOperationName } from '@rtk-query/oazapfts-patched/lib/codegen/generate';
|
3 | 3 | import { capitalize, isQuery } from '../utils';
|
4 |
| -import type { OperationDefinition, EndpointOverrides } from '../types'; |
| 4 | +import type { OperationDefinition, EndpointOverrides, ConfigFile } from '../types'; |
5 | 5 | import { getOverrides } from '../generate';
|
6 | 6 | import { factory } from '../utils/factory';
|
7 | 7 |
|
| 8 | +type HooksConfigOptions = NonNullable<ConfigFile['hooks']>; |
| 9 | + |
8 | 10 | type GetReactHookNameParams = {
|
9 | 11 | operationDefinition: OperationDefinition;
|
10 | 12 | endpointOverrides: EndpointOverrides[] | undefined;
|
| 13 | + config: HooksConfigOptions; |
11 | 14 | };
|
12 | 15 |
|
13 |
| -const getReactHookName = ({ |
14 |
| - operationDefinition: { verb, path, operation }, |
15 |
| - operationDefinition, |
16 |
| - endpointOverrides, |
17 |
| -}: GetReactHookNameParams) => { |
18 |
| - const overrides = getOverrides(operationDefinition, endpointOverrides); |
| 16 | +type CreateBindingParams = { |
| 17 | + operationDefinition: OperationDefinition; |
| 18 | + overrides?: EndpointOverrides; |
| 19 | + isLazy?: boolean; |
| 20 | +}; |
19 | 21 |
|
20 |
| - return factory.createBindingElement( |
| 22 | +const createBinding = ({ |
| 23 | + operationDefinition: { verb, path, operation }, |
| 24 | + overrides, |
| 25 | + isLazy = false, |
| 26 | +}: CreateBindingParams) => |
| 27 | + factory.createBindingElement( |
21 | 28 | undefined,
|
22 | 29 | undefined,
|
23 | 30 | factory.createIdentifier(
|
24 |
| - `use${capitalize(getOperationName(verb, path, operation.operationId))}${ |
| 31 | + `use${isLazy ? 'Lazy' : ''}${capitalize(getOperationName(verb, path, operation.operationId))}${ |
25 | 32 | isQuery(verb, overrides) ? 'Query' : 'Mutation'
|
26 | 33 | }`
|
27 | 34 | ),
|
28 | 35 | undefined
|
29 | 36 | );
|
| 37 | + |
| 38 | +const getReactHookName = ({ operationDefinition, endpointOverrides, config }: GetReactHookNameParams) => { |
| 39 | + const overrides = getOverrides(operationDefinition, endpointOverrides); |
| 40 | + |
| 41 | + const baseParams = { |
| 42 | + operationDefinition, |
| 43 | + overrides, |
| 44 | + }; |
| 45 | + |
| 46 | + const _isQuery = isQuery(operationDefinition.verb, overrides); |
| 47 | + |
| 48 | + // If `config` is true, just generate everything |
| 49 | + if (typeof config === 'boolean') { |
| 50 | + return createBinding(baseParams); |
| 51 | + } |
| 52 | + |
| 53 | + // `config` is an object and we need to check for the configuration of each property |
| 54 | + if (_isQuery) { |
| 55 | + return [ |
| 56 | + ...(config.queries ? [createBinding(baseParams)] : []), |
| 57 | + ...(config.lazyQueries ? [createBinding({ ...baseParams, isLazy: true })] : []), |
| 58 | + ]; |
| 59 | + } |
| 60 | + |
| 61 | + return config.mutations ? createBinding(baseParams) : []; |
30 | 62 | };
|
31 | 63 |
|
32 | 64 | type GenerateReactHooksParams = {
|
33 | 65 | exportName: string;
|
34 | 66 | operationDefinitions: OperationDefinition[];
|
35 | 67 | endpointOverrides: EndpointOverrides[] | undefined;
|
| 68 | + config: HooksConfigOptions; |
36 | 69 | };
|
37 |
| -export const generateReactHooks = ({ exportName, operationDefinitions, endpointOverrides }: GenerateReactHooksParams) => |
| 70 | +export const generateReactHooks = ({ |
| 71 | + exportName, |
| 72 | + operationDefinitions, |
| 73 | + endpointOverrides, |
| 74 | + config, |
| 75 | +}: GenerateReactHooksParams) => |
38 | 76 | factory.createVariableStatement(
|
39 | 77 | [factory.createModifier(ts.SyntaxKind.ExportKeyword)],
|
40 | 78 | factory.createVariableDeclarationList(
|
41 | 79 | [
|
42 | 80 | factory.createVariableDeclaration(
|
43 | 81 | factory.createObjectBindingPattern(
|
44 |
| - operationDefinitions.map((operationDefinition) => |
45 |
| - getReactHookName({ operationDefinition, endpointOverrides }) |
46 |
| - ) |
| 82 | + operationDefinitions |
| 83 | + .map((operationDefinition) => getReactHookName({ operationDefinition, endpointOverrides, config })) |
| 84 | + .flat() |
47 | 85 | ),
|
48 | 86 | undefined,
|
49 | 87 | undefined,
|
|
0 commit comments