forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Search] [Onboarding] Search api key refactor (elastic#199790)
Refactor search api key - get rid from useReduces - simplify logic in provider - create request hooks - fix multiple initialization --------- Co-authored-by: Elastic Machine <[email protected]>
- Loading branch information
1 parent
35d3f4f
commit 1705075
Showing
10 changed files
with
186 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/kbn-search-api-keys-components/src/hooks/use_create_api_key.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import { useMutation } from '@tanstack/react-query'; | ||
import type { APIKeyCreationResponse } from '@kbn/search-api-keys-server/types'; | ||
import { useKibana } from '@kbn/kibana-react-plugin/public'; | ||
import { APIRoutes } from '../types'; | ||
|
||
export const useCreateApiKey = ({ | ||
onSuccess, | ||
onError, | ||
}: { | ||
onSuccess(key: APIKeyCreationResponse): void; | ||
onError(err: XMLHttpRequest): void; | ||
}) => { | ||
const { http } = useKibana().services; | ||
const { mutateAsync: createApiKey } = useMutation<APIKeyCreationResponse | undefined>({ | ||
mutationFn: async () => { | ||
try { | ||
if (!http?.post) { | ||
throw new Error('HTTP service is unavailable'); | ||
} | ||
|
||
return await http.post<APIKeyCreationResponse>(APIRoutes.API_KEYS); | ||
} catch (err) { | ||
onError(err); | ||
} | ||
}, | ||
onSuccess: (receivedApiKey) => { | ||
if (receivedApiKey) { | ||
onSuccess(receivedApiKey); | ||
} | ||
}, | ||
}); | ||
|
||
return createApiKey; | ||
}; |
33 changes: 33 additions & 0 deletions
33
packages/kbn-search-api-keys-components/src/hooks/use_validate_api_key.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import { useMutation } from '@tanstack/react-query'; | ||
import { useKibana } from '@kbn/kibana-react-plugin/public'; | ||
import { APIRoutes } from '../types'; | ||
|
||
export const useValidateApiKey = (): ((id: string) => Promise<boolean>) => { | ||
const { http } = useKibana().services; | ||
const { mutateAsync: validateApiKey } = useMutation(async (id: string) => { | ||
try { | ||
if (!http?.post) { | ||
throw new Error('HTTP service is unavailable'); | ||
} | ||
|
||
const response = await http.post<{ isValid: boolean }>(APIRoutes.API_KEY_VALIDITY, { | ||
body: JSON.stringify({ id }), | ||
}); | ||
|
||
return response.isValid; | ||
} catch (err) { | ||
return false; | ||
} | ||
}); | ||
|
||
return validateApiKey; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.