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 { 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<StoredServer, void>({
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<string, void>({
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<string, void>({
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<string, void>({
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<FirebaseConfig, void>({
query: () => ({ url: `admin/fcm/config` })
}),
getGoogleAuthConfig: builder.query({
getGoogleAuthConfig: builder.query<GoogleAuthConfig, void>({
query: () => ({ url: `admin/google_auth/config` })
}),
updateGoogleAuthConfig: builder.mutation({
updateGoogleAuthConfig: builder.mutation<void, GoogleAuthConfig>({
query: (data) => ({
url: `admin/google_auth/config`,
method: "POST",
body: data
})
}),
getGithubAuthConfig: builder.query({
getGithubAuthConfig: builder.query<GithubAuthConfig, void>({
query: () => ({ url: `admin/github_auth/config` })
}),
updateGithubAuthConfig: builder.mutation({
updateGithubAuthConfig: builder.mutation<void, GithubAuthConfig>({
query: (data) => ({
url: `admin/github_auth/config`,
method: "POST",
body: data
})
}),
sendTestEmail: builder.mutation({
sendTestEmail: builder.mutation<void, TestEmailDTO>({
query: (data) => ({
url: `/admin/system/send_mail`,
method: "POST",
body: data
})
}),
updateFirebaseConfig: builder.mutation({
updateFirebaseConfig: builder.mutation<void, FirebaseConfig>({
query: (data) => ({
url: `admin/fcm/config`,
method: "POST",
body: data
})
}),
getAgoraConfig: builder.query({
getAgoraConfig: builder.query<AgoraConfig, void>({
query: () => ({ url: `admin/agora/config` })
}),
updateAgoraConfig: builder.mutation({
updateAgoraConfig: builder.mutation<void, AgoraConfig>({
query: (data) => ({
url: `admin/agora/config`,
method: "POST",
body: data
})
}),
getSMTPConfig: builder.query({
getSMTPConfig: builder.query<SMTPConfig, void>({
query: () => ({ url: `admin/smtp/config` })
}),
getSMTPStatus: builder.query({
getSMTPStatus: builder.query<boolean, void>({
query: () => ({ url: `/admin/smtp/enabled` })
}),
updateSMTPConfig: builder.mutation({
updateSMTPConfig: builder.mutation<void, SMTPConfig>({
query: (data) => ({
url: `admin/smtp/config`,
method: "POST",
body: data
})
}),
getLoginConfig: builder.query({
getLoginConfig: builder.query<LoginConfig, void>({
query: () => ({ url: `admin/login/config` })
}),
updateLoginConfig: builder.mutation({
updateLoginConfig: builder.mutation<void, LoginConfig>({
query: (data) => ({
url: `admin/login/config`,
method: "POST",
@@ -147,29 +153,30 @@ export const serverApi = createApi({
}
}
}),
createInviteLink: builder.query({
createInviteLink: builder.query<string, number>({
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<void, Server>({
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<User, NewAdminDTO>({
query: (data) => ({
url: `/admin/system/create_admin`,
method: "POST",
body: data
})
}),
getInitialized: builder.query({
getInitialized: builder.query<boolean, void>({
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,
+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;
}