refactor: server with TS

This commit is contained in:
Tristan Yang
2022-06-24 12:30:43 +08:00
parent a8ffc197dc
commit 4ca90c23c0
2 changed files with 110 additions and 42 deletions
+46 -42
View File
@@ -2,18 +2,31 @@ import { createApi } from "@reduxjs/toolkit/query/react";
import BASE_URL from "../config"; import BASE_URL from "../config";
import { updateInfo } from "../slices/server"; import { updateInfo } from "../slices/server";
import baseQuery from "./base.query"; 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; const defaultExpireDuration = 7 * 24 * 60 * 60;
export const serverApi = createApi({ export const serverApi = createApi({
reducerPath: "serverApi", reducerPath: "serverApi",
baseQuery, baseQuery,
endpoints: (builder) => ({ endpoints: (builder) => ({
getServer: builder.query({ getServer: builder.query<StoredServer, void>({
query: () => ({ url: `admin/system/organization` }), query: () => ({ url: `admin/system/organization` }),
transformResponse: (data) => { transformResponse: (data: Server) => {
data.logo = `${BASE_URL}/resource/organization/logo?t=${+new Date()}`; const logo = `${BASE_URL}/resource/organization/logo?t=${+new Date()}`;
return data; return { ...data, logo };
}, },
async onQueryStarted(data, { dispatch, queryFulfilled }) { async onQueryStarted(data, { dispatch, queryFulfilled }) {
try { try {
@@ -24,101 +37,94 @@ export const serverApi = createApi({
} }
} }
}), }),
getThirdPartySecret: builder.query({ getThirdPartySecret: builder.query<string, void>({
query: () => ({ query: () => ({
// headers: {
// "content-type": "text/plain",
// accept: "text/plain",
// },
url: `/admin/system/third_party_secret`, url: `/admin/system/third_party_secret`,
responseHandler: (response) => response.text() responseHandler: (response: Response) => response.text()
}), }),
keepUnusedDataFor: 0 keepUnusedDataFor: 0
}), }),
updateThirdPartySecret: builder.mutation({ updateThirdPartySecret: builder.mutation<string, void>({
query: () => ({ query: () => ({
url: `/admin/system/third_party_secret`, url: `/admin/system/third_party_secret`,
method: "POST", method: "POST",
responseHandler: (response) => response.text() responseHandler: (response: Response) => response.text()
}) })
}), }),
getMetrics: builder.query({ getServerVersion: builder.query<string, void>({
query: () => ({ url: `/admin/system/metrics` })
}),
getServerVersion: builder.query({
query: () => ({ query: () => ({
headers: { headers: {
// "content-type": "text/plain", // "content-type": "text/plain",
accept: "text/plain" accept: "text/plain"
}, },
url: `/admin/system/version`, url: `/admin/system/version`,
responseHandler: (response) => response.text() responseHandler: (response: Response) => response.text()
}) })
}), }),
getFirebaseConfig: builder.query({ getFirebaseConfig: builder.query<FirebaseConfig, void>({
query: () => ({ url: `admin/fcm/config` }) query: () => ({ url: `admin/fcm/config` })
}), }),
getGoogleAuthConfig: builder.query({ getGoogleAuthConfig: builder.query<GoogleAuthConfig, void>({
query: () => ({ url: `admin/google_auth/config` }) query: () => ({ url: `admin/google_auth/config` })
}), }),
updateGoogleAuthConfig: builder.mutation({ updateGoogleAuthConfig: builder.mutation<void, GoogleAuthConfig>({
query: (data) => ({ query: (data) => ({
url: `admin/google_auth/config`, url: `admin/google_auth/config`,
method: "POST", method: "POST",
body: data body: data
}) })
}), }),
getGithubAuthConfig: builder.query({ getGithubAuthConfig: builder.query<GithubAuthConfig, void>({
query: () => ({ url: `admin/github_auth/config` }) query: () => ({ url: `admin/github_auth/config` })
}), }),
updateGithubAuthConfig: builder.mutation({ updateGithubAuthConfig: builder.mutation<void, GithubAuthConfig>({
query: (data) => ({ query: (data) => ({
url: `admin/github_auth/config`, url: `admin/github_auth/config`,
method: "POST", method: "POST",
body: data body: data
}) })
}), }),
sendTestEmail: builder.mutation({ sendTestEmail: builder.mutation<void, TestEmailDTO>({
query: (data) => ({ query: (data) => ({
url: `/admin/system/send_mail`, url: `/admin/system/send_mail`,
method: "POST", method: "POST",
body: data body: data
}) })
}), }),
updateFirebaseConfig: builder.mutation({ updateFirebaseConfig: builder.mutation<void, FirebaseConfig>({
query: (data) => ({ query: (data) => ({
url: `admin/fcm/config`, url: `admin/fcm/config`,
method: "POST", method: "POST",
body: data body: data
}) })
}), }),
getAgoraConfig: builder.query({ getAgoraConfig: builder.query<AgoraConfig, void>({
query: () => ({ url: `admin/agora/config` }) query: () => ({ url: `admin/agora/config` })
}), }),
updateAgoraConfig: builder.mutation({ updateAgoraConfig: builder.mutation<void, AgoraConfig>({
query: (data) => ({ query: (data) => ({
url: `admin/agora/config`, url: `admin/agora/config`,
method: "POST", method: "POST",
body: data body: data
}) })
}), }),
getSMTPConfig: builder.query({ getSMTPConfig: builder.query<SMTPConfig, void>({
query: () => ({ url: `admin/smtp/config` }) query: () => ({ url: `admin/smtp/config` })
}), }),
getSMTPStatus: builder.query({ getSMTPStatus: builder.query<boolean, void>({
query: () => ({ url: `/admin/smtp/enabled` }) query: () => ({ url: `/admin/smtp/enabled` })
}), }),
updateSMTPConfig: builder.mutation({ updateSMTPConfig: builder.mutation<void, SMTPConfig>({
query: (data) => ({ query: (data) => ({
url: `admin/smtp/config`, url: `admin/smtp/config`,
method: "POST", method: "POST",
body: data body: data
}) })
}), }),
getLoginConfig: builder.query({ getLoginConfig: builder.query<LoginConfig, void>({
query: () => ({ url: `admin/login/config` }) query: () => ({ url: `admin/login/config` })
}), }),
updateLoginConfig: builder.mutation({ updateLoginConfig: builder.mutation<void, LoginConfig>({
query: (data) => ({ query: (data) => ({
url: `admin/login/config`, url: `admin/login/config`,
method: "POST", method: "POST",
@@ -147,29 +153,30 @@ export const serverApi = createApi({
} }
} }
}), }),
createInviteLink: builder.query({ createInviteLink: builder.query<string, number>({
query: (expired_in = defaultExpireDuration) => ({ query: (expired_in = defaultExpireDuration) => ({
headers: { headers: {
"content-type": "text/plain", "content-type": "text/plain",
accept: "text/plain" accept: "text/plain"
}, },
url: `/admin/system/create_invite_link?expired_in=${expired_in}`, 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); const invite = new URL(link);
return `${location.origin}${invite.pathname}${invite.search}${invite.hash}`; return `${location.origin}${invite.pathname}${invite.search}${invite.hash}`;
} }
}), }),
updateServer: builder.mutation({ updateServer: builder.mutation<void, Server>({
query: (data) => ({ query: (data) => ({
url: `admin/system/organization`, url: `admin/system/organization`,
method: "POST", method: "POST",
body: data body: data
}), }),
async onQueryStarted(data, { dispatch, queryFulfilled, getState }) { 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)); dispatch(updateInfo(data));
try { try {
await queryFulfilled; await queryFulfilled;
@@ -178,14 +185,14 @@ export const serverApi = createApi({
} }
} }
}), }),
createAdmin: builder.mutation({ createAdmin: builder.mutation<User, NewAdminDTO>({
query: (data) => ({ query: (data) => ({
url: `/admin/system/create_admin`, url: `/admin/system/create_admin`,
method: "POST", method: "POST",
body: data body: data
}) })
}), }),
getInitialized: builder.query({ getInitialized: builder.query<boolean, void>({
query: () => ({ query: () => ({
url: `/admin/system/initialized` url: `/admin/system/initialized`
}) })
@@ -206,15 +213,12 @@ export const {
useLazyGetAgoraConfigQuery, useLazyGetAgoraConfigQuery,
useLazyGetSMTPConfigQuery, useLazyGetSMTPConfigQuery,
useLazyGetLoginConfigQuery, useLazyGetLoginConfigQuery,
// useGetFirebaseConfigQuery,
// useGetAgoraConfigQuery,
useGetLoginConfigQuery, useGetLoginConfigQuery,
useUpdateLoginConfigMutation, useUpdateLoginConfigMutation,
useGetSMTPConfigQuery, useGetSMTPConfigQuery,
useUpdateSMTPConfigMutation, useUpdateSMTPConfigMutation,
useUpdateAgoraConfigMutation, useUpdateAgoraConfigMutation,
useGetServerQuery, useGetServerQuery,
useGetMetricsQuery,
useLazyGetServerQuery, useLazyGetServerQuery,
useUpdateServerMutation, useUpdateServerMutation,
useUpdateLogoMutation, useUpdateLogoMutation,
+64
View File
@@ -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<User, "email" | "name" | "gender"> {
password: string;
}