integration Directus cms
This commit is contained in:
127
src/utils/directus.ts
Normal file
127
src/utils/directus.ts
Normal file
@@ -0,0 +1,127 @@
|
||||
// Removed: import type { DirectusClient } from '@directus/sdk';
|
||||
|
||||
interface DirectusAuthResponse {
|
||||
data: {
|
||||
access_token: string;
|
||||
refresh_token: string;
|
||||
expires: number;
|
||||
};
|
||||
}
|
||||
|
||||
interface BlogPost {
|
||||
id: string;
|
||||
title: string;
|
||||
content: string;
|
||||
slug: string;
|
||||
date_created: string;
|
||||
status: string;
|
||||
// Add other fields as needed
|
||||
}
|
||||
|
||||
let cachedToken: string | null = null;
|
||||
let tokenExpiry: number = 0;
|
||||
|
||||
async function getAccessToken(): Promise<string> {
|
||||
// Return cached token if it's still valid
|
||||
if (cachedToken && Date.now() < tokenExpiry) {
|
||||
return cachedToken;
|
||||
}
|
||||
|
||||
try {
|
||||
console.log('Attempting to authenticate with Directus...');
|
||||
const response = await fetch('https://cms.365devnet.eu/auth/login', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
email: 'admin@365devnet.eu',
|
||||
password: 'DirectusAdmin2024!',
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
console.error('Authentication failed:', {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
error: errorText
|
||||
});
|
||||
throw new Error(`Failed to authenticate with Directus: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
const data: DirectusAuthResponse = await response.json();
|
||||
console.log('Successfully authenticated with Directus');
|
||||
|
||||
// Cache the token
|
||||
cachedToken = data.data.access_token;
|
||||
tokenExpiry = Date.now() + (data.data.expires * 1000); // Convert to milliseconds
|
||||
|
||||
return data.data.access_token;
|
||||
} catch (error) {
|
||||
console.error('Error getting Directus access token:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getBlogPosts(): Promise<BlogPost[]> {
|
||||
try {
|
||||
const token = await getAccessToken();
|
||||
console.log('Fetching blog posts...');
|
||||
|
||||
const response = await fetch('https://cms.365devnet.eu/items/Posts?sort=-date_created', {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
console.error('Failed to fetch posts:', {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
error: errorText
|
||||
});
|
||||
throw new Error(`Failed to fetch blog posts: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
console.log('Successfully fetched blog posts');
|
||||
return data.data;
|
||||
} catch (error) {
|
||||
console.error('Error fetching blog posts:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getBlogPostBySlug(slug: string): Promise<BlogPost | null> {
|
||||
try {
|
||||
const token = await getAccessToken();
|
||||
console.log(`Fetching blog post with slug: ${slug}`);
|
||||
|
||||
const response = await fetch(`https://cms.365devnet.eu/items/Posts?filter[slug][_eq]=${slug}`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
console.error('Failed to fetch post:', {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
error: errorText
|
||||
});
|
||||
throw new Error(`Failed to fetch blog post: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
console.log('Successfully fetched blog post');
|
||||
return data.data[0] || null;
|
||||
} catch (error) {
|
||||
console.error('Error fetching blog post:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user