Skip to content

Commit 29f4345

Browse files
committed
context
1 parent 420bcc5 commit 29f4345

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

__fixtures__/output/context.ts renamed to __fixtures__/output/context.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ export interface KubernetesConfig {
1111

1212
// Context types
1313
interface KubernetesContextValue {
14-
client: KubernetesClient
14+
client: KubernetesClient;
15+
config: KubernetesConfig;
16+
updateConfig: (config: Partial<KubernetesConfig>) => void;
1517
}
1618

1719
// Create context
18-
const KubernetesContext = createContext<KubernetesContextValue | undefined>(undefined)
20+
const KubernetesContext = createContext<KubernetesContextValue | undefined>(undefined);
1921

2022
// Query client for TanStack Query
2123
const queryClient = new QueryClient({
@@ -31,8 +33,8 @@ const queryClient = new QueryClient({
3133

3234
// Provider props
3335
interface KubernetesProviderProps {
34-
children: React.ReactNode
35-
initialConfig?: Partial<KubernetesConfig>
36+
children: React.ReactNode;
37+
initialConfig?: Partial<KubernetesConfig>;
3638
}
3739

3840
// Provider component
@@ -52,8 +54,15 @@ export function KubernetesProvider({
5254
})
5355
}, [config.restEndpoint])
5456

57+
// Update config function
58+
const updateConfig = (newConfig: Partial<KubernetesConfig>) => {
59+
setConfig(prev => ({ ...prev, ...newConfig }))
60+
}
61+
5562
const contextValue: KubernetesContextValue = {
56-
client
63+
client,
64+
config,
65+
updateConfig,
5766
}
5867

5968
return (
@@ -75,4 +84,4 @@ export function useKubernetes() {
7584
}
7685

7786
// Export query client for use in hooks
78-
export { queryClient };
87+
export { queryClient }

packages/schema-sdk/__tests__/hooks.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('generateReactQueryHooks', () => {
4747
it('generate context', () => {
4848
const context = generateContext('Kubernetes', './swagger-client');
4949
writeFileSync(
50-
__dirname + '/../../../__fixtures__/output/context.ts',
50+
__dirname + '/../../../__fixtures__/output/context.tsx',
5151
context
5252
);
5353
});

packages/schema-sdk/src/context-template.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export const generateContext = (Type: string, pathToClient: string) => `
22
import React, { createContext, useContext, useMemo, useState } from 'react'
3-
import { KubernetesClient } from '${pathToClient}'
3+
import { ${Type}Client } from '${pathToClient}'
44
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
55
66
// Configuration types
@@ -11,11 +11,13 @@ export interface ${Type}Config {
1111
1212
// Context types
1313
interface ${Type}ContextValue {
14-
client: ${Type}Client
14+
client: ${Type}Client;
15+
config: ${Type}Config;
16+
updateConfig: (config: Partial<${Type}Config>) => void;
1517
}
1618
1719
// Create context
18-
const ${Type}Context = createContext<${Type}ContextValue | undefined>(undefined)
20+
const ${Type}Context = createContext<${Type}ContextValue | undefined>(undefined);
1921
2022
// Query client for TanStack Query
2123
const queryClient = new QueryClient({
@@ -31,8 +33,8 @@ const queryClient = new QueryClient({
3133
3234
// Provider props
3335
interface ${Type}ProviderProps {
34-
children: React.ReactNode
35-
initialConfig?: Partial<${Type}Config>
36+
children: React.ReactNode;
37+
initialConfig?: Partial<${Type}Config>;
3638
}
3739
3840
// Provider component
@@ -52,8 +54,15 @@ export function ${Type}Provider({
5254
})
5355
}, [config.restEndpoint])
5456
57+
// Update config function
58+
const updateConfig = (newConfig: Partial<${Type}Config>) => {
59+
setConfig(prev => ({ ...prev, ...newConfig }))
60+
}
61+
5562
const contextValue: ${Type}ContextValue = {
56-
client
63+
client,
64+
config,
65+
updateConfig,
5766
}
5867
5968
return (
@@ -75,4 +84,5 @@ export function use${Type}() {
7584
}
7685
7786
// Export query client for use in hooks
78-
export { queryClient };`;
87+
export { queryClient }
88+
`;

0 commit comments

Comments
 (0)