query catalog file instead of gh api

This commit is contained in:
7w1 committed 2026-04-02 15:22:07 -05:00
1 parent c31a298965
commit f60245b658
4 files changed
+199 -38

No files matched your search

@@ -60,6 +60,7 @@ export function ThemeCatalogSettings({
const patchSettings = usePatchSettings();
const configBase = clientConfig.themeCatalogBaseUrl?.trim();
const catalogBase = themeCatalogListingBaseUrl(configBase);
const catalogManifestUrl = clientConfig.themeCatalogManifestUrl?.trim() || undefined;
const isAppearanceMode = mode === 'appearance';
const [browseOpen, setBrowseOpen] = useState(false);
@@ -125,8 +126,8 @@ export function ThemeCatalogSettings({
);
const pairsQuery = useQuery({
queryKey: ['theme-catalog-pairs', catalogBase],
queryFn: () => listThemePairsFromCatalog(catalogBase),
queryKey: ['theme-catalog-pairs', catalogBase, catalogManifestUrl ?? ''],
queryFn: () => listThemePairsFromCatalog(catalogBase, { manifestUrl: catalogManifestUrl }),
enabled: isRemoteMode,
staleTime: 5 * 60_000,
});
+1
View File
@@ -45,6 +45,7 @@ export type ClientConfig = {
settingsLinkBaseUrl?: string;
themeCatalogBaseUrl?: string;
themeCatalogManifestUrl?: string;
themeCatalogApprovedHostPrefixes?: string[];
};
+132 -35
View File
@@ -1,5 +1,24 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { listThemePairsFromCatalog } from './catalog';
import { listThemePairsFromCatalog, themeCatalogManifestUrlFromBase } from './catalog';
describe('themeCatalogManifestUrlFromBase', () => {
it('resolves catalog.json beside raw root', () => {
expect(
themeCatalogManifestUrlFromBase('https://raw.githubusercontent.com/SableClient/themes/main/')
).toBe('https://raw.githubusercontent.com/SableClient/themes/main/catalog.json');
expect(
themeCatalogManifestUrlFromBase('https://raw.githubusercontent.com/SableClient/themes/main')
).toBe('https://raw.githubusercontent.com/SableClient/themes/main/catalog.json');
});
it('resolves under nested path', () => {
expect(
themeCatalogManifestUrlFromBase(
'https://raw.githubusercontent.com/SableClient/themes/main/dist/themes/'
)
).toBe('https://raw.githubusercontent.com/SableClient/themes/main/dist/themes/catalog.json');
});
});
describe('listThemePairsFromCatalog', () => {
const origFetch = globalThis.fetch;
@@ -8,67 +27,145 @@ describe('listThemePairsFromCatalog', () => {
globalThis.fetch = origFetch;
});
it('pairs preview and full theme files from GitHub contents response', async () => {
it('loads pairs from catalog.json manifest when present', async () => {
const base = 'https://raw.githubusercontent.com/SableClient/themes/main/';
globalThis.fetch = vi.fn().mockResolvedValue({
ok: true,
json: async () => [
{
name: 'rose-pine.preview.sable.css',
path: 'rose-pine.preview.sable.css',
type: 'file',
download_url:
'https://raw.githubusercontent.com/SableClient/themes/main/rose-pine.preview.sable.css',
},
{
name: 'rose-pine.sable.css',
path: 'rose-pine.sable.css',
type: 'file',
download_url:
'https://raw.githubusercontent.com/SableClient/themes/main/rose-pine.sable.css',
},
{
name: 'README.md',
path: 'README.md',
type: 'file',
download_url: null,
},
],
json: async () => ({
version: 1,
themes: [
{
basename: 'rose-pine',
previewUrl:
'https://raw.githubusercontent.com/SableClient/themes/main/rose-pine.preview.sable.css',
fullUrl:
'https://raw.githubusercontent.com/SableClient/themes/main/rose-pine.sable.css',
},
],
}),
});
const pairs = await listThemePairsFromCatalog(base);
expect(pairs).toHaveLength(1);
expect(pairs[0].basename).toBe('rose-pine');
expect(globalThis.fetch).toHaveBeenCalledTimes(1);
expect(globalThis.fetch).toHaveBeenCalledWith(
'https://raw.githubusercontent.com/SableClient/themes/main/catalog.json',
expect.objectContaining({ mode: 'cors' })
);
});
it('uses custom manifest URL when provided', async () => {
globalThis.fetch = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({
themes: [
{
basename: 'x',
previewUrl: 'https://cdn.example/p.preview.sable.css',
fullUrl: 'https://cdn.example/p.sable.css',
},
],
}),
});
const pairs = await listThemePairsFromCatalog(
'https://raw.githubusercontent.com/SableClient/themes/main/',
{
manifestUrl: 'https://pages.example.com/catalog.json',
}
);
expect(pairs[0].basename).toBe('x');
expect(globalThis.fetch).toHaveBeenCalledWith(
'https://pages.example.com/catalog.json',
expect.anything()
);
});
it('returns empty array when manifest is valid but themes is empty', async () => {
globalThis.fetch = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({ version: 1, themes: [] }),
});
const pairs = await listThemePairsFromCatalog(
'https://raw.githubusercontent.com/SableClient/themes/main/'
);
expect(pairs).toEqual([]);
expect(globalThis.fetch).toHaveBeenCalledTimes(1);
});
it('falls back to GitHub contents API when manifest fetch fails', async () => {
globalThis.fetch = vi
.fn()
.mockResolvedValueOnce({ ok: false, status: 404 })
.mockResolvedValueOnce({
ok: true,
json: async () => [
{
name: 'rose-pine.preview.sable.css',
path: 'rose-pine.preview.sable.css',
type: 'file',
download_url:
'https://raw.githubusercontent.com/SableClient/themes/main/rose-pine.preview.sable.css',
},
{
name: 'rose-pine.sable.css',
path: 'rose-pine.sable.css',
type: 'file',
download_url:
'https://raw.githubusercontent.com/SableClient/themes/main/rose-pine.sable.css',
},
],
});
const base = 'https://raw.githubusercontent.com/SableClient/themes/main/';
const pairs = await listThemePairsFromCatalog(base);
expect(pairs).toHaveLength(1);
expect(pairs[0].basename).toBe('rose-pine');
expect(pairs[0].fullUrl).toContain('rose-pine.sable.css');
expect(pairs[0].previewUrl).toContain('rose-pine.preview.sable.css');
expect(globalThis.fetch).toHaveBeenCalledWith(
'https://raw.githubusercontent.com/SableClient/themes/main/catalog.json',
expect.objectContaining({ mode: 'cors' })
);
expect(globalThis.fetch).toHaveBeenCalledWith(
'https://api.github.com/repos/SableClient/themes/contents?ref=main',
expect.objectContaining({ headers: { Accept: 'application/vnd.github+json' } })
);
});
it('requests nested directory when catalog URL includes a path', async () => {
globalThis.fetch = vi.fn().mockResolvedValue({
ok: true,
json: async () => [],
});
it('requests nested directory when catalog URL includes a path and manifest is missing', async () => {
globalThis.fetch = vi
.fn()
.mockResolvedValueOnce({ ok: false, status: 404 })
.mockResolvedValueOnce({
ok: true,
json: async () => [],
});
await listThemePairsFromCatalog(
'https://raw.githubusercontent.com/SableClient/themes/main/dist/themes/'
);
expect(globalThis.fetch).toHaveBeenCalledWith(
'https://raw.githubusercontent.com/SableClient/themes/main/dist/themes/catalog.json',
expect.anything()
);
expect(globalThis.fetch).toHaveBeenCalledWith(
'https://api.github.com/repos/SableClient/themes/contents/dist/themes?ref=main',
expect.anything()
);
});
it('returns empty array when base URL is not a raw GitHub URL', async () => {
globalThis.fetch = vi.fn();
it('returns empty array when base URL is not a raw GitHub URL and manifest fails', async () => {
globalThis.fetch = vi.fn().mockResolvedValue({ ok: false, status: 404 });
const pairs = await listThemePairsFromCatalog('https://example.com/');
expect(pairs).toEqual([]);
expect(globalThis.fetch).not.toHaveBeenCalled();
expect(globalThis.fetch).toHaveBeenCalledWith(
'https://example.com/catalog.json',
expect.anything()
);
});
});
+63 -1
View File
@@ -13,9 +13,61 @@ export type ThemePair = {
fullUrl: string;
};
export type ThemeCatalogManifest = {
version?: number;
themes?: ThemePair[];
};
export type ListThemeCatalogOptions = {
manifestUrl?: string | null;
};
const PREVIEW_SUFFIX = '.preview.sable.css';
const FULL_SUFFIX = '.sable.css';
export function themeCatalogManifestUrlFromBase(catalogBaseUrl: string): string | null {
const trimmed = catalogBaseUrl.trim();
if (!trimmed) return null;
const base = trimmed.endsWith('/') ? trimmed : `${trimmed}/`;
try {
return new URL('catalog.json', base).href;
} catch {
return null;
}
}
async function fetchThemePairsFromManifest(manifestUrl: string): Promise<ThemePair[] | null> {
const res = await fetch(manifestUrl, { mode: 'cors' });
if (!res.ok) return null;
let data: unknown;
try {
data = await res.json();
} catch {
return null;
}
if (!data || typeof data !== 'object') return null;
const themes = (data as ThemeCatalogManifest).themes;
if (!Array.isArray(themes)) return null;
const pairs: ThemePair[] = [];
for (const row of themes) {
if (!row || typeof row !== 'object') continue;
const o = row as Record<string, unknown>;
const basename = o.basename;
const previewUrl = o.previewUrl;
const fullUrl = o.fullUrl;
if (
typeof basename !== 'string' ||
typeof previewUrl !== 'string' ||
typeof fullUrl !== 'string'
) {
continue;
}
if (!basename || !previewUrl || !fullUrl) continue;
pairs.push({ basename, previewUrl, fullUrl });
}
return pairs;
}
/** Path segments for GET /repos/.../contents/{path} */
function directoryPathToApiSegment(directoryPath: string): string {
if (!directoryPath) return '';
@@ -40,7 +92,17 @@ async function fetchGithubContents(parts: GithubRawParts): Promise<GithubContent
return Array.isArray(data) ? data : [data];
}
export async function listThemePairsFromCatalog(baseUrl: string): Promise<ThemePair[]> {
export async function listThemePairsFromCatalog(
baseUrl: string,
options?: ListThemeCatalogOptions
): Promise<ThemePair[]> {
const manifestUrl =
options?.manifestUrl?.trim() || themeCatalogManifestUrlFromBase(baseUrl) || undefined;
if (manifestUrl) {
const fromManifest = await fetchThemePairsFromManifest(manifestUrl);
if (fromManifest !== null) return fromManifest;
}
const parts = parseGithubRawBaseUrl(baseUrl);
if (!parts) return [];
const items = await fetchGithubContents(parts);