Duffer Derek
import { mapGridStateToUmbraco } from '../mappers/umbracoMapper';
const host = "https://uibuilderbackend.s.cmshelp.dk";
class UmbracoService {
private token: string = '';
private tokenExpiryTime: Date | null = null;
private commonNodeId: string = '';
async getToken() {
if (this.token && this.tokenExpiryTime && new Date() < this.tokenExpiryTime) {
return this.token;
}
const response = await fetch(`${host}/umbraco/management/api/v1/security/back-office/token`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
'client_id': 'umbraco-back-office-uibuilder',
'client_secret': 'JV54unLNYvtOdDGd0tVsAcbFnGMZpnB8n1MbnAELo6G86iJXZt',
'grant_type': 'client_credentials'
})
});
if (!response.ok) {
throw new Error('Failed to obtain token');
}
const data = await response.json();
this.token = data.access_token;
this.tokenExpiryTime = new Date(new Date().getTime() + data.expires_in * 1000);
console.log('Token:', data);
return this.token;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async publish(data: any, documentId: string) {
debugger
const token = await this.getToken();
console.log('data', data);
const response = await fetch(`${host}/umbraco/management/api/v1/document/${documentId}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'accept': '*/*',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({
values: data.values,
variants: data.variants,
id: data.id,
parent: data.parent,
documentType: data.documentType,
template: data.template
})
});
if (!response.ok) {
throw new Error('Failed to publish data');
}
const publishResponse = await fetch(`https://localhost:44313/umbraco/management/api/v1/document/${documentId}/publish`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'accept': '*/*',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({
publishSchedules: [
{
culture: null
}
]
})
});
if (!publishResponse.ok) {
throw new Error('Failed to publish document');
}
return response.json();
}
async fetchPages() {
const token = await this.getToken();
const response = await fetch(`${host}/umbraco/management/api/v1/tree/document/root?skip=0&take=50`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
}
});
if (!response.ok) {
throw new Error('Failed to fetch pages');
}
const data = await response.json();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const filteredPages = data.items.filter((page: any) => page.documentType.id !== '10c7361a-54c2-4623-b83d-e595bfcb733f');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.commonNodeId = data.items.filter((page: any) => page.documentType.id === '10c7361a-54c2-4623-b83d-e595bfcb733f').map((page: any) => page.id)[0];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return filteredPages.map((page: any) => ({
id: page.id,
name: page.variants[0].name,
url: '', // Assuming URL is not provided in the response
isProtected: page.isProtected,
documentType: page.documentType,
variants: page.variants,
noAccess: page.noAccess,
isTrashed: page.isTrashed,
parent: page.parent,
hasChildren: page.hasChildren
}));
}
async fetchPageById(id: string) {
const token = await this.getToken();
const response = await fetch(`${host}/umbraco/management/api/v1/document/${id}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
}
});
if (!response.ok) {
throw new Error('Failed to fetch page data');
}
return response.json();
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async publishPage (pageId: string, gridState: any) {
if (!gridState || !gridState[pageId]) {
throw new Error('Invalid grid state');
}
// Pass the page-specific grid state instead of the entire gridState object
const umbracoData = mapGridStateToUmbraco(gridState[pageId]);
const token = await this.getToken();
const response = await fetch(`${host}/umbraco/management/api/v1/document/${pageId}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'accept': '*/*',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify(umbracoData)
});
if (!response.ok) {
const error = await response.text();
throw new Error(`Failed to publish page: ${error}`);
}
debugger
const publishResponse = await fetch(`${host}/umbraco/management/api/v1/document/${pageId}/publish`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'accept': '*/*',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({
publishSchedules: [{ culture: null }]
})
});
if (!publishResponse.ok) {
throw new Error('Failed to publish document');
}
return response.json();
}
getCommonNodeId() {
return this.commonNodeId;
}
}
export default new UmbracoService();
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists