From 33cd99ce908fda40a8c18181c31e155dffd00bf0 Mon Sep 17 00:00:00 2001 From: 2paperstar Date: Mon, 8 Jan 2024 16:43:57 +0900 Subject: [PATCH] refactor: make Scopes type alias --- src/api/oauth.ts | 5 ++--- src/pages/Authorize/index.tsx | 7 ++----- src/pages/Authorize/useAuthorize.ts | 12 ++++-------- src/utils/schema.ts | 1 + 4 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/api/oauth.ts b/src/api/oauth.ts index a97bf7f..9e8e3db 100644 --- a/src/api/oauth.ts +++ b/src/api/oauth.ts @@ -1,5 +1,4 @@ -import { Scope } from "src/utils/schema"; -import { z } from "zod"; +import { Scopes } from "src/utils/schema"; import api from "."; @@ -13,7 +12,7 @@ export const getClientInformation = (clientId: string) => .then(({ data }) => ({ id: data.id, name: data.name, - recentConsent: data.recent_consent as z.infer[], + recentConsent: data.recent_consent as Scopes, })); export const authorize = ({ diff --git a/src/pages/Authorize/index.tsx b/src/pages/Authorize/index.tsx index 63713a4..c841848 100644 --- a/src/pages/Authorize/index.tsx +++ b/src/pages/Authorize/index.tsx @@ -2,9 +2,8 @@ import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import Button from "src/components/Button"; import Logo from "src/components/Logo"; -import { Scope } from "src/utils/schema"; +import { Scopes } from "src/utils/schema"; import styled from "styled-components"; -import { z } from "zod"; import useAuthorize from "./useAuthorize"; @@ -46,9 +45,7 @@ const Item = styled.li` const Authorize = () => { const { error, consent, scopesNotConsented, clientData } = useAuthorize(); - const [scopesConsented, setScopesConsented] = useState< - z.infer[] - >([]); + const [scopesConsented, setScopesConsented] = useState([]); const { t } = useTranslation(); useEffect(() => { diff --git a/src/pages/Authorize/useAuthorize.ts b/src/pages/Authorize/useAuthorize.ts index 05efa41..5197c5f 100644 --- a/src/pages/Authorize/useAuthorize.ts +++ b/src/pages/Authorize/useAuthorize.ts @@ -8,7 +8,7 @@ import { authorizeSchema, isConsentRequiredScope, isNotConsentRequiredScope, - Scope, + Scopes, } from "src/utils/schema"; import useSWR from "swr"; import { z } from "zod"; @@ -51,14 +51,10 @@ const useAuthorize = () => { const { user, logout } = useAuth(); const navigate = useNavigate(); const [error, setError] = useState(); - const [scopesConsented, setScopesConsented] = useState< - z.infer[] - >([]); - const [scopesNotConsented, setScopesNotConsented] = useState< - z.infer[] - >([]); + const [scopesConsented, setScopesConsented] = useState([]); + const [scopesNotConsented, setScopesNotConsented] = useState([]); - const consent = (scopes: z.infer[]) => { + const consent = (scopes: Scopes) => { setScopesConsented((prev) => [...prev, ...scopes]); setScopesNotConsented([]); }; diff --git a/src/utils/schema.ts b/src/utils/schema.ts index 9d3601b..22233c3 100644 --- a/src/utils/schema.ts +++ b/src/utils/schema.ts @@ -8,6 +8,7 @@ export const Scope = z.enum([ "phone", "student_id", ]); +export type Scopes = z.infer[]; const ScopeRequireConsent = Scope.exclude(["openid", "offline_access"]); export const isConsentRequiredScope = (scope: string) => ScopeRequireConsent.safeParse(scope).success;