From 260f39f72d1954be65a5a60e818856fe617805a2 Mon Sep 17 00:00:00 2001 From: Charles Zhao Date: Fri, 5 May 2023 17:32:40 +0800 Subject: [PATCH] refactor(console): remove superstruct dependency from console (#3808) --- packages/console/package.json | 1 - .../containers/LinkEmailModal/index.tsx | 5 ++--- .../containers/VerificationCodeModal/index.tsx | 6 ++---- .../containers/VerifyPasswordModal/index.tsx | 5 ++--- packages/console/src/pages/Profile/utils.ts | 18 ++++++------------ packages/console/src/types/profile.ts | 11 ++++++----- pnpm-lock.yaml | 7 ------- 7 files changed, 18 insertions(+), 35 deletions(-) diff --git a/packages/console/package.json b/packages/console/package.json index 0e29b4216..2d2f508e0 100644 --- a/packages/console/package.json +++ b/packages/console/package.json @@ -111,7 +111,6 @@ "recharts": "^2.1.13", "remark-gfm": "^3.0.1", "stylelint": "^15.0.0", - "superstruct": "^0.16.0", "swr": "^1.3.0", "tslib": "^2.4.1", "typescript": "^5.0.0", diff --git a/packages/console/src/pages/Profile/containers/LinkEmailModal/index.tsx b/packages/console/src/pages/Profile/containers/LinkEmailModal/index.tsx index f9921cae3..6eb5f3773 100644 --- a/packages/console/src/pages/Profile/containers/LinkEmailModal/index.tsx +++ b/packages/console/src/pages/Profile/containers/LinkEmailModal/index.tsx @@ -1,5 +1,4 @@ import { emailRegEx } from '@logto/core-kit'; -import { conditional } from '@silverhand/essentials'; import { useForm } from 'react-hook-form'; import { useTranslation } from 'react-i18next'; import { useLocation, useNavigate } from 'react-router-dom'; @@ -10,7 +9,7 @@ import { adminTenantEndpoint, meApi } from '@/consts'; import { useStaticApi } from '@/hooks/use-api'; import MainFlowLikeModal from '../../components/MainFlowLikeModal'; -import { checkLocationState } from '../../utils'; +import { parseLocationState } from '../../utils'; type EmailForm = { email: string; @@ -44,7 +43,7 @@ function LinkEmailModal() { })(); }; - const currentEmail = conditional(checkLocationState(state) && state.email); + const { email: currentEmail } = parseLocationState(state); return ( { navigate('/profile'); diff --git a/packages/console/src/pages/Profile/utils.ts b/packages/console/src/pages/Profile/utils.ts index 2b4e5edf1..007ca3e67 100644 --- a/packages/console/src/pages/Profile/utils.ts +++ b/packages/console/src/pages/Profile/utils.ts @@ -2,19 +2,13 @@ import type { RequestErrorBody } from '@logto/schemas'; import { HTTPError } from 'ky'; import { toast } from 'react-hot-toast'; -export type LocationState = { - email: string; - action: 'changePassword' | 'changeEmail'; -}; +import { type LocationState, locationStateGuard } from '@/types/profile'; -export const checkLocationState = (state: unknown): state is LocationState => - typeof state === 'object' && - state !== null && - 'email' in state && - 'action' in state && - typeof state.email === 'string' && - typeof state.action === 'string' && - ['changePassword', 'changeEmail'].includes(state.action); +export const parseLocationState = (state: unknown): Partial => { + const parsed = locationStateGuard.safeParse(state); + + return parsed.success ? parsed.data : { email: undefined, action: undefined }; +}; export const popupWindow = (url: string, windowName: string, width: number, height: number) => { const outerHeight = window.top?.outerHeight ?? 0; diff --git a/packages/console/src/types/profile.ts b/packages/console/src/types/profile.ts index bc054f2e3..7c5ae08a5 100644 --- a/packages/console/src/types/profile.ts +++ b/packages/console/src/types/profile.ts @@ -1,8 +1,9 @@ -import * as s from 'superstruct'; +import { emailRegEx } from '@logto/core-kit'; +import { z } from 'zod'; -export const locationStateGuard = s.object({ - email: s.string(), - action: s.union([s.literal('changeEmail'), s.literal('changePassword')]), +export const locationStateGuard = z.object({ + email: z.string().regex(emailRegEx), + action: z.union([z.literal('changeEmail'), z.literal('changePassword')]), }); -export type LocationState = s.Infer; +export type LocationState = z.infer; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 29144b6b4..6f69d3859 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3026,9 +3026,6 @@ importers: stylelint: specifier: ^15.0.0 version: 15.0.0 - superstruct: - specifier: ^0.16.0 - version: 0.16.0 swr: specifier: ^1.3.0 version: 1.3.0(react@18.2.0) @@ -18861,10 +18858,6 @@ packages: - supports-color dev: true - /superstruct@0.16.0: - resolution: {integrity: sha512-IDQtwnnlaan1NhuHqyD/U11lROYvCQ79JyfwlFU9xEVHzqV/Ys/RrwmHPCG0CVH/1g0BuodEjH1msxK2UHxehA==} - dev: true - /superstruct@1.0.3: resolution: {integrity: sha512-8iTn3oSS8nRGn+C2pgXSKPI3jmpm6FExNazNpjvqS6ZUJQCej3PUXEKM8NjHBOs54ExM+LPW/FBRhymrdcCiSg==} engines: {node: '>=14.0.0'}