0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-06 20:40:08 -05:00
logto/packages/experience-legacy/src/hooks/use-api.ts

38 lines
854 B
TypeScript
Raw Permalink Normal View History

refactor(experience): experience api migration (#6407) * refactor(experience): migrate the password register and sign-in migrate the password register and sign-in flow * fix(experience): update some namings update some namings * refactor(experience): refactor the verification code flow (migration-2) (#6408) * refactor(experience): refactor the verificaiton code flow refactor the verification code flow * refactor(experience): migrate the social and sso flow (migration-3) (#6406) * refactor(experience): migrate the social and sso flow migrate the social and sso flow * refactor(experience): migrate profile fulfillment flow (migration-4) (#6414) * refactor(experience): migrate profile fulfillment flow migrate the profile fulfillment flow * refactor(experience): remove unused hook remove unused hook * fix(experience): fix password policy checker fix password policy checker error display * fix(experience): fix the api name fix the api name * refactor(experience): migrate mfa flow (migration-5) (#6417) * refactor(experience): migrate mfa binding flow migrate mfa binding flow * test(experience): update unit tests (migration-6) (#6420) * test(experience): update unit tests update unit tests * chore(experience): remove legacy APIs remove legacy APIs * refactor(experience): revert api prefix revert api prefix * fix(experience): update the sso connectors endpoint update the sso connectors endpoint * chore: add changeset add changeset * fix(experience): comments fix comments fix * refactor(experience): refactor the code verificatin api refactor the code verification api * refactor(experience): code refactor refactor some implementation logic * feat(experience, core): add experience legacy package (#6527) add experience legacy package
2024-09-08 21:08:52 -05:00
import type { Nullable } from '@silverhand/essentials';
import { useCallback, useContext } from 'react';
import PageContext from '@/Providers/PageContextProvider/PageContext';
type Options = {
silent?: boolean;
};
const useApi = <Args extends unknown[], Response>(
api: (...args: Args) => Promise<Response>,
options?: Options
) => {
const { setLoading } = useContext(PageContext);
const request = useCallback(
async (...args: Args): Promise<[Nullable<unknown>, Response?]> => {
if (!options?.silent) {
setLoading(true);
}
try {
const result = await api(...args);
return [null, result];
} catch (error: unknown) {
return [error];
} finally {
setLoading(false);
}
},
[api, options?.silent, setLoading]
);
return request;
};
export default useApi;