Skip to content

Commit 52fbf53

Browse files
authored
Merge pull request #2942 from hey-api/chore/dsl-func
fix(@pinia/colada): correctly access instantiated SDKs
2 parents 520251d + c041e96 commit 52fbf53

File tree

23 files changed

+550
-217
lines changed

23 files changed

+550
-217
lines changed

.changeset/lemon-plants-shop.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hey-api/openapi-ts': patch
3+
---
4+
5+
@pinia/colada: correctly access instantiated SDKs

dev/openapi-ts.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export default defineConfig(() => {
4242
// 'dutchie.json',
4343
// 'invalid',
4444
// 'full.yaml',
45-
'openai.yaml',
46-
// 'opencode.yaml',
45+
// 'openai.yaml',
46+
'opencode.yaml',
4747
// 'sdk-instance.yaml',
4848
// 'string-with-format.yaml',
4949
// 'transformers.json',

packages/openapi-ts/src/generate/renderer.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { createBinding, mergeBindings, renderIds } from '@hey-api/codegen-core';
1313
import ts from 'typescript';
1414

1515
import { ensureValidIdentifier } from '~/openApi/shared/utils/identifier';
16+
import { TsDsl } from '~/ts-dsl';
1617
import { tsc } from '~/tsc';
1718
import { tsNodeToString } from '~/tsc/utils';
1819

@@ -186,7 +187,10 @@ export class TypeScriptRenderer implements Renderer {
186187

187188
for (const symbolId of file.symbols.body) {
188189
const value = project.symbols.getValue(symbolId);
189-
if (typeof value === 'string') {
190+
if (value instanceof TsDsl) {
191+
const node = value.$render();
192+
lines.push(tsNodeToString({ node, unescape: true }));
193+
} else if (typeof value === 'string') {
190194
lines.push(value);
191195
} else if (value instanceof Array) {
192196
for (const node of value) {

packages/openapi-ts/src/plugins/@hey-api/sdk/shared/class.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ export const generateClassSdk = ({
387387
plugin.config.instance
388388
? $.new(refChildClass.placeholder).args(
389389
$.object((o) =>
390-
o.prop('client', () => $('this').attr('client')),
390+
o.prop('client', $('this').attr('client')),
391391
),
392392
)
393393
: $(refChildClass.placeholder),
@@ -402,7 +402,7 @@ export const generateClassSdk = ({
402402
? $.return(
403403
$.new(refChildClass.placeholder).args(
404404
$.object((o) =>
405-
o.prop('client', () => $('this').attr('client')),
405+
o.prop('client', $('this').attr('client')),
406406
),
407407
),
408408
)
@@ -426,7 +426,7 @@ export const generateClassSdk = ({
426426
const node = createClientClass({
427427
plugin,
428428
symbol: symbolHeyApiClient,
429-
}).$render();
429+
});
430430
plugin.setSymbolValue(symbolHeyApiClient, node);
431431
}
432432

@@ -486,7 +486,7 @@ export const generateClassSdk = ({
486486
plugin,
487487
sdkName: symbol.placeholder,
488488
symbol: symbolHeyApiRegistry,
489-
}).$render();
489+
});
490490
plugin.setSymbolValue(symbolHeyApiRegistry, node);
491491
const registryNode = $.field(registryName, (f) =>
492492
f
@@ -509,11 +509,10 @@ export const generateClassSdk = ({
509509
category: 'external',
510510
resource: '@angular/core.Injectable',
511511
}).placeholder,
512-
(o) => o.prop('providedIn', () => $.literal('root')),
512+
(o) => o.prop('providedIn', $.literal('root')),
513513
),
514514
)
515-
.do(...currentClass.nodes)
516-
.$render();
515+
.do(...currentClass.nodes);
517516
plugin.setSymbolValue(symbol, node);
518517
};
519518

packages/openapi-ts/src/plugins/@pinia/colada/v0/plugin.ts

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { registryName } from '~/plugins/@hey-api/sdk/shared/class';
12
import { operationClasses } from '~/plugins/@hey-api/sdk/shared/operation';
23
import { stringCase } from '~/utils/stringCase';
34

@@ -64,32 +65,36 @@ export const handlerV0: PiniaColadaPlugin['Handler'] = ({ plugin }) => {
6465
})
6566
: undefined;
6667
const entry = classes ? classes.values().next().value : undefined;
67-
const queryFn =
68-
// TODO: this should use class graph to determine correct path string
69-
// as it's really easy to break once we change the class casing
70-
entry
71-
? [
72-
plugin.referenceSymbol({
73-
category: 'utility',
74-
resource: 'class',
75-
resourceId: entry.path[0],
76-
tool: 'sdk',
77-
}).placeholder,
78-
...entry.path.slice(1).map((className: string) =>
79-
stringCase({
80-
case: 'camelCase',
81-
value: className,
82-
}),
83-
),
84-
entry.methodName,
85-
]
86-
.filter(Boolean)
87-
.join('.')
88-
: plugin.referenceSymbol({
89-
category: 'sdk',
90-
resource: 'operation',
91-
resourceId: operation.id,
92-
}).placeholder;
68+
// TODO: this should use class graph to determine correct path string
69+
// as it's really easy to break once we change the class casing
70+
let queryFn: string;
71+
if (entry) {
72+
const symbolClass = plugin.referenceSymbol({
73+
category: 'utility',
74+
resource: 'class',
75+
resourceId: entry.path[0],
76+
tool: 'sdk',
77+
});
78+
queryFn = [
79+
symbolClass.placeholder,
80+
...(sdkPlugin.config.instance ? [registryName, 'get()'] : []),
81+
...entry.path.slice(1).map((className) =>
82+
stringCase({
83+
case: 'camelCase',
84+
value: className,
85+
}),
86+
),
87+
entry.methodName,
88+
]
89+
.filter(Boolean)
90+
.join('.');
91+
} else {
92+
queryFn = plugin.referenceSymbol({
93+
category: 'sdk',
94+
resource: 'operation',
95+
resourceId: operation.id,
96+
}).placeholder;
97+
}
9398

9499
if (plugin.hooks.operation.isQuery(operation)) {
95100
if (plugin.config.queryOptions.enabled) {

packages/openapi-ts/src/plugins/swr/v2/plugin.ts

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { registryName } from '~/plugins/@hey-api/sdk/shared/class';
12
import { operationClasses } from '~/plugins/@hey-api/sdk/shared/operation';
23
import { stringCase } from '~/utils/stringCase';
34

@@ -29,32 +30,36 @@ export const handlerV2: SwrPlugin['Handler'] = ({ plugin }) => {
2930
})
3031
: undefined;
3132
const entry = classes ? classes.values().next().value : undefined;
32-
const queryFn =
33-
// TODO: this should use class graph to determine correct path string
34-
// as it's really easy to break once we change the class casing
35-
entry
36-
? [
37-
plugin.referenceSymbol({
38-
category: 'utility',
39-
resource: 'class',
40-
resourceId: entry.path[0],
41-
tool: 'sdk',
42-
}).placeholder,
43-
...entry.path.slice(1).map((className) =>
44-
stringCase({
45-
case: 'camelCase',
46-
value: className,
47-
}),
48-
),
49-
entry.methodName,
50-
]
51-
.filter(Boolean)
52-
.join('.')
53-
: plugin.referenceSymbol({
54-
category: 'sdk',
55-
resource: 'operation',
56-
resourceId: operation.id,
57-
}).placeholder;
33+
// TODO: this should use class graph to determine correct path string
34+
// as it's really easy to break once we change the class casing
35+
let queryFn: string;
36+
if (entry) {
37+
const symbolClass = plugin.referenceSymbol({
38+
category: 'utility',
39+
resource: 'class',
40+
resourceId: entry.path[0],
41+
tool: 'sdk',
42+
});
43+
queryFn = [
44+
symbolClass.placeholder,
45+
...(sdkPlugin.config.instance ? [registryName, 'get()'] : []),
46+
...entry.path.slice(1).map((className) =>
47+
stringCase({
48+
case: 'camelCase',
49+
value: className,
50+
}),
51+
),
52+
entry.methodName,
53+
]
54+
.filter(Boolean)
55+
.join('.');
56+
} else {
57+
queryFn = plugin.referenceSymbol({
58+
category: 'sdk',
59+
resource: 'operation',
60+
resourceId: operation.id,
61+
}).placeholder;
62+
}
5863

5964
if (plugin.hooks.operation.isQuery(operation)) {
6065
// if (plugin.config.queryOptions.enabled) {

packages/openapi-ts/src/plugins/swr/v2/useSwr.ts

Lines changed: 28 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
createOperationComment,
77
hasOperationSse,
88
} from '~/plugins/shared/utils/operation';
9-
import { tsc } from '~/tsc';
9+
import type { TsDsl } from '~/ts-dsl';
10+
import { $ } from '~/ts-dsl';
1011

1112
import type { SwrPlugin } from '../types';
1213

@@ -35,86 +36,37 @@ export const createUseSwr = ({
3536
}),
3637
});
3738

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>> = [];
6844
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));
7446
} else {
7547
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'),
8450
);
8551
}
8652

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+
);
11971
plugin.setSymbolValue(symbolUseQueryFn, statement);
12072
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import ts from 'typescript';
2+
3+
import type { ExprInput, MaybeTsDsl } from './base';
4+
import { TsDsl } from './base';
5+
6+
export class AwaitTsDsl extends TsDsl<ts.AwaitExpression> {
7+
private exprNode: MaybeTsDsl<ExprInput>;
8+
9+
constructor(expr: MaybeTsDsl<ExprInput>) {
10+
super();
11+
this.exprNode = expr;
12+
}
13+
14+
$render(): ts.AwaitExpression {
15+
const expr = this.$node(this.exprNode);
16+
return ts.factory.createAwaitExpression(expr);
17+
}
18+
}

0 commit comments

Comments
 (0)