UNPKG

mt-flowbite-react

Version:

Official React components built for Flowbite and Tailwind CSS

58 lines (57 loc) 2.11 kB
import { camelCase } from 'lodash-es'; // import type { Account } from 'next-auth'; import { useSessionStore } from '../store/session'; // const baseApiUrl = 'https://dev18383.yuepa8.com'; const baseApiUrl = ''; // const baseApiUrl = 'https://dev18383.yuepa8.com'; const apiBasePath = ''; // const apiBasePath = '/api/v3'; export const mtmApiCall = async (resName, action, searchParams, payload) => { const sessionData = useSessionStore.getState().sessionData; const resName2 = camelCase(resName); let url = `${baseApiUrl}${apiBasePath}/${resName2}/${action}`; if (searchParams) { url = `${url}?${new URLSearchParams(searchParams).toString()}`; } const res = await fetch(url, { method: payload ? 'POST' : 'GET', headers: { Authorization: `Bearer ${sessionData?.user?.accessToken}`, ...(payload && { 'Content-Type': 'application/json', }), }, body: JSON.stringify(payload), }); const contentType = res.headers.get('Content-Type'); if (contentType && contentType.includes('application/json')) { return await res.json(); } else { throw new Error(`响应不是 ${url} , JSON 格式:, ${await res.text()}`); } }; export const mtmApiPost = async (apiPath, payload) => { // const sessionData = useSessionStore.getState().sessionData const url = `${apiPath}`; const res = await fetch(url, { method: 'POST', headers: { // "Authorization": `Bearer ${sessionData?.user?.accessToken}`, ...(payload && { 'Content-Type': 'application/json', }), }, body: JSON.stringify(payload), }); const contentType = res.headers.get('Content-Type'); if (contentType && contentType.includes('application/json')) { return await res.json(); } else { throw new Error(`响应不是 ${url} , JSON 格式:, ${await res.text()}`); } }; export const oauth2Login = (account) => { return mtmApiPost(baseApiUrl + '/auth/oauth2Login', account); };