mirror of
https://github.com/penpot/penpot.git
synced 2026-03-16 23:46:26 +00:00
39 lines
1.0 KiB
TypeScript
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');
|
|
});
|
|
}
|