|
6 | 6 | createOperationComment, |
7 | 7 | hasOperationSse, |
8 | 8 | } from '~/plugins/shared/utils/operation'; |
9 | | -import { tsc } from '~/tsc'; |
| 9 | +import type { TsDsl } from '~/ts-dsl'; |
| 10 | +import { $ } from '~/ts-dsl'; |
10 | 11 |
|
11 | 12 | import type { SwrPlugin } from '../types'; |
12 | 13 |
|
@@ -35,86 +36,37 @@ export const createUseSwr = ({ |
35 | 36 | }), |
36 | 37 | }); |
37 | 38 |
|
38 | | - const awaitSdkExpression = tsc.awaitExpression({ |
39 | | - expression: tsc.callExpression({ |
40 | | - functionName: queryFn, |
41 | | - parameters: [ |
42 | | - tsc.objectExpression({ |
43 | | - multiLine: true, |
44 | | - obj: [ |
45 | | - // { |
46 | | - // spread: optionsParamName, |
47 | | - // }, |
48 | | - // { |
49 | | - // spread: 'queryKey[0]', |
50 | | - // }, |
51 | | - // { |
52 | | - // key: 'signal', |
53 | | - // shorthand: true, |
54 | | - // value: tsc.identifier({ |
55 | | - // text: 'signal', |
56 | | - // }), |
57 | | - // }, |
58 | | - { |
59 | | - key: 'throwOnError', |
60 | | - value: true, |
61 | | - }, |
62 | | - ], |
63 | | - }), |
64 | | - ], |
65 | | - }), |
66 | | - }); |
67 | | - const statements: Array<ts.Statement> = []; |
| 39 | + const awaitSdkFn = $(queryFn) |
| 40 | + .call($.object((o) => o.prop('throwOnError', $.literal(true)))) |
| 41 | + .await(); |
| 42 | + |
| 43 | + const statements: Array<ts.Statement | TsDsl<any>> = []; |
68 | 44 | if (plugin.getPluginOrThrow('@hey-api/sdk').config.responseStyle === 'data') { |
69 | | - statements.push( |
70 | | - tsc.returnVariable({ |
71 | | - expression: awaitSdkExpression, |
72 | | - }), |
73 | | - ); |
| 45 | + statements.push($.return(awaitSdkFn)); |
74 | 46 | } else { |
75 | 47 | statements.push( |
76 | | - tsc.constVariable({ |
77 | | - destructure: true, |
78 | | - expression: awaitSdkExpression, |
79 | | - name: 'data', |
80 | | - }), |
81 | | - tsc.returnVariable({ |
82 | | - expression: 'data', |
83 | | - }), |
| 48 | + $.const().object('data').assign(awaitSdkFn), |
| 49 | + $.return('data'), |
84 | 50 | ); |
85 | 51 | } |
86 | 52 |
|
87 | | - const statement = tsc.constVariable({ |
88 | | - comment: plugin.config.comments |
89 | | - ? createOperationComment({ operation }) |
90 | | - : undefined, |
91 | | - exportConst: symbolUseQueryFn.exported, |
92 | | - expression: tsc.arrowFunction({ |
93 | | - parameters: [ |
94 | | - // { |
95 | | - // isRequired: isRequiredOptions, |
96 | | - // name: optionsParamName, |
97 | | - // type: typeData, |
98 | | - // }, |
99 | | - ], |
100 | | - statements: [ |
101 | | - tsc.returnStatement({ |
102 | | - expression: tsc.callExpression({ |
103 | | - functionName: symbolUseSwr.placeholder, |
104 | | - parameters: [ |
105 | | - tsc.stringLiteral({ |
106 | | - text: operation.path, |
107 | | - }), |
108 | | - tsc.arrowFunction({ |
109 | | - async: true, |
110 | | - statements, |
111 | | - }), |
112 | | - ], |
113 | | - }), |
114 | | - }), |
115 | | - ], |
116 | | - }), |
117 | | - name: symbolUseQueryFn.placeholder, |
118 | | - }); |
| 53 | + const statement = $.const(symbolUseQueryFn.placeholder) |
| 54 | + .export(symbolUseQueryFn.exported) |
| 55 | + .$if( |
| 56 | + plugin.config.comments && createOperationComment({ operation }), |
| 57 | + (c, v) => c.describe(v as Array<string>), |
| 58 | + ) |
| 59 | + .assign( |
| 60 | + $.func().do( |
| 61 | + $.return( |
| 62 | + $(symbolUseSwr.placeholder).call( |
| 63 | + $.literal(operation.path), |
| 64 | + $.func() |
| 65 | + .async() |
| 66 | + .do(...statements), |
| 67 | + ), |
| 68 | + ), |
| 69 | + ), |
| 70 | + ); |
119 | 71 | plugin.setSymbolValue(symbolUseQueryFn, statement); |
120 | 72 | }; |
0 commit comments