Skip to content

Commit

Permalink
refactor(console,core,schemas): rename the jwtCustomizer related type…
Browse files Browse the repository at this point in the history
… and guards

rename the jwtCustomizer related type and guards
  • Loading branch information
simeng-li committed Mar 12, 2024
1 parent 9c3cc3e commit a2a7474
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 38 deletions.
12 changes: 6 additions & 6 deletions packages/console/src/pages/JwtClaims/Main.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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({
Expand Down
12 changes: 6 additions & 6 deletions packages/console/src/pages/JwtClaims/use-jwt-customizer.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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] }),
});
Expand All @@ -33,7 +33,7 @@ function useJwtCustomizer() {
mutate: mutateClientCredentialsJwtCustomizer,
isLoading: isClientCredentialsJwtDataLoading,
error: clientCredentialsError,
} = useSWR<JwtCustomizerClientCredentials, ResponseError>(
} = useSWR<ClientCredentialsJwtCustomizer, ResponseError>(
getApiPath(LogtoJwtTokenPath.ClientCredentials),
{
fetcher: clientCredentialsFetcher,
Expand Down
14 changes: 6 additions & 8 deletions packages/console/src/pages/JwtClaims/utils.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
Expand All @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/routes/logto-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
type OidcConfigKeysResponse,
type OidcConfigKey,
LogtoOidcConfigKeyType,
jwtCustomizerAccessTokenGuard,
jwtCustomizerClientCredentialsGuard,
accessTokenJwtCustomizerGuard,
clientCredentialsJwtCustomizerGuard,
LogtoJwtTokenKey,
LogtoJwtTokenPath,
} from '@logto/schemas';
Expand All @@ -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),
};
};

Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand Down
8 changes: 4 additions & 4 deletions packages/integration-tests/src/api/logto-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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}`);
16 changes: 8 additions & 8 deletions packages/schemas/src/types/logto-config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,30 @@ 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 --- */
Expand Down

0 comments on commit a2a7474

Please sign in to comment.