Skip to content

added tanstack #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2025
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
9,799 changes: 9,799 additions & 0 deletions __fixtures__/output/hooks.ts

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions packages/schema-sdk/__tests__/hooks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import schema from '../../../__fixtures__/openapi/swagger.json';
import { defaultSchemaSDKOptions } from '../src/types';
import { generateReactQueryHooks } from '../src/openapi';
import { writeFileSync } from 'fs';
import { OpenAPIOptions } from '../dist';

/**
* Return a new schema object filtered to only include the given paths.
*/
function getFilteredSchema(schema: any, patterns: string[]): any {
const filteredPaths = Object.fromEntries(
Object.entries(schema.paths).filter(([p]) => patterns.includes(p))
);
return { ...schema, paths: filteredPaths } as any;
}

describe('generateReactQueryHooks', () => {
it('returns empty array when hooks are disabled', () => {
const hooks = generateReactQueryHooks(defaultSchemaSDKOptions as any, schema as any);
expect(hooks).toEqual([]);
});

it('returns hook files when enabled for filtered schema', () => {
const options: OpenAPIOptions = {
...(defaultSchemaSDKOptions as any),
hooks: {
enabled: true,
path: 'hooks',
typesImportPath: './swagger-client',
contextImportPath: './k8s-context',
},
} as any;
// const testSchema = getFilteredSchema(schema as any, patterns);
const testSchema = schema as any;
const hooks = generateReactQueryHooks(options, testSchema);
expect(hooks.length).toBeGreaterThan(0);
// Verify each hook file has expected properties
hooks.forEach(hook => {
expect(hook).toHaveProperty('fileName');
expect(hook).toHaveProperty('code');
});

writeFileSync(
__dirname + '/../../../__fixtures__/output/hooks.ts',
hooks[0].code
);
});
});
81 changes: 81 additions & 0 deletions packages/schema-sdk/src/context-template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
export const generateContext = (Type: string, pathToClient: string) => `
'use client'

import React, { createContext, useContext, useMemo, useState } from 'react'
import { KubernetesClient } from '${pathToClient}'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import generate from '@babel/generator'

// Configuration types
export interface ${Type}Config {
restEndpoint: string
headers?: Record<string, string>
}

// Context types
interface ${Type}ContextValue {
client: ${Type}Client
}

// Create context
const ${Type}Context = createContext<${Type}ContextValue | undefined>(undefined)

// Query client for TanStack Query
const queryClient = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
retry: 3,
staleTime: 30 * 1000, // 30 seconds
gcTime: 5 * 60 * 1000, // 5 minutes
},
},
})

// Provider props
interface ${Type}ProviderProps {
children: React.ReactNode
initialConfig?: Partial<${Type}Config>
}

// Provider component
export function ${Type}Provider({
children,
initialConfig
}: ${Type}ProviderProps) {
const [config, setConfig] = useState<${Type}Config>({
restEndpoint: initialConfig?.restEndpoint,
headers: initialConfig?.headers || {},
})

// Create client instance
const client = useMemo(() => {
return new ${Type}Client({
restEndpoint: config.restEndpoint,
})
}, [config.restEndpoint])

const contextValue: ${Type}ContextValue = {
client
}

return (
<QueryClientProvider client={queryClient}>
<${Type}Context.Provider value={contextValue}>
{children}
</${Type}Context.Provider>
</QueryClientProvider>
)
}

// Hook to use ${Type} context
export function use${Type}() {
const context = useContext(${Type}Context)
if (!context) {
throw new Error('use${Type} must be used within a ${Type}Provider')
}
return context
}

// Export query client for use in hooks
export { queryClient };`;
Loading