Files
penpot/plugins/libs/plugins-runtime/src/lib/parse-manifest.ts
Andrey Antukh ec1af4ad96 🎉 Import penpot-plugins repository
As commit 819a549e4928d2b1fa98e52bee82d59aec0f70d8
2025-12-30 14:56:15 +01:00

39 lines
1.0 KiB
TypeScript

import { Manifest } from './models/manifest.model.js';
import { manifestSchema } from './models/manifest.schema.js';
export function getValidUrl(host: string, path: string): string {
return new URL(path, host).toString();
}
export function loadManifest(url: string): Promise<Manifest> {
return fetch(url)
.then((response) => response.json())
.then((manifest: Manifest): Manifest => {
const parseResult = manifestSchema.safeParse(manifest);
if (!parseResult.success) {
throw new Error('Invalid plugin manifest');
}
return manifest;
})
.catch((error) => {
console.error(error);
throw error;
});
}
export function loadManifestCode(manifest: Manifest): Promise<string> {
if (!manifest.host && !manifest.code.startsWith('http')) {
return Promise.resolve(manifest.code);
}
return fetch(getValidUrl(manifest.host, manifest.code)).then((response) => {
if (response.ok) {
return response.text();
}
throw new Error('Failed to load plugin code');
});
}