@dijitrak/react-nextjs-seo-plugin
Version:
A modern, user-friendly SEO plugin for React and Next.js with multilingual support and comprehensive optimization tools
150 lines (131 loc) • 3.64 kB
JavaScript
/**
* API istemcisi işlemleri
* Meta veriler, şemalar ve diğer SEO bilgilerini almak ve güncellemek için API çağrıları
*/
// API taban URL'si
let apiBaseUrl = '/api/seo';
/**
* API taban URL'sini ayarlar
* @param {string} url API URL'si
*/
export function setApiBaseUrl(url) {
apiBaseUrl = url;
}
/**
* API isteği yapar
* @param {string} endpoint API son noktası
* @param {object} options İstek seçenekleri
* @returns {Promise} İstek sonucu
*/
async function apiRequest(endpoint, options = {}) {
const url = endpoint.startsWith('http') ? endpoint : `${apiBaseUrl}${endpoint}`;
const defaultOptions = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
};
const requestOptions = { ...defaultOptions, ...options };
// POST isteğinde body'yi JSON'a çevir
if (requestOptions.body && typeof requestOptions.body === 'object') {
requestOptions.body = JSON.stringify(requestOptions.body);
}
try {
const response = await fetch(url, requestOptions);
if (!response.ok) {
throw new Error(`API request failed: ${response.status} ${response.statusText}`);
}
// JSON yanıtını ayrıştır
const data = await response.json();
return data;
} catch (error) {
console.error('API request error:', error);
throw error;
}
}
/**
* Sayfa meta verilerini getirir
* @param {string} path Sayfa yolu
* @returns {Promise} Sayfa meta verileri
*/
export async function fetchPageMeta(path) {
return apiRequest(`/pages${path}/meta`);
}
/**
* Sayfa meta verilerini günceller
* @param {string} path Sayfa yolu
* @param {object} metaData Güncellenecek meta verileri
* @returns {Promise} Güncellenmiş meta verileri
*/
export async function updatePageMeta(path, metaData) {
return apiRequest(`/pages${path}/meta`, {
method: 'PATCH',
body: metaData
});
}
/**
* Sayfa JSON-LD şemalarını getirir
* @param {string} path Sayfa yolu
* @returns {Promise} Sayfa JSON-LD şemaları
*/
export async function fetchJSONLDSchemas(path) {
return apiRequest(`/pages${path}/schemas`);
}
/**
* Sayfa JSON-LD şemalarını günceller
* @param {string} path Sayfa yolu
* @param {Array} schemas Güncellenecek şemalar
* @returns {Promise} Güncellenmiş şemalar
*/
export async function updateJSONLDSchemas(path, schemas) {
return apiRequest(`/pages${path}/schemas`, {
method: 'PATCH',
body: schemas
});
}
/**
* Sitemap.xml oluştur
* @returns {Promise} Sitemap.xml içeriği
*/
export async function generateSitemap() {
return apiRequest('/sitemap/generate');
}
/**
* Robots.txt oluştur
* @param {boolean} allowIndexing İndekslemeye izin ver
* @param {string} customRules Özel kurallar
* @returns {Promise} Robots.txt içeriği
*/
export async function generateRobotsTxt(allowIndexing, customRules) {
return apiRequest('/robots/generate', {
method: 'POST',
body: {
allowIndexing,
customRules
}
});
}
/**
* SEO puanını getirir
* @param {string} path Sayfa yolu
* @returns {Promise} SEO puanı
*/
export async function fetchSEOScore(path) {
return apiRequest(`/score${path ? `?path=${encodeURIComponent(path)}` : ''}`);
}
/**
* SEO sorunlarını getirir
* @param {string} path Sayfa yolu
* @returns {Promise} SEO sorunları
*/
export async function fetchSEOIssues(path) {
return apiRequest(`/issues${path ? `?path=${encodeURIComponent(path)}` : ''}`);
}
/**
* SEO önerilerini getirir
* @returns {Promise} SEO önerileri
*/
export async function fetchSEOTips() {
return apiRequest('/tips');
}