Skip to content

Commit 1cd56c2

Browse files
committed
fix(colada): isolate client-nuxt specific options shape (composable)
1 parent 3b08665 commit 1cd56c2

File tree

4 files changed

+60
-18
lines changed

4 files changed

+60
-18
lines changed

packages/openapi-ts/src/plugins/@pinia/colada/mutationOptions.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import { createOperationComment } from '../../shared/utils/operation';
77
import { handleMeta } from './meta';
88
import type { PluginState } from './state';
99
import type { PiniaColadaPlugin } from './types';
10-
import { useTypeData, useTypeError, useTypeResponse } from './utils';
10+
import {
11+
getPublicTypeData,
12+
useTypeData,
13+
useTypeError,
14+
useTypeResponse,
15+
} from './utils';
1116

1217
const mutationOptionsType = 'UseMutationOptions';
1318

@@ -46,6 +51,10 @@ export const createMutationOptions = ({
4651
const typeData = useTypeData({ file, operation, plugin });
4752
const typeError = useTypeError({ file, operation, plugin });
4853
const typeResponse = useTypeResponse({ file, operation, plugin });
54+
const { isNuxtClient, strippedTypeData } = getPublicTypeData({
55+
plugin,
56+
typeData,
57+
});
4958

5059
const identifierMutationOptions = file.identifier({
5160
$ref: `#/pinia-colada-mutation-options/${operation.id}`,
@@ -108,9 +117,12 @@ export const createMutationOptions = ({
108117
async: true,
109118
multiLine: true,
110119
parameters: [
111-
{
112-
name: fnOptions,
113-
},
120+
isNuxtClient
121+
? {
122+
name: fnOptions,
123+
type: `Partial<${strippedTypeData}>`,
124+
}
125+
: { name: fnOptions },
114126
],
115127
statements,
116128
}),
@@ -136,11 +148,12 @@ export const createMutationOptions = ({
136148
{
137149
isRequired: false,
138150
name: 'options',
139-
type: `Partial<${typeData}>`,
151+
type: `Partial<${strippedTypeData}>`,
140152
},
141153
],
142-
// TODO: better types syntax
143-
returnType: `${mutationOptionsType}<${typeResponse}, ${typeData}, ${typeError.name}>`,
154+
returnType: isNuxtClient
155+
? `${mutationOptionsType}<${typeResponse}, ${strippedTypeData}, ${typeError.name}>`
156+
: `${mutationOptionsType}<${typeResponse}, ${typeData}, ${typeError.name}>`,
144157
statements: [
145158
tsc.returnStatement({
146159
expression: tsc.objectExpression({

packages/openapi-ts/src/plugins/@pinia/colada/queryKey.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { IR } from '../../../ir/types';
77
import { type Property, tsc } from '../../../tsc';
88
import { getClientBaseUrlKey } from '../../@hey-api/client-core/utils';
99
import type { PiniaColadaPlugin } from './types';
10-
import { useTypeData } from './utils';
10+
import { getPublicTypeData, useTypeData } from './utils';
1111

1212
const createQueryKeyFn = 'createQueryKey';
1313
const queryKeyName = 'QueryKey';
@@ -352,6 +352,7 @@ export const queryKeyStatement = ({
352352
plugin: PiniaColadaPlugin['Instance'];
353353
}) => {
354354
const typeData = useTypeData({ file, operation, plugin });
355+
const { strippedTypeData } = getPublicTypeData({ plugin, typeData });
355356
const identifier = file.identifier({
356357
// TODO: refactor for better cross-plugin compatibility
357358
$ref: `#/pinia-colada-query-key/${operation.id}`,
@@ -367,7 +368,7 @@ export const queryKeyStatement = ({
367368
{
368369
isRequired: hasOperationDataRequired(operation),
369370
name: 'options',
370-
type: typeData,
371+
type: strippedTypeData,
371372
},
372373
],
373374
statements: createQueryKeyLiteral({

packages/openapi-ts/src/plugins/@pinia/colada/queryOptions.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ import {
1616
} from './queryKey';
1717
import type { PluginState } from './state';
1818
import type { PiniaColadaPlugin } from './types';
19-
import { useTypeData, useTypeError, useTypeResponse } from './utils';
19+
import {
20+
getPublicTypeData,
21+
useTypeData,
22+
useTypeError,
23+
useTypeResponse,
24+
} from './utils';
2025

2126
const queryOptionsType = 'UseQueryOptions';
2227
const optionsParamName = 'options';
@@ -75,6 +80,10 @@ export const createQueryOptions = ({
7580
const typeData = useTypeData({ file, operation, plugin });
7681
const typeError = useTypeError({ file, operation, plugin });
7782
const typeResponse = useTypeResponse({ file, operation, plugin });
83+
const { isNuxtClient, strippedTypeData } = getPublicTypeData({
84+
plugin,
85+
typeData,
86+
});
7887

7988
const identifierQueryOptions = file.identifier({
8089
$ref: `#/pinia-colada-query-options/${operation.id}`,
@@ -152,9 +161,12 @@ export const createQueryOptions = ({
152161
async: true,
153162
multiLine: true,
154163
parameters: [
155-
{
156-
name: fnOptions,
157-
},
164+
isNuxtClient
165+
? {
166+
name: fnOptions,
167+
type: `Partial<${strippedTypeData}>`,
168+
}
169+
: { name: fnOptions },
158170
],
159171
statements,
160172
}),
@@ -180,11 +192,12 @@ export const createQueryOptions = ({
180192
{
181193
isRequired: isRequiredOptions,
182194
name: optionsParamName,
183-
type: typeData,
195+
type: strippedTypeData,
184196
},
185197
],
186-
// TODO: better types syntax
187-
returnType: `${queryOptionsType}<${typeResponse}, ${typeError.name}>`,
198+
returnType: isNuxtClient
199+
? `${queryOptionsType}<${typeResponse}, ${strippedTypeData}, ${typeError.name}>`
200+
: `${queryOptionsType}<${typeResponse}, ${typeError.name}>`,
188201
statements: [
189202
tsc.returnStatement({
190203
expression: tsc.objectExpression({

packages/openapi-ts/src/plugins/@pinia/colada/utils.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ export const getFileForOperation = ({
4949
};
5050
};
5151

52+
export const getPublicTypeData = ({
53+
plugin,
54+
typeData,
55+
}: {
56+
plugin: PiniaColadaPlugin['Instance'];
57+
typeData: string;
58+
}) => {
59+
const client = getClientPlugin(plugin.context.config);
60+
const isNuxtClient = client.name === '@hey-api/client-nuxt';
61+
const strippedTypeData = isNuxtClient
62+
? `Omit<${typeData}, 'composable'>`
63+
: typeData;
64+
65+
return { isNuxtClient, strippedTypeData };
66+
};
67+
5268
export const useTypeData = ({
5369
file,
5470
operation,
@@ -59,8 +75,7 @@ export const useTypeData = ({
5975
plugin: PiniaColadaPlugin['Instance'];
6076
}) => {
6177
const pluginSdk = plugin.getPlugin('@hey-api/sdk')!;
62-
const typeData = operationOptionsType({ file, operation, plugin: pluginSdk });
63-
return typeData;
78+
return operationOptionsType({ file, operation, plugin: pluginSdk });
6479
};
6580

6681
export const useTypeError = ({

0 commit comments

Comments
 (0)