Skip to content

Commit 0b6c08e

Browse files
committed
added tanstack
1 parent 93bef87 commit 0b6c08e

File tree

6 files changed

+10378
-14
lines changed

6 files changed

+10378
-14
lines changed

__fixtures__/output/hooks.ts

Lines changed: 9799 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import schema from '../../../__fixtures__/openapi/swagger.json';
2+
import { defaultSchemaSDKOptions } from '../src/types';
3+
import { generateReactQueryHooks } from '../src/openapi';
4+
import { writeFileSync } from 'fs';
5+
import { OpenAPIOptions } from '../dist';
6+
7+
/**
8+
* Return a new schema object filtered to only include the given paths.
9+
*/
10+
function getFilteredSchema(schema: any, patterns: string[]): any {
11+
const filteredPaths = Object.fromEntries(
12+
Object.entries(schema.paths).filter(([p]) => patterns.includes(p))
13+
);
14+
return { ...schema, paths: filteredPaths } as any;
15+
}
16+
17+
describe('generateReactQueryHooks', () => {
18+
it('returns empty array when hooks are disabled', () => {
19+
const hooks = generateReactQueryHooks(defaultSchemaSDKOptions as any, schema as any);
20+
expect(hooks).toEqual([]);
21+
});
22+
23+
it('returns hook files when enabled for filtered schema', () => {
24+
const options: OpenAPIOptions = {
25+
...(defaultSchemaSDKOptions as any),
26+
hooks: {
27+
enabled: true,
28+
path: 'hooks',
29+
typesImportPath: './swagger-client',
30+
contextImportPath: './k8s-context',
31+
},
32+
} as any;
33+
// const testSchema = getFilteredSchema(schema as any, patterns);
34+
const testSchema = schema as any;
35+
const hooks = generateReactQueryHooks(options, testSchema);
36+
expect(hooks.length).toBeGreaterThan(0);
37+
// Verify each hook file has expected properties
38+
hooks.forEach(hook => {
39+
expect(hook).toHaveProperty('fileName');
40+
expect(hook).toHaveProperty('code');
41+
});
42+
43+
writeFileSync(
44+
__dirname + '/../../../__fixtures__/output/hooks.ts',
45+
hooks[0].code
46+
);
47+
});
48+
});
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
export const generateContext = (Type: string, pathToClient: string) => `
2+
'use client'
3+
4+
import React, { createContext, useContext, useMemo, useState } from 'react'
5+
import { KubernetesClient } from '${pathToClient}'
6+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
7+
import generate from '@babel/generator'
8+
9+
// Configuration types
10+
export interface ${Type}Config {
11+
restEndpoint: string
12+
headers?: Record<string, string>
13+
}
14+
15+
// Context types
16+
interface ${Type}ContextValue {
17+
client: ${Type}Client
18+
}
19+
20+
// Create context
21+
const ${Type}Context = createContext<${Type}ContextValue | undefined>(undefined)
22+
23+
// Query client for TanStack Query
24+
const queryClient = new QueryClient({
25+
defaultOptions: {
26+
queries: {
27+
refetchOnWindowFocus: false,
28+
retry: 3,
29+
staleTime: 30 * 1000, // 30 seconds
30+
gcTime: 5 * 60 * 1000, // 5 minutes
31+
},
32+
},
33+
})
34+
35+
// Provider props
36+
interface ${Type}ProviderProps {
37+
children: React.ReactNode
38+
initialConfig?: Partial<${Type}Config>
39+
}
40+
41+
// Provider component
42+
export function ${Type}Provider({
43+
children,
44+
initialConfig
45+
}: ${Type}ProviderProps) {
46+
const [config, setConfig] = useState<${Type}Config>({
47+
restEndpoint: initialConfig?.restEndpoint,
48+
headers: initialConfig?.headers || {},
49+
})
50+
51+
// Create client instance
52+
const client = useMemo(() => {
53+
return new ${Type}Client({
54+
restEndpoint: config.restEndpoint,
55+
})
56+
}, [config.restEndpoint])
57+
58+
const contextValue: ${Type}ContextValue = {
59+
client
60+
}
61+
62+
return (
63+
<QueryClientProvider client={queryClient}>
64+
<${Type}Context.Provider value={contextValue}>
65+
{children}
66+
</${Type}Context.Provider>
67+
</QueryClientProvider>
68+
)
69+
}
70+
71+
// Hook to use ${Type} context
72+
export function use${Type}() {
73+
const context = useContext(${Type}Context)
74+
if (!context) {
75+
throw new Error('use${Type} must be used within a ${Type}Provider')
76+
}
77+
return context
78+
}
79+
80+
// Export query client for use in hooks
81+
export { queryClient };`;

0 commit comments

Comments
 (0)