chore: moving the secrets handling to the react package

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2022-12-23 12:25:39 +01:00
parent 99819da873
commit 562175cf78
12 changed files with 51 additions and 15 deletions
+25
View File
@@ -202,6 +202,28 @@ export const scaffolderListTaskRouteRef: SubRouteRef<undefined>;
// @public (undocumented)
export const scaffolderTaskRouteRef: SubRouteRef<PathParams<'/tasks/:taskId'>>;
// @public
export interface ScaffolderUseTemplateSecrets {
// (undocumented)
setSecrets: (input: Record<string, string>) => void;
}
// @public
export const SecretsContext: React_2.Context<
SecretsContextContents | undefined
>;
// @public
export type SecretsContextContents = {
secrets: Record<string, string>;
setSecrets: React_2.Dispatch<React_2.SetStateAction<Record<string, string>>>;
};
// @public
export const SecretsContextProvider: ({
children,
}: PropsWithChildren<{}>) => JSX.Element;
// @public (undocumented)
export const selectedTemplateRouteRef: SubRouteRef<
PathParams<'/templates/:namespace/:templateName'>
@@ -266,6 +288,9 @@ export const useTemplateSchema: (manifest: TemplateParameterSchema) => {
steps: ParsedTemplateSchema[];
};
// @public
export const useTemplateSecrets: () => ScaffolderUseTemplateSecrets;
// @public (undocumented)
export const viewTechDocRouteRef: ExternalRouteRef<
{
+3 -1
View File
@@ -16,5 +16,7 @@
export * from './routes';
export * from './extensions';
export * from './next';
export * from './types';
export * from './secrets';
export * from './next';
@@ -0,0 +1,43 @@
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React, { useContext } from 'react';
import {
useTemplateSecrets,
SecretsContextProvider,
SecretsContext,
} from './SecretsContext';
import { renderHook, act } from '@testing-library/react-hooks';
describe('SecretsContext', () => {
it('should allow the setting of secrets in the context', async () => {
const { result } = renderHook(
() => ({
hook: useTemplateSecrets(),
context: useContext(SecretsContext),
}),
{
wrapper: ({ children }) => (
<SecretsContextProvider>{children}</SecretsContextProvider>
),
},
);
expect(result.current.context?.secrets.foo).toEqual(undefined);
act(() => result.current.hook.setSecrets({ foo: 'bar' }));
expect(result.current.context?.secrets.foo).toEqual('bar');
});
});
@@ -0,0 +1,86 @@
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React, {
useState,
useCallback,
useContext,
createContext,
PropsWithChildren,
} from 'react';
/**
* The contents of the {@link SecretsContext}.
* @public
*/
export type SecretsContextContents = {
secrets: Record<string, string>;
setSecrets: React.Dispatch<React.SetStateAction<Record<string, string>>>;
};
/**
* The context to hold the Secrets.
* @public
*/
export const SecretsContext = createContext<SecretsContextContents | undefined>(
undefined,
);
/**
* The Context Provider that holds the state for the secrets.
* @public
*/
export const SecretsContextProvider = ({ children }: PropsWithChildren<{}>) => {
const [secrets, setSecrets] = useState<Record<string, string>>({});
return (
<SecretsContext.Provider value={{ secrets, setSecrets }}>
{children}
</SecretsContext.Provider>
);
};
/**
* The return type from the useTemplateSecrets hook.
* @public
*/
export interface ScaffolderUseTemplateSecrets {
setSecrets: (input: Record<string, string>) => void;
}
/**
* Hook to access the secrets context to be able to set secrets that are
* passed to the Scaffolder backend.
* @public
*/
export const useTemplateSecrets = (): ScaffolderUseTemplateSecrets => {
const value = useContext(SecretsContext);
if (!value) {
throw new Error(
'useTemplateSecrets must be used within a SecretsContextProvider',
);
}
const { setSecrets: updateSecrets } = value;
const setSecrets = useCallback(
(input: Record<string, string>) => {
updateSecrets(currentSecrets => ({ ...currentSecrets, ...input }));
},
[updateSecrets],
);
return { setSecrets };
};
@@ -0,0 +1,22 @@
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export {
useTemplateSecrets,
SecretsContext,
SecretsContextProvider,
type ScaffolderUseTemplateSecrets,
type SecretsContextContents,
} from './SecretsContext';