Updated api fetch to return an object.

This commit is contained in:
Mike Cao 2020-09-30 15:14:44 -07:00
parent e64b35f701
commit e5cd162b83
10 changed files with 28 additions and 28 deletions

View File

@ -32,15 +32,13 @@ export default function AccountEditForm({ values, onSave, onClose }) {
const [message, setMessage] = useState();
const handleSubmit = async values => {
const response = await post(`/api/account`, values);
const { ok, data } = await post(`/api/account`, values);
if (typeof response !== 'string') {
if (ok) {
onSave();
} else {
setMessage(
response || (
<FormattedMessage id="message.failure" defaultMessage="Something went wrong." />
),
data || <FormattedMessage id="message.failure" defaultMessage="Something went wrong." />,
);
}
};

View File

@ -40,15 +40,13 @@ export default function ChangePasswordForm({ values, onSave, onClose }) {
const [message, setMessage] = useState();
const handleSubmit = async values => {
const response = await post(`/api/account/password`, values);
const { ok, data } = await post(`/api/account/password`, values);
if (typeof response !== 'string') {
if (ok) {
onSave();
} else {
setMessage(
response || (
<FormattedMessage id="message.failure" defaultMessage="Something went wrong." />
),
data || <FormattedMessage id="message.failure" defaultMessage="Something went wrong." />,
);
}
};

View File

@ -31,19 +31,19 @@ export default function LoginForm() {
const [message, setMessage] = useState();
const handleSubmit = async ({ username, password }) => {
const response = await post('/api/auth/login', { username, password });
const { ok, status, data } = await post('/api/auth/login', { username, password });
if (typeof response !== 'string') {
if (ok) {
await Router.push('/');
} else {
setMessage(
response.startsWith('401') ? (
status === 401 ? (
<FormattedMessage
id="message.incorrect-username-password"
defaultMessage="Incorrect username/password."
/>
) : (
response
data
),
);
}

View File

@ -37,12 +37,14 @@ export default function WebsiteEditForm({ values, onSave, onClose }) {
const [message, setMessage] = useState();
const handleSubmit = async values => {
const response = await post(`/api/website`, values);
const { ok, data } = await post(`/api/website`, values);
if (typeof response !== 'string') {
if (ok) {
onSave();
} else {
setMessage(<FormattedMessage id="message.failure" defaultMessage="Something went wrong." />);
setMessage(
data || <FormattedMessage id="message.failure" defaultMessage="Something went wrong." />,
);
}
};

View File

@ -6,6 +6,7 @@ import { updateQuery } from 'redux/actions/queries';
export default function useFetch(url, params = {}, options = {}) {
const dispatch = useDispatch();
const [data, setData] = useState();
const [status, setStatus] = useState();
const [error, setError] = useState();
const [loading, setLoadiing] = useState(false);
const keys = Object.keys(params)
@ -18,11 +19,12 @@ export default function useFetch(url, params = {}, options = {}) {
setLoadiing(true);
setError(null);
const time = performance.now();
const data = await get(url, params);
const { data, status } = await get(url, params);
dispatch(updateQuery({ url, time: performance.now() - time, completed: Date.now() }));
setData(data);
setStatus(status);
onDataLoad(data);
} catch (e) {
console.error(e);
@ -46,5 +48,5 @@ export default function useFetch(url, params = {}, options = {}) {
}
}, [url, ...keys, ...update]);
return { data, error, loading, loadData };
return { data, status, error, loading };
}

View File

@ -1,6 +1,6 @@
import { getQueryString } from './url';
export const apiRequest = (method, url, body) =>
export const apiRequest = (method, url, body, headers) =>
fetch(url, {
method,
cache: 'no-cache',
@ -8,18 +8,16 @@ export const apiRequest = (method, url, body) =>
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
...headers,
},
body,
}).then(res => {
console.log({ res });
if (res.ok) {
return res.json();
return res.json().then(data => ({ ok: res.ok, status: res.status, data }));
}
if (['post', 'put', 'delete'].includes(method)) {
return res.text();
}
return null;
return res.text().then(data => ({ ok: res.ok, status: res.status, res: res, data }));
});
export const get = (url, params) => apiRequest('get', `${url}${getQueryString(params)}`);

View File

@ -1,6 +1,6 @@
{
"name": "umami",
"version": "0.62.0",
"version": "0.63.0",
"description": "A simple, fast, website analytics alternative to Google Analytics. ",
"author": "Mike Cao <mike@mikecao.com>",
"license": "MIT",

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -9,5 +9,5 @@ export default {
file: 'public/umami.js',
format: 'iife',
},
plugins: [resolve(), buble(), terser({ compress: { evaluate: false } })],
plugins: [resolve(), buble({ objectAssign: true }), terser({ compress: { evaluate: false } })],
};