feat: lots of updates
This commit is contained in:
@@ -8,7 +8,7 @@ export const authApi = createApi({
|
||||
query: (credentials) => ({
|
||||
url: "token/login",
|
||||
method: "POST",
|
||||
body: credentials,
|
||||
body: { credential: credentials, device: "web", device_token: "test" },
|
||||
}),
|
||||
// transformResponse: (resp) => {
|
||||
// console.log("resp", resp);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { fetchBaseQuery } from "@reduxjs/toolkit/query";
|
||||
import toast from "react-hot-toast";
|
||||
import BASE_URL, { tokenHeader } from "../config";
|
||||
const whiteList = ["login"];
|
||||
const baseQuery = fetchBaseQuery({
|
||||
@@ -9,22 +10,48 @@ const baseQuery = fetchBaseQuery({
|
||||
if (token && !whiteList.includes(endpoint)) {
|
||||
headers.set(tokenHeader, token);
|
||||
}
|
||||
// 发送channel msg (临时举措)
|
||||
// if (endpoint == "sendChannelMsg") {
|
||||
// headers.set("Content-Type", "text/plain");
|
||||
// }
|
||||
return headers;
|
||||
},
|
||||
});
|
||||
const baseQueryWithReauth = async (args, api, extraOptions) => {
|
||||
let result = await baseQuery(args, api, extraOptions);
|
||||
if (result.error && result.error.status === 401) {
|
||||
// try to get a new token with refreshToken
|
||||
// const refreshResult = await baseQuery('/refreshToken', api, extraOptions);
|
||||
// if (refreshResult.data) {
|
||||
// // store the new token
|
||||
// api.dispatch(tokenReceived(refreshResult.data));
|
||||
// // retry the initial query
|
||||
// result = await baseQuery(args, api, extraOptions);
|
||||
// } else {
|
||||
// api.dispatch(loggedOut());
|
||||
// }
|
||||
if (result.error) {
|
||||
console.log("api error", result.error);
|
||||
switch (result.error.originalStatus) {
|
||||
case 404:
|
||||
{
|
||||
toast.error("Request Not Found");
|
||||
}
|
||||
break;
|
||||
case 500:
|
||||
{
|
||||
toast.error(result.error.data || "server error");
|
||||
}
|
||||
break;
|
||||
case 401:
|
||||
{
|
||||
toast.error("token expired, please login again");
|
||||
// try to get a new token with refreshToken
|
||||
// const refreshResult = await baseQuery('/refreshToken', api, extraOptions);
|
||||
// if (refreshResult.data) {
|
||||
// // store the new token
|
||||
// api.dispatch(tokenReceived(refreshResult.data));
|
||||
// // retry the initial query
|
||||
// result = await baseQuery(args, api, extraOptions);
|
||||
// } else {
|
||||
// api.dispatch(loggedOut());
|
||||
// }
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { createApi } from "@reduxjs/toolkit/query/react";
|
||||
import baseQuery from "./base.query";
|
||||
import { REHYDRATE } from "redux-persist";
|
||||
import { ContentTypes } from "../config";
|
||||
|
||||
export const channelApi = createApi({
|
||||
reducerPath: "channel",
|
||||
baseQuery,
|
||||
@@ -22,10 +24,13 @@ export const channelApi = createApi({
|
||||
}),
|
||||
}),
|
||||
sendChannelMsg: builder.mutation({
|
||||
query: ({ gid, message }) => ({
|
||||
url: `group/${gid}/send`,
|
||||
query: ({ id, content, type = "text" }) => ({
|
||||
headers: {
|
||||
"content-type": ContentTypes[type],
|
||||
},
|
||||
url: `group/${id}/send`,
|
||||
method: "POST",
|
||||
body: message,
|
||||
body: content,
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { createApi } from "@reduxjs/toolkit/query/react";
|
||||
import baseQuery from "./base.query";
|
||||
import BASE_URL from "../config";
|
||||
import BASE_URL, { ContentTypes } from "../config";
|
||||
import { REHYDRATE } from "redux-persist";
|
||||
|
||||
export const contactApi = createApi({
|
||||
reducerPath: "contact",
|
||||
baseQuery,
|
||||
@@ -22,10 +23,13 @@ export const contactApi = createApi({
|
||||
},
|
||||
}),
|
||||
sendMsg: builder.mutation({
|
||||
query: ({ uid, message }) => ({
|
||||
url: `user/${uid}/send`,
|
||||
query: ({ id, content, type = "text" }) => ({
|
||||
headers: {
|
||||
"content-type": ContentTypes[type],
|
||||
},
|
||||
url: `user/${id}/send`,
|
||||
method: "POST",
|
||||
body: message,
|
||||
body: content,
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user