From 55ef27994740ba0af0bc8db17b3e71bfb5c11714 Mon Sep 17 00:00:00 2001 From: Lubos Date: Mon, 8 Dec 2025 07:03:24 +0800 Subject: [PATCH] fix(@hey-api/sdk): correctly map flat parameters --- .changeset/deep-lies-wish.md | 5 + dev/openapi-ts.config.ts | 38 ++--- dev/package.json | 3 + dev/playground.ts | 16 ++- .../__snapshots__/opencode/flat/sdk.gen.ts | 130 +++++++++--------- .../plugins/@hey-api/sdk/shared/operation.ts | 7 +- 6 files changed, 113 insertions(+), 86 deletions(-) create mode 100644 .changeset/deep-lies-wish.md diff --git a/.changeset/deep-lies-wish.md b/.changeset/deep-lies-wish.md new file mode 100644 index 000000000..a13cb13d1 --- /dev/null +++ b/.changeset/deep-lies-wish.md @@ -0,0 +1,5 @@ +--- +'@hey-api/openapi-ts': patch +--- + +**@hey-api/sdk**: correctly map flat parameters diff --git a/dev/openapi-ts.config.ts b/dev/openapi-ts.config.ts index 2320c3687..f9906b457 100644 --- a/dev/openapi-ts.config.ts +++ b/dev/openapi-ts.config.ts @@ -40,10 +40,10 @@ export default defineConfig(() => { // 'circular.yaml', // 'dutchie.json', // 'invalid', - 'full.yaml', + // 'full.yaml', // 'object-property-names.yaml', // 'openai.yaml', - // 'opencode.yaml', + 'opencode.yaml', // 'pagination-ref.yaml', // 'sdk-instance.yaml', // 'string-with-format.yaml', @@ -96,15 +96,15 @@ export default defineConfig(() => { // suffix: '.meh', // }, // format: 'prettier', - // importFileExtension: '.ts', + importFileExtension: '.ts', // indexFile: false, // lint: 'eslint', path: path.resolve(__dirname, '.gen'), - tsConfigPath: path.resolve( - __dirname, - 'tsconfig', - 'tsconfig.nodenext.json', - ), + // tsConfigPath: path.resolve( + // __dirname, + // 'tsconfig', + // 'tsconfig.nodenext.json', + // ), }, // '.gen', ], @@ -217,7 +217,7 @@ export default defineConfig(() => { { // baseUrl: false, // exportFromIndex: true, - name: '@hey-api/client-angular', + // name: '@hey-api/client-angular', // runtimeConfigPath: path.resolve(__dirname, 'hey-api.ts'), // runtimeConfigPath: './src/hey-api.ts', // strictBaseUrl: true, @@ -264,14 +264,14 @@ export default defineConfig(() => { // instance: true, name: '@hey-api/sdk', // operationId: false, - // paramsStructure: 'flat', - responseStyle: 'data', + paramsStructure: 'flat', + // responseStyle: 'data', // signature: 'auto', // signature: 'client', // signature: 'object', // transformer: '@hey-api/transformers', // transformer: true, - validator: 'valibot', + // validator: 'valibot', // validator: { // request: 'zod', // response: 'zod', @@ -293,13 +293,13 @@ export default defineConfig(() => { { // bigInt: true, dates: true, - name: '@hey-api/transformers', + // name: '@hey-api/transformers', }, { - name: 'fastify', + // name: 'fastify', }, { - name: 'swr', + // name: 'swr', }, { // case: 'SCREAMING_SNAKE_CASE', @@ -357,7 +357,7 @@ export default defineConfig(() => { }, }, { - name: 'arktype', + // name: 'arktype', types: { infer: true, }, @@ -368,7 +368,7 @@ export default defineConfig(() => { // definitions: 'z{{name}}Definition', exportFromIndex: true, // metadata: true, - name: 'valibot', + // name: 'valibot', // requests: { // case: 'PascalCase', // name: '{{name}}Data', @@ -445,7 +445,7 @@ export default defineConfig(() => { }, // exportFromIndex: true, metadata: true, - name: 'zod', + // name: 'zod', // requests: { // // case: 'SCREAMING_SNAKE_CASE', // // name: 'z{{name}}TestData', @@ -522,7 +522,7 @@ export default defineConfig(() => { httpResources: { asClass: true, }, - name: '@angular/common', + // name: '@angular/common', }, { exportFromIndex: true, diff --git a/dev/package.json b/dev/package.json index ea3e3da47..c29b68339 100644 --- a/dev/package.json +++ b/dev/package.json @@ -6,6 +6,9 @@ "engines": { "node": ">=20.19.0" }, + "scripts": { + "dev": "ts-node ./playground.ts" + }, "devDependencies": { "@hey-api/codegen-core": "workspace:*", "@hey-api/openapi-ts": "workspace:*", diff --git a/dev/playground.ts b/dev/playground.ts index cf0cd4e77..6152ddd8a 100644 --- a/dev/playground.ts +++ b/dev/playground.ts @@ -1,10 +1,12 @@ import type { DefinePlugin, IR } from '@hey-api/openapi-ts'; +import { authSet } from './.gen/index.ts'; + type MyPluginConfig = { readonly name: 'myplugin' }; type MyPlugin = DefinePlugin; export function f(schema: IR.SchemaObject, plugin: MyPlugin['Instance']) { - plugin.context.resolveIrRef(schema.$ref); + plugin.context.resolveIrRef(schema.$ref!); } export const handler: MyPlugin['Handler'] = ({ plugin }) => { @@ -12,3 +14,15 @@ export const handler: MyPlugin['Handler'] = ({ plugin }) => { console.log(event); }); }; + +console.log( + authSet({ + auth: { + access: '', + expires: 1, + refresh: '', + type: 'oauth', + }, + id: '123', + }), +); diff --git a/packages/openapi-ts-tests/sdks/__snapshots__/opencode/flat/sdk.gen.ts b/packages/openapi-ts-tests/sdks/__snapshots__/opencode/flat/sdk.gen.ts index f694c0d7e..56ce2c581 100644 --- a/packages/openapi-ts-tests/sdks/__snapshots__/opencode/flat/sdk.gen.ts +++ b/packages/openapi-ts-tests/sdks/__snapshots__/opencode/flat/sdk.gen.ts @@ -50,7 +50,7 @@ export const sessionCreate = (parameters?: parentID?: string; title?: string; }, options?: Options) => { - const params = buildClientParams([parameters], [{ in: 'body', key: 'parentID' }, { in: 'body', key: 'title' }]); + const params = buildClientParams([parameters], [{ args: [{ in: 'body', key: 'parentID' }, { in: 'body', key: 'title' }] }]); return (options?.client ?? client).post({ url: '/session', ...options, @@ -69,7 +69,7 @@ export const sessionCreate = (parameters?: export const sessionDelete = (parameters: { id: string; }, options?: Options) => { - const params = buildClientParams([parameters], [{ in: 'path', key: 'id' }]); + const params = buildClientParams([parameters], [{ args: [{ in: 'path', key: 'id' }] }]); return (options?.client ?? client).delete({ url: '/session/{id}', ...options, @@ -83,7 +83,7 @@ export const sessionDelete = (parameters: export const sessionGet = (parameters: { id: string; }, options?: Options) => { - const params = buildClientParams([parameters], [{ in: 'path', key: 'id' }]); + const params = buildClientParams([parameters], [{ args: [{ in: 'path', key: 'id' }] }]); return (options?.client ?? client).get({ url: '/session/{id}', ...options, @@ -98,7 +98,7 @@ export const sessionUpdate = (parameters: id: string; title?: string; }, options?: Options) => { - const params = buildClientParams([parameters], [{ in: 'path', key: 'id' }, { in: 'body', key: 'title' }]); + const params = buildClientParams([parameters], [{ args: [{ in: 'path', key: 'id' }, { in: 'body', key: 'title' }] }]); return (options?.client ?? client).patch({ url: '/session/{id}', ...options, @@ -117,7 +117,7 @@ export const sessionUpdate = (parameters: export const sessionChildren = (parameters: { id: string; }, options?: Options) => { - const params = buildClientParams([parameters], [{ in: 'path', key: 'id' }]); + const params = buildClientParams([parameters], [{ args: [{ in: 'path', key: 'id' }] }]); return (options?.client ?? client).get({ url: '/session/{id}/children', ...options, @@ -134,12 +134,12 @@ export const sessionInit = (parameters: { providerID?: string; modelID?: string; }, options?: Options) => { - const params = buildClientParams([parameters], [ - { in: 'path', key: 'id' }, - { in: 'body', key: 'messageID' }, - { in: 'body', key: 'providerID' }, - { in: 'body', key: 'modelID' } - ]); + const params = buildClientParams([parameters], [{ args: [ + { in: 'path', key: 'id' }, + { in: 'body', key: 'messageID' }, + { in: 'body', key: 'providerID' }, + { in: 'body', key: 'modelID' } + ] }]); return (options?.client ?? client).post({ url: '/session/{id}/init', ...options, @@ -158,7 +158,7 @@ export const sessionInit = (parameters: { export const sessionAbort = (parameters: { id: string; }, options?: Options) => { - const params = buildClientParams([parameters], [{ in: 'path', key: 'id' }]); + const params = buildClientParams([parameters], [{ args: [{ in: 'path', key: 'id' }] }]); return (options?.client ?? client).post({ url: '/session/{id}/abort', ...options, @@ -172,7 +172,7 @@ export const sessionAbort = (parameters: { export const sessionUnshare = (parameters: { id: string; }, options?: Options) => { - const params = buildClientParams([parameters], [{ in: 'path', key: 'id' }]); + const params = buildClientParams([parameters], [{ args: [{ in: 'path', key: 'id' }] }]); return (options?.client ?? client).delete({ url: '/session/{id}/share', ...options, @@ -186,7 +186,7 @@ export const sessionUnshare = (parameters: export const sessionShare = (parameters: { id: string; }, options?: Options) => { - const params = buildClientParams([parameters], [{ in: 'path', key: 'id' }]); + const params = buildClientParams([parameters], [{ args: [{ in: 'path', key: 'id' }] }]); return (options?.client ?? client).post({ url: '/session/{id}/share', ...options, @@ -202,11 +202,11 @@ export const sessionSummarize = (parameter providerID?: string; modelID?: string; }, options?: Options) => { - const params = buildClientParams([parameters], [ - { in: 'path', key: 'id' }, - { in: 'body', key: 'providerID' }, - { in: 'body', key: 'modelID' } - ]); + const params = buildClientParams([parameters], [{ args: [ + { in: 'path', key: 'id' }, + { in: 'body', key: 'providerID' }, + { in: 'body', key: 'modelID' } + ] }]); return (options?.client ?? client).post({ url: '/session/{id}/summarize', ...options, @@ -225,7 +225,7 @@ export const sessionSummarize = (parameter export const sessionMessages = (parameters: { id: string; }, options?: Options) => { - const params = buildClientParams([parameters], [{ in: 'path', key: 'id' }]); + const params = buildClientParams([parameters], [{ args: [{ in: 'path', key: 'id' }] }]); return (options?.client ?? client).get({ url: '/session/{id}/message', ...options, @@ -254,16 +254,16 @@ export const sessionChat = (parameters: { type: 'agent'; } & AgentPartInput)>; }, options?: Options) => { - const params = buildClientParams([parameters], [ - { in: 'path', key: 'id' }, - { in: 'body', key: 'messageID' }, - { in: 'body', key: 'providerID' }, - { in: 'body', key: 'modelID' }, - { in: 'body', key: 'agent' }, - { in: 'body', key: 'system' }, - { in: 'body', key: 'tools' }, - { in: 'body', key: 'parts' } - ]); + const params = buildClientParams([parameters], [{ args: [ + { in: 'path', key: 'id' }, + { in: 'body', key: 'messageID' }, + { in: 'body', key: 'providerID' }, + { in: 'body', key: 'modelID' }, + { in: 'body', key: 'agent' }, + { in: 'body', key: 'system' }, + { in: 'body', key: 'tools' }, + { in: 'body', key: 'parts' } + ] }]); return (options?.client ?? client).post({ url: '/session/{id}/message', ...options, @@ -283,7 +283,7 @@ export const sessionMessage = (parameters: id: string; messageID: string; }, options?: Options) => { - const params = buildClientParams([parameters], [{ in: 'path', key: 'id' }, { in: 'path', key: 'messageID' }]); + const params = buildClientParams([parameters], [{ args: [{ in: 'path', key: 'id' }, { in: 'path', key: 'messageID' }] }]); return (options?.client ?? client).get({ url: '/session/{id}/message/{messageID}', ...options, @@ -299,11 +299,11 @@ export const sessionShell = (parameters: { agent?: string; command?: string; }, options?: Options) => { - const params = buildClientParams([parameters], [ - { in: 'path', key: 'id' }, - { in: 'body', key: 'agent' }, - { in: 'body', key: 'command' } - ]); + const params = buildClientParams([parameters], [{ args: [ + { in: 'path', key: 'id' }, + { in: 'body', key: 'agent' }, + { in: 'body', key: 'command' } + ] }]); return (options?.client ?? client).post({ url: '/session/{id}/shell', ...options, @@ -324,11 +324,11 @@ export const sessionRevert = (parameters: messageID?: string; partID?: string; }, options?: Options) => { - const params = buildClientParams([parameters], [ - { in: 'path', key: 'id' }, - { in: 'body', key: 'messageID' }, - { in: 'body', key: 'partID' } - ]); + const params = buildClientParams([parameters], [{ args: [ + { in: 'path', key: 'id' }, + { in: 'body', key: 'messageID' }, + { in: 'body', key: 'partID' } + ] }]); return (options?.client ?? client).post({ url: '/session/{id}/revert', ...options, @@ -347,7 +347,7 @@ export const sessionRevert = (parameters: export const sessionUnrevert = (parameters: { id: string; }, options?: Options) => { - const params = buildClientParams([parameters], [{ in: 'path', key: 'id' }]); + const params = buildClientParams([parameters], [{ args: [{ in: 'path', key: 'id' }] }]); return (options?.client ?? client).post({ url: '/session/{id}/unrevert', ...options, @@ -363,11 +363,11 @@ export const postSessionByIdPermissionsByPermissionId = ) => { - const params = buildClientParams([parameters], [ - { in: 'path', key: 'id' }, - { in: 'path', key: 'permissionID' }, - { in: 'body', key: 'response' } - ]); + const params = buildClientParams([parameters], [{ args: [ + { in: 'path', key: 'id' }, + { in: 'path', key: 'permissionID' }, + { in: 'body', key: 'response' } + ] }]); return (options?.client ?? client).post({ url: '/session/{id}/permissions/{permissionID}', ...options, @@ -391,7 +391,7 @@ export const configProviders = (options?: export const findText = (parameters: { pattern: string; }, options?: Options) => { - const params = buildClientParams([parameters], [{ in: 'query', key: 'pattern' }]); + const params = buildClientParams([parameters], [{ args: [{ in: 'query', key: 'pattern' }] }]); return (options?.client ?? client).get({ url: '/find', ...options, @@ -405,7 +405,7 @@ export const findText = (parameters: { export const findFiles = (parameters: { query: string; }, options?: Options) => { - const params = buildClientParams([parameters], [{ in: 'query', key: 'query' }]); + const params = buildClientParams([parameters], [{ args: [{ in: 'query', key: 'query' }] }]); return (options?.client ?? client).get({ url: '/find/file', ...options, @@ -419,7 +419,7 @@ export const findFiles = (parameters: { export const findSymbols = (parameters: { query: string; }, options?: Options) => { - const params = buildClientParams([parameters], [{ in: 'query', key: 'query' }]); + const params = buildClientParams([parameters], [{ args: [{ in: 'query', key: 'query' }] }]); return (options?.client ?? client).get({ url: '/find/symbol', ...options, @@ -433,7 +433,7 @@ export const findSymbols = (parameters: { export const fileRead = (parameters: { path: string; }, options?: Options) => { - const params = buildClientParams([parameters], [{ in: 'query', key: 'path' }]); + const params = buildClientParams([parameters], [{ args: [{ in: 'query', key: 'path' }] }]); return (options?.client ?? client).get({ url: '/file', ...options, @@ -457,12 +457,12 @@ export const appLog = (parameters?: { [key: string]: unknown; }; }, options?: Options) => { - const params = buildClientParams([parameters], [ - { in: 'body', key: 'service' }, - { in: 'body', key: 'level' }, - { in: 'body', key: 'message' }, - { in: 'body', key: 'extra' } - ]); + const params = buildClientParams([parameters], [{ args: [ + { in: 'body', key: 'service' }, + { in: 'body', key: 'level' }, + { in: 'body', key: 'message' }, + { in: 'body', key: 'extra' } + ] }]); return (options?.client ?? client).post({ url: '/log', ...options, @@ -486,7 +486,7 @@ export const appAgents = (options?: Option export const tuiAppendPrompt = (parameters?: { text?: string; }, options?: Options) => { - const params = buildClientParams([parameters], [{ in: 'body', key: 'text' }]); + const params = buildClientParams([parameters], [{ args: [{ in: 'body', key: 'text' }] }]); return (options?.client ?? client).post({ url: '/tui/append-prompt', ...options, @@ -535,7 +535,7 @@ export const tuiClearPrompt = (options?: O export const tuiExecuteCommand = (parameters?: { command?: string; }, options?: Options) => { - const params = buildClientParams([parameters], [{ in: 'body', key: 'command' }]); + const params = buildClientParams([parameters], [{ args: [{ in: 'body', key: 'command' }] }]); return (options?.client ?? client).post({ url: '/tui/execute-command', ...options, @@ -556,11 +556,11 @@ export const tuiShowToast = (parameters?: message?: string; variant?: 'info' | 'success' | 'warning' | 'error'; }, options?: Options) => { - const params = buildClientParams([parameters], [ - { in: 'body', key: 'title' }, - { in: 'body', key: 'message' }, - { in: 'body', key: 'variant' } - ]); + const params = buildClientParams([parameters], [{ args: [ + { in: 'body', key: 'title' }, + { in: 'body', key: 'message' }, + { in: 'body', key: 'variant' } + ] }]); return (options?.client ?? client).post({ url: '/tui/show-toast', ...options, @@ -580,7 +580,7 @@ export const authSet = (parameters: { id: string; auth?: Auth; }, options?: Options) => { - const params = buildClientParams([parameters], [{ in: 'path', key: 'id' }, {}]); + const params = buildClientParams([parameters], [{ args: [{ in: 'path', key: 'id' }, { key: 'auth', map: 'body' }] }]); return (options?.client ?? client).put({ url: '/auth/{id}', ...options, diff --git a/packages/openapi-ts/src/plugins/@hey-api/sdk/shared/operation.ts b/packages/openapi-ts/src/plugins/@hey-api/sdk/shared/operation.ts index ed35b7ac7..9f59e0c3b 100644 --- a/packages/openapi-ts/src/plugins/@hey-api/sdk/shared/operation.ts +++ b/packages/openapi-ts/src/plugins/@hey-api/sdk/shared/operation.ts @@ -548,6 +548,8 @@ export const operationStatements = ({ const shape = $.object(); if ('in' in field) { shape.prop('in', $.literal(field.in)); + } + if ('key' in field) { if (field.key) { shape.prop('key', $.literal(field.key)); } @@ -563,7 +565,10 @@ export const operationStatements = ({ }); statements.push( $.const('params').assign( - $(symbol.placeholder).call($.array(...args), $.array(...config)), + $(symbol.placeholder).call( + $.array(...args), + $.array($.object().prop('args', $.array(...config))), + ), ), ); reqOptions.spread('params'); -- 2.51.2