ui redesign, markdown fix + metadata and auth header
This commit is contained in:
+20
-13
@@ -1,25 +1,19 @@
|
||||
import type { Post, SiteConfig, Asset } from './types';
|
||||
|
||||
function getToken(): string | null {
|
||||
if (typeof window === 'undefined') return null;
|
||||
return localStorage.getItem('admin_token');
|
||||
}
|
||||
|
||||
async function apiFetch<T>(path: string, options: RequestInit = {}): Promise<T> {
|
||||
const headers: Record<string, string> = {
|
||||
...(options.headers as Record<string, string> || {}),
|
||||
};
|
||||
|
||||
const token = getToken();
|
||||
if (token) {
|
||||
headers['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
if (!(options.body instanceof FormData)) {
|
||||
headers['Content-Type'] = headers['Content-Type'] || 'application/json';
|
||||
}
|
||||
|
||||
const res = await fetch(`/api${path}`, { ...options, headers });
|
||||
const res = await fetch(`/api${path}`, {
|
||||
...options,
|
||||
headers,
|
||||
credentials: 'same-origin',
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const body = await res.json().catch(() => ({}));
|
||||
@@ -37,11 +31,24 @@ export class ApiError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
// Auth
|
||||
export const login = (token: string) =>
|
||||
apiFetch<void>('/auth/login', { method: 'POST', body: JSON.stringify({ token }) });
|
||||
export const logout = () => apiFetch<void>('/auth/logout', { method: 'POST' });
|
||||
export const checkSession = () => apiFetch<void>('/auth/me');
|
||||
|
||||
// Posts
|
||||
export const getPosts = () => apiFetch<Post[]>('/posts');
|
||||
export const getPost = (slug: string) => apiFetch<Post>(`/posts/${encodeURIComponent(slug)}`);
|
||||
export const savePost = (data: { slug: string; old_slug?: string | null; summary?: string | null; content: string }) =>
|
||||
apiFetch<Post>('/posts', { method: 'POST', body: JSON.stringify(data) });
|
||||
export const savePost = (data: {
|
||||
slug: string;
|
||||
old_slug?: string | null;
|
||||
date?: string;
|
||||
summary?: string | null;
|
||||
tags?: string[];
|
||||
draft?: boolean;
|
||||
content: string;
|
||||
}) => apiFetch<Post>('/posts', { method: 'POST', body: JSON.stringify(data) });
|
||||
export const deletePost = (slug: string) =>
|
||||
apiFetch<void>(`/posts/${encodeURIComponent(slug)}`, { method: 'DELETE' });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user