这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lemon-plants-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

@pinia/colada: correctly access instantiated SDKs
4 changes: 2 additions & 2 deletions dev/openapi-ts.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export default defineConfig(() => {
// 'dutchie.json',
// 'invalid',
// 'full.yaml',
'openai.yaml',
// 'opencode.yaml',
// 'openai.yaml',
'opencode.yaml',
// 'sdk-instance.yaml',
// 'string-with-format.yaml',
// 'transformers.json',
Expand Down
6 changes: 5 additions & 1 deletion packages/openapi-ts/src/generate/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { createBinding, mergeBindings, renderIds } from '@hey-api/codegen-core';
import ts from 'typescript';

import { ensureValidIdentifier } from '~/openApi/shared/utils/identifier';
import { TsDsl } from '~/ts-dsl';
import { tsc } from '~/tsc';
import { tsNodeToString } from '~/tsc/utils';

Expand Down Expand Up @@ -186,7 +187,10 @@ export class TypeScriptRenderer implements Renderer {

for (const symbolId of file.symbols.body) {
const value = project.symbols.getValue(symbolId);
if (typeof value === 'string') {
if (value instanceof TsDsl) {
const node = value.$render();
lines.push(tsNodeToString({ node, unescape: true }));
} else if (typeof value === 'string') {
lines.push(value);
} else if (value instanceof Array) {
for (const node of value) {
Expand Down
13 changes: 6 additions & 7 deletions packages/openapi-ts/src/plugins/@hey-api/sdk/shared/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ export const generateClassSdk = ({
plugin.config.instance
? $.new(refChildClass.placeholder).args(
$.object((o) =>
o.prop('client', () => $('this').attr('client')),
o.prop('client', $('this').attr('client')),
),
)
: $(refChildClass.placeholder),
Expand All @@ -402,7 +402,7 @@ export const generateClassSdk = ({
? $.return(
$.new(refChildClass.placeholder).args(
$.object((o) =>
o.prop('client', () => $('this').attr('client')),
o.prop('client', $('this').attr('client')),
),
),
)
Expand All @@ -426,7 +426,7 @@ export const generateClassSdk = ({
const node = createClientClass({
plugin,
symbol: symbolHeyApiClient,
}).$render();
});
plugin.setSymbolValue(symbolHeyApiClient, node);
}

Expand Down Expand Up @@ -486,7 +486,7 @@ export const generateClassSdk = ({
plugin,
sdkName: symbol.placeholder,
symbol: symbolHeyApiRegistry,
}).$render();
});
plugin.setSymbolValue(symbolHeyApiRegistry, node);
const registryNode = $.field(registryName, (f) =>
f
Expand All @@ -509,11 +509,10 @@ export const generateClassSdk = ({
category: 'external',
resource: '@angular/core.Injectable',
}).placeholder,
(o) => o.prop('providedIn', () => $.literal('root')),
(o) => o.prop('providedIn', $.literal('root')),
),
)
.do(...currentClass.nodes)
.$render();
.do(...currentClass.nodes);
plugin.setSymbolValue(symbol, node);
};

Expand Down
57 changes: 31 additions & 26 deletions packages/openapi-ts/src/plugins/@pinia/colada/v0/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { registryName } from '~/plugins/@hey-api/sdk/shared/class';
import { operationClasses } from '~/plugins/@hey-api/sdk/shared/operation';
import { stringCase } from '~/utils/stringCase';

Expand Down Expand Up @@ -64,32 +65,36 @@ export const handlerV0: PiniaColadaPlugin['Handler'] = ({ plugin }) => {
})
: undefined;
const entry = classes ? classes.values().next().value : undefined;
const queryFn =
// TODO: this should use class graph to determine correct path string
// as it's really easy to break once we change the class casing
entry
? [
plugin.referenceSymbol({
category: 'utility',
resource: 'class',
resourceId: entry.path[0],
tool: 'sdk',
}).placeholder,
...entry.path.slice(1).map((className: string) =>
stringCase({
case: 'camelCase',
value: className,
}),
),
entry.methodName,
]
.filter(Boolean)
.join('.')
: plugin.referenceSymbol({
category: 'sdk',
resource: 'operation',
resourceId: operation.id,
}).placeholder;
// TODO: this should use class graph to determine correct path string
// as it's really easy to break once we change the class casing
let queryFn: string;
if (entry) {
const symbolClass = plugin.referenceSymbol({
category: 'utility',
resource: 'class',
resourceId: entry.path[0],
tool: 'sdk',
});
queryFn = [
symbolClass.placeholder,
...(sdkPlugin.config.instance ? [registryName, 'get()'] : []),
...entry.path.slice(1).map((className) =>
stringCase({
case: 'camelCase',
value: className,
}),
),
entry.methodName,
]
.filter(Boolean)
.join('.');
} else {
queryFn = plugin.referenceSymbol({
category: 'sdk',
resource: 'operation',
resourceId: operation.id,
}).placeholder;
}

if (plugin.hooks.operation.isQuery(operation)) {
if (plugin.config.queryOptions.enabled) {
Expand Down
57 changes: 31 additions & 26 deletions packages/openapi-ts/src/plugins/swr/v2/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { registryName } from '~/plugins/@hey-api/sdk/shared/class';
import { operationClasses } from '~/plugins/@hey-api/sdk/shared/operation';
import { stringCase } from '~/utils/stringCase';

Expand Down Expand Up @@ -29,32 +30,36 @@ export const handlerV2: SwrPlugin['Handler'] = ({ plugin }) => {
})
: undefined;
const entry = classes ? classes.values().next().value : undefined;
const queryFn =
// TODO: this should use class graph to determine correct path string
// as it's really easy to break once we change the class casing
entry
? [
plugin.referenceSymbol({
category: 'utility',
resource: 'class',
resourceId: entry.path[0],
tool: 'sdk',
}).placeholder,
...entry.path.slice(1).map((className) =>
stringCase({
case: 'camelCase',
value: className,
}),
),
entry.methodName,
]
.filter(Boolean)
.join('.')
: plugin.referenceSymbol({
category: 'sdk',
resource: 'operation',
resourceId: operation.id,
}).placeholder;
// TODO: this should use class graph to determine correct path string
// as it's really easy to break once we change the class casing
let queryFn: string;
if (entry) {
const symbolClass = plugin.referenceSymbol({
category: 'utility',
resource: 'class',
resourceId: entry.path[0],
tool: 'sdk',
});
queryFn = [
symbolClass.placeholder,
...(sdkPlugin.config.instance ? [registryName, 'get()'] : []),
...entry.path.slice(1).map((className) =>
stringCase({
case: 'camelCase',
value: className,
}),
),
entry.methodName,
]
.filter(Boolean)
.join('.');
} else {
queryFn = plugin.referenceSymbol({
category: 'sdk',
resource: 'operation',
resourceId: operation.id,
}).placeholder;
}

if (plugin.hooks.operation.isQuery(operation)) {
// if (plugin.config.queryOptions.enabled) {
Expand Down
104 changes: 28 additions & 76 deletions packages/openapi-ts/src/plugins/swr/v2/useSwr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
createOperationComment,
hasOperationSse,
} from '~/plugins/shared/utils/operation';
import { tsc } from '~/tsc';
import type { TsDsl } from '~/ts-dsl';
import { $ } from '~/ts-dsl';

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

Expand Down Expand Up @@ -35,86 +36,37 @@ export const createUseSwr = ({
}),
});

const awaitSdkExpression = tsc.awaitExpression({
expression: tsc.callExpression({
functionName: queryFn,
parameters: [
tsc.objectExpression({
multiLine: true,
obj: [
// {
// spread: optionsParamName,
// },
// {
// spread: 'queryKey[0]',
// },
// {
// key: 'signal',
// shorthand: true,
// value: tsc.identifier({
// text: 'signal',
// }),
// },
{
key: 'throwOnError',
value: true,
},
],
}),
],
}),
});
const statements: Array<ts.Statement> = [];
const awaitSdkFn = $(queryFn)
.call($.object((o) => o.prop('throwOnError', $.literal(true))))
.await();

const statements: Array<ts.Statement | TsDsl<any>> = [];
if (plugin.getPluginOrThrow('@hey-api/sdk').config.responseStyle === 'data') {
statements.push(
tsc.returnVariable({
expression: awaitSdkExpression,
}),
);
statements.push($.return(awaitSdkFn));
} else {
statements.push(
tsc.constVariable({
destructure: true,
expression: awaitSdkExpression,
name: 'data',
}),
tsc.returnVariable({
expression: 'data',
}),
$.const().object('data').assign(awaitSdkFn),
$.return('data'),
);
}

const statement = tsc.constVariable({
comment: plugin.config.comments
? createOperationComment({ operation })
: undefined,
exportConst: symbolUseQueryFn.exported,
expression: tsc.arrowFunction({
parameters: [
// {
// isRequired: isRequiredOptions,
// name: optionsParamName,
// type: typeData,
// },
],
statements: [
tsc.returnStatement({
expression: tsc.callExpression({
functionName: symbolUseSwr.placeholder,
parameters: [
tsc.stringLiteral({
text: operation.path,
}),
tsc.arrowFunction({
async: true,
statements,
}),
],
}),
}),
],
}),
name: symbolUseQueryFn.placeholder,
});
const statement = $.const(symbolUseQueryFn.placeholder)
.export(symbolUseQueryFn.exported)
.$if(
plugin.config.comments && createOperationComment({ operation }),
(c, v) => c.describe(v as Array<string>),
)
.assign(
$.func().do(
$.return(
$(symbolUseSwr.placeholder).call(
$.literal(operation.path),
$.func()
.async()
.do(...statements),
),
),
),
);
plugin.setSymbolValue(symbolUseQueryFn, statement);
};
18 changes: 18 additions & 0 deletions packages/openapi-ts/src/ts-dsl/await.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import ts from 'typescript';

import type { ExprInput, MaybeTsDsl } from './base';
import { TsDsl } from './base';

export class AwaitTsDsl extends TsDsl<ts.AwaitExpression> {
private exprNode: MaybeTsDsl<ExprInput>;

constructor(expr: MaybeTsDsl<ExprInput>) {
super();
this.exprNode = expr;
}

$render(): ts.AwaitExpression {
const expr = this.$node(this.exprNode);
return ts.factory.createAwaitExpression(expr);
}
}
Loading
Loading