Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 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/smooth-ties-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

feat(plugin): add `@pinia/colada` plugin
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// This file is auto-generated by @hey-api/openapi-ts

export const _400Schema = {
description: 'Model with number-only name',
type: 'string'
} as const;
export const useCallWithParametersMutation = async (params: {
query: unknown;
path: unknown;
headers: unknown;
body: unknown;
cookies: export const _400Schema = {
description: 'Model with number-only name',
type: 'string'
} as const;

export const camelCaseCommentWithBreaksSchema = {
description: `Testing multiline comments in string: First line
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// This file is auto-generated by @hey-api/openapi-ts

export const _400Schema = {
description: 'Model with number-only name',
type: 'string'
} as const;
export const useCallWithParametersMutation = async (params: {
query: unknown;
path: unknown;
headers: unknown;
body: unknown;
cookies: export const _400Schema = {
description: 'Model with number-only name',
type: 'string'
} as const;

export const camelCaseCommentWithBreaksSchema = {
description: `Testing multiline comments in string: First line
Expand Down
11 changes: 10 additions & 1 deletion packages/openapi-ts-tests/main/test/plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,15 @@ for (const version of versions) {
description:
'generate Fetch API client with TanStack Vue Query plugin with custom names',
},
// TODO: add Pinia Colada snapshots
// {
// config: createConfig({
// output: 'fetch',
// plugins: ['@pinia/colada', '@hey-api/client-fetch'],
// }),
// description:
// 'generate Fetch API client with Pinia Colada plugin',
// },
{
config: createConfig({
output: 'default',
Expand Down Expand Up @@ -514,7 +523,7 @@ for (const version of versions) {
},
];

it.each(scenarios)('$description', async ({ config }) => {
it.only.each(scenarios)('$description', async ({ config }) => {
await createClient(config);

const outputPath =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { schemaToType } from './plugin';
import { typesId } from './ref';
import type { HeyApiTypeScriptPlugin, PluginState } from './types';

const irParametersToIrSchema = ({
// TODO: exported just for @pinia/colada, remove export once that plugin does not depend on it
export const irParametersToIrSchema = ({
parameters,
}: {
parameters: Record<string, IR.ParameterObject>;
Expand Down
104 changes: 104 additions & 0 deletions packages/openapi-ts/src/plugins/@pinia/colada/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { definePluginConfig } from '../../shared/utils/config';
import { handler } from './plugin';
import type { PiniaColadaPlugin } from './types';

export const defaultConfig: PiniaColadaPlugin['Config'] = {
config: {
enablePaginationOnKey: undefined,
errorHandling: 'specific',
exportFromIndex: false,
groupByTag: false,
importPath: '@pinia/colada',
includeTypes: true,
prefixUse: true,
resolveQuery: undefined,
resolveQueryKey: undefined,
suffixQueryMutation: true,
useInfiniteQueries: false,
},
dependencies: ['@hey-api/typescript'],
handler: handler as PiniaColadaPlugin['Handler'],
name: '@pinia/colada',
output: '@pinia/colada',
resolveConfig: (plugin, context) => {
// plugin.config.infiniteQueryKeys = context.valueToObject({
// defaultValue: {
// case: plugin.config.case ?? 'camelCase',
// enabled: true,
// name: '{{name}}InfiniteQueryKey',
// tags: false,
// },
// mappers: {
// boolean: (enabled) => ({ enabled }),
// function: (name) => ({ name }),
// string: (name) => ({ name }),
// },
// value: plugin.config.infiniteQueryKeys,
// });

// plugin.config.infiniteQueryOptions = context.valueToObject({
// defaultValue: {
// case: plugin.config.case ?? 'camelCase',
// enabled: true,
// meta: false,
// name: '{{name}}InfiniteOptions',
// },
// mappers: {
// boolean: (enabled) => ({ enabled }),
// function: (name) => ({ name }),
// string: (name) => ({ name }),
// },
// value: plugin.config.infiniteQueryOptions,
// });

// plugin.config.mutationOptions = context.valueToObject({
// defaultValue: {
// case: plugin.config.case ?? 'camelCase',
// enabled: true,
// meta: false,
// name: '{{name}}Mutation',
// },
// mappers: {
// boolean: (enabled) => ({ enabled }),
// function: (name) => ({ name }),
// string: (name) => ({ name }),
// },
// value: plugin.config.mutationOptions,
// });

// plugin.config.queryKeys = context.valueToObject({
// defaultValue: {
// case: plugin.config.case ?? 'camelCase',
// enabled: true,
// name: '{{name}}QueryKey',
// tags: false,
// },
// mappers: {
// boolean: (enabled) => ({ enabled }),
// function: (name) => ({ name }),
// string: (name) => ({ name }),
// },
// value: plugin.config.queryKeys,
// });

// plugin.config.queryOptions = context.valueToObject({
// defaultValue: {
// case: plugin.config.case ?? 'camelCase',
// enabled: true,
// meta: false,
// name: '{{name}}Options',
// },
// mappers: {
// boolean: (enabled) => ({ enabled }),
// function: (name) => ({ name }),
// string: (name) => ({ name }),
// },
// value: plugin.config.queryOptions,
// });
},
};

/**
* Type helper for `@pinia/colada` plugin, returns {@link Plugin.Config} object
*/
export const defineConfig = definePluginConfig(defaultConfig);
2 changes: 2 additions & 0 deletions packages/openapi-ts/src/plugins/@pinia/colada/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { defaultConfig, defineConfig } from './config';
export type { PiniaColadaPlugin } from './types';
26 changes: 26 additions & 0 deletions packages/openapi-ts/src/plugins/@pinia/colada/mutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { GeneratedFile } from '../../../generate/file';
import type { IR } from '../../../ir/types';
import type { PiniaColadaPlugin } from './types';
import { createComposable } from './utils';

/**
* Creates a mutation function for an operation
*/
export const createMutationFunction = ({
context,
file,
operation,
plugin,
}: {
context: IR.Context;
file: GeneratedFile;
operation: IR.OperationObject;
plugin: PiniaColadaPlugin['Instance'];
}) => {
// Allow hooks to customize or skip mutation generation
if (plugin.config.onMutation && plugin.config.onMutation(operation) === false) {
return;
}

createComposable({ context, file, isQuery: false, operation, plugin });
};
59 changes: 59 additions & 0 deletions packages/openapi-ts/src/plugins/@pinia/colada/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { clientId } from '../../@hey-api/client-core/utils';
import { createMutationFunction } from './mutation';
import { createQueryFunction } from './query';
import type { PiniaColadaPlugin } from './types';
import { isQuery } from './utils';

export const handler: PiniaColadaPlugin['Handler'] = ({ plugin }) => {
if (!plugin.config.groupByTag) {
plugin.createFile({
id: plugin.name,
path: plugin.output,
});
}

// Create files based on grouping strategy
const getFile = (tag: string) => {
if (!plugin.config.groupByTag) {
return (
plugin.context.file({ id: plugin.name }) ??
plugin.createFile({
id: plugin.name,
path: plugin.output,
})
);
}

const fileId = `${plugin.name}/${tag}`;
return (
plugin.context.file({ id: fileId }) ??
plugin.createFile({
id: fileId,
path: `${plugin.output}/${tag}`,
})
);
};

plugin.forEach('operation', ({ operation }) => {
const file = getFile(operation.tags?.[0] || 'default');

// Determine if the operation should be a query or mutation
if (isQuery(operation, plugin)) {
createQueryFunction({ context: plugin.context, file, operation, plugin });
} else {
createMutationFunction({ context: plugin.context, file, operation, plugin });
}
})

// Add client import to all generated files
Object.entries(plugin.context.files).forEach(([fileId, file]) => {
if (fileId.startsWith(plugin.name)) {
// Make sure we have a client import
file.import({
alias: '_heyApiClient',
module: file.relativePathToFile({ context: plugin.context, id: clientId }),
name: 'client',
});
}
});
};
25 changes: 25 additions & 0 deletions packages/openapi-ts/src/plugins/@pinia/colada/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { GeneratedFile } from '../../../generate/file';
import type { IR } from '../../../ir/types';
import type { PiniaColadaPlugin } from './types';
import { createComposable } from './utils';

/**
* Creates a query function for an operation
*/
export const createQueryFunction = ({
context,
file,
operation,
plugin,
}: {
context: IR.Context;
file: GeneratedFile;
operation: IR.OperationObject;
plugin: PiniaColadaPlugin['Instance'];
}) => {
if (plugin.config.onQuery && plugin.config.onQuery(operation) === false) {
return;
}

createComposable({ context, file, isQuery: true, operation, plugin });
};
Loading
Loading