|
1 | | -import { |
2 | | - createParser, |
3 | | - parseMethodMetadata, |
4 | | - parseMethodParameters, |
5 | | - transformBodyStringify, |
6 | | - transformFetchBody, |
7 | | - transformHeaderOptions, |
8 | | - transformParameters, |
9 | | - transformQueryParams, |
10 | | - transformUrlSyntax, |
11 | | -} from '@genapi/parser' |
| 1 | +import { createQueryParser } from '../../../_shared/query' |
12 | 2 |
|
13 | | -function hookName(fetcherName: string) { |
14 | | - return `use${fetcherName.charAt(0).toUpperCase()}${fetcherName.slice(1)}` |
15 | | -} |
16 | | - |
17 | | -export const parser = createParser((config, { configRead, functions, interfaces }) => { |
18 | | - const { parameters, interfaces: attachInters, options } = parseMethodParameters(config) |
19 | | - let { name, description, url, responseType, body } = parseMethodMetadata(config) |
20 | | - |
21 | | - attachInters.forEach(i => interfaces.add('type', i)) |
22 | | - const fetcherParams = [...parameters] |
23 | | - fetcherParams.push({ |
24 | | - name: 'config', |
25 | | - type: 'RequestInit', |
26 | | - required: false, |
27 | | - }) |
28 | | - |
29 | | - if (config.method.toLowerCase() !== 'get') |
30 | | - options.unshift(['method', `"${config.method}"`]) |
31 | | - |
32 | | - transformHeaderOptions('body', { options, parameters }) |
33 | | - |
34 | | - options.push(['...', 'config']) |
35 | | - |
36 | | - const { spaceResponseType } = transformParameters(parameters, { |
37 | | - syntax: 'typescript', |
38 | | - configRead, |
39 | | - description, |
40 | | - interfaces: interfaces.all(), |
41 | | - responseType, |
42 | | - }) |
43 | | - |
44 | | - transformBodyStringify('body', { options, parameters }) |
45 | | - url = transformQueryParams('query', { body, options, url }) |
46 | | - url = transformUrlSyntax(url, { baseURL: configRead.config.meta?.baseURL }) |
47 | | - const fetchBody = transformFetchBody(url, options, spaceResponseType) |
48 | | - |
49 | | - const hasApiOutput = configRead.outputs.some(o => o.type === 'api') |
50 | | - const fetcherScope = hasApiOutput ? 'api' : 'main' |
51 | | - const fetcherRef = hasApiOutput ? `Api.${name}` : name |
52 | | - |
53 | | - functions.add(fetcherScope, { |
54 | | - export: true, |
55 | | - async: true, |
56 | | - name, |
57 | | - description, |
58 | | - parameters: fetcherParams, |
59 | | - body: [ |
60 | | - ...body, |
61 | | - ...fetchBody, |
62 | | - ], |
63 | | - }) |
64 | | - |
65 | | - const isRead = ['get', 'head'].includes(config.method.toLowerCase()) |
66 | | - const hook = hookName(name) |
67 | | - const paramNames = fetcherParams.map(p => p.name).join(', ') |
68 | | - |
69 | | - if (isRead) { |
70 | | - const keyItems = `'${name}', ${paramNames}` |
71 | | - functions.add('main', { |
72 | | - export: true, |
73 | | - name: hook, |
74 | | - description: [`@wraps ${name}`], |
75 | | - parameters: fetcherParams, |
76 | | - body: [ |
77 | | - `return useQuery({ key: [${keyItems}], query: () => ${fetcherRef}(${paramNames}) })`, |
78 | | - ], |
79 | | - }) |
80 | | - } |
81 | | - else { |
82 | | - functions.add('main', { |
83 | | - export: true, |
84 | | - name: hook, |
85 | | - description: description ? [...(Array.isArray(description) ? description : [description]), `@wraps ${name}`] : [`@wraps ${name}`], |
86 | | - parameters: [], |
87 | | - body: [ |
88 | | - `return useMutation({ mutation: ${fetcherRef} })`, |
89 | | - ], |
90 | | - }) |
91 | | - } |
92 | | -}) |
| 3 | +export const parser = createQueryParser() |
0 commit comments