feat(admin): centralize tool & module settings in Werkzeuge tab with per-tool permissions, DB-backed config, connection tests, and cog-button navigation
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import axios from 'axios';
|
||||
import httpClient from '../config/httpClient';
|
||||
import environment from '../config/environment';
|
||||
import toolConfigService, { BookstackConfig } from './toolConfig.service';
|
||||
import logger from '../utils/logger';
|
||||
|
||||
export interface BookStackPage {
|
||||
@@ -74,10 +74,9 @@ function isValidServiceUrl(raw: string): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
function buildHeaders(): Record<string, string> {
|
||||
const { bookstack } = environment;
|
||||
function buildHeaders(config: BookstackConfig): Record<string, string> {
|
||||
return {
|
||||
'Authorization': `Token ${bookstack.tokenId}:${bookstack.tokenSecret}`,
|
||||
'Authorization': `Token ${config.tokenId}:${config.tokenSecret}`,
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
}
|
||||
@@ -95,11 +94,11 @@ async function getBookSlugMap(): Promise<Map<number, string>> {
|
||||
if (bookSlugMapCache && Date.now() < bookSlugMapCache.expiresAt) {
|
||||
return bookSlugMapCache.map;
|
||||
}
|
||||
const { bookstack } = environment;
|
||||
const config = await toolConfigService.getBookstackConfig();
|
||||
try {
|
||||
const response = await httpClient.get(
|
||||
`${bookstack.url}/api/books`,
|
||||
{ params: { count: 500 }, headers: buildHeaders() },
|
||||
`${config.url}/api/books`,
|
||||
{ params: { count: 500 }, headers: buildHeaders(config) },
|
||||
);
|
||||
const books: Array<{ id: number; slug: string }> = response.data?.data ?? [];
|
||||
const map = new Map(books.map((b) => [b.id, b.slug]));
|
||||
@@ -111,18 +110,18 @@ async function getBookSlugMap(): Promise<Map<number, string>> {
|
||||
}
|
||||
|
||||
async function getRecentPages(): Promise<BookStackPage[]> {
|
||||
const { bookstack } = environment;
|
||||
if (!bookstack.url || !isValidServiceUrl(bookstack.url)) {
|
||||
const config = await toolConfigService.getBookstackConfig();
|
||||
if (!config.url || !isValidServiceUrl(config.url)) {
|
||||
throw new Error('BOOKSTACK_URL is not configured or is not a valid service URL');
|
||||
}
|
||||
|
||||
try {
|
||||
const [response, bookSlugMap] = await Promise.all([
|
||||
httpClient.get(
|
||||
`${bookstack.url}/api/pages`,
|
||||
`${config.url}/api/pages`,
|
||||
{
|
||||
params: { sort: '-updated_at', count: 20 },
|
||||
headers: buildHeaders(),
|
||||
headers: buildHeaders(config),
|
||||
},
|
||||
),
|
||||
getBookSlugMap(),
|
||||
@@ -130,7 +129,7 @@ async function getRecentPages(): Promise<BookStackPage[]> {
|
||||
const pages: BookStackPage[] = response.data?.data ?? [];
|
||||
return pages.map((p) => ({
|
||||
...p,
|
||||
url: `${bookstack.url}/books/${bookSlugMap.get(p.book_id) || p.book_slug || p.book_id}/page/${p.slug}`,
|
||||
url: `${config.url}/books/${bookSlugMap.get(p.book_id) || p.book_slug || p.book_id}/page/${p.slug}`,
|
||||
}));
|
||||
} catch (error) {
|
||||
if (axios.isAxiosError(error)) {
|
||||
@@ -145,17 +144,17 @@ async function getRecentPages(): Promise<BookStackPage[]> {
|
||||
}
|
||||
|
||||
async function searchPages(query: string): Promise<BookStackSearchResult[]> {
|
||||
const { bookstack } = environment;
|
||||
if (!bookstack.url || !isValidServiceUrl(bookstack.url)) {
|
||||
const config = await toolConfigService.getBookstackConfig();
|
||||
if (!config.url || !isValidServiceUrl(config.url)) {
|
||||
throw new Error('BOOKSTACK_URL is not configured or is not a valid service URL');
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await httpClient.get(
|
||||
`${bookstack.url}/api/search`,
|
||||
`${config.url}/api/search`,
|
||||
{
|
||||
params: { query, count: 50 },
|
||||
headers: buildHeaders(),
|
||||
headers: buildHeaders(config),
|
||||
},
|
||||
);
|
||||
const bookSlugMap = await getBookSlugMap();
|
||||
@@ -167,7 +166,7 @@ async function searchPages(query: string): Promise<BookStackSearchResult[]> {
|
||||
slug: item.slug,
|
||||
book_id: item.book_id ?? 0,
|
||||
book_slug: item.book_slug ?? '',
|
||||
url: `${bookstack.url}/books/${bookSlugMap.get(item.book_id) || item.book_slug || item.book_id}/page/${item.slug}`,
|
||||
url: `${config.url}/books/${bookSlugMap.get(item.book_id) || item.book_slug || item.book_id}/page/${item.slug}`,
|
||||
preview_html: item.preview_html ?? { content: '' },
|
||||
tags: item.tags ?? [],
|
||||
}));
|
||||
@@ -201,16 +200,16 @@ export interface BookStackPageDetail {
|
||||
}
|
||||
|
||||
async function getPageById(id: number): Promise<BookStackPageDetail> {
|
||||
const { bookstack } = environment;
|
||||
if (!bookstack.url || !isValidServiceUrl(bookstack.url)) {
|
||||
const config = await toolConfigService.getBookstackConfig();
|
||||
if (!config.url || !isValidServiceUrl(config.url)) {
|
||||
throw new Error('BOOKSTACK_URL is not configured or is not a valid service URL');
|
||||
}
|
||||
|
||||
try {
|
||||
const [response, bookSlugMap] = await Promise.all([
|
||||
httpClient.get(
|
||||
`${bookstack.url}/api/pages/${id}`,
|
||||
{ headers: buildHeaders() },
|
||||
`${config.url}/api/pages/${id}`,
|
||||
{ headers: buildHeaders(config) },
|
||||
),
|
||||
getBookSlugMap(),
|
||||
]);
|
||||
@@ -226,7 +225,7 @@ async function getPageById(id: number): Promise<BookStackPageDetail> {
|
||||
html: page.html ?? '',
|
||||
created_at: page.created_at,
|
||||
updated_at: page.updated_at,
|
||||
url: `${bookstack.url}/books/${bookSlug}/page/${page.slug}`,
|
||||
url: `${config.url}/books/${bookSlug}/page/${page.slug}`,
|
||||
book: page.book,
|
||||
createdBy: page.created_by,
|
||||
updatedBy: page.updated_by,
|
||||
|
||||
Reference in New Issue
Block a user