From 4ca90c23c07cac25873ae4782ab3912bd547c08e Mon Sep 17 00:00:00 2001 From: Tristan Yang Date: Fri, 24 Jun 2022 12:30:43 +0800 Subject: [PATCH] refactor: server with TS --- src/app/services/server.ts | 88 ++++++++++++++++++++------------------ src/types/server.ts | 64 +++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 42 deletions(-) create mode 100644 src/types/server.ts diff --git a/src/app/services/server.ts b/src/app/services/server.ts index 612e1572..2be7b7b3 100644 --- a/src/app/services/server.ts +++ b/src/app/services/server.ts @@ -2,18 +2,31 @@ import { createApi } from "@reduxjs/toolkit/query/react"; import BASE_URL from "../config"; import { updateInfo } from "../slices/server"; import baseQuery from "./base.query"; - +import { RootState } from "../store"; +import { User } from "../../types/auth"; +import { + FirebaseConfig, + GoogleAuthConfig, + LoginConfig, + Server, + StoredServer, + TestEmailDTO, + NewAdminDTO, + SMTPConfig, + AgoraConfig, + GithubAuthConfig +} from "../../types/server"; const defaultExpireDuration = 7 * 24 * 60 * 60; export const serverApi = createApi({ reducerPath: "serverApi", baseQuery, endpoints: (builder) => ({ - getServer: builder.query({ + getServer: builder.query({ query: () => ({ url: `admin/system/organization` }), - transformResponse: (data) => { - data.logo = `${BASE_URL}/resource/organization/logo?t=${+new Date()}`; - return data; + transformResponse: (data: Server) => { + const logo = `${BASE_URL}/resource/organization/logo?t=${+new Date()}`; + return { ...data, logo }; }, async onQueryStarted(data, { dispatch, queryFulfilled }) { try { @@ -24,101 +37,94 @@ export const serverApi = createApi({ } } }), - getThirdPartySecret: builder.query({ + getThirdPartySecret: builder.query({ query: () => ({ - // headers: { - // "content-type": "text/plain", - // accept: "text/plain", - // }, url: `/admin/system/third_party_secret`, - responseHandler: (response) => response.text() + responseHandler: (response: Response) => response.text() }), keepUnusedDataFor: 0 }), - updateThirdPartySecret: builder.mutation({ + updateThirdPartySecret: builder.mutation({ query: () => ({ url: `/admin/system/third_party_secret`, method: "POST", - responseHandler: (response) => response.text() + responseHandler: (response: Response) => response.text() }) }), - getMetrics: builder.query({ - query: () => ({ url: `/admin/system/metrics` }) - }), - getServerVersion: builder.query({ + getServerVersion: builder.query({ query: () => ({ headers: { // "content-type": "text/plain", accept: "text/plain" }, url: `/admin/system/version`, - responseHandler: (response) => response.text() + responseHandler: (response: Response) => response.text() }) }), - getFirebaseConfig: builder.query({ + getFirebaseConfig: builder.query({ query: () => ({ url: `admin/fcm/config` }) }), - getGoogleAuthConfig: builder.query({ + getGoogleAuthConfig: builder.query({ query: () => ({ url: `admin/google_auth/config` }) }), - updateGoogleAuthConfig: builder.mutation({ + updateGoogleAuthConfig: builder.mutation({ query: (data) => ({ url: `admin/google_auth/config`, method: "POST", body: data }) }), - getGithubAuthConfig: builder.query({ + getGithubAuthConfig: builder.query({ query: () => ({ url: `admin/github_auth/config` }) }), - updateGithubAuthConfig: builder.mutation({ + updateGithubAuthConfig: builder.mutation({ query: (data) => ({ url: `admin/github_auth/config`, method: "POST", body: data }) }), - sendTestEmail: builder.mutation({ + sendTestEmail: builder.mutation({ query: (data) => ({ url: `/admin/system/send_mail`, method: "POST", body: data }) }), - updateFirebaseConfig: builder.mutation({ + updateFirebaseConfig: builder.mutation({ query: (data) => ({ url: `admin/fcm/config`, method: "POST", body: data }) }), - getAgoraConfig: builder.query({ + getAgoraConfig: builder.query({ query: () => ({ url: `admin/agora/config` }) }), - updateAgoraConfig: builder.mutation({ + updateAgoraConfig: builder.mutation({ query: (data) => ({ url: `admin/agora/config`, method: "POST", body: data }) }), - getSMTPConfig: builder.query({ + getSMTPConfig: builder.query({ query: () => ({ url: `admin/smtp/config` }) }), - getSMTPStatus: builder.query({ + getSMTPStatus: builder.query({ query: () => ({ url: `/admin/smtp/enabled` }) }), - updateSMTPConfig: builder.mutation({ + updateSMTPConfig: builder.mutation({ query: (data) => ({ url: `admin/smtp/config`, method: "POST", body: data }) }), - getLoginConfig: builder.query({ + getLoginConfig: builder.query({ query: () => ({ url: `admin/login/config` }) }), - updateLoginConfig: builder.mutation({ + updateLoginConfig: builder.mutation({ query: (data) => ({ url: `admin/login/config`, method: "POST", @@ -147,29 +153,30 @@ export const serverApi = createApi({ } } }), - createInviteLink: builder.query({ + createInviteLink: builder.query({ query: (expired_in = defaultExpireDuration) => ({ headers: { "content-type": "text/plain", accept: "text/plain" }, url: `/admin/system/create_invite_link?expired_in=${expired_in}`, - responseHandler: (response) => response.text() + responseHandler: (response: Response) => response.text() }), - transformResponse: (link) => { + transformResponse: (link: string) => { // 替换掉域名 const invite = new URL(link); return `${location.origin}${invite.pathname}${invite.search}${invite.hash}`; } }), - updateServer: builder.mutation({ + updateServer: builder.mutation({ query: (data) => ({ url: `admin/system/organization`, method: "POST", body: data }), async onQueryStarted(data, { dispatch, queryFulfilled, getState }) { - const { name: prevName, description: prevDesc } = getState().server; + const rootStore = getState() as RootState; + const { name: prevName, description: prevDesc } = rootStore.server; dispatch(updateInfo(data)); try { await queryFulfilled; @@ -178,14 +185,14 @@ export const serverApi = createApi({ } } }), - createAdmin: builder.mutation({ + createAdmin: builder.mutation({ query: (data) => ({ url: `/admin/system/create_admin`, method: "POST", body: data }) }), - getInitialized: builder.query({ + getInitialized: builder.query({ query: () => ({ url: `/admin/system/initialized` }) @@ -206,15 +213,12 @@ export const { useLazyGetAgoraConfigQuery, useLazyGetSMTPConfigQuery, useLazyGetLoginConfigQuery, - // useGetFirebaseConfigQuery, - // useGetAgoraConfigQuery, useGetLoginConfigQuery, useUpdateLoginConfigMutation, useGetSMTPConfigQuery, useUpdateSMTPConfigMutation, useUpdateAgoraConfigMutation, useGetServerQuery, - useGetMetricsQuery, useLazyGetServerQuery, useUpdateServerMutation, useUpdateLogoMutation, diff --git a/src/types/server.ts b/src/types/server.ts new file mode 100644 index 00000000..a9bf70b1 --- /dev/null +++ b/src/types/server.ts @@ -0,0 +1,64 @@ +import { User } from "./auth"; +export interface Server { + name: string; + description: string; +} +export interface StoredServer extends Server { + logo: string; +} +export interface GithubAuthConfig { + client_id: string; + client_secret: string; +} +export interface FirebaseConfig { + enabled: boolean; + token_url: string; + project_id: string; + private_key: string; + client_email: string; +} +export interface AgoraConfig { + enabled: boolean; + url: string; + project_id: string; + app_id: string; + app_certificate: string; + rtm_key: string; + rtm_secret: string; +} +export interface SMTPConfig { + enabled: boolean; + host: string; + port: number; + from: string; + username: string; + password: string; +} + +export interface GoogleAuthConfig { + client_id: string; +} +export interface TestEmailDTO { + to: string; + content: string; + subject: string; +} +export type WhoCanSignUp = "EveryOne" | "InvitationOnly"; +export type OIDCSetting = { + enable: boolean; + favicon: string; + domain: string; +}; +export interface LoginConfig { + who_can_sign_up: WhoCanSignUp; + password: boolean; + magic_link: boolean; + google: boolean; + github: boolean; + oidc: OIDCSetting[]; + metamask: boolean; + third_party: boolean; +} +export interface NewAdminDTO extends Pick { + password: string; +}