mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
refactor(console,core,schemas): rename the jwtCustomizer related type and guards (#5496)
rename the jwtCustomizer related type and guards
This commit is contained in:
parent
f11e95e1aa
commit
2a358bf241
6 changed files with 36 additions and 38 deletions
|
@ -1,7 +1,7 @@
|
|||
import {
|
||||
type JwtCustomizerAccessToken,
|
||||
type AccessTokenJwtCustomizer,
|
||||
LogtoJwtTokenPath,
|
||||
type JwtCustomizerClientCredentials,
|
||||
type ClientCredentialsJwtCustomizer,
|
||||
} from '@logto/schemas';
|
||||
import classNames from 'classnames';
|
||||
import { useMemo } from 'react';
|
||||
|
@ -21,10 +21,10 @@ import { formatResponseDataToFormData, formatFormDataToRequestData, getApiPath }
|
|||
|
||||
type Props = {
|
||||
tab: LogtoJwtTokenPath;
|
||||
accessTokenJwtCustomizer: JwtCustomizerAccessToken | undefined;
|
||||
clientCredentialsJwtCustomizer: JwtCustomizerClientCredentials | undefined;
|
||||
mutateAccessTokenJwtCustomizer: KeyedMutator<JwtCustomizerAccessToken>;
|
||||
mutateClientCredentialsJwtCustomizer: KeyedMutator<JwtCustomizerClientCredentials>;
|
||||
accessTokenJwtCustomizer: AccessTokenJwtCustomizer | undefined;
|
||||
clientCredentialsJwtCustomizer: ClientCredentialsJwtCustomizer | undefined;
|
||||
mutateAccessTokenJwtCustomizer: KeyedMutator<AccessTokenJwtCustomizer>;
|
||||
mutateClientCredentialsJwtCustomizer: KeyedMutator<ClientCredentialsJwtCustomizer>;
|
||||
};
|
||||
|
||||
function Main({
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import {
|
||||
LogtoJwtTokenPath,
|
||||
type JwtCustomizerAccessToken,
|
||||
type JwtCustomizerClientCredentials,
|
||||
type AccessTokenJwtCustomizer,
|
||||
type ClientCredentialsJwtCustomizer,
|
||||
} from '@logto/schemas';
|
||||
import { type ResponseError } from '@withtyped/client';
|
||||
import { useMemo } from 'react';
|
||||
|
@ -15,15 +15,15 @@ import { getApiPath } from './utils';
|
|||
|
||||
function useJwtCustomizer() {
|
||||
const fetchApi = useApi({ hideErrorToast: true });
|
||||
const accessTokenFetcher = useSwrFetcher<JwtCustomizerAccessToken>(fetchApi);
|
||||
const clientCredentialsFetcher = useSwrFetcher<JwtCustomizerClientCredentials>(fetchApi);
|
||||
const accessTokenFetcher = useSwrFetcher<AccessTokenJwtCustomizer>(fetchApi);
|
||||
const clientCredentialsFetcher = useSwrFetcher<ClientCredentialsJwtCustomizer>(fetchApi);
|
||||
|
||||
const {
|
||||
data: accessTokenJwtCustomizer,
|
||||
mutate: mutateAccessTokenJwtCustomizer,
|
||||
isLoading: isAccessTokenJwtDataLoading,
|
||||
error: accessTokenError,
|
||||
} = useSWR<JwtCustomizerAccessToken, ResponseError>(getApiPath(LogtoJwtTokenPath.AccessToken), {
|
||||
} = useSWR<AccessTokenJwtCustomizer, ResponseError>(getApiPath(LogtoJwtTokenPath.AccessToken), {
|
||||
fetcher: accessTokenFetcher,
|
||||
shouldRetryOnError: shouldRetryOnError({ ignore: [404] }),
|
||||
});
|
||||
|
@ -33,7 +33,7 @@ function useJwtCustomizer() {
|
|||
mutate: mutateClientCredentialsJwtCustomizer,
|
||||
isLoading: isClientCredentialsJwtDataLoading,
|
||||
error: clientCredentialsError,
|
||||
} = useSWR<JwtCustomizerClientCredentials, ResponseError>(
|
||||
} = useSWR<ClientCredentialsJwtCustomizer, ResponseError>(
|
||||
getApiPath(LogtoJwtTokenPath.ClientCredentials),
|
||||
{
|
||||
fetcher: clientCredentialsFetcher,
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
import {
|
||||
type LogtoJwtTokenPath,
|
||||
type JwtCustomizerAccessToken,
|
||||
type JwtCustomizerClientCredentials,
|
||||
type AccessTokenJwtCustomizer,
|
||||
type ClientCredentialsJwtCustomizer,
|
||||
} from '@logto/schemas';
|
||||
|
||||
import type { JwtClaimsFormType } from './type';
|
||||
|
||||
const formatEnvVariablesResponseToFormData = (
|
||||
enVariables?: JwtCustomizerAccessToken['envVars']
|
||||
enVariables?: AccessTokenJwtCustomizer['envVars']
|
||||
) => {
|
||||
if (!enVariables) {
|
||||
return;
|
||||
|
@ -16,9 +16,7 @@ const formatEnvVariablesResponseToFormData = (
|
|||
return Object.entries(enVariables).map(([key, value]) => ({ key, value }));
|
||||
};
|
||||
|
||||
const formatSampleCodeJsonToString = (
|
||||
sampleJson?: JwtCustomizerAccessToken['contextSample'] | JwtCustomizerAccessToken['tokenSample']
|
||||
) => {
|
||||
const formatSampleCodeJsonToString = (sampleJson?: AccessTokenJwtCustomizer['contextSample']) => {
|
||||
if (!sampleJson) {
|
||||
return;
|
||||
}
|
||||
|
@ -29,8 +27,8 @@ const formatSampleCodeJsonToString = (
|
|||
export const formatResponseDataToFormData = <T extends LogtoJwtTokenPath>(
|
||||
tokenType: T,
|
||||
data?: T extends LogtoJwtTokenPath.AccessToken
|
||||
? JwtCustomizerAccessToken
|
||||
: JwtCustomizerClientCredentials
|
||||
? AccessTokenJwtCustomizer
|
||||
: ClientCredentialsJwtCustomizer
|
||||
): JwtClaimsFormType => {
|
||||
return {
|
||||
script: data?.script,
|
||||
|
|
|
@ -12,8 +12,8 @@ import {
|
|||
type OidcConfigKeysResponse,
|
||||
type OidcConfigKey,
|
||||
LogtoOidcConfigKeyType,
|
||||
jwtCustomizerAccessTokenGuard,
|
||||
jwtCustomizerClientCredentialsGuard,
|
||||
accessTokenJwtCustomizerGuard,
|
||||
clientCredentialsJwtCustomizerGuard,
|
||||
LogtoJwtTokenKey,
|
||||
LogtoJwtTokenPath,
|
||||
} from '@logto/schemas';
|
||||
|
@ -37,12 +37,12 @@ const getJwtTokenKeyAndBody = (tokenPath: LogtoJwtTokenPath, body: unknown) => {
|
|||
if (tokenPath === LogtoJwtTokenPath.AccessToken) {
|
||||
return {
|
||||
key: LogtoJwtTokenKey.AccessToken,
|
||||
body: parse('body', jwtCustomizerAccessTokenGuard, body),
|
||||
body: parse('body', accessTokenJwtCustomizerGuard, body),
|
||||
};
|
||||
}
|
||||
return {
|
||||
key: LogtoJwtTokenKey.ClientCredentials,
|
||||
body: parse('body', jwtCustomizerClientCredentialsGuard, body),
|
||||
body: parse('body', clientCredentialsJwtCustomizerGuard, body),
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -219,7 +219,7 @@ export default function logtoConfigRoutes<T extends AuthedRouter>(
|
|||
* Should specify `body` in koaGuard, otherwise the request body is not accessible even via `ctx.request.body`.
|
||||
*/
|
||||
body: z.unknown(),
|
||||
response: jwtCustomizerAccessTokenGuard.or(jwtCustomizerClientCredentialsGuard),
|
||||
response: accessTokenJwtCustomizerGuard.or(clientCredentialsJwtCustomizerGuard),
|
||||
status: [200, 201, 400],
|
||||
}),
|
||||
async (ctx, next) => {
|
||||
|
@ -248,7 +248,7 @@ export default function logtoConfigRoutes<T extends AuthedRouter>(
|
|||
params: z.object({
|
||||
tokenTypePath: z.nativeEnum(LogtoJwtTokenPath),
|
||||
}),
|
||||
response: jwtCustomizerAccessTokenGuard.or(jwtCustomizerClientCredentialsGuard),
|
||||
response: accessTokenJwtCustomizerGuard.or(clientCredentialsJwtCustomizerGuard),
|
||||
status: [200, 404],
|
||||
}),
|
||||
async (ctx, next) => {
|
||||
|
|
|
@ -3,8 +3,8 @@ import {
|
|||
type AdminConsoleData,
|
||||
type OidcConfigKeysResponse,
|
||||
type LogtoOidcConfigKeyType,
|
||||
type JwtCustomizerAccessToken,
|
||||
type JwtCustomizerClientCredentials,
|
||||
type AccessTokenJwtCustomizer,
|
||||
type ClientCredentialsJwtCustomizer,
|
||||
} from '@logto/schemas';
|
||||
|
||||
import { authedAdminApi } from './api.js';
|
||||
|
@ -39,12 +39,12 @@ export const upsertJwtCustomizer = async (
|
|||
) =>
|
||||
authedAdminApi
|
||||
.put(`configs/jwt-customizer/${keyTypePath}`, { json: value })
|
||||
.json<JwtCustomizerAccessToken | JwtCustomizerClientCredentials>();
|
||||
.json<AccessTokenJwtCustomizer | ClientCredentialsJwtCustomizer>();
|
||||
|
||||
export const getJwtCustomizer = async (keyTypePath: 'access-token' | 'client-credentials') =>
|
||||
authedAdminApi
|
||||
.get(`configs/jwt-customizer/${keyTypePath}`)
|
||||
.json<JwtCustomizerAccessToken | JwtCustomizerClientCredentials>();
|
||||
.json<AccessTokenJwtCustomizer | ClientCredentialsJwtCustomizer>();
|
||||
|
||||
export const deleteJwtCustomizer = async (keyTypePath: 'access-token' | 'client-credentials') =>
|
||||
authedAdminApi.delete(`configs/jwt-customizer/${keyTypePath}`);
|
||||
|
|
|
@ -64,30 +64,30 @@ export const jwtCustomizerGuard = z
|
|||
})
|
||||
.partial();
|
||||
|
||||
export const jwtCustomizerAccessTokenGuard = jwtCustomizerGuard.extend({
|
||||
export const accessTokenJwtCustomizerGuard = jwtCustomizerGuard.extend({
|
||||
// Use partial token guard since users customization may not rely on all fields.
|
||||
tokenSample: accessTokenPayloadGuard.partial().optional(),
|
||||
});
|
||||
|
||||
export type JwtCustomizerAccessToken = z.infer<typeof jwtCustomizerAccessTokenGuard>;
|
||||
export type AccessTokenJwtCustomizer = z.infer<typeof accessTokenJwtCustomizerGuard>;
|
||||
|
||||
export const jwtCustomizerClientCredentialsGuard = jwtCustomizerGuard.extend({
|
||||
export const clientCredentialsJwtCustomizerGuard = jwtCustomizerGuard.extend({
|
||||
// Use partial token guard since users customization may not rely on all fields.
|
||||
tokenSample: clientCredentialsPayloadGuard.partial().optional(),
|
||||
});
|
||||
|
||||
export type JwtCustomizerClientCredentials = z.infer<typeof jwtCustomizerClientCredentialsGuard>;
|
||||
export type ClientCredentialsJwtCustomizer = z.infer<typeof clientCredentialsJwtCustomizerGuard>;
|
||||
|
||||
export type JwtCustomizerType = {
|
||||
[LogtoJwtTokenKey.AccessToken]: JwtCustomizerAccessToken;
|
||||
[LogtoJwtTokenKey.ClientCredentials]: JwtCustomizerClientCredentials;
|
||||
[LogtoJwtTokenKey.AccessToken]: AccessTokenJwtCustomizer;
|
||||
[LogtoJwtTokenKey.ClientCredentials]: ClientCredentialsJwtCustomizer;
|
||||
};
|
||||
|
||||
export const jwtCustomizerConfigGuard: Readonly<{
|
||||
[key in LogtoJwtTokenKey]: ZodType<JwtCustomizerType[key]>;
|
||||
}> = Object.freeze({
|
||||
[LogtoJwtTokenKey.AccessToken]: jwtCustomizerAccessTokenGuard,
|
||||
[LogtoJwtTokenKey.ClientCredentials]: jwtCustomizerClientCredentialsGuard,
|
||||
[LogtoJwtTokenKey.AccessToken]: accessTokenJwtCustomizerGuard,
|
||||
[LogtoJwtTokenKey.ClientCredentials]: clientCredentialsJwtCustomizerGuard,
|
||||
});
|
||||
|
||||
/* --- Logto tenant configs --- */
|
||||
|
|
Loading…
Reference in a new issue